ID-card-OCR / check.py
subashdvorak's picture
Update check.py
0965028 verified
import streamlit as st
import base64
from groq import Groq
# Function to encode the image
def encode_image(image):
return base64.b64encode(image.read()).decode('utf-8')
# Function to extract ID card details from the image
def extract_id_details(image_url_or_file):
client = Groq()
# Check if the input is a file or a URL
if isinstance(image_url_or_file, str) and (image_url_or_file.startswith("http") or image_url_or_file.startswith("data:image")):
# If it's a URL or base64 string
url = image_url_or_file
else:
# If it's a file, encode it to base64
url = f"data:image/jpeg;base64,{encode_image(image_url_or_file)}"
completion = client.chat.completions.create(
model="llama-3.2-90b-vision-preview",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "List all the details from the provided image of an ID card. Just list them in points. Don't give any other background descriptions."},
{
"type": "image_url",
"image_url": {"url": url}
},
],
}
],
temperature=0.2,
max_completion_tokens=200,
top_p=0,
stream=False,
stop=None,
)
return completion.choices[0].message.content
# Streamlit app interface
st.title("ID Card OCR Extractor")
# Input for image URL or upload
image_url = st.text_input("Enter the image URL:")
uploaded_image = st.file_uploader("Or upload an image of an ID card", type=["jpg", "jpeg", "png"])
if st.button("Extract Details"):
if image_url:
with st.spinner("Extracting details from the URL..."):
details = extract_id_details(image_url) # Use URL directly
st.subheader("Extracted Details:")
st.write(details)
elif uploaded_image:
with st.spinner("Extracting details from the uploaded image..."):
# Pass the uploaded image directly to the function
details = extract_id_details(uploaded_image) # File uploaded
st.subheader("Extracted Details:")
st.write(details)
else:
st.warning("Please provide either a valid image URL or upload an image.")