日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python多进程读图提取特征存npy

發(fā)布時間:2023/12/20 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python多进程读图提取特征存npy 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
import multiprocessing import os, time, random import numpy as np import cv2 import os import sys from time import ctime import tensorflow as tfimage_dir = r"D:/sxl/處理圖片/漢字分類/train10/" #圖像文件夾路徑 data_type = 'test' save_path = r'E:/sxl_Programs/Python/CNN/npy/' #存儲路徑 data_name = 'Img10' #npy文件名char_set = np.array(os.listdir(image_dir)) #文件夾名稱列表 np.save(save_path+'ImgShuZi10.npy',char_set) #文件夾名稱列表 char_set_n = len(char_set) #文件夾列表長度read_process_n = 1 #進程數(shù) repate_n = 4 #隨機移動次數(shù) 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)# print(file_path)# print(cv_img.shape)# print(len(cv_img.shape))if(type==0):if(len(cv_img.shape)==3):cv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2GRAY)return cv_img#多個數(shù)組按同一規(guī)則打亂數(shù)據(jù) def ShuffledData(features,labels):'''@description:隨機打亂數(shù)據(jù)與標簽,但保持數(shù)據(jù)與標簽一一對應(yīng)'''permutation = np.random.permutation(features.shape[0])shuffled_features = features[permutation,:] #多維shuffled_labels = labels[permutation] #1維return shuffled_features,shuffled_labels#函數(shù)功能:簡單網(wǎng)格 #函數(shù)要求:1.無關(guān)圖像大小;2.輸入圖像默認為灰度圖;3.參數(shù)只有輸入圖像 #返回數(shù)據(jù):1x64*64維特征 def GetFeature(image):#圖像大小歸一化image = cv2.resize(image,(64,64))img_h = image.shape[0]img_w = image.shape[1]#定義特征向量feature = np.zeros(img_h*img_w,dtype=np.int16)for h in range(img_h):for w in range(img_w):feature[h*img_h+w] = image[h,w]return feature# 寫數(shù)據(jù)進程執(zhí)行的代碼: def read_image_to_queue(queue):print('Process to write: %s' % os.getpid())for j,dirname in enumerate(char_set): # dirname 是文件夾名稱label = np.where(char_set==dirname)[0][0] #文件夾名稱對應(yīng)的下標序號print('序號:'+str(j),'讀 '+dirname+' 文件夾...時間:',ctime() )for parent,_,filenames in os.walk(os.path.join(image_dir,dirname)):for filename in filenames:if(filename[-4:]!='.jpg'):continueimage = cv_imread(os.path.join(parent,filename),0)# cv2.imshow(dirname,image)# cv2.waitKey(0)queue.put((image,label))for i in range(read_process_n):queue.put((None,-1))print('讀圖結(jié)束!')return True# 讀數(shù)據(jù)進程執(zhí)行的代碼: def extract_feature(queue,lock,count):'''@description:從隊列中取出圖片進行特征提取@queue:先進先出隊列l(wèi)ock:鎖,在計數(shù)時上鎖,防止沖突count:計數(shù)'''print('Process %s start reading...' % os.getpid())global data_nfeatures = [] #存放提取到的特征labels = [] #存放標簽flag = True #標志著進程是否結(jié)束while flag:image,label = queue.get() #從隊列中獲取圖像和標簽if len(features) >= data_size or label == -1: #特征數(shù)組的長度大于指定長度,則開始存儲array_features = np.array(features) #轉(zhuǎn)換成數(shù)組array_labels = np.array(labels)array_features,array_labels = ShuffledData(array_features,array_labels) #打亂數(shù)據(jù)lock.acquire() # 鎖開始# 拆分數(shù)據(jù)為訓(xùn)練集,測試集split_x = int(array_features.shape[0] * 0.8)train_data, test_data = np.split(array_features, [split_x], axis=0) # 拆分特征數(shù)據(jù)集train_labels, test_labels = np.split(array_labels, [split_x], axis=0) # 拆分標簽數(shù)據(jù)集count.value += 1 #下標計數(shù)加1str_features_name_train = data_name+'_features_train_'+str(count.value)+'.npy'str_labels_name_train = data_name+'_labels_train_'+str(count.value)+'.npy'str_features_name_test = data_name+'_features_test_'+str(count.value)+'.npy'str_labels_name_test = data_name+'_labels_test_'+str(count.value)+'.npy'lock.release() # 鎖釋放np.save(save_path+str_features_name_train,train_data)np.save(save_path+str_labels_name_train,train_labels)np.save(save_path+str_features_name_test,test_data)np.save(save_path+str_labels_name_test,test_labels)print(os.getpid(),'save:',str_features_name_train)print(os.getpid(),'save:',str_labels_name_train)print(os.getpid(),'save:',str_features_name_test)print(os.getpid(),'save:',str_labels_name_test)features.clear()labels.clear()if label == -1:break# 獲取特征向量,傳入灰度圖feature = GetFeature(image)features.append(feature)labels.append(label)# # 隨機移動4次# for itime in range(repate_n):# rMovedImage = randomMoveImage(image)# feature = SimpleGridFeature(rMovedImage) # 簡單網(wǎng)格# features.append(feature)# labels.append(label)print('Process %s is done!' % os.getpid())if __name__=='__main__':time_start = time.time() # 開始計時# 父進程創(chuàng)建Queue,并傳給各個子進程:image_queue = multiprocessing.Queue(maxsize=1000) #隊列l(wèi)ock = multiprocessing.Lock() #鎖count = multiprocessing.Value('i',0) #計數(shù)#將圖寫入隊列進程write_sub_process = multiprocessing.Process(target=read_image_to_queue, args=(image_queue,))read_sub_processes = [] #讀圖子線程for i in range(read_process_n):read_sub_processes.append(multiprocessing.Process(target=extract_feature, args=(image_queue,lock,count)))# 啟動子進程pw,寫入:write_sub_process.start()# 啟動子進程pr,讀取:for p in read_sub_processes:p.start()# 等待進程結(jié)束:write_sub_process.join()for p in read_sub_processes:p.join()time_end=time.time()time_h=(time_end-time_start)/3600print('用時:%.6f 小時'% time_h)print ("讀圖提取特征存npy,運行結(jié)束!")

總結(jié)

以上是生活随笔為你收集整理的Python多进程读图提取特征存npy的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。