python中多进程读图提取特征存npy的示例分析

  介绍

这篇文章将为大家详细讲解有关python中多进程读图提取特征存npy的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

具体内容如下

import 多处理   import 操作系统,,时间,随机   import  numpy  as  np   import  cv2   import 操作系统   import 系统   得到time  import  ctime   import  tensorflow  as  tf   ,   image_dir =, r" D:/sxl处理图片/汉字分类/train10/,,, #图像文件夹路径   时间=data_type  & # 39;测试# 39;   save_path =, " # 39; E:/sxl_Programs/Python/CNN/npy & # 39;,, #存储路径   时间=data_name  & # 39; Img10& # 39;,,,,,,,, # npy文件名   ,   时间=char_set  np.array (os.listdir (image_dir)),,, #文件夹名称列表   np.save (save_path + & # 39; ImgShuZi10.npy& # 39;, char_set),,, #文件夹名称列表   char_set_n =, len (char_set),,,,,,, #文件夹列表长度   ,   时间=read_process_n  1, #进程数   时间=repate_n  4,,, #随机移动次数   时间=data_size  1000000, # 1个npy大小   ,   时间=shuffled  True , #是否打乱   ,   #可以读取带中文路径的图   def  cv_imread (file_path type=0):   ,cv_img=cv2.imdecode (np.fromfile file_path dtype=np.uint8) (1)   ,#打印(file_path)   ,#打印(cv_img.shape)   ,# print (len (cv_img.shape))   ,如果(type==0):   如果才能(len (cv_img.shape)==3):   ,,cv_img =, cv2.cvtColor (cv_img, cv2.COLOR_BGR2GRAY)   return  cv_img   ,   #多个数组按同一规则打乱数据   def  ShuffledData(特性、标签):   ,& # 39;& # 39;& # 39;   ,@description:随机打乱数据与标签,但保持数据与标签一一对应   ,& # 39;& # 39;& # 39;=,,permutation  np.random.permutation (features.shape [0])=,shuffled_features 特性(排列:),#多维=,shuffled_labels 标签(排列),# 1维   return  shuffled_features, shuffled_labels   ,   #函数功能:简单网格   #函数要求:1。无关图像大小;2。输入图像默认为灰度图;3。参数只有输入图像   #返回数据:1 x64 * 64维特征   def  GetFeature(图片):   ,   ,#图像大小归一化=,,image  cv2.resize(图片,(64、64))=,,img_h  image.shape [0]=,,img_w  image.shape [1]   ,   ,#定义特征向量=,,feature  np.zeros (img_h * img_w dtype=np.int16)   ,   ,for  h 拷贝范围(img_h):   for 才能;w 拷贝范围(img_w):   ,,功能(h * img_h + w),=,图像(h, w)   ,   return 特性   ,   #,写数据进程执行的代码:   def  read_image_to_queue(队列):   ,打印(& # 39;Process 用写:,% & # 39;,%,os.getpid ())   ,for  j, dirname 列举(char_set):拷贝,#,dirname 是文件夹名称   时间=label 才能;np.where (char_set==目录名)[0][0],,#文件夹名称对应的下标序号   打印才能(& # 39;序号:& # 39;+ str (j) & # 39;读,& # 39;+目录名+ & # 39;,文件夹…时间:& # 39;,ctime (),)   for 才能母公司_ filenames 拷贝os.walk (os.path.join (image_dir,目录名):   ,,for  filename 拷贝文件名:   ,,,如果(文件名[4:]!=& # 39;jpg # 39;):   ,,,,继续   ,,,image =, cv_imread (os.path.join(父母,文件名),0)   ,   ,,,#,cv2.imshow(目录名、图片)   ,,,#,cv2.waitKey (0)   ,,,queue.put((图片,标签))   ,   ,for 小姐:拷贝范围(read_process_n):   queue.put才能((没有,1))   ,   ,打印(& # 39;读图结束! & # 39;)   return 真正的   ,,   #,读数据进程执行的代码:   def  extract_feature(队列,锁,计数):   ,& # 39;& # 39;& # 39;   ,@description:从队列中取出图片进行特征提取   ,@queue:先进先出队列   锁:锁,才能在计数时上的锁,防止冲突   数:才能计数   ,& # 39;& # 39;& # 39;   ,   ,打印(& # 39;Process  % s  start 阅读…& # 39;,%,os.getpid ())   ,   global  data_n=,features [], #存放提取到的特征=,labels [], #存放标签=,,flag  True  #标志着进程是否结束   ,while 国旗:   形象,才能label =, queue.get(), #从队列中获取图像和标签   ,   if 才能;len(特性),在=,data_size 或是label ==, 1:, #特征数组的长度大于指定长度,则开始存储   ,   ,,array_features =, np.array(特性),#转换成数组   ,,array_labels =, np.array(标签)   ,   ,,array_features array_labels =, ShuffledData (array_features array_labels), #打乱数据   ,,   ,,lock.acquire(), #,锁开始   ,   ,,#,拆分数据为训练集,测试集   ,,split_x =, int (array_features.shape [0], *, 0.8)   ,,,train_data, test_data =, np.split (array_features,, (split_x),轴=0),,#,拆分特征数据集   ,,,train_labels, test_labels =, np.split (array_labels,, (split_x),轴=0),#,拆分标签数据集   ,   ,,count.value  +=, 1, #下标计数加1   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

python中多进程读图提取特征存npy的示例分析