|
| 1 | +//============================================================================== |
| 2 | +// File: opencv |
| 3 | +// |
| 4 | +// Description: A wrapper for opencv camera frame grabber |
| 5 | +// |
| 6 | +// Created: November 8, 2011, 4:08pm |
| 7 | +// |
| 8 | +// Author: Jordan Bates // [email protected] |
| 9 | +// Using code from Clement Farabet's wrappers for camiface and v4l |
| 10 | +//============================================================================== |
| 11 | + |
| 12 | +#include <luaT.h> |
| 13 | +#include <TH.h> |
| 14 | + |
| 15 | +#include <dirent.h> |
| 16 | +#include <errno.h> |
| 17 | +#include <time.h> |
| 18 | +#include <unistd.h> |
| 19 | +#include <stdio.h> |
| 20 | +#include <string.h> |
| 21 | +#include <fcntl.h> |
| 22 | +#include <sys/types.h> |
| 23 | +#include <sys/mman.h> |
| 24 | +#include <sys/ipc.h> |
| 25 | +#include <sys/shm.h> |
| 26 | +#include <signal.h> |
| 27 | +#include <pthread.h> |
| 28 | + |
| 29 | +#include <cv.h> |
| 30 | +#include <highgui.c> |
| 31 | + |
| 32 | +static CvCapture* capture; |
| 33 | +static IplImage* frame; |
| 34 | +static int height; |
| 35 | +static int width; |
| 36 | + |
| 37 | +static int l_initCam(lua_State *L) { |
| 38 | + const int idx = luaL_tonumber(L, 1); |
| 39 | + capture = cvCreateCameraCapture(idx); |
| 40 | + if( capture == NULL ) { |
| 41 | + perror("could not create OpenCV capture"); |
| 42 | + } |
| 43 | + height = (int) cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT); |
| 44 | + width = (int) cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH); |
| 45 | + printf("cap height %i\n", height); |
| 46 | + printf("cap width %i\n", width); |
| 47 | + frame = cvQueryFrame( capture ); |
| 48 | + printf("frame height %i\n", frame->height); |
| 49 | + printf("frame width %i\n", frame->width); |
| 50 | + printf("frame depth %i\n", frame->depth); |
| 51 | + |
| 52 | + return 0; |
| 53 | +} |
| 54 | + |
| 55 | +// frame grabber |
| 56 | +static int l_grabFrame (lua_State *L) { |
| 57 | + // Get Tensor's Info |
| 58 | + THDoubleTensor * tensor = luaT_checkudata(L, 1, luaT_checktypename2id(L, "torch.DoubleTensor")); |
| 59 | + |
| 60 | + frame = cvQueryFrame ( capture ); |
| 61 | + if( !frame ) { |
| 62 | + perror("could not query OpenCV capture"); |
| 63 | + } |
| 64 | + |
| 65 | + // resize given tensor |
| 66 | + THDoubleTensor_resize3d(tensor, 3, height, width); |
| 67 | + |
| 68 | + // grab frame |
| 69 | + int m0 = tensor->stride[1]; |
| 70 | + int m1 = tensor->stride[2]; |
| 71 | + int m2 = tensor->stride[0]; |
| 72 | + unsigned char *src; |
| 73 | + double *dst = THDoubleTensor_data(tensor); |
| 74 | + int i, j, k; |
| 75 | + for (i=0; i < height; i++) { |
| 76 | + for (j=0, k=0; j < width; j++, k+=m1) { |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return 0; |
| 81 | +} |
| 82 | + |
| 83 | +static int l_releaseCam (lua_State *L) { |
| 84 | + cvReleaseCapture( &capture ); |
| 85 | + return 0; |
| 86 | +} |
| 87 | + |
| 88 | +// Register functions |
| 89 | +static const struct luaL_reg camiface [] = { |
| 90 | + {"initCam", l_initCam}, |
| 91 | + {"grabFrame", l_grabFrame}, |
| 92 | + {"releaseCam", l_releaseCam}, |
| 93 | + {NULL, NULL} /* sentinel */ |
| 94 | +}; |
| 95 | + |
| 96 | +int luaopen_libopencv (lua_State *L) { |
| 97 | + luaL_openlib(L, "libopencv", opencv, 0); |
| 98 | + return 1; |
| 99 | +} |
0 commit comments