Update periodic_detection_function.py
Browse files
periodic_detection_function.py
CHANGED
|
@@ -189,8 +189,8 @@ def run_periodic_detection(video_path, trajectory_path, output_video_path=None,
|
|
| 189 |
|
| 190 |
### Task 1
|
| 191 |
period_num = len([x for x in seg_labels.values() if len(x)>0.5*win])
|
| 192 |
-
print("period_num: {}".format(period_num))
|
| 193 |
-
print("seg_labels_index: {}".format(seg_labels.keys()))
|
| 194 |
if period_num>0:
|
| 195 |
period_boundaries = {}
|
| 196 |
for p_id, (k,v) in enumerate(seg_labels.items()):
|
|
@@ -293,7 +293,7 @@ def run_periodic_detection(video_path, trajectory_path, output_video_path=None,
|
|
| 293 |
print("Error: Could not open video writer with any compatible codec.")
|
| 294 |
|
| 295 |
i, j = 0, 0
|
| 296 |
-
for k in tqdm(list(seg_labels.keys())):
|
| 297 |
if not seg_labels[k]: # Skip empty segments
|
| 298 |
continue
|
| 299 |
|
|
@@ -301,6 +301,21 @@ def run_periodic_detection(video_path, trajectory_path, output_video_path=None,
|
|
| 301 |
frame_ids = list(seg_labels[k].keys())
|
| 302 |
j += len(seg_labels[k])
|
| 303 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
cv2.putText(progress_bar, f'Period {k+1}', (5, height*k+30),
|
| 305 |
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2)
|
| 306 |
|
|
@@ -324,15 +339,27 @@ def run_periodic_detection(video_path, trajectory_path, output_video_path=None,
|
|
| 324 |
print(f"Error in video generation: {str(e)}")
|
| 325 |
continue
|
| 326 |
|
| 327 |
-
cv2.putText(progress_bar, f'Frame: {
|
| 328 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2)
|
| 329 |
i += len(seg_labels[k])
|
| 330 |
-
|
| 331 |
-
try:
|
| 332 |
-
out.write(frame)
|
| 333 |
-
except:
|
| 334 |
-
pass
|
| 335 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
# Release resources
|
| 337 |
cap.release()
|
| 338 |
out.release()
|
|
|
|
| 189 |
|
| 190 |
### Task 1
|
| 191 |
period_num = len([x for x in seg_labels.values() if len(x)>0.5*win])
|
| 192 |
+
#print("period_num: {}".format(period_num))
|
| 193 |
+
#print("seg_labels_index: {}".format(seg_labels.keys()))
|
| 194 |
if period_num>0:
|
| 195 |
period_boundaries = {}
|
| 196 |
for p_id, (k,v) in enumerate(seg_labels.items()):
|
|
|
|
| 293 |
print("Error: Could not open video writer with any compatible codec.")
|
| 294 |
|
| 295 |
i, j = 0, 0
|
| 296 |
+
for idx, k in enumerate(tqdm(list(seg_labels.keys()))):
|
| 297 |
if not seg_labels[k]: # Skip empty segments
|
| 298 |
continue
|
| 299 |
|
|
|
|
| 301 |
frame_ids = list(seg_labels[k].keys())
|
| 302 |
j += len(seg_labels[k])
|
| 303 |
|
| 304 |
+
# Use boundaries from JSON (period_boundaries) if available, otherwise fallback or match
|
| 305 |
+
start_frame_text = "????"
|
| 306 |
+
end_frame_text = "????"
|
| 307 |
+
# period_boundaries handles both dict (from detection) and list (fallback)
|
| 308 |
+
# Keys or indices matching the segment order
|
| 309 |
+
try:
|
| 310 |
+
# period_boundaries might be dict or list.
|
| 311 |
+
# If dict, keys usually match iteration order of seg_labels.items() if consistent
|
| 312 |
+
# If list, idx matches sequence
|
| 313 |
+
boundary = period_boundaries[idx]
|
| 314 |
+
start_frame_text = f"{boundary[0]:04d}"
|
| 315 |
+
end_frame_text = f"{boundary[1]:04d}"
|
| 316 |
+
except:
|
| 317 |
+
pass
|
| 318 |
+
|
| 319 |
cv2.putText(progress_bar, f'Period {k+1}', (5, height*k+30),
|
| 320 |
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2)
|
| 321 |
|
|
|
|
| 339 |
print(f"Error in video generation: {str(e)}")
|
| 340 |
continue
|
| 341 |
|
| 342 |
+
cv2.putText(progress_bar, f'Frame: {start_frame_text}-{end_frame_text}', (5, height*k+52),
|
| 343 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2)
|
| 344 |
i += len(seg_labels[k])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
|
| 346 |
+
# Add freeze frame at the end to show the final state
|
| 347 |
+
try:
|
| 348 |
+
# Reconstruct the final frame with the updated progress_bar (which has the last period's boundaries text)
|
| 349 |
+
# frame structure: Top part (Video + ProgressBar), Bottom part (Anchor)
|
| 350 |
+
# We assume 'frame' holds the last written frame. We extract the video component (Top-Left).
|
| 351 |
+
# Video part is [:H, :W]
|
| 352 |
+
if frame is not None:
|
| 353 |
+
last_video_part = frame[:H, :W, :]
|
| 354 |
+
top_part = np.concatenate([last_video_part, (progress_bar*255).astype(np.uint8)[:,:,::-1]], axis=1)
|
| 355 |
+
final_frame = np.concatenate([top_part, anchor], axis=0)
|
| 356 |
+
|
| 357 |
+
for _ in range(90): # 3 seconds pause
|
| 358 |
+
out.write(final_frame)
|
| 359 |
+
except Exception as e:
|
| 360 |
+
print(f"Error creating freeze frame: {e}")
|
| 361 |
+
pass
|
| 362 |
+
|
| 363 |
# Release resources
|
| 364 |
cap.release()
|
| 365 |
out.release()
|