Commit cc3faa0d authored by Matteo's avatar Matteo
Browse files

update Irregularity Classes

parent 8fe0315d
......@@ -4,37 +4,79 @@
#include <boost/uuid/uuid_generators.hpp>
#include "Irregularity.h"
Irregularity::Irregularity(const Irregularity& other)
: id(other.id), source(other.source), time_label(other.time_label), type(other.type) {}
Irregularity::Irregularity(Source source, string time_label, IrregularityType type, string image_URI)
{
Irregularity::Irregularity(Irregularity&& other) noexcept
: id(std::move(other.id)), source(other.source), time_label(std::move(other.time_label)), type(std::move(other.type)) {}
Irregularity::Irregularity(Source source, string time_label) {
this->id = boost::uuids::random_generator()();
this->source = source;
this->time_label = time_label;
this->type = type;
this->image_URI = image_URI;
this->type = std::nullopt;
}
Irregularity::~Irregularity() {}
Irregularity::Irregularity(Source source, string time_label, IrregularityType type) {
this->id = boost::uuids::random_generator()();
this->source = source;
this->time_label = time_label;
this->type = type;
}
json Irregularity::toJSON() {
json Irregularity::to_JSON() const {
json j;
j["IrregularityID"] = boost::lexical_cast<string>(this->id);
j["Source"] = sourceToString(this->source);
j["TimeLabel"] = this->time_label;
j["IrregularityType"] = irregularityTypeToString(this->type);
if (!this->image_URI.empty())
j["ImageURI"] = this->image_URI;
if (this->type.has_value())
j["IrregularityType"] = irregularityTypeToString(this->type.value());
if (this->image_URI.has_value())
j["ImageURI"] = this->image_URI.value();
if (this->audio_URI.has_value())
j["AudioURI"] = this->audio_URI.value();
return j;
}
Irregularity Irregularity::fromJSON(json j) {
Irregularity Irregularity::from_JSON(const json& j) {
Source source = sourceFromString(j["Source"]);
string time_label = j["TimeLabel"];
IrregularityType type = irregularityTypeFromString(j["IrregularityType"]);
return Irregularity(source, time_label, type);
}
Source Irregularity::get_source() const {
return this->source;
}
string Irregularity::get_time_label() const {
return this->time_label;
}
std::optional<IrregularityType> Irregularity::get_type() const {
return this->type;
}
boost::uuids::uuid Irregularity::get_id() const {
return this->id;
}
std::optional<string> Irregularity::get_audio_URI() const {
return this->audio_URI;
}
Irregularity& Irregularity::set_audio_URI(string audio_URI) {
this->audio_URI = audio_URI;
return *this;
}
return Irregularity(
sourceFromString(j["Source"]),
j["TimeLabel"],
irregularityTypeFromString(j["IrregularityType"]),
j["ImageURI"]
);
std::optional<string> Irregularity::get_image_URI() const {
return this->image_URI;
}
Irregularity& Irregularity::set_image_URI(string image_URI) {
this->image_URI = image_URI;
return *this;
}
......@@ -12,18 +12,83 @@ using json = nlohmann::json;
* @brief an irregularity of the tape detected by the system
*
*/
struct Irregularity
{
class Irregularity {
private:
boost::uuids::uuid id;
Source source;
string time_label;
IrregularityType type;
string image_URI;
Irregularity(Source source, string time_label, IrregularityType type, string image_URI);
~Irregularity();
json toJSON();
static Irregularity fromJSON(json j);
std::optional<IrregularityType> type;
std::optional<string> image_URI;
std::optional<string> audio_URI;
public:
Irregularity(const Irregularity& other);
Irregularity(Irregularity&& other) noexcept;
Irregularity(Source source, string time_label);
Irregularity(Source source, string time_label, IrregularityType type);
~Irregularity() = default;
/**
* @brief Convert the Irregularity to a JSON object
*
* @return json
*/
json to_JSON() const;
/**
* @brief Create an Irregularity object from a JSON object
*
* @param j the JSON object
* @return Irregularity
*/
static Irregularity from_JSON(const json& j);
/**
* @brief Get the source object
*
* @return Source
*/
Source get_source() const;
/**
* @brief Get the time label object
*
* @return string
*/
string get_time_label() const;
/**
* @brief Get the type object
*
* @return IrregularityType
*/
std::optional<IrregularityType> get_type() const;
/**
* @brief Get the id object
*
* @return boost::uuids::uuid
*/
boost::uuids::uuid get_id() const;
/**
* @brief Get the audio URI object
*
* @return std::optional<string>
*/
std::optional<string> get_audio_URI() const;
/**
* @brief Get the image URI object
*
* @return std::optional<string>
*/
std::optional<string> get_image_URI() const;
/**
* @brief Set the audio URI object
*
* @param audio_URI
* @return Irregularity&
*/
Irregularity& set_audio_URI(string audio_URI);
/**
* @brief Set the image URI object
*
* @param image_URI
* @return Irregularity&
*/
Irregularity& set_image_URI(string image_URI);
};
#endif // IRREGULARITY_H
\ No newline at end of file
#include <exception>
#include "IrregularityFile.h"
#include <algorithm>
#include <iterator>
#include <memory>
IrregularityFile::IrregularityFile(uint16_t offset, std::list<Irregularity> irregularities)
: offset(offset), irregularities(irregularities) {}
IrregularityFile::IrregularityFile(std::optional<uint16_t> offset) : offset_(offset) {}
IrregularityFile::~IrregularityFile() {}
json IrregularityFile::toJSON() {
json j;
IrregularityFile::IrregularityFile(const IrregularityFile &rhs) {
std::transform(rhs.irregularities_.begin(), rhs.irregularities_.end(),
std::back_inserter(irregularities_),
[](const std::unique_ptr<Irregularity> &ptr) {
return std::make_unique<Irregularity>(*ptr);
});
}
j["Offset"] = this->offset;
IrregularityFile& IrregularityFile::add(std::unique_ptr<Irregularity> irregularity) {
irregularities_.push_back(std::move(irregularity));
return *this;
}
return j;
IrregularityFile& IrregularityFile::remove_by_id(const boost::uuids::uuid id) {
auto it = std::find_if(irregularities_.begin(), irregularities_.end(), [&id](const std::unique_ptr<Irregularity>& irregularity) {
return irregularity->get_id() == id;
});
if (it != irregularities_.end()) {
irregularities_.erase(it);
}
return *this;
}
IrregularityFile IrregularityFile::fromJSON(json j) {
throw "Not implemented";
IrregularityFile& IrregularityFile::sort() {
std::sort(irregularities_.begin(), irregularities_.end(), [](const std::unique_ptr<Irregularity>& a, const std::unique_ptr<Irregularity>& b) {
return a->get_time_label() < b->get_time_label();
});
return *this;
}
uint16_t IrregularityFile::getOffset() const {
return this->offset;
std::optional<uint16_t> IrregularityFile::get_offset() const {
return offset_;
}
std::list<Irregularity> IrregularityFile::getIrregularities() const {
return this->irregularities;
std::vector<std::unique_ptr<Irregularity>>::iterator IrregularityFile::begin() {
return irregularities_.begin();
}
IrregularityFile& IrregularityFile::add(const Irregularity irregularity) {
this->irregularities.push_back(irregularity);
return *this;
std::vector<std::unique_ptr<Irregularity>>::iterator IrregularityFile::end() {
return irregularities_.end();
}
IrregularityFile& IrregularityFile::remove_by_id(const boost::uuids::uuid id) {
for (auto it = this->irregularities.begin(); it != this->irregularities.end(); ++it) {
if (it->id == id) {
this->irregularities.erase(it);
break;
}
json IrregularityFile::toJSON() const {
json j;
j["Offset"] = offset_.value_or(0);
j["Irregularities"] = json::array();
for (const auto& irregularity : irregularities_) {
j["Irregularities"].push_back(irregularity->to_JSON());
}
return *this;
return j;
}
IrregularityFile& IrregularityFile::sort() {
this->irregularities.sort([](const Irregularity& a, const Irregularity& b) {
return a.time_label < b.time_label;
});
return *this;
}
\ No newline at end of file
IrregularityFile IrregularityFile::fromJSON(const json j) {
if (!j.contains("Offset") || !j.contains("Irregularities")) {
throw std::invalid_argument("Invalid JSON");
}
IrregularityFile irregularity_file(j["Offset"].get<uint16_t>());
for (const auto& irregularity : j["Irregularities"]) {
irregularity_file.add(std::make_unique<Irregularity>(Irregularity::from_JSON(irregularity)));
}
return irregularity_file;
}
#ifndef IRREGULARITY_FILE_H
#define IRREGULARITY_FILE_H
#include <list>
#include <vector>
#include <nlohmann/json.hpp>
#include "Irregularity.h"
#include <optional>
using json = nlohmann::json;
......@@ -11,25 +13,71 @@ using json = nlohmann::json;
* @brief An IrregularityFile is a collection of Irregularities detected on a tape.
*
*/
class IrregularityFile
{
private:
/* data */
uint16_t offset;
std::list<Irregularity> irregularities;
class IrregularityFile {
public:
IrregularityFile(uint16_t offset, std::list<Irregularity> irregularities);
~IrregularityFile();
/**
* @brief Create an IrregularityFile object from a JSON object
*
* @param j
* @return IrregularityFile
*/
static IrregularityFile fromJSON(const json j);
json toJSON();
uint16_t getOffset() const;
std::list<Irregularity> getIrregularities() const;
IrregularityFile& add(const Irregularity irregularity);
/**
* @brief Convert the IrregularityFile to a JSON object
*
* @return json
*/
json toJSON() const;
IrregularityFile(std::optional<uint16_t> offset = std::nullopt);
/**
* @brief Copy constructor
*
* @param rhs
*/
IrregularityFile(const IrregularityFile &rhs);
~IrregularityFile() {};
/**
* @brief Add an Irregularity to the IrregularityFile
*
* @param irregularity
* @return IrregularityFile&
*/
IrregularityFile& add(std::unique_ptr<Irregularity> irregularity);
/**
* @brief Remove an Irregularity from the IrregularityFile
*
* @param id
* @return IrregularityFile&
*/
IrregularityFile& remove_by_id(const boost::uuids::uuid id);
/**
* @brief Sort the IrregularityFile by time_label
*
* @return IrregularityFile&
*/
IrregularityFile& sort();
/**
* @brief Get the offset object
*
* @return std::optional<uint16_t>
*/
std::optional<uint16_t> get_offset() const;
/**
* @brief Get an iterator to the beginning of the IrregularityFile
*
* @return std::vector<std::unique_ptr<Irregularity>>::iterator
*/
std::vector<std::unique_ptr<Irregularity>>::iterator begin();
/**
* @brief Get an iterator to the end of the IrregularityFile
*
* @return std::vector<std::unique_ptr<Irregularity>>::iterator
*/
std::vector<std::unique_ptr<Irregularity>>::iterator end();
private:
std::optional<uint16_t> offset_;
std::vector<std::unique_ptr<Irregularity>> irregularities_;
};
#endif // IRREGULARITY_FILE_H
\ No newline at end of file
#endif // IRREGULARITY_FILE_HPP
#include <stdexcept>
#include "enums.h"
std::string sourceToString(Source source)
{
std::string sourceToString(Source source) {
switch (source) {
case Audio:
return "a";
......@@ -15,8 +14,7 @@ std::string sourceToString(Source source)
}
}
Source sourceFromString(std::string source)
{
Source sourceFromString(std::string source) {
if (source == "a")
return Audio;
else if (source == "v")
......@@ -27,8 +25,7 @@ Source sourceFromString(std::string source)
throw std::invalid_argument("Invalid Source");
}
std::string irregularityTypeToString(IrregularityType type)
{
std::string irregularityTypeToString(IrregularityType type) {
switch (type) {
case BRANDS_ON_TAPE:
return "b";
......@@ -63,8 +60,7 @@ std::string irregularityTypeToString(IrregularityType type)
}
}
IrregularityType irregularityTypeFromString(std::string type)
{
IrregularityType irregularityTypeFromString(std::string type) {
if (type == "b")
return BRANDS_ON_TAPE;
else if (type == "sp")
......
......@@ -26,11 +26,7 @@
* An Irregularity can be detected by the Audio analyser, the Video analyser or both.
*
*/
enum Source{
Audio,
Video,
Both
};
enum Source { Audio, Video, Both };
/**
* @enum IrregularityType
......
/**
* @mainpage MPAI CAE-ARP Video Analyser
* @file main.cpp
* MPAI CAE-ARP Video Analyser.
*
......@@ -10,7 +11,8 @@
* WARNING:
* Currently, this program is only compatible with the Studer A810 and videos recorded in PAL standard.
*
* @authors Nadir Dalla Pozza <nadir.dallapozza@unipd.it>, Matteo Spanio <dev2@audioinnova.com>
* @author Nadir Dalla Pozza <nadir.dallapozza@unipd.it>
* @author Matteo Spanio <dev2@audioinnova.com>
* @copyright 2023, Audio Innova S.r.l.
* @credits Niccolò Pretto, Nadir Dalla Pozza, Sergio Canazza
* @license GPL v3.0
......@@ -50,6 +52,8 @@
#include "lib/files.h"
#include "lib/colors.h"
#include "lib/time.h"
#include "lib/Irregularity.h"
#include "lib/IrregularityFile.h"
using namespace cv;
using namespace std;
......@@ -639,12 +643,12 @@ void processing(cv::VideoCapture videoCapture) {
if (frame.empty()) {
cout << endl << "Empty frame!" << endl;
videoCapture.release();
break;
return;
}
int msToEnd = video_length_ms - video_current_ms;
if (video_current_ms == 0) // With OpenCV library, this happens at the last few frames of the video before realising that "frame" is empty.
break;
return;
// Variables to display program status
int secToEnd = msToEnd / 1000;
......@@ -691,27 +695,9 @@ void processing(cv::VideoCapture videoCapture) {
cv::imwrite(irregularityImagesPath / irregularityImageFilename, oddFrame);
// Append Irregularity information to JSON
boost::uuids::uuid uuid = boost::uuids::random_generator()();
irregularityFileOutput1["Irregularities"] += {
{
"IrregularityID", boost::lexical_cast<string>(uuid)
}, {
"Source", "v"
}, {
"TimeLabel", timeLabel
}
};
irregularityFileOutput2["Irregularities"] += {
{
"IrregularityID", boost::lexical_cast<string>(uuid)
}, {
"Source", "v"
}, {
"TimeLabel", timeLabel
}, {
"ImageURI", irregularityImagesPath.string() + "/" + irregularityImageFilename
}
};
Irregularity irreg = Irregularity(Source::Video, timeLabel);
irregularityFileOutput1["Irregularities"] += irreg.to_JSON();
irregularityFileOutput2["Irregularities"] += irreg.set_image_URI(irregularityImagesPath.string() + "/" + irregularityImageFilename).to_JSON();
lastSaved = video_current_ms;
savedFrames++;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment