File size: 1,296 Bytes
764ea86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os

import streamlit as st
from PIL import Image

from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array

# IMG_PATH = 'imgs'

def main():

    st.title("AI MNIST")
    file = st.file_uploader('画像ををップロードしてください.', type=['jpg', 'jpeg', 'png'])

    if file:
        st.markdown(f'{file.name} γ‚’γ‚’γƒƒγƒ—γƒ­γƒΌγƒ‰γ—γΎγ—γŸ.')

        img_path = os.path.join(file.name)
        # η”»εƒγ‚’δΏε­˜γ™γ‚‹
        with open(img_path, 'wb') as f:
            f.write(file.read())
            
        # δΏε­˜γ—γŸη”»εƒγ‚’θ‘¨η€Ί
        img = Image.open(img_path)
        st.image(img)

        # 画像をArray归式に倉換
        img = load_img(img_path, target_size=(28, 28), color_mode = 'grayscale')
        img_array = img_to_array(img)
        img_array = img_array.reshape((1, 28, 28))
        img_array = img_array/255        

        # δΏε­˜γ—γŸγƒ’γƒ‡γƒ«γ‚’ε‘Όγ³ε‡Ίγ—
        model_path = os.path.join('model.h5')
        model = load_model(model_path)
        result = model.predict(img_array)

        prediction = result.argmax()

        st.text_area("γ“γ‚Œγ―:", prediction, height=20)

if __name__ == '__main__':
    main()