Skip to content

Instantly share code, notes, and snippets.

@quanon
Created April 11, 2017 14:10
Show Gist options
  • Save quanon/7d885e6030f1dcc383de4e115ae4d6a6 to your computer and use it in GitHub Desktop.
Save quanon/7d885e6030f1dcc383de4e115ae4d6a6 to your computer and use it in GitHub Desktop.
import cv2
# https://github.com/nagadomi/lbpcascade_animeface/blob/master/lbpcascade_animeface.xml
cascade_path = './lbpcascade_animeface.xml'
def detect(image):
image_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
image_gray = cv2.equalizeHist(image_gray)
cascade = cv2.CascadeClassifier(cascade_path)
face_rect = cascade.detectMultiScale(image_gray, scaleFactor=1.1, minNeighbors=3, minSize=(50, 50))
return face_rect
video_path = '/path/to/video.mp4'
cap = cv2.VideoCapture(video_path)
total_frame_count = 0
face_frame_count = 0
while(cap.isOpened()):
total_frame_count += 1
result, image = cap.read()
if result == False:
break
if total_frame_count % 10 != 0:
continue
face_rect = detect(image)
if len(face_rect) == 0:
continue
for (x, y, w, h) in face_rect:
croped = image[y:y+h, x:x+w]
file_path = 'croped/%04d.jpg' % face_frame_count
print(file_path)
cv2.imwrite(file_path, croped)
face_frame_count += 1
cap.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment