sonygod commited on
Commit
cf4bb10
·
1 Parent(s): 5cf4e95
Files changed (1) hide show
  1. app.py +44 -20
app.py CHANGED
@@ -43,14 +43,14 @@ class OCRUI:
43
  except Exception as e:
44
  return {"error": str(e)}
45
 
46
- def ocr_process(self, image, task, preprocess_method):
47
  if image is None:
48
- return "Please upload an image", "No image provided"
49
 
50
  # Preprocess image
51
  processed_img = self.process_image(image, preprocess_method)
52
  if processed_img is None:
53
- return "Image processing failed", "Processing error"
54
 
55
  # Prepare image for API
56
  encode_params = [cv2.IMWRITE_JPEG_QUALITY, 50]
@@ -60,13 +60,16 @@ class OCRUI:
60
  result = asyncio.run(self.send_to_api(img_bytes.tobytes()))
61
 
62
  if result is None or "error" in result:
63
- return "API call failed", "Error calling OCR service"
64
 
65
- # Format results
66
- text_output = f"Task: {task}\nResults:\n{str(result)}"
67
- html_output = f"<pre>{text_output}</pre>"
68
-
69
- return text_output, html_output
 
 
 
70
 
71
  def create_ui():
72
  ui = OCRUI()
@@ -76,27 +79,48 @@ def create_ui():
76
 
77
  with gr.Row():
78
  with gr.Column():
79
- image_input = gr.Image(type="numpy", label="Input Image")
 
 
 
 
 
 
 
80
  preprocess_dropdown = gr.Dropdown(
81
  choices=["Original", "Fourier", "Morphological", "Adaptive", "Enhanced"],
82
  label="Preprocessing Method",
83
  value="Original"
84
  )
85
- task_dropdown = gr.Dropdown(
86
- choices=["Plain OCR", "Format OCR", "Box OCR"],
87
- label="OCR Task",
88
- value="Plain OCR"
89
- )
90
  process_btn = gr.Button("Process Image")
91
-
92
  with gr.Column():
93
- text_output = gr.Textbox(label="OCR Results")
94
- html_output = gr.HTML(label="Formatted Results")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  process_btn.click(
97
  fn=ui.ocr_process,
98
- inputs=[image_input, task_dropdown, preprocess_dropdown],
99
- outputs=[text_output, html_output]
100
  )
101
 
102
  return demo
 
43
  except Exception as e:
44
  return {"error": str(e)}
45
 
46
+ def ocr_process(self, image, preprocess_method):
47
  if image is None:
48
+ return None, "Please upload an image"
49
 
50
  # Preprocess image
51
  processed_img = self.process_image(image, preprocess_method)
52
  if processed_img is None:
53
+ return None, "Image processing failed"
54
 
55
  # Prepare image for API
56
  encode_params = [cv2.IMWRITE_JPEG_QUALITY, 50]
 
60
  result = asyncio.run(self.send_to_api(img_bytes.tobytes()))
61
 
62
  if result is None or "error" in result:
63
+ return None, "API call failed"
64
 
65
+ return result, None # Return raw JSON for gradio.JSON component
66
+
67
+
68
+ def preview_process(self, image, method):
69
+ """Preview processed image without OCR"""
70
+ if image is None:
71
+ return None
72
+ return self.process_image(image, method)
73
 
74
  def create_ui():
75
  ui = OCRUI()
 
79
 
80
  with gr.Row():
81
  with gr.Column():
82
+ image_input = gr.Image(
83
+ type="numpy",
84
+ label="Input Image",
85
+ height=200, # Smaller initial height
86
+ show_download_button=False,
87
+ interactive=True,
88
+ container=True,
89
+ )
90
  preprocess_dropdown = gr.Dropdown(
91
  choices=["Original", "Fourier", "Morphological", "Adaptive", "Enhanced"],
92
  label="Preprocessing Method",
93
  value="Original"
94
  )
 
 
 
 
 
95
  process_btn = gr.Button("Process Image")
96
+
97
  with gr.Column():
98
+ preview_output = gr.Image(
99
+ type="numpy",
100
+ label="Processed Preview",
101
+ height=200,
102
+ show_download_button=False,
103
+ interactive=False,
104
+ )
105
+ json_output = gr.JSON(label="OCR Results") # Replace text_output
106
+ status_output = gr.Text(label="Status") # For error messages
107
+
108
+ # Add preview update on image or method change
109
+ image_input.change(
110
+ fn=ui.preview_process,
111
+ inputs=[image_input, preprocess_dropdown],
112
+ outputs=preview_output
113
+ )
114
+ preprocess_dropdown.change(
115
+ fn=ui.preview_process,
116
+ inputs=[image_input, preprocess_dropdown],
117
+ outputs=preview_output
118
+ )
119
 
120
  process_btn.click(
121
  fn=ui.ocr_process,
122
+ inputs=[image_input, preprocess_dropdown],
123
+ outputs=[json_output, status_output]
124
  )
125
 
126
  return demo