轻松使用OpenCV Python控制Webcam,读取Barcode

虽然手机上Barcode应用已经非常流行,但是工作的时候还是用Webcam比较方便。比如需要检测Barcode,我只需要拿Webcam对着电脑屏幕或者纸张扫一下就可以了。今天分享下如何轻松使用OpenCV控制Webcam,以及如何获取一帧图像做Barcode检测。

参考原文:Reading Barcode with Webcam in OpenCV and Python

作者:Xiao Ling

翻译:yushulx

软件安装

  • Dynamsoft Barcode SDK: http://www.dynamsoft.com/Downloads/Dynamic-Barcode-Reader-Download.aspx

  • Python: https://www.python.org/ftp/python/

  • NumPy: http://sourceforge.net/projects/numpy/files/NumPy/

  • SciPy: http://sourceforge.net/projects/scipy/files/scipy/

  • OpenCV: http://sourceforge.net/projects/opencvlibrary/files/opencv-win/

控制Webcam,读取Barcode

基本步骤:

  1. 拷贝<opencv_installation_dir>\build\python\2.7\x86\cv2.pyd到 <Python27>\Lib\site-packages\cv2.pyd。

  2. 创建一个工程目录。

  3. 构建Python Barcode动态链接库。

  4. 拷贝所有依赖的动态链接库到工程目录。

  5. 把Webcam连接到PC上。

  6. 创建Python脚本控制Webcam,捕捉Webcam图像,并使用Python Barcode库来读取图像中的Barcode。

使用Dynamsoft Barcode SDK创建Python Barcode动态链接库

编译Python Barcode动态链接库。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "Python.h"
  
#include "If_DBR.h"
#include "BarcodeFormat.h"
#include "BarcodeStructs.h"
#include "ErrorCode.h"
  
#ifdef _WIN64
#pragma comment(lib, "DBRx64.lib")
#else
#pragma comment(lib, "DBRx86.lib")
#endif
  
void  SetOptions(pReaderOptions pOption,  int  option_iMaxBarcodesNumPerPage,  int  option_llBarcodeFormat){
  
     if  (option_llBarcodeFormat > 0)
         pOption->llBarcodeFormat = option_llBarcodeFormat;
     else
         pOption->llBarcodeFormat = OneD;
  
     if  (option_iMaxBarcodesNumPerPage > 0)
         pOption->iMaxBarcodesNumPerPage = option_iMaxBarcodesNumPerPage;
     else
         pOption->iMaxBarcodesNumPerPage = INT_MAX;
  
}
  
static  PyObject *
initLicense(PyObject *self, PyObject *args)
{
     char  *license;
  
     if  (!PyArg_ParseTuple(args,  "s" , &license)) {
         return  NULL;
     }
  
     printf ( "information: %s\n" , license);
  
     int  ret = DBR_InitLicense(license);
  
     printf ( "return value = %d" , ret);
  
     return  Py_None;
}
  
static  PyObject *
decodeFile(PyObject *self, PyObject *args)
{
     char  *pFileName;
     int  option_iMaxBarcodesNumPerPage = -1;
     int  option_llBarcodeFormat = -1;
  
     if  (!PyArg_ParseTuple(args,  "s" , &pFileName)) {
         return  NULL;
     }
  
     pBarcodeResultArray pResults = NULL;
     ReaderOptions option;
     SetOptions(&option, option_iMaxBarcodesNumPerPage, option_llBarcodeFormat);
  
     int  ret = DBR_DecodeFile(
         pFileName,
         &option,
         &pResults
         );
  
     if  (ret == DBR_OK){
         int  count = pResults->iBarcodeCount;
         pBarcodeResult* ppBarcodes = pResults->ppBarcodes;
         pBarcodeResult tmp = NULL;
  
         PyObject* list = PyList_New(count);
         PyObject* result = NULL;
  
         for  ( int  i = 0; i < count; i++)
         {
             tmp = ppBarcodes[i];
             result = PyString_FromString(tmp->pBarcodeData);
  
             PyList_SetItem(list, i, Py_BuildValue( "iN" , ( int )tmp->llFormat, result));
         }
  
         // release memory
         DBR_FreeBarcodeResults(&pResults);
  
         return  list;
     }
  
     return  Py_None;
}
  
static  PyMethodDef methods[] = {
     "initLicense" , initLicense, METH_VARARGS, NULL },
     "decodeFile" , decodeFile, METH_VARARGS, NULL },
     { NULL, NULL }
};
  
PyMODINIT_FUNC
initDynamsoftBarcodeReader( void )
{
     Py_InitModule( "DynamsoftBarcodeReader" , methods);
}

具体请参考GitHub上的源码。编译成功之后请记得拷贝DynamsoftBarcodeReader.pydDynamsoftBarcodeReaderx64.dll /DynamsoftBarcodeReaderx86.dll到工程目录中。

使用OpenCV打开Webcam

?
1
2
3
4
5
6
7
8
9
import  cv2.cv as cv
  
title  =  "Dynamsoft Barcode Reader"
cv.NamedWindow(title,  1 )
capture  =  cv.CaptureFromCAM( 0 )
  
while  True :
     img  =  cv.QueryFrame(capture)
     cv.ShowImage(title, img)

使用OpenCV绘制显示结果

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
line_type  =  cv.CV_AA
font  =  cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX,
                           0.1 1 1 1 , line_type)
fileName  =  'test.jpg'
img  =  cv.QueryFrame(capture)
cv.SaveImage(fileName, img)
  
results  =  DynamsoftBarcodeReader.decodeFile(fileName)
top  =  30
increase  =  20
if  results:
     for  result  in  results:
         barcode_format  =  "Format: "  +  formats[result[ 0 ]]
         barcode_value  =  "Value: "  +  result[ 1 ]
         cv.PutText(img, barcode_format, ( 10 , top), font, ( 254 142 20 ))
         top  + =  increase
         cv.PutText(img, barcode_value, ( 10 , top), font, ( 254 142 20 ))
         top  + =  increase
         cv.PutText(img,  "************************" , ( 10 , top), font, ( 254 142 20 ))
         top  + =  increase
  
cv.ShowImage(title, img)

源码

https://github.com/yushulx/webcam-barcode-reader


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值