Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Barcode Scanning with OpenCV, Webcam and Dynamsoft Barcode Reader
#!/usr/bin/env python3
'''
Usage:
barcode_scanning.py <license.txt>
Keyboard shortcuts:
ESC - exit
'''
from dbr import *
import numpy as np
import time
import cv2 as cv
from multiprocessing import Process, Queue
def process_barcode_frame(license, frameQueue, resultQueue):
# Create Dynamsoft Barcode Reader
reader = BarcodeReader()
# Apply for a trial license: https://www.dynamsoft.com/customer/license/trialLicense
reader.init_license(license)
settings = reader.get_runtime_settings()
settings.max_algorithm_thread_count = 1
reader.update_runtime_settings(settings)
while True:
results = None
try:
frame = frameQueue.get(False, 10)
if type(frame) is str:
break
except:
continue
try:
frameHeight, frameWidth, channel = frame.shape[:3]
# results = reader.decode_buffer(frame)
results = reader.decode_buffer_manually(np.array(frame).tobytes(), frameWidth, frameHeight, frame.strides[0], EnumImagePixelFormat.IPF_RGB_888)
except BarcodeReaderError as error:
print(error)
try:
resultQueue.put(results, False, 10)
except:
pass
def main():
import sys
try:
with open(sys.argv[1]) as f:
license = f.read()
except:
license = ""
reader = BarcodeReader()
# # Apply for a trial license: https://www.dynamsoft.com/customer/license/trialLicense
reader.init_license(license)
# cap = cv.VideoCapture('dbr.mp4')
cap = cv.VideoCapture(0)
# cap.set(cv.CAP_PROP_FRAME_WIDTH, 480)
# cap.set(cv.CAP_PROP_FRAME_HEIGHT, 320)
# Initialize two queues
size = 1
frameQueue = Queue(size)
resultQueue = Queue(size)
# Start barcode scanning process
barcodeScanning = Process(target=process_barcode_frame, args=(license, frameQueue, resultQueue))
barcodeScanning.start()
thickness = 2
color = (0,255,0)
results = None
while True:
_ret, frame = cap.read()
if not _ret:
break
try:
results = resultQueue.get(False, 10)
except:
pass
if results != None:
for result in results:
print("Barcode Format :")
print(result.barcode_format_string)
print("Barcode Text :")
print(result.barcode_text)
print("Localization Points : ")
print(result.localization_result.localization_points)
print("-------------")
points = result.localization_result.localization_points
cv.line(frame, points[0], points[1], color, thickness)
cv.line(frame, points[1], points[2], color, thickness)
cv.line(frame, points[2], points[3], color, thickness)
cv.line(frame, points[3], points[0], color, thickness)
cv.putText(frame, result.barcode_text, points[0], cv.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255))
cv.imshow('BarcodeReader', frame)
break
cv.imshow('BarcodeReader', frame)
# Append a frame to the frame queue
try:
frameQueue.put(frame.copy(), False, 10)
except:
pass
ch = cv.waitKey(1)
# ESC
if ch == 27:
break
frameQueue.put("")
barcodeScanning.join()
frameQueue.close()
resultQueue.close()
cv.waitKey(0)
print('Done')
if __name__ == '__main__':
print(__doc__)
main()
cv.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment