-
Notifications
You must be signed in to change notification settings - Fork 22
/
image_classification.py
49 lines (37 loc) · 1.38 KB
/
image_classification.py
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
import io
import streamlit as st
from PIL import Image
import numpy as np
from tensorflow.keras.applications import EfficientNetB0
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.efficientnet import preprocess_input, decode_predictions
@st.cache(allow_output_mutation=True)
def load_model():
return EfficientNetB0(weights='imagenet')
def preprocess_image(img):
img = img.resize((224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
return x
def load_image():
uploaded_file = st.file_uploader(label='Выберите изображение для распознавания')
if uploaded_file is not None:
image_data = uploaded_file.getvalue()
st.image(image_data)
return Image.open(io.BytesIO(image_data))
else:
return None
def print_predictions(preds):
classes = decode_predictions(preds, top=3)[0]
for cl in classes:
st.write(cl[1], cl[2])
model = load_model()
st.title('Новая улучшенная классификации изображений в облаке Streamlit')
img = load_image()
result = st.button('Распознать изображение')
if result:
x = preprocess_image(img)
preds = model.predict(x)
st.write('**Результаты распознавания:**')
print_predictions(preds)