forAudioAnalyser.h 2.59 KB
Newer Older
Matteo's avatar
Matteo committed
1
2
#ifndef FORAUDIOANALYSER_H
#define FORAUDIOANALYSER_H
Matteo's avatar
Matteo committed
3
4
5
6
7
8
#include <filesystem>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>

Matteo's avatar
Matteo committed
9
10
#include "lib/time.hpp"

Matteo's avatar
Matteo committed
11
12
using namespace cv;
using namespace std;
13
using json = nlohmann::json;
Matteo's avatar
Matteo committed
14
namespace fs = std::filesystem;
15

Matteo's avatar
Matteo committed
16
17
18
19
const string G_IMG_FOLDER_PATH = "fromAudioAnalyser";

void extract_irregularity_images_for_audio(std::string output_path, const std::string video_path,
                                           json irregularity_file_input, json &irregularity_file_output) {
Matteo's avatar
Matteo committed
20
    // Make fromAudioAnalyser folder
Matteo's avatar
Matteo committed
21
    int caps_directory = fs::create_directory(output_path + G_IMG_FOLDER_PATH + "/");
Matteo's avatar
Matteo committed
22
23

    // Open video
Matteo's avatar
Matteo committed
24
    cv::VideoCapture videoCapture(video_path);
Matteo's avatar
Matteo committed
25
26
27
28

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

Matteo's avatar
Matteo committed
29
    for (int i = 0; i < irregularity_file_input["Irregularities"].size(); i++) {
Matteo's avatar
Matteo committed
30
31
        // Declare output image frame
        cv::Mat frame;
Matteo's avatar
Matteo committed
32
        std::string frame_path;
Matteo's avatar
Matteo committed
33
        // Extract TimeLabel from input JSON
Matteo's avatar
Matteo committed
34
        std::string time_label = irregularity_file_input["Irregularities"][i]["TimeLabel"];
Matteo's avatar
Matteo committed
35
        // Obtain time measures from JSON
Matteo's avatar
Matteo committed
36
37
38
39
        int h = stoi(time_label.substr(0, 2));
        int min = stoi(time_label.substr(3, 2));
        int sec = stoi(time_label.substr(6, 2));
        int ms = stoi(time_label.substr(9, 3));
Matteo's avatar
Matteo committed
40
41

        // Compute the Irregularity instant in milliseconds
Matteo's avatar
Matteo committed
42
43
44
45
        int irr_time_in_ms = ms + sec * 1000 + min * 60000 + h * 3600000;

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

Matteo's avatar
Matteo committed
46
        // Compute the frame number corresponding to the Irregularity
Matteo's avatar
Matteo committed
47
        int irr_frame = std::round((float)(irr_time_in_ms / 1000) * fps);
Matteo's avatar
Matteo committed
48
49

        try {
Matteo's avatar
Matteo committed
50
51
            frame_path = output_path + G_IMG_FOLDER_PATH + "/AudioIrregularity_" + safe_time_label + ".jpg";
            videoCapture.set(CAP_PROP_POS_FRAMES, irr_frame);
Matteo's avatar
Matteo committed
52
            videoCapture >> frame;
Matteo's avatar
Matteo committed
53
            cv::imwrite(frame_path, frame);
Matteo's avatar
Matteo committed
54
55
56

            // Append Irregularity information to JSON
            boost::uuids::uuid uuid = boost::uuids::random_generator()();
Matteo's avatar
Matteo committed
57
58
            irregularity_file_output["Irregularities"] +=
                {{"IrregularityID", irregularity_file_input["Irregularities"][i]["IrregularityID"]},
Matteo's avatar
Matteo committed
59
                 {"Source", "a"},
Matteo's avatar
Matteo committed
60
61
                 {"TimeLabel", time_label},
                 {"ImageURI", frame_path}};
Matteo's avatar
Matteo committed
62
63
64
65
        } catch (cv::Exception e) {
            std::cout << "\033[0;31mTimeLabel error for Audio Analyser Irregularity " << i << "." << std::endl;
        }
    }
Matteo's avatar
Matteo committed
66
67
}
#endif  // FORAUDIOANALYSER_H