How to Convert OpenCV Image Data from Python to C
OpenCV officially provides both C++ and Python APIs for developers. Most of the time, developers just need to use one kind of programming languages to read, write and process images with hundreds of computer vision algorithms. However, if you want to use OpenCV Python APIs with an extended C/C++ library, it will be tricky to pass the data. In this article, I will share how to read camera stream with OpenCV-Python and detect barcode with Dynamsoft C/C++ Barcode SDK.
Development Environment
- Dynamsoft Barcode Reader 4.2
- Python version: 2.7.0
- OpenCV version: 2.4.10. Download cv2.pyd
- Windows 10
- USB webcam
Python Extension for Reading Barcode from OpenCV Image Data
Installation
Copy DynamsoftBarcodeReaderx86.dll and cv2.pyd to Python27\Lib\site-packages.
What is the type of the frame data that output by OpenCV?
To convert the image data, we have to know what type it is. The type isnumpy.ndarray:
> rval, frame =vc.read();
type(frame)
> <type'numpy.ndarray'>
How to get the C/C++ pointer that pointing to numpy.ndarray?
According to the OpenCV source file opencv\modules\python\src2\cv2.cv.hpp, we can use the following code to get the memory address of the data in C:
PyObject *o;if(!PyArg_ParseTuple(args, "O", &o))
returnNULL;
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");returnNULL;
}PyArrayInterface *pai =(PyArrayInterface*)PyCObject_AsVoidPtr(ao);
if(pai->two !=2) {
PyErr_SetString(PyExc_TypeError, "object does not have array interface");Py_DECREF(ao);returnNULL;
}//Construct data with header info andimage data
char *buffer=(char*)pai->data; //The address of image data
intwidth =pai->shape[1]; //image width
intheight =pai->shape[0]; //image height
intsize =pai->strides[0] *pai->shape[0]; //image size =stride *height
How to use the barcode detection API?
You can’t just pass the data pointer directly to DBR_DecodeBuffer(). The data needs to be reconstructed with some extra information:
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 topchar*data = total + 40;
intstride = pai->strides[0];
for(inti = 1; i <= height; i++) {
memcpy(data, buffer + stride * (height - i), stride);data += stride;}Read barcode from images and return results:
// Dynamsoft Barcode Reader initialization__int64llFormat = (OneD | QR_CODE | PDF417 | DATAMATRIX);
intiMaxCount = 0x7FFFFFFF;
ReaderOptions ro = {0};pBarcodeResultArray pResults = NULL;ro.llBarcodeFormat = llFormat;ro.iMaxBarcodesNumPerPage = iMaxCount;printf("width: %d, height: %d, size:%d\n", width, height, size);intiRet = 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 resultsintcount = pResults->iBarcodeCount;
pBarcodeResult* ppBarcodes = pResults->ppBarcodes;pBarcodeResult tmp = NULL;retval = PyList_New(count); // The returned Python objectPyObject* result = NULL;for(inti = 0; 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 memoryDBR_FreeBarcodeResults(&pResults);What if you see the error ‘Unable to find vcvarsall.bat’ when building Python extension on Windows?
According to the answer from StackOverflow, execute the following command based on the version of Visual Studio installed:
- Visual Studio 2010 (VS10): SET VS90COMNTOOLS=%VS100COMNTOOLS%
- Visual Studio 2012 (VS11): SET VS90COMNTOOLS=%VS110COMNTOOLS%
- Visual Studio 2013 (VS12): SET VS90COMNTOOLS=%VS120COMNTOOLS%
- Visual Studio 2015 (VS14): SET VS90COMNTOOLS=%VS140COMNTOOLS%
I’m using Visual Studio 2015, and thus I can build Python extension as follows:
SET VS90COMNTOOLS=%VS140COMNTOOLS%python setup.py build installPython Script for Testing
Open camera:
importcv2
fromdbr import*
importtime
vc =cv2.VideoCapture(0)
Read and render camera video stream:
cv2.imshow(windowName, frame)rval, frame =vc.read();
Detect barcode from frame and show results in console:
initLicense("<license>") # Invalid license is fine.results =decodeBuffer(frame)
if(len(results) > 0):
"Total count: "+str(len(results))
forresult inresults:
"Type: "+types[result[0]]
"Value: "+result[1] +"\n"
Source Code
https://github.com/yushulx/opencv-python-webcam-barcode-reader