1 | /*
|
---|
2 | * Fish Detector Hook
|
---|
3 | * Copyright (c) 2002 Philip Gladstone
|
---|
4 | *
|
---|
5 | * This file implements a fish detector. It is used to see when a
|
---|
6 | * goldfish passes in front of the camera. It does this by counting
|
---|
7 | * the number of input pixels that fall within a particular HSV
|
---|
8 | * range.
|
---|
9 | *
|
---|
10 | * It takes a multitude of arguments:
|
---|
11 | *
|
---|
12 | * -h <num>-<num> the range of H values that are fish
|
---|
13 | * -s <num>-<num> the range of S values that are fish
|
---|
14 | * -v <num>-<num> the range of V values that are fish
|
---|
15 | * -z zap all non-fish values to black
|
---|
16 | * -l <num> limit the number of saved files to <num>
|
---|
17 | * -i <num> only check frames every <num> seconds
|
---|
18 | * -t <num> the threshold for the amount of fish pixels (range 0-1)
|
---|
19 | * -d turn debugging on
|
---|
20 | * -D <directory> where to put the fish images
|
---|
21 | *
|
---|
22 | * This library is free software; you can redistribute it and/or
|
---|
23 | * modify it under the terms of the GNU Lesser General Public
|
---|
24 | * License as published by the Free Software Foundation; either
|
---|
25 | * version 2 of the License, or (at your option) any later version.
|
---|
26 | *
|
---|
27 | * This library is distributed in the hope that it will be useful,
|
---|
28 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
30 | * Lesser General Public License for more details.
|
---|
31 | *
|
---|
32 | * You should have received a copy of the GNU Lesser General Public
|
---|
33 | * License along with this library; if not, write to the Free Software
|
---|
34 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
35 | */
|
---|
36 | #include <stdlib.h>
|
---|
37 | #include <fcntl.h>
|
---|
38 | #include <unistd.h>
|
---|
39 | #include <stdarg.h>
|
---|
40 | #include <string.h>
|
---|
41 | #include <time.h>
|
---|
42 | #include <stdio.h>
|
---|
43 | #include <dirent.h>
|
---|
44 |
|
---|
45 | #include "framehook.h"
|
---|
46 | #include "dsputil.h"
|
---|
47 | #include "avformat.h"
|
---|
48 |
|
---|
49 | #define SCALEBITS 10
|
---|
50 | #define ONE_HALF (1 << (SCALEBITS - 1))
|
---|
51 | #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
|
---|
52 |
|
---|
53 | #define YUV_TO_RGB1_CCIR(cb1, cr1)\
|
---|
54 | {\
|
---|
55 | cb = (cb1) - 128;\
|
---|
56 | cr = (cr1) - 128;\
|
---|
57 | r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
|
---|
58 | g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
|
---|
59 | ONE_HALF;\
|
---|
60 | b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
|
---|
61 | }
|
---|
62 |
|
---|
63 | #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
|
---|
64 | {\
|
---|
65 | yt = ((y1) - 16) * FIX(255.0/219.0);\
|
---|
66 | r = cm[(yt + r_add) >> SCALEBITS];\
|
---|
67 | g = cm[(yt + g_add) >> SCALEBITS];\
|
---|
68 | b = cm[(yt + b_add) >> SCALEBITS];\
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 |
|
---|
73 |
|
---|
74 | typedef struct {
|
---|
75 | int h; /* 0 .. 360 */
|
---|
76 | int s; /* 0 .. 255 */
|
---|
77 | int v; /* 0 .. 255 */
|
---|
78 | } HSV;
|
---|
79 |
|
---|
80 | typedef struct {
|
---|
81 | int zapping;
|
---|
82 | int threshold;
|
---|
83 | HSV dark, bright;
|
---|
84 | char *dir;
|
---|
85 | int file_limit;
|
---|
86 | int debug;
|
---|
87 | int min_interval;
|
---|
88 | int64_t next_pts;
|
---|
89 | int inset;
|
---|
90 | int min_width;
|
---|
91 | } ContextInfo;
|
---|
92 |
|
---|
93 | static void dorange(const char *s, int *first, int *second, int maxval)
|
---|
94 | {
|
---|
95 | sscanf(s, "%d-%d", first, second);
|
---|
96 | if (*first > maxval)
|
---|
97 | *first = maxval;
|
---|
98 | if (*second > maxval)
|
---|
99 | *second = maxval;
|
---|
100 | }
|
---|
101 |
|
---|
102 | void Release(void *ctx)
|
---|
103 | {
|
---|
104 | if (ctx)
|
---|
105 | av_free(ctx);
|
---|
106 | }
|
---|
107 |
|
---|
108 | int Configure(void **ctxp, int argc, char *argv[])
|
---|
109 | {
|
---|
110 | ContextInfo *ci;
|
---|
111 | int c;
|
---|
112 |
|
---|
113 | *ctxp = av_mallocz(sizeof(ContextInfo));
|
---|
114 | ci = (ContextInfo *) *ctxp;
|
---|
115 |
|
---|
116 | optind = 0;
|
---|
117 |
|
---|
118 | ci->dir = "/tmp";
|
---|
119 | ci->threshold = 100;
|
---|
120 | ci->file_limit = 100;
|
---|
121 | ci->min_interval = 1000000;
|
---|
122 | ci->inset = 10; /* Percent */
|
---|
123 |
|
---|
124 | while ((c = getopt(argc, argv, "w:i:dh:s:v:zl:t:D:")) > 0) {
|
---|
125 | switch (c) {
|
---|
126 | case 'h':
|
---|
127 | dorange(optarg, &ci->dark.h, &ci->bright.h, 360);
|
---|
128 | break;
|
---|
129 | case 's':
|
---|
130 | dorange(optarg, &ci->dark.s, &ci->bright.s, 255);
|
---|
131 | break;
|
---|
132 | case 'v':
|
---|
133 | dorange(optarg, &ci->dark.v, &ci->bright.v, 255);
|
---|
134 | break;
|
---|
135 | case 'z':
|
---|
136 | ci->zapping = 1;
|
---|
137 | break;
|
---|
138 | case 'l':
|
---|
139 | ci->file_limit = atoi(optarg);
|
---|
140 | break;
|
---|
141 | case 'i':
|
---|
142 | ci->min_interval = 1000000 * atof(optarg);
|
---|
143 | break;
|
---|
144 | case 't':
|
---|
145 | ci->threshold = atof(optarg) * 1000;
|
---|
146 | if (ci->threshold > 1000 || ci->threshold < 0) {
|
---|
147 | fprintf(stderr, "Invalid threshold value '%s' (range is 0-1)\n", optarg);
|
---|
148 | return -1;
|
---|
149 | }
|
---|
150 | break;
|
---|
151 | case 'w':
|
---|
152 | ci->min_width = atoi(optarg);
|
---|
153 | break;
|
---|
154 | case 'd':
|
---|
155 | ci->debug++;
|
---|
156 | break;
|
---|
157 | case 'D':
|
---|
158 | ci->dir = av_strdup(optarg);
|
---|
159 | break;
|
---|
160 | default:
|
---|
161 | fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
|
---|
162 | return -1;
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | fprintf(stderr, "Fish detector configured:\n");
|
---|
167 | fprintf(stderr, " HSV range: %d,%d,%d - %d,%d,%d\n",
|
---|
168 | ci->dark.h,
|
---|
169 | ci->dark.s,
|
---|
170 | ci->dark.v,
|
---|
171 | ci->bright.h,
|
---|
172 | ci->bright.s,
|
---|
173 | ci->bright.v);
|
---|
174 | fprintf(stderr, " Threshold is %d%% pixels\n", ci->threshold / 10);
|
---|
175 |
|
---|
176 |
|
---|
177 | return 0;
|
---|
178 | }
|
---|
179 |
|
---|
180 | static void get_hsv(HSV *hsv, int r, int g, int b)
|
---|
181 | {
|
---|
182 | int i, v, x, f;
|
---|
183 |
|
---|
184 | x = (r < g) ? r : g;
|
---|
185 | if (b < x)
|
---|
186 | x = b;
|
---|
187 | v = (r > g) ? r : g;
|
---|
188 | if (b > v)
|
---|
189 | v = b;
|
---|
190 |
|
---|
191 | if (v == x) {
|
---|
192 | hsv->h = 0;
|
---|
193 | hsv->s = 0;
|
---|
194 | hsv->v = v;
|
---|
195 | return;
|
---|
196 | }
|
---|
197 |
|
---|
198 | if (r == v) {
|
---|
199 | f = g - b;
|
---|
200 | i = 0;
|
---|
201 | } else if (g == v) {
|
---|
202 | f = b - r;
|
---|
203 | i = 2 * 60;
|
---|
204 | } else {
|
---|
205 | f = r - g;
|
---|
206 | i = 4 * 60;
|
---|
207 | }
|
---|
208 |
|
---|
209 | hsv->h = i + (60 * f) / (v - x);
|
---|
210 | if (hsv->h < 0)
|
---|
211 | hsv->h += 360;
|
---|
212 |
|
---|
213 | hsv->s = (255 * (v - x)) / v;
|
---|
214 | hsv->v = v;
|
---|
215 |
|
---|
216 | return;
|
---|
217 | }
|
---|
218 |
|
---|
219 | void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
|
---|
220 | {
|
---|
221 | ContextInfo *ci = (ContextInfo *) ctx;
|
---|
222 | uint8_t *cm = cropTbl + MAX_NEG_CROP;
|
---|
223 | int rowsize = picture->linesize[0];
|
---|
224 |
|
---|
225 | #if 0
|
---|
226 | printf("pix_fmt = %d, width = %d, pts = %lld, ci->next_pts = %lld\n",
|
---|
227 | pix_fmt, width, pts, ci->next_pts);
|
---|
228 | #endif
|
---|
229 |
|
---|
230 | if (pts < ci->next_pts)
|
---|
231 | return;
|
---|
232 |
|
---|
233 | if (width < ci->min_width)
|
---|
234 | return;
|
---|
235 |
|
---|
236 | ci->next_pts = pts + 1000000;
|
---|
237 |
|
---|
238 | if (pix_fmt == PIX_FMT_YUV420P) {
|
---|
239 | uint8_t *y, *u, *v;
|
---|
240 | int width2 = width >> 1;
|
---|
241 | int inrange = 0;
|
---|
242 | int pixcnt;
|
---|
243 | int h;
|
---|
244 | int h_start, h_end;
|
---|
245 | int w_start, w_end;
|
---|
246 |
|
---|
247 | h_end = 2 * ((ci->inset * height) / 200);
|
---|
248 | h_start = height - h_end;
|
---|
249 |
|
---|
250 | w_end = (ci->inset * width2) / 100;
|
---|
251 | w_start = width2 - w_end;
|
---|
252 |
|
---|
253 | pixcnt = ((h_start - h_end) >> 1) * (w_start - w_end);
|
---|
254 |
|
---|
255 | y = picture->data[0] + h_end * picture->linesize[0] + w_end * 2;
|
---|
256 | u = picture->data[1] + h_end * picture->linesize[1] / 2 + w_end;
|
---|
257 | v = picture->data[2] + h_end * picture->linesize[2] / 2 + w_end;
|
---|
258 |
|
---|
259 | for (h = h_start; h > h_end; h -= 2) {
|
---|
260 | int w;
|
---|
261 |
|
---|
262 | for (w = w_start; w > w_end; w--) {
|
---|
263 | unsigned int r,g,b;
|
---|
264 | HSV hsv;
|
---|
265 | int cb, cr, yt, r_add, g_add, b_add;
|
---|
266 |
|
---|
267 | YUV_TO_RGB1_CCIR(u[0], v[0]);
|
---|
268 | YUV_TO_RGB2_CCIR(r, g, b, y[0]);
|
---|
269 |
|
---|
270 | get_hsv(&hsv, r, g, b);
|
---|
271 |
|
---|
272 | if (ci->debug > 1)
|
---|
273 | fprintf(stderr, "(%d,%d,%d) -> (%d,%d,%d)\n",
|
---|
274 | r,g,b,hsv.h,hsv.s,hsv.v);
|
---|
275 |
|
---|
276 |
|
---|
277 | if (hsv.h >= ci->dark.h && hsv.h <= ci->bright.h &&
|
---|
278 | hsv.s >= ci->dark.s && hsv.s <= ci->bright.s &&
|
---|
279 | hsv.v >= ci->dark.v && hsv.v <= ci->bright.v) {
|
---|
280 | inrange++;
|
---|
281 | } else if (ci->zapping) {
|
---|
282 | y[0] = y[1] = y[rowsize] = y[rowsize + 1] = 16;
|
---|
283 | u[0] = 128;
|
---|
284 | v[0] = 128;
|
---|
285 | }
|
---|
286 |
|
---|
287 | y+= 2;
|
---|
288 | u++;
|
---|
289 | v++;
|
---|
290 | }
|
---|
291 |
|
---|
292 | y += picture->linesize[0] * 2 - (w_start - w_end) * 2;
|
---|
293 | u += picture->linesize[1] - (w_start - w_end);
|
---|
294 | v += picture->linesize[2] - (w_start - w_end);
|
---|
295 | }
|
---|
296 |
|
---|
297 | if (ci->debug)
|
---|
298 | fprintf(stderr, "Fish: Inrange=%d of %d = %d threshold\n", inrange, pixcnt, 1000 * inrange / pixcnt);
|
---|
299 |
|
---|
300 | if (inrange * 1000 / pixcnt >= ci->threshold) {
|
---|
301 | /* Save to file */
|
---|
302 | int size;
|
---|
303 | char *buf;
|
---|
304 | AVPicture picture1;
|
---|
305 | static int frame_counter;
|
---|
306 | static int foundfile;
|
---|
307 |
|
---|
308 | if ((frame_counter++ % 20) == 0) {
|
---|
309 | /* Check how many files we have */
|
---|
310 | DIR *d;
|
---|
311 |
|
---|
312 | foundfile = 0;
|
---|
313 |
|
---|
314 | d = opendir(ci->dir);
|
---|
315 | if (d) {
|
---|
316 | struct dirent *dent;
|
---|
317 |
|
---|
318 | while ((dent = readdir(d))) {
|
---|
319 | if (strncmp("fishimg", dent->d_name, 7) == 0) {
|
---|
320 | if (strcmp(".ppm", dent->d_name + strlen(dent->d_name) - 4) == 0) {
|
---|
321 | foundfile++;
|
---|
322 | }
|
---|
323 | }
|
---|
324 | }
|
---|
325 | closedir(d);
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 | if (foundfile < ci->file_limit) {
|
---|
330 | size = avpicture_get_size(PIX_FMT_RGB24, width, height);
|
---|
331 | buf = av_malloc(size);
|
---|
332 |
|
---|
333 | avpicture_fill(&picture1, buf, PIX_FMT_RGB24, width, height);
|
---|
334 | if (img_convert(&picture1, PIX_FMT_RGB24,
|
---|
335 | picture, pix_fmt, width, height) >= 0) {
|
---|
336 | /* Write out the PPM file */
|
---|
337 |
|
---|
338 | FILE *f;
|
---|
339 | char fname[256];
|
---|
340 |
|
---|
341 | snprintf(fname, sizeof(fname), "%s/fishimg%ld_%"PRId64".ppm", ci->dir, (long)(av_gettime() / 1000000), pts);
|
---|
342 | f = fopen(fname, "w");
|
---|
343 | if (f) {
|
---|
344 | fprintf(f, "P6 %d %d 255\n", width, height);
|
---|
345 | fwrite(buf, width * height * 3, 1, f);
|
---|
346 | fclose(f);
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | av_free(buf);
|
---|
351 | ci->next_pts = pts + ci->min_interval;
|
---|
352 | }
|
---|
353 | }
|
---|
354 | }
|
---|
355 | }
|
---|
356 |
|
---|