Spaces:
Sleeping
Sleeping
File size: 3,062 Bytes
7e64213 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
{
"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"
]
}
]
} |