Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from paddleocr import PaddleOCR | |
| from PIL import Image, ImageOps | |
| import numpy as np | |
| # Initialize PaddleOCR | |
| ocr = PaddleOCR(use_angle_cls=True, lang='en') | |
| # Function to process captured images | |
| def process_image(image, camera_type="rear"): | |
| if image is None: | |
| return "No image captured." | |
| # Flip the image horizontally for the rear camera | |
| if camera_type == "rear": | |
| image = ImageOps.mirror(image) | |
| # Perform OCR on the corrected image | |
| result = ocr.ocr(np.array(image)) | |
| extracted_text = [] | |
| for line in result[0]: | |
| extracted_text.append(line[1][0]) | |
| return "\n".join(extracted_text) | |
| # Gradio Interface | |
| def create_app(): | |
| with gr.Blocks() as app: | |
| gr.Markdown("## OCR Processor with Rear Camera Fix") | |
| # Inputs for image capture and camera type selection | |
| with gr.Row(): | |
| image_input = gr.Image(type="pil", label="Capture Image") # For image capture | |
| camera_type = gr.Dropdown( | |
| choices=["rear", "front"], | |
| value="rear", | |
| label="Camera Type" | |
| ) | |
| # Output for extracted text | |
| extracted_text = gr.Textbox(label="Extracted Text") | |
| # Button to process the captured image | |
| process_button = gr.Button("Process Image") | |
| process_button.click( | |
| fn=process_image, | |
| inputs=[image_input, camera_type], | |
| outputs=extracted_text | |
| ) | |
| return app | |
| if __name__ == "__main__": | |
| create_app().launch(share=True) | |