Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,48 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from ultralyticsplus import YOLO
|
| 4 |
-
from PIL import Image
|
| 5 |
import os
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
iou_thresold=0.50):
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
model = YOLO(model_path) # Thay bằng đường dẫn model của bạn
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
num_boxes = len(boxes)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
if num_boxes > 10:
|
| 29 |
severity = "Nặng"
|
| 30 |
recommendation = "Bạn nên đến gặp bác sĩ da liễu và sử dụng liệu trình trị mụn chuyên sâu."
|
|
@@ -35,31 +53,44 @@ def yolov8_func(image,
|
|
| 35 |
severity = "Tốt"
|
| 36 |
recommendation = "Làn da bạn khá ổn! Tiếp tục duy trì thói quen chăm sóc da hiện tại."
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
predicted_image_save_path = "predicted_image.jpg"
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
return predicted_image_save_path, f"Tình trạng mụn: {severity}", recommendation
|
| 47 |
|
| 48 |
-
# Tạo phần mô tả và hướng dẫn
|
| 49 |
description_md = """
|
| 50 |
-
## Ứng dụng Nhận Diện
|
| 51 |
-
|
| 52 |
-
-
|
| 53 |
-
-
|
| 54 |
-
-
|
| 55 |
-
|
| 56 |
-
Phần kết quả sẽ cho ra:
|
| 57 |
-
- Ảnh với bounding box quanh các nốt mụn được phát hiện.
|
| 58 |
-
- Đánh giá tình trạng mụn.
|
| 59 |
-
- Lời khuyên chăm sóc da.
|
| 60 |
"""
|
| 61 |
|
| 62 |
-
# CSS tùy chỉnh để giao diện đẹp hơn
|
| 63 |
custom_css = """
|
| 64 |
#component-0, #component-1, #component-2, #component-3, #component-4 {
|
| 65 |
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
|
|
@@ -83,26 +114,27 @@ custom_css = """
|
|
| 83 |
"""
|
| 84 |
|
| 85 |
inputs = [
|
| 86 |
-
gr.Image(type="filepath", label="Ảnh Khuôn Mặt
|
| 87 |
gr.Slider(minimum=320, maximum=1280, step=32, value=640, label="Kích thước ảnh (Image Size)"),
|
| 88 |
-
gr.Slider(minimum=0, maximum=1, step=0.05, value=0.
|
| 89 |
-
gr.Slider(minimum=0, maximum=1, step=0.05, value=0.
|
| 90 |
]
|
| 91 |
|
| 92 |
outputs = [
|
| 93 |
gr.Image(type="filepath", label="Ảnh Sau Khi Xử Lý"),
|
| 94 |
gr.Textbox(label="Tình Trạng Mụn", interactive=False),
|
| 95 |
-
gr.Textbox(label="Khuyến Nghị", interactive=False)
|
|
|
|
| 96 |
]
|
| 97 |
|
| 98 |
yolo_app = gr.Interface(
|
| 99 |
-
fn=
|
| 100 |
inputs=inputs,
|
| 101 |
outputs=outputs,
|
| 102 |
-
title="YOLOv8: Nhận Diện Mụn",
|
| 103 |
description=description_md,
|
| 104 |
css=custom_css,
|
| 105 |
-
theme="default"
|
| 106 |
)
|
| 107 |
|
| 108 |
-
yolo_app.launch(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from ultralyticsplus import YOLO
|
| 4 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 5 |
import os
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
import numpy as np
|
| 8 |
|
| 9 |
+
# Tải model YOLO (detection)
|
| 10 |
+
yolo_model_path = "best.pt" # Thay đường dẫn model YOLO của bạn
|
| 11 |
+
yolo_model = YOLO(yolo_model_path)
|
|
|
|
| 12 |
|
| 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")
|
| 19 |
+
|
| 20 |
+
# Detect với YOLO
|
| 21 |
+
results = yolo_model.predict(pil_image, conf=conf_thresold, iou=iou_thresold, imgsz=image_size)
|
| 22 |
+
boxes = results[0].boxes
|
| 23 |
num_boxes = len(boxes)
|
| 24 |
|
| 25 |
+
# Nếu không có vùng mụn, trả về kết quả
|
| 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."
|
| 29 |
+
return image, f"Tình trạng mụn: {severity}", recommendation, "Không có loại mụn nào được phát hiện."
|
| 30 |
+
|
| 31 |
+
# Chuyển boxes về array
|
| 32 |
+
xyxy = boxes.xyxy.detach().cpu().numpy().astype(int)
|
| 33 |
+
confidences = boxes.conf.detach().cpu().numpy()
|
| 34 |
+
|
| 35 |
+
# Crop từng box và classify
|
| 36 |
+
class_names = []
|
| 37 |
+
for box in xyxy:
|
| 38 |
+
x1, y1, x2, y2 = box
|
| 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
|
| 46 |
if num_boxes > 10:
|
| 47 |
severity = "Nặng"
|
| 48 |
recommendation = "Bạn nên đến gặp bác sĩ da liễu và sử dụng liệu trình trị mụn chuyên sâu."
|
|
|
|
| 53 |
severity = "Tốt"
|
| 54 |
recommendation = "Làn da bạn khá ổn! Tiếp tục duy trì thói quen chăm sóc da hiện tại."
|
| 55 |
|
| 56 |
+
# Vẽ bounding box và class name lên ảnh
|
| 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 |
+
# Dùng textbbox để xác định kích thước text
|
| 66 |
+
bbox = draw.textbbox((0,0), text, font=font)
|
| 67 |
+
text_w = bbox[2]-bbox[0]
|
| 68 |
+
text_h = bbox[3]-bbox[1]
|
| 69 |
+
|
| 70 |
+
# Vẽ nền cho text
|
| 71 |
+
draw.rectangle([x1, y1 - text_h, x1 + text_w, y1], fill="red")
|
| 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ứ tự và loại mụn tương ứng.
|
| 91 |
+
- Đánh giá tình trạng da và đưa ra khuyến nghị.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
"""
|
| 93 |
|
|
|
|
| 94 |
custom_css = """
|
| 95 |
#component-0, #component-1, #component-2, #component-3, #component-4 {
|
| 96 |
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
|
|
|
|
| 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)"),
|
| 119 |
+
gr.Slider(minimum=0, maximum=1, step=0.05, value=0.4, label="Ngưỡng Confidence"),
|
| 120 |
+
gr.Slider(minimum=0, maximum=1, step=0.05, value=0.5, label="Ngưỡng IOU")
|
| 121 |
]
|
| 122 |
|
| 123 |
outputs = [
|
| 124 |
gr.Image(type="filepath", label="Ảnh Sau Khi Xử Lý"),
|
| 125 |
gr.Textbox(label="Tình Trạng Mụn", interactive=False),
|
| 126 |
+
gr.Textbox(label="Khuyến Nghị", interactive=False),
|
| 127 |
+
gr.Textbox(label="Loại Mụn Phát Hiện", interactive=False)
|
| 128 |
]
|
| 129 |
|
| 130 |
yolo_app = gr.Interface(
|
| 131 |
+
fn=detect_and_classify,
|
| 132 |
inputs=inputs,
|
| 133 |
outputs=outputs,
|
| 134 |
+
title="YOLOv8 + Classification: Nhận Diện & Phân Loại Mụn",
|
| 135 |
description=description_md,
|
| 136 |
css=custom_css,
|
| 137 |
+
theme="default"
|
| 138 |
)
|
| 139 |
|
| 140 |
+
yolo_app.launch(share=True)
|