Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
# Load Haar Cascade for face detection
|
6 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
7 |
+
|
8 |
+
def process_image(image, operation, canny_threshold1=100, canny_threshold2=200, blur_kernel=5):
|
9 |
+
# Convert Gradio image (PIL) to OpenCV format (BGR)
|
10 |
+
image = np.array(image)
|
11 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
12 |
+
|
13 |
+
# Initialize output dictionary
|
14 |
+
outputs = {}
|
15 |
+
|
16 |
+
# Perform selected operation
|
17 |
+
if operation == "Grayscale":
|
18 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
19 |
+
outputs["Grayscale Image"] = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
|
20 |
+
|
21 |
+
elif operation == "Canny Edge Detection":
|
22 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
23 |
+
edges = cv2.Canny(gray, canny_threshold1, canny_threshold2)
|
24 |
+
outputs["Edges"] = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)
|
25 |
+
|
26 |
+
elif operation == "Gaussian Blur":
|
27 |
+
if blur_kernel % 2 == 0:
|
28 |
+
blur_kernel += 1 # Kernel size must be odd
|
29 |
+
blurred = cv2.GaussianBlur(image, (blur_kernel, blur_kernel), 0)
|
30 |
+
outputs["Blurred Image"] = blurred
|
31 |
+
|
32 |
+
elif operation == "Face Detection":
|
33 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
34 |
+
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
35 |
+
output_image = image.copy()
|
36 |
+
for (x, y, w, h) in faces:
|
37 |
+
cv2.rectangle(output_image, (x, y), (x+w, y+h), (0, 255, 0), 2)
|
38 |
+
outputs["Faces Detected"] = output_image
|
39 |
+
|
40 |
+
# Convert back to RGB for Gradio display
|
41 |
+
for key in outputs:
|
42 |
+
outputs[key] = cv2.cvtColor(outputs[key], cv2.COLOR_BGR2RGB)
|
43 |
+
|
44 |
+
return outputs
|
45 |
+
|
46 |
+
# Define Gradio interface
|
47 |
+
with gr.Blocks() as demo:
|
48 |
+
gr.Markdown("# OpenCV Feature Demo")
|
49 |
+
gr.Markdown("Upload an image and select an OpenCV operation to apply.")
|
50 |
+
|
51 |
+
with gr.Row():
|
52 |
+
with gr.Column():
|
53 |
+
image_input = gr.Image(label="Upload Image", type="pil")
|
54 |
+
operation = gr.Dropdown(
|
55 |
+
choices=["Grayscale", "Canny Edge Detection", "Gaussian Blur", "Face Detection"],
|
56 |
+
label="Select Operation",
|
57 |
+
value="Grayscale"
|
58 |
+
)
|
59 |
+
canny_threshold1 = gr.Slider(0, 500, value=100, step=10, label="Canny Threshold 1", visible=False)
|
60 |
+
canny_threshold2 = gr.Slider(0, 500, value=200, step=10, label="Canny Threshold 2", visible=False)
|
61 |
+
blur_kernel = gr.Slider(3, 21, value=5, step=2, label="Blur Kernel Size", visible=False)
|
62 |
+
|
63 |
+
# Show/hide sliders based on operation
|
64 |
+
def update_sliders(op):
|
65 |
+
if op == "Canny Edge Detection":
|
66 |
+
return gr.update(visible=True), gr.update(visible=True), gr.update(visible=False)
|
67 |
+
elif op == "Gaussian Blur":
|
68 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
|
69 |
+
else:
|
70 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
71 |
+
|
72 |
+
operation.change(update_sliders, inputs=operation, outputs=[canny_threshold1, canny_threshold2, blur_kernel])
|
73 |
+
|
74 |
+
with gr.Column():
|
75 |
+
output = gr.Gallery(label="Processed Image")
|
76 |
+
|
77 |
+
submit_button = gr.Button("Process Image")
|
78 |
+
submit_button.click(
|
79 |
+
fn=process_image,
|
80 |
+
inputs=[image_input, operation, canny_threshold1, canny_threshold2, blur_kernel],
|
81 |
+
outputs=output
|
82 |
+
)
|
83 |
+
|
84 |
+
if __name__ == "__main__":
|
85 |
+
demo.launch()
|