Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +58 -0
- example-image.jpg +0 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
+
import supervision as sv
|
| 6 |
+
import time
|
| 7 |
+
|
| 8 |
+
model = YOLO("yolov8x.pt")
|
| 9 |
+
|
| 10 |
+
def callback(x: np.ndarray) -> sv.Detections:
|
| 11 |
+
result = model(x, verbose=False, conf=0.25)[0]
|
| 12 |
+
return sv.Detections.from_ultralytics(result)
|
| 13 |
+
|
| 14 |
+
def main():
|
| 15 |
+
st.title("Small Object Detection with SAHI and YOLOv8")
|
| 16 |
+
|
| 17 |
+
example_image_loaded = st.checkbox("Load example image")
|
| 18 |
+
uploaded_image = None
|
| 19 |
+
|
| 20 |
+
if example_image_loaded:
|
| 21 |
+
image = cv2.imread("example-image.jpg")
|
| 22 |
+
else:
|
| 23 |
+
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
| 24 |
+
if uploaded_image is not None:
|
| 25 |
+
image = cv2.imdecode(np.fromstring(uploaded_image.read(), np.uint8), 1)
|
| 26 |
+
|
| 27 |
+
if uploaded_image is not None or example_image_loaded:
|
| 28 |
+
with st.spinner("Loading..."):
|
| 29 |
+
|
| 30 |
+
start_time_sahi = time.time()
|
| 31 |
+
slicer = sv.InferenceSlicer(callback=callback)
|
| 32 |
+
sliced_detections = slicer(image=image)
|
| 33 |
+
end_time_sahi = time.time()
|
| 34 |
+
|
| 35 |
+
start_time_yolo = time.time()
|
| 36 |
+
yolo_results = model(image, verbose=False, conf=0.25)
|
| 37 |
+
end_time_yolo = time.time()
|
| 38 |
+
|
| 39 |
+
st.header("Original Image")
|
| 40 |
+
st.image(image, channels="BGR")
|
| 41 |
+
|
| 42 |
+
st.header("SAHI-Processed Image")
|
| 43 |
+
sliced_image = sv.BoxAnnotator().annotate(image.copy(), detections=sliced_detections)
|
| 44 |
+
st.image(sliced_image, channels="BGR")
|
| 45 |
+
|
| 46 |
+
st.header("YOLO-Detected Image (Without SAHI)")
|
| 47 |
+
yolo_image = sv.BoxAnnotator().annotate(image.copy(), detections=sv.Detections.from_ultralytics(yolo_results[0]))
|
| 48 |
+
st.image(yolo_image, channels="BGR")
|
| 49 |
+
|
| 50 |
+
st.subheader("Method Comparison")
|
| 51 |
+
st.write("SAHI Inference Time:", round(end_time_sahi - start_time_sahi, 2), "seconds")
|
| 52 |
+
st.write("YOLOv8 Inference Time:", round(end_time_yolo - start_time_yolo, 2), "seconds")
|
| 53 |
+
|
| 54 |
+
st.write("SAHI Detection Count:", len(sliced_detections))
|
| 55 |
+
st.write("YOLOv8 Detection Count:", len(yolo_results[0]))
|
| 56 |
+
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
main()
|
example-image.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics
|
| 2 |
+
supervision
|
| 3 |
+
numpy
|