How to Scan Barcodes in Jupyter Notebook

Pre-requisites

Creating a Jupyter Notebook for Python Barcode Scanning

  1. Run Jupyter notebook:
!pip install dbr opencv-python matplotlib
from dbr import *
license_key = "LICENSE-KEY"
reader = BarcodeReader()
reader.init_license(license_key)
from ipywidgets import FileUpload
def on_upload_change(change):
if not change.new:
return
up = change.owner

up.value.clear()

uploader = FileUpload(accept='image/*', multiple=False)
uploader.observe(on_upload_change)
uploader
import cv2 as cv
import numpy as np

up = change.owner
for _, data in up.value.items():
image_buffer = np.frombuffer(data['content'], dtype=np.uint8)
img = cv.imdecode(image_buffer, 1)


up.value.clear()
def decode(frame):

before = time.time()
results = reader.decode_buffer(frame)
after = time.time()

COLOR_RED = (0,0,255)
thickness = 2
margin = 1
text_x = 10; text_y = 20
if results != None:
found = len(results)
for result in results:
print("Format: %s, Text: %s" % (result.barcode_format_string, result.barcode_text))
text = result.barcode_text
points = result.localization_result.localization_points
data = np.array([[points[0][0], points[0][1]], [points[1][0], points[1][1]], [points[2][0], points[2][1]], [points[3][0], points[3][1]]])
cv.drawContours(image=frame, contours=[data], contourIdx=-1, color=COLOR_RED, thickness=thickness, lineType=cv.LINE_AA)

cv.putText(frame, '%.2f s, barcode found: %d' % (after - before, found), (text_x, text_y), cv.FONT_HERSHEY_SIMPLEX, 0.5, COLOR_RED)
else:
cv.putText(frame, '%.2f s, barcode found: %d' % (after - before, 0), (text_x, text_y), cv.FONT_HERSHEY_SIMPLEX, 0.5, COLOR_RED)

img = cv.imdecode(image_buffer, 1)

new_img = img.copy()
# barcode recognition
decode(new_img)
import matplotlib.pyplot as plt

def show_image(img1, img2):
fig = plt.figure(figsize=(25, 10))
ax1 = fig.add_subplot(1, 2, 1)
plt.title('Input image', fontsize=16)
ax1.axis('off')
ax2 = fig.add_subplot(1, 2, 2)
plt.title('Barcode Recognition', fontsize=16)
ax2.axis('off')
ax1.imshow(img1)
ax2.imshow(img2)

img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
new_img = cv.cvtColor(new_img, cv.COLOR_BGR2RGB)
show_image(img, new_img)

Source Code

--

--

--

Manager of Dynamsoft Open Source Projects | Tech Lover

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Airbyte ETL in real cases

How to add a dynamic delivery calculator to your food ordering

Singleton in Kotlin Part II

Continuous Integration with Quality

The Impact Automation Has on Brick-and-Mortar Roles

Case Study | Kubernetes

Why capacity management is more relevant than ever in the cloud era

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Xiao Ling

Xiao Ling

Manager of Dynamsoft Open Source Projects | Tech Lover

More from Medium

Easier Python Package Deploying

Deploy your dashboard app (Plotly Dash — Python) to Heroku with the container registry way

Simple Audio Compression With Python

Integrate Python and Oracle Databases