1+ import os
2+ import sys
3+ import time
4+ from dbr import *
5+ import cv2
6+
7+
8+ reader = BarcodeReader ()
9+
10+ results = None
11+
12+
13+ Template_Settings = {
14+ '1' : '{"ImageParameter":{'
15+ '"Name":"BestCoverage",'
16+ '"DeblurLevel":9,'
17+ '"ExpectedBarcodesCount":512,'
18+ '"ScaleDownThreshold":100000,'
19+ '"LocalizationModes":['
20+ '{"Mode":"LM_CONNECTED_BLOCKS"},'
21+ '{"Mode":"LM_SCAN_DIRECTLY"},'
22+ '{"Mode":"LM_STATISTICS"},'
23+ '{"Mode":"LM_LINES"},'
24+ '{"Mode":"LM_STATISTICS_MARKS"}],'
25+ '"GrayscaleTransformationModes":['
26+ '{"Mode":"GTM_ORIGINAL"},'
27+ '{"Mode":"GTM_INVERTED"}]'
28+ '}'
29+ '}' ,
30+ '2' : '{"ImageParameter":{'
31+ '"Name":"BestSpeed",'
32+ '"DeblurLevel":3,'
33+ '"ExpectedBarcodesCount":512,'
34+ '"LocalizationModes":['
35+ '{"Mode":"LM_SCAN_DIRECTLY"}],'
36+ '"TextFilterModes":['
37+ '{"MinImageDimension":262144,"Mode":"TFM_GENERAL_CONTOUR"}]'
38+ '}'
39+ '}' ,
40+ '3' : '{"ImageParameter":{'
41+ '"Name":"Balance",'
42+ '"DeblurLevel":5,'
43+ '"ExpectedBarcodesCount":512,'
44+ '"LocalizationModes":['
45+ '{"Mode":"LM_CONNECTED_BLOCKS"},'
46+ '{"Mode":"LM_STATISTICS"}]'
47+ '}'
48+ '}'
49+ }
50+
51+
52+ class SubTextResultResultCallBack (TextResultResultCallBack ):
53+ @staticmethod
54+ def text_results_callback_func (frame_id , t_results , user_data ):
55+ print (frame_id )
56+ global results
57+ results = t_results
58+
59+
60+ def init_runtime_settings ():
61+ while True :
62+ print ()
63+ print ("Step 1: Choose a template settings : " )
64+ print ("\t 1: Best Coverage Settings" )
65+ print ("\t 2: Best Speed Settings" )
66+ print ("\t 3: Balance Settings" )
67+ item = input ()
68+
69+ if str (item ) == 'q' or str (item ) == 'Q' :
70+ print ('Bye, looking forward to your next use.' )
71+ exit ()
72+
73+ if str (item ) not in Template_Settings .keys ():
74+ print ('Please choose a valid number.' )
75+ continue
76+ else :
77+ reader .init_runtime_settings_with_string (Template_Settings [item ])
78+ break
79+
80+
81+ def get_video_path ():
82+ while True :
83+ print ()
84+ print ("Step 2: Choose the way to get video : " )
85+ print ("\t 1: Camera" )
86+ print ("\t 2: Video file" )
87+ print ()
88+ item = input ()
89+
90+ if str (item ) is 'q' or str (item ) is 'Q' :
91+ print ('Bye, looking forward to your next use.' )
92+ exit ()
93+ elif str (item ) is not '1' and str (item ) is not '2' :
94+ print ('Please choose a valid number.' )
95+ continue
96+ else :
97+ if str (item ) is '1' :
98+ video_file = 0
99+ return video_file
100+ else :
101+ while True :
102+ print ()
103+ print ("Step 3: Input the path to the video file : " )
104+ print ()
105+ video_file = input ()
106+ if video_file == 'q' or video_file == 'Q' :
107+ print ('Bye, looking forward to your next use.' )
108+ exit ()
109+ if not os .path .exists (video_file ):
110+ print ("The video file doesn't exist , please input a valid path." )
111+ continue
112+ else :
113+ return video_file
114+
115+
116+ def read_barcode ():
117+ global results
118+ video_width = 0
119+ video_height = 0
120+
121+ video_file = get_video_path ()
122+
123+ vc = cv2 .VideoCapture (video_file )
124+ video_width = vc .get (cv2 .CAP_PROP_FRAME_WIDTH )
125+ video_height = vc .get (cv2 .CAP_PROP_FRAME_HEIGHT )
126+ vc .set (3 , video_width ) #set width
127+ vc .set (4 , video_height ) #set height
128+
129+ stride = 0
130+ if vc .isOpened ():
131+ rval , frame = vc .read ()
132+ stride = frame .strides [0 ]
133+ else :
134+ return
135+
136+ windowName = "Barcode Reader"
137+
138+ parameters = reader .init_frame_decoding_parameters ()
139+ # you can modify these following parameters.
140+ parameters .max_queue_length = 30
141+ parameters .max_result_queue_length = 30
142+ parameters .width = video_width
143+ parameters .height = video_height
144+ parameters .stride = stride
145+ parameters .image_pixel_format = EnumImagePixelFormat .IPF_RGB_888
146+ parameters .region_top = 0
147+ parameters .region_bottom = 100
148+ parameters .region_left = 0
149+ parameters .region_right = 100
150+ parameters .region_measured_by_percentage = 1
151+ parameters .threshold = 0.01
152+ parameters .fps = 0
153+ parameters .auto_filter = 1
154+
155+ reader .start_video_mode (parameters , SubTextResultResultCallBack .text_results_callback_func )
156+
157+ while True :
158+ if results != None :
159+ thickness = 2
160+ color = (0 ,255 ,0 )
161+ for result in results :
162+ text_result = TextResult (result )
163+ print ("Barcode Format : " )
164+ print (text_result .barcode_format_string )
165+ print ("Barcode Text : " )
166+ print (text_result .barcode_text )
167+ print ("Localization Points : " )
168+ print (text_result .localization_result .localization_points )
169+ print ("Exception : " )
170+ print (text_result .exception )
171+ print ("-------------" )
172+
173+ points = text_result .localization_result .localization_points
174+
175+ cv2 .line (frame , points [0 ], points [1 ], color , thickness )
176+ cv2 .line (frame , points [1 ], points [2 ], color , thickness )
177+ cv2 .line (frame , points [2 ], points [3 ], color , thickness )
178+ cv2 .line (frame , points [3 ], points [0 ], color , thickness )
179+ cv2 .putText (frame , text_result .barcode_text , points [0 ], cv2 .FONT_HERSHEY_SIMPLEX , 0.5 , (0 ,0 ,255 ))
180+ results = None
181+
182+ cv2 .imshow (windowName , frame )
183+ rval , frame = vc .read ()
184+ if rval == False :
185+ break
186+
187+ try :
188+ ret = reader .append_video_frame (frame )
189+ except :
190+ pass
191+
192+ # 'ESC' for quit
193+ key = cv2 .waitKey (1 )
194+ if key == 27 :
195+ break
196+
197+ reader .stop_video_mode ()
198+ cv2 .destroyWindow (windowName )
199+
200+
201+ if __name__ == '__main__' :
202+
203+ # you can change the following variables' value to your own value.
204+ license_key = "Input your own license"
205+
206+ # Apply for a trial license: https://www.dynamsoft.com/customer/license/trialLicense
207+ reader .init_license (license_key )
208+
209+ ## The code snippet below shows how to use the full license in DBR 8.x:
210+ # connection_paras = BarcodeReader.init_lts_connection_parameters()
211+ ## If DBR service is already built on your server, you can fill in the address of your server, or leave this property's default value.
212+ # connection_paras.main_server_url = "Input your own server url"
213+ # connection_paras.handshake_code = "Input your own handshake"
214+ # connection_paras.deployment_type = EnumDMDeploymentType.DM_DT_DESKTOP
215+ # connection_paras.uuid_generation_method = EnumDMUUIDGenerationMethod.DM_UUIDGM_RANDOM
216+ # try:
217+ # error = BarcodeReader.init_license_from_lts(connection_paras)
218+ # if error[0] != EnumErrorCode.DBR_OK:
219+ # print(error[1])
220+ # except BarcodeReaderError as bre:
221+ # print(bre)
222+
223+ print ("*************************************************" )
224+ print ("Welcome to Dynamsoft Barcode Reader DecodeVideoDemo" )
225+ print ("*************************************************" )
226+ print ("Hints: Please input 'Q'or 'q' to quit the application." )
227+ print ()
228+
229+ init_runtime_settings ()
230+
231+ read_barcode ()
232+ print ("-------------------over------------------------" )
0 commit comments