Python 感兴趣的矩形区域超出边界时出错-opencv
我的脚本检测到一张脸,然后使用dlib相关跟踪器算法跟踪它 我试图使用Python 感兴趣的矩形区域超出边界时出错-opencv,python,opencv,face-detection,dlib,imshow,Python,Opencv,Face Detection,Dlib,Imshow,我的脚本检测到一张脸,然后使用dlib相关跟踪器算法跟踪它 我试图使用cv2.imshow(track\u index,face\u img)在一个单独的窗口中显示每个跟踪的人脸,其中face\u image是使用dlib.rectangle坐标从视频捕获的帧中裁剪出的人脸的感兴趣区域 部分代码如下所示: #get the updated tracker position pos = tracker.get_posi
cv2.imshow(track\u index,face\u img)
在一个单独的窗口中显示每个跟踪的人脸,其中face\u image
是使用dlib.rectangle
坐标从视频捕获的帧中裁剪出的人脸的感兴趣区域
部分代码如下所示:
#get the updated tracker position
pos = tracker.get_position()
pos = dlib.rectangle(
int(pos.left()),
int(pos.top()),
int(pos.right()),
int(pos.bottom()),
)
#draw a bounding box around the tracked face
cv2.rectangle(image, (pos.left(), pos.top()), (pos.right(), pos.bottom()),
(100, 200, 100))
#crop the face from the frame
face_img = image[pos.top():pos.bottom(),pos.left():pos.right()]
#refers to the number of the track created
track_index = "track no.{}".format(trc - i)
font = cv2.FONT_HERSHEY_TRIPLEX
cv2.putText(image, track_index, (pos.left(), pos.bottom() +12), font, 0.5, (255, 255, 0))
#show the tracked face
cv2.imshow(track_index, face_img)
这可以正常工作,直到面超出边界或第一次出现在帧的一个边界外。在这种情况下,程序停止并抛出一个大小错误
Traceback (most recent call last):
File "/home/Developing space/facetrack/hog_detect_face_track.py", line 44, in <module>
cv2.imshow(track_index, face_img)
cv2.error: OpenCV(3.4.1) /io/opencv/modules/highgui/src/window.cpp:356: error: (-215) size.width>0 && size.height>0 in function imshow
回溯(最近一次呼叫最后一次):
文件“/home/Developing space/facetrack/hog\u detect\u face\u track.py”,第44行,在
cv2.imshow(轨道索引、面img)
cv2.error:OpenCV(3.4.1)/io/OpenCV/modules/highgui/src/window.cpp:356:error:(-215)函数imshow中的size.width>0和size.height>0
如何强制帧边框内的ROI阻止此错误发生?对ROI进行边界检查
h,w = image.shape[:2]
face_img = image[max(0,pos.top()):min(pos.bottom(),h),max(0,pos.left()):min(pos.right(),w)]