Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,8 +18,8 @@ def process_input(uploaded_file, youtube_link, image_url, sensitivity):
|
|
| 18 |
Priority: YouTube link > Image URL > Uploaded file.
|
| 19 |
The sensitivity slider value is passed as the confidence threshold.
|
| 20 |
|
| 21 |
-
For video files (mp4, mov, avi, webm), we
|
| 22 |
-
For images, we use
|
| 23 |
|
| 24 |
Returns a tuple:
|
| 25 |
- download_file_path (for gr.File)
|
|
@@ -65,26 +65,28 @@ def process_input(uploaded_file, youtube_link, image_url, sensitivity):
|
|
| 65 |
output_path = None
|
| 66 |
|
| 67 |
if ext_input in video_exts:
|
| 68 |
-
# Process video using
|
| 69 |
try:
|
| 70 |
-
# Open video to get properties.
|
| 71 |
cap = cv2.VideoCapture(input_path)
|
| 72 |
if not cap.isOpened():
|
| 73 |
return None, None, None, "Error opening video file."
|
| 74 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 75 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 76 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 77 |
-
cap.release()
|
| 78 |
-
|
| 79 |
-
# Use streaming mode to process each frame.
|
| 80 |
frames = []
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
frames.append(annotated_frame)
|
|
|
|
| 85 |
if not frames:
|
| 86 |
-
return None, None, None, "No detections were returned from video
|
| 87 |
-
# Write frames to a temporary video file.
|
| 88 |
temp_video_path = os.path.join(tempfile.gettempdir(), "annotated_video.mp4")
|
| 89 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 90 |
out = cv2.VideoWriter(temp_video_path, fourcc, fps, (width, height))
|
|
@@ -100,7 +102,6 @@ def process_input(uploaded_file, youtube_link, image_url, sensitivity):
|
|
| 100 |
results = model.predict(source=input_path, save=True, conf=sensitivity)
|
| 101 |
except Exception as e:
|
| 102 |
return None, None, None, f"Error running prediction: {e}"
|
| 103 |
-
|
| 104 |
try:
|
| 105 |
if not results or len(results) == 0:
|
| 106 |
return None, None, None, "No detections were returned."
|
|
@@ -147,7 +148,7 @@ with gr.Blocks(css="""
|
|
| 147 |
youtube_input = gr.Textbox(label="YouTube Link", placeholder="https://...")
|
| 148 |
with gr.TabItem("Image URL"):
|
| 149 |
image_url_input = gr.Textbox(label="Image URL", placeholder="https://...")
|
| 150 |
-
sensitivity_slider = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, value=0.
|
| 151 |
label="Sensitivity (Confidence Threshold)")
|
| 152 |
# Right Column: Results display at the top.
|
| 153 |
with gr.Column(scale=2):
|
|
|
|
| 18 |
Priority: YouTube link > Image URL > Uploaded file.
|
| 19 |
The sensitivity slider value is passed as the confidence threshold.
|
| 20 |
|
| 21 |
+
For video files (mp4, mov, avi, webm), we process the video frame-by-frame
|
| 22 |
+
using OpenCV. For images, we use normal prediction.
|
| 23 |
|
| 24 |
Returns a tuple:
|
| 25 |
- download_file_path (for gr.File)
|
|
|
|
| 65 |
output_path = None
|
| 66 |
|
| 67 |
if ext_input in video_exts:
|
| 68 |
+
# Process video frame-by-frame using OpenCV.
|
| 69 |
try:
|
|
|
|
| 70 |
cap = cv2.VideoCapture(input_path)
|
| 71 |
if not cap.isOpened():
|
| 72 |
return None, None, None, "Error opening video file."
|
| 73 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 74 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 75 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
|
|
|
|
|
|
|
|
|
| 76 |
frames = []
|
| 77 |
+
while True:
|
| 78 |
+
ret, frame = cap.read()
|
| 79 |
+
if not ret:
|
| 80 |
+
break
|
| 81 |
+
# Run detection on the frame.
|
| 82 |
+
# Note: model.predict() accepts an image (numpy array) as source.
|
| 83 |
+
result = model.predict(source=frame, conf=sensitivity)[0]
|
| 84 |
+
annotated_frame = result.plot() # returns an annotated frame (numpy array)
|
| 85 |
frames.append(annotated_frame)
|
| 86 |
+
cap.release()
|
| 87 |
if not frames:
|
| 88 |
+
return None, None, None, "No detections were returned from video processing."
|
| 89 |
+
# Write annotated frames to a temporary video file.
|
| 90 |
temp_video_path = os.path.join(tempfile.gettempdir(), "annotated_video.mp4")
|
| 91 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 92 |
out = cv2.VideoWriter(temp_video_path, fourcc, fps, (width, height))
|
|
|
|
| 102 |
results = model.predict(source=input_path, save=True, conf=sensitivity)
|
| 103 |
except Exception as e:
|
| 104 |
return None, None, None, f"Error running prediction: {e}"
|
|
|
|
| 105 |
try:
|
| 106 |
if not results or len(results) == 0:
|
| 107 |
return None, None, None, "No detections were returned."
|
|
|
|
| 148 |
youtube_input = gr.Textbox(label="YouTube Link", placeholder="https://...")
|
| 149 |
with gr.TabItem("Image URL"):
|
| 150 |
image_url_input = gr.Textbox(label="Image URL", placeholder="https://...")
|
| 151 |
+
sensitivity_slider = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, value=0.15,
|
| 152 |
label="Sensitivity (Confidence Threshold)")
|
| 153 |
# Right Column: Results display at the top.
|
| 154 |
with gr.Column(scale=2):
|