Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,124 +1,50 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 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 |
-
#
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 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 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
"Upload an image to automatically blur faces, license plates, and text for privacy protection.\n"
|
| 92 |
-
"This model uses YOLOv8 to detect and apply blur to sensitive content."
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
-
# Interface with flagging disabled
|
| 96 |
-
interface = gr.Interface(
|
| 97 |
-
fn=detect_and_blur,
|
| 98 |
-
inputs=gr.Image(),
|
| 99 |
-
outputs=[
|
| 100 |
-
gr.Image(label="Processed Image"),
|
| 101 |
-
gr.Textbox(label="Detection Results")
|
| 102 |
-
],
|
| 103 |
-
title=title,
|
| 104 |
-
description=description,
|
| 105 |
-
examples=[
|
| 106 |
-
['examples/example0.jpg'],
|
| 107 |
-
['examples/example1.jpg'],
|
| 108 |
-
['examples/example2.jpg'],
|
| 109 |
-
['examples/example3.jpg'],
|
| 110 |
-
['examples/example4.jpg'],
|
| 111 |
-
['examples/example5.jpg'],
|
| 112 |
-
['examples/example6.jpg'],
|
| 113 |
-
['examples/example7.jpg'],
|
| 114 |
-
],
|
| 115 |
-
allow_flagging="never", # Disable flagging
|
| 116 |
-
analytics_enabled=False # Disable analytics
|
| 117 |
-
)
|
| 118 |
|
| 119 |
# Launch the app with the correct server parameters
|
| 120 |
if __name__ == "__main__":
|
| 121 |
-
|
| 122 |
server_name="0.0.0.0",
|
| 123 |
server_port=7860,
|
| 124 |
share=False
|
|
|
|
| 1 |
+
with gr.Blocks(title="Privacy Protection") as demo:
|
| 2 |
+
gr.Markdown("# Privacy Protection")
|
| 3 |
+
gr.Markdown(
|
| 4 |
+
"Upload an image to automatically blur faces, license plates, and text for privacy protection.\n"
|
| 5 |
+
"This model uses YOLOv8 to detect and apply blur to sensitive content."
|
| 6 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Place examples at the top
|
| 9 |
+
with gr.Row():
|
| 10 |
+
example_images = gr.Examples(
|
| 11 |
+
examples=[
|
| 12 |
+
['examples/example0.jpg'],
|
| 13 |
+
['examples/example1.jpg'],
|
| 14 |
+
['examples/example2.jpg'],
|
| 15 |
+
['examples/example3.jpg'],
|
| 16 |
+
['examples/example4.jpg'],
|
| 17 |
+
['examples/example5.jpg'],
|
| 18 |
+
['examples/example6.jpg'],
|
| 19 |
+
['examples/example7.jpg'],
|
| 20 |
+
],
|
| 21 |
+
inputs="image_input",
|
| 22 |
+
label="Example Images",
|
| 23 |
+
)
|
| 24 |
|
| 25 |
+
# Split the interface into two columns
|
| 26 |
+
with gr.Row():
|
| 27 |
+
# Left column for input
|
| 28 |
+
with gr.Column(scale=1):
|
| 29 |
+
image_input = gr.Image(label="Input Image")
|
| 30 |
+
process_btn = gr.Button("Process Image", variant="primary")
|
| 31 |
+
|
| 32 |
+
# Right column for output
|
| 33 |
+
with gr.Column(scale=1):
|
| 34 |
+
# Set a fixed height for the output image
|
| 35 |
+
image_output = gr.Image(label="Processed Image", height=400)
|
| 36 |
+
output_text = gr.Textbox(label="Detection Results")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# Set up the processing function
|
| 39 |
+
process_btn.click(
|
| 40 |
+
fn=detect_and_blur,
|
| 41 |
+
inputs=[image_input],
|
| 42 |
+
outputs=[image_output, output_text]
|
| 43 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
# Launch the app with the correct server parameters
|
| 46 |
if __name__ == "__main__":
|
| 47 |
+
demo.launch(
|
| 48 |
server_name="0.0.0.0",
|
| 49 |
server_port=7860,
|
| 50 |
share=False
|