nemozajung commited on
Commit
70e4285
·
verified ·
1 Parent(s): e9a573f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -14
app.py CHANGED
@@ -3,17 +3,14 @@ from PIL import Image
3
  import gradio as gr
4
  from transformers import AutoImageProcessor, ResNetForImageClassification
5
 
6
- # โหลดโมเดล ResNet-50
7
  model_name = "microsoft/resnet-50"
8
  model = ResNetForImageClassification.from_pretrained(model_name)
9
  processor = AutoImageProcessor.from_pretrained(model_name)
10
 
11
- # จำลอง label BMI category
12
- bmi_labels = ["Underweight", "Normal", "Overweight", "Obese"]
13
-
14
- # ตัวอย่าง logic จำแนกเบื้องต้นโดย mapping จาก class id (mock-up logic)
15
  def map_to_bmi(class_id):
16
- if class_id < 250: # สุ่ม logic สำหรับ demo
17
  return "Underweight"
18
  elif class_id < 500:
19
  return "Normal"
@@ -22,22 +19,34 @@ def map_to_bmi(class_id):
22
  else:
23
  return "Obese"
24
 
25
- # ฟังก์ชันหลัก
26
- def predict_bmi(image):
 
 
 
 
 
 
 
 
 
27
  inputs = processor(images=image, return_tensors="pt")
28
  with torch.no_grad():
29
  logits = model(**inputs).logits
30
  class_id = logits.argmax(-1).item()
31
- bmi_category = map_to_bmi(class_id)
32
- return f"Estimated Body Type (BMI category): {bmi_category}"
 
 
 
33
 
34
- # Gradio UI
35
  demo = gr.Interface(
36
- fn=predict_bmi,
37
  inputs=gr.Image(type="pil"),
38
  outputs="text",
39
- title="BMI Estimator (Demo with ResNet-50)",
40
- description="จำแนกรูปร่างตามลักษณะ BMI ด้วย ResNet-50 (Demo Simulation)"
41
  )
42
 
43
  demo.launch()
 
3
  import gradio as gr
4
  from transformers import AutoImageProcessor, ResNetForImageClassification
5
 
6
+ # โหลดโมเดล
7
  model_name = "microsoft/resnet-50"
8
  model = ResNetForImageClassification.from_pretrained(model_name)
9
  processor = AutoImageProcessor.from_pretrained(model_name)
10
 
11
+ # Mock-up: แมป class_id ไปยัง BMI category
 
 
 
12
  def map_to_bmi(class_id):
13
+ if class_id < 250:
14
  return "Underweight"
15
  elif class_id < 500:
16
  return "Normal"
 
19
  else:
20
  return "Obese"
21
 
22
+ # Mock-up: แมป class_id ไปยัง Body Type
23
+ def map_to_body_type(class_id):
24
+ if class_id % 3 == 0:
25
+ return "Ectomorph (ผอมเพรียว)"
26
+ elif class_id % 3 == 1:
27
+ return "Mesomorph (สมส่วน/ล่ำ)"
28
+ else:
29
+ return "Endomorph (ล่ำอวบ)"
30
+
31
+ # ฟังก์ชันประมวลผลภาพ
32
+ def analyze_image(image):
33
  inputs = processor(images=image, return_tensors="pt")
34
  with torch.no_grad():
35
  logits = model(**inputs).logits
36
  class_id = logits.argmax(-1).item()
37
+
38
+ bmi = map_to_bmi(class_id)
39
+ body_type = map_to_body_type(class_id)
40
+
41
+ return f"🧍 Body Type: {body_type}\n📏 BMI Category: {bmi}"
42
 
43
+ # Gradio Interface
44
  demo = gr.Interface(
45
+ fn=analyze_image,
46
  inputs=gr.Image(type="pil"),
47
  outputs="text",
48
+ title="BMI + Body Type Estimator (Demo)",
49
+ description="วิเคราะห์ BMI และลักษณะรูปร่างจากภาพถ่ายด้วย ResNet-50 (จำลอง)"
50
  )
51
 
52
  demo.launch()