Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,19 +14,14 @@ import os
|
|
| 14 |
mp_face_detection = mp.solutions.face_detection
|
| 15 |
|
| 16 |
def convert_video_if_needed(video_path):
|
| 17 |
-
"""Convert video to mp4 if it's in
|
| 18 |
-
if video_path.lower().endswith('.avi')
|
| 19 |
output_path = video_path.rsplit('.', 1)[0] + '.mp4'
|
| 20 |
try:
|
| 21 |
-
cap = cv2.VideoCapture(video_path
|
| 22 |
-
if not cap.isOpened():
|
| 23 |
-
raise ValueError("Cannot open the video file for conversion.")
|
| 24 |
-
|
| 25 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 29 |
-
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
| 30 |
|
| 31 |
while cap.isOpened():
|
| 32 |
ret, frame = cap.read()
|
|
@@ -39,18 +34,23 @@ def convert_video_if_needed(video_path):
|
|
| 39 |
return output_path
|
| 40 |
except Exception as e:
|
| 41 |
print(f"Error converting video: {e}")
|
| 42 |
-
return video_path
|
| 43 |
return video_path
|
| 44 |
|
| 45 |
def process_video(video_path):
|
| 46 |
-
"""Process video to calculate respiration rate
|
| 47 |
-
cap = cv2.VideoCapture(video_path, cv2.CAP_FFMPEG)
|
| 48 |
if not cap.isOpened():
|
| 49 |
raise ValueError("Error opening video file. Please check the format or path.")
|
| 50 |
|
| 51 |
points = (0, 0, 0, 0)
|
| 52 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
|
|
|
|
|
|
|
|
|
| 53 |
num_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
|
|
|
|
|
|
| 54 |
|
| 55 |
# Detect face for the first frame
|
| 56 |
with mp_face_detection.FaceDetection(model_selection=1, min_detection_confidence=0.5) as face_detection:
|
|
@@ -89,7 +89,7 @@ def process_video(video_path):
|
|
| 89 |
|
| 90 |
cap.release()
|
| 91 |
|
| 92 |
-
# Apply Band-Pass
|
| 93 |
data = val_list
|
| 94 |
lowcut = 0.16
|
| 95 |
highcut = 0.5
|
|
@@ -103,7 +103,10 @@ def process_video(video_path):
|
|
| 103 |
|
| 104 |
# Find peaks and calculate metrics
|
| 105 |
peaks, _ = find_peaks(filtered)
|
| 106 |
-
time_length = num_frames / fps
|
|
|
|
|
|
|
|
|
|
| 107 |
respiration_times = peaks / fps
|
| 108 |
rr = math.ceil((len(peaks) / time_length) * 60)
|
| 109 |
|
|
@@ -134,7 +137,7 @@ def process_video(video_path):
|
|
| 134 |
}
|
| 135 |
|
| 136 |
def process_input(video_path):
|
| 137 |
-
"""Handle input video processing with error handling
|
| 138 |
try:
|
| 139 |
if video_path is None:
|
| 140 |
return ["Please upload a video file."] * 5 + [None]
|
|
@@ -176,7 +179,30 @@ interface = gr.Interface(
|
|
| 176 |
title="Respiration Rate Analyzer",
|
| 177 |
description="🫁 Analyze your respiration rate with ease!",
|
| 178 |
theme="default",
|
| 179 |
-
css="""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
)
|
| 181 |
|
| 182 |
interface.launch()
|
|
|
|
| 14 |
mp_face_detection = mp.solutions.face_detection
|
| 15 |
|
| 16 |
def convert_video_if_needed(video_path):
|
| 17 |
+
"""Convert video to mp4 if it's in avi format"""
|
| 18 |
+
if video_path.lower().endswith('.avi'):
|
| 19 |
output_path = video_path.rsplit('.', 1)[0] + '.mp4'
|
| 20 |
try:
|
| 21 |
+
cap = cv2.VideoCapture(video_path)
|
|
|
|
|
|
|
|
|
|
| 22 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 23 |
+
out = cv2.VideoWriter(output_path, fourcc, 30.0,
|
| 24 |
+
(int(cap.get(3)), int(cap.get(4))))
|
|
|
|
|
|
|
| 25 |
|
| 26 |
while cap.isOpened():
|
| 27 |
ret, frame = cap.read()
|
|
|
|
| 34 |
return output_path
|
| 35 |
except Exception as e:
|
| 36 |
print(f"Error converting video: {e}")
|
| 37 |
+
return video_path
|
| 38 |
return video_path
|
| 39 |
|
| 40 |
def process_video(video_path):
|
| 41 |
+
"""Process video to calculate respiration rate"""
|
| 42 |
+
cap = cv2.VideoCapture(video_path, cv2.CAP_FFMPEG)
|
| 43 |
if not cap.isOpened():
|
| 44 |
raise ValueError("Error opening video file. Please check the format or path.")
|
| 45 |
|
| 46 |
points = (0, 0, 0, 0)
|
| 47 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 48 |
+
if not fps or fps == 0:
|
| 49 |
+
fps = 30.0 # Default FPS
|
| 50 |
+
|
| 51 |
num_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 52 |
+
if not num_frames or num_frames <= 0:
|
| 53 |
+
num_frames = 1 # Avoid division by zero
|
| 54 |
|
| 55 |
# Detect face for the first frame
|
| 56 |
with mp_face_detection.FaceDetection(model_selection=1, min_detection_confidence=0.5) as face_detection:
|
|
|
|
| 89 |
|
| 90 |
cap.release()
|
| 91 |
|
| 92 |
+
# Apply Band-Pass-Filter
|
| 93 |
data = val_list
|
| 94 |
lowcut = 0.16
|
| 95 |
highcut = 0.5
|
|
|
|
| 103 |
|
| 104 |
# Find peaks and calculate metrics
|
| 105 |
peaks, _ = find_peaks(filtered)
|
| 106 |
+
time_length = num_frames / fps if fps > 0 else 0.0
|
| 107 |
+
if time_length == 0.0:
|
| 108 |
+
raise ValueError("Unable to determine video duration.")
|
| 109 |
+
|
| 110 |
respiration_times = peaks / fps
|
| 111 |
rr = math.ceil((len(peaks) / time_length) * 60)
|
| 112 |
|
|
|
|
| 137 |
}
|
| 138 |
|
| 139 |
def process_input(video_path):
|
| 140 |
+
"""Handle input video processing with error handling"""
|
| 141 |
try:
|
| 142 |
if video_path is None:
|
| 143 |
return ["Please upload a video file."] * 5 + [None]
|
|
|
|
| 179 |
title="Respiration Rate Analyzer",
|
| 180 |
description="🫁 Analyze your respiration rate with ease!",
|
| 181 |
theme="default",
|
| 182 |
+
css="""
|
| 183 |
+
body {
|
| 184 |
+
background-color: #001f3f;
|
| 185 |
+
color: #f0f8ff;
|
| 186 |
+
}
|
| 187 |
+
.output {
|
| 188 |
+
font-size: 16px;
|
| 189 |
+
color: #f0f8ff;
|
| 190 |
+
}
|
| 191 |
+
.label {
|
| 192 |
+
font-weight: bold;
|
| 193 |
+
color: #0074D9;
|
| 194 |
+
}
|
| 195 |
+
.plot {
|
| 196 |
+
border: 2px solid #0074D9;
|
| 197 |
+
border-radius: 10px;
|
| 198 |
+
}
|
| 199 |
+
.gradio-container {
|
| 200 |
+
background-color: #002b57;
|
| 201 |
+
}
|
| 202 |
+
.output-markdown p {
|
| 203 |
+
color: #f0f8ff !important;
|
| 204 |
+
}
|
| 205 |
+
"""
|
| 206 |
)
|
| 207 |
|
| 208 |
interface.launch()
|