【发布时间】:2016-12-11 10:53:02
【问题描述】:
Dlib 有一个非常方便、快速和高效的对象检测例程,我想制作一个酷炫的面部跟踪示例,类似于示例 here。
得到广泛支持的 OpenCV 具有相当快的 VideoCapture 模块(快照需要五分之一秒,而调用某些程序来唤醒网络摄像头并获取图片需要 1 秒或更长时间)。我将此添加到 Dlib 中的人脸检测 Python 示例中。
如果您直接显示和处理 OpenCV VideoCapture 输出,它看起来很奇怪,因为显然 OpenCV 存储 BGR 而不是 RGB 顺序。调整后,它可以工作,但速度很慢:
from __future__ import division
import sys
import dlib
from skimage import io
detector = dlib.get_frontal_face_detector()
win = dlib.image_window()
if len( sys.argv[1:] ) == 0:
from cv2 import VideoCapture
from time import time
cam = VideoCapture(0) #set the port of the camera as before
while True:
start = time()
retval, image = cam.read() #return a True bolean and and the image if all go right
for row in image:
for px in row:
#rgb expected... but the array is bgr?
r = px[2]
px[2] = px[0]
px[0] = r
#import matplotlib.pyplot as plt
#plt.imshow(image)
#plt.show()
print( "readimage: " + str( time() - start ) )
start = time()
dets = detector(image, 1)
print "your faces: %f" % len(dets)
for i, d in enumerate( dets ):
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
i, d.left(), d.top(), d.right(), d.bottom()))
print("from left: {}".format( ( (d.left() + d.right()) / 2 ) / len(image[0]) ))
print("from top: {}".format( ( (d.top() + d.bottom()) / 2 ) /len(image)) )
print( "process: " + str( time() - start ) )
start = time()
win.clear_overlay()
win.set_image(image)
win.add_overlay(dets)
print( "show: " + str( time() - start ) )
#dlib.hit_enter_to_continue()
for f in sys.argv[1:]:
print("Processing file: {}".format(f))
img = io.imread(f)
# The 1 in the second argument indicates that we should upsample the image
# 1 time. This will make everything bigger and allow us to detect more
# faces.
dets = detector(img, 1)
print("Number of faces detected: {}".format(len(dets)))
for i, d in enumerate(dets):
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
i, d.left(), d.top(), d.right(), d.bottom()))
win.clear_overlay()
win.set_image(img)
win.add_overlay(dets)
dlib.hit_enter_to_continue()
# Finally, if you really want to you can ask the detector to tell you the score
# for each detection. The score is bigger for more confident detections.
# Also, the idx tells you which of the face sub-detectors matched. This can be
# used to broadly identify faces in different orientations.
if (len(sys.argv[1:]) > 0):
img = io.imread(sys.argv[1])
dets, scores, idx = detector.run(img, 1)
for i, d in enumerate(dets):
print("Detection {}, score: {}, face_type:{}".format(
d, scores[i], idx[i]))
从这个程序的时间输出来看,处理和抓取图片似乎都需要五分之一秒,所以你会认为它应该每秒显示一到两次更新 - 但是,如果你举手它会在 5 秒左右后显示在网络摄像头视图中!
是否有某种内部缓存阻止它获取最新的网络摄像头图像?我可以调整或多线程网络摄像头输入过程来修复延迟吗?这是在配备 16GB RAM 的 Intel i5 上。
更新
根据此处,它建议读取逐帧抓取视频。这将解释它抓取下一帧和下一帧,直到它最终赶上在处理时已抓取的所有帧。我想知道是否有一个选项来设置帧率或将其设置为丢帧,然后只需单击网络摄像头中的面部图片现在阅读? http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html#capture-video-from-camera
【问题讨论】:
-
dlib 检测图像所花费的时间,您可以尝试将图像调整为更小的尺寸以获得更好的性能。
-
@ZdaR 感谢您的建议。请运行该示例,您会发现只需要几分之一秒。为什么从移动到在网络摄像头窗口中显示移动需要将近 5 秒(在它更新之前显示了许多中间帧?)这就是问题所在。
-
相机分辨率和人脸尺寸是多少?
-
1280x720,面部的位置似乎并不重要。