Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""app.py
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1J1hXUB5eoxFBDoclJh3sO2ehEIFKr3Pw
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import gradio as gr
|
| 11 |
+
import tensorflow as tf
|
| 12 |
+
import numpy as np
|
| 13 |
+
from PIL import Image
|
| 14 |
+
|
| 15 |
+
# ขนาดภาพที่ใช้ในโมเดล
|
| 16 |
+
IMG_SIZE = (224, 224)
|
| 17 |
+
|
| 18 |
+
# สร้าง Dictionary ที่เก็บชื่อโมเดลและ path ไฟล์ .h5
|
| 19 |
+
model_paths = {
|
| 20 |
+
"Custom CNN": "Custom_CNN_model.h5",
|
| 21 |
+
"VGG16": "VGG16_model.h5",
|
| 22 |
+
"ResNet50": "ResNet50_model.h5"
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
# ฟังก์ชันเตรียมข้อมูลภาพ
|
| 26 |
+
def preprocess_image(image):
|
| 27 |
+
image = image.resize(IMG_SIZE) # Resize
|
| 28 |
+
image = np.array(image) / 255.0 # Normalize
|
| 29 |
+
image = np.expand_dims(image, axis=0) # เพิ่ม batch dimension
|
| 30 |
+
return image
|
| 31 |
+
|
| 32 |
+
# ฟังก์ชันทำนาย โดยเลือกโมเดล
|
| 33 |
+
def predict_with_model(image, model_name):
|
| 34 |
+
# โหลดโมเดลที่เลือก
|
| 35 |
+
model = tf.keras.models.load_model(model_paths[model_name])
|
| 36 |
+
|
| 37 |
+
# เตรียมภาพ
|
| 38 |
+
processed_image = preprocess_image(image)
|
| 39 |
+
|
| 40 |
+
# ทำนายผล
|
| 41 |
+
prediction = model.predict(processed_image)[0][0] # ได้ค่าความน่าจะเป็น
|
| 42 |
+
class_name = "Stroke" if prediction > 0.5 else "Non-Stroke"
|
| 43 |
+
confidence = round(float(prediction if prediction > 0.5 else 1 - prediction) * 100, 2)
|
| 44 |
+
|
| 45 |
+
# คืนผลลัพธ์
|
| 46 |
+
return f"Class: {class_name} (Confidence: {confidence}%)"
|
| 47 |
+
|
| 48 |
+
# Gradio Interface
|
| 49 |
+
interface = gr.Interface(
|
| 50 |
+
fn=predict_with_model,
|
| 51 |
+
inputs=[
|
| 52 |
+
gr.Image(type="pil", label="Upload Face Image"),
|
| 53 |
+
gr.Dropdown(choices=["Custom CNN", "VGG16", "ResNet50"], label="Select Model")
|
| 54 |
+
],
|
| 55 |
+
outputs="text",
|
| 56 |
+
title="Stroke Face Classification",
|
| 57 |
+
description="Upload a face image to predict whether the person has stroke or not. Select model to classify."
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# Run app
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
interface.launch()
|