Skip to content

Commit

Permalink
stubbed opencv
Browse files Browse the repository at this point in the history
  • Loading branch information
jtbates committed Nov 16, 2011
1 parent da6d11e commit 35b19fa
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
*.swp
2 changes: 2 additions & 0 deletions opencv/CMakeLists.txt
@@ -0,0 +1,2 @@

install_files (/lua/opencv init.lua)
44 changes: 44 additions & 0 deletions opencv/init.lua
@@ -0,0 +1,44 @@

----------------------------------
-- dependencies
----------------------------------
require 'torch'
require 'xlua'
require 'image'
require 'libopencv'

----------------------------------
-- a camera class
----------------------------------
local Camera = torch.class('image.Camera')

function Camera:__init(...)
-- parse args
local args, idx = xlua.unpack(
{...},
'ImageSource', help_desc,
{arg='idx', type='number', help='camera index', default=0}
)
-- init vars
self.camidx = (idx or 0);
self.tensor = torch.DoubleTensor(3,480,640)
self.tensortyped = torch.Tensor(self.tensor:size())

-- init capture
libopencv.initCam(idx);
end

function Camera:forward()
libopencv.grabFrame(self.tensor)
self.tensortyped:copy(self.tensor)
if tensor then
image.scale(self.tensortyped, tensor)
return tensor
end
return self.tensortyped
end

function Camera:stop()
libopencv.releaseCam()
print('stopping camera')
end
99 changes: 99 additions & 0 deletions opencv/opencv.c
@@ -0,0 +1,99 @@
//==============================================================================
// File: opencv
//
// Description: A wrapper for opencv camera frame grabber
//
// Created: November 8, 2011, 4:08pm
//
// Author: Jordan Bates // jtbates@gmail.com
// Using code from Clement Farabet's wrappers for camiface and v4l
//==============================================================================

#include <luaT.h>
#include <TH.h>

#include <dirent.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <signal.h>
#include <pthread.h>

#include <cv.h>
#include <highgui.c>

static CvCapture* capture;
static IplImage* frame;
static int height;
static int width;

static int l_initCam(lua_State *L) {
const int idx = luaL_tonumber(L, 1);
capture = cvCreateCameraCapture(idx);
if( capture == NULL ) {
perror("could not create OpenCV capture");
}
height = (int) cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT);
width = (int) cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH);
printf("cap height %i\n", height);
printf("cap width %i\n", width);
frame = cvQueryFrame( capture );
printf("frame height %i\n", frame->height);
printf("frame width %i\n", frame->width);
printf("frame depth %i\n", frame->depth);

return 0;
}

// frame grabber
static int l_grabFrame (lua_State *L) {
// Get Tensor's Info
THDoubleTensor * tensor = luaT_checkudata(L, 1, luaT_checktypename2id(L, "torch.DoubleTensor"));

frame = cvQueryFrame ( capture );
if( !frame ) {
perror("could not query OpenCV capture");
}

// resize given tensor
THDoubleTensor_resize3d(tensor, 3, height, width);

// grab frame
int m0 = tensor->stride[1];
int m1 = tensor->stride[2];
int m2 = tensor->stride[0];
unsigned char *src;
double *dst = THDoubleTensor_data(tensor);
int i, j, k;
for (i=0; i < height; i++) {
for (j=0, k=0; j < width; j++, k+=m1) {
}
}

return 0;
}

static int l_releaseCam (lua_State *L) {
cvReleaseCapture( &capture );
return 0;
}

// Register functions
static const struct luaL_reg camiface [] = {
{"initCam", l_initCam},
{"grabFrame", l_grabFrame},
{"releaseCam", l_releaseCam},
{NULL, NULL} /* sentinel */
};

int luaopen_libopencv (lua_State *L) {
luaL_openlib(L, "libopencv", opencv, 0);
return 1;
}
4 changes: 4 additions & 0 deletions opencv/opencv.h
@@ -0,0 +1,4 @@
static int l_initCam(lua_State*);
static int l_grabFrame (lua_State*);
static int l_releaseCam (lua_State*);
int luaopen_libopencv (lua_State*);

0 comments on commit 35b19fa

Please sign in to comment.