-
Notifications
You must be signed in to change notification settings - Fork 69
/
frame.lua
94 lines (66 loc) · 2.16 KB
/
frame.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
--[[ frame
The 'frame' package has two public functions. The first is the 'init' function
that takes as arguments the application options table and a second source
definitions table. The second public function is a 'forward' function that is
created by the the 'init' function using the options/source tables to select an
appropriate method. It is this 'forward' function that is called by the
applications 'main' loop to grab an image frame.
--]]
local frame = {}
torch.setdefaulttensortype('torch.FloatTensor')
local sys = assert(require('sys'))
local pf = function(...) print(string.format(...)) end
local Cr = sys.COLORS.red
local Cb = sys.COLORS.blue
local Cg = sys.COLORS.green
local Cn = sys.COLORS.none
local THIS = sys.COLORS.blue .. 'THIS' .. Cn
local function prep_lua_camera(opt, source)
assert(require('camera'))
local cam = image.Camera {
idx = opt.cam,
width = source.w,
height = source.h,
}
-- set frame forward function
frame.forward = function(img)
return cam:forward(img)
end
source.cam = cam
end
local function prep_libcamera_tensor(opt, source)
local cam = assert(require("libcamera_tensor"))
if not cam.init(opt.cam, source.w, source.h, opt.fps, 1) then
error("No camera")
end
-- set frame forward function
frame.forward = function(img)
if not cam.frame_rgb(opt.cam, img) then
error('frame grab failed')
end
return img
end
source.cam = cam
end
local function prep_lua_linuxcamera(opt, source)
local cam = assert(require('linuxcamera'))
-- buffers default == 1
cam.capture('/dev/video'..opt.cam, source.w, source.h, source.fps)
-- set frame forward function
frame.forward = function(img)
img = torch.FloatTensor(3, source.h, source.w)
cam.frame_rgb(img)
return img
end
source.cam = cam
end
function frame:init(opt, source)
if (opt.input == 'usbcam') and (sys.OS == 'macos') then
prep_lua_camera(opt, source)
elseif (opt.input == 'usbcam') then
prep_lua_linuxcamera(opt, source)
else
error("<ERROR frame> Unsupported platform and frame source combination")
end
end
return frame