<commit_before>//
// Created by dialight on 25.11.16.
//

#include <utils/VideoSource.hpp>

VideoSource::VideoSource() : src(NONE) {}

VideoSource::~VideoSource() {
    switch (src) {
        case NONE:break;
        case OPENCV_VIDEO:
            cap->release();
            delete cap;
            break;
        case IMAGE:break;
#ifdef USE_RASPICAM
        case RASPICAM:
            cam->release();
            delete cam;
            break;
#endif
    }
}

bool VideoSource::setOpenCVBased(int deviceIndex) {
    if(src != VIDEO_SOURCE::NONE) return false;
    //init video source
    cap = new VideoCapture(deviceIndex); // default camera
    if (!cap->isOpened()) {
        cerr << "No video capture device with index " << deviceIndex << endl;
        return false;
    }
    src = VIDEO_SOURCE::OPENCV_VIDEO;
    return true;
}

bool VideoSource::setImageBased(const string& imagePath) {
    if(src != VIDEO_SOURCE::NONE) return false;
    image = imread(imagePath, 1);
    if(image.empty()) {
        cerr << "Cannot read image file: " << imagePath << endl;
        return false;
    }
    src = VIDEO_SOURCE::IMAGE;
    return true;
}

bool VideoSource::setRaspicamBased() {
    if(src != VIDEO_SOURCE::NONE) return false;
#ifdef USE_RASPICAM
    cout << "try to open raspicam camera..." << endl;
    cam = new raspicam::RaspiCam_Cv();
    cam->set( CV_CAP_PROP_FORMAT, CV_8UC1 );
    if ( !cam->open()) {
        cerr << "Error opening camera" << endl;
        return false;
    }
    src = VIDEO_SOURCE::RASPICAM;
    return true;
#else
    cerr << "Raspicam module usage unsupported in this build." << endl;
    cerr << "Ensure Raspicam library installed in your system and recompile OpenCVClient." << endl;
    cerr << "Raspicam module repository: https://github.com/cedricve/raspicam" << endl;
    cerr << "You still can use regular usb web camera with flag -c <index>" << endl;
    cerr << "or image source with flag -i <path to image>" << endl;
    return false;
#endif
}

void VideoSource::capture(Mat &mat) {
    switch (src) {
        case NONE:
            cerr << "There is no opened video source" << endl;
            exit(-1);
        case OPENCV_VIDEO:
            *cap >> mat;<commit_msg>Added instructions to setup camera module<commit_after>//
// Created by dialight on 25.11.16.
//

#include <utils/VideoSource.hpp>

VideoSource::VideoSource() : src(NONE) {}

VideoSource::~VideoSource() {
    switch (src) {
        case NONE:break;
        case OPENCV_VIDEO:
            cap->release();
            delete cap;
            break;
        case IMAGE:break;
#ifdef USE_RASPICAM
        case RASPICAM:
            cam->release();
            delete cam;
            break;
#endif
    }
}

bool VideoSource::setOpenCVBased(int deviceIndex) {
    if(src != VIDEO_SOURCE::NONE) return false;
    //init video source
    cap = new VideoCapture(deviceIndex); // default camera
    if (!cap->isOpened()) {
        cerr << "No video capture device with index " << deviceIndex << endl;
        return false;
    }
    src = VIDEO_SOURCE::OPENCV_VIDEO;
    return true;
}

bool VideoSource::setImageBased(const string& imagePath) {
    if(src != VIDEO_SOURCE::NONE) return false;
    image = imread(imagePath, 1);
    if(image.empty()) {
        cerr << "Cannot read image file: " << imagePath << endl;
        return false;
    }
    src = VIDEO_SOURCE::IMAGE;
    return true;
}

bool VideoSource::setRaspicamBased() {
    if(src != VIDEO_SOURCE::NONE) return false;
#ifdef USE_RASPICAM
    cam = new raspicam::RaspiCam_Cv();
    cam->set( CV_CAP_PROP_FORMAT, CV_8UC1 );
    if ( !cam->open()) {
        cerr << "Error opening camera." << endl;
        cerr << "Ensure you have camera module connected to raspberry board through 15-pin ribbon cable." << endl;
        cerr << "Don't forget to enable camera interface with \"sudo raspi-config\" command." << endl;
        cerr << "Setting up the camera hardware instructions:" << endl;
        cerr << "https://www.raspberrypi.org/documentation/configuration/camera.md" << endl;
        return false;
    }
    src = VIDEO_SOURCE::RASPICAM;
    return true;
#else
    cerr << "Raspicam module usage unsupported in this build." << endl;
    cerr << "Ensure Raspicam library installed in your system and recompile OpenCVClient." << endl;
    cerr << "Raspicam module repository: https://github.com/cedricve/raspicam" << endl;
    cerr << "You still can use regular usb web camera with flag -c <index>" << endl;
    cerr << "or image source with flag -i <path to image>" << endl;
    return false;
#endif
}

void VideoSource::capture(Mat &mat) {
    switch (src) {
        case NONE:
            cerr << "There is no opened video source" << endl;
            exit(-1);
        case OPENCV_VIDEO:
            *cap >> mat;
{'fraction_non_alphanumeric': 0.1261487050960735, 'fraction_numerical': 0.004594820384294068, 'mean_word_length': 2.608892238131123, 'pattern_counts': {'":': 0, '<': 109, '<?xml version=': 0, '>': 23, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3618771', 'n_tokens_mistral': 1518, 'n_tokens_neox': 1488, 'n_words': 435}