一、前言
之前的一篇文章在用SSD進行行人檢測是重新對coco數據集中的人標籤進行訓練,但是精度和速度都比yolov3差一點,應該是訓練的原因,這裏將基於yolov3tennsorflow版實現單目標檢測(Keras版可同理進行更改)。
二、準備
準備將yolov3目標檢測項目下載下來,進入項目的core文件夾將utils.py文件中的draw_bbox函數更改成下面這樣
def draw_bbox(image, bboxes, classes=read_class_names(cfg.YOLO.CLASSES), show_label=True):
"""
bboxes: [x_min, y_min, x_max, y_max, probability, cls_id] format coordinates.
"""
num_classes = len(classes)
image_h, image_w, _ = image.shape
hsv_tuples = [(1.0 * x / num_classes, 1., 1.) for x in range(num_classes)]
colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
colors = list(map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), colors))
random.seed(0)
random.shuffle(colors)
random.seed(None)
for i, bbox in enumerate(bboxes):
coor = np.array(bbox[:4], dtype=np.int32)
fontScale = 0.5
score = bbox[4]
class_ind = int(bbox[5])
# print('ind',class_ind)
bbox_color = colors[class_ind]
bbox_thick = int(0.6 * (image_h + image_w) / 600)
c1, c2 = (coor[0], coor[1]), (coor[2], coor[3])
#cv2.rectangle(image, c1, c2, bbox_color, bbox_thick)
if show_label and class_ind == 0: # 0 只檢測人 1 自行車 2 汽車 可根據coconame 進行單目標檢查
#if show_label:
bbox_mess = '%s: %.2f' % (classes[class_ind], score)
t_size = cv2.getTextSize(bbox_mess, 0, fontScale, thickness=bbox_thick//2)[0]
cv2.rectangle(image, c1, (c1[0] + t_size[0], c1[1] - t_size[1] - 3), bbox_color, -1) # filled
cv2.putText(image, bbox_mess, (c1[0], c1[1]-2), cv2.FONT_HERSHEY_SIMPLEX,
fontScale, (0, 0, 0), bbox_thick//2, lineType=cv2.LINE_AA)
# cv2.rectangle(image, c1, c2, bbox_color, bbox_thick)
return image
三、效果
之前:
之後: