Skip to content

Instantly share code, notes, and snippets.

@macrat
Created October 31, 2016 08:37
Show Gist options
  • Save macrat/612de0ff34ece3da03dfd2471e536765 to your computer and use it in GitHub Desktop.
Save macrat/612de0ff34ece3da03dfd2471e536765 to your computer and use it in GitHub Desktop.
顔がやばいことになるカメラ。LinuxClubの学祭展示中にライブコーディングしたやつ。
import random
import cv2
import numpy
cascade = cv2.CascadeClassifier('/usr/local/Cellar/opencv3/HEAD/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml')
cam = cv2.VideoCapture(0)
while True:
ret, img = cam.read()
if not ret:
print('error?')
break
img = cv2.resize(img, (1024, 768))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.resize(gray, (img.shape[1]//4, img.shape[0]//4))
faces = cascade.detectMultiScale(gray)
blur = cv2.cvtColor(cv2.GaussianBlur(img, (9, 9), 10), cv2.COLOR_BGR2HSV)
mask = numpy.zeros_like(img)
if len(faces) > 0:
for rect in faces:
width = max(rect) * 2
x, y = (rect[:2] + (rect[2:]//2)) * 4
cv2.circle(mask, (x, y), width, (1, 1, 1), -1)
img = mask * blur + (1-mask) * img
#cv2.namedWindow('scr', 0x10)
cv2.imshow('scr', img)
if cv2.waitKey(10) > 0:
break
cam.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment