Update app.py
Browse files
app.py
CHANGED
|
@@ -1,93 +1,93 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import numpy as np
|
| 4 |
-
from ultralytics import YOLO
|
| 5 |
-
import tempfile
|
| 6 |
-
import os
|
| 7 |
-
|
| 8 |
-
# Load YOLOv8 face and plate detection models
|
| 9 |
-
face_model = YOLO("yolov8n-face.pt")
|
| 10 |
-
plate_model = YOLO("
|
| 11 |
-
|
| 12 |
-
def pixelate(image, boxes, factor=10):
|
| 13 |
-
for box in boxes:
|
| 14 |
-
x1, y1, x2, y2 = map(int, box[:4])
|
| 15 |
-
region = image[y1:y2, x1:x2]
|
| 16 |
-
h, w = region.shape[:2]
|
| 17 |
-
if h == 0 or w == 0:
|
| 18 |
-
continue
|
| 19 |
-
temp = cv2.resize(region, (factor, factor), interpolation=cv2.INTER_LINEAR)
|
| 20 |
-
pixelated = cv2.resize(temp, (w, h), interpolation=cv2.INTER_NEAREST)
|
| 21 |
-
image[y1:y2, x1:x2] = pixelated
|
| 22 |
-
return image
|
| 23 |
-
|
| 24 |
-
def detect_and_redact(img, redact_faces, redact_plates):
|
| 25 |
-
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 26 |
-
|
| 27 |
-
if redact_faces:
|
| 28 |
-
face_results = face_model.predict(img, conf=0.3, verbose=False)
|
| 29 |
-
face_boxes = face_results[0].boxes.xyxy.cpu().numpy() if face_results else []
|
| 30 |
-
img = pixelate(img, face_boxes)
|
| 31 |
-
|
| 32 |
-
if redact_plates:
|
| 33 |
-
plate_results = plate_model.predict(img, conf=0.3, verbose=False)
|
| 34 |
-
plate_boxes = plate_results[0].boxes.xyxy.cpu().numpy() if plate_results else []
|
| 35 |
-
img = pixelate(img, plate_boxes)
|
| 36 |
-
|
| 37 |
-
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 38 |
-
|
| 39 |
-
def process_image(input_image, mode):
|
| 40 |
-
redact_faces = mode in ["Faces Only", "Both"]
|
| 41 |
-
redact_plates = mode in ["Plates Only", "Both"]
|
| 42 |
-
return detect_and_redact(input_image, redact_faces, redact_plates)
|
| 43 |
-
|
| 44 |
-
def process_video(video_file, mode):
|
| 45 |
-
redact_faces = mode in ["Faces Only", "Both"]
|
| 46 |
-
redact_plates = mode in ["Plates Only", "Both"]
|
| 47 |
-
|
| 48 |
-
cap = cv2.VideoCapture(video_file)
|
| 49 |
-
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 50 |
-
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 51 |
-
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 52 |
-
|
| 53 |
-
# Temporary output file
|
| 54 |
-
temp_video = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
|
| 55 |
-
out = cv2.VideoWriter(temp_video.name, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
|
| 56 |
-
|
| 57 |
-
while cap.isOpened():
|
| 58 |
-
ret, frame = cap.read()
|
| 59 |
-
if not ret:
|
| 60 |
-
break
|
| 61 |
-
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 62 |
-
redacted = detect_and_redact(frame_rgb, redact_faces, redact_plates)
|
| 63 |
-
out.write(cv2.cvtColor(redacted, cv2.COLOR_RGB2BGR))
|
| 64 |
-
|
| 65 |
-
cap.release()
|
| 66 |
-
out.release()
|
| 67 |
-
return temp_video.name
|
| 68 |
-
|
| 69 |
-
# Gradio Interface
|
| 70 |
-
with gr.Blocks(title="Visual Privacy Filter") as demo:
|
| 71 |
-
gr.Markdown("## 🔒 Visual Privacy Filter (Faces & License Plates)")
|
| 72 |
-
gr.Markdown("Upload an image or video and choose what to redact.")
|
| 73 |
-
|
| 74 |
-
mode = gr.Radio(
|
| 75 |
-
choices=["Faces Only", "Plates Only", "Both"],
|
| 76 |
-
value="Both",
|
| 77 |
-
label="Redaction Mode"
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
with gr.Tabs():
|
| 81 |
-
with gr.Tab("📷 Image"):
|
| 82 |
-
img_input = gr.Image(type="numpy", label="Upload Image")
|
| 83 |
-
img_output = gr.Image(type="numpy", label="Redacted Output")
|
| 84 |
-
img_btn = gr.Button("Redact Image")
|
| 85 |
-
img_btn.click(fn=process_image, inputs=[img_input, mode], outputs=img_output)
|
| 86 |
-
|
| 87 |
-
with gr.Tab("🎥 Video"):
|
| 88 |
-
vid_input = gr.Video(label="Upload Video")
|
| 89 |
-
vid_output = gr.Video(label="Redacted Video")
|
| 90 |
-
vid_btn = gr.Button("Redact Video")
|
| 91 |
-
vid_btn.click(fn=process_video, inputs=[vid_input, mode], outputs=vid_output)
|
| 92 |
-
|
| 93 |
-
demo.launch()
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Load YOLOv8 face and plate detection models
|
| 9 |
+
face_model = YOLO("yolov8n-face.pt")
|
| 10 |
+
plate_model = YOLO("license_plate_detector.pt")
|
| 11 |
+
|
| 12 |
+
def pixelate(image, boxes, factor=10):
|
| 13 |
+
for box in boxes:
|
| 14 |
+
x1, y1, x2, y2 = map(int, box[:4])
|
| 15 |
+
region = image[y1:y2, x1:x2]
|
| 16 |
+
h, w = region.shape[:2]
|
| 17 |
+
if h == 0 or w == 0:
|
| 18 |
+
continue
|
| 19 |
+
temp = cv2.resize(region, (factor, factor), interpolation=cv2.INTER_LINEAR)
|
| 20 |
+
pixelated = cv2.resize(temp, (w, h), interpolation=cv2.INTER_NEAREST)
|
| 21 |
+
image[y1:y2, x1:x2] = pixelated
|
| 22 |
+
return image
|
| 23 |
+
|
| 24 |
+
def detect_and_redact(img, redact_faces, redact_plates):
|
| 25 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 26 |
+
|
| 27 |
+
if redact_faces:
|
| 28 |
+
face_results = face_model.predict(img, conf=0.3, verbose=False)
|
| 29 |
+
face_boxes = face_results[0].boxes.xyxy.cpu().numpy() if face_results else []
|
| 30 |
+
img = pixelate(img, face_boxes)
|
| 31 |
+
|
| 32 |
+
if redact_plates:
|
| 33 |
+
plate_results = plate_model.predict(img, conf=0.3, verbose=False)
|
| 34 |
+
plate_boxes = plate_results[0].boxes.xyxy.cpu().numpy() if plate_results else []
|
| 35 |
+
img = pixelate(img, plate_boxes)
|
| 36 |
+
|
| 37 |
+
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 38 |
+
|
| 39 |
+
def process_image(input_image, mode):
|
| 40 |
+
redact_faces = mode in ["Faces Only", "Both"]
|
| 41 |
+
redact_plates = mode in ["Plates Only", "Both"]
|
| 42 |
+
return detect_and_redact(input_image, redact_faces, redact_plates)
|
| 43 |
+
|
| 44 |
+
def process_video(video_file, mode):
|
| 45 |
+
redact_faces = mode in ["Faces Only", "Both"]
|
| 46 |
+
redact_plates = mode in ["Plates Only", "Both"]
|
| 47 |
+
|
| 48 |
+
cap = cv2.VideoCapture(video_file)
|
| 49 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 50 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 51 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 52 |
+
|
| 53 |
+
# Temporary output file
|
| 54 |
+
temp_video = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
|
| 55 |
+
out = cv2.VideoWriter(temp_video.name, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
|
| 56 |
+
|
| 57 |
+
while cap.isOpened():
|
| 58 |
+
ret, frame = cap.read()
|
| 59 |
+
if not ret:
|
| 60 |
+
break
|
| 61 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 62 |
+
redacted = detect_and_redact(frame_rgb, redact_faces, redact_plates)
|
| 63 |
+
out.write(cv2.cvtColor(redacted, cv2.COLOR_RGB2BGR))
|
| 64 |
+
|
| 65 |
+
cap.release()
|
| 66 |
+
out.release()
|
| 67 |
+
return temp_video.name
|
| 68 |
+
|
| 69 |
+
# Gradio Interface
|
| 70 |
+
with gr.Blocks(title="Visual Privacy Filter") as demo:
|
| 71 |
+
gr.Markdown("## 🔒 Visual Privacy Filter (Faces & License Plates)")
|
| 72 |
+
gr.Markdown("Upload an image or video and choose what to redact.")
|
| 73 |
+
|
| 74 |
+
mode = gr.Radio(
|
| 75 |
+
choices=["Faces Only", "Plates Only", "Both"],
|
| 76 |
+
value="Both",
|
| 77 |
+
label="Redaction Mode"
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
with gr.Tabs():
|
| 81 |
+
with gr.Tab("📷 Image"):
|
| 82 |
+
img_input = gr.Image(type="numpy", label="Upload Image")
|
| 83 |
+
img_output = gr.Image(type="numpy", label="Redacted Output")
|
| 84 |
+
img_btn = gr.Button("Redact Image")
|
| 85 |
+
img_btn.click(fn=process_image, inputs=[img_input, mode], outputs=img_output)
|
| 86 |
+
|
| 87 |
+
with gr.Tab("🎥 Video"):
|
| 88 |
+
vid_input = gr.Video(label="Upload Video")
|
| 89 |
+
vid_output = gr.Video(label="Redacted Video")
|
| 90 |
+
vid_btn = gr.Button("Redact Video")
|
| 91 |
+
vid_btn.click(fn=process_video, inputs=[vid_input, mode], outputs=vid_output)
|
| 92 |
+
|
| 93 |
+
demo.launch()
|