Untitled
raw download clone
TEXT
views 88
,
size 6887 b
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include <opencv2/imgproc.hpp>
#include <string>
#include <chrono>
#include <sys/types.h>
#include <sys/stat.h>
#include <iostream>
#include <memory>
#include <string>
#include <fstream>
#include <time.h>
#include <chrono>
#include <ctime>

#include <dlib/dnn.h>
#include <dlib/image_processing.h>
#include <dlib/image_io.h>
#include <dlib/opencv/cv_image.h>
#include <dlib/opencv.h>
#include <dlib/gui_widgets.h>
#include <dlib/clustering.h>
#include <dlib/config_reader.h>

//#include "../include/storageData/storageData.h"

#include <aws/core/Aws.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/Bucket.h>
#include <aws/s3/model/PutObjectRequest.h>
#include <aws/core/utils/StringUtils.h>
#include <boost/interprocess/streams/bufferstream.hpp>
#include <sys/types.h>
#include <signal.h>

#define CONNECT_TIMEOUT_MS 1000
#define REQUEST_TIMEOUT_MS 1000

using namespace std;
using namespace dlib;

struct Object_s3{
    cv::Mat img;
    std::string path;
};

config_reader cr("../data/config.txt");
//StorageData m_storage = StorageData(cr["aws_endpoint"], cr["aws_assessid"], cr["aws_secretkey"]);
string m_storage_person_bucketName = cr["storage_person_bucketName"];
string m_folder_person = "/persons/";
string camID = cr["cam_id"];

//void save_image_to_AWS(cv::Mat img_crop, int cnt, string folder_name, string bucketName) {
//    std::time_t t = std::time(0);
//    string file_name = "Cam_"  + camID + "_" + std::to_string(cnt) + "_" + std::to_string(t) + ".jpg";
//    m_storage.uploadFile(img_crop, file_name, bucketName);
//}

const std::string currentDateTime() {
    time_t     now = time(0);
    struct tm  tstruct;
    char       buf[80];
    tstruct = *localtime(&now);
    strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);

    return buf;
}

int frame_ignore = 30;
std::queue<Object_s3> queue_objs_s3;
std::mutex s3_mutex;

void ProcessFrame();
void StorageS3();
int main(){
    std::vector<std::thread> ths;
    ths.push_back(std::thread(ProcessFrame));
    ths.push_back(std::thread(StorageS3));

    for(auto &t : ths){
        t.join();
    }
}

void ProcessFrame() {
    std::cout << "currentDateTime()=" << currentDateTime() << std::endl;
    cv::VideoCapture cap;
    cv::Mat cvimage;

    std::string videofile = cr["video"];

    int frameNumber = 0;

    if (!cap.open(videofile)) {
        std::cerr << "Could not open video file" << std::endl;
        return;
    }

    int cnt_S3 = 0;
    while (true) {
        int64_t starttime = cv::getTickCount();

        frameNumber++;
        if (!cap.read(cvimage)) {
            cap.open(videofile);
            std::cout << "Reload Video" << std::endl;
            continue;
        }
        cv::resize(cvimage, cvimage, cv::Size(1280, 720));
        cv::Mat imageTracks = cvimage.clone();

        cv::Mat crop_img = imageTracks(cv::Rect(0, 0, 50, 100));

        if(frameNumber % frame_ignore == 0){
            while(queue_objs_s3.size() > 50){
                queue_objs_s3.pop();
            }

            Object_s3 obj_s3;
            obj_s3.img = crop_img;
            std::time_t t = std::time(0);
            string file_path = "Cam_"  + camID + "_" + std::to_string(cnt_S3) + "_" + std::to_string(t) + ".jpg";
            obj_s3.path = file_path;
            queue_objs_s3.push(obj_s3);
            cnt_S3++;
        }

        imshow("Ecovison Yolo Detection", imageTracks);
        cv::waitKey(1);

//        double fps = cv::getTickFrequency() / (cv::getTickCount() - starttime);
//        std::cout << "FPS : " << fps << std::endl;
    }

    return;
}

void StorageS3(){
    Aws::String endpoint;
    Aws::String accessId;
    Aws::String secretKey;
    endpoint = cr["aws_endpoint"].c_str();
    accessId = cr["aws_assessid"].c_str();
    secretKey = cr["aws_secretkey"].c_str();

    Aws::SDKOptions options;
    Aws::InitAPI(options);

    const Aws::String bucketName = m_storage_person_bucketName.c_str();
    Aws::Client::ClientConfiguration config;
    config.scheme = Aws::Http::Scheme::HTTP;
    config.connectTimeoutMs = CONNECT_TIMEOUT_MS;
    config.requestTimeoutMs = REQUEST_TIMEOUT_MS;
    config.endpointOverride = endpoint;
    Aws::S3::S3Client *s3Client = new Aws::S3::S3Client(
            Aws::Auth::AWSCredentials(accessId, secretKey), config,
            Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never, false);

    while(true){
        //
//        Aws::SDKOptions options;
//        Aws::InitAPI(options);

//        const Aws::String bucketName = m_storage_person_bucketName.c_str();
//        Aws::Client::ClientConfiguration config;
//        config.scheme = Aws::Http::Scheme::HTTP;
//        config.connectTimeoutMs = CONNECT_TIMEOUT_MS;
//        config.requestTimeoutMs = REQUEST_TIMEOUT_MS;
//        config.endpointOverride = endpoint;
//        Aws::S3::S3Client *s3Client = new Aws::S3::S3Client(
//                Aws::Auth::AWSCredentials(accessId, secretKey), config,
//                Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never, false);
        //


        if(queue_objs_s3.size() == 0) continue;
        cout << queue_objs_s3.size() << endl;
        s3_mutex.lock();
        Object_s3 obj = queue_objs_s3.front();
        queue_objs_s3.pop();
        s3_mutex.unlock();

        const Aws::String aws_fileName = obj.path.c_str();

        bool debug = true;
        std::vector<unsigned char> imgBuffer;
        cv::imencode(".jpg", obj.img, imgBuffer);
        // Buffer to String with pointer/length
        unsigned char *imgBuffPtr = &imgBuffer[0];
        long imgBuffLength = imgBuffer.size();

        std::string contentLength = std::to_string(imgBuffLength);
        std::shared_ptr<Aws::IOStream> body =
                std::shared_ptr<Aws::IOStream>(new boost::interprocess::bufferstream(
                        (char *)imgBuffPtr, imgBuffLength));

        Aws::S3::Model::PutObjectRequest putObjectRequest;
        putObjectRequest.WithBucket(bucketName).WithKey(aws_fileName).SetBody(body);

        auto putObjectOutcome = s3Client->PutObject(putObjectRequest);
        if (putObjectOutcome.IsSuccess()) {
            if (debug){
            std::cout << "Done!" << std::endl;
            }
        } else {
            if (debug){
                std::cout << "PutObject error: "
                          << putObjectOutcome.GetError().GetExceptionName() << " "
                          << putObjectOutcome.GetError().GetMessage() << std::endl;
            }
            break;
        }
    }

    Aws::ShutdownAPI(options);
    kill(getpid(),SIGINT);

}
close fullscreen
Login or Register to edit or fork this paste. It's free.