Dlib has a really convenient, fast, and efficient object detection procedure, and I wanted to make a cool face tracking example, similar to the example here .
OpenCV, which is widely supported, has a VideoCapture module that is pretty fast (a fifth of a second for a snapshot compared to 1 second or more for calling some program that wakes the webcam and retrieves the image). I added this to a Python example with a face detector in Dlib.
If you directly display and process the output of OpenCV VideoCapture, this looks weird because, apparently, OpenCV stores BGR instead of an RGB order. After setting it up, it works, but slowly:
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)
while True:
start = time()
retval, image = cam.read()
for row in image:
for px in row:
r = px[2]
px[2] = px[0]
px[0] = r
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 ) )
for f in sys.argv[1:]:
print("Processing file: {}".format(f))
img = io.imread(f)
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()
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 !
- , -? -, ? Intel i5 16- .
, , . , , , , , . , -, ?
http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html#capture-video-from-camera