forked from Omanshu840/Movie-Recommender
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
211 lines (144 loc) · 5.76 KB
/
main.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
from flask import Flask, redirect, url_for, request, render_template, jsonify, make_response
from werkzeug.utils import secure_filename
from keras.preprocessing.image import img_to_array
from keras.models import load_model
import cv2
import imutils
from keras.preprocessing import image
import numpy as np
import sys
import os
import glob
import re
from flask_sqlalchemy import SQLAlchemy
import pymysql
from random import seed
from random import randint
import base64
seed(1)
detection_model_path = 'haarcascade_files/haarcascade_frontalface_default.xml'
emotion_model_path = 'models/_mini_XCEPTION.73.hdf5'
face_detection = cv2.CascadeClassifier(detection_model_path)
emotion_classifier = load_model(emotion_model_path, compile=False)
EMOTIONS = ["angry","angry","angry","happy","sad","happy","neutral"]
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['UPLOAD_FOLDER'] = 'static/images'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://Om_movie:Password@localhost:3306/movierecommender'
app.config['SQLALCHEMY_ECHO'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
app.secret_key = 'key'
class result(db.Model):
Rid = db.Column(db.Integer, primary_key=True)
ans = db.Column(db.String(20), unique=False, nullable = False)
class happy(db.Model):
mid = db.Column(db.Integer, primary_key=True)
mnane = db.Column(db.String(40), unique=False, nullable = False)
imglink = db.Column(db.String(100), unique=False, nullable=True)
class sad(db.Model):
mid = db.Column(db.Integer, primary_key=True)
mnane = db.Column(db.String(40), unique=False, nullable = False)
imglink = db.Column(db.String(100), unique=False, nullable=True)
class angry(db.Model):
mid = db.Column(db.Integer, primary_key=True)
mnane = db.Column(db.String(40), unique=False, nullable = False)
imglink = db.Column(db.String(100), unique=False, nullable=True)
class neutral(db.Model):
mid = db.Column(db.Integer, primary_key=True)
mnane = db.Column(db.String(40), unique=False, nullable = False)
imglink = db.Column(db.String(100), unique=False, nullable=True)
@app.route("/")
def home():
return render_template('index.html')
@app.route('/collectFace',methods = ['GET','POST'])
def collectFace():
if request.method == "POST":
f = open('temp.jpg', 'wb')
b = base64.decodestring(request.get_json()['image'][23:].encode())
f.write(b)
f.close()
emotion_classifier = load_model(emotion_model_path, compile=False)
frame = cv2.imread('temp.jpg',1)
frame = imutils.resize(frame,width=300)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_detection.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5,minSize=(30,30),flags=cv2.CASCADE_SCALE_IMAGE)
canvas = np.zeros((250, 300, 3), dtype='uint8')
frameClone = frame.copy()
faces = sorted(faces, reverse=True,
key=lambda x: (x[2] - x[0])*(x[3]-x[1]))[0]
(fX, fY, fW, fH) = faces
roi = gray[fY:fY + fH, fX:fX + fW]
roi = cv2.resize(roi, (48, 48))
roi = roi.astype("float") / 255.0
roi = img_to_array(roi)
roi = np.expand_dims(roi, axis=0)
preds = emotion_classifier.predict(roi)[0]
label = EMOTIONS[preds.argmax()]
print(label)
# r=[]
# for _ in range(5):
# value=randint(0, 10)
# q = "select * from "+label+" where mid="+str(value)+""
# r1 = db.engine.execute(q)
# r.append(r1)
return jsonify(label)
return render_template('index.html')
@app.route('/list_movies/')
def list_movies():
label = request.args.get("label",0)
q = "select * from "+label+" order by rand() limit 6"
r = db.engine.execute(q)
# r=[]
# for _ in range(5):
# value=randint(0, 10)
# q = "select * from "+label+" where mid="+str(value)+""
# r1 = db.engine.execute(q)
# r.append(r1)
db.session.commit()
return render_template('predict.html', label=label, r = r)
# @app.route('/predict')
# def upload():
# camera = cv2.VideoCapture(0)
# while True:
# frame = camera.read()[1]
# # print(temp)
# print("hi 1")
# cv2.imshow('frame', frame)
# print("hi 2")
# if cv2.waitKey(1) & 0xFF == ord('q'):
# cv2.imwrite('static/images/1.jpg',frame)
# break
# camera.release()
# cv2.destroyAllWindows()
# emotion_classifier = load_model(emotion_model_path, compile=False)
# frame = cv2.imread('static/images/1.jpg',1)
# frame = imutils.resize(frame,width=300)
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# faces = face_detection.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5,minSize=(30,30),flags=cv2.CASCADE_SCALE_IMAGE)
# canvas = np.zeros((250, 300, 3), dtype='uint8')
# frameClone = frame.copy()
# faces = sorted(faces, reverse=True,
# key=lambda x: (x[2] - x[0])*(x[3]-x[1]))[0]
# (fX, fY, fW, fH) = faces
# roi = gray[fY:fY + fH, fX:fX + fW]
# roi = cv2.resize(roi, (48, 48))
# roi = roi.astype("float") / 255.0
# roi = img_to_array(roi)
# roi = np.expand_dims(roi, axis=0)
# preds = emotion_classifier.predict(roi)[0]
# label = EMOTIONS[preds.argmax()]
# print(label)
# q = "select * from "+label+""
# r = db.engine.execute(q)
# # r=[]
# # for _ in range(5):
# # value=randint(0, 10)
# # q = "select * from "+label+" where mid="+str(value)+""
# # r1 = db.engine.execute(q)
# # r.append(r1)
# entry = result(ans=label)
# db.session.add(entry)
# db.session.commit()
# return render_template('predict.html', label=label, r = r)
app.run(debug=True)