martinnecp/document_processing
0
1import gradio as gr2import os3import tempfile4import cv25import numpy as np6 7# function that calculate lower boun level 8# it was defined experimentaly and means when M as a mean walue of gryscale is 180 then lower bound walue is 1309# when mean walue of gry scale pixels is 90 then lower baund value is 7010def calculate_LowerBound(M):11 # Define the slope (m) and intercept (b)12 m = 2 / 313 b = 1014 15 # Calculate B based on M16 B = m * M + b17 return B18 19# Function to process the image by dividing it into blocks and returns 25 blocks of image20def process_image_in_blocks(image, rows=5, cols=5):21 blocks = []22 height, width = image.shape[:2]23 24 # Calculate the size of each block25 block_height = height // rows26 block_width = width // cols27 28 # Create an empty array to store the processed image29 processed_image = np.zeros_like(image)30 31 for i in range(rows):32 for j in range(cols):33 # Extract each block from the image34 y_start = i * block_height35 y_end = (i + 1) * block_height if i != rows - 1 else height36 x_start = j * block_width37 x_end = (j + 1) * block_width if j != cols - 1 else width38 39 block = image[y_start:y_end, x_start:x_end]40 blocks.append(block)41 #cv2.imwrite(f'block.png{i},{j}.png', block) ## prepare to save image to see it42 return blocks43 44def change_image(image,lower_bound = 100):45 46 # Assuming 'image' is your RGB image (shape = (2777, 1903, 3))47 # For example, using a dummy image for demonstration48 # image = np.random.randint(0, 256, (2777, 1903, 3), dtype=np.uint8)49 50 # Define the range of values for the R, G, or B channels51 upper_bound = 25552 53 # Create a condition where any channel (R, G, or B) is in the range [50, 255]54 condition = (image[:, :, 0] >= lower_bound) & (image[:, :, 0] <= upper_bound) | \55 (image[:, :, 1] >= lower_bound) & (image[:, :, 1] <= upper_bound) | \56 (image[:, :, 2] >= lower_bound) & (image[:, :, 2] <= upper_bound)57 58 # Set the RGB value to white where the condition is true59 image[condition] = [255, 255, 255]60 61 # Now, 'image' has white pixels where any of the R, G, or B channels were in the range [50, 255]62 return image63 64 65def process_image(file):66 image = cv2.imread(file.name) # file is a tempfile._TemporaryFileWrapper67 68 block_images = process_image_in_blocks(image=image) # vrati roylo6ene obraykz na bloky69 70 ch_blocks = [] # will be list of changed blocks 71 for part_image in block_images:72 ll = calculate_LowerBound(np.mean(part_image)) # calculate lower bound od picel which pixels will be removed from image is obtain experimentaly73 changed = change_image(part_image,lower_bound=ll) # remove pixel of lower boud 74 ch_blocks.append(changed) # create new list 75 76 # Assuming 'ch_blocks' is a list of 25 blocks (e.g., 5x5 grid)77 # Concatenate the blocks horizontally in each row, and then concatenate the rows vertically78 79 # List to store the rows80 rows = []81 # Loop to concatenate blocks in each row82 for i in range(0, 25, 5): # Step size of 5 to group the blocks into rows83 row = np.concatenate(ch_blocks[i:i+5], axis=1) # Concatenate blocks horizontally (axis=1)84 rows.append(row)85 # Concatenate the rows vertically to form the final image86 cc = np.concatenate(rows, axis=0) # Concatenate along the vertical axis (axis=0) 87 88 image = cc # test to be image OK89 90 temp_dir = tempfile.mkdtemp()91 file_path = os.path.join(temp_dir, "bw_image.png")92 cv2.imwrite(file_path, image)93 return image, file_path94 95with gr.Blocks() as demo:96 with gr.Tabs():97 with gr.Tab("Home"):98 gr.Markdown("## Welcome to the Home Page")99 with gr.Tab("Settings"):100 gr.Markdown("## Adjust your settings here")101 with gr.Tab("My getting started"):102 gr.Markdown("## Upload an image, adjust brightness, and download the result")103 104 with gr.Row():105 cv_adjust = gr.Button('cv', variant="primary")106 107 with gr.Row():108 cv_file_input = gr.File(label="Upload an image")109 cv_image_output = gr.Image(type="numpy", width=300, height=300)110 fileoutput = gr.File(label="Dovnlosad processed file")111 112 cv_adjust.click(113 fn = process_image,114 inputs = cv_file_input,115 outputs = [cv_image_output, fileoutput]116 )117 118if __name__ == "__main__":119 demo.launch()