dannyroxas commited on
Commit
0870a5a
·
verified ·
1 Parent(s): d6b0126

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -0
app.py CHANGED
@@ -1,3 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  with gr.Blocks(title="Privacy Protection") as demo:
2
  gr.Markdown("# Privacy Protection")
3
  gr.Markdown(
 
1
+ import gradio as gr
2
+ import os
3
+ import cv2
4
+ import numpy as np
5
+ import torch
6
+ from PIL import Image
7
+ from ultralytics import YOLO
8
+
9
+ # Set environment variables for temporary directories
10
+ os.environ["MPLCONFIGDIR"] = "/tmp/matplotlib"
11
+ os.environ["YOLO_CONFIG_DIR"] = "/tmp/ultralytics"
12
+
13
+ # Load model
14
+ MODEL_PATH = os.path.join(os.path.dirname(__file__), "models", "unified_detector.pt")
15
+
16
+ try:
17
+ print(f"CUDA Available: {torch.cuda.is_available()}")
18
+ model = YOLO(MODEL_PATH)
19
+ print("Model loaded successfully")
20
+ except Exception as e:
21
+ print(f"Error loading model: {e}")
22
+ model = None
23
+
24
+ def detect_and_blur(image):
25
+ """Process and blur sensitive content in image with blur for better protection"""
26
+ # Handle different input types
27
+ if isinstance(image, np.ndarray):
28
+ img_array = image
29
+ elif isinstance(image, str):
30
+ img_array = np.array(Image.open(image).convert('RGB'))
31
+ else:
32
+ return None, "Invalid input type"
33
+
34
+ # Process the image
35
+ result_img = img_array.copy()
36
+ detections = {'faces': 0, 'plates': 0, 'text': 0} # Added text tracking
37
+
38
+ try:
39
+ # Run inference with model
40
+ results = model.predict(img_array, conf=0.45) # Slightly lower threshold for better recall
41
+ for r in results:
42
+ for box in r.boxes:
43
+ x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
44
+ cls_id = int(box.cls[0])
45
+ conf = float(box.conf[0])
46
+
47
+ # Log detection details for debugging
48
+ class_name = ["license_plate", "face", "text"][cls_id] if cls_id < 3 else f"unknown({cls_id})"
49
+ print(f"Detected {class_name} with confidence {conf:.2f} at coordinates [{x1}, {y1}, {x2}, {y2}]")
50
+
51
+ # Ensure coordinates are within image bounds
52
+ x1, y1 = max(0, x1), max(0, y1)
53
+ x2, y2 = min(img_array.shape[1], x2), min(img_array.shape[0], y2)
54
+
55
+ if x2 <= x1 or y2 <= y1:
56
+ continue
57
+
58
+ # Apply stronger blur based on class
59
+ region = result_img[y1:y2, x1:x2]
60
+
61
+ # Increase kernel size for more extreme blur
62
+ # Base kernel size on region dimensions but make it larger
63
+ dim_min = min(x2-x1, y2-y1)
64
+ kernel_size = min(99, max(25, dim_min // 2 * 2 + 1)) # Ensure odd number, max 99
65
+
66
+ # Higher sigma for more extreme blur
67
+ sigma = 70 if cls_id == 1 else 85 # Higher sigma for plates and text
68
+
69
+ if kernel_size >= 3:
70
+ # Apply more extreme blur with higher sigma value
71
+ blurred = cv2.GaussianBlur(region, (kernel_size, kernel_size), sigma)
72
+ result_img[y1:y2, x1:x2] = blurred
73
+
74
+ # Update detection counters for all three classes
75
+ if cls_id == 0:
76
+ detections['plates'] += 1
77
+ elif cls_id == 1:
78
+ detections['faces'] += 1
79
+ elif cls_id == 2:
80
+ detections['text'] += 1
81
+ except Exception as e:
82
+ print(f"Error during detection: {str(e)}")
83
+ return img_array, f"Error: {str(e)}"
84
+
85
+ message = f"Detected and blurred {detections['faces']} faces, {detections['plates']} license plates, and {detections['text']} text regions"
86
+ return result_img, message
87
+
88
+ # Use Blocks for better layout control
89
  with gr.Blocks(title="Privacy Protection") as demo:
90
  gr.Markdown("# Privacy Protection")
91
  gr.Markdown(