CardOCR / demo.py
cadyderwin's picture
Update demo.py
5156788 verified
import os
import gradio as gr
import requests
import json
import io
from gradio.components import Image
from PIL import Image as PILImage, ImageDraw, ImageFont # This import may be needed if you're processing images
from PIL import Image
def process_image(image):
# Convert PIL image to bytes to send in POST request
img_bytes = io.BytesIO()
image.save(img_bytes, format="JPEG")
img_bytes.seek(0)
url = "http://127.0.0.1:9000/process_image"
files = {'image': img_bytes}
result = requests.post(url=url, files=files)
if result.ok:
json_result = result.json()
if json_result.get("resultCode") == "Error":
return [{"resultCode": "Error", "result": "Failed to process image"}]
return [json_result]
else:
return [{"resultCode": "Error", "result": result.text}]
with gr.Blocks() as demo:
gr.Markdown(
"""
<div style="display: flex;align-items: center;">
<img alt="Opulentyn Logo" src="https://github.com/user-attachments/assets/5fc78032-bff2-4f7e-a174-7d64b22f506d" width="350"/>
<div>
<h1>Credit Card OCR</h1>
<p>We offer <b>on-premises</b> OCR and liveness check solutions available with a <b>perpetual license</b>.</p>
</div>
</div>
## 🤝 Talk to us
<div style="display: flex; align-items: center;">
<a href="https://opulentyn.com" target="_blank">
<img src="https://img.shields.io/badge/Website-https%3A%2F%2Fopulentyn.com-blue?style=flat&logo=google-chrome&logoColor=white" alt="Website">
</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="mailto:support@opulentyn.com">
<img src="https://img.shields.io/badge/Email-support%40opulentyn.com-blue?style=flat&logo=gmail&logoColor=white" alt="Email">
</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="https://join.slack.com/t/opulentyn/shared_invite/zt-2s230jtbq-dWBs8XUZcrYim~nUqiimSA" target="_blank">
<img src="https://img.shields.io/badge/Slack-support--sdk-blueviolet?style=flat&logo=slack&logoColor=white" alt="Slack">
</a>
</div>
"""
)
with gr.Row():
with gr.Column():
image_input = gr.Image(type='pil')
gr.Examples(['examples/1.jpg', 'examples/2.jpg', 'examples/3.jpg'],
inputs=image_input)
process_button = gr.Button("Process")
with gr.Column():
json_output = gr.JSON()
process_button.click(process_image, inputs=[image_input], outputs=[json_output])
gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fopulentyn%2FCardOCR"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fopulentyn%2FCardOCR&countColor=%23263759" /></a>')
demo.launch(server_name="0.0.0.0", server_port=7860)