Commit 7571afd7 authored by Serasinghe K.A.E.Y.D's avatar Serasinghe K.A.E.Y.D 😊

Merge branch 'yomal-dev' into 'master'

app.py update

See merge request !9
parents 867218c6 5d64f3ee
from inference import *
from flask_cors import CORS
from flask import Flask, Response, jsonify, request
from tensorflow.keras.models import load_model
from youtubesearchpython import VideosSearch
import cv2
from pymongo import MongoClient
import threading
import requests
import numpy as np
# import schedule
import time
import random
from datetime import datetime
from mongodb_api import *
data = {0: 'Angry', 1: 'Disgust', 2: 'Fear', 3: 'Happy', 4: 'Sad', 5: 'Surprise', 6: 'Neutral'}
model = load_model('weights/model.h5')
app = Flask(__name__)
CORS(app)
......@@ -41,6 +47,34 @@ def update_diet_plan():
time.sleep(60)
def pre_processing(img):
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.equalizeHist(img)
img = img / 255
return img
def emo_rec(img):
img = np.asarray(img)
img = cv2.resize(img, (48, 48))
img = pre_processing(img)
img = img.reshape(1, 48, 48, 1)
predict_x = model.predict(img)
classes_x = np.argmax(predict_x, axis=1)
return data[classes_x[0]], int(classes_x[0]) # Convert int64 to int
def get_music_link(emotion):
search_query = f'{emotion} relaxing music'
videos_search = VideosSearch(search_query, limit=10)
results = videos_search.result()
video_links = [video['link'] for video in results['result']]
if not video_links:
return None
selected_link = random.choice(video_links)
return selected_link
thread1 = threading.Thread(target=update_risk_level)
thread1.start()
......@@ -102,6 +136,25 @@ def record():
)
@app.route('/predict', methods=['POST'])
def predict_emotion_and_music():
if 'image' not in request.files:
return jsonify({'error': 'No image part'})
image = request.files['image']
image_np = np.fromstring(image.read(), np.uint8)
image_cv2 = cv2.imdecode(image_np, cv2.IMREAD_COLOR)
emotion_label, emotion_class = emo_rec(image_cv2)
music_link = get_music_link(emotion_label)
if music_link is None:
return jsonify({'emotion': emotion_label, 'class': emotion_class, 'message': 'No music found'})
return jsonify({'emotion': emotion_label, 'class': emotion_class, 'music_link': music_link})
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