Created
September 20, 2016 19:48
-
-
Save yoshimax/b43fff093f205298805672fc8fd88a8f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding:utf-8 | |
# http://taizo.hatenablog.jp/entry/2016/06/28/072112 | |
# http://qiita.com/lrf141/items/ff1462c5c6b7b3207775 | |
import numpy as np | |
import cv2 | |
cap = cv2.VideoCapture(0) | |
cap.set(3, 640) | |
cap.set(4, 480) | |
cascade_path = "/usr/local/Cellar/opencv3/HEAD-cd6e7ac_4/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml" | |
cascade = cv2.CascadeClassifier(cascade_path) | |
color = (0,0,255) | |
cnt = 0 | |
while(True): | |
cnt += 1 | |
ret, frame = cap.read() | |
if ret == False: | |
break | |
if (cnt % 1) == 0: | |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
facerect = cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=1, minSize=(1, 1)) | |
if len(facerect) > 0: | |
for (x,y,w,h) in facerect: | |
print "face x:" + str(x) + "face y:" + str(y) | |
#顔の部分だけ切り抜いてモザイク処理をする | |
cut_img = frame[y:y+h,x:x+w] | |
cut_face = cut_img.shape[:2][::-1] | |
#10分の1にする | |
cut_img = cv2.resize(cut_img,(cut_face[0]/10, cut_face[0]/10)) | |
#画像を元のサイズに拡大 | |
cut_img = cv2.resize(cut_img,cut_face,interpolation = cv2.INTER_NEAREST) | |
#モザイク処理した部分を重ねる | |
frame[y:y+h,x:x+w] = cut_img | |
for rect in facerect: | |
cv2.rectangle(frame, tuple(rect[0:2]),tuple(rect[0:2]+rect[2:4]), color, thickness=4) | |
cv2.imshow('fram', frame) | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
cap.release() | |
cv2.dstroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment