kalpit sharma
changes
c514c11
from flask import Flask, request, jsonify , render_template , Response
from flask_cors import CORS
from models.clip_emotion import detect_emotion , detect_age
import cv2
import os
from cnn_emotion import detect_emotion as detect_emotion_cnn
# from cnn_lstm import detect_cnn_lstm_emotion
from cnn_resnet import detect_cnn_resnetemotion
from cnn import detect_cnn
from knn import detect_knn
from svm import detect_svm
from randomforest import detect_rf
from logisticregression import detect_lr
import logging
# Configure logging
logging.basicConfig(
level=logging.DEBUG, # Change to INFO in production
format='[%(asctime)s] %(levelname)s in %(module)s: %(message)s'
)
app = Flask(__name__)
CORS(app)
UPLOAD_FOLDER = "temp_frames"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/knn')
def index_knn():
return render_template('knn.html')
@app.route('/svm')
def index_svm():
return render_template('svm.html')
@app.route('/logistic_regression')
def index_lr():
return render_template('lr.html')
@app.route('/randomforest')
def index_rf():
return render_template('rf.html')
@app.route('/vit')
def index_vit():
return render_template('vit.html')
@app.route('/cnn')
def index_cnnonly():
return render_template('cnn.html')
@app.route('/cnnkeras')
def index_cnn():
return render_template('cnnkeras.html')
@app.route('/cnnlstm')
def index_cnnlstm():
return render_template('cnn_lstm.html')
@app.route('/cnn_resnet')
def index_cnn_resnet():
return render_template('cnn_resnet.html')
def gen(camera):
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
@app.route('/video_feed', methods=["POST"])
def video_feed():
# return Response(gen(VideoCamera()),
# mimetype='multipart/x-mixed-replace; boundary=frame')
if "frame" not in request.files:
return jsonify({"error": "No frame received"}), 400
file = request.files["frame"]
filepath = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(filepath)
try:
emotion, image_base64 = detect_emotion_cnn(filepath)
return jsonify({"emotion": emotion, "image": image_base64})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/analyze", methods=["POST"])
def analyze():
if "frame" not in request.files:
return jsonify({"error": "No frame received"}), 400
file = request.files["frame"]
filepath = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(filepath)
try:
emotion, scores = detect_emotion(filepath)
age, age_scores = detect_age(filepath)
return jsonify({"emotion": emotion, "scores": scores, "age": age})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/cnn_lstm_video_feed', methods=["POST"])
def cnn_lstm_video_feed():
if "frame" not in request.files:
return jsonify({"error": "No frame received"}), 400
file = request.files["frame"]
filepath = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(filepath)
try:
emotion, image_base64 = detect_emotion_cnn(filepath)
return jsonify({"emotion": emotion, "image": image_base64})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/cnn_resnet', methods=['POST'])
def cnn_resnet():
if "frame" not in request.files:
logging.warning("No frame in request")
return jsonify({"error": "No frame received"}), 400
file = request.files["frame"]
filepath = os.path.join(UPLOAD_FOLDER, file.filename)
logging.info(f"File saved to {filepath}")
file.save(filepath)
try:
emotion, image_base64 = detect_cnn_resnetemotion(filepath)
return jsonify({"emotion": emotion, "image": image_base64})
except Exception as e:
logging.error(f"Failed to save file: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/cnn', methods=['POST'])
def cnn():
if "frame" not in request.files:
logging.warning("No frame in request")
return jsonify({"error": "No frame received"}), 400
file = request.files["frame"]
filepath = os.path.join(UPLOAD_FOLDER, file.filename)
logging.info(f"File saved to {filepath}")
file.save(filepath)
try:
emotion, image_base64 = detect_cnn(filepath)
return jsonify({"emotion": emotion, "image": image_base64})
except Exception as e:
logging.error(f"Failed to save file: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/knn', methods=['POST'])
def knnn():
if "frame" not in request.files:
logging.warning("No frame in request")
return jsonify({"error": "No frame received"}), 400
file = request.files["frame"]
filepath = os.path.join(UPLOAD_FOLDER, file.filename)
logging.info(f"File saved to {filepath}")
file.save(filepath)
try:
emotion, image_base64 = detect_knn(filepath)
return jsonify({"emotion": emotion, "image": image_base64})
except Exception as e:
logging.error(f"Failed to save file: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/svm', methods=['POST'])
def svmm():
if "frame" not in request.files:
logging.warning("No frame in request")
return jsonify({"error": "No frame received"}), 400
file = request.files["frame"]
filepath = os.path.join(UPLOAD_FOLDER, file.filename)
logging.info(f"File saved to {filepath}")
file.save(filepath)
try:
emotion, image_base64 = detect_svm(filepath)
return jsonify({"emotion": emotion, "image": image_base64})
except Exception as e:
logging.error(f"Failed to save file: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/randomforest', methods=['POST'])
def rff():
if "frame" not in request.files:
logging.warning("No frame in request")
return jsonify({"error": "No frame received"}), 400
file = request.files["frame"]
filepath = os.path.join(UPLOAD_FOLDER, file.filename)
logging.info(f"File saved to {filepath}")
file.save(filepath)
try:
emotion, image_base64 = detect_rf(filepath)
return jsonify({"emotion": emotion, "image": image_base64})
except Exception as e:
logging.error(f"Failed to save file: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/logistic_regression', methods=['POST'])
def lr():
if "frame" not in request.files:
logging.warning("No frame in request")
return jsonify({"error": "No frame received"}), 400
file = request.files["frame"]
filepath = os.path.join(UPLOAD_FOLDER, file.filename)
logging.info(f"File saved to {filepath}")
file.save(filepath)
try:
emotion, image_base64 = detect_lr(filepath)
return jsonify({"emotion": emotion, "image": image_base64})
except Exception as e:
logging.error(f"Failed to save file: {e}")
return jsonify({"error": str(e)}), 500
@app.route("/reports")
def show_reports():
svm_report = """[RESULTS] SVM Classification Report
precision recall f1-score support
angry 0.33 0.35 0.34 779
disgust 0.56 0.16 0.25 92
fear 0.33 0.25 0.29 838
happy 0.59 0.68 0.63 1473
neutral 0.42 0.44 0.43 987
sad 0.35 0.33 0.34 977
surprise 0.57 0.54 0.55 596
accuracy 0.45 5742
macro avg 0.45 0.39 0.40 5742
weighted avg 0.44 0.45 0.44 5742"""
rf_report = """[RESULTS] Random Forest Classification Report
precision recall f1-score support
angry 0.38 0.20 0.26 779
disgust 1.00 0.27 0.43 92
fear 0.39 0.21 0.28 838
happy 0.47 0.82 0.60 1473
neutral 0.40 0.43 0.41 987
sad 0.37 0.31 0.34 977
surprise 0.71 0.50 0.58 596
accuracy 0.45 5742
macro avg 0.53 0.39 0.41 5742
weighted avg 0.45 0.45 0.42 5742"""
knn_report = """[RESULTS] k-NN Classification Report
precision recall f1-score support
angry 0.34 0.35 0.35 779
disgust 0.39 0.36 0.38 92
fear 0.38 0.31 0.34 838
happy 0.53 0.75 0.62 1473
neutral 0.39 0.42 0.40 987
sad 0.40 0.21 0.28 977
surprise 0.56 0.47 0.51 596
accuracy 0.45 5742
macro avg 0.43 0.41 0.41 5742
weighted avg 0.44 0.45 0.43 5742"""
lr_report = """[RESULTS] Logistic Regression Classification Report
precision recall f1-score support
angry 0.33 0.31 0.32 779
disgust 0.56 0.15 0.24 92
fear 0.32 0.22 0.26 838
happy 0.57 0.70 0.63 1473
neutral 0.41 0.43 0.42 987
sad 0.34 0.31 0.32 977
surprise 0.51 0.55 0.53 596
accuracy 0.44 5742
macro avg 0.43 0.38 0.39 5742
weighted avg 0.43 0.44 0.43 5742"""
cnn_report = """[RESULTS] CNN Emotion Model
Accuracy: 0.3349122318194483
Precision: 0.3389126479893201
Recall: 0.3349122318194483
F1 Score: 0.325833741237971
Confusion Matrix:
[[ 357 0 97 116 164 183 41]
[ 60 1 11 5 8 24 2]
[ 127 0 243 126 151 217 160]
[ 60 0 46 1434 103 69 62]
[ 144 0 129 181 308 450 35]
[ 32 0 84 52 49 19 595]
[ 77 0 66 164 686 198 42]]"""
vit_report = """[RESULTS] ViT-B/32 (CLIP)
Accuracy: 0.4374477570353859
Precision: 0.4877307816123326
Recall: 0.4374477570353859
F1 Score: 0.43121246174906935
Confusion Matrix:
[[ 628 69 54 82 29 29 67]
[ 76 10 2 9 0 4 10]
[ 428 31 87 102 108 90 178]
[ 71 2 12 1282 3 37 367]
[ 486 81 32 66 326 10 246]
[ 170 3 84 107 4 392 71]
[ 510 55 16 107 121 9 415]]"""
cnnrnn_report=""" [RESULTS] CNN + RNN Emotion Model
Accuracy: 0.21301198105321817
Precision: 0.16701683943798576
Recall: 0.21301198105321817
F1 Score: 0.12850228001425937
Confusion Matrix:
[[ 190 0 16 632 1 119 0]
[ 13 0 2 88 0 8 0]
[ 237 0 17 675 0 95 0]
[ 279 0 29 1269 0 197 0]
[ 204 0 19 822 1 201 0]
[ 201 0 19 559 0 52 0]
[ 252 0 29 775 1 176 0]]"""
custom_report=""" [RESULTS] Custom JSON + Weights Model
Accuracy: 0.4577040958484257
Precision: 0.01340278773544221
Recall: 0.4577040958484257
F1 Score: 0.12402427528155929
Confusion Matrix:
[[ 70 0 18 340 1 158 0]
[ 10 0 1 550 0 11 0]
[ 225 0 22 72 0 104 0]
[ 266 0 27 1289 0 174 0]
[ 203 0 19 89 1 127 0]
[ 252 0 15 523 0 81 0]
[ 201 0 27 651 1 133 0]]"""
return render_template(
"classification_reports.html",
svm_report=svm_report,
rf_report=rf_report,
knn_report=knn_report,
lr_report=lr_report,
cnn_report=cnn_report,
vit_report=vit_report,
cnnrnn_report=cnnrnn_report,
custom_report=custom_report
)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)