File size: 1,259 Bytes
8b98450
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from PIL import Image
import requests
import io
import base64
import imghdr

def call(input_image):
    buffer = io.BytesIO()
    input_image.save(buffer, format='PNG')  # You can choose the format
    base64_string = base64.b64encode(buffer.getvalue()).decode('utf-8')

    # Decode the base64 string to binary data
    image_data = base64.b64decode(base64_string)
    # Detect the image type
    image_type = imghdr.what(None, image_data)
    # Map the image type to a MIME type
    mime_type = f"image/{image_type}" if image_type else "application/octet-stream"
    # Create the data URL
    data_url = f"data:{mime_type};base64,{base64_string}"

    url = 'https://matthewfarant-business-card-scanner.hf.space/api/scan'
    headers = {
        'accept': 'application/json',
        'X-API-Key': 'sk-3a85ec28-b262-448f-9736-a4717740c8b6',
        'Content-Type': 'application/json'
    }
    data = {
        'url': data_url
    }

    response = requests.post(url, headers=headers, json=data)
    
    if response.status_code == 200:
        return response.json()
    else:
        response.raise_for_status() 

app = gr.Interface(fn=call, inputs=gr.Image(type = 'pil'), outputs="textbox")

if __name__ == "__main__":
    app.launch()