gopichandra commited on
Commit
3029115
·
verified ·
1 Parent(s): 0a3510a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -11
app.py CHANGED
@@ -1,17 +1,21 @@
1
  import gradio as gr
2
  from paddleocr import PaddleOCR
3
- from PIL import Image
4
  import numpy as np
5
 
6
  # Initialize PaddleOCR
7
  ocr = PaddleOCR(use_angle_cls=True, lang='en')
8
 
9
- # Function to process uploaded image
10
- def process_uploaded_image(image):
11
  if image is None:
12
  return "No image uploaded."
13
 
14
- # Perform OCR on the uploaded image
 
 
 
 
15
  result = ocr.ocr(np.array(image))
16
  extracted_text = []
17
  for line in result[0]:
@@ -19,12 +23,31 @@ def process_uploaded_image(image):
19
  return "\n".join(extracted_text)
20
 
21
  # Gradio Interface
22
- app = gr.Interface(
23
- fn=process_uploaded_image,
24
- inputs=gr.Image(type="pil", label="Upload or Capture Image"),
25
- outputs=gr.Textbox(label="Extracted Text"),
26
- title="OCR Processor"
27
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  if __name__ == "__main__":
30
- app.launch()
 
1
  import gradio as gr
2
  from paddleocr import PaddleOCR
3
+ from PIL import Image, ImageOps
4
  import numpy as np
5
 
6
  # Initialize PaddleOCR
7
  ocr = PaddleOCR(use_angle_cls=True, lang='en')
8
 
9
+ # Function to process uploaded/captured images
10
+ def process_uploaded_image(image, camera_type="rear"):
11
  if image is None:
12
  return "No image uploaded."
13
 
14
+ # Mirror the image for rear camera
15
+ if camera_type == "rear":
16
+ image = ImageOps.mirror(image)
17
+
18
+ # Perform OCR on the corrected image
19
  result = ocr.ocr(np.array(image))
20
  extracted_text = []
21
  for line in result[0]:
 
23
  return "\n".join(extracted_text)
24
 
25
  # Gradio Interface
26
+ def create_app():
27
+ with gr.Blocks() as app:
28
+ gr.Markdown("## OCR Processor with Rear Camera Fix")
29
+
30
+ # Input for image capture and camera type selection
31
+ with gr.Row():
32
+ image_input = gr.Image(source="webcam", type="pil", label="Capture Image from Webcam")
33
+ camera_type = gr.Dropdown(
34
+ choices=["rear", "front"],
35
+ value="rear",
36
+ label="Camera Type"
37
+ )
38
+
39
+ # Output for extracted text
40
+ extracted_text = gr.Textbox(label="Extracted Text")
41
+
42
+ # Button to process the image
43
+ process_button = gr.Button("Process Image")
44
+ process_button.click(
45
+ fn=process_uploaded_image,
46
+ inputs=[image_input, camera_type],
47
+ outputs=extracted_text
48
+ )
49
+
50
+ return app
51
 
52
  if __name__ == "__main__":
53
+ create_app().launch(share=True)