File size: 4,523 Bytes
49ae8dc
 
 
 
 
4574f39
49ae8dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4574f39
49ae8dc
 
 
 
 
 
 
 
 
 
7998469
4574f39
 
49ae8dc
7998469
49ae8dc
 
 
 
 
 
 
 
 
 
 
 
 
 
4574f39
 
49ae8dc
4574f39
49ae8dc
4574f39
49ae8dc
 
 
 
 
 
 
4574f39
49ae8dc
 
 
4574f39
49ae8dc
 
4574f39
49ae8dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import os
import cv2
import streamlit as st
from ultralytics import YOLO
from streamlit_image_comparison import image_comparison
from utils import save_uploadedfile, apply_masks

model = YOLO('models/last.pt')

st.title("Image Segmentation with YOLOv8: A Web Integration")
st.subheader("Implementing for Image Segmentation and Object Detection")
st.write("Image segmentation is a critical task in computer vision that involves dividing an image into multiple "
         "segments or regions. YOLOv8 is a state-of-the-art deep learning model that can be used for "
         "image segmentation and object detection. In this web app, we will implement an interesting example "
         "using YOLOv8 for image segmentation. We can simply drop an image, View the identified segment "
         "a piece, a whole and the distribution of the identified segments. The essence of this application "
         "is to build a practical understanding and implementation of the powerful and light YOLOv8 "
         "for image segmentation and object detection")

url = "https://github.com/AkanimohOD19A/img-segmentation"
link = f'<a href="{url}">This sample app was heavily based on code shared by Akan Daniel. Huge credits to him.</a>'
st.markdown(link, unsafe_allow_html=True)



st.divider()

st.markdown('')
st.markdown('##### Segmented Pieces')

## Placeholder Image
parent_media_path = "media-directory"
img_file = './media-directory/example_input.png'

## Application States
APPLICATION_MODE = st.sidebar.selectbox("Our Options",
                                        ["Take Picture", "Upload Picture"]
                                        )

## captured_picture Image
if APPLICATION_MODE == "Take Picture":
    st.sidebar.write(
        """
            A computer aided application that segments your input image, built on 
            the powerful YOLOv8 instance segmentation algorithm developed by *ultralytics*.

            Simply take a captured_picture and it gets segmentated in real time.
        """
    )
    picture = st.camera_input("Take a picture")
    st.markdown('')
    if picture:
        st.sidebar.divider()
        st.sidebar.image(picture, caption="captured_picture")
        if st.button("Segment!"):
            img_file = os.path.join(parent_media_path, "captured_picture.jpg")
            save_uploadedfile(picture, save_path=img_file)
            st.sidebar.success("Saved File")
        st.write("Click on **Clear photo** to retake picture")

    st.divider()

elif APPLICATION_MODE == "Upload Picture":
    st.sidebar.write(
        """
            A computer aided application that segments your input image, built on 
            the powerful YOLOv8 object detection algorithm developed by *ultralytics*.

            Simply drop your image and it gets segmentated in real time.
        """
    )
    st.sidebar.divider()
    uploaded_file = st.sidebar.file_uploader("Drop a JPG/PNG file", accept_multiple_files=False, type=['jpg', 'png'])
    if uploaded_file is not None:
        img_file = os.path.join(parent_media_path, "uploaded_image.jpg")
        save_uploadedfile(uploaded_file, save_path=os.path.join(parent_media_path, "uploaded_image.jpg"))
        file_details = {"FileName": uploaded_file.name, "FileType": uploaded_file.type}

        st.sidebar.success("File saved successfully")
        print(f"File saved successfully to {os.path.abspath(img_file)}")
    else:
        st.sidebar.write("You are using a placeholder image, Upload your Image (.jpg for now) to explore")

results = model(img_file)
img = cv2.imread(img_file)
for result in results:
    # segmentation
    masks = [] if not result.masks else result.masks.data.cpu().numpy()
    numCols = len(masks)
    if numCols > 0:
        cols = st.columns(numCols)
        print(f"Number of instances found: {numCols}")
    else:
        st.warning("Unable to id Distinct items - Please retry with a clearer Image")
    img = apply_masks(img, masks)

    st.markdown('')
    st.markdown('##### Slider of Uploaded Image and Segments')
    image_comparison(
        img1=img_file,
        img2=img,
        label1="Actual Image",
        label2="Segmented Image",
        width=700,
        starting_position=50,
        show_labels=True,
        make_responsive=True,
        in_memory=True
    )

st.sidebar.divider()
st.sidebar.markdown('')
st.sidebar.markdown('#### Distribution of identified items')


st.markdown('')
st.markdown('')
st.markdown('')
st.markdown('')
st.markdown('')
st.markdown('')
st.sidebar.divider()