forAudioAnalyser.h 2.69 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
9
10
#include <filesystem>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>

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

Matteo's avatar
Matteo committed
14
15
16
17
18
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
53
54
55
56
57
58
59
60
61
62
63
64
65
void extractIrregularityImagesForAudio(std::string outputPath, const std::string videoPath, json irregularityFileInput,
                                       json &irregularityFileOutput2) {
    // Make fromAudioAnalyser folder
    int capsDirectory = fs::create_directory(outputPath + "fromAudioAnalyser/");

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

    // Compute video length in milliseconds
    int frameCount = videoCapture.get(CAP_PROP_FRAME_COUNT);
    int fps = videoCapture.get(CAP_PROP_FPS);
    int videoLenghtMS = (frameCount / fps) * 1000 + std::round((float)((frameCount % fps) * 1000) / fps);

    for (int i = 0; i < irregularityFileInput["Irregularities"].size(); i++) {
        // Declare output image frame
        cv::Mat frame;
        std::string framePath;
        // Extract TimeLabel from input JSON
        std::string timeLabel = irregularityFileInput["Irregularities"][i]["TimeLabel"];
        // Obtain time measures from JSON
        int h = stoi(timeLabel.substr(0, 2));
        int min = stoi(timeLabel.substr(3, 2));
        int sec = stoi(timeLabel.substr(6, 2));
        int ms = stoi(timeLabel.substr(9, 3));

        std::string safeTimeLabel = timeLabel;
        safeTimeLabel[2] = '-';
        safeTimeLabel[5] = '-';
        safeTimeLabel[8] = '-';

        // Compute the Irregularity instant in milliseconds
        int irrInstMS = ms + sec * 1000 + min * 60000 + h * 3600000;
        // Compute the frame number corresponding to the Irregularity
        int irrFrame = std::round((float)(irrInstMS / 1000) * fps);

        try {
            framePath = outputPath + "fromAudioAnalyser/AudioIrregularity_" + safeTimeLabel + ".jpg";
            videoCapture.set(CAP_PROP_POS_FRAMES, irrFrame);
            videoCapture >> frame;
            cv::imwrite(framePath, frame);

            // Append Irregularity information to JSON
            boost::uuids::uuid uuid = boost::uuids::random_generator()();
            irregularityFileOutput2["Irregularities"] +=
                {{"IrregularityID", irregularityFileInput["Irregularities"][i]["IrregularityID"]},
                 {"Source", "a"},
                 {"TimeLabel", timeLabel},
                 {"ImageURI", framePath}};
        } 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