Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,48 +6,48 @@ import re
|
|
6 |
from paddleocr import PaddleOCR
|
7 |
from datetime import datetime
|
8 |
|
9 |
-
#
|
10 |
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
11 |
|
12 |
-
# Preprocessing: Convert to grayscale and threshold
|
13 |
def preprocess_image(image):
|
14 |
img = np.array(image.convert("RGB"))
|
15 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
16 |
_, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
|
17 |
return Image.fromarray(thresh)
|
18 |
|
19 |
-
# OCR detection + regex filtering
|
20 |
def extract_weight_text(image):
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
# Main function
|
30 |
def detect_weight(image):
|
31 |
if image is None:
|
32 |
-
return "No image uploaded.", "", None
|
33 |
pre_img = preprocess_image(image)
|
34 |
-
weight = extract_weight_text(pre_img)
|
35 |
-
if weight:
|
36 |
-
return f"Detected Weight: {weight} kg", datetime.now().strftime("Captured At: %Y-%m-%d %H:%M:%S"), pre_img
|
37 |
else:
|
38 |
-
return "Weight Not Detected", "", pre_img
|
39 |
|
40 |
-
|
41 |
-
interface = gr.Interface(
|
42 |
fn=detect_weight,
|
43 |
inputs=gr.Image(type="pil", label="Upload or Capture Image"),
|
44 |
outputs=[
|
45 |
gr.Textbox(label="Weight"),
|
46 |
gr.Textbox(label="Timestamp"),
|
47 |
-
gr.Image(label="Preprocessed Image")
|
|
|
48 |
],
|
49 |
-
title="Auto Weight Logger",
|
50 |
-
description="Upload
|
51 |
-
)
|
52 |
-
|
53 |
-
interface.launch()
|
|
|
6 |
from paddleocr import PaddleOCR
|
7 |
from datetime import datetime
|
8 |
|
9 |
+
# Load OCR model
|
10 |
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
11 |
|
|
|
12 |
def preprocess_image(image):
|
13 |
img = np.array(image.convert("RGB"))
|
14 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
15 |
_, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
|
16 |
return Image.fromarray(thresh)
|
17 |
|
|
|
18 |
def extract_weight_text(image):
|
19 |
+
try:
|
20 |
+
results = ocr.ocr(np.array(image), cls=True)
|
21 |
+
texts = []
|
22 |
+
for line in results[0]:
|
23 |
+
text = line[1][0]
|
24 |
+
texts.append(text)
|
25 |
+
match = re.search(r"\d+\.\d+", text)
|
26 |
+
if match:
|
27 |
+
return match.group(), texts
|
28 |
+
return None, texts
|
29 |
+
except Exception as e:
|
30 |
+
return f"Error: {str(e)}", []
|
31 |
|
|
|
32 |
def detect_weight(image):
|
33 |
if image is None:
|
34 |
+
return "No image uploaded.", "", None, ""
|
35 |
pre_img = preprocess_image(image)
|
36 |
+
weight, texts = extract_weight_text(pre_img)
|
37 |
+
if weight and "Error" not in weight:
|
38 |
+
return f"Detected Weight: {weight} kg", datetime.now().strftime("Captured At: %Y-%m-%d %H:%M:%S"), pre_img, "\n".join(texts)
|
39 |
else:
|
40 |
+
return "Weight Not Detected", "", pre_img, "\n".join(texts)
|
41 |
|
42 |
+
gr.Interface(
|
|
|
43 |
fn=detect_weight,
|
44 |
inputs=gr.Image(type="pil", label="Upload or Capture Image"),
|
45 |
outputs=[
|
46 |
gr.Textbox(label="Weight"),
|
47 |
gr.Textbox(label="Timestamp"),
|
48 |
+
gr.Image(label="Preprocessed Image"),
|
49 |
+
gr.Textbox(label="Detected Text (Debug)")
|
50 |
],
|
51 |
+
title="Auto Weight Logger (Debug Mode)",
|
52 |
+
description="Upload a clear image of a digital scale. This version shows detected text for debugging."
|
53 |
+
).launch()
|
|
|
|