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