Cleanup
Browse files
app.py
CHANGED
|
@@ -21,7 +21,36 @@ from transformers import pipeline # Import Hugging Face transformers pipeline
|
|
| 21 |
import requests
|
| 22 |
from io import BytesIO # Import for handling byte streams
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
#
|
| 26 |
#
|
| 27 |
# Function to analyze an input frame and generate an analyzed frame
|
|
@@ -34,7 +63,12 @@ TEXT_SIZE = 1
|
|
| 34 |
LINE_SIZE = 2
|
| 35 |
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
start_time = time.time() # Start timing the analysis
|
| 39 |
img_container["input"] = frame # Store the input frame
|
| 40 |
frame = frame.copy() # Create a copy of the frame to modify
|
|
@@ -44,6 +78,7 @@ def analyze_frame(frame):
|
|
| 44 |
x, y, w, h = result["box"] # Get the bounding box of the detected face
|
| 45 |
face = frame[y : y + h, x : x + w] # Extract the face from the frame
|
| 46 |
sentiment = analyze_sentiment(face) # Analyze the sentiment of the face
|
|
|
|
| 47 |
# Draw a rectangle around the face
|
| 48 |
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), LINE_SIZE)
|
| 49 |
text_size = cv2.getTextSize(sentiment, cv2.FONT_HERSHEY_SIMPLEX, TEXT_SIZE, 2)[
|
|
@@ -92,6 +127,12 @@ def analyze_sentiment(face):
|
|
| 92 |
return dominant_emotion # Return the detected emotion
|
| 93 |
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
# Suppress FFmpeg logs
|
| 96 |
os.environ["FFMPEG_LOG_LEVEL"] = "quiet"
|
| 97 |
|
|
@@ -111,10 +152,6 @@ logging.getLogger("torch").setLevel(logging.ERROR)
|
|
| 111 |
# Suppress Streamlit logs using the logging module
|
| 112 |
logging.getLogger("streamlit").setLevel(logging.ERROR)
|
| 113 |
|
| 114 |
-
|
| 115 |
-
# Initialize the Hugging Face pipeline for facial emotion detection
|
| 116 |
-
emotion_pipeline = pipeline("image-classification", model="trpakov/vit-face-expression")
|
| 117 |
-
|
| 118 |
# Container to hold image data and analysis results
|
| 119 |
img_container = {"input": None, "analyzed": None, "analysis_time": None}
|
| 120 |
|
|
@@ -125,18 +162,6 @@ mtcnn = MTCNN()
|
|
| 125 |
logger = logging.getLogger(__name__)
|
| 126 |
|
| 127 |
|
| 128 |
-
# Named tuple to store detection results
|
| 129 |
-
class Detection(NamedTuple):
|
| 130 |
-
class_id: int
|
| 131 |
-
label: str
|
| 132 |
-
score: float
|
| 133 |
-
box: np.ndarray
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
# Queue to store detection results
|
| 137 |
-
result_queue: "queue.Queue[List[Detection]]" = queue.Queue()
|
| 138 |
-
|
| 139 |
-
|
| 140 |
# Callback function to process video frames
|
| 141 |
# This function is called for each video frame in the WebRTC stream.
|
| 142 |
# It converts the frame to a numpy array in RGB format, analyzes the frame,
|
|
@@ -182,7 +207,7 @@ st.markdown(
|
|
| 182 |
|
| 183 |
# Streamlit page title and subtitle
|
| 184 |
st.title("Computer Vision Playground")
|
| 185 |
-
st.subheader(
|
| 186 |
|
| 187 |
# Columns for input and output streams
|
| 188 |
col1, col2 = st.columns(2)
|
|
@@ -215,7 +240,7 @@ with col1:
|
|
| 215 |
)
|
| 216 |
|
| 217 |
# Text input for video URL
|
| 218 |
-
st.subheader("Or Enter Video URL")
|
| 219 |
video_url = st.text_input("Video URL")
|
| 220 |
|
| 221 |
|
|
|
|
| 21 |
import requests
|
| 22 |
from io import BytesIO # Import for handling byte streams
|
| 23 |
|
| 24 |
+
|
| 25 |
+
# Named tuple to store detection results
|
| 26 |
+
class Detection(NamedTuple):
|
| 27 |
+
class_id: int
|
| 28 |
+
label: str
|
| 29 |
+
score: float
|
| 30 |
+
box: np.ndarray
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# Queue to store detection results
|
| 34 |
+
result_queue: "queue.Queue[List[Detection]]" = queue.Queue()
|
| 35 |
+
|
| 36 |
+
# CHANGE CODE BELOW HERE, USE TO REPLACE WITH YOUR WANTED ANALYSIS.
|
| 37 |
+
# Update below string to set display title of analysis
|
| 38 |
+
|
| 39 |
+
# Appropriate imports needed for analysis
|
| 40 |
+
|
| 41 |
+
from mtcnn import MTCNN # Import MTCNN for face detection
|
| 42 |
+
from PIL import Image, ImageDraw # Import PIL for image processing
|
| 43 |
+
from transformers import pipeline # Import Hugging Face transformers pipeline
|
| 44 |
+
|
| 45 |
+
# Initialize the Hugging Face pipeline for facial emotion detection
|
| 46 |
+
emotion_pipeline = pipeline("image-classification", model="trpakov/vit-face-expression")
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# Default title - "Facial Sentiment Analysis"
|
| 50 |
+
|
| 51 |
+
ANALYSIS_TITLE = "Facial Sentiment Analysis"
|
| 52 |
+
|
| 53 |
+
# CHANGE THE CONTENTS OF THIS FUNCTION, USE TO REPLACE WITH YOUR WANTED ANALYSIS.
|
| 54 |
#
|
| 55 |
#
|
| 56 |
# Function to analyze an input frame and generate an analyzed frame
|
|
|
|
| 63 |
LINE_SIZE = 2
|
| 64 |
|
| 65 |
|
| 66 |
+
# Set analysis results in img_container and result queue for display
|
| 67 |
+
# img_container["input"] - holds the input frame contents - of type np.ndarray
|
| 68 |
+
# img_container["analyzed"] - holds the analyzed frame with any added annotations - of type np.ndarray
|
| 69 |
+
# img_container["analysis_time"] - holds how long the analysis has taken in miliseconds
|
| 70 |
+
# result_queue - holds the analysis metadata results - of type queue.Queue[List[Detection]]
|
| 71 |
+
def analyze_frame(frame: np.ndarray):
|
| 72 |
start_time = time.time() # Start timing the analysis
|
| 73 |
img_container["input"] = frame # Store the input frame
|
| 74 |
frame = frame.copy() # Create a copy of the frame to modify
|
|
|
|
| 78 |
x, y, w, h = result["box"] # Get the bounding box of the detected face
|
| 79 |
face = frame[y : y + h, x : x + w] # Extract the face from the frame
|
| 80 |
sentiment = analyze_sentiment(face) # Analyze the sentiment of the face
|
| 81 |
+
result["label"] = sentiment
|
| 82 |
# Draw a rectangle around the face
|
| 83 |
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), LINE_SIZE)
|
| 84 |
text_size = cv2.getTextSize(sentiment, cv2.FONT_HERSHEY_SIMPLEX, TEXT_SIZE, 2)[
|
|
|
|
| 127 |
return dominant_emotion # Return the detected emotion
|
| 128 |
|
| 129 |
|
| 130 |
+
#
|
| 131 |
+
#
|
| 132 |
+
# DO NOT TOUCH THE BELOW CODE (NOT NEEDED)
|
| 133 |
+
#
|
| 134 |
+
#
|
| 135 |
+
|
| 136 |
# Suppress FFmpeg logs
|
| 137 |
os.environ["FFMPEG_LOG_LEVEL"] = "quiet"
|
| 138 |
|
|
|
|
| 152 |
# Suppress Streamlit logs using the logging module
|
| 153 |
logging.getLogger("streamlit").setLevel(logging.ERROR)
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
# Container to hold image data and analysis results
|
| 156 |
img_container = {"input": None, "analyzed": None, "analysis_time": None}
|
| 157 |
|
|
|
|
| 162 |
logger = logging.getLogger(__name__)
|
| 163 |
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
# Callback function to process video frames
|
| 166 |
# This function is called for each video frame in the WebRTC stream.
|
| 167 |
# It converts the frame to a numpy array in RGB format, analyzes the frame,
|
|
|
|
| 207 |
|
| 208 |
# Streamlit page title and subtitle
|
| 209 |
st.title("Computer Vision Playground")
|
| 210 |
+
st.subheader(ANALYSIS_TITLE)
|
| 211 |
|
| 212 |
# Columns for input and output streams
|
| 213 |
col1, col2 = st.columns(2)
|
|
|
|
| 240 |
)
|
| 241 |
|
| 242 |
# Text input for video URL
|
| 243 |
+
st.subheader("Or Enter Video Download URL")
|
| 244 |
video_url = st.text_input("Video URL")
|
| 245 |
|
| 246 |
|