- 使用您想要使用的进程数量创建一个线程池:
threadn = 1 # cv.getNumberOfCPUs()
pool = ThreadPool(processes = threadn)
barcodeTasks = deque()
注意:如果您使用所有CPU核心,CPU使用率将会很高。
- 创建一个任务功能,从网络摄像头视频帧中检测条形码和QR码:
def process_frame(frame):
results = None
try:
results = reader.decode_buffer(frame)
except BarcodeReaderError as bre:
print(bre)
return results
while True:
ret, frame = cap.read()
while len(barcodeTasks) > 0 and barcodeTasks[0].ready():
results = barcodeTasks.popleft().get()
if results != None:
for result in results:
points = result.localization_result.localization_points
cv.line(frame, points[0], points[1], (0,255,0), 2)
cv.line(frame, points[1], points[2], (0,255,0), 2)
cv.line(frame, points[2], points[3], (0,255,0), 2)
cv.line(frame, points[3], points[0], (0,255,0), 2)
cv.putText(frame, result.barcode_text, points[0], cv.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255))
if len(barcodeTasks) < threadn:
task = pool.apply_async(process_frame, (frame.copy(), ))
barcodeTasks.append(task)
cv.imshow(‘Barcode & QR Code Scanner’, frame)
ch = cv.waitKey(1)
if ch == 27:
break
- 运行条形码和QR码扫描仪:
Dynamsoft条形码阅读器可以从一幅图像中检测多个条形码和QR码。然而,图像质量影响检测精度。正如你在上面的图像中看到的,为了捕捉所有的条形码和二维码,我们需要增加镜头的景深。这样,条形码和二维码可能会变得太小而无法读取。为了解决这一问题,我们将摄像头拉近以获得高质量的扫描图像,然后使用OpenCV拼接API将多个条形码和二维码图像拼接成一幅全景图。
将多个条形码和QR码图像拼接成全景图
OpenCV存储库包含一个stitching.py展示如何使用OpenCV缝合器API的文件。
要实现全景拼接:
- 初始化stitcher对象:
modes = (cv.Stitcher_PANORAMA, cv.Stitcher_SCANS)
stitcher = cv.Stitcher.create(modes[1])
stitcher.setPanoConfidenceThresh(0.5)
- 为拼接包含条形码和QR码的图像创建新的任务功能:
panoramaPool = ThreadPool(processes = threadn)
panoramaTask = deque()
def stitch_frame(self, frame):
try:
results = self.reader.decode_buffer(frame)
if results != None:
for result in results:
points = result.localization_result.localization_points
cv.line(frame, points[0], points[1], (0,255,0), 2)
cv.line(frame, points[1], points[2], (0,255,0), 2)
cv.line(frame, points[2], points[3], (0,255,0), 2)
cv.line(frame, points[3], points[0], (0,255,0), 2)
cv.putText(frame, result.barcode_text, points[0], cv.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255))
self.panorama.append((frame, len(results)))
print(‘Stitching …’)
try:
all_images = [frame for frame, count in self.panorama]
status, image = self.stitcher.stitch(all_images)
if status != cv.Stitcher_OK:
print(“Can’t stitch images, error code = %d” % status)
return self.panorama[0][0]
else:
Stop stitching if the output image is out of control
if image.shape[0] >= frame.shape[0] * 1.5:
self.isPanoramaDone = True
self.save_frame(all_images[0])
print(‘Stitching is done…’)
return None
Drop the stitched image if its quality is not good enough
total = 0
for frame, count in self.panorama:
total += count
count_stitch = self.count_barcodes(image)
if count_stitch > total or count_stitch < self.panorama[0][1]:
return self.panorama[0][0]
Wait for the next stitching and return the current stitched image
self.panorama = [(image, count_stitch)]
return image
except Exception as e:
print(e)
学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!
一、Python所有方向的学习路线
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
二、学习软件
工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。
三、全套PDF电子书
书籍的好处就在于权威和体系健全,刚开始学习的时候你可以只看视频或者听某个人讲课,但等你学完之后,你觉得你掌握了,这时候建议还是得去看一下书籍,看权威技术书籍也是每个程序员必经之路。
四、入门学习视频
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。
五、实战案例
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
六、面试资料
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!