danupurnomo commited on
Commit
3b99700
·
verified ·
1 Parent(s): ca946d1

Update src/prediction.py

Browse files
Files changed (1) hide show
  1. src/prediction.py +72 -75
src/prediction.py CHANGED
@@ -1,76 +1,73 @@
1
- import streamlit as st
2
- import pandas as pd
3
- import os
4
- import sys
5
- from io import BytesIO, StringIO
6
- import tensorflow as tf
7
- from PIL import Image
8
- import numpy as np
9
-
10
- # load files
11
-
12
- model=tf.keras.models.load_model('./src/best_model_sore.keras')
13
-
14
- klas = ['baseball_cap', 'beanie_hat', 'bucket_hat', 'fedora_hat', 'flat_cap']
15
-
16
- st.title('Jenis Topi')
17
-
18
- picup = st.file_uploader('Upload a picture', type=['jpg', 'jpeg', 'png'])
19
-
20
- if picup is not None:
21
- st.image(picup, caption='Uploaded Image', use_column_width=True)
22
-
23
- if st.button('Predict'):
24
-
25
- img = Image.open(picup).convert('RGB')
26
- img = img.resize((400,400))
27
- img_array = np.array(img)
28
- img_array = np.expand_dims(img_array, axis=0)
29
-
30
- prediction = model.predict(img_array)
31
- pred_index = np.argmax(prediction)
32
- pred_class = klas[pred_index]
33
- confidence = prediction[0][pred_index]*100
34
-
35
- st.success(f'Prediction: **{pred_class}** ({confidence:.2f}% confidence)')
36
-
37
- # class picupload(object):
38
-
39
- # def __init__(self):
40
- # self.fileTypes=['jpg']
41
-
42
- # def run(self):
43
- # topi = st.file_uploader('Upload picture', type=self.fileTypes)
44
- # show_file = st.empty()
45
- # if not topi:
46
- # show_file.info('Please upload a picture')
47
- # return
48
- # content = topi.getvalue()
49
- # if isinstance(topi, BytesIO):
50
- # show_file.image(topi)
51
- # topi.close()
52
-
53
- # if __name__ == '__main__':
54
- # helper = picupload()
55
- # helper.run()
56
-
57
- # def run():
58
-
59
- # with st.form(key='type-of-cap'):
60
-
61
- # st.write('##Topi')
62
-
63
- # topi = st.file_uploader('Upload file', type=['jpg'])
64
- # show_topi = st.empty()
65
-
66
- # if not topi:
67
- # show_topi.info('Please upload a picture : {}'.format(' '.join(['jpg'])))
68
- # return
69
- # content = topi.getvalue()
70
-
71
- # if isinstance(topi, BytesIO):
72
- # show_topi.image(topi)
73
- # topi.close()
74
-
75
- # run()
76
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import os
4
+ import sys
5
+ from io import BytesIO, StringIO
6
+ import tensorflow as tf
7
+ from PIL import Image
8
+ import numpy as np
9
+
10
+ # load files
11
+
12
+ model=tf.keras.models.load_model('./src/best_model_sore.keras')
13
+
14
+ klas = ['baseball_cap', 'beanie_hat', 'bucket_hat', 'fedora_hat', 'flat_cap']
15
+
16
+ st.title('Jenis Topi')
17
+
18
+ picup = st.file_uploader('Upload a picture', type=['jpg', 'jpeg', 'png'])
19
+
20
+ if picup is not None:
21
+ st.image(picup, caption='Uploaded Image', use_column_width=True)
22
+
23
+ img = Image.open(picup).convert('RGB')
24
+ img = img.resize((400,400))
25
+ img_array = np.array(img)
26
+ img_array = np.expand_dims(img_array, axis=0)
27
+
28
+ prediction = model.predict(img_array)
29
+ pred_index = np.argmax(prediction)
30
+ pred_class = klas[pred_index]
31
+ confidence = prediction[0][pred_index]*100
32
+ st.write(f'Prediction: **{pred_class}** ({confidence:.2f}% confidence)')
33
+
34
+ # class picupload(object):
35
+
36
+ # def __init__(self):
37
+ # self.fileTypes=['jpg']
38
+
39
+ # def run(self):
40
+ # topi = st.file_uploader('Upload picture', type=self.fileTypes)
41
+ # show_file = st.empty()
42
+ # if not topi:
43
+ # show_file.info('Please upload a picture')
44
+ # return
45
+ # content = topi.getvalue()
46
+ # if isinstance(topi, BytesIO):
47
+ # show_file.image(topi)
48
+ # topi.close()
49
+
50
+ # if __name__ == '__main__':
51
+ # helper = picupload()
52
+ # helper.run()
53
+
54
+ # def run():
55
+
56
+ # with st.form(key='type-of-cap'):
57
+
58
+ # st.write('##Topi')
59
+
60
+ # topi = st.file_uploader('Upload file', type=['jpg'])
61
+ # show_topi = st.empty()
62
+
63
+ # if not topi:
64
+ # show_topi.info('Please upload a picture : {}'.format(' '.join(['jpg'])))
65
+ # return
66
+ # content = topi.getvalue()
67
+
68
+ # if isinstance(topi, BytesIO):
69
+ # show_topi.image(topi)
70
+ # topi.close()
71
+
72
+ # run()
 
 
 
73