-
Notifications
You must be signed in to change notification settings - Fork 0
/
thuglife.py
74 lines (58 loc) · 1.99 KB
/
thuglife.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
from PIL import Image
import numpy as np
# thug life meme mask image path
maskPath = "mask.png"
# haarcascade path
cascPath = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Open mask as PIL image
mask = Image.open(maskPath)
def thug_mask(image):
"""
function to add thug life mask to input image
"""
# convert input image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# detect faces in grayscale image
faces = cascPath.detectMultiScale(gray, 1.15)
# convert cv2 imageto PIL image
background = Image.fromarray(image)
for (x,y,w,h) in faces:
# resize mask
resized_mask = mask.resize((w,h), Image.ANTIALIAS)
# define offset for mask
offset = (x,y)
# pask mask on background
background.paste(resized_mask, offset, mask=resized_mask)
# return background as cv2 image
return np.asarray(background)
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.rotation = -90
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))
time.sleep(.25)
# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
# grab the raw NumPy array representing the image, then initialize the timestamp
# and occupied/unoccupied text
img = frame.array
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect the faces
faces = cascPath.detectMultiScale(gray, 1.1, 4)
cv2.namedWindow("Frame", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("Frame",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
# show the frame
cv2.imshow("Frame", thug_mask(img))
key = cv2.waitKey(1) & 0xFF
# clear the stream in preparation for the next frame
rawCapture.truncate(0)
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break