Skip to content

Commit e3bbc1e

Browse files
authored
Merge pull request #1 from dynamsoft-dbr/Python-DBR7.2.2-Zoro
Python dbr7.2.2
2 parents fe70de8 + 864ba87 commit e3bbc1e

27 files changed

+9683
-1
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
*.exp
3+
*.pyd
4+
*.lib
5+
*.obj

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: 415 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)

examples/camera/camera-decodevideo.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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 = 0
25+
video_height = 0
26+
testVideo = r"D:\Work\TestVideoOrImage\20191202145135.mp4"
27+
28+
vc = cv2.VideoCapture(testVideo)
29+
# vc = cv2.VideoCapture(0)
30+
video_width = vc.get(cv2.CAP_PROP_FRAME_WIDTH)
31+
video_height = vc.get(cv2.CAP_PROP_FRAME_HEIGHT)
32+
vc.set(3, video_width) #set width
33+
vc.set(4, video_height) #set height
34+
35+
stride = 0
36+
if vc.isOpened():
37+
dbr.InitLicense('Input your license')
38+
rval, frame = vc.read()
39+
stride = frame.strides[0]
40+
else:
41+
return
42+
43+
windowName = "Barcode Reader"
44+
parameters = dbr.InitFrameDecodingParameters()
45+
parameters["MaxQueueLength"] = 30
46+
parameters["MaxResultQueueLength"] = 30
47+
parameters["Width"] = video_width
48+
parameters["Height"] = video_height
49+
parameters["Stride"] = stride
50+
parameters["ImagePixelFormat"] = dbr.IPF_RGB_888
51+
parameters["RegionTop"] = 0
52+
parameters["RegionLeft"] = 0
53+
parameters["RegionRight"] = 100
54+
parameters["RegionBottom"] = 100
55+
parameters["RegionMeasuredByPercentage"] = 1
56+
parameters["Threshold"] = 0.01
57+
parameters["FPS"] = 0
58+
59+
60+
dbr.StartVideoMode(parameters, onBarcodeResult)
61+
62+
count = 0
63+
while True:
64+
if results != None:
65+
# thickness = 2
66+
# color = (0,255,0)
67+
for result in results:
68+
# print("barcode format: " + result[0])
69+
# print("barcode text: " + result[1])
70+
print("barcode format: " + str(result["BarcodeFormat"]))
71+
print("barcode format: " + result["BarcodeFormatString"])
72+
print("barcode text: " + result["BarcodeText"])
73+
localizationResult = result["LocalizationResult"]
74+
x1 = localizationResult["X1"]
75+
y1 = localizationResult["Y1"]
76+
x2 = localizationResult["X2"]
77+
y2 = localizationResult["Y2"]
78+
x3 = localizationResult["X3"]
79+
y3 = localizationResult["Y3"]
80+
x4 = localizationResult["X4"]
81+
y4 = localizationResult["Y4"]
82+
localizationPoints = [(x1,y1),(x2,y2),(x3,y3),(x4,y4)]
83+
print(localizationPoints)
84+
85+
# cv2.line(frame, (x1, y1), (x2, y2), color, thickness)
86+
# cv2.line(frame, (x2, y2), (x3, y3), color, thickness)
87+
# cv2.line(frame, (x3, y3), (x4, y4), color, thickness)
88+
# cv2.line(frame, (x4, y4), (x1, y1), color, thickness)
89+
results = None
90+
91+
rval, frame = vc.read()
92+
if rval == False:
93+
break
94+
# cv2.imshow(windowName, frame)
95+
96+
# if count == 1000:
97+
# break
98+
# else:
99+
# count = count + 1
100+
# print(count)
101+
# start = time.time()
102+
try:
103+
ret = dbr.AppendVideoFrame(frame)
104+
except:
105+
pass
106+
107+
108+
# cost = (time.time() - start) * 1000
109+
# print('time cost: ' + str(cost) + ' ms')
110+
# 'ESC' for quit
111+
# key = cv2.waitKey(1)
112+
# if key == 27:
113+
# break
114+
115+
dbr.StopVideoMode()
116+
# cv2.destroyWindow(windowName)
117+
118+
119+
if __name__ == "__main__":
120+
print("OpenCV version: " + cv2.__version__)
121+
read_barcode()
122+
print("over")
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
29+
results = dbr.DecodeBuffer(inputframe, inputframe.shape[0], inputframe.shape[1], inputframe.strides[0])
30+
# textResults is a list object, the following program will output each whole text result.
31+
# if you just want some individual results in textResult, you can get all keys in text result and get the value by the key.
32+
textResults = results["TextResults"]
33+
intermediateResults = results["IntermediateResults"]
34+
for textResult in textResults:
35+
# print(textResult["BarcodeFormat"])
36+
print(textResult["BarcodeFormatString"])
37+
print(textResult["BarcodeText"])
38+
# print(textResult["BarcodeBytes"])
39+
# # LocalizationResult is a dictionary object, you can use the same method as textResult to get the key-value.
40+
# print(textResult["LocalizationResult"])
41+
# # DetailedResult is a dictionary object, you can use the same method as textResult to get the key-value.
42+
# print(textResult["DetailedResult"])
43+
# # ExtendedResults is a list object , and each item of it is a dictionary object.
44+
# extendedResults = textResult["ExtendedResults"]
45+
# for extendedResult in extendedResults:
46+
# print(extendedResult)
47+
# intermediateResults is a list object, the following program will output each whole intermediate result.
48+
# if you just want some individual results in intermediateResult, you can get all keys in intermediateResult and get the value by the key.
49+
# for intermediateResult in intermediateResults:
50+
# print(intermediateResult)
51+
# print(intermediateResult.keys())
52+
except:
53+
pass
54+
55+
print("Detection is done.")
56+
clear_queue(frame_queue)
57+
clear_queue(finish_queue)
58+
59+
60+
def get_time():
61+
localtime = time.localtime()
62+
capturetime = time.strftime("%Y%m%d%H%M%S", localtime)
63+
return capturetime
64+
65+
66+
def read_barcode():
67+
frame_queue = Queue(4)
68+
finish_queue = Queue(1)
69+
70+
dbr_proc = Process(target=dbr_run, args=(
71+
frame_queue, finish_queue))
72+
dbr_proc.start()
73+
74+
vc = cv2.VideoCapture(0)
75+
# vc.set(5, 30) #set FPS
76+
vc.set(3, 640) #set width
77+
vc.set(4, 480) #set height
78+
79+
if vc.isOpened(): # try to get the first frame
80+
rval, frame = vc.read()
81+
else:
82+
return
83+
84+
windowName = "Barcode Reader"
85+
base = 2
86+
count = 0
87+
while True:
88+
cv2.imshow(windowName, frame)
89+
rval, frame = vc.read()
90+
91+
count %= base
92+
if count == 0:
93+
try:
94+
frame_queue.put_nowait(frame)
95+
except:
96+
try:
97+
while True:
98+
frame_queue.get_nowait()
99+
except:
100+
pass
101+
102+
count += 1
103+
104+
# 'ESC' for quit
105+
key = cv2.waitKey(20)
106+
if key == 27:
107+
finish_queue.put(True)
108+
109+
dbr_proc.join()
110+
break
111+
112+
cv2.destroyWindow(windowName)
113+
114+
115+
if __name__ == "__main__":
116+
print("OpenCV version: " + cv2.__version__)
117+
read_barcode()

examples/command-line/test.py

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

0 commit comments

Comments
 (0)