streamlit2 / app.py
maty0505's picture
uplaod 2 files
4219708 verified
import streamlit as st
from PIL import Image
import numpy as np
import tensorflow as tf
# 事前訓練済みのモデルをロード
model = tf.keras.applications.MobileNetV2(weights='imagenet')
# 画像の前処理を行う関数
def preprocess_image(image):
image = image.resize((224, 224))
image = np.array(image)
image = np.expand_dims(image, axis=0)
image = tf.keras.applications.mobilenet_v2.preprocess_input(image)
return image
# 予測を行う関数
def predict(image):
processed_image = preprocess_image(image)
predictions = model.predict(processed_image)
decoded_predictions = tf.keras.applications.mobilenet_v2.decode_predictions(predictions, top=1)
return decoded_predictions[0][0][1]
# Streamlitの設定
st.title("Image Classification with CNN")
st.write("Upload an image to classify it using MobileNetV2")
# ファイルアップロード
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption='Uploaded Image.', use_column_width=True)
st.write("")
st.write("Classifying...")
label = predict(image)
st.write(f"Prediction: {label}")