<p>Dlib有一个非常方便、快速和高效的对象检测例程,我想制作一个类似于示例<a href="https://realpython.com/blog/python/face-detection-in-python-using-a-webcam/" rel="nofollow">here</a>的酷脸跟踪示例。在</p>
<p>OpenCV得到了广泛的支持,它具有相当快的视频捕获模块(五分之一秒用于快照,而调用某个程序来唤醒网络摄像头并获取图片的时间为1秒或更长)。我将此添加到Dlib中的facedetector Python示例中。在</p>
<p>如果直接显示和处理OpenCV VideoCapture输出,它看起来很奇怪,因为显然OpenCV存储的是BGR而不是RGB顺序。调整后,它可以工作,但速度很慢:</p>
<pre><code>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]))
</code></pre>
<p>从这个程序的计时输出来看,似乎处理和抓取图片都需要五分之一秒的时间,所以你会认为它应该每秒显示一到两个更新——但是,如果你举手,它会在5秒左右后显示在网络摄像头视图中!在</p>
<p>是否有某种内部缓存阻止它获取最新的网络摄像头图像?我可以调整或多线程的网络摄像头输入过程,以修复滞后?这是一个英特尔i5与16gb内存。在</p>
<p><strong><em>更新</em></strong></p>
<p>根据这里,它建议read逐帧抓取视频。这就解释了它会抓取下一帧和下一帧,直到它最终赶上在处理时被抓取的所有帧。我想知道是否有一个选项可以设置帧速率或设置它为drop frames,然后在阅读时点击网络摄像头中的人脸图片?
<a href="http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html#capture-video-from-camera" rel="nofollow">http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html#capture-video-from-camera</a></p>