File size: 2,296 Bytes
78d07ad
 
 
 
 
 
 
 
 
0965028
78d07ad
7f20ce6
0965028
 
 
 
78d07ad
0965028
 
7f20ce6
78d07ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f20ce6
78d07ad
 
 
 
0965028
 
78d07ad
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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.")