Skip to content

Commit 64932eb

Browse files
committed
first draft
this python version is matching with DBR7.2.2
1 parent fe70de8 commit 64932eb

18 files changed

Lines changed: 8831 additions & 1 deletion

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Dynamsoft Barcode Reader
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 426 additions & 1 deletion
Large diffs are not rendered by default.

examples/camera/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## Blog
2+
- [How to Use Multiprocessing to Optimize Python Barcode Reader](https://www.codepool.biz/multiprocessing-optimize-python-barcode-reader.html)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import cv2
2+
from dbr import DynamsoftBarcodeReader
3+
dbr = DynamsoftBarcodeReader()
4+
import time
5+
import os
6+
7+
import sys
8+
sys.path.append('../')
9+
# import config
10+
11+
results = None
12+
# The callback function for receiving barcode results
13+
def onBarcodeResult(data):
14+
global results
15+
results = data
16+
17+
def get_time():
18+
localtime = time.localtime()
19+
capturetime = time.strftime("%Y%m%d%H%M%S", localtime)
20+
return capturetime
21+
22+
def read_barcode():
23+
global results
24+
video_width = 640
25+
video_height = 480
26+
27+
vc = cv2.VideoCapture(0)
28+
vc.set(3, video_width) #set width
29+
vc.set(4, video_height) #set height
30+
31+
if vc.isOpened():
32+
dbr.InitLicense('LICENSE-KEY')
33+
rval, frame = vc.read()
34+
else:
35+
return
36+
37+
windowName = "Barcode Reader"
38+
39+
max_buffer = 2
40+
max_results = 10
41+
image_format = 1 # 0: gray; 1: rgb888
42+
43+
dbr.StartVideoMode(max_buffer, max_results, video_width, video_height, image_format, onBarcodeResult)
44+
45+
while True:
46+
if results != None:
47+
thickness = 2
48+
color = (0,255,0)
49+
for result in results:
50+
print("barcode format: " + result["BarcodeFormatString"])
51+
print("barcode text: " + result["BarcodeText"])
52+
localizationResult = result["LocalizationResult"]
53+
x1 = localizationResult["X1"]
54+
y1 = localizationResult["Y1"]
55+
x2 = localizationResult["X2"]
56+
y2 = localizationResult["Y2"]
57+
x3 = localizationResult["X3"]
58+
y3 = localizationResult["Y3"]
59+
x4 = localizationResult["X4"]
60+
y4 = localizationResult["Y4"]
61+
62+
cv2.line(frame, (x1, y1), (x2, y2), color, thickness)
63+
cv2.line(frame, (x2, y2), (x3, y3), color, thickness)
64+
cv2.line(frame, (x3, y3), (x4, y4), color, thickness)
65+
cv2.line(frame, (x4, y4), (x1, y1), color, thickness)
66+
67+
results = None
68+
69+
cv2.imshow(windowName, frame)
70+
rval, frame = vc.read()
71+
72+
# start = time.time()
73+
try:
74+
ret = dbr.AppendVideoFrame(frame)
75+
except:
76+
pass
77+
78+
# cost = (time.time() - start) * 1000
79+
# print('time cost: ' + str(cost) + ' ms')
80+
81+
# 'ESC' for quit
82+
key = cv2.waitKey(1)
83+
if key == 27:
84+
break
85+
86+
dbr.StopVideoMode()
87+
cv2.destroyWindow(windowName)
88+
89+
90+
if __name__ == "__main__":
91+
print("OpenCV version: " + cv2.__version__)
92+
read_barcode()
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import cv2
2+
from dbr import DynamsoftBarcodeReader
3+
dbr = DynamsoftBarcodeReader()
4+
import time
5+
import os
6+
from multiprocessing import Process, Queue
7+
8+
import sys
9+
sys.path.append('../')
10+
# import config
11+
12+
13+
def clear_queue(queue):
14+
try:
15+
while True:
16+
queue.get_nowait()
17+
except:
18+
pass
19+
queue.close()
20+
queue.join_thread()
21+
22+
23+
def dbr_run(frame_queue, finish_queue):
24+
dbr.InitLicense("Input your own license")
25+
while finish_queue.qsize() == 0:
26+
try:
27+
inputframe = frame_queue.get_nowait()
28+
results = dbr.DecodeBuffer(inputframe)
29+
# textResults is a list object, the following program will output each whole text result.
30+
# if you just want some individual results in textResult, you can get all keys in text result and get the value by the key.
31+
textResults = results["TextResults"]
32+
intermediateResults = results["IntermediateResults"]
33+
for textResult in textResults:
34+
print(textResult)
35+
print(textResult.keys())
36+
# print(textResult["BarcodeFormat"])
37+
# print(textResult["BarcodeFormatString"])
38+
# print(textResult["BarcodeText"])
39+
# print(textResult["BarcodeBytes"])
40+
# # LocalizationResult is a dictionary object, you can use the same method as textResult to get the key-value.
41+
# print(textResult["LocalizationResult"])
42+
# # DetailedResult is a dictionary object, you can use the same method as textResult to get the key-value.
43+
# print(textResult["DetailedResult"])
44+
# # ExtendedResults is a list object , and each item of it is a dictionary object.
45+
# extendedResults = textResult["ExtendedResults"]
46+
# for extendedResult in extendedResults:
47+
# print(extendedResult)
48+
# intermediateResults is a list object, the following program will output each whole intermediate result.
49+
# if you just want some individual results in intermediateResult, you can get all keys in intermediateResult and get the value by the key.
50+
for intermediateResult in intermediateResults:
51+
print(intermediateResult)
52+
print(intermediateResult.keys())
53+
except:
54+
pass
55+
56+
print("Detection is done.")
57+
clear_queue(frame_queue)
58+
clear_queue(finish_queue)
59+
60+
61+
def get_time():
62+
localtime = time.localtime()
63+
capturetime = time.strftime("%Y%m%d%H%M%S", localtime)
64+
return capturetime
65+
66+
67+
def read_barcode():
68+
frame_queue = Queue(4)
69+
finish_queue = Queue(1)
70+
71+
dbr_proc = Process(target=dbr_run, args=(
72+
frame_queue, finish_queue))
73+
dbr_proc.start()
74+
75+
vc = cv2.VideoCapture(0)
76+
# vc.set(5, 30) #set FPS
77+
vc.set(3, 640) #set width
78+
vc.set(4, 480) #set height
79+
80+
if vc.isOpened(): # try to get the first frame
81+
rval, frame = vc.read()
82+
else:
83+
return
84+
85+
windowName = "Barcode Reader"
86+
base = 2
87+
count = 0
88+
while True:
89+
cv2.imshow(windowName, frame)
90+
rval, frame = vc.read()
91+
92+
count %= base
93+
if count == 0:
94+
try:
95+
frame_queue.put_nowait(frame)
96+
except:
97+
try:
98+
while True:
99+
frame_queue.get_nowait()
100+
except:
101+
pass
102+
103+
count += 1
104+
105+
# 'ESC' for quit
106+
key = cv2.waitKey(20)
107+
if key == 27:
108+
finish_queue.put(True)
109+
110+
dbr_proc.join()
111+
break
112+
113+
cv2.destroyWindow(windowName)
114+
115+
116+
if __name__ == "__main__":
117+
print("OpenCV version: " + cv2.__version__)
118+
read_barcode()

examples/command-line/test.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import os
2+
import json
3+
from dbr import DynamsoftBarcodeReader
4+
dbr = DynamsoftBarcodeReader()
5+
# print(dir(dbr))
6+
7+
import cv2
8+
9+
import sys
10+
sys.path.append('../')
11+
# import config
12+
13+
def InitLicense(license):
14+
dbr.InitLicense(license)
15+
16+
def DecodeFile(fileName, templateName = ""):
17+
try:
18+
19+
# results is a dictionary object which includes TextResults and IntermediateResults.
20+
results = dbr.DecodeFile(fileName, templateName)
21+
# textResults is a list object, the following program will output each whole text result.
22+
# if you just want some individual results in textResult, you can get all keys in text result and get the value by the key.
23+
textResults = results["TextResults"]
24+
intermediateResults = results["IntermediateResults"]
25+
for textResult in textResults:
26+
print(textResult)
27+
print(textResult.keys())
28+
# print(textResult["BarcodeFormat"])
29+
# print(textResult["BarcodeFormatString"])
30+
# print(textResult["BarcodeText"])
31+
# print(textResult["BarcodeBytes"])
32+
# # LocalizationResult is a dictionary object, you can use the same method as textResult to get the key-value.
33+
# print(textResult["LocalizationResult"])
34+
# # DetailedResult is a dictionary object, you can use the same method as textResult to get the key-value.
35+
# print(textResult["DetailedResult"])
36+
# # ExtendedResults is a list object , and each item of it is a dictionary object.
37+
# extendedResults = textResult["ExtendedResults"]
38+
# for extendedResult in extendedResults:
39+
# print(extendedResult)
40+
# intermediateResults is a list object, the following program will output each whole intermediate result.
41+
# if you just want some individual results in intermediateResult, you can get all keys in intermediateResult and get the value by the key.
42+
for intermediateResult in intermediateResults:
43+
print(intermediateResult)
44+
print(intermediateResult.keys())
45+
except Exception as err:
46+
print(err)
47+
48+
def DecodeBuffer(image, templateName = ""):
49+
results = dbr.DecodeBuffer(image, templateName)
50+
textResults = results["TextResults"]
51+
thickness = 2
52+
color = (0,255,0)
53+
for textResult in textResults:
54+
print("barcode format: " + textResult["BarcodeFormatString"])
55+
print("barcode text: " + textResult["BarcodeText"])
56+
localizationResult = textResult["LocalizationResult"]
57+
x1 = localizationResult["X1"]
58+
y1 = localizationResult["Y1"]
59+
x2 = localizationResult["X2"]
60+
y2 = localizationResult["Y2"]
61+
x3 = localizationResult["X3"]
62+
y3 = localizationResult["Y3"]
63+
x4 = localizationResult["X4"]
64+
y4 = localizationResult["Y4"]
65+
66+
cv2.line(image, (x1, y1), (x2, y2), color, thickness)
67+
cv2.line(image, (x2, y2), (x3, y3), color, thickness)
68+
cv2.line(image, (x3, y3), (x4, y4), color, thickness)
69+
cv2.line(image, (x4, y4), (x1, y1), color, thickness)
70+
71+
cv2.imshow("Localization", image)
72+
cv2.waitKey(0)
73+
74+
def DecodeFileStream(imagePath, templateName = ""):
75+
with open(imagePath, "rb") as fread:
76+
total = fread.read()
77+
results = dbr.DecodeFileStream(bytearray(total), len(total))
78+
textResults = results["TextResults"]
79+
for textResult in textResults:
80+
print("barcode format: " + textResult["BarcodeFormatString"])
81+
print("barcode text: " + textResult["BarcodeText"])
82+
83+
if __name__ == "__main__":
84+
print("OpenCV version: " + cv2.__version__)
85+
import sys
86+
barcode_image = ""
87+
if sys.version_info < (3, 0):
88+
barcode_image = raw_input("Enter the barcode file: ")
89+
else:
90+
barcode_image = input("Enter the barcode file: ")
91+
92+
if not os.path.isfile(barcode_image):
93+
print("It is not a valid file.")
94+
else:
95+
InitLicense("Input your license")
96+
# Get default barcode params
97+
params = dbr.GetRuntimeSettings()
98+
params["BarcodeFormatIds"] = dbr.BF_ONED | dbr.BF_QR_CODE
99+
# Set parameters
100+
ret = dbr.UpdataRuntimeSettings(params)
101+
102+
DecodeFile(barcode_image)
103+
image = cv2.imread(barcode_image, 1)
104+
DecodeBuffer(image)

examples/config.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import sys
2+
3+
license = ''
4+
if sys.platform == "linux" or sys.platform == "linux2":
5+
# linux
6+
license = 't0068NQAAAJUlQ1oDc6zPWxOAQWn7kD9EGtgZFIqK/k3ULJC5ccG9Xe/lpVOxod82bm6nXxqQXUpC1zjRXU514mWw9XLE1JM='
7+
elif sys.platform == "darwin":
8+
# OS X
9+
license = 't0068NQAAAKrcxSxZwCY7qwBNDJJXAG3rFcJZTDsCdTHB2TlI0f1DvBg34MazLhAqhf6D2iE60OnWk9imYMc0inxb9OXWcrY='
10+
elif sys.platform == "win32":
11+
# Windows
12+
license = 't0068NQAAAJwEbwJjt0+DiC2gJpQN4VQUhYTBmazlOU0RgWzDins2JUhtO6TK2Kj/Ck9+z5FlwuHn0KLK1NvkXYvThosPYog='
13+
14+
barcodeTypes = 0x3FF | 0x2000000 | 0x4000000 | 0x8000000 | 0x10000000 # 1D, PDF417, QRCODE, DataMatrix, Aztec Code

0 commit comments

Comments
 (0)