Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,17 +3,14 @@ from PIL import Image
|
|
| 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 |
-
#
|
| 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:
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
# Gradio
|
| 35 |
demo = gr.Interface(
|
| 36 |
-
fn=
|
| 37 |
inputs=gr.Image(type="pil"),
|
| 38 |
outputs="text",
|
| 39 |
-
title="BMI Estimator (Demo
|
| 40 |
-
description="
|
| 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()
|