zoukankan      html  css  js  c++  java
  • 加载图像数据集

    #path:数据路径
    #txt:每条数据对应的标签
     1 def generateds(path, txt):
     2     f = open(txt, 'r')  # 以只读形式打开txt文件
     3     contents = f.readlines()  # 读取文件中所有行
     4     f.close()  # 关闭txt文件
     5     x, y_ = [], []  # 建立空列表
     6     for content in contents:  # 逐行取出
     7         value = content.split()  # 以空格分开,图片路径为value[0] , 标签为value[1] , 存入列表
     8         img_path = path + value[0]+' '+value[1]  # 拼出图片路径和文件名
     9         print(img_path)
    10         # img = Image.open(img_path)  # 读入图片
    11         # img1 = np.array(img.convert('L'))  # 图片变为8位宽灰度值的np.array格式
    12         img=cv2.imread('img_path',0)
    13         img = np.array(img, dtype =float) ##Mat转为numpy
    14         img = img / 255.  # 数据归一化 (实现预处理)
    15         x.append(img)  # 归一化后的数据,贴到列表x
    16         print(value[2])
    17         y_.append(value[2])  # 标签贴到列表y_
    18         print('loading : ' + content)  # 打印状态提示
    19     x = np.array(x)  # 变为np.array格式
    20     y_ = np.array(y_)  # 变为np.array格式
    21     y_ = y_.astype(np.int64)  # 变为64位整型
    22     return x, y_  # 返回输入特征x,返回标签y_
  • 相关阅读:
    Leetcode(337)-打家劫舍III
    Leetcode(213)-打家劫舍II
    Leetcode(198)-打家劫舍
    Leetcode(32)-最长有效括号
    计数排序
    智能指针
    C++中的explicit
    Leetcode(878)-第 N 个神奇数字
    Leetcode(877)-石子游戏
    C++的memset
  • 原文地址:https://www.cnblogs.com/hsy1941/p/12942844.html
Copyright © 2011-2022 走看看