Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add barcode detection to OCR #651

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ RUN apt-get -qq -y update \
fonts-droid-fallback fonts-dustin fonts-f500 fonts-fanwood fonts-freefont-ttf \
fonts-liberation fonts-lmodern fonts-lyx fonts-sil-gentium fonts-texgyre \
fonts-tlwg-purisa \
libzbar0 \
###
&& apt-get -qq -y autoremove \
&& apt-get clean \
Expand Down
86 changes: 83 additions & 3 deletions ingestors/support/ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import threading
from hashlib import sha1
from normality import stringify
from PIL import Image
from PIL import Image, ImageFilter

from pyzbar import pyzbar

from io import BytesIO
from languagecodes import list_to_alpha3 as alpha3

Expand Down Expand Up @@ -45,6 +48,81 @@ def extract_ocr_text(self, data, languages=None):
return stringify(text)


class ZBarDetectorService(object):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • It would be great to have a few testcases for this

THRESHOLDS = list(range(32, 230, 32))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be able to document where these values come from and what they represent?


def _enhance_image(self, image, threshold=127):
width, height = image.size

# Convert to grayscale using Pillow
image = image.convert("L")

# Apply Gaussian blur to reduce noise
image = image.filter(ImageFilter.GaussianBlur(3))

# Apply threshold
image = image.point(lambda p: 255 if p > threshold else 0)

# Dilatate the image
image = image.filter(ImageFilter.MaxFilter(3))

# Erode the image
image = image.filter(ImageFilter.MinFilter(3))

# Resize the image to make the QR code larger
new_size = map(lambda x: x * 2, image.size)
image = image.resize(new_size, resample=Image.Resampling.BILINEAR)

# Last round of gaussian blur
image = image.filter(ImageFilter.GaussianBlur(5))
return image

def _serialize_zbar_result(self, result):
return "\n".join(
[
"",
"--- {} CODE ---".format(result.type),
"QUALITY: {}".format(result.quality),
"ORIENTATION: {}".format(result.orientation),
"POSITION: {}".format(list(result.rect)),
"DATA: {}".format(result.data.decode("utf-8")),
]
)

def _results_to_text(self, results):
return "---\n".join([self._serialize_zbar_result(result) for result in results])

def _try_best(self, image):
results = pyzbar.decode(image)
# Found it at first try
if len(results) > 0:
log.info("OCR: zbar found (%d) results at first shot", len(results))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(minor thing but) I'd lower all of the logging calls to log.debug.

return results

log.info("OCR: zbar ehnahcing image")
# Try with our enhance logic
for threshold in self.THRESHOLDS:
log.info("OCR: zbar applying threshold %d", threshold)
# Headsup: preserve the original image
new_image = self._enhance_image(image, threshold=threshold)
results = pyzbar.decode(new_image)

if len(results) > 0:
log.info(
"OCR: zbar found (%d) results with threshold=%d",
len(results),
threshold,
)
return results

# no results found then
return []

def extract_barcodes(self, image):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I'd appreciate adding type hints here. Assuming image is a PIL.Image and the return type is a str?
  • (minor) This being public it would be great to add a docstring saying what it does, since at first glance I wouldn't necessarily expect this to return text. (Perhaps extract_text_from_barcodes is more appropiate?

log.info("OCR: zbar scanning for codes")
return self._results_to_text(self._try_best(image))


class LocalOCRService(object):
"""Perform OCR using an RPC-based service."""

Expand Down Expand Up @@ -90,6 +168,7 @@ def extract_text(self, data, languages=None):
log.error("Cannot open image data using Pillow: %s", exc)
return ""

text = ""
with temp_locale(TESSERACT_LOCALE):
languages = self.language_list(languages)
api = self.configure_engine(languages)
Expand All @@ -109,13 +188,14 @@ def extract_text(self, data, languages=None):
confidence,
duration,
)
return text
except Exception as exc:
log.error("OCR error: %s", exc)
return ""
finally:
api.Clear()

text += ZBarDetectorService().extract_barcodes(image)
return text


class GoogleOCRService(object):
"""Use Google's Vision API to perform OCR. This has very good quality
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ tesserocr==2.6.2
spacy==3.6.1
fingerprints==1.1.1
fasttext==0.9.2
pyzbar==0.1.9

# Development
pytest==8.2.0
Expand Down