File size: 3,592 Bytes
d2fc863
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import gradio as gr
import cv2
import numpy as np

# Load Haar Cascade for face detection
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

def process_image(image, operation, canny_threshold1=100, canny_threshold2=200, blur_kernel=5):
    # Convert Gradio image (PIL) to OpenCV format (BGR)
    image = np.array(image)
    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    
    # Initialize output dictionary
    outputs = {}
    
    # Perform selected operation
    if operation == "Grayscale":
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        outputs["Grayscale Image"] = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
    
    elif operation == "Canny Edge Detection":
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        edges = cv2.Canny(gray, canny_threshold1, canny_threshold2)
        outputs["Edges"] = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)
    
    elif operation == "Gaussian Blur":
        if blur_kernel % 2 == 0:
            blur_kernel += 1  # Kernel size must be odd
        blurred = cv2.GaussianBlur(image, (blur_kernel, blur_kernel), 0)
        outputs["Blurred Image"] = blurred
    
    elif operation == "Face Detection":
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
        output_image = image.copy()
        for (x, y, w, h) in faces:
            cv2.rectangle(output_image, (x, y), (x+w, y+h), (0, 255, 0), 2)
        outputs["Faces Detected"] = output_image
    
    # Convert back to RGB for Gradio display
    for key in outputs:
        outputs[key] = cv2.cvtColor(outputs[key], cv2.COLOR_BGR2RGB)
    
    return outputs

# Define Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("# OpenCV Feature Demo")
    gr.Markdown("Upload an image and select an OpenCV operation to apply.")
    
    with gr.Row():
        with gr.Column():
            image_input = gr.Image(label="Upload Image", type="pil")
            operation = gr.Dropdown(
                choices=["Grayscale", "Canny Edge Detection", "Gaussian Blur", "Face Detection"],
                label="Select Operation",
                value="Grayscale"
            )
            canny_threshold1 = gr.Slider(0, 500, value=100, step=10, label="Canny Threshold 1", visible=False)
            canny_threshold2 = gr.Slider(0, 500, value=200, step=10, label="Canny Threshold 2", visible=False)
            blur_kernel = gr.Slider(3, 21, value=5, step=2, label="Blur Kernel Size", visible=False)
            
            # Show/hide sliders based on operation
            def update_sliders(op):
                if op == "Canny Edge Detection":
                    return gr.update(visible=True), gr.update(visible=True), gr.update(visible=False)
                elif op == "Gaussian Blur":
                    return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
                else:
                    return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
            
            operation.change(update_sliders, inputs=operation, outputs=[canny_threshold1, canny_threshold2, blur_kernel])
        
        with gr.Column():
            output = gr.Gallery(label="Processed Image")
    
    submit_button = gr.Button("Process Image")
    submit_button.click(
        fn=process_image,
        inputs=[image_input, operation, canny_threshold1, canny_threshold2, blur_kernel],
        outputs=output
    )

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