{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "id": "jaqWRbrAqXI2" }, "outputs": [], "source": [ "import gradio as gr\n", "import tensorflow as tf\n", "import numpy as np\n", "from PIL import Image\n", "\n", "# ขนาดภาพที่ใช้ในโมเดล\n", "IMG_SIZE = (224, 224)\n", "\n", "# สร้าง Dictionary ที่เก็บชื่อโมเดลและ path ไฟล์ .h5\n", "model_paths = {\n", " \"Custom CNN\": \"Custom_CNN_model.h5\",\n", " \"VGG16\": \"VGG16_model.h5\",\n", " \"ResNet50\": \"ResNet50_model.h5\"\n", "}\n", "\n", "# ฟังก์ชันเตรียมข้อมูลภาพ\n", "def preprocess_image(image):\n", " image = image.resize(IMG_SIZE) # Resize\n", " image = np.array(image) / 255.0 # Normalize\n", " image = np.expand_dims(image, axis=0) # เพิ่ม batch dimension\n", " return image\n", "\n", "# ฟังก์ชันทำนาย โดยเลือกโมเดล\n", "def predict_with_model(image, model_name):\n", " # โหลดโมเดลที่เลือก\n", " model = tf.keras.models.load_model(model_paths[model_name])\n", "\n", " # เตรียมภาพ\n", " processed_image = preprocess_image(image)\n", "\n", " # ทำนายผล\n", " prediction = model.predict(processed_image)[0][0] # ได้ค่าความน่าจะเป็น\n", " class_name = \"Stroke\" if prediction > 0.5 else \"Non-Stroke\"\n", " confidence = round(float(prediction if prediction > 0.5 else 1 - prediction) * 100, 2)\n", "\n", " # คืนผลลัพธ์\n", " return f\"Class: {class_name} (Confidence: {confidence}%)\"\n", "\n", "# Gradio Interface\n", "interface = gr.Interface(\n", " fn=predict_with_model,\n", " inputs=[\n", " gr.Image(type=\"pil\", label=\"Upload Face Image\"),\n", " gr.Dropdown(choices=[\"Custom CNN\", \"VGG16\", \"ResNet50\"], label=\"Select Model\")\n", " ],\n", " outputs=\"text\",\n", " title=\"Stroke Face Classification\",\n", " description=\"Upload a face image to predict whether the person has stroke or not. Select model to classify.\"\n", ")\n", "\n", "# Run app\n", "if __name__ == \"__main__\":\n", " interface.launch()\n" ] } ] }