pavankalyan123456 commited on
Commit
3bbb111
Β·
verified Β·
1 Parent(s): 6d16bfb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -87
app.py CHANGED
@@ -12,106 +12,114 @@ from datetime import datetime
12
  # ---------------------------------
13
  st.set_page_config(page_title="Drone Object Detection", layout="wide")
14
  st.title("🚁 Drone Object Detection (Deformable DETR)")
15
- st.write("πŸ›° Detect objects in drone footage using a Transformer-based DETR model.")
16
 
17
- # ---------------------------------
18
- # πŸ”„ Status Display
19
- # ---------------------------------
20
- st.info("βš™οΈ Initializing app... this may take 2–5 minutes the first time.")
21
-
22
- # ---------------------------------
23
- # πŸš€ Load Model with Caching
24
- # ---------------------------------
25
- @st.cache_resource(show_spinner="Loading Deformable DETR model (first time may take several minutes)...")
26
- def load_model():
27
- processor = AutoImageProcessor.from_pretrained("SenseTime/deformable-detr")
28
- model = DeformableDetrForObjectDetection.from_pretrained("SenseTime/deformable-detr")
29
- device = "cuda" if torch.cuda.is_available() else "cpu"
30
- model = model.to(device)
31
- return processor, model, device
32
-
33
- processor, model, device = load_model()
34
- st.success("βœ… Model loaded successfully!")
35
 
36
  # ---------------------------------
37
- # πŸŽ›οΈ Sidebar Options
38
  # ---------------------------------
39
  st.sidebar.header("βš™οΈ Options")
40
  input_type = st.sidebar.radio("Select Input Type", ["🎞️ Video Upload", "πŸ“· Live Camera"])
41
  save_output = st.sidebar.radio("πŸ’Ύ Save detected video?", ["Yes", "No"])
42
- capture_images = st.sidebar.checkbox("πŸ“Έ Capture Photos During Detection", value=True)
43
 
44
- # Output dirs
45
  output_dir = "drone_outputs"
46
  os.makedirs(output_dir, exist_ok=True)
47
 
48
  # ---------------------------------
49
- # πŸŽ₯ Video Upload Mode
50
  # ---------------------------------
51
- if input_type == "🎞️ Video Upload":
52
- video_file = st.file_uploader("Upload Drone Video", type=["mp4", "avi", "mov", "mkv"])
53
- if video_file:
54
- tfile = tempfile.NamedTemporaryFile(delete=False)
55
- tfile.write(video_file.read())
56
- cap = cv2.VideoCapture(tfile.name)
57
- stframe = st.empty()
58
-
59
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
60
- output_video_path = os.path.join(output_dir, f"detected_{timestamp}.avi")
61
- output_image_dir = os.path.join(output_dir, f"captures_{timestamp}")
62
- os.makedirs(output_image_dir, exist_ok=True)
63
-
64
- if save_output == "Yes":
65
- fourcc = cv2.VideoWriter_fourcc(*'XVID')
66
- out = cv2.VideoWriter(output_video_path, fourcc, 20.0,
67
- (int(cap.get(3)), int(cap.get(4))))
68
- st.info("πŸ’Ύ Saving detected video...")
69
-
70
- frame_count = 0
71
- st.info("πŸš€ Running object detection... Please wait...")
72
-
73
- while cap.isOpened():
74
- ret, frame = cap.read()
75
- if not ret:
76
- break
77
-
78
- frame_count += 1
79
- image_pil = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
80
- inputs = processor(images=image_pil, return_tensors="pt").to(device)
81
- outputs = model(**inputs)
82
- target_sizes = torch.tensor([image_pil.size[::-1]]).to(device)
83
- results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.6)[0]
84
-
85
- draw = ImageDraw.Draw(image_pil)
86
- for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
87
- box = [round(i, 2) for i in box.tolist()]
88
- draw.rectangle(box, outline="red", width=3)
89
- draw.text((box[0], box[1]), f"{model.config.id2label[label.item()]} {round(score.item(), 2)}", fill="white")
90
-
91
- annotated = cv2.cvtColor(np.array(image_pil), cv2.COLOR_RGB2BGR)
92
- stframe.image(annotated, channels="BGR", use_container_width=True)
93
-
94
- if save_output == "Yes":
95
- out.write(annotated)
96
-
97
- if capture_images and frame_count % 50 == 0:
98
- capture_path = os.path.join(output_image_dir, f"capture_{frame_count}.jpg")
99
- cv2.imwrite(capture_path, annotated)
100
-
101
- cap.release()
102
- if save_output == "Yes":
103
- out.release()
104
- st.success(f"βœ… Detected video saved at: `{output_video_path}`")
105
-
106
- if capture_images:
107
- st.info(f"πŸ“Έ Captured frames saved in: `{output_image_dir}`")
108
-
109
- st.success("πŸŽ‰ Detection complete!")
110
 
111
  # ---------------------------------
112
- # ⚠️ Live Camera Mode
113
  # ---------------------------------
114
- elif input_type == "πŸ“· Live Camera":
115
- st.warning("⚠️ Live camera is not supported on Hugging Face Spaces.")
116
- st.write("To use live webcam, run locally:")
117
- st.code("streamlit run src/streamlit_app.py", language="bash")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  # ---------------------------------
13
  st.set_page_config(page_title="Drone Object Detection", layout="wide")
