Exemple #1
0
bool GetFileInfo(const AString& FileName, FILE_INFO *file)
{
	struct dirent ent;

	strcpy(ent.d_name, FileName.FilePart());

	return SetFileData(FileName.PathPart(), ent, file);
}
Exemple #2
0
bool CreateDirectory(const AString& dir)
{
	AString parentdir = dir.PathPart();
	FILE_INFO file;
	bool success = true;

	if (parentdir.Valid() && (parentdir != "/") && !::GetFileInfo(parentdir, &file)) success = CreateDirectory(parentdir);

	if (success && (dir.FilePart().Valid()) && !::GetFileInfo(dir, &file)) {
#ifdef __LINUX__
		success = (mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH) == 0);
#else
		success = ::CreateDirectoryA(dir.str(), NULL);
#endif
	}

	return success;
}
Exemple #3
0
bool GetFileInfo(const AString& FileName, FILE_INFO *file)
{
	WIN32_FIND_DATA finddata;
	HANDLE handle;
	bool ok = false;

	if ((handle = ::FindFirstFile(FileName, &finddata)) != INVALID_HANDLE_VALUE) {
		if (file) {
			SetFileData(FileName.PathPart(), finddata, file);
		}

		::FindClose(handle);

		ok = true;
	}

	return ok;
}
Exemple #4
0
void ADVBConfig::ListUsers(AList& list) const
{
	AHash 	 users(10);
	AList 	 userpatterns;
	AString  filepattern 	   = GetUserPatternsPattern();
	AString  filepattern_parsed = ParseRegex(filepattern);
	AString  _users             = GetConfigItem("users");
	AStdFile fp;
	uint_t   i, n = _users.CountColumns();

	//debug("Reading users from config %s\n", config.GetFilename().str());

	for (i = 0; i < n; i++) {
		AString user = _users.Column(i).Words(0);

		if (!users.Exists(user)) {
			users.Insert(user, 0);
			list.Add(new AString(user));
		}
	}

	if (fp.open(GetPatternsFile())) {
		AString line;

		while (line.ReadLn(fp) >= 0) {
			AString user;
			int p;

			if		((p = line.PosNoCase(" user:="******"user:=") == 0)        user = line.Mid(6).Word(0).DeQuotify();

			if (user.Valid() && !users.Exists(user)) {
				users.Insert(user, 0);
				list.Add(new AString(user));
			}
		}

		fp.close();
	}

	::CollectFiles(filepattern.PathPart(), filepattern.FilePart(), 0, userpatterns);

	const AString *file = AString::Cast(userpatterns.First());
	while (file) {
		AString   user;
		ADataList regions;

		if (MatchRegex(*file, filepattern_parsed, regions)) {
			const REGEXREGION *region = (const REGEXREGION *)regions[0];

			if (region) {
				user = file->Mid(region->pos, region->len);
				if (!users.Exists(user)) {
					users.Insert(user, 0);
					list.Add(new AString(user));
				}
			}
		}

		file = file->Next();
	}

	list.Sort(&AString::AlphaCompareCase);
}
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");
}
Exemple #6
0
bool Recurse(const AString& PathPattern, uint_t nSubDirs, bool (*fn)(const FILE_INFO *file, void *Context), void *Context)
{
	return ::Recurse(PathPattern.PathPart(), PathPattern.FilePart(), nSubDirs, fn, Context);
}