AmirKaseb commited on
Commit
991cd27
·
verified ·
1 Parent(s): 3f4fa8b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ from ultralyticsplus import YOLO, render_result
4
+ from PIL import Image
5
+
6
+ # Function to perform YOLOv8 object detection
7
+ def yoloV8_func(image, image_size=640, conf_threshold=0.4, iou_threshold=0.50):
8
+ # Load the YOLOv8 model from the 'best.pt' checkpoint
9
+ model_path = "best.pt"
10
+ model = YOLO(model_path)
11
+
12
+ # Perform object detection on the input image using the YOLOv8 model
13
+ results = model.predict(image,
14
+ conf=conf_threshold,
15
+ iou=iou_threshold,
16
+ imgsz=image_size)
17
+
18
+ # Render the output image with bounding boxes around detected objects
19
+ render = render_result(model=model, image=image, result=results[0])
20
+ return render
21
+
22
+ # Streamlit app
23
+ def main():
24
+ st.title("YOLOv8 Object Detection")
25
+ st.write("Upload an image and set the parameters to detect objects.")
26
+
27
+ # Upload image
28
+ uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
29
+
30
+ # Parameters
31
+ image_size = st.slider("Image Size", min_value=320, max_value=1280, value=640, step=32)
32
+ conf_threshold = st.slider("Confidence Threshold", min_value=0.0, max_value=1.0, value=0.4, step=0.05)
33
+ iou_threshold = st.slider("IOU Threshold", min_value=0.0, max_value=1.0, value=0.50, step=0.05)
34
+
35
+ # Perform object detection when an image is uploaded
36
+ if uploaded_image is not None:
37
+ image = Image.open(uploaded_image)
38
+ st.image(image, caption="Uploaded Image", use_column_width=True)
39
+ st.write("")
40
+ st.write("Detecting objects...")
41
+
42
+ # Perform object detection
43
+ output_image = yoloV8_func(image, image_size, conf_threshold, iou_threshold)
44
+ st.image(output_image, caption="Output Image", use_column_width=True)
45
+
46
+ if __name__ == "__main__":
47
+ main()