コード例 #1
0
int main(int argc, char* argv[]) {
    signal(SIGINT, interrupted);
    signal(SIGTERM, interrupted);

    // Evalutate/check command line arguments
    Options opts(argc, argv);
    if (opts.isOk() != true) {
	return 1;
    }

    Filter filter;
    filter.setRedEnabled(opts.isRedEnabled());
    filter.setYellowEnabled(opts.isYellowEnabled());

    // If processing a single file (-f FILE)
    if (opts.isFileMode()) {
        Mat orig = imread(opts.getImageFile());

        // Create base name for output files
        string baseName(opts.getImageFile());
        int pos = baseName.rfind('.');
        if (pos != string::npos) {
            baseName.erase(pos);
        }

        avc::Timer timer;
        Found found = filter.filter(orig);

	filter.printFrameRate(cout, timer.secsElapsed());

        // Write out individual image files
        filter.writeImages(baseName, orig, false);

        // Return 0 to parent process if we found image (for scripting)
        return (found == Found::None ? 1 : 0);
    }

    // Video processing
    VideoCapture videoFeed(0);
    {
	int attempts = 0;
	while (!videoFeed.isOpened()) {
	    float waitSecs = 3;
	    attempts++;
	    cerr << "Failed to open camera on attempt " << attempts
		 << ", trying again in "
		 << waitSecs << " seconds.\n";
	    avc::Timer::sleep(waitSecs);
	    if (isInterrupted) {
			return 1;
	    }
	    videoFeed.release();
	    videoFeed.open(0);
	}
    }

    videoFeed.set(CV_CAP_PROP_FRAME_WIDTH, 320);
    videoFeed.set(CV_CAP_PROP_FRAME_HEIGHT, 240);

    Mat origFrame;

    // Where to write out information about what we see
    ofstream stanchionsFile(opts.getStanchionsFile());

    // Get initial frame and toss (incase first one is bad)
    videoFeed >> origFrame;

    avc::Timer timer;

    int foundLast = -1;

    while (!isInterrupted) {
        videoFeed >> origFrame;

        int found = filter.filter(origFrame);
	if ((found != foundLast) || opts.verbose()) {
	    opts.writeToChangeDir(origFrame, filter.getFileData().frameCount);
	    filter.printFrameRate(cout, timer.secsElapsed());
	    foundLast = found;
	} else {
	    // No change in detection state, however, go write out image
	    // if user enabled the periodic feature (-p PERIODIC) and we've
	    // reached the periodic count
	    opts.writePeriodic(origFrame, filter.getFileData().frameCount);
	}

	stanchionsFile.seekp(0);
	stanchionsFile.write((const char*) &filter.getFileData(), sizeof(FileData));
	stanchionsFile.flush();
    }

    if (filter.getFileData().frameCount > 0) {
	filter.printFrameRate(cout, timer.secsElapsed());

	filter.writeImages(opts.getOutputDir() + "/avc-vision", origFrame, true);
    } else {
	cout << "***ERROR*** Failed to read/process any video frames from camera\n";
    }

    return 0;
}