Upload 2 files
Browse files- app.py +83 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
# Load processor and model
|
| 10 |
+
processor = AutoImageProcessor.from_pretrained("RickyIG/emotion_face_image_classification")
|
| 11 |
+
model = AutoModelForImageClassification.from_pretrained("RickyIG/emotion_face_image_classification")
|
| 12 |
+
|
| 13 |
+
# Title of the Streamlit app
|
| 14 |
+
st.title("Emotion Detection App")
|
| 15 |
+
|
| 16 |
+
# Option to choose between uploading image or using live camera
|
| 17 |
+
option = st.radio("Select an option", ("Upload Image", "Use Live Camera"))
|
| 18 |
+
|
| 19 |
+
if option == "Upload Image":
|
| 20 |
+
# Upload image
|
| 21 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 22 |
+
|
| 23 |
+
if uploaded_file is not None:
|
| 24 |
+
# Display the uploaded image
|
| 25 |
+
image = Image.open(uploaded_file)
|
| 26 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 27 |
+
|
| 28 |
+
# Preprocess the image
|
| 29 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 30 |
+
|
| 31 |
+
# Make predictions
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
outputs = model(**inputs)
|
| 34 |
+
logits = outputs.logits # raw model outputs (before softmax)
|
| 35 |
+
predicted_class_idx = logits.argmax(-1).item() # predicted class index
|
| 36 |
+
|
| 37 |
+
# Get the label of the predicted class
|
| 38 |
+
label = model.config.id2label[predicted_class_idx]
|
| 39 |
+
|
| 40 |
+
# Display the result
|
| 41 |
+
st.write(f"Predicted Emotion: {label}")
|
| 42 |
+
|
| 43 |
+
elif option == "Use Live Camera":
|
| 44 |
+
# Use OpenCV to capture video from the front camera
|
| 45 |
+
cap = cv2.VideoCapture(0)
|
| 46 |
+
|
| 47 |
+
if not cap.isOpened():
|
| 48 |
+
st.error("Error: Could not open webcam.")
|
| 49 |
+
else:
|
| 50 |
+
stframe = st.empty() # Placeholder to display live camera feed
|
| 51 |
+
|
| 52 |
+
while True:
|
| 53 |
+
# Capture frame-by-frame
|
| 54 |
+
ret, frame = cap.read()
|
| 55 |
+
|
| 56 |
+
if not ret:
|
| 57 |
+
st.error("Error: Failed to capture frame.")
|
| 58 |
+
break
|
| 59 |
+
|
| 60 |
+
# Convert frame (BGR) to RGB (PIL format)
|
| 61 |
+
image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 62 |
+
|
| 63 |
+
# Preprocess the image
|
| 64 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 65 |
+
|
| 66 |
+
# Make predictions
|
| 67 |
+
with torch.no_grad():
|
| 68 |
+
outputs = model(**inputs)
|
| 69 |
+
logits = outputs.logits # raw model outputs (before softmax)
|
| 70 |
+
predicted_class_idx = logits.argmax(-1).item() # predicted class index
|
| 71 |
+
|
| 72 |
+
# Get the label of the predicted class
|
| 73 |
+
label = model.config.id2label[predicted_class_idx]
|
| 74 |
+
|
| 75 |
+
# Display the result
|
| 76 |
+
cv2.putText(frame, f"Emotion: {label}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
|
| 77 |
+
|
| 78 |
+
# Convert the frame to RGB for Streamlit
|
| 79 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 80 |
+
stframe.image(frame_rgb, channels="RGB", use_column_width=True)
|
| 81 |
+
|
| 82 |
+
# Release the capture when finished
|
| 83 |
+
cap.release()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.41.1
|
| 2 |
+
opencv-python==4.10.0.84
|
| 3 |
+
torch==2.5.1
|
| 4 |
+
transformers==4.33.2
|
| 5 |
+
Pillow==11.0.0
|
| 6 |
+
torch==2.5.1
|
| 7 |
+
tokenizers==0.13.3
|