Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,68 +11,98 @@ def yolov8_func(image,
|
|
| 11 |
|
| 12 |
# Load the YOLOv8 model
|
| 13 |
model_path = "best.pt"
|
| 14 |
-
model = YOLO(model_path) #
|
| 15 |
|
| 16 |
-
#
|
| 17 |
result = model.predict(image, conf=conf_thresold, iou=iou_thresold, imgsz=image_size)
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
boxes = result[0].boxes # Bounding boxes
|
| 21 |
-
num_boxes = len(boxes) # Count the number of bounding boxes (detections)
|
| 22 |
-
|
| 23 |
-
# Print object detection details (optional)
|
| 24 |
print("Object type: ", boxes.cls)
|
| 25 |
print("Confidence: ", boxes.conf)
|
| 26 |
print("Coordinates: ", boxes.xyxy)
|
| 27 |
-
print(f"
|
| 28 |
|
| 29 |
-
#
|
| 30 |
if num_boxes > 10:
|
| 31 |
-
severity = "
|
| 32 |
-
recommendation = "
|
| 33 |
elif 5 <= num_boxes <= 10:
|
| 34 |
-
severity = "
|
| 35 |
-
recommendation = "
|
| 36 |
else:
|
| 37 |
-
severity = "
|
| 38 |
-
recommendation = "
|
| 39 |
|
| 40 |
-
print(f"
|
| 41 |
-
print(f"
|
| 42 |
|
| 43 |
-
#
|
| 44 |
render = render_result(model=model, image=image, result=result[0])
|
| 45 |
-
|
| 46 |
-
# Save the rendered image (with predictions)
|
| 47 |
predicted_image_save_path = "predicted_image.jpg"
|
| 48 |
render.save(predicted_image_save_path)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
# Define inputs for the Gradio app
|
| 54 |
inputs = [
|
| 55 |
-
gr.Image(type="filepath", label="Input Image"),
|
| 56 |
-
gr.Slider(minimum=320, maximum=1280, step=32, value=640, label="Image Size"),
|
| 57 |
-
gr.Slider(minimum=0, maximum=1, step=0.05, value=0.15, label="Confidence
|
| 58 |
-
gr.Slider(minimum=0, maximum=1, step=0.05, value=0.15, label="IOU
|
| 59 |
]
|
| 60 |
|
| 61 |
-
# Define the output for the Gradio app (image + text for severity and recommendation)
|
| 62 |
outputs = [
|
| 63 |
-
gr.Image(type="filepath", label="
|
| 64 |
-
gr.Textbox(label="
|
| 65 |
-
gr.Textbox(label="
|
| 66 |
]
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
-
yolo_app.launch(debug=True)
|
|
|
|
| 11 |
|
| 12 |
# Load the YOLOv8 model
|
| 13 |
model_path = "best.pt"
|
| 14 |
+
model = YOLO(model_path) # Thay bằng đường dẫn model của bạn
|
| 15 |
|
| 16 |
+
# Dự đoán
|
| 17 |
result = model.predict(image, conf=conf_thresold, iou=iou_thresold, imgsz=image_size)
|
| 18 |
+
boxes = result[0].boxes
|
| 19 |
+
num_boxes = len(boxes)
|
| 20 |
|
| 21 |
+
# In thông tin (tùy chọn)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
print("Object type: ", boxes.cls)
|
| 23 |
print("Confidence: ", boxes.conf)
|
| 24 |
print("Coordinates: ", boxes.xyxy)
|
| 25 |
+
print(f"Số lượng vùng phát hiện: {num_boxes}")
|
| 26 |
|
| 27 |
+
# Phân loại mức độ mụn và gợi ý
|
| 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."
|
| 31 |
elif 5 <= num_boxes <= 10:
|
| 32 |
+
severity = "Trung bình"
|
| 33 |
+
recommendation = "Hãy duy trì skincare đều đặn với sữa rửa mặt dịu nhẹ và dưỡng ẩm phù hợp."
|
| 34 |
else:
|
| 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 |
+
print(f"Tình trạng mụn: {severity}")
|
| 39 |
+
print(f"Khuyến nghị: {recommendation}")
|
| 40 |
|
| 41 |
+
# Kết xuất ảnh kết quả
|
| 42 |
render = render_result(model=model, image=image, result=result[0])
|
|
|
|
|
|
|
| 43 |
predicted_image_save_path = "predicted_image.jpg"
|
| 44 |
render.save(predicted_image_save_path)
|
| 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 Mụn bằng YOLOv8
|
| 51 |
+
Chọn một ảnh chụp khuôn mặt của bạn. Ứng dụng sẽ phân tích và đánh giá tình trạng mụn dựa trên mô hình YOLOv8.
|
| 52 |
+
- **Bước 1:** Tải lên ảnh khuôn mặt.
|
| 53 |
+
- **Bước 2:** Điều chỉnh các tham số (nếu cần).
|
| 54 |
+
- **Bước 3:** Nhấn **Submit** để nhận kết quả.
|
| 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;
|
| 66 |
+
text-align: center;
|
| 67 |
+
}
|
| 68 |
+
#component-0 h1 {
|
| 69 |
+
color: #F15B2A;
|
| 70 |
+
}
|
| 71 |
+
#component-0 h2, h3 {
|
| 72 |
+
color: #333;
|
| 73 |
+
}
|
| 74 |
+
.gr-button {
|
| 75 |
+
background-color: #F15B2A !important;
|
| 76 |
+
color: #fff !important;
|
| 77 |
+
border: none !important;
|
| 78 |
+
font-weight: bold !important;
|
| 79 |
+
}
|
| 80 |
+
.gr-button:hover {
|
| 81 |
+
background-color: #d94c1f !important;
|
| 82 |
+
}
|
| 83 |
+
"""
|
| 84 |
|
|
|
|
| 85 |
inputs = [
|
| 86 |
+
gr.Image(type="filepath", label="Ảnh Khuôn Mặt (Input Image)"),
|
| 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.15, label="Ngưỡng Confidence"),
|
| 89 |
+
gr.Slider(minimum=0, maximum=1, step=0.05, value=0.15, label="Ngưỡng IOU")
|
| 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=yolov8_func,
|
| 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" # Bạn có thể thử "default" hoặc "huggingface"
|
| 106 |
+
)
|
| 107 |
|
| 108 |
+
yolo_app.launch(debug=True)
|
|
|