Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -22,12 +22,12 @@ model_paths = {
|
|
| 22 |
"ResNet50": "ResNet50_model.h5"
|
| 23 |
}
|
| 24 |
|
| 25 |
-
# ฟังก์ชันเตรียมข้อมูลภาพ
|
| 26 |
def preprocess_image(image):
|
| 27 |
-
image = image.resize(IMG_SIZE) # Resize
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
return image
|
| 31 |
|
| 32 |
# ฟังก์ชันทำนาย โดยเลือกโมเดล
|
| 33 |
def predict_with_model(image, model_name):
|
|
@@ -35,26 +35,30 @@ def predict_with_model(image, model_name):
|
|
| 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 |
-
|
|
|
|
| 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=
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
# Run app
|
|
|
|
| 22 |
"ResNet50": "ResNet50_model.h5"
|
| 23 |
}
|
| 24 |
|
| 25 |
+
# ฟังก์ชันเตรียมข้อมูลภาพ (และคืนภาพที่ resize แล้วด้วย)
|
| 26 |
def preprocess_image(image):
|
| 27 |
+
image = image.resize(IMG_SIZE) # Resize เป็น 224x224
|
| 28 |
+
image_array = np.array(image) / 255.0 # Normalize
|
| 29 |
+
image_array = np.expand_dims(image_array, axis=0) # เพิ่ม batch dimension
|
| 30 |
+
return image_array, image # คืนทั้ง array สำหรับโมเดล และ image สำหรับแสดง
|
| 31 |
|
| 32 |
# ฟังก์ชันทำนาย โดยเลือกโมเดล
|
| 33 |
def predict_with_model(image, model_name):
|
|
|
|
| 35 |
model = tf.keras.models.load_model(model_paths[model_name])
|
| 36 |
|
| 37 |
# เตรียมภาพ
|
| 38 |
+
processed_image, resized_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 |
+
# คืนผลลัพธ์พร้อมภาพที่ resize แล้ว
|
| 46 |
+
result_text = f"\n\n🧠 Prediction Result\n---------------------------\nClass: {class_name}\nConfidence: {confidence}%"
|
| 47 |
+
return resized_image, result_text
|
| 48 |
|
| 49 |
# Gradio Interface
|
| 50 |
interface = gr.Interface(
|
| 51 |
fn=predict_with_model,
|
| 52 |
inputs=[
|
| 53 |
+
gr.Image(type="pil", label="🖼️ Upload Face Image"),
|
| 54 |
+
gr.Dropdown(choices=["Custom CNN", "VGG16", "ResNet50"], label="📊 Select Model to Classify")
|
| 55 |
],
|
| 56 |
+
outputs=[
|
| 57 |
+
gr.Image(label="🖼️ Resized Image (224x224)"), # แสดงภาพที่ resize แล้ว
|
| 58 |
+
gr.Textbox(label="📝 Prediction Output", lines=5) # ช่องข้อความผลลัพธ์
|
| 59 |
+
],
|
| 60 |
+
title="🧠 Stroke Face Classification",
|
| 61 |
+
description="Upload a face image to predict whether the person has stroke or not. The image will be resized to 224x224 before classification. Select model to classify and see result below."
|
| 62 |
)
|
| 63 |
|
| 64 |
# Run app
|