GandalfTheBlack commited on
Commit
2a6dc89
·
verified ·
1 Parent(s): 4750f0e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -38
app.py CHANGED
@@ -1,43 +1,43 @@
1
- import gradio as gr
2
  import cv2
3
  import numpy as np
4
-
5
- # Function to apply pencil sketch effect with adjustable outline intensity
6
- def apply_pencil_sketch(image, threshold1=50, threshold2=150, ksize=5):
7
  # Convert to grayscale
8
- gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
9
-
10
- # Apply Canny edge detection to get the pencil outlines
11
- edges = cv2.Canny(gray_image, threshold1, threshold2)
12
-
13
- # Optionally, apply a Gaussian blur to smooth the edges (to control the pencil stroke effect)
14
- blurred_edges = cv2.GaussianBlur(edges, (ksize, ksize), 0)
15
-
16
- # Invert the edges (for a black background and white strokes)
17
- inverted_edges = cv2.bitwise_not(blurred_edges)
18
-
19
- return inverted_edges
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- # Gradio interface to upload an image and apply pencil sketch effect with real-time outline control
22
- def interactive_sketch(image, threshold1, threshold2, ksize):
23
- # Convert the uploaded image to a NumPy array
24
- image = np.array(image)
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
- # Create Gradio interface with updated syntax
33
- gr.Interface(
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()