1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
import os import cv2 import numpy as np import yolox_predict
def classify(dir): dict={} for fileName in os.listdir(dir): filePath=os.path.join(dir,fileName) img=cv2.imdecode(np.fromfile(filePath,dtype=np.uint8),-1) x,y=img.shape[:2] feature = ''.join(str(i) for i in img[0,0])+''.join(str(i) for i in img[0,y-1])+''.join(str(i) for i in img[x-1,0])+''.join(str(i) for i in img[x-1,y-1]) print(feature) if dict.__contains__(str(feature)): dict[str(feature)].append(fileName) else: dict[str(feature)]=[fileName] return dict
inputDir="C:/Users/Administrator/Desktop/test" outputDir="C:/Users/Administrator/Desktop/result" DICT=classify(inputDir) print(DICT) print('分类完成...') predictor,current_time= yolox_predict.init() index=0 for key,value in DICT.items(): imgs=[] rects=[] for fileName in value: filePath=os.path.join(inputDir,fileName) rects=yolox_predict.image_demo(predictor, vis_folder=outputDir, path=filePath, current_time=current_time) print(filePath+'预测完成...') img=cv2.imdecode(np.fromfile(filePath,dtype=np.uint8),-1) for rect in rects: x1,y1,x2,y2=rect img[y1:y2,x1:x2]=[0,0,0] imgs.append(img) if len(imgs)<5: continue for rect in rects: x1,y1,x2,y2=rect for x in range(x1,x2): for y in range(y1,y2): for img in imgs[:-1]: if sum(img[y,x])>0: imgs[-1][y,x]=img[y,x] cv2.imwrite(os.path.join(outputDir,str(index)+'.jpg') ,imgs[-1]) print('合成背景图'+os.path.join(outputDir,str(index)+'.jpg')) index+=1
|