File size: 2,189 Bytes
6f4b5a8
 
 
 
 
 
 
 
 
 
 
 
 
 
3079da9
 
6f4b5a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import streamlit as st
import numpy as np
import cv2
from ultralytics import YOLO
import supervision as sv
import time

model = YOLO("yolov8x.pt")

def callback(x: np.ndarray) -> sv.Detections:
    result = model(x, verbose=False, conf=0.25)[0]
    return sv.Detections.from_ultralytics(result)

def main():
    st.title("Small Object Detection with SAHI")
    st.write("Slicing Aided Hyper Inference (SAHI) implementaion with Supervsion for small object detection")

    example_image_loaded = st.checkbox("Load example image")
    uploaded_image = None

    if example_image_loaded:
        image = cv2.imread("example-image.jpg")
    else:
        uploaded_image = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
        if uploaded_image is not None:
            image = cv2.imdecode(np.fromstring(uploaded_image.read(), np.uint8), 1)

    if uploaded_image is not None or example_image_loaded:
        with st.spinner("Loading..."):

            start_time_sahi = time.time()
            slicer = sv.InferenceSlicer(callback=callback)
            sliced_detections = slicer(image=image)
            end_time_sahi = time.time()

            start_time_yolo = time.time()
            yolo_results = model(image, verbose=False, conf=0.25)
            end_time_yolo = time.time()

        st.header("Original Image")
        st.image(image, channels="BGR")

        st.header("SAHI-Processed Image")
        sliced_image = sv.BoxAnnotator().annotate(image.copy(), detections=sliced_detections)
        st.image(sliced_image, channels="BGR")

        st.header("YOLO-Detected Image (Without SAHI)")
        yolo_image = sv.BoxAnnotator().annotate(image.copy(), detections=sv.Detections.from_ultralytics(yolo_results[0]))
        st.image(yolo_image, channels="BGR")

        st.subheader("Method Comparison")
        st.write("SAHI Inference Time:", round(end_time_sahi - start_time_sahi, 2), "seconds")
        st.write("YOLOv8 Inference Time:", round(end_time_yolo - start_time_yolo, 2), "seconds")

        st.write("SAHI Detection Count:", len(sliced_detections))
        st.write("YOLOv8 Detection Count:", len(yolo_results[0]))

if __name__ == "__main__":
    main()