之前写了一篇【杂七杂八的代码】【二】python标注自己的目标检测数据集——yolo(darknet)格式(一),是针对单帧单类别多目标的目标检测数据标注的。由于我自己的数据是一段段的连续视频帧,因此其实可以加入目标跟踪,实现半自动的标注,可以省不少的精力。(顺利的话1小时上千张没啥问题)
代码
import os
import cv2
global img
global point1, point2
global g_rect
global have_point1
def on_mouse(event, x, y, flags, param):
global img, point1, point2, g_rect, have_point1
img2 = img.copy()
if event == cv2.EVENT_LBUTTONDOWN:
# print("1-EVENT_LBUTTONDOWN")
if not have_point1:
point1 = (x, y)
have_point1 = True
else:
point2 = (x, y)
have_point1 = False
cv2.rectangle(img2, point1, point2, (0, 120, 255), thickness=1)
cv2.imshow('image', img2)
def get_image_roi(bgr_image):
global img
global have_point1
img = bgr_image
have_point1 = False
img2_h, img2_w, _ = img.shape
cv2.namedWindow('image')
while True:
cv2.setMouseCallback('image', on_mouse)
cv2.imshow('image', img)
key = cv2.waitKey(0)
if key == ord('s') and point1 != point2: # 按s,保存、退出
x_center = 0.5 * (point1[0] + point2[0]) / img_w
y_center =