Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -22,45 +22,54 @@ model_paths = {
|
|
| 22 |
"ResNet50": "ResNet50_model.h5"
|
| 23 |
}
|
| 24 |
|
| 25 |
-
# ฟังก์ชันเตรียมข้อมูลภาพ (และคืนภาพที่ resize
|
| 26 |
-
def
|
| 27 |
image = image.resize(IMG_SIZE) # Resize เป็น 224x224
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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):
|
| 34 |
# โหลดโมเดลที่เลือก
|
| 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(
|
| 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 |
result_text = f"\n\n🧠 Prediction Result\n---------------------------\nClass: {class_name}\nConfidence: {confidence}%"
|
| 47 |
-
return
|
| 48 |
-
|
| 49 |
-
# Gradio
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# Run app
|
| 65 |
if __name__ == "__main__":
|
| 66 |
-
|
|
|
|
| 22 |
"ResNet50": "ResNet50_model.h5"
|
| 23 |
}
|
| 24 |
|
| 25 |
+
# ฟังก์ชันเตรียมข้อมูลภาพ (และคืนภาพที่ resize แล้ว)
|
| 26 |
+
def resize_image(image):
|
| 27 |
image = image.resize(IMG_SIZE) # Resize เป็น 224x224
|
| 28 |
+
return image # คืนภาพเพื่อแสดงต่อ
|
| 29 |
+
|
| 30 |
+
# ฟังก์ชันทำนาย โดยใช้ภาพที่ resize แล้ว
|
| 31 |
+
def predict_with_model(resized_image, model_name):
|
| 32 |
+
# เตรียมภาพ
|
| 33 |
+
image_array = np.array(resized_image) / 255.0 # Normalize
|
| 34 |
image_array = np.expand_dims(image_array, axis=0) # เพิ่ม batch dimension
|
|
|
|
| 35 |
|
|
|
|
|
|
|
| 36 |
# โหลดโมเดลที่เลือก
|
| 37 |
model = tf.keras.models.load_model(model_paths[model_name])
|
| 38 |
|
|
|
|
|
|
|
|
|
|
| 39 |
# ทำนายผล
|
| 40 |
+
prediction = model.predict(image_array)[0][0] # ได้ค่าความน่าจะเป็น
|
| 41 |
class_name = "Stroke" if prediction > 0.5 else "Non-Stroke"
|
| 42 |
confidence = round(float(prediction if prediction > 0.5 else 1 - prediction) * 100, 2)
|
| 43 |
|
| 44 |
+
# คืนผลลัพธ์
|
| 45 |
result_text = f"\n\n🧠 Prediction Result\n---------------------------\nClass: {class_name}\nConfidence: {confidence}%"
|
| 46 |
+
return result_text
|
| 47 |
+
|
| 48 |
+
# สร้าง Gradio Blocks แบบ 2 ขั้นตอน
|
| 49 |
+
with gr.Blocks() as demo:
|
| 50 |
+
gr.Markdown("# 🧠 Stroke Face Classification App")
|
| 51 |
+
gr.Markdown("Upload a face image. The image will be resized to 224x224 automatically. Then, select a model to classify.")
|
| 52 |
+
|
| 53 |
+
with gr.Row():
|
| 54 |
+
with gr.Column():
|
| 55 |
+
image_input = gr.Image(type="pil", label="🖼️ Upload Face Image")
|
| 56 |
+
model_selector = gr.Dropdown(choices=["Custom CNN", "VGG16", "ResNet50"], label="📊 Select Model to Classify")
|
| 57 |
+
predict_button = gr.Button("🚀 Predict Stroke")
|
| 58 |
+
|
| 59 |
+
with gr.Column():
|
| 60 |
+
resized_image_output = gr.Image(label="🖼️ Resized Image (224x224)")
|
| 61 |
+
output_text = gr.Textbox(label="📝 Prediction Output", lines=5)
|
| 62 |
+
|
| 63 |
+
# เมื่ออัปโหลดรูปแล้ว resize ทันที
|
| 64 |
+
image_input.change(resize_image, inputs=image_input, outputs=resized_image_output)
|
| 65 |
+
|
| 66 |
+
# เมื่อกดปุ่ม Predict ใช้ภาพที่ resize แล้วไปทำนาย
|
| 67 |
+
predict_button.click(
|
| 68 |
+
predict_with_model,
|
| 69 |
+
inputs=[resized_image_output, model_selector],
|
| 70 |
+
outputs=output_text
|
| 71 |
+
)
|
| 72 |
|
| 73 |
# Run app
|
| 74 |
if __name__ == "__main__":
|
| 75 |
+
demo.launch()
|