File size: 1,808 Bytes
2c20728
 
c9b1b84
2c20728
c9b1b84
 
2c20728
 
 
d9ed74d
c9b1b84
2c20728
c9b1b84
2c20728
d9ed74d
c9b1b84
2c20728
 
d9ed74d
c9b1b84
 
 
 
d9ed74d
c9b1b84
 
d9ed74d
c9b1b84
 
 
d9ed74d
c9b1b84
 
 
d9ed74d
2c20728
c9b1b84
 
 
 
 
0895b46
c9b1b84
d9ed74d
c9b1b84
d9ed74d
c9b1b84
2c20728
c9b1b84
2c20728
d9ed74d
c9b1b84
 
d9ed74d
 
 
 
 
 
 
 
2c20728
 
c9b1b84
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
import gradio as gr
import easyocr
import cv2
import numpy as np
from PIL import Image
import requests
from io import BytesIO
import json

# Initialize EasyOCR once
reader = easyocr.Reader(['en'])

def detect_digits(image_url):
    try:
        # Download the image
        response = requests.get(image_url)
        response.raise_for_status()
        
        # Convert to OpenCV format
        image = Image.open(BytesIO(response.content)).convert('RGB')
        img_np = np.array(image)
        img_cv = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
        
        # Perform text detection
        results = reader.readtext(img_cv)
        
        # Process results
        output = []
        for bbox, text, conf in results:
            if any(c.isdigit() for c in text):
                # Extract coordinates
                tl = tuple(map(int, bbox[0]))
                br = tuple(map(int, bbox[2]))
                
                output.append({
                    "text": text,
                    "confidence": float(conf),
                    "coordinates": {
                        "x_min": tl[0],
                        "y_min": tl[1],
                        "x_max": br[0],
                        "y_max": br[1]
                    }
                })
        
        return json.dumps(output, indent=2, ensure_ascii=False)
    
    except Exception as e:
        return json.dumps({"error": str(e)})

# Create Gradio interface
interface = gr.Interface(
    fn=detect_digits,
    inputs=gr.Textbox(
        label="Image URL",
        placeholder="Enter image URL here..."
    ),
    outputs=gr.JSON(label="Detection Results"),
    title="Digit Region Detection",
    description="Upload an image URL to detect regions containing numbers",
    allow_flagging="never"
)

interface.launch()