Commit 424fe18a authored by Yashmika Anusara's avatar Yashmika Anusara

Actor Identification System Backend

parent 726750b6
from flask import Flask, request
from werkzeug.utils import secure_filename
import cv2
from flask_cors import CORS
from ultralytics import YOLO
app = Flask(__name__)
cors = CORS(app)
@app.route("/movie-actor-identification", methods=['POST'])
def actor_identification():
if request.method == 'POST':
f = request.files['movie']
f.save("Film-Trailer/"+secure_filename(f.filename))
# return 'file uploaded successfully'
# Starting moudle running
face_para = cv2.CascadeClassifier('../Body_Parameters/haarcascade_frontalface_default.xml');
face_recognizer = cv2.face.LBPHFaceRecognizer_create()
face_recognizer.read('../Trained_file/face_trained.yml')
people = ['Channa Perera', 'Danu Innasithamby', 'Dinakshie Priyasad', 'Eraj Gunewardena',
'Iresha Asanki De Silva',
'Mashi Siriwardene', 'Meena Kumari', 'Nadeeka Gunasekara', 'Sarah Illyas', 'Saranga Disasekara',
'Shereen Willis', 'Tanasha Satarasinghe', 'Yureni Noshika']
person = []
Uploadfile = "Film-Trailer/"+f.filename
video_file = Uploadfile.strip()
# print(video_file)
cap = cv2.VideoCapture(video_file) # cap for "Video Capture Object"
if not cap.isOpened():
print("Error opening Video File.")
try:
while True:
# Capture frame-by-frame
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face = face_para.detectMultiScale(gray, 1.2, 8)
for (x, y, w, h) in face:
face_roi = gray[y:y + h, x:x + w]
label, confidence = face_recognizer.predict(face_roi)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(frame, str(people[label]), (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2,
cv2.LINE_AA)
person.append(people[label])
# print(people[label], confidence)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# if frame is read correctly, ret is True
if not ret:
print("Can't retrieve frame - stream may have ended. Exiting..")
break
except:
print("Film Trailer Analyze Complete")
# When everything done, release the capture
removeduplicperson = list(set(person))
print(removeduplicperson)
cap.release()
cv2.destroyAllWindows()
return removeduplicperson
@app.route("/movie-film-marketing", methods=['POST'])
def filmtype_identification():
if request.method == 'POST':
f = request.files['movie']
f.save("Film-Trailer/"+secure_filename(f.filename))
# return 'file uploaded successfully'
# Starting module running
face_para = cv2.CascadeClassifier('../Body_Parameters/haarcascade_frontalface_default.xml');
face_recognizer = cv2.face.LBPHFaceRecognizer_create()
face_recognizer.read('../Trained_file/trainer.yml')
emotion = ['anger', 'happy', 'neutral', 'sad']
x = 1
filmtype = []
romantic = 0
action = 0
comedy = 0
Uploadfile = "Film-Trailer/"+f.filename
video_file = Uploadfile.strip()
# print(video_file)
cap = cv2.VideoCapture(video_file) # cap for "Emotions Video Capture Object"
cap2 = cv2.VideoCapture(video_file) # cap for "Pose Video Capture Object"
fps = cap2.get(cv2.CAP_PROP_FPS)
n = 0
if not cap.isOpened():
print("Error opening Video File.")
try:
if x == 1:
print("Film Trailer Emotions Analyze Started")
x += 1
while True:
# Capture frame-by-frame
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face = face_para.detectMultiScale(gray, 1.3, 8)
for (x, y, w, h) in face:
face_roi = gray[y:y + h, x:x + w]
label, confidence = face_recognizer.predict(face_roi)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(frame, str(emotion[label]), (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0),
2,
cv2.LINE_AA)
if (emotion[label] == "anger"):
filmtype.append("action")
elif (emotion[label] == "happy"):
filmtype.append("romantic")
# filmtype.append("comedy")
elif (emotion[label] == "sad"):
filmtype.append("romantic")
elif (emotion[label] == "neutral"):
filmtype.append("romantic")
filmtype.append("comedy")
# print(people[label], confidence)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# if frame is read correctly, ret is True
if not ret:
print("Can't retrieve frame - stream may have ended. Exiting..")
break
except:
if x == 589:
print("Film Trailer Pose Analyze Started")
while True:
ret, frame2 = cap2.read()
if (7 * n) % fps == 0:
model = YOLO('../Trained_file/best.pt')
# Run inference on image
results = model.predict(frame2) # results list
result = results[0]
# print(len(result.boxes))
for box in result.boxes:
label = result.names[box.cls[0].item()]
filmtype.append(label)
n += 1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if not ret:
# print("Can't retrieve frame - stream may have ended. Exiting..")
break
print("Film Trailer Emotions & Pose Analyze Complete")
for item in filmtype:
if item == "romantic":
romantic += 1
elif item == "action":
action += 1
elif item == "comedy":
comedy += 1
per_romantic = romantic / len(filmtype) * 100
per_action = action / len(filmtype) * 100
per_comedy = comedy / len(filmtype) * 100
romantic_rounded_number = format(per_romantic, ".2f")
action_rounded_number = format(per_action, ".2f")
comedy_rounded_number = format(per_comedy, ".2f")
film_genre_values = {"romantic": romantic_rounded_number, "action": action_rounded_number,
"comedy": comedy_rounded_number}
sorted_genres = sorted(film_genre_values.items(), key=lambda item: float(item[1]), reverse=True)
highest_genre_1 = sorted_genres[0]
highest_genre_2 = sorted_genres[1]
print(highest_genre_1, highest_genre_2)
result = {
"highest_genre_1": {
"genre": highest_genre_1[0],
"value": highest_genre_1[1]
},
"highest_genre_2": {
"genre": highest_genre_2[0],
"value": highest_genre_2[1]
}
}
# When everything done, release the capture
cap2.release()
cv2.destroyAllWindows()
return result
if __name__ == '__main__':
app.run(debug=True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment