Spaces:
Paused
Paused
luminoussg
commited on
Commit
•
6572ab5
1
Parent(s):
7cd0827
Update app.py
Browse files
app.py
CHANGED
@@ -29,8 +29,15 @@ def count_frames(video_file):
|
|
29 |
cap.release()
|
30 |
return total_frames
|
31 |
|
|
|
|
|
|
|
|
|
32 |
@spaces.GPU
|
33 |
def process_video(video_file, scale_percent, line_start_x, line_start_y, line_end_x, line_end_y, line_thickness, draw_tracks, view_img, view_in_counts, view_out_counts, track_thickness, region_thickness, line_dist_thresh, persist, conf, iou, classes, verbose, progress):
|
|
|
|
|
|
|
34 |
# Ensure classes is a list of integers
|
35 |
classes = [int(x) for x in classes.split(',') if x.strip().isdigit()] if classes else None
|
36 |
|
@@ -68,42 +75,43 @@ def process_video(video_file, scale_percent, line_start_x, line_start_y, line_en
|
|
68 |
prev_keypoints = None
|
69 |
processed_frames = 0
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
|
|
107 |
|
108 |
# Reduce the resolution of the video for download
|
109 |
output_path = "processed_output.mp4"
|
@@ -112,15 +120,24 @@ def process_video(video_file, scale_percent, line_start_x, line_start_y, line_en
|
|
112 |
else:
|
113 |
resolution = "1280x720"
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
return output_path
|
121 |
|
122 |
def preview_line(video_file, scale_percent, line_start_x, line_start_y, line_end_x, line_end_y, line_thickness):
|
123 |
cap = cv2.VideoCapture(video_file)
|
|
|
|
|
|
|
124 |
ret, frame = cap.read()
|
125 |
if not ret:
|
126 |
raise ValueError("Failed to read video frame")
|
@@ -150,7 +167,7 @@ def set_4k_coordinates():
|
|
150 |
def set_1080p_coordinates():
|
151 |
return 0, 700, 1920, 700
|
152 |
|
153 |
-
with gr.Blocks(
|
154 |
with gr.Row():
|
155 |
with gr.Column(scale=1):
|
156 |
video_input = gr.File(label="Upload your video") # Removed max_size parameter
|
|
|
29 |
cap.release()
|
30 |
return total_frames
|
31 |
|
32 |
+
def file_size_check(file_path, max_size_mb=500):
|
33 |
+
if os.path.getsize(file_path) > max_size_mb * 1024 * 1024:
|
34 |
+
raise ValueError("File size exceeds the maximum limit of {} MB".format(max_size_mb))
|
35 |
+
|
36 |
@spaces.GPU
|
37 |
def process_video(video_file, scale_percent, line_start_x, line_start_y, line_end_x, line_end_y, line_thickness, draw_tracks, view_img, view_in_counts, view_out_counts, track_thickness, region_thickness, line_dist_thresh, persist, conf, iou, classes, verbose, progress):
|
38 |
+
# Check file size
|
39 |
+
file_size_check(video_file)
|
40 |
+
|
41 |
# Ensure classes is a list of integers
|
42 |
classes = [int(x) for x in classes.split(',') if x.strip().isdigit()] if classes else None
|
43 |
|
|
|
75 |
prev_keypoints = None
|
76 |
processed_frames = 0
|
77 |
|
78 |
+
try:
|
79 |
+
while cap.isOpened():
|
80 |
+
ret, frame = cap.read()
|
81 |
+
if not ret:
|
82 |
+
break
|
83 |
+
frame = resize_frame(frame, scale_percent)
|
84 |
+
|
85 |
+
# Adjust line points based on scaling
|
86 |
+
scaled_line_points = [(int(x * scale_percent / 100), int(y * scale_percent / 100)) for x, y in line_points]
|
87 |
+
for point1, point2 in zip(scaled_line_points[:-1], scaled_line_points[1:]):
|
88 |
+
cv2.line(frame, tuple(map(int, point1)), tuple(map(int, point2)), (255, 255, 0), int(line_thickness))
|
89 |
+
|
90 |
+
tracks = model.track(frame, persist=persist, conf=conf, iou=iou, classes=classes, verbose=verbose)
|
91 |
+
|
92 |
+
# Update the counter with the current frame and tracks
|
93 |
+
frame = counter.start_counting(frame, tracks)
|
94 |
+
|
95 |
+
# Check if the previous frame is initialized for optical flow calculation
|
96 |
+
if prev_frame is not None:
|
97 |
+
try:
|
98 |
+
prev_frame_resized = resize_frame(prev_frame, scale_percent)
|
99 |
+
matched_keypoints, status, _ = cv2.calcOpticalFlowPyrLK(prev_frame_resized, frame, prev_keypoints, None)
|
100 |
+
prev_keypoints = matched_keypoints
|
101 |
+
except cv2.error as e:
|
102 |
+
print(f"Error in optical flow calculation: {e}")
|
103 |
+
|
104 |
+
prev_frame = frame.copy()
|
105 |
+
prev_keypoints = cv2.goodFeaturesToTrack(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), maxCorners=100, qualityLevel=0.3, minDistance=7, blockSize=7)
|
106 |
+
|
107 |
+
video_writer.write(frame)
|
108 |
+
|
109 |
+
# Update progress
|
110 |
+
processed_frames += 1
|
111 |
+
progress(processed_frames / total_frames)
|
112 |
+
finally:
|
113 |
+
cap.release()
|
114 |
+
video_writer.release()
|
115 |
|
116 |
# Reduce the resolution of the video for download
|
117 |
output_path = "processed_output.mp4"
|
|
|
120 |
else:
|
121 |
resolution = "1280x720"
|
122 |
|
123 |
+
try:
|
124 |
+
subprocess.run(
|
125 |
+
["ffmpeg", "-y", "-i", tmp_output_path, "-vf", f"scale={resolution}", "-crf", "18", "-preset", "veryfast", "-hide_banner", "-loglevel", "error", output_path],
|
126 |
+
check=True
|
127 |
+
)
|
128 |
+
except subprocess.CalledProcessError as e:
|
129 |
+
raise RuntimeError(f"Error during video processing: {e}")
|
130 |
+
|
131 |
+
if os.path.exists(tmp_output_path):
|
132 |
+
os.remove(tmp_output_path)
|
133 |
|
134 |
return output_path
|
135 |
|
136 |
def preview_line(video_file, scale_percent, line_start_x, line_start_y, line_end_x, line_end_y, line_thickness):
|
137 |
cap = cv2.VideoCapture(video_file)
|
138 |
+
if not cap.isOpened():
|
139 |
+
raise ValueError("Failed to read video frame")
|
140 |
+
|
141 |
ret, frame = cap.read()
|
142 |
if not ret:
|
143 |
raise ValueError("Failed to read video frame")
|
|
|
167 |
def set_1080p_coordinates():
|
168 |
return 0, 700, 1920, 700
|
169 |
|
170 |
+
with gr.Blocks() as demo:
|
171 |
with gr.Row():
|
172 |
with gr.Column(scale=1):
|
173 |
video_input = gr.File(label="Upload your video") # Removed max_size parameter
|