File size: 1,170 Bytes
f02f71d
9eb833a
 
 
 
f02f71d
9eb833a
 
f02f71d
9eb833a
 
 
 
 
 
 
f02f71d
9eb833a
 
 
 
 
 
 
 
 
f02f71d
 
acd5316
f02f71d
9eb833a
 
acd5316
f02f71d
 
acd5316
f02f71d
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
import gradio as gr
import dlib
import cv2
import numpy as np
from PIL import Image

# Dlib์˜ ์–ผ๊ตด ๊ฐ์ง€๊ธฐ ์ดˆ๊ธฐํ™”
detector = dlib.get_frontal_face_detector()

# ์–ผ๊ตด์— ๋ธ”๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ์ ์šฉํ•˜๋Š” ํ•จ์ˆ˜
def blur_faces(image):
    # Gradio์—์„œ ์ „๋‹ฌ๋œ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๋ฅผ OpenCV ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜
    img = cv2.imdecode(np.frombuffer(image.read(), np.uint8), 1)
    
    # ์–ผ๊ตด ๊ฐ์ง€
    faces = detector(img, 1)

    # ๊ฐ ์–ผ๊ตด์— ๋Œ€ํ•ด ๋ธ”๋Ÿฌ ์ฒ˜๋ฆฌ
    for face in faces:
        x, y, w, h = face.left(), face.top(), face.width(), face.height()
        face_roi = img[y:y+h, x:x+w]
        face_roi = cv2.GaussianBlur(face_roi, (99, 99), 30)  # ๋ธ”๋Ÿฌ ์ฒ˜๋ฆฌ

    # OpenCV ์ด๋ฏธ์ง€๋ฅผ Pillow ์ด๋ฏธ์ง€๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜
    img_pil = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    img_byte_array = img_pil.tobytes()
    return img_byte_array

# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ •
iface = gr.Interface(
    fn=blur_faces,  # ์‚ฌ์šฉํ•  ํ•จ์ˆ˜
    inputs=gr.Image(type="pil", label="Upload Image"),  # ์ž…๋ ฅ ํ•„๋“œ ์„ค์ •
    outputs=gr.Image(type="pil")  # ์ถœ๋ ฅ ํ•„๋“œ ์„ค์ •
)

# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
iface.launch()