Spaces:
Build error
Build error
Commit ·
d99d98f
1
Parent(s): cc602cf
Add application file
Browse files- objectDetectionUI.py +56 -0
- requirements.txt +5 -0
objectDetectionUI.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Load the YOLO model
|
| 8 |
+
model = YOLO("../yolov9e.pt")
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def process_image(image_path):
|
| 12 |
+
# Run inference
|
| 13 |
+
results = model(image_path)
|
| 14 |
+
|
| 15 |
+
# Save the result image
|
| 16 |
+
output_path = os.path.join(tempfile.gettempdir(), "result.jpg")
|
| 17 |
+
results[0].save(filename=output_path)
|
| 18 |
+
|
| 19 |
+
return output_path, results[0]
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def main():
|
| 23 |
+
st.title("Object Detection App")
|
| 24 |
+
|
| 25 |
+
uploaded_file = st.file_uploader("Choose an image", type=["jpg", "png", "jpeg"])
|
| 26 |
+
|
| 27 |
+
if uploaded_file is not None:
|
| 28 |
+
# Display uploaded image
|
| 29 |
+
image = Image.open(uploaded_file)
|
| 30 |
+
|
| 31 |
+
# Save the uploaded image temporarily
|
| 32 |
+
temp_image_path = os.path.join(tempfile.gettempdir(), uploaded_file.name)
|
| 33 |
+
image.save(temp_image_path)
|
| 34 |
+
|
| 35 |
+
if st.button("Process"):
|
| 36 |
+
result_path, result = process_image(temp_image_path)
|
| 37 |
+
|
| 38 |
+
# Display results
|
| 39 |
+
st.image(result_path, caption="Detected Objects", use_container_width=True)
|
| 40 |
+
|
| 41 |
+
# Display detected objects details
|
| 42 |
+
st.write("### Detected Objects:")
|
| 43 |
+
|
| 44 |
+
i = 1
|
| 45 |
+
for box in result.boxes:
|
| 46 |
+
x1, y1, x2, y2 = box.xyxy[0]
|
| 47 |
+
class_id = int(box.cls[0])
|
| 48 |
+
label = model.names[class_id]
|
| 49 |
+
st.write(
|
| 50 |
+
f"{i}: {label.capitalize()}, **Location:** ({x1:.2f}, {y1:.2f}, {x2:.2f}, {y2:.2f})"
|
| 51 |
+
)
|
| 52 |
+
i += 1
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics
|
| 2 |
+
opencv-python
|
| 3 |
+
torch
|
| 4 |
+
torchvision
|
| 5 |
+
ruff
|