gpt4 book ai didi

python - 这个网络摄像头面部检测有什么问题?

转载 作者:太空宇宙 更新时间:2023-11-03 10:57:42 25 4
gpt4 key购买 nike

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 秒左右后显示在网络摄像头 View 中!

是否有某种内部缓存阻止它获取最新的网络摄像头图像?我可以调整或多线程网络摄像头输入过程来修复延迟吗?这是在具有 16gb RAM 的 Intel i5 上。

更新

根据此处,它建议读取逐帧抓取视频。这可以解释它会抓取下一帧和下一帧,直到它最终 catch 它正在处理时抓取的所有帧。我想知道是否有一个选项可以设置帧速率或将其设置为丢帧,然后只需现在单击网络摄像头中的面部图片即可阅读? http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html#capture-video-from-camera

最佳答案

我感受到了你的痛苦。事实上,我最近使用了那个网络摄像头脚本(多次迭代;大量编辑)。我觉得我让它工作得很好,我想。为了让您看到我做了什么,我创建了一个包含详细信息的 GitHub Gist(代码;HTML 自述文件;示例输出):

https://gist.github.com/victoriastuart/8092a3dd7e97ab57ede7614251bf5cbd

关于python - 这个网络摄像头面部检测有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38782191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com