14
  st.title("🚁 Drone Object Detection (Deformable DETR)")
15
+ st.write("πŸ›° Detect objects in drone footage using a Transformer-based Deformable DETR model.")
16
 
17
+ st.info("βš™οΈ App ready. Click **Load Model** to start β€” the first load may take 2–5 min on Hugging Face.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  # ---------------------------------
20
+ # πŸŽ›οΈ Sidebar
21
  # ---------------------------------
22
  st.sidebar.header("βš™οΈ Options")
23
  input_type = st.sidebar.radio("Select Input Type", ["🎞️ Video Upload", "πŸ“· Live Camera"])
24
  save_output = st.sidebar.radio("πŸ’Ύ Save detected video?", ["Yes", "No"])
25
+ capture_images = st.sidebar.checkbox("πŸ“Έ Capture photos during detection", value=True)
26
 
 
27
  output_dir = "drone_outputs"
28
  os.makedirs(output_dir, exist_ok=True)
29
 
30
  # ---------------------------------
31
+ # πŸš€ Model Loader (lazy, on click)
32
  # ---------------------------------
33
+ @st.cache_resource(show_spinner=False)
34
+ def load_model():
35
+ with st.spinner("⏳ Downloading Deformable DETR model… please wait"):
36
+ processor = AutoImageProcessor.from_pretrained("SenseTime/deformable-detr")
37
+ model = DeformableDetrForObjectDetection.from_pretrained("SenseTime/deformable-detr")
38
+ device = "cuda" if torch.cuda.is_available() else "cpu"
39
+ model = model.to(device)
40
+ return processor, model, device
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  # ---------------------------------
43
+ # 🧠 Load model only when user clicks
44
  # ---------------------------------
45
+ if st.button("πŸš€ Load Model and Start"):
46
+ try:
47
+ processor, model, device = load_model()
48
+ st.success("βœ… Model loaded successfully!")
49
+
50
+ # ----- VIDEO UPLOAD -----
51
+ if input_type == "🎞️ Video Upload":
52
+ video_file = st.file_uploader("Upload Drone Video", type=["mp4", "avi", "mov", "mkv"])
53
+ if video_file:
54
+ tfile = tempfile.NamedTemporaryFile(delete=False)
55
+ tfile.write(video_file.read())
56
+ cap = cv2.VideoCapture(tfile.name)
57
+ stframe = st.empty()
58
+
59
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
60
+ output_video_path = os.path.join(output_dir, f"detected_{timestamp}.avi")
61
+ output_image_dir = os.path.join(output_dir, f"captures_{timestamp}")
62
+ os.makedirs(output_image_dir, exist_ok=True)
63
+
64
+ if save_output == "Yes":
65
+ fourcc = cv2.VideoWriter_fourcc(*'XVID')
66
+ out = cv2.VideoWriter(output_video_path, fourcc, 20.0,
67
+ (int(cap.get(3)), int(cap.get(4))))
68
+ st.info("πŸ’Ύ Saving detected video...")
69
+
70
+ frame_count = 0
71
+ st.info("πŸš€ Running object detection...")
72
+
73
+ while cap.isOpened():
74
+ ret, frame = cap.read()
75
+ if not ret:
76
+ break
77
+
78
+ frame_count += 1
79
+ image_pil = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
80
+ inputs = processor(images=image_pil, return_tensors="pt").to(device)
81
+ outputs = model(**inputs)
82
+ target_sizes = torch.tensor([image_pil.size[::-1]]).to(device)
83
+ results = processor.post_process_object_detection(
84
+ outputs, target_sizes=target_sizes, threshold=0.6
85
+ )[0]
86
+
87
+ draw = ImageDraw.Draw(image_pil)
88
+ for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
89
+ box = [round(i, 2) for i in box.tolist()]
90
+ draw.rectangle(box, outline="red", width=3)
91
+ draw.text(
92
+ (box[0], box[1]),
93
+ f"{model.config.id2label[label.item()]} {round(score.item(), 2)}",
94
+ fill="white"
95
+ )
96
+
97
+ annotated = cv2.cvtColor(np.array(image_pil), cv2.COLOR_RGB2BGR)
98
+ stframe.image(annotated, channels="BGR", use_container_width=True)
99
+
100
+ if save_output == "Yes":
101
+ out.write(annotated)
102
+ if capture_images and frame_count % 50 == 0:
103
+ cv2.imwrite(
104
+ os.path.join(output_image_dir, f"capture_{frame_count}.jpg"),
105
+ annotated
106
+ )
107
+
108
+ cap.release()
109
+ if save_output == "Yes":
110
+ out.release()
111
+ st.success(f"βœ… Saved video: `{output_video_path}`")
112
+ if capture_images:
113
+ st.info(f"πŸ“Έ Captured frames in `{output_image_dir}`")
114
+
115
+ st.success("πŸŽ‰ Detection complete!")
116
+
117
+ # ----- LIVE CAMERA -----
118
+ elif input_type == "πŸ“· Live Camera":
119
+ st.warning("⚠️ Live camera not supported on Hugging Face Spaces.\nRun locally instead:")
120
+ st.code("streamlit run app.py", language="bash")
121
+
122
+ except Exception as e:
123
+ st.error(f"❌ Error loading model: {e}")
124
+ else:
125
+ st.warning("πŸ‘† Click **Load Model and Start** to begin detection.")