Spaces:
Sleeping
Sleeping
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() | |