Spaces:
Sleeping
Sleeping
File size: 1,746 Bytes
b66f07e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
```python
import os
import time
import logging
from config import CONFIG
logger = logging.getLogger(__name__)
def clean_output_directory(max_age_seconds=86400): # 24 hours
"""Remove output files older than max_age_seconds."""
output_dir = os.path.join("static", "output")
if not os.path.exists(output_dir):
return
current_time = time.time()
for filename in os.listdir(output_dir):
file_path = os.path.join(output_dir, filename)
if os.path.isfile(file_path):
file_age = current_time - os.path.getmtime(file_path)
if file_age > max_age_seconds:
try:
os.remove(file_path)
logger.info(f"Removed old output file: {file_path}")
except Exception as e:
logger.error(f"Failed to remove {file_path}: {e}")
async def save_snapshot(frame, violation, output_dir):
"""Save a snapshot with metadata."""
snapshot_filename = f"violation_{violation['violation']}_worker{violation['worker_id']}_{int(violation['timestamp']*100)}.jpg"
snapshot_path = os.path.join(output_dir, snapshot_filename)
cv2.imwrite(snapshot_path, frame, [cv2.IMWRITE_JPEG_QUALITY, CONFIG["SNAPSHOT_QUALITY"]])
logger.info(f"Saved snapshot: {snapshot_path}")
return snapshot_path, f"{CONFIG['PUBLIC_URL_BASE']}{snapshot_filename}"
```
Add a call to `clean_output_directory()` at the start of `process_video()` in `app.py`:
```python
async def process_video(video_data, temp_dir, progress=gr.Progress()):
clean_output_directory() # Clean old files before processing
output_dir = os.path.join(temp_dir, "output")
os.makedirs(output_dir, exist_ok=True)
# ... rest of the function
``` |