Spaces:
Sleeping
Sleeping
Kesheratmex
commited on
Commit
·
f939cfe
1
Parent(s):
18d3833
Add MAX_FRAMES env var to limit video frame processing
Browse filesAllow users to set the `MAX_FRAMES` environment variable to cap the number of frames processed during strong analysis.
Adds debug output, removes the hard‑coded frame counter, and updates the loop to process all frames or stop at the specified limit.
app.py
CHANGED
|
@@ -389,13 +389,24 @@ def generar_analisis_fuerte(media_path):
|
|
| 389 |
if ext in [".mp4", ".mov", ".avi", ".mkv"]:
|
| 390 |
cap = cv2.VideoCapture(media_path)
|
| 391 |
idx = 0
|
| 392 |
-
|
| 393 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
ret, frame = cap.read()
|
| 395 |
if not ret:
|
| 396 |
break
|
| 397 |
tmpf = os.path.join(tmpdir, f"frame_{idx}.jpg")
|
| 398 |
cv2.imwrite(tmpf, frame)
|
|
|
|
| 399 |
results = model.predict(source=tmpf, conf=0.25, iou=0.45)
|
| 400 |
dets = []
|
| 401 |
if results and len(results) > 0:
|
|
@@ -418,7 +429,8 @@ def generar_analisis_fuerte(media_path):
|
|
| 418 |
visual = compute_visual_features(tmpf, dets)
|
| 419 |
frames.append({"frame_index": idx, "detections": dets, "visual": visual, "image_path": tmpf})
|
| 420 |
idx += 1
|
| 421 |
-
|
|
|
|
| 422 |
cap.release()
|
| 423 |
else:
|
| 424 |
# single image
|
|
|
|
| 389 |
if ext in [".mp4", ".mov", ".avi", ".mkv"]:
|
| 390 |
cap = cv2.VideoCapture(media_path)
|
| 391 |
idx = 0
|
| 392 |
+
# Process all frames in the video. This may be expensive for long videos.
|
| 393 |
+
# To limit processing, set the environment variable MAX_FRAMES to a positive integer.
|
| 394 |
+
max_frames_env = os.getenv("MAX_FRAMES", "0")
|
| 395 |
+
try:
|
| 396 |
+
max_frames = int(max_frames_env)
|
| 397 |
+
except Exception:
|
| 398 |
+
max_frames = 0
|
| 399 |
+
if max_frames > 0:
|
| 400 |
+
print(f"DEBUG: processing up to {max_frames} frames (MAX_FRAMES set)")
|
| 401 |
+
else:
|
| 402 |
+
print("DEBUG: processing all video frames for strong analysis (may be slow)...")
|
| 403 |
+
while True:
|
| 404 |
ret, frame = cap.read()
|
| 405 |
if not ret:
|
| 406 |
break
|
| 407 |
tmpf = os.path.join(tmpdir, f"frame_{idx}.jpg")
|
| 408 |
cv2.imwrite(tmpf, frame)
|
| 409 |
+
# Run detection per frame
|
| 410 |
results = model.predict(source=tmpf, conf=0.25, iou=0.45)
|
| 411 |
dets = []
|
| 412 |
if results and len(results) > 0:
|
|
|
|
| 429 |
visual = compute_visual_features(tmpf, dets)
|
| 430 |
frames.append({"frame_index": idx, "detections": dets, "visual": visual, "image_path": tmpf})
|
| 431 |
idx += 1
|
| 432 |
+
if max_frames > 0 and idx >= max_frames:
|
| 433 |
+
break
|
| 434 |
cap.release()
|
| 435 |
else:
|
| 436 |
# single image
|