Skip to content

Instantly share code, notes, and snippets.

@WittmannF
Created August 14, 2018 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WittmannF/73aa4bb07877846556e0ab08cf3ae3fc to your computer and use it in GitHub Desktop.
Save WittmannF/73aa4bb07877846556e0ab08cf3ae3fc to your computer and use it in GitHub Desktop.
import numpy as np
import cv2, os
face_cascade = cv2.CascadeClassifier('cascade/haarcascades/haarcascade_frontalface_alt2.xml')
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5)
for (x, y, w, h) in faces:
print(x,y,w,h)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
img_item = "my-image.png"
cv2.imwrite(img_item, roi_gray)
color = (255, 0, 0)
stroke = 2
cv2.rectangle(frame, (x, y), (x+w, y+h), color, stroke)
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
# When everythin done, release the caputre
cap.release()
cv2.destroyAllWindows()
os.system("open my-image.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment