Srikanthgoud7 commited on
Commit
1965b44
·
verified ·
1 Parent(s): 678c6be

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # streamlit_yolo_app.py
2
+
3
+ import streamlit as st
4
+ import cv2
5
+ import numpy as np
6
+ from ultralytics import YOLO
7
+ from PIL import Image
8
+
9
+ # -------------------------------
10
+ # Streamlit App Title
11
+ # -------------------------------
12
+ st.title("YOLOv8 Object Detection in Images")
13
+ model = YOLO("yolov8n.pt")
14
+
15
+ # -------------------------------
16
+ # Load YOLO Model
17
+ @st.cache_resource
18
+ def load_model(model_name):
19
+ return YOLO(model_name)
20
+
21
+ # -------------------------------
22
+ # Upload Image
23
+ # -------------------------------
24
+ uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
25
+
26
+ if uploaded_file is not None:
27
+ # Load uploaded image
28
+ image = Image.open(uploaded_file).convert("RGB")
29
+ img_array = np.array(image)
30
+
31
+ # Perform Inference
32
+ results = model(img_array)
33
+
34
+ # Annotated results
35
+ annotated_img = results[0].plot()
36
+
37
+ # Get shape of annotated image
38
+ h, w, c = annotated_img.shape
39
+ st.write(f"**Annotated Image Shape:** {h} x {w} x {c}")
40
+
41
+ # Display Input and Output
42
+ col1, col2 = st.columns(2)
43
+ with col1:
44
+ st.image(image, caption="Original Image", use_container_width=True)
45
+ with col2:
46
+ st.image(annotated_img, caption="Detected Objects", use_container_width=True)
47
+
48
+ # Option to download the annotated image
49
+ result_bgr = cv2.cvtColor(annotated_img, cv2.COLOR_RGB2BGR)
50
+ cv2.imwrite("annotated_output.jpg", result_bgr)
51
+ with open("annotated_output.jpg", "rb") as f:
52
+ st.download_button("Download Annotated Image", f, "annotated_output.jpg")