-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.d
80 lines (56 loc) · 2.19 KB
/
app.d
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
import std.stdio;
import std.process;
import std.datetime.stopwatch : StopWatch;
import dcv.core;
import dcv.plot.figure;
import mir.ndslice;
import oclcv;
// compile release for speed: dub -b release
// Video resolution
enum W = 640;
enum H = 480;
void main()
{
CLContext context = new CLContext;
context.clInfo().writeln;
// for video file as input
/*auto pipes = pipeProcess(["ffmpeg", "-i", "file_example_MP4_640_3MG.mp4", "-f", "image2pipe",
"-vcodec", "rawvideo", "-pix_fmt", "rgb24", "-"], // yuv420p
Redirect.stdout);*/
// for camera device as input
auto pipes = pipeProcess(["ffmpeg", "-y", "-hwaccel", "auto", "-f", "dshow", "-video_size", "640x480", "-i",
`video=Lenovo EasyCamera`, "-framerate", "30", "-f", "image2pipe", "-vcodec", "rawvideo",
"-pix_fmt", "rgb24", "-"], Redirect.stdout);
// scope(exit) wait(pipes.pid);
auto thresholder = new INRANGE3(H, W, context);
auto rgb2hsvconv = new HSVConv(H, W, RGB2HSV, context);
auto d_rgb = new CLBuffer(context, BufferMeta(UBYTE, H, W, 3));
// Process video frames
auto frame = slice!ubyte([H, W, 3], 0);
auto grayBool = slice!ubyte([H, W], 0);
auto figGray = imshow(grayBool, "grayBool");
double fps = 30.0;
double waitFrame = 1.0;
StopWatch s;
s.start;
while(1)
{
import std.algorithm.comparison : max;
s.reset;
// Read a frame from the input pipe into the buffer
ubyte[] dt = pipes.stdout.rawRead(frame.ptr[0..H*W*3]);
// If we didn't get a frame of video, we're probably at the end
if (dt.length != H*W*3) break;
d_rgb.upload(frame.ptr[0..frame.elementCount]);
auto d_hsv = rgb2hsvconv.run(d_rgb);
auto d_bool = thresholder.run(d_hsv, 100/2, 130/2, 0, 255, 0, 255); // search for green pixels
d_bool.download(grayBool.ptr[0..grayBool.elementCount]);
figGray.draw(grayBool, ImageFormat.IF_MONO);
int wait = max(1, cast(int)waitFrame - cast(int)s.peek.total!"msecs");
if (waitKey(wait) == KEY_ESCAPE)
break;
if (!figGray.visible)
break;
}
destroyFigures();
}