Image_Filter / app.py
MinE0137's picture
Update app.py
9eb833a verified
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()