Suphawan commited on
Commit
5f05292
·
verified ·
1 Parent(s): 7a1ed27

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -28
app.py CHANGED
@@ -22,45 +22,54 @@ model_paths = {
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):
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(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
65
  if __name__ == "__main__":
66
- interface.launch()
 
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()