Spaces:
Sleeping
Sleeping
# -*- coding: utf-8 -*- | |
"""app.py | |
Automatically generated by Colab. | |
Original file is located at | |
https://colab.research.google.com/drive/1J1hXUB5eoxFBDoclJh3sO2ehEIFKr3Pw | |
""" | |
import gradio as gr | |
import tensorflow as tf | |
import numpy as np | |
from PIL import Image | |
# ขนาดภาพที่ใช้ในโมเดล | |
IMG_SIZE = (224, 224) | |
# สร้าง Dictionary ที่เก็บชื่อโมเดลและ path ไฟล์ .h5 | |
model_paths = { | |
"Custom CNN": "Custom_CNN_model.h5", | |
"VGG16": "VGG16_model.h5", | |
"ResNet50": "ResNet50_model.h5" | |
} | |
# ฟังก์ชันเตรียมข้อมูลภาพ | |
def preprocess_image(image): | |
image = image.resize(IMG_SIZE) # Resize | |
image = np.array(image) / 255.0 # Normalize | |
image = np.expand_dims(image, axis=0) # เพิ่ม batch dimension | |
return image | |
# ฟังก์ชันทำนาย โดยเลือกโมเดล | |
def predict_with_model(image, model_name): | |
# โหลดโมเดลที่เลือก | |
model = tf.keras.models.load_model(model_paths[model_name]) | |
# เตรียมภาพ | |
processed_image = preprocess_image(image) | |
# ทำนายผล | |
prediction = model.predict(processed_image)[0][0] # ได้ค่าความน่าจะเป็น | |
class_name = "Stroke" if prediction > 0.5 else "Non-Stroke" | |
confidence = round(float(prediction if prediction > 0.5 else 1 - prediction) * 100, 2) | |
# คืนผลลัพธ์ | |
return f"\n\n🧠 Prediction Result\n---------------------------\nClass: {class_name}\nConfidence: {confidence}%" | |
# Gradio Interface | |
interface = gr.Interface( | |
fn=predict_with_model, | |
inputs=[ | |
gr.Image(type="pil", label="🖼️ Upload Face Image"), | |
gr.Dropdown(choices=["Custom CNN", "VGG16", "ResNet50"], label="📊 Select Model to Classify") | |
], | |
outputs=gr.Textbox(label="📝 Prediction Output", lines=5), # ช่อง Output ใหญ่ขึ้น | |
title="🧠 Stroke Face Classification", | |
description="Upload a face image to predict whether the person has stroke or not. Select model to classify. The output will show the prediction result clearly." | |
) | |
# Run app | |
if __name__ == "__main__": | |
interface.launch() |