pypocquant.lib.barcode
¶
Module Contents¶
Functions¶
|
Detect the barcode in the image. |
|
Rotate the image by given angle in degrees. |
|
Calculate area and approximate aspect ratio of a contour. |
|
Try to estimate the orientation of the image, and rotate if needed. |
|
Read the FID string from the barcode image using pytesseract and |
|
Parse the output of pyzbar and retrieve the FID. |
|
Use pytesseract to retrieve FID from the strip box image. |
|
Try extracting barcode from QR code box while scaling it for different orientations [0, 90, 180, -90]. |
|
Try extracting barcode from QR code box for a list of angles in the range of angle_range. |
|
Extract the box around the strip using the QR barcode data. |
|
Extract the box around the strip using the QR barcode data. |
|
Try to extract the barcodes from the image by rescaling the intensity of the image with a linear stretch. |
|
Try finding a CODE 128 barcode in barcode data that should contain the patient FID. |
|
Extract FID from rgb image. |
|
Try extracting the fid and all barcodes from the image by rescaling the intensity of the image with a |
|
Try extracting the fid and all barcodes from the image by rescaling the intensity of the image with a |
|
Rotate the image if the orientation is not the expected one. |
|
Rotate the image if the orientation is not the expected one. |
|
Selection of FID from candidates depending on if candidates contain a FID. |
|
Hide the barcode on the strip image. |
|
Extract the strip from the strip box. |
Return the numeric value of the FID (as string). |
|
Return the numeric value of the FID. |
|
|
Determine the the QR code box rotation angle |
|
Method to align QR code box with image border of the full image (old pipeline). |
|
Method to align QR code box with image border of the full image. |
-
class
pypocquant.lib.barcode.
Barcode
(top: int, left: int, width: int, height: int, data: Union[bytes, str], symbol: str)¶ Bases:
object
Pythonic barcode object.
-
classmethod
from_barcode
(cls, barcode)¶ Initialize from pyzbar barcode object.
- Parameters
barcode – A barcode (QR, CODE39, CODE128).
-
scale
(self, factor: float)¶ Scale the barcode object by given factor.
The (top, left) is scaled accordingly.
- Parameters
factor – Scaling factor for the barcode.
-
__str__
(self)¶ Return str(self).
-
__repr__
(self)¶ Return repr(self).
-
classmethod
-
pypocquant.lib.barcode.
detect
(image: np.ndarray, expected_area=22000, expected_aspect_ratio=7.5, barcode_border=75, blur_size=(3, 3), morph_rect=(9, 3), mm_iter=1, qc=True, verbose=False)¶ Detect the barcode in the image.
Adapted from: https://www.pyimagesearch.com/2014/11/24/detecting-barcodes-images-python-opencv/
Returns the extracted barcode image, the coordinates of the extracted rectangle, the (possibly rotated) image, and (if qc is True) a copy of the (possibly rotated) image with the extracted rectangle coordinates overlaid on it.
- Parameters
image (np.ndarray) – Image from which barcode should be read
expected_area (int) – Expected area for barcode.
expected_aspect_ratio (float) – Aspect ratio for barcode.
barcode_border (int) – Border of the barcode.
blur_size (tuple) – Kernel (3,3) by default for bluring the image.
morph_rect (tuple) – Kernel (9,3) by default for morph rect.
mm_iter (int) – Dilation & Eroding iterations.
qc (bool) – Bool, if true quality control images will be saved.
verbose (bool) – Bool, if true additional loggin info will be displayed.
- Returns
barcode_img: The image of the barcode.
- Returns
coordinates: The position and size coordinates of the barcode (x,y, w, h)/
- Return type
coordinates: tuple
- Returns
image: The image.
- Returns
mask_image The mask of the image.
- Return type
tuple
-
pypocquant.lib.barcode.
rotate
(image, angle)¶ Rotate the image by given angle in degrees.
- Parameters
image – The image to be rotated.
angle – Rotation angle in degrees for the image.
- Returns
image: Rotated image.
-
pypocquant.lib.barcode.
calc_area_and_approx_aspect_ratio
(contour)¶ Calculate area and approximate aspect ratio of a contour.
- Parameters
contour – cv2.Contour.
- Returns
area: Area of the contour.
- Returns
aspect_ratio: Aspect ratio of the contour.
-
pypocquant.lib.barcode.
rotate_90_if_needed
(image)¶ Try to estimate the orientation of the image, and rotate if needed.
@TODO: This is not very robust so far.
- Parameters
image – Image to be rotated by 90 degrees.
- Returns
image: By 90 degrees rotated image.
-
pypocquant.lib.barcode.
read_FID_from_barcode_image
(image)¶ Read the FID string from the barcode image using pytesseract and decode the barcode itself using pyzbar.
- Parameters
image – Image to read FID from barcode.
- Returns
fid_tesseract: FID detected by tesseract (OCR).
- Returns
fid_pyzbar: FID detected by pyzbar (barcode).
- Returns
score: Score how well FID detection worked. For more details about the score read the manual.
-
pypocquant.lib.barcode.
get_fid_from_barcode_data
(barcode_data, barcode_type='CODE128')¶ Parse the output of pyzbar and retrieve the FID.
- Parameters
barcode_data – Barcode data (zbar).
barcode_type – Type of barcode (CODE39, CODE128, QRCODE).
- Returns
barcode: Decoded barcode as utf8.
-
pypocquant.lib.barcode.
get_fid_from_box_image_using_ocr
(box_img)¶ Use pytesseract to retrieve FID from the strip box image.
- Parameters
box_img – Image of the QR code box.
- Returns
fid_tesseract: FID detected by tesseract from image using OCR.
-
pypocquant.lib.barcode.
try_extracting_barcode_from_box_with_rotations
(box, scaling=(1.0, 0.5, 0.25), verbose=False, log_list=None)¶ Try extracting barcode from QR code box while scaling it for different orientations [0, 90, 180, -90].
- Parameters
box – QR code box
scaling – Scaling factors.
verbose – Display additional logging information to the console.
log_list – Log list.
- Returns
fid: FID number
- Returns
log_list Appended Log list with current log information.
-
pypocquant.lib.barcode.
try_extracting_barcode_with_rotation
(image, angle_range=15, verbose=True, log_list: list = None)¶ Try extracting barcode from QR code box for a list of angles in the range of angle_range.
- Parameters
image – Input image
angle_range (int) – Range of angles to rotate input images in degrees.
verbose – Display additional logging information to the console.
log_list – Log list.
- Returns
fid: Extracted FID
- Returns
angle: Rotation angle that led to FID detection
- Returns
log_list: Appended log list.
-
pypocquant.lib.barcode.
find_strip_box_from_barcode_data_fh
(image, barcode_data, qr_code_border=30, qc=False)¶ Extract the box around the strip using the QR barcode data.
- Parameters
image – Strip image.
barcode_data – Barcode data.
qr_code_border – Border around QR codes.
qc – Bool, if true quality control image will be saved.
- Returns
box: Strip box.
- Returns
qr_code_size: The size of the QR codes (qr_code_width, qr_code_height).
- Returns
qc_image: Quality control image.
- Returns
box_rect: Rectangle of the QR box.
-
pypocquant.lib.barcode.
find_strip_box_from_barcode_data
(image, barcode_data, qr_code_border=30, qr_code_spacer=40, barcode_border=80, qc=False)¶ Extract the box around the strip using the QR barcode data.
- Parameters
image – Input image.
barcode_data – Barcode data
qr_code_border – Border around QR code on image.
qr_code_spacer – Spacer around QR code.
barcode_border – Border around barcode such as CODE128.
qc – Bool, if true quality control image will be saved.
- Returns
box QR code box around strip
- Returns
x_barcode Return the (x) coordinate of the left edge of the barcode rectangle.
- Returns
qr_code_size: The size of the QR codes (qr_code_width, qr_code_height).
- Returns
qc_image Quality control image.
-
pypocquant.lib.barcode.
try_extracting_barcode_with_linear_stretch
(image, lower_bound_range=(25), upper_bound_range=(98))¶ Try to extract the barcodes from the image by rescaling the intensity of the image with a linear stretch.
- Parameters
image – Input image
lower_bound_range – Lower bound range.
lower_bound_range – tuple
upper_bound_range – Upper bound range.
upper_bound_range – tuple
- Returns
””
- Returns
gray
-
pypocquant.lib.barcode.
try_getting_fid_from_code128_barcode
(barcode_data)¶ Try finding a CODE 128 barcode in barcode data that should contain the patient FID.
- Parameters
barcode_data – Barcode data
- Returns
barcode: Decoded CODE128 barcode.
-
pypocquant.lib.barcode.
try_get_fid_from_rgb
(image)¶ Extract FID from rgb image.
- Parameters
image – RGB image with FID.
- Returns
fid: Detected FID as string.
-
pypocquant.lib.barcode.
try_extracting_fid_and_all_barcodes_with_linear_stretch_fh
(image, lower_bound_range=(0, 5, 15, 25, 35), upper_bound_range=(100, 98, 95, 92, 89), scaling=(1.0))¶ Try extracting the fid and all barcodes from the image by rescaling the intensity of the image with a linear stretch.
- Parameters
image – Input image
lower_bound_range – Lower bound range.
lower_bound_range – tuple
upper_bound_range – Upper bound range.
upper_bound_range – tuple
scaling – Scaling factor
scaling – tuple
- Returns
barcodes: Barcode object
- Returns
fid: FID number
- Returns
manufacturer: Manufacturer name.
- Returns
plate: Plate info.
- Returns
well: Well info.
- Returns
user: Additional user data.
- Returns
best_lb: Best lower bound.
- Returns
best_ub: Best upper bound
- Returns
best_score: Best score.
- Returns
best_scaling_factor: Best scaling factor
- Returns
fid_128: FID 128 code.
-
pypocquant.lib.barcode.
try_extracting_all_barcodes_with_linear_stretch
(image, lower_bound_range=(0, 5, 15, 25, 35), upper_bound_range=(100, 98, 95, 92, 89))¶ Try extracting the fid and all barcodes from the image by rescaling the intensity of the image with a linear stretch.
- Parameters
image – Input image.
lower_bound_range – Lower bound range.
lower_bound_range – tuple
upper_bound_range – Upper bound range.
upper_bound_range – tuple
- Returns
best_barcode_data
- Returns
best_lb
- Returns
best_ub
- Returns
best_score
-
pypocquant.lib.barcode.
rotate_if_needed_fh
(image, barcode_data, image_log, verbose=True)¶ Rotate the image if the orientation is not the expected one.
- Parameters
image – Input image.
barcode_data – Barcode data.
image_log – Image log list.
verbose (bool) – Bool, if true displays additional information to the console.
- Returns
image_was_rotated: Bool, true if image was rotated.
- Return type
image_was_rotated: bool
- Returns
image: Rotated image.
- Return type
tuple
-
pypocquant.lib.barcode.
rotate_if_needed
(image, barcode_data, image_log, verbose=True)¶ Rotate the image if the orientation is not the expected one.
- Parameters
image – Input image.
barcode_data – Barcode data.
image_log – Image log list.
verbose (bool) – Bool, if true displays additional information to the console.
- Returns
image_was_rotated: Bool, true if image was rotated.
- Return type
image_was_rotated: bool
- Returns
image: Rotated image.
- Returns
image_log: Log for this image
- Return type
tuple
-
pypocquant.lib.barcode.
pick_FID_from_candidates
(fid_pyzbar, fid_tesseract)¶ Selection of FID from candidates depending on if candidates contain a FID.
- Parameters
fid_pyzbar – FID string determined with pyzbar.
fid_tesseract – FID string determined with tesseract.
- Returns
fid FID number
- Returns
score Score for the candidate determination.
-
pypocquant.lib.barcode.
mask_strip
(strip_gray, x_barcode, qr_code_extents)¶ Hide the barcode on the strip image.
- Parameters
strip_gray – Image of the strip (POCT).
x_barcode – X coordinate of the barcode on the strip.
qr_code_extents – QR code extents on the strip.
- Returns
strip_gray_masked Strip with QR code masked away.
- Returns
background_value Background value used for strip masking.
-
pypocquant.lib.barcode.
extract_strip_from_box
(box, qr_code_width, qr_code_height, qr_code_spacer=40, slack=0)¶ Extract the strip from the strip box.
- Parameters
box – Image of the QR code box.
qr_code_width – Width ot the QR code
qr_code_height – Height ot the QR code
qr_code_spacer – Horizontal and vertical distance between the internal edge of the QR codes and the beginning of the strip.
slack – Some buffer (subtracted from qr_code_spacer) to avoid cropping into the strip
- Returns
strip Returns the extracted POCT strip as image matrix.
-
pypocquant.lib.barcode.
get_fid_numeric_value_fh
(fid)¶ Return the numeric value of the FID (as string).
A FID could be in the form ‘F0123456’. We want to preserve the leading 0 after we removed the ‘F’.
- Parameters
fid (str) – FID number
- Returns
fid:
-
pypocquant.lib.barcode.
get_fid_numeric_value
(fid)¶ Return the numeric value of the FID.
- Parameters
fid (str) – FID number
- Returns
filtered_fid: FID number as numeric
-
pypocquant.lib.barcode.
get_box_rotation_angle
(pt1, pt2, pt3)¶ Determine the the QR code box rotation angle
- Parameters
pt1 – Coordinate corner 1
pt2 – Coordinate corner 2
pt3 – Coordinate corner 3
- Returns
rot_angle Rotation angle in degree.
-
pypocquant.lib.barcode.
align_box_with_image_border_fh
(barcode_data, image)¶ Method to align QR code box with image border of the full image (old pipeline).
- Parameters
barcode_data – QR code data
image – Image
- Returns
image_rotated: Rotated image
- Returns
angle Rotation angle in degrees.
-
pypocquant.lib.barcode.
align_box_with_image_border
(barcode_data, image)¶ Method to align QR code box with image border of the full image.
- Parameters
barcode_data – QR code data
image – Image
- Returns
image_rotated: Rotated image
- Returns
angle Rotation angle in degrees.