Spaces:
Running
Running
File size: 3,203 Bytes
c08aeab |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
import torch
import cv2
import numpy as np
from PIL import Image
import gradio as gr
from ultralytics import YOLO
from transformers import AutoImageProcessor, ResNetForImageClassification
# โหลด YOLOv8 โมเดลสำหรับตรวจจับบุคคล
yolo_model = YOLO("yolov8n.pt") # ใช้เวอร์ชันเล็ก (nano)
# โหลด ResNet-50 สำหรับจำแนกภาพ
resnet_model_name = "microsoft/resnet-50"
resnet_model = ResNetForImageClassification.from_pretrained(resnet_model_name)
processor = AutoImageProcessor.from_pretrained(resnet_model_name)
# ฟังก์ชันสำหรับ mapping class_id → BMI
def map_to_bmi(class_id):
if class_id < 250:
return "Underweight"
elif class_id < 500:
return "Normal"
elif class_id < 750:
return "Overweight"
else:
return "Obese"
# ฟังก์ชันสำหรับ mapping class_id → Body Type
def map_to_body_type(class_id):
if class_id % 3 == 0:
return "Ectomorph (ผอมเพรียว)"
elif class_id % 3 == 1:
return "Mesomorph (สมส่วน/ล่ำ)"
else:
return "Endomorph (ล่ำอวบ)"
# ฟังก์ชันตรวจจับและครอบตัดบุคคล
def detect_and_crop_person(image_np):
results = yolo_model(image_np)
for result in results:
for box in result.boxes:
class_id = int(box.cls)
if yolo_model.names[class_id] == 'person':
x1, y1, x2, y2 = map(int, box.xyxy[0])
cropped = image_np[y1:y2, x1:x2]
cropped_rgb = cv2.cvtColor(cropped, cv2.COLOR_BGR2RGB)
return Image.fromarray(cropped_rgb)
return None
# ฟังก์ชันหลักสำหรับ Gradio
def process_image(image):
# Convert PIL to numpy
image_np = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
# ตรวจจับและครอบตัดบุคคล
cropped = detect_and_crop_person(image_np)
if cropped is None:
return "⚠️ ไม่พบบุคคลในภาพ"
# วิเคราะห์ด้วย ResNet
inputs = processor(images=cropped, return_tensors="pt")
with torch.no_grad():
logits = resnet_model(**inputs).logits
class_id = logits.argmax(-1).item()
# แมปผลลัพธ์
bmi = map_to_bmi(class_id)
body_type = map_to_body_type(class_id)
label = resnet_model.config.id2label[class_id]
return f"📷 ResNet Label: {label}\n🧍 Body Type: {body_type}\n📏 BMI Category: {bmi}"
# สร้าง Gradio Interface
demo = gr.Interface(
fn=process_image,
inputs=gr.Image(type="pil"),
outputs="text",
title="BMI + Body Type Estimator (with YOLOv8)",
description="วิเคราะห์ BMI และลักษณะรูปร่างจากภาพถ่าย โดยใช้ YOLOv8 สำหรับตรวจจับบุคคล และ ResNet-50 สำหรับวิเคราะห์"
)
if __name__ == "__main__":
demo.launch() |