Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -32,11 +32,12 @@ def find_result_image(path):
|
|
| 32 |
print(f"Error opening result image {filename}: {e}")
|
| 33 |
return None
|
| 34 |
|
| 35 |
-
# --- 2. Main Processing Function (UPDATED) ---
|
| 36 |
@spaces.GPU
|
| 37 |
def process_ocr_task(image, model_size, task_type, ref_text):
|
| 38 |
"""
|
| 39 |
Processes an image with DeepSeek-OCR for all supported tasks.
|
|
|
|
| 40 |
"""
|
| 41 |
if image is None:
|
| 42 |
return "Please upload an image first.", None
|
|
@@ -89,48 +90,45 @@ def process_ocr_task(image, model_size, task_type, ref_text):
|
|
| 89 |
|
| 90 |
print(f"====\nπ Text Result: {text_result}\n====")
|
| 91 |
|
| 92 |
-
# --- NEW:
|
| 93 |
result_image_pil = None
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
match = pattern.search(text_result)
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
# Extract coordinates as integers
|
| 103 |
coords_norm = [int(c) for c in match.groups()]
|
| 104 |
x1_norm, y1_norm, x2_norm, y2_norm = coords_norm
|
| 105 |
|
| 106 |
-
# Get the original image's dimensions
|
| 107 |
-
w, h = image.size
|
| 108 |
-
|
| 109 |
# Scale the normalized coordinates (from 1000x1000 space) to the image's actual size
|
| 110 |
x1 = int(x1_norm / 1000 * w)
|
| 111 |
y1 = int(y1_norm / 1000 * h)
|
| 112 |
x2 = int(x2_norm / 1000 * w)
|
| 113 |
y2 = int(y2_norm / 1000 * h)
|
| 114 |
|
| 115 |
-
# Create a copy of the original image to draw on
|
| 116 |
-
image_with_bbox = image.copy()
|
| 117 |
-
draw = ImageDraw.Draw(image_with_bbox)
|
| 118 |
-
|
| 119 |
# Draw the rectangle with a red outline, 3 pixels wide
|
| 120 |
draw.rectangle([x1, y1, x2, y2], outline="red", width=3)
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
else:
|
| 124 |
-
print("β οΈ Could not parse bbox from text. Falling back to searching for a result image.")
|
| 125 |
-
result_image_pil = find_result_image(output_path)
|
| 126 |
else:
|
| 127 |
-
#
|
|
|
|
| 128 |
result_image_pil = find_result_image(output_path)
|
| 129 |
|
| 130 |
return text_result, result_image_pil
|
| 131 |
|
| 132 |
|
| 133 |
-
# --- 3. Build the Gradio Interface ---
|
| 134 |
with gr.Blocks(title="π³DeepSeek-OCRπ³", theme=gr.themes.Soft()) as demo:
|
| 135 |
gr.Markdown(
|
| 136 |
"""
|
|
@@ -139,37 +137,23 @@ with gr.Blocks(title="π³DeepSeek-OCRπ³", theme=gr.themes.Soft()) as demo:
|
|
| 139 |
|
| 140 |
**π‘ How to use:**
|
| 141 |
1. **Upload an image** using the upload box.
|
| 142 |
-
2. Select a **Model Size**. `Gundam` is recommended for most documents
|
| 143 |
3. Choose a **Task Type**:
|
| 144 |
-
- **π Free OCR**: Extracts raw text from the image.
|
| 145 |
-
- **π Convert to Markdown**: Converts the
|
| 146 |
-
- **π Parse Figure**:
|
| 147 |
-
- **π Locate Object by Reference**: Finds a specific object
|
|
|
|
|
|
|
| 148 |
"""
|
| 149 |
)
|
| 150 |
|
| 151 |
with gr.Row():
|
| 152 |
with gr.Column(scale=1):
|
| 153 |
image_input = gr.Image(type="pil", label="πΌοΈ Upload Image", sources=["upload", "clipboard"])
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
value="Gundam (Recommended)",
|
| 158 |
-
label="βοΈ Model Size",
|
| 159 |
-
)
|
| 160 |
-
|
| 161 |
-
task_type = gr.Dropdown(
|
| 162 |
-
choices=["π Free OCR", "π Convert to Markdown", "π Parse Figure", "π Locate Object by Reference"],
|
| 163 |
-
value="π Convert to Markdown",
|
| 164 |
-
label="π Task Type",
|
| 165 |
-
)
|
| 166 |
-
|
| 167 |
-
ref_text_input = gr.Textbox(
|
| 168 |
-
label="π Reference Text (for Locate task)",
|
| 169 |
-
placeholder="e.g., the teacher, 11-2=, a red car...",
|
| 170 |
-
visible=False, # Initially hidden
|
| 171 |
-
)
|
| 172 |
-
|
| 173 |
submit_btn = gr.Button("Process Image", variant="primary")
|
| 174 |
|
| 175 |
with gr.Column(scale=2):
|
|
@@ -178,27 +162,12 @@ with gr.Blocks(title="π³DeepSeek-OCRπ³", theme=gr.themes.Soft()) as demo:
|
|
| 178 |
|
| 179 |
# --- UI Interaction Logic ---
|
| 180 |
def toggle_ref_text_visibility(task):
|
| 181 |
-
|
| 182 |
-
if task == "π Locate Object by Reference":
|
| 183 |
-
return gr.Textbox(visible=True)
|
| 184 |
-
else:
|
| 185 |
-
return gr.Textbox(visible=False)
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
fn=toggle_ref_text_visibility,
|
| 190 |
-
inputs=task_type,
|
| 191 |
-
outputs=ref_text_input,
|
| 192 |
-
)
|
| 193 |
-
|
| 194 |
-
# Define what happens when the submit button is clicked
|
| 195 |
-
submit_btn.click(
|
| 196 |
-
fn=process_ocr_task,
|
| 197 |
-
inputs=[image_input, model_size, task_type, ref_text_input],
|
| 198 |
-
outputs=[output_text, output_image],
|
| 199 |
-
)
|
| 200 |
|
| 201 |
-
# --- Example Images and Tasks ---
|
| 202 |
gr.Examples(
|
| 203 |
examples=[
|
| 204 |
["doc_markdown.png", "Gundam (Recommended)", "π Convert to Markdown", ""],
|
|
@@ -215,11 +184,9 @@ with gr.Blocks(title="π³DeepSeek-OCRπ³", theme=gr.themes.Soft()) as demo:
|
|
| 215 |
|
| 216 |
# --- 4. Launch the App ---
|
| 217 |
if __name__ == "__main__":
|
| 218 |
-
# Create an 'examples' directory if it doesn't exist
|
| 219 |
if not os.path.exists("examples"):
|
| 220 |
os.makedirs("examples")
|
| 221 |
-
#
|
| 222 |
-
# e.g., doc_markdown.png, chart.png, teacher.
|
| 223 |
|
| 224 |
-
demo.queue(max_size=20)
|
| 225 |
-
demo.launch(share=True) # Set share=True to create a public link
|
|
|
|
| 32 |
print(f"Error opening result image {filename}: {e}")
|
| 33 |
return None
|
| 34 |
|
| 35 |
+
# --- 2. Main Processing Function (UPDATED for multi-bbox drawing) ---
|
| 36 |
@spaces.GPU
|
| 37 |
def process_ocr_task(image, model_size, task_type, ref_text):
|
| 38 |
"""
|
| 39 |
Processes an image with DeepSeek-OCR for all supported tasks.
|
| 40 |
+
Now draws ALL detected bounding boxes for ANY task.
|
| 41 |
"""
|
| 42 |
if image is None:
|
| 43 |
return "Please upload an image first.", None
|
|
|
|
| 90 |
|
| 91 |
print(f"====\nπ Text Result: {text_result}\n====")
|
| 92 |
|
| 93 |
+
# --- NEW LOGIC: Always try to find and draw all bounding boxes ---
|
| 94 |
result_image_pil = None
|
| 95 |
|
| 96 |
+
# Define the pattern to find all coordinates like [[280, 15, 696, 997]]
|
| 97 |
+
pattern = re.compile(r"<\|det\|>\[\[(\d+),\s*(\d+),\s*(\d+),\s*(\d+)\]\]<\|/det\|>")
|
| 98 |
+
matches = list(pattern.finditer(text_result)) # Use finditer to get all matches
|
|
|
|
| 99 |
|
| 100 |
+
if matches:
|
| 101 |
+
print(f"β
Found {len(matches)} bounding box(es). Drawing on the original image.")
|
| 102 |
+
|
| 103 |
+
# Create a copy of the original image to draw on
|
| 104 |
+
image_with_bboxes = image.copy()
|
| 105 |
+
draw = ImageDraw.Draw(image_with_bboxes)
|
| 106 |
+
w, h = image.size # Get original image dimensions
|
| 107 |
+
|
| 108 |
+
for match in matches:
|
| 109 |
# Extract coordinates as integers
|
| 110 |
coords_norm = [int(c) for c in match.groups()]
|
| 111 |
x1_norm, y1_norm, x2_norm, y2_norm = coords_norm
|
| 112 |
|
|
|
|
|
|
|
|
|
|
| 113 |
# Scale the normalized coordinates (from 1000x1000 space) to the image's actual size
|
| 114 |
x1 = int(x1_norm / 1000 * w)
|
| 115 |
y1 = int(y1_norm / 1000 * h)
|
| 116 |
x2 = int(x2_norm / 1000 * w)
|
| 117 |
y2 = int(y2_norm / 1000 * h)
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
# Draw the rectangle with a red outline, 3 pixels wide
|
| 120 |
draw.rectangle([x1, y1, x2, y2], outline="red", width=3)
|
| 121 |
+
|
| 122 |
+
result_image_pil = image_with_bboxes
|
|
|
|
|
|
|
|
|
|
| 123 |
else:
|
| 124 |
+
# If no coordinates are found in the text, fall back to finding a pre-generated image
|
| 125 |
+
print("β οΈ No bounding box coordinates found in text result. Falling back to search for a result image file.")
|
| 126 |
result_image_pil = find_result_image(output_path)
|
| 127 |
|
| 128 |
return text_result, result_image_pil
|
| 129 |
|
| 130 |
|
| 131 |
+
# --- 3. Build the Gradio Interface (UPDATED) ---
|
| 132 |
with gr.Blocks(title="π³DeepSeek-OCRπ³", theme=gr.themes.Soft()) as demo:
|
| 133 |
gr.Markdown(
|
| 134 |
"""
|
|
|
|
| 137 |
|
| 138 |
**π‘ How to use:**
|
| 139 |
1. **Upload an image** using the upload box.
|
| 140 |
+
2. Select a **Model Size**. `Gundam` is recommended for most documents.
|
| 141 |
3. Choose a **Task Type**:
|
| 142 |
+
- **π Free OCR**: Extracts raw text from the image.
|
| 143 |
+
- **π Convert to Markdown**: Converts the document into Markdown, preserving structure.
|
| 144 |
+
- **π Parse Figure**: Extracts structured data from charts and figures.
|
| 145 |
+
- **π Locate Object by Reference**: Finds a specific object/text.
|
| 146 |
+
|
| 147 |
+
**βοΈ New Feature**: For **ALL** tasks, if the model detects page elements (text blocks, tables, titles, etc.), it will now draw **red bounding boxes** for them on the result image!
|
| 148 |
"""
|
| 149 |
)
|
| 150 |
|
| 151 |
with gr.Row():
|
| 152 |
with gr.Column(scale=1):
|
| 153 |
image_input = gr.Image(type="pil", label="πΌοΈ Upload Image", sources=["upload", "clipboard"])
|
| 154 |
+
model_size = gr.Dropdown(choices=["Tiny", "Small", "Base", "Large", "Gundam (Recommended)"], value="Gundam (Recommended)", label="βοΈ Model Size")
|
| 155 |
+
task_type = gr.Dropdown(choices=["π Free OCR", "π Convert to Markdown", "π Parse Figure", "π Locate Object by Reference"], value="π Convert to Markdown", label="π Task Type")
|
| 156 |
+
ref_text_input = gr.Textbox(label="π Reference Text (for Locate task)", placeholder="e.g., the teacher, 20-10, a red car...", visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
submit_btn = gr.Button("Process Image", variant="primary")
|
| 158 |
|
| 159 |
with gr.Column(scale=2):
|
|
|
|
| 162 |
|
| 163 |
# --- UI Interaction Logic ---
|
| 164 |
def toggle_ref_text_visibility(task):
|
| 165 |
+
return gr.Textbox(visible=True) if task == "π Locate Object by Reference" else gr.Textbox(visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
+
task_type.change(fn=toggle_ref_text_visibility, inputs=task_type, outputs=ref_text_input)
|
| 168 |
+
submit_btn.click(fn=process_ocr_task, inputs=[image_input, model_size, task_type, ref_text_input], outputs=[output_text, output_image])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
+
# --- UPDATED Example Images and Tasks ---
|
| 171 |
gr.Examples(
|
| 172 |
examples=[
|
| 173 |
["doc_markdown.png", "Gundam (Recommended)", "π Convert to Markdown", ""],
|
|
|
|
| 184 |
|
| 185 |
# --- 4. Launch the App ---
|
| 186 |
if __name__ == "__main__":
|
|
|
|
| 187 |
if not os.path.exists("examples"):
|
| 188 |
os.makedirs("examples")
|
| 189 |
+
# Make sure to have the correct image files in your "examples" folder
|
| 190 |
+
# e.g., doc_markdown.png, chart.png, teacher.jpg, math_locate.jpg, receipt.jpg
|
| 191 |
|
| 192 |
+
demo.queue(max_size=20).launch(share=True)
|
|
|