樹莓派小而強大,有很是多的應用場景。這裏分享下使用樹莓派,攝像頭,以及C和Python代碼來實現一個條形碼掃描工具。以前分享過如何把OpenCV Python獲取的圖像傳遞到C層處理,會用到裏面的代碼。html
1. 下載源代碼。python
2. 安裝依賴庫:git
sudo apt-get install cmake sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev sudo apt-get install libxvidcore-dev libx264-dev sudo apt-get install python-dev
3. 設置編譯環境:github
cd ~/opencv-3.0.0/ mkdir build cd build cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_C_EXAMPLES=ON \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.0.0/modules \ -D BUILD_EXAMPLES=ON ..
4. 編譯安裝OpenCV:web
make -j4 sudo make install
生成的動態連接庫會被安裝到/usr/local/lib/python2.7/dist-packages/cv2.so。windows
1. 下載SDK。bash
2. 建立符號連接:app
sudo ln –s <Your dbr path>/lib/libDynamsoftBarcodeReader.so /usr/lib/libDynamsoftBarcodeReader.so
3. 編輯setup.py。設置頭文件和庫文件路徑:python2.7
include_dirs=["/usr/lib/python2.7/dist-packages/numpy/core/include/numpy", "<Your dbr path>/include"], library_dirs=['<Your dbr path>/lib'],
4. 編譯安裝Python擴展:ide
sudo python setup.py build install
底層的C代碼是從windows版本上移植過來的。須要添加一些定義:
typedef unsigned long DWORD; typedef long LONG; typedef unsigned short WORD; typedef struct tagBITMAPINFOHEADER { DWORD biSize; LONG biWidth; LONG biHeight; WORD biPlanes; WORD biBitCount; DWORD biCompression; DWORD biSizeImage; LONG biXPelsPerMeter; LONG biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant; } BITMAPINFOHEADER;
把Python的numpy數據轉換成C。而後經過底層的接口檢測識別條形碼:
#include <ndarraytypes.h> static PyObject * decodeBuffer(PyObject *self, PyObject *args) { PyObject *o; if (!PyArg_ParseTuple(args, "O", &o)) return NULL; PyObject *ao = PyObject_GetAttrString(o, "__array_struct__"); PyObject *retval; if ((ao == NULL) || !PyCObject_Check(ao)) { PyErr_SetString(PyExc_TypeError, "object does not have array interface"); return NULL; } PyArrayInterface *pai = (PyArrayInterface*)PyCObject_AsVoidPtr(ao); if (pai->two != 2) { PyErr_SetString(PyExc_TypeError, "object does not have array interface"); Py_DECREF(ao); return NULL; } // Construct data with header info and image data char *buffer = (char*)pai->data; // The address of image data int width = pai->shape[1]; // image width int height = pai->shape[0]; // image height int size = pai->strides[0] * pai->shape[0]; // image size = stride * height char *total = (char *)malloc(size + 40); // buffer size = image size + header size memset(total, 0, size + 40); BITMAPINFOHEADER bitmap_info = {40, width, height, 0, 24, 0, size, 0, 0, 0, 0}; memcpy(total, &bitmap_info, 40); // Copy image data to buffer from bottom to top char *data = total + 40; int stride = pai->strides[0]; int i = 1; for (; i <= height; i++) { memcpy(data, buffer + stride * (height - i), stride); data += stride; } // Dynamsoft Barcode Reader initialization __int64 llFormat = (OneD | QR_CODE | PDF417 | DATAMATRIX); int iMaxCount = 0x7FFFFFFF; ReaderOptions ro = {0}; pBarcodeResultArray pResults = NULL; ro.llBarcodeFormat = llFormat; ro.iMaxBarcodesNumPerPage = iMaxCount; printf("width: %d, height: %d, size:%d\n", width, height, size); int iRet = DBR_DecodeBuffer((unsigned char *)total, size + 40, &ro, &pResults); printf("DBR_DecodeBuffer ret: %d\n", iRet); free(total); // Do not forget to release the constructed buffer // Get results int count = pResults->iBarcodeCount; pBarcodeResult* ppBarcodes = pResults->ppBarcodes; pBarcodeResult tmp = NULL; retval = PyList_New(count); // The returned Python object PyObject* result = NULL; i = 0; for (; i < count; i++) { tmp = ppBarcodes[i]; result = PyString_FromString(tmp->pBarcodeData); printf("result: %s\n", tmp->pBarcodeData); PyList_SetItem(retval, i, Py_BuildValue("iN", (int)tmp->llFormat, result)); // Add results to list } // release memory DBR_FreeBarcodeResults(&pResults); Py_DECREF(ao); return retval; }
能夠參考官方文檔Property identifier:
若是設置失敗,顯示這些常量沒有定義,能夠直接輸入值:
vc = cv2.VideoCapture(0) vc.set(5, 30) #set FPS vc.set(3, 320) #set width vc.set(4, 240) #set height
如何在Python應用中使用decodeBuffer():
while True: cv2.imshow(windowName, frame) rval, frame = vc.read(); results = decodeBuffer(frame) if (len(results) > 0): print "Total count: " + str(len(results)) for result in results: print "Type: " + types[result[0]] print "Value: " + result[1] + "\n" # 'ESC' for quit key = cv2.waitKey(20) if key == 27: break
1. 用樹莓派2或者3鏈接一個USB攝像頭。
2. 執行程序:
python app.py
https://github.com/yushulx/opencv-python-webcam-barcode-reader/tree/master/raspberrypi