Esempio n. 1
0
void ADVBConfig::writetorecordlog(const char *fmt, ...) const
{
	ADateTime dt;
	AStdFile fp;

	if (fp.open(GetRecordLog(dt.GetDays()), "a")) {
		va_list ap;

		fp.printf("%s: ", dt.DateFormat("%Y-%M-%D %h:%m:%s.%S").str());

		va_start(ap, fmt);
		fp.vprintf(fmt, ap);
		va_end(ap);

		fp.printf("\n");

		fp.close();
	}
}
Esempio n. 2
0
void ADVBConfig::vlogit(const char *fmt, va_list ap, bool show) const
{
	ADateTime dt;
	AString   filename = GetLogFile(dt.GetDays());
	AString   str;
	AStdFile  fp;

	str.vprintf(fmt, ap);

	if (fp.open(filename, "a")) {
		uint_t i, n = str.CountLines("\n", 0);

		for (i = 0; i < n; i++) {
			fp.printf("%s [%05u]: %s\n", dt.DateFormat("%Y-%M-%D %h:%m:%s.%S").str(), (uint_t)getpid(), str.Line(i, "\n", 0).str());
		}

		fp.close();
	}

	if (show && !webresponse) {
		Stdout->printf("%s\n", str.str());
	}
}
Esempio n. 3
0
void MotionDetector::ProcessImage(const ADateTime& dt, const AImage& image)
{
    AString datestr = dt.DateFormat("%Y-%M-%D %h:%m:%s.%S");

    images[imgindex] = image;

    AImage& image1 = images[(imgindex + images.size() - 1) % images.size()];
    AImage& image2 = images[imgindex];
    imgindex = (imgindex + 1) % images.size();

    if ((image1.GetRect() == image2.GetRect()) && (image2 != image1)) {
        const AImage::PIXEL *pix1 = image1.GetPixelData();
        const AImage::PIXEL *pix2 = image2.GetPixelData();
        const ARect& rect = image1.GetRect();
        std::vector<float> data;
        float *ptr, *p;
        double avg[3];
        uint_t x, y, w = rect.w, h = rect.h, w3 = w * 3, len = w * h, len3 = w3 * h;

        data.resize(len3);
        ptr = &data[0];

        memset(avg, 0, sizeof(avg));

        for (y = 0, p = ptr; y < h; y++) {
            double yavg[3];

            memset(yavg, 0, sizeof(yavg));

            for (x = 0; x < w; x++, p += 3, pix1++, pix2++) {
                p[0] 	 = ((float)pix1->r - (float)pix2->r) * redscale;
                p[1] 	 = ((float)pix1->g - (float)pix2->g) * grnscale;
                p[2] 	 = ((float)pix1->b - (float)pix2->b) * bluscale;

                yavg[0] += p[0];
                yavg[1] += p[1];
                yavg[2] += p[2];
            }

            yavg[0] /= (double)w;
            yavg[1] /= (double)w;
            yavg[2] /= (double)w;

            p -= 3 * w;

            for (x = 0; x < w; x++, p += 3) {
                p[0] -= yavg[0];
                avg[0] += p[0];
                p[1] -= yavg[1];
                avg[1] += p[1];
                p[2] -= yavg[2];
                avg[2] += p[2];
            }
        }

        avg[0] /= (double)len;
        avg[1] /= (double)len;
        avg[2] /= (double)len;

        float *p2;
        for (y = 0, p = ptr, p2 = ptr; y < h; y++) {
            for (x = 0; x < w; x++, p += 3, p2++) {
                p[0] -= avg[0];
                p[1] -= avg[1];
                p[2] -= avg[2];
                p2[0] = sqrt(p[0] * p[0] + p[1] * p[1] + p[2] * p[2]);
            }
        }

        double avg2 = 0.0, sd2 = 0.0;
        uint_t mx, my, cx = (matwid - 1) >> 1, cy = (mathgt - 1) >> 1;
        for (x = 0; x < w; x++) {
            for (y = 0; y < h; y++) {
                float val = 0.f;

                if (matwid && mathgt) {
                    for (my = 0; my < mathgt; my++) {
                        if (((y + my) >= cy) && ((y + my) < (h + cy))) {
                            for (mx = 0; mx < matwid; mx++) {
                                if (((x + mx) >= cx) && ((x + mx) < (h + cx))) {
                                    val += matrix[mx + my * matwid] * ptr[(x + mx - cx) + (y + my - cy) * w];
                                }
                            }
                        }
                    }

                    val *= matmul;
                }
                else val = ptr[x + y * w];

                ptr[w * h + x + y * w] = (float)val;
                avg2 += val;
                sd2  += val * val;
            }
        }

        avg2 /= (double)len;
        sd2   = sqrt(sd2 / (double)len - avg2 * avg2);

        Interpolate(diffavg, avg2, coeff);
        Interpolate(diffsd,  sd2,  coeff);

        stats.Set(AString("avg%").Arg(index), AString("%0.16e").Arg(diffavg));
        stats.Set(AString("sd%").Arg(index),  AString("%0.16e").Arg(diffsd));

        double diff = avgfactor * diffavg + sdfactor * diffsd, total = 0.0;
        for (x = 0; x < len; x++) {
            ptr[x] = MAX(ptr[w * h + x] - diff, 0.0);
            total += ptr[x];
        }

        total = total * 1000.0 / ((double)w * (double)h);

        if (verbose) {
            log.printf("%s[%u]: Level = %0.1lf (diff = %0.6lf)\n",
                       datestr.str(), index, total, diff);
        }

        stats.Set(AString("level%").Arg(index), AString("%0.4").Arg(total));

        if (total >= threshold) {
            static const TAG tags[] = {
                {AImage::TAG_JPEG_QUALITY, 95},
                {TAG_DONE, 0},
            };

            if (detimgdir.Valid()) {
                AImage img;
                if (img.Create(rect.w, rect.h)) {
                    const AImage::PIXEL *pixel1 = image1.GetPixelData();
                    const AImage::PIXEL *pixel2 = image2.GetPixelData();
                    AImage::PIXEL       *pixel  = img.GetPixelData();

                    for (x = 0; x < len; x++, pixel++, pixel1++, pixel2++) {
                        pixel->r = (uint8_t)LIMIT((double)MAX(pixel1->r, pixel2->r) * ptr[x] / 255.0, 0.0, 255.0);
                        pixel->g = (uint8_t)LIMIT((double)MAX(pixel1->g, pixel2->g) * ptr[x] / 255.0, 0.0, 255.0);
                        pixel->b = (uint8_t)LIMIT((double)MAX(pixel1->b, pixel2->b) * ptr[x] / 255.0, 0.0, 255.0);
                    }

                    AString filename = detimgdir.CatPath(dt.DateFormat(detimgfmt) + ".jpg");
                    CreateDirectory(filename.PathPart());
                    img.SaveJPEG(filename, tags);
                }
            }

            AString filename = imagedir.CatPath(dt.DateFormat(imagefmt) + ".jpg");
            CreateDirectory(filename.PathPart());
            log.printf("%s[%u]: Saving detection image in '%s'\n", datestr.str(), index, filename.str());
            image2.SaveJPEG(filename, tags);

            if (detcmd.Valid()) {
                AString cmd = detcmd.SearchAndReplace("{level}", AString("%0.4").Arg(total));
                if (system(cmd) != 0) {
                    log.printf("%s[%u]: Detection command '%s' failed\n", datestr.str(), index, cmd.str());
                }
            }
        }
        else if (nodetcmd.Valid()) {
            AString cmd = nodetcmd.SearchAndReplace("{level}", AString("%0.4").Arg(total));
            if (system(cmd) != 0) {
                log.printf("%s[%u]: No-detection command '%s' failed\n", datestr.str(), index, cmd.str());
            }
        }
    }
    //else debug("Images are different sizes or identical\n");
}