-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathads.py
395 lines (294 loc) · 13.8 KB
/
ads.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
from pathlib import Path
import cv2
import dlib
import numpy as np
import argparse
from contextlib import contextmanager
from wide_resnet import WideResNet
from keras.utils.data_utils import get_file
import os
import tkinter as tk
from itertools import cycle
from PIL import ImageTk, Image
from itertools import cycle
from PIL import ImageTk
from PIL import Image
import tensorflow as tf
from sa import clothes_detector
import sa
from tkinter import messagebox
"""import matplotlib.pyplot as plt"""
from tensorflow import keras
""" print(tf.__version__)"""
pretrained_model = "https://github.com/yu4u/age-gender-estimation/releases/download/v0.5/weights.28-3.73.hdf5"
modhash = 'fbe63257a054c1c5466cfd7bf14646d6'
variables=['','','']
def get_args():
parser = argparse.ArgumentParser(description="This script detects faces from web cam input, "
"and estimates age and gender for the detected faces.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--weight_file", type=str, default=None,
help="path to weight file (e.g. weights.28-3.73.hdf5)")
parser.add_argument("--depth", type=int, default=16,
help="depth of network")
parser.add_argument("--width", type=int, default=8,
help="width of network")
parser.add_argument("--margin", type=float, default=0.4,
help="margin around detected face for age-gender estimation")
parser.add_argument("--image_dir", type=str, default=None,
help="target image directory; if set, images in image_dir are used instead of webcam")
args = parser.parse_args()
return args
def draw_label(image, point, label, font=cv2.FONT_HERSHEY_SIMPLEX,
font_scale=0.8, thickness=1):
size = cv2.getTextSize(label, font, font_scale, thickness)[0]
x, y = point
cv2.rectangle(image, (x, y - size[1]), (x + size[0], y), (255, 0, 0), cv2.FILLED)
cv2.putText(image, label, point, font, font_scale, (255, 255, 255), thickness, lineType=cv2.LINE_AA)
@contextmanager
def video_capture(*args, **kwargs):
cap = cv2.VideoCapture(*args, **kwargs)
try:
yield cap
finally:
cap.release()
def yield_images():
# capture video
with video_capture(0) as cap:
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
while True:
# get video frame
ret, img = cap.read()
if not ret:
raise RuntimeError("Failed to capture image")
yield img
def yield_images_from_dir(image_dir):
image_dir = Path(image_dir)
for image_path in image_dir.glob("*.*"):
img = cv2.imread(str(image_path), 1)
if img is not None:
h, w, _ = img.shape
r = 640 / max(w, h)
yield cv2.resize(img, (int(w * r), int(h * r)))
def main():
args = get_args()
depth = args.depth
k = args.width
weight_file = args.weight_file
margin = args.margin
image_dir = args.image_dir
if not weight_file:
weight_file = get_file("weights.28-3.73.hdf5", pretrained_model, cache_subdir="pretrained_models",
file_hash=modhash, cache_dir=str(Path(__file__).resolve().parent))
# for face detection
detector = dlib.get_frontal_face_detector()
# load model and weights
img_size = 64
model = WideResNet(img_size, depth=depth, k=k)()
model.load_weights(weight_file)
image_generator = yield_images_from_dir(image_dir) if image_dir else yield_images()
for img in image_generator:
input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_h, img_w, _ = np.shape(input_img)
# detect faces using dlib detector
detected = detector(input_img, 1)
faces = np.empty((len(detected), img_size, img_size, 3))
if len(detected) > 0:
for i, d in enumerate(detected):
x1, y1, x2, y2, w, h = d.left(), d.top(), d.right() + 1, d.bottom() + 1, d.width(), d.height()
xw1 = max(int(x1 - margin * w), 0)
yw1 = max(int(y1 - margin * h), 0)
xw2 = min(int(x2 + margin * w), img_w - 1)
yw2 = min(int(y2 + margin * h), img_h - 1)
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
# cv2.rectangle(img, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2)
faces[i, :, :, :] = cv2.resize(img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size))
# predict ages and genders of the detected faces
results = model.predict(faces)
predicted_genders = results[0]
ages = np.arange(0, 101).reshape(101, 1)
predicted_ages = results[1].dot(ages).flatten()
# draw results
for i, d in enumerate(detected):
#label = "{}, {}".format(int(predicted_ages[i]),
#"M" if predicted_genders[i][0] < 0.5 else "F")
label = "{}".format("Detected")
draw_label(img, (d.left(), d.top()), label)
variables[0]=predicted_ages[i]
variables[1]="M" if predicted_genders[i][0] < 0.5 else "F"
if (variables[0]!='' or variables[1]!=''):
cv2.imshow("result", img)
key = cv2.waitKey(-1) if image_dir else cv2.waitKey(30)
cv2.imwrite('image'+'.jpg',img)
cv2.waitKey(1)
variables[2]=sa.clothes_detector('image.jpg')
print(variables[0],":",variables[1],":",variables[2])
cv2.destroyAllWindows()
break
cv2.imshow("result", img)
key = cv2.waitKey(1) if image_dir else cv2.waitKey(30)
if key == 27: # ESC
break
main()
entries=""
print(variables[0],variables[1],variables[2])
a=os.getcwd()
if (variables[1]=='M'):
if(int(variables[0])<10):
if(variables[2]=="T-shirt/top"):
print(a+"\Advertisements\Male\Kids\T-shirt_top")
entries= os.listdir(a+"\Advertisements\Male\Kids\T-shirt_top")
e1=a+"\Advertisements\Male\Kids\T-shirt_top/"
elif(variables[2]=='Coat'):
print(a+"\Advertisements\Male\Kids\Coat")
entries= os.listdir(a+"\Advertisements\Male\Kids\Coat")
e1=a+"\Advertisements\Male\Kids\Coat/"
elif(variables[2]=='Shirt'):
print(a+"\Advertisements\Male\Kids\Shirts")
entries= os.listdir(a+"\Advertisements\Male\Kids\Shirts")
e1=a+"\Advertisements\Male\Kids\Shirts/"
elif(int(variables[0])>10 and int(variables[0])<18):
if(variables[2]=="T-shirt/top"):
print(a+"\Advertisements\Mal\Teenagers\T-shirt_top")
entries= os.listdir(a+"\Advertisements\Male\Teenagers\T-shirt_top")
e1=a+"\Advertisements\Male\Teenagers\T-shirt_top/"
elif(variables[2]=='Coat'):
print(a+"\Advertisements\Mal\Teenagers\Coat")
entries= os.listdir(a+"\Advertisements\Male\Teenagers\Coat")
e1=a+"\Advertisements\Male\Teenagers\Coat/"
elif(variables[2]=='Shirt'):
print(a+"\Advertisements\Mal\Teenagers\Shirts")
entries= os.listdir(a+"\Advertisements\Male\Teenagers\Shirts")
e1=a+"\Advertisements\Male\Teenagers\Shirts/"
elif(int(variables[0])>18 and int(variables[0])<55):
if(variables[2]=="T-shirt/top"):
print(a+"\Advertisements\Male\Adults\T-shirt_top")
entries= os.listdir(a+"\Advertisements\Male\Adults\T-shirt_top")
e1=a+"\Advertisements\Male\Adults\T-shirt_top/"
elif(variables[2]=='Coat'):
print(a+"\Advertisements\Male\Adults\Coat")
entries= os.listdir(a+"\Advertisements\Male\Adults\Coat")
e1=a+"\Advertisements\Male\Adults\Coat/"
elif(variables[2]=='Shirt'):
print(a+"\Advertisements\Male\Adults\Shirts")
entries= os.listdir(a+"\Advertisements\Male\Adults\Shirts")
e1=a+"\Advertisements\Male\Adults\Shirts/"
elif(int(variables[0])>55):
if(variables[2]=="T-shirt/top"):
print(a+"\Advertisements\Male\Old people\T-shirt_top")
entries= os.listdir(a+"\Advertisements\Male\Old people\T-shirt_top")
e1=a+"\Advertisements\Male\Old people\T-shirt_top/"
elif(variables[2]=='Coat'):
print(a+"\Advertisements\Male\Old people\Coat")
entries= os.listdir(a+"\Advertisements\Male\Old people\Coat")
e1=a+"\Advertisements\Male\Old people\Coat/"
elif(variables[2]=='Shirt'):
print(a+"\Advertisements\Male\Old people\Shirts")
entries= os.listdir(a+"\Advertisements\Male\Old people\Shirts")
e1=a+"\Advertisements\Male\Old people\Shirts/"
elif (variables[1]=='F'):
if(int(variables[0])<10):
if(variables[2]=="T-shirt/top"):
print(a+"\Advertisements\Female\Kids\T-shirt_top")
entries= os.listdir(a+"\Advertisements\Female\Kids\T-shirt_top")
e1=a+"\Advertisements\Female\Kids\T-shirt_top/"
elif(variables[2]=='Shirt'):
print(a+"\Advertisements\Female\Kids\Shirt")
entries= os.listdir(a+"\Advertisements\Female\Kids\Shirt")
e1=a+"\Advertisements\Female\Kids\Shirt/"
elif(variables[2]=='Dress'):
print(a+"\Advertisements\Female\Kids\Dress")
entries= os.listdir(a+"\Advertisements\Female\Kids\Dress")
e1=a+"\Advertisements\Female\Kids\Dress/"
elif(int(variables[0])>10 and int(variables[0])<18):
if(variables[2]=="T-shirt/top"):
print(a+"\Advertisements\Female\Teenagers\T-shirt_top")
entries= os.listdir(a+"\Advertisements\Female\Teenagers\T-shirt_top")
e1=a+"\Advertisements\Female\Teenagers\Kids\T-shirt_top/"
elif(variables[2]=='Shirt'):
print(a+"\Advertisements\Female\Teenagers\Shirt")
entries= os.listdir(a+"\Advertisements\Female\Teenagers\Shirt")
e1=a+"\Advertisements\Female\Teenagers\Kids\Shirt/"
elif(variables[2]=='Dress'):
print(a+"\Advertisements\Female\Teenagers\Dress")
entries= os.listdir(a+"\Advertisements\Female\Teenagers\Dress")
e1=a+"\Advertisements\Female\Teenagers\Kids\Dress/"
elif(int(variables[0])>18 and int(variables[0])<55):
if(variables[2]=="T-shirt/top"):
print(a+"\Advertisements\Female\Adults\T-shirt_top")
entries= os.listdir(a+"\Advertisements\Female\Adults\T-shirt_top")
e1=a+"\Advertisements\Female\Adults\T-shirt_top/"
elif(variables[2]=='Shirt'):
print(a+"\Advertisements\Female\Adults\Shirt")
entries= os.listdir(a+"\Advertisements\Female\Adults\Shirt")
e1=a+"\Advertisements\Female\Adults\Shirt/"
elif(variables[2]=='Dress'):
print(a+"\Advertisements\Female\Adults\Dress")
entries= os.listdir(a+"\Advertisements\Female\Adults\Dress")
e1=a+"\Advertisements\Female\Adults\Dress/"
elif(int(variables[0])>55):
if(variables[2]=="T-shirt/top"):
print(a+"\Advertisements\Female\Old people\T-shirt_top")
entries= os.listdir(a+"\Advertisements\Female\Old people\T-shirt_top")
e1=a+"\Advertisements\Female\Old people\T-shirt_top/"
elif(variables[2]=='Shirt'):
print(a+"\Advertisements\Female\Old people\Shirt")
entries= os.listdir(a+"\Advertisements\Female\Old people\Shirt")
e1=a+"\Advertisements\Female\Old people\Shirt/"
elif(variables[2]=='Dress'):
print(a+"\Advertisements\Female\Old people\Dress")
entries= os.listdir(a+"\Advertisements\Female\Old people\Dress")
e1=a+"\Advertisements\Female\Old people\Dress/"
print("Entries")
photos = cycle(ImageTk.PhotoImage(Image.open(e1+image),master=root) for image in entries)
def slideShow():
img = next(photos)
displayCanvas.config(image=img)
root.after(1000, slideShow) # 0.05 seconds
root = tk.Tk()
width = 500
height = 400
root.geometry('%dx%d' % (640, 480))
displayCanvas = tk.Label(root)
displayCanvas.pack()
root.after(10, lambda: slideShow())
def on_closing():
if messagebox.askokcancel("Quit", "Do you want to quit?"):
root.destroy()
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()
"""
class App(tk.Tk):
'''Tk window/label adjusts to size of image'''
def __init__(self, x, y, delay):
# the root will be self
tk.Tk.__init__(self,)
# set x, y position only
self.geometry('400x300')
self.delay = delay
# allows repeat cycling through the pictures
# store as (img_object, img_name) tuple
self.pictures = cycle((ImageTk.PhotoImage(e1+image, master=root), image)
for image in entries)
self.picture_display = tk.Label(self)
self.picture_display.pack()
def show_slides(self):
'''cycle through the images and show them'''
# next works with Python26 or higher
img_object, img_name = next(self.pictures)
self.picture_display.config(image=img_object)
# shows the image filename, but could be expanded
# to show an associated description of the image
self.title(img_name)
self.after(self.delay, self.show_slides)
def run(self):
self.mainloop()
# set milliseconds time between slides
delay = 3500
x = 10
y = 10
app = App( x, y, delay)
app.show_slides()
app.run()
"""