Opencv最强案例——使用摄像头、OpenCV和Python扫描条形码和二维码。

barcodeTasks = deque()

注意:如果您使用所有CPU核心,CPU使用率将会很高。

  1. 创建一个任务功能,从网络摄像头视频帧中检测条形码和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

  1. 运行条形码和QR码扫描仪:

Dynamsoft条形码阅读器可以从一幅图像中检测多个条形码和QR码。然而,图像质量影响检测精度。正如你在上面的图像中看到的,为了捕捉所有的条形码和二维码,我们需要增加镜头的景深。这样,条形码和二维码可能会变得太小而无法读取。为了解决这一问题,我们将摄像头拉近以获得高质量的扫描图像,然后使用OpenCV拼接API将多个条形码和二维码图像拼接成一幅全景图。

将多个条形码和QR码图像拼接成全景图

OpenCV存储库包含一个stitching.py展示如何使用OpenCV缝合器API的文件。

要实现全景拼接:

  1. 初始化stitcher对象:

modes = (cv.Stitcher_PANORAMA, cv.Stitcher_SCANS)

stitcher = cv.Stitcher.create(modes[1])

stitcher.setPanoConfidenceThresh(0.5)

  1. 为拼接包含条形码和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)

return None

except BarcodeReaderError as e:

print(e)

return None

return None

while len(panoramaTask) > 0 and panoramaTask[0].ready():

image = panoramaTask.popleft().get()

if image is not None:

cv.imshow(‘panorama’, image)

if len(panoramaTask) < threadn:

task = panoramaPool.apply_async(self.stitch_frame, (frame_cp, ))

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注:Python)

g](https://img-blog.csdnimg.cn/img_convert/9f49b566129f47b8a67243c1008edf79.png)

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注:Python)

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值