import cv2 import numpy as np import gradio as gr from PIL import Image as PILImage def threshold_segmentation_gradio(image, threshold_value): """ Applies threshold segmentation and displays results in a Gradio interface. Args: image_path: Path to the image file. threshold_value: Threshold value for binarization. Returns: A tuple of: - Thresholded image: Gradio Image component. - Bright pixel count: Gradio Label component. - Dim pixel count: Gradio Label component. - Total pixel count: Gradio Label component. """ # Convert PIL image to grayscale OpenCV format (assuming RGB format) image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY) # Apply thresholding _, thresholded_image = cv2.threshold(image, threshold_value, 255, cv2.THRESH_BINARY) # Count bright and dim pixels bright_count = cv2.countNonZero(thresholded_image) dim_count = thresholded_image.size - bright_count total_count = thresholded_image.size return thresholded_image,bright_count, dim_count, total_count if __name__ == "__main__": # Launch the Gradio interface inputs = [gr.Image(type="pil"), gr.Slider(0, 255)] outputs = [gr.Image(label="Thresholded Image"),gr.Label(label="Bright Pixels"), gr.Label(label="Dim Pixels"), gr.Label(label="Total Pixels")] # Launch the Gradio interface interface = gr.Interface(fn=threshold_segmentation_gradio, inputs=inputs, outputs=outputs,css="footer { visibility: hidden }") interface.launch()