Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,43 @@
|
|
1 |
-
import gradio as gr
|
2 |
import cv2
|
3 |
import numpy as np
|
4 |
-
|
5 |
-
|
6 |
-
def apply_pencil_sketch(image, threshold1=50, threshold2=150, ksize=5):
|
7 |
# Convert to grayscale
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
# Apply the pencil sketch filter to get the outlines
|
27 |
-
result = apply_pencil_sketch(image, threshold1=threshold1, threshold2=threshold2, ksize=ksize)
|
28 |
-
|
29 |
-
# Return the resulting image with pencil outlines
|
30 |
-
return result
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
fn=interactive_sketch,
|
35 |
-
inputs=[
|
36 |
-
gr.Image(type="numpy", label="Upload Image"), # Image upload button
|
37 |
-
gr.Slider(minimum=0, maximum=255, step=1, value=50, label="Canny Edge Threshold 1"), # Slider for low threshold (Canny)
|
38 |
-
gr.Slider(minimum=0, maximum=255, step=1, value=150, label="Canny Edge Threshold 2"), # Slider for high threshold (Canny)
|
39 |
-
gr.Slider(minimum=3, maximum=15, step=2, value=5, label="Gaussian Blur Kernel Size") # Slider for kernel size (blur)
|
40 |
-
],
|
41 |
-
outputs="image", # Display the processed image with pencil outlines
|
42 |
-
live=True # Update image in real-time
|
43 |
-
).launch()
|
|
|
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
+
import gradio as gr
|
4 |
+
def pencil_sketch(image, blur, threshold, stroke_width):
|
|
|
5 |
# Convert to grayscale
|
6 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
7 |
+
# Apply Gaussian blur
|
8 |
+
ksize = blur * 2 + 1
|
9 |
+
blurred = cv2.GaussianBlur(gray, (ksize, ksize), 0)
|
10 |
+
# Detect edges using Canny
|
11 |
+
edges = cv2.Canny(blurred, threshold, threshold * 2)
|
12 |
+
# Dilate edges to simulate thicker pencil strokes
|
13 |
+
kernel = np.ones((stroke_width, stroke_width), np.uint8)
|
14 |
+
edges_dilated = cv2.dilate(edges, kernel, iterations=1)
|
15 |
+
# Invert edges to create a sketch effect
|
16 |
+
sketch = cv2.bitwise_not(edges_dilated)
|
17 |
+
# Convert back to BGR
|
18 |
+
sketch_bgr = cv2.cvtColor(sketch, cv2.COLOR_GRAY2BGR)
|
19 |
+
return sketch_bgr
|
20 |
+
|
21 |
+
|
22 |
+
with gr.Blocks() as demo:
|
23 |
+
with gr.Row():
|
24 |
+
image_input = gr.Image(source='upload', type='numpy', label="Upload Image")
|
25 |
+
image_output = gr.Image(type='numpy', label="Pencil Sketch Output")
|
26 |
+
with gr.Row():
|
27 |
+
blur_slider = gr.Slider(1, 10, step=1, label="Blur", value=1)
|
28 |
+
threshold_slider = gr.Slider(50, 255, step=1, label="Threshold", value=100)
|
29 |
+
stroke_width_slider = gr.Slider(1, 10, step=1, label="Stroke Width", value=1)
|
30 |
+
|
31 |
+
def update(image, blur, threshold, stroke_width):
|
32 |
+
if image is None:
|
33 |
+
return None
|
34 |
+
return pencil_sketch(image, blur, threshold, stroke_width)
|
35 |
|
36 |
+
inputs = [image_input, blur_slider, threshold_slider, stroke_width_slider]
|
37 |
+
image_input.change(fn=update, inputs=inputs, outputs=image_output)
|
38 |
+
blur_slider.change(fn=update, inputs=inputs, outputs=image_output)
|
39 |
+
threshold_slider.change(fn=update, inputs=inputs, outputs=image_output)
|
40 |
+
stroke_width_slider.change(fn=update, inputs=inputs, outputs=image_output)
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
if __name__ == "__main__":
|
43 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|