-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrypy.py
116 lines (99 loc) · 2.99 KB
/
trypy.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
from flask import Flask, render_template, request, redirect, url_for
import base64
import re
import numpy as np
from io import BytesIO
from tkinter import *
from PIL import Image, ImageTk
import time
import threading
import cv2
import os
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import tensorflow as tf
from keras import backend as k
from keras.models import load_model
app = Flask(__name__)
img_w = 75
img_h = 75
weight_dir = os.path.join(os.getcwd(), 'weights/')
model_name = 'face_model.h5'
model_dir = os.path.join(os.getcwd(), 'models/')
predictedAge='NA'
graph = tf.get_default_graph()
@app.route('/result' , methods=['GET'])
def result():
global predictedAge
print(predictedAge)
return render_template('result.html',predictedAge=predictedAge)
@app.route('/', methods=['POST','GET'])
def index():
print(request.method)
if request.method == 'POST':
with graph.as_default():
global predictedAge
print("INSIDE POST")
print(request.form['number'])
image_b64 = request.form['image']
print(e)
image_b64 = image_b64.split(',')[1]
print(image_b64[:100])
sbuf = BytesIO()
sbuf.write(base64.b64decode(image_b64))
pimg = Image.open(sbuf)
image = cv2.cvtColor(np.array(pimg), cv2.COLOR_RGB2BGR)
print('image produced')
print(image.shape)
#cv2.imread('captured image', (image))
#cv2.waitKey(0)
global weight_dir, img_w, img_h
img = image
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier('C:/Python35/Scripts/env/haarcascade_frontalface_default.xml')
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
print('displaying image')
roi = None
for (x,y,w,h) in faces:
roi = gray[y:y+h, x:x+w]
try:
print('using face only')
gray_img = cv2.resize(roi, (img_w,img_h))
gray_img = np.expand_dims(gray_img, axis=2)
gray_img = np.array([gray_img])/255.0
#cv2.imshow('face', (gray_img))
except:
print('Unable to find face')
print('using whole picture')
gray = cv2.resize(gray, (img_w,img_h))
gray = np.expand_dims(gray, axis=2)
gray = np.array([gray])/255.0
print(gray.shape)
#cv2.imshow('face', (gray))
predicted_age = 0
sum=0.0
counter=0.0
try:
for wt in os.listdir(weight_dir):
counter+=1.0
model.load_weights(weight_dir+wt)
print("wt: ",wt)
try:
ynew = model.predict_classes(gray_img)
except:
ynew = model.predict_classes(gray)
sum+=ynew[0]
except Exception as e:
print('line 217 ',e)
predicted_age = sum/counter
predictedAge = predicted_age
# predictedAge = 22
print('predict_age=', predictedAge)
return redirect(url_for('result'))
else:
return render_template('index.html')
if __name__ =="__main__":
os.environ['TF_CPP_MIN_LOG_LEVEL']='3'
model = load_model(model_dir+model_name)
print('model prepared')
app.run(debug=True,port=10080)