Update app.py
Browse files
app.py
CHANGED
@@ -375,26 +375,31 @@ def analyze_video_activities(video_path):
|
|
375 |
@spaces.GPU
|
376 |
def process_diary(day, date, total_people, total_machinery, machinery_types, activities, media_source, local_file, azure_blob):
|
377 |
"""Process the site diary entry with media from local file or Azure Blob Storage."""
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
if not azure_blob:
|
384 |
-
return [day, date, "No blob selected", "No blob selected", "No blob selected", "No blob selected", None]
|
385 |
-
try:
|
386 |
-
blob_client = blob_service_client.get_blob_client(container=CONTAINER_NAME, blob=azure_blob)
|
387 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(azure_blob)[1]) as temp_file:
|
388 |
-
temp_path = temp_file.name
|
389 |
-
blob_data = blob_client.download_blob()
|
390 |
-
blob_data.readinto(temp_file)
|
391 |
-
media_path = temp_path
|
392 |
-
except Exception as e:
|
393 |
-
print(f"Error downloading blob: {str(e)}")
|
394 |
-
return [day, date, "Error downloading blob", "Error downloading blob", "Error downloading blob", "Error downloading blob", None]
|
395 |
-
|
396 |
-
# Process the media file
|
397 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
398 |
file_ext = get_file_extension(media_path)
|
399 |
if not (is_image(media_path) or is_video(media_path)):
|
400 |
raise ValueError(f"Unsupported file type: {file_ext}")
|
@@ -412,18 +417,26 @@ def process_diary(day, date, total_people, total_machinery, machinery_types, act
|
|
412 |
detected_activities = analyze_video_activities(media_path)
|
413 |
annotated_video_path = annotate_video_with_bboxes(media_path)
|
414 |
|
415 |
-
# Clean up temporary file if downloaded from Azure
|
416 |
-
if media_source == "Azure Blob" and os.path.exists(media_path):
|
417 |
-
os.remove(media_path)
|
418 |
-
|
419 |
detected_types_str = ", ".join([f"{k}: {v}" for k, v in detected_machinery_types.items()])
|
420 |
return [day, date, str(detected_people), str(detected_machinery), detected_types_str, detected_activities, annotated_video_path]
|
|
|
421 |
except Exception as e:
|
422 |
print(f"Error processing media: {str(e)}")
|
423 |
-
if media_source == "Azure Blob" and os.path.exists(media_path):
|
424 |
-
os.remove(media_path)
|
425 |
return [day, date, "Error processing media", "Error processing media", "Error processing media", "Error processing media", None]
|
426 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
427 |
with gr.Blocks(title="Digital Site Diary") as demo:
|
428 |
gr.Markdown("# 📝 Digital Site Diary")
|
429 |
with gr.Row():
|
|
|
375 |
@spaces.GPU
|
376 |
def process_diary(day, date, total_people, total_machinery, machinery_types, activities, media_source, local_file, azure_blob):
|
377 |
"""Process the site diary entry with media from local file or Azure Blob Storage."""
|
378 |
+
import torch
|
379 |
+
import gc
|
380 |
+
|
381 |
+
# Initialize media_path
|
382 |
+
media_path = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
383 |
try:
|
384 |
+
if media_source == "Local File":
|
385 |
+
if local_file is None:
|
386 |
+
return [day, date, "No media uploaded", "No media uploaded", "No media uploaded", "No media uploaded", None]
|
387 |
+
media_path = local_file # local_file is a string path in Gradio
|
388 |
+
else: # Azure Blob
|
389 |
+
if not azure_blob:
|
390 |
+
return [day, date, "No blob selected", "No blob selected", "No blob selected", "No blob selected", None]
|
391 |
+
try:
|
392 |
+
blob_client = blob_service_client.get_blob_client(container=CONTAINER_NAME, blob=azure_blob)
|
393 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(azure_blob)[1]) as temp_file:
|
394 |
+
temp_path = temp_file.name
|
395 |
+
blob_data = blob_client.download_blob()
|
396 |
+
blob_data.readinto(temp_file)
|
397 |
+
media_path = temp_path
|
398 |
+
except Exception as e:
|
399 |
+
print(f"Error downloading blob: {str(e)}")
|
400 |
+
return [day, date, "Error downloading blob", "Error downloading blob", "Error downloading blob", "Error downloading blob", None]
|
401 |
+
|
402 |
+
# Process the media file
|
403 |
file_ext = get_file_extension(media_path)
|
404 |
if not (is_image(media_path) or is_video(media_path)):
|
405 |
raise ValueError(f"Unsupported file type: {file_ext}")
|
|
|
417 |
detected_activities = analyze_video_activities(media_path)
|
418 |
annotated_video_path = annotate_video_with_bboxes(media_path)
|
419 |
|
|
|
|
|
|
|
|
|
420 |
detected_types_str = ", ".join([f"{k}: {v}" for k, v in detected_machinery_types.items()])
|
421 |
return [day, date, str(detected_people), str(detected_machinery), detected_types_str, detected_activities, annotated_video_path]
|
422 |
+
|
423 |
except Exception as e:
|
424 |
print(f"Error processing media: {str(e)}")
|
|
|
|
|
425 |
return [day, date, "Error processing media", "Error processing media", "Error processing media", "Error processing media", None]
|
426 |
|
427 |
+
finally:
|
428 |
+
# Clean up GPU memory
|
429 |
+
if torch.cuda.is_available():
|
430 |
+
torch.cuda.empty_cache()
|
431 |
+
gc.collect()
|
432 |
+
# Remove temporary file if downloaded from Azure
|
433 |
+
if media_source == "Azure Blob" and media_path and os.path.exists(media_path):
|
434 |
+
try:
|
435 |
+
os.remove(media_path)
|
436 |
+
print(f"Removed temporary file: {media_path}")
|
437 |
+
except Exception as e:
|
438 |
+
print(f"Error removing temporary file: {str(e)}")
|
439 |
+
|
440 |
with gr.Blocks(title="Digital Site Diary") as demo:
|
441 |
gr.Markdown("# 📝 Digital Site Diary")
|
442 |
with gr.Row():
|