正在实现从list中读取要画框的位置,然后根据位置在原图像上制矩形,源代码如下:
for location in faces:
x = location['left']
y = location['top']
w = location['width']
h = location['height']
user_id = location['user_id']
if user_id:
# 在原图像上绘制绿色矩形标识
cv2.rectangle(img=frame, pt1=(x, y), pt2=(x + w, y + h), color=(0, 255, 0), thickness=2)
else:
# 在原图像上绘制红色矩形标识
cv2.rectangle(img=frame, pt1=(x, y), pt2=(x + w, y + h), color=(0, 0, 255), thickness=2)
然后程序在cv2.rectangle
这步崩了,快乐消失了.
过了亿会儿时间,我发现cv2.rectangle
的pt1
和pt2
参数中的坐标改成int类型就不会崩了,而我这里的x,y,w,h都是float类型的.
所以,源代码改成如下:
for location in faces:
x = int(location['left'])
y = int(location['top'])
w = int(location['width'])
h = int(location['height'])
user_id = location['user_id']
if user_id:
# 在原图像上绘制绿色矩形标识
cv2.rectangle(img=frame, pt1=(x, y), pt2=(x + w, y + h), color=(0, 255, 0), thickness=2)
else:
# 在原图像上绘制红色矩形标识
cv2.rectangle(img=frame, pt1=(x, y), pt2=(x + w, y + h), color=(0, 0, 255), thickness=2)
ok!不崩了,框出来了.