|
|
|
|
| import streamlit as st
|
| import cv2
|
| import numpy as np
|
| from ultralytics import YOLO
|
| from PIL import Image
|
|
|
|
|
| model = YOLO("yolov8n-seg.pt")
|
|
|
| st.title("🖼️ Image Segmentation using YOLOv8")
|
| st.write("Upload an image and see segmentation results using YOLOv8!")
|
|
|
|
|
| uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
|
|
| if uploaded_file is not None:
|
|
|
| image = Image.open(uploaded_file).convert("RGB")
|
| img_np = np.array(image)
|
| img_cv = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
|
|
|
| st.subheader("Original Image")
|
| st.image(image, caption="Uploaded Image", use_container_width=True)
|
|
|
|
|
| results = model(img_cv)
|
|
|
|
|
| for r in results:
|
| annotated_img = r.plot()
|
|
|
|
|
| annotated_img = cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB)
|
|
|
| st.subheader("Segmented Image")
|
| st.image(annotated_img, caption="YOLOv8 Segmentation", use_container_width=True)
|
|
|
|
|
| result_pil = Image.fromarray(annotated_img)
|
| st.download_button(
|
| label="Download Segmented Image",
|
| data=cv2.imencode('.jpg', cv2.cvtColor(annotated_img, cv2.COLOR_RGB2BGR))[1].tobytes(),
|
| file_name="segmented_output.jpg",
|
| mime="image/jpeg"
|
| )
|
|
|