File size: 1,184 Bytes
411d541
 
 
 
 
 
 
555b3f0
 
411d541
 
 
 
 
555b3f0
411d541
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
from huggingface_hub import hf_hub_download
from ultralytics import YOLO
from supervision import Detections
from PIL import Image
import torch
import numpy as np
import gradio as gr

def detect_faces(input_img):
    # download model
    model_path = hf_hub_download(
    repo_id="arnabdhar/YOLOv8-Face-Detection", filename="model.pt"
    )

    # load model
    model = YOLO(model_path)
    
    # 이미지를 YOLO 모델 입력 형식으로 변환
    image_cv = cv2.cvtColor(np.array(input_img), cv2.COLOR_RGB2BGR)

    # 얼굴 탐지
    output = model(input_img)
    results = Detections.from_ultralytics(output[0])

    # 탐지 결과 그리기
    drawn_image = draw_rect_with_conf(image_cv, results)

    # OpenCV 이미지를 PIL 이미지로 변환
    drawn_image_pil = Image.fromarray(cv2.cvtColor(drawn_image, cv2.COLOR_BGR2RGB))
    return drawn_image_pil

def gradio_interface(input_img):
    # 얼굴 탐지 함수 호출
    detected_img = detect_faces(input_img)
    return detected_img

# Gradio 인터페이스 설정
demo = gr.Interface(fn=gradio_interface, inputs=gr.Image(type="pil"), outputs="image")

if __name__ == "__main__":
    demo.launch()