File size: 1,707 Bytes
33ba83f
 
 
 
 
 
 
 
 
 
 
 
 
 
5716cb7
33ba83f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 os
import cv2
import numpy as np
import streamlit as st
import tensorflow as tf


def process_image(uploaded_file):
    file_content = uploaded_file.read()
    nparr = np.frombuffer(file_content, np.uint8)
    image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    return image


model = tf.saved_model.load("model")


def perform_segmentation(image):
    resized_image = cv2.resize(image, (128, 128))
    resized_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2RGB)
    normalized_image = resized_image / 255.0

    input_array = np.expand_dims(normalized_image, axis=0).astype(np.float32)
    segmented_mask = model(tf.constant(input_array))[0]

    threshold = 0.5
    binary_mask = (segmented_mask > threshold).numpy().astype(np.uint8)

    # Resize the binary mask to match the size of the input image
    binary_mask_resized = cv2.resize(binary_mask, (image.shape[1], image.shape[0]))

    segmented_image = cv2.bitwise_and(image, image, mask=binary_mask_resized)

    return segmented_image


def main():
    st.title("Solution Challenge")

    uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
    if uploaded_file is not None:
        image = process_image(uploaded_file)
        segmented_image = perform_segmentation(image)

        # Create two columns to display images side by side
        col1, col2 = st.columns(2)

        # Display original image in the first column
        col1.image(image, caption="Original Image", use_column_width=True)

        # Display segmented image in the second column
        col2.image(
            segmented_image, caption="Segmentation Results", use_column_width=True
        )


if __name__ == "__main__":
    main()