Update app.py
Browse files
app.py
CHANGED
@@ -17,7 +17,7 @@ logger.info("EasyOCR initialized.")
|
|
17 |
|
18 |
logger.info("Loading nutrition extraction model...")
|
19 |
# Load the model using the Hugging Face Transformers pipeline.
|
20 |
-
#
|
21 |
tokenizer = AutoTokenizer.from_pretrained("openfoodfacts/nutrition-extractor")
|
22 |
model = AutoModelForTokenClassification.from_pretrained("openfoodfacts/nutrition-extractor")
|
23 |
logger.info("Model loaded successfully.")
|
@@ -26,6 +26,7 @@ def ocr_extract(image: Image.Image):
|
|
26 |
"""
|
27 |
Uses EasyOCR to extract text tokens and their bounding boxes from an image.
|
28 |
Returns a list of tokens and corresponding boxes in [left, top, width, height] format.
|
|
|
29 |
"""
|
30 |
# Convert PIL image to numpy array.
|
31 |
np_image = np.array(image)
|
@@ -39,10 +40,10 @@ def ocr_extract(image: Image.Image):
|
|
39 |
# Convert the bounding box (list of 4 points) to [left, top, width, height].
|
40 |
xs = [point[0] for point in bbox]
|
41 |
ys = [point[1] for point in bbox]
|
42 |
-
left = min(xs)
|
43 |
-
top = min(ys)
|
44 |
-
width = max(xs) - left
|
45 |
-
height = max(ys) - top
|
46 |
boxes.append([left, top, width, height])
|
47 |
logger.info(f"OCR extracted {len(tokens)} tokens.")
|
48 |
return tokens, boxes
|
|
|
17 |
|
18 |
logger.info("Loading nutrition extraction model...")
|
19 |
# Load the model using the Hugging Face Transformers pipeline.
|
20 |
+
# Force CPU inference with device=-1.
|
21 |
tokenizer = AutoTokenizer.from_pretrained("openfoodfacts/nutrition-extractor")
|
22 |
model = AutoModelForTokenClassification.from_pretrained("openfoodfacts/nutrition-extractor")
|
23 |
logger.info("Model loaded successfully.")
|
|
|
26 |
"""
|
27 |
Uses EasyOCR to extract text tokens and their bounding boxes from an image.
|
28 |
Returns a list of tokens and corresponding boxes in [left, top, width, height] format.
|
29 |
+
Bounding box coordinates are cast to int.
|
30 |
"""
|
31 |
# Convert PIL image to numpy array.
|
32 |
np_image = np.array(image)
|
|
|
40 |
# Convert the bounding box (list of 4 points) to [left, top, width, height].
|
41 |
xs = [point[0] for point in bbox]
|
42 |
ys = [point[1] for point in bbox]
|
43 |
+
left = int(min(xs))
|
44 |
+
top = int(min(ys))
|
45 |
+
width = int(max(xs) - left)
|
46 |
+
height = int(max(ys) - top)
|
47 |
boxes.append([left, top, width, height])
|
48 |
logger.info(f"OCR extracted {len(tokens)} tokens.")
|
49 |
return tokens, boxes
|