Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
| 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
|
| 43 |
|
| 44 |
-
# Output dirs
|
| 45 |
output_dir = "drone_outputs"
|
| 46 |
os.makedirs(output_dir, exist_ok=True)
|
| 47 |
|
| 48 |
# ---------------------------------
|
| 49 |
-
#
|
| 50 |
# ---------------------------------
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 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 |
-
#
|
| 113 |
# ---------------------------------
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.")
|