Skip to content

Instantly share code, notes, and snippets.

@akameco
Created November 2, 2016 03:24
Show Gist options
  • Save akameco/f747a444ad1448d6c94b3f5965840cb7 to your computer and use it in GitHub Desktop.
Save akameco/f747a444ad1448d6c94b3f5965840cb7 to your computer and use it in GitHub Desktop.
OpenCV+anime faceで動画のアニメ顔認識
# coding=utf-8
import sys
import os.path
import datetime
import numpy as np
import cv2
def export(filename, output="output.m4v"):
cascade_file="./lbpcascade_animeface.xml"
cascade = cv2.CascadeClassifier(cascade_file)
if cascade.empty():
raise Exception('cascade file not found')
cap = cv2.VideoCapture(filename)
if not cap.isOpened():
raise RuntimeError("{}: not found".format(filename))
fps = 20
# fps = cap.get(cv2.CAP_PROP_FPS)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(
output,
fourcc,
fps,
(int(width), int(height))
)
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
# img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.equalizeHist(gray)
faces = cascade.detectMultiScale(
gray,
scaleFactor = 1.1,
minNeighbors = 5,
minSize = (24, 24)
)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w ,y + h), (0 , 0, 255), 3)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if cap.get(cv2.CAP_PROP_POS_FRAMES) % 100 == 0:
print(str(int(cap.get(cv2.CAP_PROP_POS_FRAMES))))
out.write(frame)
cv2.imshow('frame', frame)
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
args = sys.argv[1:]
l = len(args)
if l < 0:
sys.stderr.write('error')
sys.exit(-1)
elif l == 1:
export(args[0])
elif l == 2:
export(args[0], args[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment