forAudioAnalyser.h 2.47 KB
Newer Older
Matteo Spanio's avatar
update    
Matteo Spanio committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef FORAUDIOANALYSER_H
#define FORAUDIOANALYSER_H
#include <filesystem>
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <nlohmann/json.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>

#include "lib/time.hpp"
#include "lib/core.hpp"

using namespace cv;
using namespace std;
18
using json = nlohmann::json;
Matteo Spanio's avatar
update    
Matteo Spanio committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
namespace fs = std::filesystem;
namespace va = videoanalyser;

const string G_IMG_FOLDER_PATH = "fromAudioAnalyser";

va::Result<int> extract_irregularity_images_for_audio(std::string output_path, const std::string video_path,
                                           json irregularity_file_input, json &irregularity_file_output) {
    // Make fromAudioAnalyser folder
    int caps_directory = fs::create_directory(output_path + G_IMG_FOLDER_PATH + "/");

    // Open video
    cv::VideoCapture videoCapture(video_path);

    // Compute video length in milliseconds
    int fps = videoCapture.get(CAP_PROP_FPS);

    for (int i = 0; i < irregularity_file_input["Irregularities"].size(); i++) {
        // Declare output image frame
        cv::Mat frame;
        std::string frame_path;
        // Extract TimeLabel from input JSON
        std::string time_label = irregularity_file_input["Irregularities"][i]["TimeLabel"];
        int irr_time_in_ms = time_label_to_ms(time_label);

        std::string safe_time_label = getTimeLabel(irr_time_in_ms, "-");

        // Compute the frame number corresponding to the Irregularity
        int irr_frame = std::round((float)(irr_time_in_ms / 1000) * fps);

        try {
            frame_path = output_path + G_IMG_FOLDER_PATH + "/AudioIrregularity_" + safe_time_label + ".jpg";
            videoCapture.set(CAP_PROP_POS_FRAMES, irr_frame);
            videoCapture >> frame;
            cv::imwrite(frame_path, frame);
53

Matteo Spanio's avatar
update    
Matteo Spanio committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
            // Append Irregularity information to JSON
            boost::uuids::uuid uuid = boost::uuids::random_generator()();
            irregularity_file_output["Irregularities"] +=
                {{"IrregularityID", irregularity_file_input["Irregularities"][i]["IrregularityID"]},
                 {"Source", "a"},
                 {"TimeLabel", time_label},
                 {"ImageURI", frame_path}};
        } catch (cv::Exception e) {
            return va::Error("TimeLabel error for Audio Analyser Irregularity " + i);
        }
    }
    return va::Result<int>(0);
}
#endif  // FORAUDIOANALYSER_H