Spaces:
Runtime error
Runtime error
gmarti
commited on
Commit
·
55de749
1
Parent(s):
3f67478
Upload app and model
Browse files- app.py +95 -0
- images/18.jpg +0 -0
- images/22.png +0 -0
app.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import math
|
3 |
+
import os
|
4 |
+
import yolov5
|
5 |
+
import cv2
|
6 |
+
import torch
|
7 |
+
import numpy as np
|
8 |
+
from models.experimental import attempt_load
|
9 |
+
|
10 |
+
# load model
|
11 |
+
#model_face = torch.hub.load('.', 'custom', '/home/gmarti/Documentos/detection/yolov5s-face.pt', source='local', force_reload=True).to("cpu")
|
12 |
+
#torch.save(model_face, 'face_model.pt')
|
13 |
+
model_face = torch.load('/home/gmarti/Documentos/detection/face_model.pt')
|
14 |
+
model_face.eval()
|
15 |
+
model_plate = yolov5.load('keremberke/yolov5m-license-plate', device="cpu")
|
16 |
+
# # set model parameters
|
17 |
+
model_plate.conf = 0.25 # NMS confidence threshold
|
18 |
+
model_plate.iou = 0.45 # NMS IoU threshold
|
19 |
+
model_plate.agnostic = False # NMS class-agnostic
|
20 |
+
model_plate.multi_label = False # NMS multiple labels per box
|
21 |
+
model_plate.max_det = 1000 # maximum number of detections per image
|
22 |
+
|
23 |
+
|
24 |
+
#model = torch.hub.load('.', 'yolov5s','/home/gmarti/Documentos/detection/yolov5s-face.pt', 'yolov5s', device='cpu', source='local')
|
25 |
+
|
26 |
+
#model_face = torch.load('/home/gmarti/Documentos/detection/yolov5s-face.pt', map_location=torch.device("cpu"))
|
27 |
+
#model_face = attempt_load('/home/gmarti/Documentos/detection/yolov5s-face.pt', map_location=torch.device("cpu"))
|
28 |
+
#model_face.to(torch.device("cpu"))
|
29 |
+
|
30 |
+
|
31 |
+
def blur_plates_image(image, plate_blur):
|
32 |
+
|
33 |
+
results_plate = model_plate(image, augment=True)
|
34 |
+
boxes_plate_list = results_plate.pred[0][:, :4].cpu().numpy().tolist()
|
35 |
+
for box in boxes_plate_list:
|
36 |
+
ROI = image[int(box[1]):int(box[3]), int(box[0]):int(box[2])]
|
37 |
+
|
38 |
+
blur_value = (plate_blur * 2 - 1)
|
39 |
+
blur = cv2.GaussianBlur(ROI, (blur_value, blur_value), 20, cv2.BORDER_DEFAULT)
|
40 |
+
# Insert ROI back into image
|
41 |
+
image[int(box[1]):int(box[3]), int(box[0]):int(box[2])] = blur
|
42 |
+
|
43 |
+
return image
|
44 |
+
|
45 |
+
|
46 |
+
def blur_faces_image(image, face_blur):
|
47 |
+
|
48 |
+
results = model_face(np.array(image))
|
49 |
+
#results = model_face(torch.from_numpy(image))
|
50 |
+
|
51 |
+
# parse results
|
52 |
+
boxes_face_list = results.pred[0][:, :4].cpu().numpy().tolist() # x1, y1, x2, y2
|
53 |
+
|
54 |
+
for box in boxes_face_list:
|
55 |
+
p1 = (int(box[0]), int(box[1]))
|
56 |
+
p2 = (int(box[2]), int(box[3]))
|
57 |
+
w = p2[0] - p1[0]
|
58 |
+
h = p2[1] - p1[1]
|
59 |
+
|
60 |
+
circle_center = ((p1[0] + p2[0]) // 2, (p1[1] + p2[1]) // 2)
|
61 |
+
circle_radius = int(math.sqrt(w * w + h * h) // 2)
|
62 |
+
ROI = np.zeros(image.shape, dtype='uint8')
|
63 |
+
cv2.circle(ROI, circle_center, circle_radius, (255, 255, 255), -1)
|
64 |
+
|
65 |
+
blur_value = (face_blur * 2 - 1)
|
66 |
+
blur = cv2.GaussianBlur(image, (blur_value, blur_value), cv2.BORDER_DEFAULT)
|
67 |
+
|
68 |
+
image = np.where(ROI > 0, blur, image)
|
69 |
+
|
70 |
+
return image
|
71 |
+
|
72 |
+
|
73 |
+
def blur(image, face_blur, plate_blur):
|
74 |
+
|
75 |
+
image = blur_plates_image(image, plate_blur)
|
76 |
+
image = blur_faces_image(image, face_blur)
|
77 |
+
|
78 |
+
return image
|
79 |
+
|
80 |
+
|
81 |
+
gr.Interface(
|
82 |
+
fn=blur,
|
83 |
+
inputs=[
|
84 |
+
gr.Image(source="upload", type="numpy", optional=False),
|
85 |
+
gr.Slider(minimum=0, maximum=30, step=1, value=5, label="Face Blur Intensity"),
|
86 |
+
gr.Slider(minimum=0, maximum=30, step=1, value=5, label="Plate Blur Intensity"),
|
87 |
+
],
|
88 |
+
outputs="image",
|
89 |
+
title="Plate Licenses and Faces Blur",
|
90 |
+
enable_queue=False,
|
91 |
+
sidebar="<img src='images/22.png' width='100%'/>",
|
92 |
+
description="This app uses image processing techniques to automatically blur out license plates and faces in photos. Protect the privacy of individuals and comply with regulations by obscuring sensitive information in your images with ease.",
|
93 |
+
examples=[["images/22.png"], ["images/26.jpg"], ["images/23.jpg"], ["images/18.jpg"]],
|
94 |
+
css=".gradio-container {margin-top:80px !important;}"
|
95 |
+
).launch()
|
images/18.jpg
ADDED
images/22.png
ADDED