使用下面的示例代码,我正在使用基本的 dlib 人脸检测。 我最初是在检测到的人脸周围画一个边界框,但现在我只想在检测到的人脸(又名人脸)内显示:
img[top:bottom,left:right,:]
import sys
import dlib
import cv2
detector = dlib.get_frontal_face_detector()
cam = cv2.VideoCapture(1)
color_green = (0,255,0)
line_width = 3
while True:
ret_val, img = cam.read()
rgb_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
dets = detector(rgb_image)
#for det in dets:
#cv2.rectangle(img,(det.left(), det.top()), (det.right(), det.bottom()), color_green, line_width)
new_img = img[top:bottom,left:right,:]
cv2.imshow('my webcam', new_img)
if cv2.waitKey(1) == 27:
break # esc to quit
cv2.destroyAllWindows()
我面临的问题是,它成功地向我展示了 x、y、w、h 中的内容,但图像不断调整大小,具体取决于我离相机的距离。
我做的是以下步骤:
img[top:bottom,left:right,:]
focus_face = cv2.resize(img, (480, 480))
然后传图展示
所以我遇到的问题是,如果我调整数组(img)的大小,它似乎并没有跟随检测到的脸,而是聚焦在屏幕的中心,尤其是我向后移动的越多..所以如果我在屏幕的中心屏幕然后它显示我的整个脸,如果我在两侧,它只会显示我脸的一部分。
我尽力解释了这一点,但如果您有任何疑问,请告诉我。
最佳。