Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.swp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
install_files (/lua/opencv init.lua) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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*); |