Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,6 +13,13 @@ yolo_model = YOLO(yolo_model_path)
|
|
| 13 |
# Tải pipeline classification
|
| 14 |
class_pipe = pipeline("image-classification", model="Hemg/Acne-classification")
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def detect_and_classify(image, image_size, conf_thresold=0.4, iou_thresold=0.5):
|
| 17 |
# image là đường dẫn file, đọc thành PIL
|
| 18 |
pil_image = Image.open(image).convert("RGB")
|
|
@@ -22,7 +29,7 @@ def detect_and_classify(image, image_size, conf_thresold=0.4, iou_thresold=0.5):
|
|
| 22 |
boxes = results[0].boxes
|
| 23 |
num_boxes = len(boxes)
|
| 24 |
|
| 25 |
-
# Nếu không có vùng mụn
|
| 26 |
if num_boxes == 0:
|
| 27 |
severity = "Tốt"
|
| 28 |
recommendation = "Làn da bạn khá ổn! Tiếp tục duy trì thói quen chăm sóc da."
|
|
@@ -39,7 +46,7 @@ def detect_and_classify(image, image_size, conf_thresold=0.4, iou_thresold=0.5):
|
|
| 39 |
crop = pil_image.crop((x1, y1, x2, y2))
|
| 40 |
# Classification trên vùng crop
|
| 41 |
results_class = class_pipe(crop)
|
| 42 |
-
top_class = results_class[0]['label']
|
| 43 |
class_names.append(top_class)
|
| 44 |
|
| 45 |
# Đánh giá tình trạng dựa trên số lượng mụn
|
|
@@ -57,12 +64,20 @@ def detect_and_classify(image, image_size, conf_thresold=0.4, iou_thresold=0.5):
|
|
| 57 |
draw = ImageDraw.Draw(pil_image)
|
| 58 |
font = ImageFont.load_default()
|
| 59 |
|
|
|
|
|
|
|
|
|
|
| 60 |
for i, (box, cname, conf) in enumerate(zip(xyxy, class_names, confidences), start=1):
|
| 61 |
x1, y1, x2, y2 = box
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
draw.rectangle([x1, y1, x2, y2], outline="red", width=2)
|
| 63 |
-
text = f"#{i}: {cname} ({conf:.2f})"
|
| 64 |
|
| 65 |
-
#
|
| 66 |
bbox = draw.textbbox((0,0), text, font=font)
|
| 67 |
text_w = bbox[2]-bbox[0]
|
| 68 |
text_h = bbox[3]-bbox[1]
|
|
@@ -72,22 +87,20 @@ def detect_and_classify(image, image_size, conf_thresold=0.4, iou_thresold=0.5):
|
|
| 72 |
# Vẽ text
|
| 73 |
draw.text((x1, y1 - text_h), text, fill="white", font=font)
|
| 74 |
|
|
|
|
|
|
|
|
|
|
| 75 |
# Lưu ảnh kết quả
|
| 76 |
predicted_image_save_path = "predicted_image.jpg"
|
| 77 |
pil_image.save(predicted_image_save_path)
|
| 78 |
|
| 79 |
-
# Liệt kê loại mụn theo số thứ tự
|
| 80 |
-
acne_types_str = "Danh sách mụn phát hiện:\n"
|
| 81 |
-
for i, cname in enumerate(class_names, start=1):
|
| 82 |
-
acne_types_str += f"Mụn #{i}: {cname}\n"
|
| 83 |
-
|
| 84 |
return predicted_image_save_path, f"Tình trạng mụn: {severity}", recommendation, acne_types_str
|
| 85 |
|
| 86 |
description_md = """
|
| 87 |
## Ứng dụng Nhận Diện & Phân Loại Mụn
|
| 88 |
- Sử dụng YOLO để phát hiện các vùng mụn trên khuôn mặt.
|
| 89 |
- Sử dụng mô hình phân loại (Hemg/Acne-classification) để xác định loại mụn.
|
| 90 |
-
- Hiển thị bounding box kèm số thứ
|
| 91 |
- Đánh giá tình trạng da và đưa ra khuyến nghị.
|
| 92 |
"""
|
| 93 |
|
|
@@ -113,6 +126,8 @@ custom_css = """
|
|
| 113 |
}
|
| 114 |
"""
|
| 115 |
|
|
|
|
|
|
|
| 116 |
inputs = [
|
| 117 |
gr.Image(type="filepath", label="Ảnh Khuôn Mặt"),
|
| 118 |
gr.Slider(minimum=320, maximum=1280, step=32, value=640, label="Kích thước ảnh (Image Size)"),
|
|
|
|
| 13 |
# Tải pipeline classification
|
| 14 |
class_pipe = pipeline("image-classification", model="Hemg/Acne-classification")
|
| 15 |
|
| 16 |
+
# Mapping từ tiếng Anh sang tiếng Việt cho các lớp: Comedo, Acne, Clear
|
| 17 |
+
label_mapping = {
|
| 18 |
+
"Comedo": "Mụn cám",
|
| 19 |
+
"Acne": "Mụn trứng cá",
|
| 20 |
+
"Clear": "Vùng da sạch"
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
def detect_and_classify(image, image_size, conf_thresold=0.4, iou_thresold=0.5):
|
| 24 |
# image là đường dẫn file, đọc thành PIL
|
| 25 |
pil_image = Image.open(image).convert("RGB")
|
|
|
|
| 29 |
boxes = results[0].boxes
|
| 30 |
num_boxes = len(boxes)
|
| 31 |
|
| 32 |
+
# Nếu không có vùng mụn
|
| 33 |
if num_boxes == 0:
|
| 34 |
severity = "Tốt"
|
| 35 |
recommendation = "Làn da bạn khá ổn! Tiếp tục duy trì thói quen chăm sóc da."
|
|
|
|
| 46 |
crop = pil_image.crop((x1, y1, x2, y2))
|
| 47 |
# Classification trên vùng crop
|
| 48 |
results_class = class_pipe(crop)
|
| 49 |
+
top_class = results_class[0]['label'] # tiếng Anh
|
| 50 |
class_names.append(top_class)
|
| 51 |
|
| 52 |
# Đánh giá tình trạng dựa trên số lượng mụn
|
|
|
|
| 64 |
draw = ImageDraw.Draw(pil_image)
|
| 65 |
font = ImageFont.load_default()
|
| 66 |
|
| 67 |
+
# Tạo chuỗi kết quả loại mụn
|
| 68 |
+
acne_types_str = "Danh sách mụn phát hiện:\n"
|
| 69 |
+
|
| 70 |
for i, (box, cname, conf) in enumerate(zip(xyxy, class_names, confidences), start=1):
|
| 71 |
x1, y1, x2, y2 = box
|
| 72 |
+
|
| 73 |
+
# Lấy tên tiếng việt từ mapping (nếu không có, dùng tên cũ)
|
| 74 |
+
vn_name = label_mapping.get(cname, cname)
|
| 75 |
+
|
| 76 |
+
# Vẽ box
|
| 77 |
draw.rectangle([x1, y1, x2, y2], outline="red", width=2)
|
| 78 |
+
text = f"#{i}: {cname} ({vn_name}) ({conf:.2f})"
|
| 79 |
|
| 80 |
+
# Sử dụng textbbox để lấy kích thước text
|
| 81 |
bbox = draw.textbbox((0,0), text, font=font)
|
| 82 |
text_w = bbox[2]-bbox[0]
|
| 83 |
text_h = bbox[3]-bbox[1]
|
|
|
|
| 87 |
# Vẽ text
|
| 88 |
draw.text((x1, y1 - text_h), text, fill="white", font=font)
|
| 89 |
|
| 90 |
+
# Thêm vào chuỗi mô tả
|
| 91 |
+
acne_types_str += f"Mụn #{i}: {cname} ({vn_name})\n"
|
| 92 |
+
|
| 93 |
# Lưu ảnh kết quả
|
| 94 |
predicted_image_save_path = "predicted_image.jpg"
|
| 95 |
pil_image.save(predicted_image_save_path)
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
return predicted_image_save_path, f"Tình trạng mụn: {severity}", recommendation, acne_types_str
|
| 98 |
|
| 99 |
description_md = """
|
| 100 |
## Ứng dụng Nhận Diện & Phân Loại Mụn
|
| 101 |
- Sử dụng YOLO để phát hiện các vùng mụn trên khuôn mặt.
|
| 102 |
- Sử dụng mô hình phân loại (Hemg/Acne-classification) để xác định loại mụn.
|
| 103 |
+
- Hiển thị bounding box kèm số thứ tự, tên tiếng Anh và tiếng Việt của loại mụn.
|
| 104 |
- Đánh giá tình trạng da và đưa ra khuyến nghị.
|
| 105 |
"""
|
| 106 |
|
|
|
|
| 126 |
}
|
| 127 |
"""
|
| 128 |
|
| 129 |
+
import gradio as gr
|
| 130 |
+
|
| 131 |
inputs = [
|
| 132 |
gr.Image(type="filepath", label="Ảnh Khuôn Mặt"),
|
| 133 |
gr.Slider(minimum=320, maximum=1280, step=32, value=640, label="Kích thước ảnh (Image Size)"),
|