Spaces:
Sleeping
Sleeping
uplaod 2 files
Browse files- app.py +38 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
|
| 6 |
+
# 事前訓練済みのモデルをロード
|
| 7 |
+
model = tf.keras.applications.MobileNetV2(weights='imagenet')
|
| 8 |
+
|
| 9 |
+
# 画像の前処理を行う関数
|
| 10 |
+
def preprocess_image(image):
|
| 11 |
+
image = image.resize((224, 224))
|
| 12 |
+
image = np.array(image)
|
| 13 |
+
image = np.expand_dims(image, axis=0)
|
| 14 |
+
image = tf.keras.applications.mobilenet_v2.preprocess_input(image)
|
| 15 |
+
return image
|
| 16 |
+
|
| 17 |
+
# 予測を行う関数
|
| 18 |
+
def predict(image):
|
| 19 |
+
processed_image = preprocess_image(image)
|
| 20 |
+
predictions = model.predict(processed_image)
|
| 21 |
+
decoded_predictions = tf.keras.applications.mobilenet_v2.decode_predictions(predictions, top=1)
|
| 22 |
+
return decoded_predictions[0][0][1]
|
| 23 |
+
|
| 24 |
+
# Streamlitの設定
|
| 25 |
+
st.title("Image Classification with CNN")
|
| 26 |
+
st.write("Upload an image to classify it using MobileNetV2")
|
| 27 |
+
|
| 28 |
+
# ファイルアップロード
|
| 29 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 30 |
+
|
| 31 |
+
if uploaded_file is not None:
|
| 32 |
+
image = Image.open(uploaded_file)
|
| 33 |
+
st.image(image, caption='Uploaded Image.', use_column_width=True)
|
| 34 |
+
st.write("")
|
| 35 |
+
st.write("Classifying...")
|
| 36 |
+
label = predict(image)
|
| 37 |
+
st.write(f"Prediction: {label}")
|
| 38 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
tensorflow
|
| 3 |
+
Pillow
|
| 4 |
+
|