[英]how to detect a particular detected object in Yolo model
我正在使用 Yolor 檢測多個對象,然后裁剪檢測到的特定對象 object 並存儲在單獨的文件夾中。 我能夠裁剪每個檢測到的 object,但我對如何僅裁剪特定檢測到的 object 感到困惑。
下面是裁剪檢測到的圖像的代碼: save_obj function 保存裁剪后的圖像。
# Write results
k = 0
for *xyxy, conf, cls in det:
if save_txt: # Write to file
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
with open(txt_path + '.txt', 'a') as f:
f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format
if save_img or view_img: # Add bbox to image
label = '%s %.2f' % (names[int(cls)], conf)
plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)
if save_obj:
x,y,w,h=int(xyxy[0]), int(xyxy[1]), int(xyxy[2] - xyxy[0]), int(xyxy[3] - xyxy[1])
img_ = im1.astype(np.uint8)
crop_img=img_[y:y+ h, x:x + w]
#!!rescale image !!!
filename=p.name
filename_no_extesion=filename.split('.')[0]
extension=filename.split('.')[1]
new_filename=str(filename_no_extesion) + '_' + str(k) + '.' + str(extension)
dir_path=os.path.join('/mydrive/yolor/','cropped')
filepath=os.path.join(dir_path, new_filename)
print(filepath)
cv2.imwrite(filepath, crop_img)
k=k+1
我希望這有幫助
# Write results
k = 0
for *xyxy, conf, cls in det:
if save_txt: # Write to file
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
with open(txt_path + '.txt', 'a') as f:
f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format
if save_img or view_img: # Add bbox to image
label = '%s %.2f' % (names[int(cls)], conf)
plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)
if save_obj and names[int(cls)]=='xyz_class':
x,y,w,h=int(xyxy[0]), int(xyxy[1]), int(xyxy[2] - xyxy[0]), int(xyxy[3] - xyxy[1])
img_ = im1.astype(np.uint8)
crop_img=img_[y:y+ h, x:x + w]
#!!rescale image !!!
filename=p.name
filename_no_extesion=filename.split('.')[0]
extension=filename.split('.')[1]
new_filename=str(filename_no_extesion) + '_' + str(k) + '.' + str(extension)
dir_path=os.path.join('/mydrive/yolor/','cropped')
filepath=os.path.join(dir_path, new_filename)
print(filepath)
cv2.imwrite(filepath, crop_img)
k=k+1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.