Skip to content

Instantly share code, notes, and snippets.

@roberto-butti
Created August 25, 2011 12:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roberto-butti/1170526 to your computer and use it in GitHub Desktop.
Save roberto-butti/1170526 to your computer and use it in GitHub Desktop.
opencv
from opencv import cv as opencv
from opencv import highgui
import sys
print "Inizializzazione webcam"
camera = highgui.cvCreateCameraCapture(-1)
if (camera):
print "Camera inizializzata"
else:
sys.exit("Camera NON inizializzata")
im = highgui.cvQueryFrame(camera)
if (im):
print "Acquisizione FRAME"
else:
sys.exit("Errore acquisizione FRAME da webcam")
filexml ="haarcascade_frontalface_default.xml"
print "Caricamento file xml:"+filexml
cascade = opencv.cvLoadHaarClassifierCascade(filexml, opencv.cvSize(1,1))
if (cascade):
print "Caricato"
else:
sys.exit("Non caricato")
storage = opencv.cvCreateMemStorage(0)
loop = True
while(loop):
frame = highgui.cvQueryFrame(camera)
if (frame == None):
break;
grayscale = opencv.cvCreateImage(opencv.cvSize(frame.width, frame.height), 8, 1)
opencv.cvCvtColor(frame, grayscale, opencv.CV_BGR2GRAY)
faces = opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2,opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(50,50))
if faces:
#print 'face detected!'
for face in faces:
opencv.cvRectangle(grayscale, opencv.cvPoint( int(face.x), int(face.y)),
opencv.cvPoint(int(face.x + face.width), int(face.y + face.height)),
opencv.CV_RGB(127, 255, 0), 2) # RGB #7FFF00 width=2
highgui.cvShowImage("Finestra", grayscale)
char = highgui.cvWaitKey(33)
if (char != -1):
if (ord(char) == 27):
loop = False
highgui.cvDestroyWindow("Finestra")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment