notebooks/ssd_ notebook.py(或者notebooks/ssd_ notebook.ipynb)文件中有一段函数
rclasses, rscores, rbboxes = process_image(frame)
rbboxes是一个<numpy.ndarray>格式,
里面是二维数组
[[0.37487125 0.40014353 0.6358912 0.51521194]] (这里示例是一个目标的)
读出这个目标的信息:
rbboxes[0]: y1 x1 y2 x2 (注意这里的信息是正则化之后的0-1之间)
box[0] box[1] box[2] box[3]
cv2的画框函数
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)cv2.rectangle(frame, (int(box[1]), int(box[0])), (int(box[3]), int(box[2])),
(242, 133, 74), 2)
从正则化结果中变换为真实框的大小
frame.shape[0] 是frame图片的高度
# print(frame.shape) [0]高1080 [1]宽1440
for box in rbboxes:
box[0] = box[0] * frame.shape[0]
box[1] = box[1] * frame.shape[1]
box[2] = box[2] * frame.shape[0]
box[3] = box[3] * frame.shape[1]
if __name__ == '__main__':
pic_vid_flag = False
# 1 如果是检测图片True视频False
if pic_vid_flag:
# Test on some demo image and visualize output.
path = '../demo/'
image_names = sorted(os.listdir(path))
print('image_names', image_names)
img = mpimg.imread(path + image_names[0])
rclasses, rscores, rbboxes = process_image(img) # img
# visualization.bboxes_draw_on_img(img, rclasses, rscores, rbboxes, visualization.colors_plasma)
visualization.plt_bboxes(img, rclasses, rscores, rbboxes)
# 2 如果是检测视频False, 运行下面部分
else:
video_capture = cv2.VideoCapture(
'/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_ssd/SSD-Tensorflow-master/zjc.avi')
while True:
ret, frame = video_capture.read() # frame shape 640*480*3
# print(frame.shape) [0]高1080 [1]宽1440
# print('图片的长宽:', frame.shape) 高宽(480, 640, 3)
if ret is False:
break
rclasses, rscores, rbboxes = process_image(frame) # TODO
# visualization.plt_bboxes(frame, rclasses, rscores, rbboxes)
if len(rbboxes) > 0:
for box in rbboxes:
box[0] = box[0] * frame.shape[0]
box[1] = box[1] * frame.shape[1]
box[2] = box[2] * frame.shape[0]
box[3] = box[3] * frame.shape[1]
'''
rbboxes: y1 x1 -h2 w2
box[0] box[1] box[2] box[3]
cv3.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
'''
cv2.rectangle(frame, (int(box[1]), int(box[0])), (int(box[3]), int(box[2])),
(242, 133, 74), 2)
print('yx-hw', int(box[0]), int(box[1]), int(box[0] + box[2]), int(box[0] + box[3]))
cv2.imshow('', frame)
# Press Q to stop!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放与关闭
video_capture.release()
cv2.destroyAllWindows()