-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPyFry.py
More file actions
212 lines (173 loc) · 7.6 KB
/
PyFry.py
File metadata and controls
212 lines (173 loc) · 7.6 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import cv2
from PIL import Image, ImageOps, ImageEnhance
import os
from utils.utils import Colors
from imutils import face_utils
import dlib
import sys
from tkinter import *
from tkinter import messagebox
from PIL import Image, ImageFont
window = Tk()
window.eval('tk::PlaceWindow %s center' % window.winfo_toplevel())
window.withdraw()
if messagebox.askyesno('Welcome to PyFry', 'Do you want to use your web cam for the Test Picture?', icon='error') == True:
print('yes')
# using camera to read
cam = cv2.VideoCapture(0)
cv2.namedWindow("Smile - Press spacebar to capture")
cv2.moveWindow("Smile - Press spacebar to capture",30,40)
img_counter = 0
while True:
ret, frame = cam.read()
cv2.imshow("Smile - Press spacebar to capture", frame)
if not ret:
break
k = cv2.waitKey(1)
if k % 256 == 27:
# ESC pressed
print("Escape hit, closing...")
break
elif k % 256 == 32:
# SPACE pressed
img_name = "opencv_frame_{}.jpg".format(img_counter)
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
#img_counter += 1
#cv2.destroyAllWindows()
image = cv2.imread(img_name)
cv2.imshow("Press Esc to PyFry", image)
cv2.moveWindow("Press Esc to PyFry",30,40)
break
cam.release()
cv2.waitKey(0)
cv2.destroyAllWindows()
def irisCoords(eye):
#Finding the center point of th eye using the average outer extremes average of the eyes
mid = (eye[0] +eye[3])/2
mid = (int(mid[0]), int(mid[1]))
return mid
def generateHue(img):
#Generating and increasing prominency of red band of the image
img = img.convert('RGB')
red = img.split()[0] #(R,G,B)
red = ImageEnhance.Contrast(red).enhance(2.0)
red = ImageEnhance.Brightness(red).enhance(1.5)
red = ImageOps.colorize(red, Colors.RED, Colors.YELLOW)
img = Image.blend(img, red, 0.77)
#Keeping a 100% sharpness value for now, But would probably be better with a higher sharpness value
img = ImageEnhance.Sharpness(img).enhance(150)
return img
def crushAndBack(img):
img = img.convert('RGB')
w,h = img.width, img.height
img = img.resize((int(w ** .95), int(h ** .95)), resample=Image.LANCZOS)
img = img.resize((int(w ** .90), int(h ** .90)), resample = Image.BILINEAR)
img = img.resize((int(w ** .90), int(h ** .90)), resample = Image.BICUBIC)
img = img.resize((w,h), resample = Image.BICUBIC)
return img
def main():
# Initialising dlib for frontal facial features
flare = Image.open('flare.png')
detect = dlib.get_frontal_face_detector()
predict = dlib.shape_predictor("/Users/shahil/Documents/GitHub/PyFry/assets/shape_predictor_68_face_landmarks.dat")
(lS, lE) = face_utils.FACIAL_LANDMARKS_68_IDXS["left_eye"]
(rS, rE) = face_utils.FACIAL_LANDMARKS_68_IDXS["right_eye"]
#use camera to read
imgCV = cv2.imread('opencv_frame_0.jpg')
img = Image.open('opencv_frame_0.jpg')
gray = cv2.cvtColor(imgCV, cv2.COLOR_BGR2GRAY)
subjects = detect(gray, 0)
for subject in subjects:
shape = predict(gray, subject)
shape = face_utils.shape_to_np(shape)
leftEye = shape[lS:lE]
rightEye = shape[rS:rE]
print(leftEye)
print(irisCoords(leftEye))
#leftEyeHull = cv2.convexHull(leftEye)
#rightEyeHull = cv2.convexHull(rightEye)
#cv2.drawContours(imgCV, [leftEyeHull], -1, (0, 255, 0), 1)
#cv2.drawContours(imgCV, [rightEyeHull], -1, (0, 255, 0), 1)
#cv2.circle(imgCV, irisCoords(leftEye),5, (0.255,255), 1)
#cv2.circle(imgCV, irisCoords(rightEye),5, (0.255,255), 1)
#rightEyeArray = []
#rightEyeArray.append(irisCoords(leftEye))
eyeLeft = (leftEye[0],(leftEye[1] +leftEye[2])/2,leftEye[3],(leftEye[4]+leftEye[5])/2)
eyeLeft = (leftEye[0], leftEye[1])
img = img.convert('RGB')
img = crushAndBack(img)
img = generateHue(img)
#img.paste(flare,eyeLeft,flare)
img.show()
img.save('output.jpg')
#img.save('sample.jpg','jpeg')
if __name__ == '__main__':
main()
else:
print('no')
def irisCoords(eye):
#Finding the center point of th eye using the average outer extremes average of the eyes
mid = (eye[0] +eye[3])/2
mid = (int(mid[0]), int(mid[1]))
return mid
def generateHue(img):
#Generating and increasing prominency of red band of the image
img = img.convert('RGB')
red = img.split()[0] #(R,G,B)
red = ImageEnhance.Contrast(red).enhance(2.0)
red = ImageEnhance.Brightness(red).enhance(1.5)
red = ImageOps.colorize(red, Colors.RED, Colors.YELLOW)
img = Image.blend(img, red, 0.77)
#Keeping a 100% sharpness value for now, But would probably be better with a higher sharpness value
img = ImageEnhance.Sharpness(img).enhance(150)
return img
def crushAndBack(img):
img = img.convert('RGB')
w,h = img.width, img.height
img = img.resize((int(w ** .95), int(h ** .95)), resample=Image.LANCZOS)
img = img.resize((int(w ** .90), int(h ** .90)), resample = Image.BILINEAR)
img = img.resize((int(w ** .90), int(h ** .90)), resample = Image.BICUBIC)
img = img.resize((w,h), resample = Image.BICUBIC)
return img
def main():
# Initialising dlib for frontal facial features
flare = Image.open('flare.png')
detect = dlib.get_frontal_face_detector()
predict = dlib.shape_predictor("/Users/shahil/Documents/GitHub/PyFry/assets/shape_predictor_68_face_landmarks.dat")
(lS, lE) = face_utils.FACIAL_LANDMARKS_68_IDXS["left_eye"]
(rS, rE) = face_utils.FACIAL_LANDMARKS_68_IDXS["right_eye"]
#use camera to read
imgCV = cv2.imread('test.jpg')
img = Image.open('test.jpg')
gray = cv2.cvtColor(imgCV, cv2.COLOR_BGR2GRAY)
subjects = detect(gray, 0)
for subject in subjects:
shape = predict(gray, subject)
shape = face_utils.shape_to_np(shape)
leftEye = shape[lS:lE]
rightEye = shape[rS:rE]
print(leftEye)
print(irisCoords(leftEye))
#leftEyeHull = cv2.convexHull(leftEye)
#rightEyeHull = cv2.convexHull(rightEye)
#cv2.drawContours(imgCV, [leftEyeHull], -1, (0, 255, 0), 1)
#cv2.drawContours(imgCV, [rightEyeHull], -1, (0, 255, 0), 1)
#cv2.circle(imgCV, irisCoords(leftEye),5, (0.255,255), 1)
#cv2.circle(imgCV, irisCoords(rightEye),5, (0.255,255), 1)
#rightEyeArray = []
#rightEyeArray.append(irisCoords(leftEye))
eyeLeft = (leftEye[0],(leftEye[1] +leftEye[2])/2,leftEye[3],(leftEye[4]+leftEye[5])/2)
eyeLeft = (leftEye[0], leftEye[1])
img = img.convert('RGB')
img = crushAndBack(img)
img = generateHue(img)
#img.paste(flare,eyeLeft,flare)
img.show()
img.save('output.jpg')
#img.save('sample.jpg','jpeg')
if __name__ == '__main__':
main()
window.deiconify()
window.destroy()
window.quit()