Skip to content

Instantly share code, notes, and snippets.

@ivder
Created April 9, 2019 06:20
Show Gist options
  • Save ivder/d20fc9ea38791da79dcda4e6ace286b4 to your computer and use it in GitHub Desktop.
Save ivder/d20fc9ea38791da79dcda4e6ace286b4 to your computer and use it in GitHub Desktop.
Save frame for every detected face using Haar Cascades
import cv2
import numpy as np
cap = cv2.VideoCapture('test.mp4')
#cap = cv2.VideoCapture(0) #tried using webcam and works
count = 0
while cap.isOpened():
ret,frame = cap.read()
cv2.imshow('window-name',frame)
face_cascade = cv2.CascadeClassifier('C:/ProgramData/Anaconda2/pkgs/opencv-3.2.0-np111py27_0/Library/etc/haarcascades/haarcascade_frontalface_default.xml')
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.imwrite("frame%d.jpg" % count, frame)
count = count + 1
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows() # destroy all the opened windows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment