Python - Opencv + pyzbar实时摄像头识别二维码

直接上代码:

复制代码
import cv2
from pyzbar.pyzbar import decode

cap = cv2.VideoCapture(0)  # 打开摄像头

while True:  # 循环读取摄像头帧
    ret, frame = cap.read()

    # 在循环中,将每一帧作为图像输入,使用pyzbar的decode()函数识别二维码
    barcodes = decode(frame)

    # 绘制二维码框和数据
    for barcode in barcodes:
        rect = barcode.rect
        x, y, w, h = rect
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
        data = barcode.data.decode("utf8")
        cv2.putText(frame, data, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
        print("Result:" + data)
    # 显示结果  
    cv2.imshow('Image', frame)

    if cv2.waitKey(1) == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

简单使用的记录

相关推荐
Williamtym9 分钟前
PickGo Lab实践:用浏览器、Python和MCP串起机器人搬运仿真
开发语言·python·机器人
CS创新实验室32 分钟前
Python 对象“死“了之后去了哪?——引用计数与分代垃圾回收
开发语言·jvm·python
CCblandy35 分钟前
雪花 ID 四个边界实测:时钟回拨重号 500 个,前端 JSON.parse 直接丢精度
后端·python·算法·性能优化·架构
quantdash_cc1 小时前
Pandas 处理分钟级股票行情:从原始数据清洗到 K 线聚合
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
把我格子衫拿来1 小时前
VTK 9.5 与 Qt 6 联合开发:从嵌入三维窗口到可维护的桌面架构
c++·python·qt·vtk·三维可视化
cpolar技术支持1 小时前
把配置发出去怕带出密钥?先把含凭据配置转成脱敏只读快照,用 cpolar 给同事核对差异
运维·python·内网穿透·cpolar·配置脱敏
惊讶的猫2 小时前
langchian_agent的高级使用
python
fbincon2 小时前
Claude Code 自定义状态栏工具:安装、配置与 Subagent 状态展示
python·typescript·claude·claude-code·statusline
yunfengzhineng3 小时前
天津立体库货架配套厂家
大数据·运维·人工智能·python
a puppy slide 葱3 小时前
ENAS | 基于参数共享的高效神经网络架构搜索
论文阅读·人工智能·深度学习·神经网络·机器学习·计算机视觉·神经网络架构搜索