Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,25 +6,18 @@ import cv2
|
|
6 |
import requests
|
7 |
from ultralytics import YOLO
|
8 |
|
9 |
-
# Remove extra CLI arguments that Spaces might pass
|
10 |
sys.argv = [arg for arg in sys.argv if arg != "--import"]
|
11 |
|
12 |
-
# Load the YOLO11-pose model
|
13 |
model = YOLO("yolo11n-pose.pt")
|
14 |
|
15 |
def process_input(uploaded_file, youtube_link, image_url, sensitivity):
|
16 |
-
"""
|
17 |
-
Process input from Upload, YouTube, or Image URL.
|
18 |
-
Priority: YouTube link > Image URL > Uploaded file.
|
19 |
-
Sensitivity is the confidence threshold.
|
20 |
-
"""
|
21 |
input_path = None
|
22 |
temp_files = []
|
23 |
|
24 |
-
#
|
25 |
if youtube_link and youtube_link.strip():
|
26 |
try:
|
27 |
-
from pytubefix import YouTube
|
28 |
yt = YouTube(youtube_link)
|
29 |
stream = yt.streams.filter(file_extension='mp4', progressive=True).order_by("resolution").desc().first()
|
30 |
if not stream:
|
@@ -35,8 +28,6 @@ def process_input(uploaded_file, youtube_link, image_url, sensitivity):
|
|
35 |
temp_files.append(input_path)
|
36 |
except Exception as e:
|
37 |
return None, None, None, f"Error downloading YouTube video: {str(e)}"
|
38 |
-
|
39 |
-
# Priority 2: Image URL
|
40 |
elif image_url and image_url.strip():
|
41 |
try:
|
42 |
response = requests.get(image_url, stream=True, timeout=10)
|
@@ -48,14 +39,12 @@ def process_input(uploaded_file, youtube_link, image_url, sensitivity):
|
|
48 |
temp_files.append(input_path)
|
49 |
except Exception as e:
|
50 |
return None, None, None, f"Error downloading image: {str(e)}"
|
51 |
-
|
52 |
-
# Priority 3: Uploaded file
|
53 |
elif uploaded_file is not None:
|
54 |
input_path = uploaded_file.name
|
55 |
else:
|
56 |
return None, None, None, "Please provide an input."
|
57 |
|
58 |
-
# Process
|
59 |
ext = os.path.splitext(input_path)[1].lower()
|
60 |
video_exts = [".mp4", ".mov", ".avi", ".webm"]
|
61 |
output_path = None
|
@@ -65,38 +54,50 @@ def process_input(uploaded_file, youtube_link, image_url, sensitivity):
|
|
65 |
# Video processing
|
66 |
cap = cv2.VideoCapture(input_path)
|
67 |
if not cap.isOpened():
|
68 |
-
return None, None, None, "
|
69 |
|
70 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
71 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
72 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
|
|
|
|
|
|
|
|
73 |
|
74 |
-
# Create output video
|
75 |
output_path = os.path.join(tempfile.gettempdir(), f"out_{os.urandom(8).hex()}.mp4")
|
76 |
-
|
|
|
77 |
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
78 |
|
|
|
|
|
|
|
|
|
79 |
while True:
|
80 |
ret, frame = cap.read()
|
81 |
if not ret:
|
82 |
break
|
83 |
|
84 |
-
#
|
85 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
86 |
results = model.predict(source=frame_rgb, conf=sensitivity)[0]
|
87 |
annotated_frame = results.plot()
|
88 |
-
# Convert back to BGR for video writing
|
89 |
annotated_frame_bgr = cv2.cvtColor(annotated_frame, cv2.COLOR_RGB2BGR)
|
|
|
90 |
out.write(annotated_frame_bgr)
|
|
|
91 |
|
92 |
cap.release()
|
93 |
out.release()
|
94 |
temp_files.append(output_path)
|
95 |
|
96 |
-
if
|
97 |
-
return None, None, None, "
|
|
|
|
|
|
|
98 |
|
99 |
-
return output_path, None, output_path, "Video processed successfully!"
|
100 |
|
101 |
else:
|
102 |
# Image processing
|
@@ -109,17 +110,16 @@ def process_input(uploaded_file, youtube_link, image_url, sensitivity):
|
|
109 |
|
110 |
except Exception as e:
|
111 |
return None, None, None, f"Processing error: {str(e)}"
|
112 |
-
|
113 |
finally:
|
114 |
-
|
115 |
-
for f in temp_files[:-1]: # Exclude output_path
|
116 |
if f and os.path.exists(f):
|
117 |
try:
|
118 |
os.remove(f)
|
119 |
except:
|
120 |
pass
|
121 |
|
122 |
-
# Gradio interface
|
123 |
with gr.Blocks(css="""
|
124 |
.result_img > img {
|
125 |
width: 100%;
|
|
|
6 |
import requests
|
7 |
from ultralytics import YOLO
|
8 |
|
|
|
9 |
sys.argv = [arg for arg in sys.argv if arg != "--import"]
|
10 |
|
|
|
11 |
model = YOLO("yolo11n-pose.pt")
|
12 |
|
13 |
def process_input(uploaded_file, youtube_link, image_url, sensitivity):
|
|
|
|
|
|
|
|
|
|
|
14 |
input_path = None
|
15 |
temp_files = []
|
16 |
|
17 |
+
# Input priority handling
|
18 |
if youtube_link and youtube_link.strip():
|
19 |
try:
|
20 |
+
from pytubefix import YouTube
|
21 |
yt = YouTube(youtube_link)
|
22 |
stream = yt.streams.filter(file_extension='mp4', progressive=True).order_by("resolution").desc().first()
|
23 |
if not stream:
|
|
|
28 |
temp_files.append(input_path)
|
29 |
except Exception as e:
|
30 |
return None, None, None, f"Error downloading YouTube video: {str(e)}"
|
|
|
|
|
31 |
elif image_url and image_url.strip():
|
32 |
try:
|
33 |
response = requests.get(image_url, stream=True, timeout=10)
|
|
|
39 |
temp_files.append(input_path)
|
40 |
except Exception as e:
|
41 |
return None, None, None, f"Error downloading image: {str(e)}"
|
|
|
|
|
42 |
elif uploaded_file is not None:
|
43 |
input_path = uploaded_file.name
|
44 |
else:
|
45 |
return None, None, None, "Please provide an input."
|
46 |
|
47 |
+
# Process file
|
48 |
ext = os.path.splitext(input_path)[1].lower()
|
49 |
video_exts = [".mp4", ".mov", ".avi", ".webm"]
|
50 |
output_path = None
|
|
|
54 |
# Video processing
|
55 |
cap = cv2.VideoCapture(input_path)
|
56 |
if not cap.isOpened():
|
57 |
+
return None, None, None, f"Cannot open video file: {input_path}"
|
58 |
|
59 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
60 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
61 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
62 |
+
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
63 |
+
|
64 |
+
if fps <= 0 or width <= 0 or height <= 0:
|
65 |
+
return None, None, None, "Invalid video properties detected."
|
66 |
|
|
|
67 |
output_path = os.path.join(tempfile.gettempdir(), f"out_{os.urandom(8).hex()}.mp4")
|
68 |
+
# Use H.264 codec which is more widely supported
|
69 |
+
fourcc = cv2.VideoWriter_fourcc(*'avc1') # Changed from 'mp4v' to 'avc1'
|
70 |
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
71 |
|
72 |
+
if not out.isOpened():
|
73 |
+
return None, None, None, "Failed to initialize video writer."
|
74 |
+
|
75 |
+
processed_frames = 0
|
76 |
while True:
|
77 |
ret, frame = cap.read()
|
78 |
if not ret:
|
79 |
break
|
80 |
|
81 |
+
# Process frame
|
82 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
83 |
results = model.predict(source=frame_rgb, conf=sensitivity)[0]
|
84 |
annotated_frame = results.plot()
|
|
|
85 |
annotated_frame_bgr = cv2.cvtColor(annotated_frame, cv2.COLOR_RGB2BGR)
|
86 |
+
|
87 |
out.write(annotated_frame_bgr)
|
88 |
+
processed_frames += 1
|
89 |
|
90 |
cap.release()
|
91 |
out.release()
|
92 |
temp_files.append(output_path)
|
93 |
|
94 |
+
if processed_frames == 0:
|
95 |
+
return None, None, None, "No frames processed from video."
|
96 |
+
|
97 |
+
if not os.path.exists(output_path) or os.path.getsize(output_path) < 1024: # Check if file is too small
|
98 |
+
return None, None, None, f"Output video created but too small ({os.path.getsize(output_path)} bytes) - processing failed."
|
99 |
|
100 |
+
return output_path, None, output_path, f"Video processed successfully! ({processed_frames}/{frame_count} frames)"
|
101 |
|
102 |
else:
|
103 |
# Image processing
|
|
|
110 |
|
111 |
except Exception as e:
|
112 |
return None, None, None, f"Processing error: {str(e)}"
|
113 |
+
|
114 |
finally:
|
115 |
+
for f in temp_files[:-1]:
|
|
|
116 |
if f and os.path.exists(f):
|
117 |
try:
|
118 |
os.remove(f)
|
119 |
except:
|
120 |
pass
|
121 |
|
122 |
+
# Gradio interface (unchanged)
|
123 |
with gr.Blocks(css="""
|
124 |
.result_img > img {
|
125 |
width: 100%;
|