標籤:
雖然手機上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
基本步驟:
拷貝<opencv_installation_dir>\build\python\2.7\x86\cv2.pyd到 <Python27>\Lib\site-packages\cv2.pyd。
建立一個工程目錄。
構建Python Barcode動態連結程式庫。
拷貝所有依賴的動態連結程式庫到工程目錄。
把Webcam串連到PC上。
建立Python指令碼控制Webcam,捕捉Webcam映像,並使用Python Barcode庫來讀取映像中的Barcode。
使用Dynamsoft Barcode SDK建立Python Barcode動態連結程式庫
編譯Python Barcode動態連結程式庫。
#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_FUNCinitDynamsoftBarcodeReader(void){ Py_InitModule("DynamsoftBarcodeReader", methods);}
具體請參考GitHub上的源碼。編譯成功之後請記得拷貝DynamsoftBarcodeReader.pyd和DynamsoftBarcodeReaderx64.dll /DynamsoftBarcodeReaderx86.dll到工程目錄中。
使用OpenCV開啟Webcam
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繪製顯示結果
line_type = cv.CV_AAfont = 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 = 30increase = 20if 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
便於使用OpenCV Python控制Webcam,讀取Barcode