File size: 946 Bytes
c4904a4
56810b2
 
 
37bea37
56810b2
 
37bea37
56810b2
 
 
 
 
 
 
 
 
 
 
37bea37
56810b2
37bea37
56810b2
 
 
 
 
 
 
 
37bea37
56810b2
 
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
import gradio as gr
from mmocr.apis import MMOCR
import cv2
import os

# Initialize MMOCR once
ocr = MMOCR(det='DB_r18', recog='SAR', device='cpu')  # use 'cuda:0' if GPU is available

def detect_weight(image):
    results = ocr.readtext(image, output=None)
    
    detected_texts = [item['text'] for item in results[0]['result']]
    
    # Search for weight-like patterns like "25.5 kg", "100kg", etc.
    weight = "Not detected"
    for text in detected_texts:
        if 'kg' in text.lower():
            weight = text
            break

    return f"Detected Weight: {weight}"

# Gradio UI
interface = gr.Interface(
    fn=detect_weight,
    inputs=gr.Image(type="filepath", label="Upload Weight Image"),
    outputs=gr.Textbox(label="OCR Output"),
    title="Weight Detector using MMOCR",
    description="Upload an image with weight text (e.g., '25 kg'), and this app will detect it."
)

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