Skip to content

Commit 35b19fa

Browse files
committed
stubbed opencv
1 parent da6d11e commit 35b19fa

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.swp

opencv/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
install_files (/lua/opencv init.lua)

opencv/init.lua

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
----------------------------------
3+
-- dependencies
4+
----------------------------------
5+
require 'torch'
6+
require 'xlua'
7+
require 'image'
8+
require 'libopencv'
9+
10+
----------------------------------
11+
-- a camera class
12+
----------------------------------
13+
local Camera = torch.class('image.Camera')
14+
15+
function Camera:__init(...)
16+
-- parse args
17+
local args, idx = xlua.unpack(
18+
{...},
19+
'ImageSource', help_desc,
20+
{arg='idx', type='number', help='camera index', default=0}
21+
)
22+
-- init vars
23+
self.camidx = (idx or 0);
24+
self.tensor = torch.DoubleTensor(3,480,640)
25+
self.tensortyped = torch.Tensor(self.tensor:size())
26+
27+
-- init capture
28+
libopencv.initCam(idx);
29+
end
30+
31+
function Camera:forward()
32+
libopencv.grabFrame(self.tensor)
33+
self.tensortyped:copy(self.tensor)
34+
if tensor then
35+
image.scale(self.tensortyped, tensor)
36+
return tensor
37+
end
38+
return self.tensortyped
39+
end
40+
41+
function Camera:stop()
42+
libopencv.releaseCam()
43+
print('stopping camera')
44+
end

opencv/opencv.c

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
}

opencv/opencv.h

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
static int l_initCam(lua_State*);
2+
static int l_grabFrame (lua_State*);
3+
static int l_releaseCam (lua_State*);
4+
int luaopen_libopencv (lua_State*);

0 commit comments

Comments
 (0)