Skip to content

Instantly share code, notes, and snippets.

@guglielmino
Created February 26, 2015 20:16
Show Gist options
  • Save guglielmino/12720b29b86da9536d38 to your computer and use it in GitHub Desktop.
Save guglielmino/12720b29b86da9536d38 to your computer and use it in GitHub Desktop.
import cv2
def DetectFace(img, face_cascade, callback):
image = None
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
image = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
callback(img, (x, y), (x + w, y + h))
return image
def onFace(img, pt1, pt2):
height, width = img.shape[:2]
(center_x, center_y) = (width / 2, height / 2)
(pic_center_x, pic_center_y) = (((pt2[0] - pt1[0]) / 2) + pt1[0], ((pt2[1] - pt1[1]) / 2) + pt1[1])
if pic_center_x > center_x:
print "DESTRA"
else:
print "SINISTRA"
if pic_center_y > center_y:
print "GIU'"
else:
print "SU"
# ----------
# M A I N
# ----------
winName = "Traccia sta Faccia"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)
capture = cv2.VideoCapture(1)
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
while (cv2.cv.WaitKey(15) == -1):
ret, img = capture.read()
if ret:
image = DetectFace(img, face_cascade, onFace)
if image is None:
image = img
cv2.imshow(winName, image)
else:
print "Camera not detected"
exit(0)
capture.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment