Xbits commited on
Commit
c505a87
·
1 Parent(s): 553e5b1

add 3 binarization strategies

Browse files
Files changed (2) hide show
  1. app.py +101 -4
  2. requirements.txt +16 -0
app.py CHANGED
@@ -1,7 +1,104 @@
1
  import gradio as gr
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ import torch
5
+ import spaces
6
+ from PIL import Image
7
 
8
+ # -------------------------------------------------------------
9
+ # 1. SAUVOLA BINARIZATION (Traditional / CPU)
10
+ # -------------------------------------------------------------
11
+ def run_sauvola(image_np, window_size=15, k=0.2, r=128):
12
+ """
13
+ Standard Sauvola local thresholding using OpenCV.
14
+ Formula: T = m * (1 + k * (s / R - 1))
15
+ """
16
+ gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
17
+ window_size = int(window_size) | 1 # Ensure odd window size
18
+
19
+ mean = cv2.blur(gray, (window_size, window_size))
20
+ mean_sq = cv2.blur(gray**2, (window_size, window_size))
21
+ std = np.sqrt(mean_sq - mean**2)
22
+
23
+ threshold = mean * (1.0 + k * (std / r - 1.0))
24
+ binary = np.where(gray > threshold, 255, 0).astype(np.uint8)
25
+ return Image.fromarray(binary)
26
 
27
+ # -------------------------------------------------------------
28
+ # 2. TZEFA-BINARIZATION (HF Zero GPU)
29
+ # -------------------------------------------------------------
30
+ @spaces.GPU
31
+ def run_tzefa(image_pil):
32
+ # Load WARAJA/Tzefa-Binarization (uses sbb_binarization / ResNet+Transformer)
33
+ # Ensure you load the pipeline/model *inside* or cached globally
34
+ from transformers import pipeline
35
+ # Example wrapper depending on how Tzefa hosts their pipeline:
36
+ # pipe = pipeline("image-to-image", model="WARAJA/Tzefa-Binarization")
37
+ # return pipe(image_pil)
38
+ pass
39
+
40
+ # -------------------------------------------------------------
41
+ # 3. TWO-STAGE GAN (opensuh/DocumentBinarization)
42
+ # -------------------------------------------------------------
43
+ @spaces.GPU
44
+ def run_two_stage_gan(image_pil):
45
+ # 1. Preprocess image to fit the 512x512 patches or required shapes
46
+ # 2. Pass through Stage 1 (Enhancement Network)
47
+ # 3. Pass through Stage 2 (Global/Local Binarization Network)
48
+ pass
49
+
50
+ # -------------------------------------------------------------
51
+ # 4. DOCRES GENERALIST MODEL (HF Zero GPU)
52
+ # -------------------------------------------------------------
53
+ @spaces.GPU
54
+ def run_docres(image_pil):
55
+ # DocRes acts as an intelligent outer shell handling unified tasks.
56
+ # We pass it the image along with its DTSPrompt (Dynamic Task-Specific Prompt)
57
+ # tailored specifically for the 'Binarization' task.
58
+ pass
59
+
60
+ def process_image(input_img, algo_choice, sauvola_w, sauvola_k):
61
+ # Convert PIL to Numpy for opencv processing if needed
62
+ img_np = np.array(input_img)
63
+
64
+ if algo_choice == "Sauvola (Traditional)":
65
+ return run_sauvola(img_np, sauvola_w, sauvola_k)
66
+ elif algo_choice == "Tzefa-Binarization":
67
+ return run_tzefa(input_img)
68
+ elif algo_choice == "Two-Stage GAN (opensuh)":
69
+ return run_two_stage_gan(input_img)
70
+ elif algo_choice == "DocRes (Generalist Transformer)":
71
+ return run_docres(input_img)
72
+
73
+ # Building the Interface
74
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
75
+ gr.Markdown("# 📄 Document Image Binarization Benchmarking Suite")
76
+ gr.Markdown("Compare historical document cleaning, GAN-based restoration, and local adaptive thresholding.")
77
+
78
+ with gr.Row():
79
+ with gr.Column(scale=1):
80
+ input_image = gr.Image(type="pil", label="Upload Degraded Document")
81
+
82
+ algo = gr.Dropdown(
83
+ choices=["Sauvola (Traditional)", "Tzefa-Binarization", "Two-Stage GAN (opensuh)", "DocRes (Generalist Transformer)"],
84
+ value="Sauvola (Traditional)",
85
+ label="Select Binarization Engine"
86
+ )
87
+
88
+ # Interactive container for Sauvola params (hides/shows dynamically)
89
+ with gr.Group() as sauvola_params:
90
+ s_w = gr.Slider(minimum=3, maximum=99, step=2, value=15, label="Sauvola Window Size")
91
+ s_k = gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.2, label="Sauvola K Parameter")
92
+
93
+ submit_btn = gr.Button("Binarize Document", variant="primary")
94
+
95
+ with gr.Column(scale=1):
96
+ output_image = gr.Image(type="pil", label="Binarized / Enhanced Output")
97
+
98
+ submit_btn.click(
99
+ fn=process_image,
100
+ inputs=[input_image, algo, s_w, s_k],
101
+ outputs=output_image
102
+ )
103
+
104
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Basic interface and image processing
2
+ gradio==4.44.0
3
+ opencv-python-headless
4
+ numpy
5
+ pillow
6
+
7
+ # Deep Learning core
8
+ torch
9
+ torchvision
10
+ torchaudio --index-url https://download.pytorch.org/whl/cu121
11
+ transformers
12
+ segmentation-models-pytorch
13
+ pytesseract
14
+
15
+ # For Hugging Face Zero instances
16
+ spaces