File size: 2,199 Bytes
30fae3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st 
import pandas as pd 
import numpy as np 
import tensorflow as tf
import matplotlib.pyplot as plt 
from PIL import Image


def run():

    # membuat title
    st.title('Computer Vision Artificial Neural Network')

    # membuat subheader
    st.subheader('Prediction Between Daisy and Dandelion Flower')

    # menambahkan gambar 
    image = Image.open('header2.jpg')
    st.image(image)
        
    # inference
    model = tf.keras.models.load_model('model_cv.h5')    
    
    data_inf = st.file_uploader("Upload file image to predict", type=['jpg', 'png', 'jpeg'])

    # submit button
    submitted = st.button('Predict')

    # logic ketika predict button ditekan
    if submitted and data_inf:

        img = Image.open(data_inf)
        img = img.resize((150,150))


        # img = tf.keras.utils.load_img(data_inf, target_size=(150, 150))
        x = tf.keras.utils.img_to_array(img)/255

        x = np.expand_dims(x, axis=0)

        # menampilkan gambar upload
        left_co, cent_co,last_co = st.columns(3)
        with cent_co:
                st.image(img, caption='Uploaded Image')

        # prediksi
        pred_inf = model.predict(x)[0,0]
        threshold = 0.395

        # menentukan kelas
        if pred_inf >= threshold:
            predicted_class = 0
        else:
            predicted_class = 1

        clas = ['Daisy', 'Dandelion']
        st.write('### Prediction :', clas[predicted_class])
        st.write('#### Probability : {:.3f}'.format(pred_inf))
        

    #     images = np.vstack([x])
    #     output = model.predict(images, batch_size=32)
    #     probability = output[0, 0]
    #     threshold = 0.395  # threshold untuk klasifikasi biner

    #     if probability >= threshold:
    #         predicted_class = 0
    #     else:
    #         predicted_class = 1

    #     clas = ['daisy', 'dandelion']
    #     print('Prediction is a {} with probability {:.3f}'.format(clas[predicted_class], probability))

    # # predict
    #     pred_inf = model.predict(data_inf)
    #     st.write('##  Prediction :', str(int(pred_inf)))
    #     st.write('### Positive : 1, Negative : 2')

if __name__ == '__main__':
    run()