如何检测 Yolo 模型中的特定检测对象
how to detect a particular detected object in Yolo model
我正在使用 Yolor 检测多个对象,然后裁剪检测到的特定对象并存储在单独的文件夹中。我能够裁剪每个检测到的对象,但我对如何仅裁剪特定检测到的对象感到困惑。
下面是裁剪检测到的图像的代码:
save_obj 函数保存裁剪后的图像。
# 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
我正在使用 Yolor 检测多个对象,然后裁剪检测到的特定对象并存储在单独的文件夹中。我能够裁剪每个检测到的对象,但我对如何仅裁剪特定检测到的对象感到困惑。
下面是裁剪检测到的图像的代码: save_obj 函数保存裁剪后的图像。
# 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