This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain+movie_recommendation.py
251 lines (186 loc) · 9.36 KB
/
main+movie_recommendation.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
from keras.preprocessing.image import img_to_array
import imutils
import cv2
from keras.models import load_model
import numpy as np
import tkinter as tk
from tkinter import*
import playsound # to play saved mp3 file
from gtts import gTTS # google text to speech
import os
from matplotlib import pyplot as plt
num=1
def speaks(output):
global num
num +=1
#print("Safety Assistant : ", output)
tospeak = gTTS(text=output, lang='en-US', slow=False)
file = str(num)+".mp3"
tospeak.save(file)
playsound.playsound(file, True)
os.remove(file)
def music():
import music_recommendation
def movie():
speaks("MOVIES WILL BE SUGGESTED BASED ON THE EMOTION DETECTED")
# parameters for loading data and images
detection_model_path = 'haarcascade_files/haarcascade_frontalface_default.xml'
emotion_model_path = 'models/_mini_XCEPTION.102-0.66.hdf5'
# hyper-parameters for bounding boxes shape
# loading models
face_detection = cv2.CascadeClassifier(detection_model_path)
emotion_classifier = load_model(emotion_model_path, compile=False)
EMOTIONS = ["angry" ,"disgust","scared", "happy", "sad", "surprised",
"neutral"]
#feelings_faces = []
#for index, emotion in enumerate(EMOTIONS):
# feelings_faces.append(cv2.imread('emojis/' + emotion + '.png', -1))
# starting video streaming
cv2.namedWindow('your_face')
camera = cv2.VideoCapture(0)
while True:
frame = camera.read()[1]
#reading the frame
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()
if len(faces) > 0:
faces = sorted(faces, reverse=True,
key=lambda x: (x[2] - x[0]) * (x[3] - x[1]))[0]
(fX, fY, fW, fH) = faces
# Extract the ROI of the face from the grayscale image, resize it to a fixed 28x28 pixels, and then prepare
# the ROI for classification via the CNN
roi = gray[fY:fY + fH, fX:fX + fW]
roi = cv2.resize(roi, (64, 64))
roi = roi.astype("float") / 255.0
roi = img_to_array(roi)
roi = np.expand_dims(roi, axis=0)
preds = emotion_classifier.predict(roi)[0]
emotion_probability = np.max(preds)
label = EMOTIONS[preds.argmax()]
else:
import tkinter as tk
from tkinter import messagebox
root123= tk.Tk()
root123.withdraw()
msgbox=tk.messagebox.showinfo('ERROR MESSAGE', "FACE NOT DETECTED,PLEASE TRY AGAIN!!")
break
for (i, (emotion, prob)) in enumerate(zip(EMOTIONS, preds)):
# construct the label text
text = "{}: {:.2f}%".format(emotion, prob * 100)
# draw the label + probability bar on the canvas
# emoji_face = feelings_faces[np.argmax(preds)]
w = int(prob * 300)
cv2.rectangle(canvas, (7, (i * 35) + 5),
(w, (i * 35) + 35), (0, 0, 255), -1)
cv2.putText(canvas, text, (10, (i * 35) + 23),
cv2.FONT_HERSHEY_SIMPLEX, 0.45,
(255, 255, 255), 2)
cv2.putText(frameClone, label, (fX, fY - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
cv2.rectangle(frameClone, (fX, fY), (fX + fW, fY + fH),
(0, 0, 255), 2)
# for c in range(0, 3):
# frame[200:320, 10:130, c] = emoji_face[:, :, c] * \
# (emoji_face[:, :, 3] / 255.0) + frame[200:320,
# 10:130, c] * (1.0 - emoji_face[:, :, 3] / 255.0)
cv2.imshow('your_face', frameClone)
cv2.imshow("Probabilities", canvas)
if cv2.waitKey(1) & 0xFF == ord('s'):
from bs4 import BeautifulSoup as SOUP
import re
import requests as HTTP
# Main Function for scraping
def main(emotion):
em=emotion.lower()
import tkinter as tk
from tkinter import messagebox
root= tk.Tk()
root.withdraw()
msgbox=tk.messagebox.showinfo('EMOTION', em.upper())
# IMDb Url for Drama genre of
# movie against emotion Sad
if(em == "sad"):
speaks('GENRE ALLOCATED IS DRAMA')
urlhere = 'http://www.imdb.com/search/title?genres=drama&title_type=feature&sort=moviemeter, asc'
# IMDb Url for Musical genre of
# movie against emotion Disgust
elif(em == "disgust"):
speaks('GENRE ALLOCATED IS MUSICAL')
urlhere = 'http://www.imdb.com/search/title?genres=musical&title_type=feature&sort=moviemeter, asc'
# IMDb Url for Family genre of
# movie against emotion Anger
elif(em == "angry"):
speaks('GENRE ALLOCATED IS FAMILY')
urlhere = 'http://www.imdb.com/search/title?genres=family&title_type=feature&sort=moviemeter, asc'
# IMDb Url for Thriller genre of
# movie against emotion Anticipation
elif(em == "neutral"):
speaks('GENRE ALLOCATED IS THRILLER')
urlhere = 'http://www.imdb.com/search/title?genres=thriller&title_type=feature&sort=moviemeter, asc'
# IMDb Url for Sport genre of
# movie against emotion Fear
elif(em == "scared"):
speaks('GENRE ALLOCATED IS SPORTS')
urlhere = 'http://www.imdb.com/search/title?genres=sport&title_type=feature&sort=moviemeter, asc'
# IMDb Url for Thriller genre of
# movie against emotion Enjoyment
elif(em == "happy"):
speaks('GENRE ALLOCATED IS THRILLER')
urlhere = 'http://www.imdb.com/search/title?genres=thriller&title_type=feature&sort=moviemeter, asc'
# IMDb Url for Film_noir genre of
# movie against emotion Surprise
elif(em == "surprised"):
speaks('GENRE ALLOCATED IS FILM NOIR')
urlhere = 'http://www.imdb.com/search/title?genres=film_noir&title_type=feature&sort=moviemeter, asc'
# HTTP request to get the data of
# the whole page
response = HTTP.get(urlhere)
data = response.text
# Parsing the data using
# BeautifulSoup
soup = SOUP(data, "lxml")
# Extract movie titles from the
# data using regex
title = soup.find_all("a", attrs = {"href" : re.compile(r'\/title\/tt+\d*\/')})
title1 = soup.find_all("h3",{"class":"lister-item-header"})
rating = soup.find_all("div", {"class": "inline-block ratings-imdb-rating"})
import tkinter
from tkinter import ttk
root12 = tkinter.Tk()
root12.geometry("600x600")
root12.title("MOVIES RECOMMENDED FOR DETECTED EMOTION")
tree = ttk.Treeview(root12)
tree["columns"]=("one","two")
tree.column("one", width=200)
tree.column("two", width=200)
style = ttk.Style(root12)
style.configure('Treeview', rowheight=45)
tree.heading("one", text="MOVIES")
tree.heading("two", text="RATINGS")
for i in range(9,-1,-1):
tree.insert("" , 0, text="", values=(title1[i].text,rating[i].text))
tree.pack()
return title1
# Driver Function
if __name__ == '__main__':
#emotion = input("Enter the emotion: ")
a = main(label)
cv2.destroyAllWindows()
break
camera.release()
cv2.destroyAllWindows()
speaks("WELCOME TO ENTERTAINMENT RECOMMENDATION SYSTEM BASED ON EMOTION RECOGNITION")
speaks("In the next window,choose what you want us to suggest between MOVIES or MUSIC ")
root = Tk()
root.geometry("200x200")
var = IntVar()
R1 = Radiobutton(root, text="MOVIES", variable=var, value=1,
command=movie)
R1.pack( anchor = W )
R2 = Radiobutton(root, text="MUSIC", variable=var, value=2,command=music)
R2.pack( anchor = W )
label = Label(root)
label.pack()