hiandrewfisher commited on
Commit
f630d31
·
verified ·
1 Parent(s): 3df9c0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -5
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
- # We force CPU inference by using 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,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