学生
图片为普通jpg图片
txt文件格式为:
person 0.95 396 173 754 481
car 0.99 811 252 1009 411
importnumpyimportcv2importosimg_root=r"F;\Code\dataset\Imagestrain"txt_root=r"F:\Code\input\detection-results"save_path=r"F:\Code\ssd_pytorch\input\image_kuang"txt_list=os.listdir(txt_root)fornameintxt_list:txt_path=os.path.join(txt_root,name)img_name=name[:-3]+'jpg'# 去掉'txt'+‘jpg’image_path=os.path.join(img_root,img_name)img=cv2.imread(image_path)withopen(txt_path)asf:lines=f.readlins()forlineinlines:class_name=line.split(" ")[0]score=float(line.split(" ")[1])bbox=line.split(" ")[2:]bbox=list(map(int,bbox))ifclass_name=="carhead"andscore>=0.3img_kuang=cv2.rectangle(img,bbox[0],bbox[1],bbox[1],bbox[2],bbox[3],(0,0,255),2)cv2,imshow("img",img_kuang)cv2.waitKey(1)cv2.imwrite(save_path+'\\'+image_name,img_kuang)ifclass_name=="cartail"andscore>=0.3img_kuang=cv2.rectangle(img,bbox[0],bbox[1],bbox[1],bbox[2],bbox[3],(0,255,0),2)cv2,imshow("img",img_kuang)cv2.imwrite(save_path+'\\'+image_name,img_kuan