Spaces:
Running
Running
vijul.shah
commited on
Commit
·
5f721d1
1
Parent(s):
8f8ef33
Added EAR Plots. TODO: solve color jitter bug
Browse files- app.py +37 -2
- app_utils.py +9 -3
- feature_extraction/extractor_mediapipe.py +2 -1
app.py
CHANGED
|
@@ -100,7 +100,7 @@ def main():
|
|
| 100 |
with st.spinner("Analyzing..."):
|
| 101 |
|
| 102 |
if is_image(file_extension):
|
| 103 |
-
input_frames, output_frames, predicted_diameters, face_frames = process_frames(
|
| 104 |
cols,
|
| 105 |
[input_img],
|
| 106 |
tv_model,
|
|
@@ -130,7 +130,7 @@ def main():
|
|
| 130 |
|
| 131 |
elif is_video(file_extension):
|
| 132 |
output_video_path = f"{root_path}/tmp.webm"
|
| 133 |
-
input_frames, output_frames, predicted_diameters, face_frames = process_video(
|
| 134 |
cols,
|
| 135 |
video_frames,
|
| 136 |
tv_model,
|
|
@@ -187,6 +187,41 @@ def main():
|
|
| 187 |
# Display the Altair chart
|
| 188 |
st.altair_chart(chart, use_container_width=True)
|
| 189 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
if __name__ == "__main__":
|
| 192 |
main()
|
|
|
|
| 100 |
with st.spinner("Analyzing..."):
|
| 101 |
|
| 102 |
if is_image(file_extension):
|
| 103 |
+
input_frames, output_frames, predicted_diameters, face_frames, eyes_ratios = process_frames(
|
| 104 |
cols,
|
| 105 |
[input_img],
|
| 106 |
tv_model,
|
|
|
|
| 130 |
|
| 131 |
elif is_video(file_extension):
|
| 132 |
output_video_path = f"{root_path}/tmp.webm"
|
| 133 |
+
input_frames, output_frames, predicted_diameters, face_frames, eyes_ratios = process_video(
|
| 134 |
cols,
|
| 135 |
video_frames,
|
| 136 |
tv_model,
|
|
|
|
| 187 |
# Display the Altair chart
|
| 188 |
st.altair_chart(chart, use_container_width=True)
|
| 189 |
|
| 190 |
+
if eyes_ratios is not None and len(eyes_ratios) > 0:
|
| 191 |
+
df = pd.DataFrame(eyes_ratios, columns=["Eyes Aspect Ratio"])
|
| 192 |
+
df["Frame"] = range(1, len(eyes_ratios) + 1) # Create a frame column starting from 1
|
| 193 |
+
|
| 194 |
+
# Create an Altair chart for eyes_ratios
|
| 195 |
+
line_chart = (
|
| 196 |
+
alt.Chart(df)
|
| 197 |
+
.mark_line(point=True, color=colors[-1]) # Set color of the line
|
| 198 |
+
.encode(
|
| 199 |
+
x=alt.X("Frame:Q", title="Frame Number"),
|
| 200 |
+
y=alt.Y("Eyes Aspect Ratio:Q", title="Eyes Aspect Ratio"),
|
| 201 |
+
tooltip=[
|
| 202 |
+
alt.Tooltip("Frame:Q", title="Frame Number"),
|
| 203 |
+
alt.Tooltip("Eyes Aspect Ratio:Q", title="Eyes Aspect Ratio"),
|
| 204 |
+
],
|
| 205 |
+
)
|
| 206 |
+
# .properties(title="Eyes Aspect Ratios (EARs)")
|
| 207 |
+
# .configure_axis(grid=True)
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
# Create a horizontal rule at y=0.22
|
| 211 |
+
line1 = alt.Chart(pd.DataFrame({"y": [0.22]})).mark_rule(color="red").encode(y="y:Q")
|
| 212 |
+
|
| 213 |
+
line2 = alt.Chart(pd.DataFrame({"y": [0.25]})).mark_rule(color="blue").encode(y="y:Q")
|
| 214 |
+
|
| 215 |
+
# Combine line chart and horizontal line, and apply configuration
|
| 216 |
+
final_chart = line_chart.properties(title="Eyes Aspect Ratios (EARs)") + line1 + line2
|
| 217 |
+
|
| 218 |
+
# Configure axis properties at the chart level
|
| 219 |
+
final_chart = final_chart.configure_axis(grid=True)
|
| 220 |
+
|
| 221 |
+
# Display the Altair chart
|
| 222 |
+
st.subheader("Eyes Aspect Ratios (EARs)")
|
| 223 |
+
st.altair_chart(final_chart, use_container_width=True)
|
| 224 |
+
|
| 225 |
|
| 226 |
if __name__ == "__main__":
|
| 227 |
main()
|
app_utils.py
CHANGED
|
@@ -238,6 +238,8 @@ def process_frames(
|
|
| 238 |
]
|
| 239 |
preprocess_function = transforms.Compose(preprocess_steps)
|
| 240 |
|
|
|
|
|
|
|
| 241 |
for idx, input_img in enumerate(input_imgs):
|
| 242 |
|
| 243 |
img = np.array(input_img)
|
|
@@ -246,6 +248,7 @@ def process_frames(
|
|
| 246 |
left_eye = None
|
| 247 |
right_eye = None
|
| 248 |
blinked = False
|
|
|
|
| 249 |
|
| 250 |
if ds_results is not None and "face" in ds_results:
|
| 251 |
face_img = to_pil_image(ds_results["face"])
|
|
@@ -257,6 +260,9 @@ def process_frames(
|
|
| 257 |
|
| 258 |
if ds_results is not None and "eyes" in ds_results.keys():
|
| 259 |
blinked = ds_results["eyes"]["blinked"]
|
|
|
|
|
|
|
|
|
|
| 260 |
if "left_eye" in ds_results["eyes"].keys() and ds_results["eyes"]["left_eye"] is not None:
|
| 261 |
left_eye = ds_results["eyes"]["left_eye"]
|
| 262 |
left_eye = to_pil_image(left_eye).convert("RGB")
|
|
@@ -368,7 +374,7 @@ def process_frames(
|
|
| 368 |
show_cam_frames(output_frames, output_path, codec, video_output_placeholders)
|
| 369 |
show_pred_text_frames(output_frames, output_path, predicted_diameters, codec, video_predictions_placeholders)
|
| 370 |
|
| 371 |
-
return input_frames, output_frames, predicted_diameters, face_frames
|
| 372 |
|
| 373 |
|
| 374 |
# Function to display video with autoplay and loop
|
|
@@ -468,11 +474,11 @@ def process_video(cols, video_frames, tv_model, pupil_selection, output_path, ca
|
|
| 468 |
file_format = output_path.split(".")[-1]
|
| 469 |
codec, extension = get_codec_and_extension(file_format)
|
| 470 |
|
| 471 |
-
input_frames, output_frames, predicted_diameters, face_frames = process_frames(
|
| 472 |
cols, resized_frames, tv_model, pupil_selection, cam_method, output_path, codec, blink_detection
|
| 473 |
)
|
| 474 |
|
| 475 |
-
return input_frames, output_frames, predicted_diameters, face_frames
|
| 476 |
|
| 477 |
|
| 478 |
# Function to convert string values to float or None
|
|
|
|
| 238 |
]
|
| 239 |
preprocess_function = transforms.Compose(preprocess_steps)
|
| 240 |
|
| 241 |
+
eyes_ratios = []
|
| 242 |
+
|
| 243 |
for idx, input_img in enumerate(input_imgs):
|
| 244 |
|
| 245 |
img = np.array(input_img)
|
|
|
|
| 248 |
left_eye = None
|
| 249 |
right_eye = None
|
| 250 |
blinked = False
|
| 251 |
+
eyes_ratio = None
|
| 252 |
|
| 253 |
if ds_results is not None and "face" in ds_results:
|
| 254 |
face_img = to_pil_image(ds_results["face"])
|
|
|
|
| 260 |
|
| 261 |
if ds_results is not None and "eyes" in ds_results.keys():
|
| 262 |
blinked = ds_results["eyes"]["blinked"]
|
| 263 |
+
eyes_ratio = ds_results["eyes"]["eyes_ratio"]
|
| 264 |
+
if eyes_ratio is not None:
|
| 265 |
+
eyes_ratios.append(eyes_ratio)
|
| 266 |
if "left_eye" in ds_results["eyes"].keys() and ds_results["eyes"]["left_eye"] is not None:
|
| 267 |
left_eye = ds_results["eyes"]["left_eye"]
|
| 268 |
left_eye = to_pil_image(left_eye).convert("RGB")
|
|
|
|
| 374 |
show_cam_frames(output_frames, output_path, codec, video_output_placeholders)
|
| 375 |
show_pred_text_frames(output_frames, output_path, predicted_diameters, codec, video_predictions_placeholders)
|
| 376 |
|
| 377 |
+
return input_frames, output_frames, predicted_diameters, face_frames, eyes_ratios
|
| 378 |
|
| 379 |
|
| 380 |
# Function to display video with autoplay and loop
|
|
|
|
| 474 |
file_format = output_path.split(".")[-1]
|
| 475 |
codec, extension = get_codec_and_extension(file_format)
|
| 476 |
|
| 477 |
+
input_frames, output_frames, predicted_diameters, face_frames, eyes_ratios = process_frames(
|
| 478 |
cols, resized_frames, tv_model, pupil_selection, cam_method, output_path, codec, blink_detection
|
| 479 |
)
|
| 480 |
|
| 481 |
+
return input_frames, output_frames, predicted_diameters, face_frames, eyes_ratios
|
| 482 |
|
| 483 |
|
| 484 |
# Function to convert string values to float or None
|
feature_extraction/extractor_mediapipe.py
CHANGED
|
@@ -245,6 +245,7 @@ class ExtractorMediaPipe:
|
|
| 245 |
left_eye = self.extract_eyes_regions(image, face_landmarks, self.LEFT_EYE)
|
| 246 |
right_eye = self.extract_eyes_regions(image, face_landmarks, self.RIGHT_EYE)
|
| 247 |
blinked = False
|
|
|
|
| 248 |
|
| 249 |
if blink_detection:
|
| 250 |
mesh_coordinates = self.landmarksDetection(image, results, False)
|
|
@@ -266,7 +267,7 @@ class ExtractorMediaPipe:
|
|
| 266 |
else:
|
| 267 |
blinked = False
|
| 268 |
|
| 269 |
-
return {"left_eye": left_eye, "right_eye": right_eye, "blinked": blinked}
|
| 270 |
|
| 271 |
@staticmethod
|
| 272 |
def segment_iris(iris_img):
|
|
|
|
| 245 |
left_eye = self.extract_eyes_regions(image, face_landmarks, self.LEFT_EYE)
|
| 246 |
right_eye = self.extract_eyes_regions(image, face_landmarks, self.RIGHT_EYE)
|
| 247 |
blinked = False
|
| 248 |
+
eyes_ratio = None
|
| 249 |
|
| 250 |
if blink_detection:
|
| 251 |
mesh_coordinates = self.landmarksDetection(image, results, False)
|
|
|
|
| 267 |
else:
|
| 268 |
blinked = False
|
| 269 |
|
| 270 |
+
return {"left_eye": left_eye, "right_eye": right_eye, "blinked": blinked, "eyes_ratio": eyes_ratio}
|
| 271 |
|
| 272 |
@staticmethod
|
| 273 |
def segment_iris(iris_img):
|