Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,51 @@
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image, ImageEnhance, ImageOps
|
3 |
-
import
|
4 |
-
|
5 |
from datetime import datetime
|
6 |
import pytz
|
7 |
-
import re
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1")
|
12 |
|
13 |
-
#
|
14 |
def enhance_image(image):
|
15 |
-
|
16 |
-
image =
|
17 |
-
|
18 |
-
image =
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
image = image.resize((image.width * 3, image.height * 3))
|
24 |
-
# Convert back to RGB for model
|
25 |
-
return image.convert("RGB")
|
26 |
-
|
27 |
-
# Extract accurate decimal weight
|
28 |
def detect_weight(image):
|
29 |
try:
|
30 |
-
# Enhance image
|
31 |
processed_image = enhance_image(image)
|
32 |
|
33 |
-
# OCR using
|
34 |
-
|
35 |
-
|
36 |
-
generated_ids = model.generate(
|
37 |
-
pixel_values,
|
38 |
-
max_length=64,
|
39 |
-
num_beams=4, # Beam search to improve precision
|
40 |
-
early_stopping=True
|
41 |
-
)
|
42 |
-
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
|
43 |
|
44 |
-
# Extract
|
45 |
-
match = re.search(r"(\d{1,4}
|
46 |
weight = match.group(1) if match else "Not detected"
|
47 |
|
48 |
-
#
|
49 |
ist = pytz.timezone('Asia/Kolkata')
|
50 |
current_time = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S")
|
51 |
|
52 |
return f"Weight: {weight} kg\nCaptured At: {current_time} (IST)", image
|
|
|
53 |
except Exception as e:
|
54 |
return f"Error: {str(e)}", image
|
55 |
|
56 |
-
# Gradio
|
57 |
interface = gr.Interface(
|
58 |
fn=detect_weight,
|
59 |
inputs=gr.Image(type="pil", label="Upload or Capture Image"),
|
60 |
outputs=[gr.Textbox(label="Weight Info"), gr.Image(label="Snapshot")],
|
61 |
-
title="⚖️ Auto Weight Detector
|
62 |
-
description="Detects full weight
|
63 |
)
|
64 |
|
65 |
interface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image, ImageEnhance, ImageOps
|
3 |
+
import easyocr
|
4 |
+
import re
|
5 |
from datetime import datetime
|
6 |
import pytz
|
|
|
7 |
|
8 |
+
# Initialize EasyOCR reader
|
9 |
+
reader = easyocr.Reader(['en'])
|
|
|
10 |
|
11 |
+
# Preprocessing to enhance image for OCR
|
12 |
def enhance_image(image):
|
13 |
+
image = image.convert("L") # Grayscale
|
14 |
+
image = ImageOps.invert(image) # Invert
|
15 |
+
image = ImageEnhance.Contrast(image).enhance(2.0)
|
16 |
+
image = ImageEnhance.Sharpness(image).enhance(2.5)
|
17 |
+
image = image.resize((image.width * 2, image.height * 2)) # Enlarge
|
18 |
+
return image
|
19 |
+
|
20 |
+
# Weight detection function
|
|
|
|
|
|
|
|
|
|
|
21 |
def detect_weight(image):
|
22 |
try:
|
|
|
23 |
processed_image = enhance_image(image)
|
24 |
|
25 |
+
# OCR using EasyOCR
|
26 |
+
result = reader.readtext(np.array(processed_image), detail=0)
|
27 |
+
full_text = " ".join(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
# Extract number with decimal point
|
30 |
+
match = re.search(r"(\d{1,4}\.\d{1,4})", full_text)
|
31 |
weight = match.group(1) if match else "Not detected"
|
32 |
|
33 |
+
# Get IST time
|
34 |
ist = pytz.timezone('Asia/Kolkata')
|
35 |
current_time = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S")
|
36 |
|
37 |
return f"Weight: {weight} kg\nCaptured At: {current_time} (IST)", image
|
38 |
+
|
39 |
except Exception as e:
|
40 |
return f"Error: {str(e)}", image
|
41 |
|
42 |
+
# Gradio interface
|
43 |
interface = gr.Interface(
|
44 |
fn=detect_weight,
|
45 |
inputs=gr.Image(type="pil", label="Upload or Capture Image"),
|
46 |
outputs=[gr.Textbox(label="Weight Info"), gr.Image(label="Snapshot")],
|
47 |
+
title="⚖️ Accurate Auto Weight Detector",
|
48 |
+
description="Detects full decimal weight (e.g., 52.75 kg) using EasyOCR and shows timestamp (IST)"
|
49 |
)
|
50 |
|
51 |
interface.launch()
|