sparsh007 commited on
Commit
71f299e
·
verified ·
1 Parent(s): f608c6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -3
app.py CHANGED
@@ -5,6 +5,7 @@ import cv2
5
  import tempfile
6
  from ultralytics import YOLO
7
  import numpy as np
 
8
 
9
  # Azure Storage config
10
  SAS_TOKEN = "sv=2024-11-04&ss=bfqt&srt=sco&sp=rwdlacupiytfx&se=2025-04-30T04:25:22Z&st=2025-04-16T20:25:22Z&spr=https&sig=HYrJBoOYc4PRe%2BoqBMl%2FmoL5Kz4ZYugbTLuEh63sbeo%3D"
@@ -26,8 +27,29 @@ def list_videos():
26
  except Exception as e:
27
  return [f"Error: {str(e)}"]
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  def annotate_video_with_bboxes(video_path):
30
  try:
 
 
 
31
  # Download video from Azure
32
  blob_service_client = BlobServiceClient(account_url=f"https://{ACCOUNT_NAME}.blob.core.windows.net", credential=SAS_TOKEN)
33
  blob_client = blob_service_client.get_blob_client(CONTAINER_NAME, video_path)
@@ -40,6 +62,10 @@ def annotate_video_with_bboxes(video_path):
40
 
41
  # Process video with YOLO
42
  cap = cv2.VideoCapture(temp_input.name)
 
 
 
 
43
  fps = cap.get(cv2.CAP_PROP_FPS)
44
  w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
45
  h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
@@ -86,10 +112,12 @@ def annotate_video_with_bboxes(video_path):
86
  # Gradio UI
87
  with gr.Blocks() as demo:
88
  gr.Markdown("## PRISM Site Diary V3 - Video Annotator")
89
- video_dropdown = gr.Dropdown(label="Select Video from Azure Blob", choices=list_videos(), interactive=True)
90
- annotate_btn = gr.Button("Annotate Video")
 
91
  output_video = gr.Video(label="Annotated Video")
92
- annotate_btn.click(fn=annotate_video_with_bboxes, inputs=video_dropdown, outputs=output_video)
 
93
  video_dropdown.change(fn=list_videos, outputs=video_dropdown)
94
 
95
  demo.launch()
 
5
  import tempfile
6
  from ultralytics import YOLO
7
  import numpy as np
8
+ from datetime import datetime
9
 
10
  # Azure Storage config
11
  SAS_TOKEN = "sv=2024-11-04&ss=bfqt&srt=sco&sp=rwdlacupiytfx&se=2025-04-30T04:25:22Z&st=2025-04-16T20:25:22Z&spr=https&sig=HYrJBoOYc4PRe%2BoqBMl%2FmoL5Kz4ZYugbTLuEh63sbeo%3D"
 
27
  except Exception as e:
28
  return [f"Error: {str(e)}"]
29
 
30
+ def get_latest_video():
31
+ try:
32
+ blob_service_client = BlobServiceClient(account_url=f"https://{ACCOUNT_NAME}.blob.core.windows.net", credential=SAS_TOKEN)
33
+ container_client = blob_service_client.get_container_client(CONTAINER_NAME)
34
+ blobs = container_client.list_blobs(name_starts_with=VIDEOS_FOLDER)
35
+ latest_blob = None
36
+ latest_time = None
37
+ for blob in blobs:
38
+ if blob.name.endswith(".mp4"):
39
+ blob_client = container_client.get_blob_client(blob.name)
40
+ props = blob_client.get_blob_properties()
41
+ if not latest_time or props.last_modified > latest_time:
42
+ latest_time = props.last_modified
43
+ latest_blob = blob.name
44
+ return latest_blob if latest_blob else "No videos found"
45
+ except Exception as e:
46
+ return f"Error: {str(e)}"
47
+
48
  def annotate_video_with_bboxes(video_path):
49
  try:
50
+ if not video_path or video_path == "No videos found":
51
+ return "Error: No video selected"
52
+
53
  # Download video from Azure
54
  blob_service_client = BlobServiceClient(account_url=f"https://{ACCOUNT_NAME}.blob.core.windows.net", credential=SAS_TOKEN)
55
  blob_client = blob_service_client.get_blob_client(CONTAINER_NAME, video_path)
 
62
 
63
  # Process video with YOLO
64
  cap = cv2.VideoCapture(temp_input.name)
65
+ if not cap.isOpened():
66
+ os.remove(temp_input.name)
67
+ return "Error: Could not open video"
68
+
69
  fps = cap.get(cv2.CAP_PROP_FPS)
70
  w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
71
  h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
 
112
  # Gradio UI
113
  with gr.Blocks() as demo:
114
  gr.Markdown("## PRISM Site Diary V3 - Video Annotator")
115
+ video_dropdown = gr.Dropdown(label="Select Video from Azure Blob", choices=list_videos(), interactive=True, allow_custom_value=True)
116
+ auto_btn = gr.Button("Annotate Latest Video")
117
+ manual_btn = gr.Button("Annotate Selected Video")
118
  output_video = gr.Video(label="Annotated Video")
119
+ auto_btn.click(fn=annotate_video_with_bboxes, inputs=gr.State(get_latest_video()), outputs=output_video)
120
+ manual_btn.click(fn=annotate_video_with_bboxes, inputs=video_dropdown, outputs=output_video)
121
  video_dropdown.change(fn=list_videos, outputs=video_dropdown)
122
 
123
  demo.launch()