Skip to content

Instantly share code, notes, and snippets.

@Superbil
Created January 9, 2013 04:06
Show Gist options
  • Save Superbil/4490478 to your computer and use it in GitHub Desktop.
Save Superbil/4490478 to your computer and use it in GitHub Desktop.
use OpenCV API to find face from video steam
#!/usr/bin/env python
import cv
import cv2
window_name = "preview"
load_model = "haarcascade_frontalface_default.xml"
cv2.namedWindow(window_name)
vc = cv2.VideoCapture(0)
if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False
haar = cv2.CascadeClassifier(load_model)
while rval:
detected = haar.detectMultiScale(frame, 1.2, 2,
cv.CV_HAAR_DO_CANNY_PRUNING)
if detected is not None:
for (x,y,w,h) in detected:
color = (0,255,0)
cv2.rectangle(frame, (x,y), (x+w,y+h), color) # draw rectangle
cv2.imshow(window_name, frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
vc.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment