-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcowboyHat.py
169 lines (128 loc) · 4.62 KB
/
cowboyHat.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import cv2
import numpy as np
from PIL import Image
def faceDetect():
faceCascade = cv2.CascadeClassifier('Pictures/haarcascade_frontalface_default.xml')
img = cv2.imread('Pictures/feed.png')
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
try:
faces = faceCascade.detectMultiScale(imgGray,1.5,4)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
#the following returns the X and Y values which we will use to place the hat on the figure (Stored in a Numpy Array)
fx = faces [0,0]
fy = faces [0,1]
return [fx, fy]
except TypeError:
#this will only run if there is no face found with OpenCV
print("ERROR: FACE NOT FOUND")
return [1000,1000]
def addCowboyHat():
# Opening the primary image (used in background)
headVals = faceDetect()
img1 = Image.open(r"Pictures/feed.png")
#We must now tweak headVals
headVals[0] -= 115
headVals[1] -= 170
# Opening the secondary image (overlay image)
img2 = Image.open(r"Pictures/Cowboy-hat.jpg")
img2 = img2.resize((500,300))
transpColor = Image.open(r"Pictures/Cowboy-hat-mask.jpg")
transpColor = transpColor.resize((500,300))
#then you mast add the alpha channel
transpColor = transpColor.convert('L') # greyscale
img2.putalpha(transpColor)
# Pasting img2 image on top of img1
img1.paste(img2, headVals, mask = transpColor)
img1.save(r"Pictures/feed.png")
return 0
def addBandana():
# Opening the primary image (used in background)
headVals = faceDetect()
img1 = Image.open(r"Pictures/feed.png")
#We must now tweak headVals
headVals[0] -= 155
headVals[1] += 100
# Opening the secondary image (overlay image)
img2 = Image.open(r"Pictures/red-bandana.jpg")
img2 = img2.resize((600,400))
transpColor = Image.open(r"Pictures/red-bandana-mask.jpg")
transpColor = transpColor.resize((600,400))
#then you mast add the alpha channel
transpColor = transpColor.convert('L') # greyscale
img2.putalpha(transpColor)
# Pasting img2 image on top of img1
img1.paste(img2, headVals, mask = transpColor)
#img1.show()
#/\ FOR DEBUGGING
img1.save(r"Pictures/feed.png")
return 0
def addAltCowboyHat():
# Opening the primary image (used in background)
headVals = faceDetect()
img1 = Image.open(r"Pictures/feed.png")
#We must now tweak headVals
headVals[0] -= 155
headVals[1] -= 170
# Opening the secondary image (overlay image)
img2 = Image.open(r"Pictures/black-cowboy-hat.jpg")
img2 = img2.resize((600,300))
transpColor = Image.open(r"Pictures/black-cowboy-hat-mask.jpg")
transpColor = transpColor.resize((600,300))
#then you mast add the alpha channel
transpColor = transpColor.convert('L') # greyscale
img2.putalpha(transpColor)
# Pasting img2 image on top of img1
img1.paste(img2, headVals, mask = transpColor)
#img1.show()
# /\ FOR DEBUGGING
img1.save(r"Pictures/feed.png")
return 0
def addMustache():
# Opening the primary image (used in background)
headVals = faceDetect()
img1 = Image.open(r"Pictures/feed.png")
#We must now tweak headVals
headVals[0] -= 5
headVals[1] += 100
# Opening the secondary image (overlay image)
img2 = Image.open(r"Pictures/handlebar-mustache.jpg")
img2 = img2.resize((275,200))
transpColor = Image.open(r"Pictures/handlebar-mustache-mask.jpg")
transpColor = transpColor.resize((275,200))
#then you mast add the alpha channel
transpColor = transpColor.convert('L') # greyscale
img2.putalpha(transpColor)
# Pasting img2 image on top of img1
img1.paste(img2, headVals, mask = transpColor)
#img1.show()
#/\ FOR DEBUGGING
img1.save(r"Pictures/feed.png")
return 0
def addCigar():
# Opening the primary image (used in background)
headVals = faceDetect()
img1 = Image.open(r"Pictures/feed.png")
#We must now tweak headVals
headVals[0] += 20
headVals[1] += 200
# Opening the secondary image (overlay image)
img2 = Image.open(r"Pictures/cigar.jpg")
img2 = img2.resize((100,100))
transpColor = Image.open(r"Pictures/cigar-mask.jpg")
transpColor = transpColor.resize((100,100))
#then you mast add the alpha channel
transpColor = transpColor.convert('L') # greyscale
img2.putalpha(transpColor)
# Pasting img2 image on top of img1
img1.paste(img2, headVals, mask = transpColor)
#img1.show()
#/\ FOR DEBUGGING
img1.save(r"Pictures/feed.png")
return 0
#faceDetect()
#addCowboyHat()
#addBandana()
#addAltCowboyHat()
#addMustache()
#addCigar()