File size: 2,758 Bytes
d518755
b82d995
 
432cc96
685757b
4750f0e
2a6dc89
685757b
 
 
 
2a6dc89
 
 
685757b
 
 
 
 
 
 
 
 
 
 
 
 
2a6dc89
 
685757b
 
2a6dc89
 
685757b
432cc96
 
 
685757b
432cc96
2a6dc89
 
 
d518755
2a6dc89
 
 
 
685757b
 
 
 
432cc96
 
 
2a6dc89
685757b
2a6dc89
432cc96
685757b
432cc96
b82d995
685757b
432cc96
685757b
432cc96
 
b82d995
2a6dc89
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import gradio as gr
import cv2
import numpy as np

def process_image(image, blur, threshold, stroke_width, use_sobel, use_laplacian, contrast):
    # Convert to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    # Adjust contrast
    gray = cv2.convertScaleAbs(gray, alpha=contrast, beta=0)  # Alpha controls contrast

    # Apply Gaussian blur
    ksize = blur * 2 + 1
    blurred = cv2.GaussianBlur(gray, (ksize, ksize), 0)

    # Apply Sobel or Laplacian
    if use_sobel:
        edges = cv2.Sobel(blurred, cv2.CV_64F, 1, 1, ksize=5)
        edges = cv2.convertScaleAbs(edges)
    elif use_laplacian:
        edges = cv2.Laplacian(blurred, cv2.CV_64F)
        edges = cv2.convertScaleAbs(edges)
    else:
        # Default to Canny if no additional edge enhancement is selected
        edges = cv2.Canny(blurred, threshold, threshold * 2)

    # Dilate edges for thicker strokes
    kernel = np.ones((stroke_width, stroke_width), np.uint8)
    edges_dilated = cv2.dilate(edges, kernel, iterations=1)

    # Invert for sketch effect
    sketch = cv2.bitwise_not(edges_dilated)
    sketch_bgr = cv2.cvtColor(sketch, cv2.COLOR_GRAY2BGR)

    # Apply bilateral filter for cartoon effect
    bilateral = cv2.bilateralFilter(image, 9, 75, 75)
    cartoon = cv2.bitwise_and(bilateral, bilateral, mask=sketch)

    return sketch_bgr, cartoon

with gr.Blocks() as demo:
    with gr.Row():
        image_input = gr.Image(type='numpy', label="Upload Image")
    with gr.Row():
        blur_slider = gr.Slider(1, 10, step=1, label="Blur", value=1)
        threshold_slider = gr.Slider(50, 255, step=1, label="Threshold", value=100)
        stroke_width_slider = gr.Slider(1, 10, step=1, label="Stroke Width", value=1)
        contrast_slider = gr.Slider(0.5, 3.0, step=0.1, label="Contrast", value=1.0)
        sobel_checkbox = gr.Checkbox(label="Use Sobel Filter", value=False)
        laplacian_checkbox = gr.Checkbox(label="Use Laplacian Filter", value=False)
    
    with gr.Row():
        sketch_output = gr.Image(type='numpy', label="Pencil Sketch")
        cartoon_output = gr.Image(type='numpy', label="Cartoonized Image")

    def update(image, blur, threshold, stroke_width, contrast, use_sobel, use_laplacian):
        if image is None:
            return None, None
        sketch, cartoon = process_image(image, blur, threshold, stroke_width, use_sobel, use_laplacian, contrast)
        return sketch, cartoon

    inputs = [image_input, blur_slider, threshold_slider, stroke_width_slider, contrast_slider, sobel_checkbox, laplacian_checkbox]
    outputs = [sketch_output, cartoon_output]

    for component in inputs:
        component.change(fn=update, inputs=inputs, outputs=outputs)

if __name__ == "__main__":
    demo.launch()