File size: 1,559 Bytes
48caa33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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


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()