import cv2 import numpy as np import gradio as gr import matplotlib.pyplot as plt def gradient_magnitude_edge_detection(image_path, threshold): # Read the image img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) # Apply Sobel operator to calculate gradients gradient_x = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3) gradient_y = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3) # Calculate gradient magnitude gradient_magnitude = np.sqrt(gradient_x**2 + gradient_y**2) # Thresholding to obtain edges edges = (gradient_magnitude > threshold) * 255 return edges # Define the Gradio interface iface = gr.Interface( fn=gradient_magnitude_edge_detection, inputs=[ gr.Image(type="filepath", label="Select Image"), gr.Slider(minimum=0, maximum=255, value=50, label="Threshold") ], outputs="image", live=True ) # Launch the Gradio interface iface.launch()