-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
792 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include "MemoryCamera.hpp" | ||
|
||
#include <opencv2/imgcodecs.hpp> | ||
|
||
namespace FairyCam | ||
{ | ||
|
||
bool MemoryCamera::grab() | ||
{ | ||
if (!m_is_open) | ||
return false; | ||
|
||
m_current = m_next; | ||
if (m_current == m_opts.images.cend()) | ||
return false; | ||
|
||
++m_next; | ||
if (m_opts.circular && m_next == m_opts.images.cend()) | ||
m_next = m_opts.images.begin(); | ||
|
||
return !m_current->empty(); | ||
} | ||
|
||
bool MemoryCamera::retrieve(cv::OutputArray image, int flag) | ||
{ | ||
if (!m_is_open || m_current == m_opts.images.cend()) | ||
return false; | ||
cv::Mat mat = *m_current; | ||
image.assign(mat); | ||
return !mat.empty(); | ||
} | ||
|
||
bool MemoryCamera::read(cv::OutputArray image) | ||
{ | ||
if (!grab()) | ||
{ | ||
image.assign(cv::Mat()); | ||
return false; | ||
} | ||
return retrieve(image); | ||
} | ||
|
||
} // namespace FairyCam |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#pragma once | ||
|
||
#include <filesystem> | ||
#include <opencv2/core.hpp> | ||
#include <vector> | ||
|
||
namespace FairyCam | ||
{ | ||
|
||
class MemoryCamera | ||
{ | ||
public: | ||
using ImageContainer = std::vector<cv::Mat>; | ||
struct Options | ||
{ | ||
|
||
ImageContainer images = {}; | ||
bool circular = false; | ||
}; | ||
|
||
private: | ||
Options m_opts; | ||
bool m_is_open = false; | ||
ImageContainer::const_iterator m_current; | ||
ImageContainer::const_iterator m_next; | ||
|
||
public: | ||
MemoryCamera() : MemoryCamera(Options{}) {} | ||
MemoryCamera(Options opts) | ||
: m_opts{std::move(opts)}, m_current{m_opts.images.cend()}, | ||
m_next{m_opts.images.cend()} | ||
{ | ||
} | ||
bool open(int idx, int apiPreference, const std::vector<int> ¶ms) | ||
{ | ||
m_is_open = true; | ||
m_next = m_opts.images.cbegin(); | ||
return true; | ||
} | ||
bool isOpened() const { return m_is_open; } | ||
void release() | ||
{ | ||
m_is_open = false; | ||
m_current = m_opts.images.cend(); | ||
m_next = m_opts.images.cend(); | ||
} | ||
bool grab(); | ||
bool retrieve(cv::OutputArray image, int flag = 0); | ||
bool read(cv::OutputArray image); | ||
bool set(int propId, double value) { return false; } | ||
double get(int propId) const { return -1.0; } | ||
void setExceptionMode(bool enable) {} | ||
bool getExceptionMode() const { return false; } | ||
MemoryCamera &operator>>(CV_OUT cv::Mat &image) | ||
{ | ||
read(image); | ||
return *this; | ||
} | ||
MemoryCamera &operator>>(CV_OUT cv::UMat &image) | ||
{ | ||
read(image); | ||
return *this; | ||
} | ||
}; | ||
|
||
} // namespace FairyCam |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
#pragma once | ||
|
||
#include "../AnyCamera.hpp" | ||
#include "Exceptions.hpp" | ||
#include "Sequence.hpp" | ||
#include <concepts> | ||
#include <memory> | ||
#include <opencv2/core.hpp> | ||
|
||
namespace FairyCam | ||
{ | ||
|
||
class ChaosCamera | ||
{ | ||
private: | ||
bool m_active_exception = true; | ||
AnyCamera m_cam; | ||
std::unique_ptr<Sequence> m_error_sequence; | ||
|
||
bool checkIsOpen() | ||
{ | ||
try | ||
{ | ||
m_error_sequence->checkIsOpen(); | ||
} | ||
catch (...) | ||
{ | ||
this->release(); | ||
if (!m_active_exception) | ||
return false; | ||
throw; | ||
} | ||
return true; | ||
} | ||
|
||
bool checkGrab() | ||
{ | ||
try | ||
{ | ||
m_error_sequence->checkGrab(); | ||
} | ||
catch (...) | ||
{ | ||
if (!m_active_exception) | ||
return false; | ||
throw; | ||
} | ||
return true; | ||
} | ||
|
||
bool checkRetrieve() | ||
{ | ||
try | ||
{ | ||
m_error_sequence->checkRetrieve(); | ||
} | ||
catch (...) | ||
{ | ||
if (!m_active_exception) | ||
return false; | ||
throw; | ||
} | ||
return true; | ||
} | ||
|
||
public: | ||
template <std::derived_from<Sequence> SeqT> | ||
ChaosCamera(AnyCamera &&camera, SeqT seq) | ||
: m_cam{std::move(camera)}, | ||
m_error_sequence{std::make_unique<SeqT>(std::move(seq))} | ||
{ | ||
} | ||
bool open(int idx, int apiPreference, const std::vector<int> ¶ms) | ||
{ | ||
if (!checkIsOpen()) | ||
return false; | ||
return m_cam.open(idx, apiPreference, params); | ||
} | ||
bool isOpened() const | ||
{ | ||
if (!const_cast<ChaosCamera *>(this)->checkIsOpen()) | ||
return false; | ||
return m_cam.isOpened(); | ||
} | ||
void release() { m_cam.release(); } | ||
bool grab() | ||
{ | ||
if (!isOpened()) | ||
return false; | ||
if (!checkGrab()) | ||
return false; | ||
if (!m_cam.grab()) | ||
{ | ||
if (m_active_exception) | ||
throw GrabException{}; | ||
return false; | ||
} | ||
return true; | ||
} | ||
bool retrieve(cv::OutputArray image, int flag = 0) | ||
{ | ||
if (!isOpened()) | ||
return false; | ||
if (!checkRetrieve()) | ||
return false; | ||
if (!m_cam.retrieve(image, flag)) | ||
{ | ||
if (m_active_exception) | ||
throw RetrieveException{}; | ||
return false; | ||
} | ||
return true; | ||
} | ||
bool read(cv::OutputArray image) | ||
{ | ||
if (!grab()) | ||
return false; | ||
return retrieve(image); | ||
} | ||
bool set(int propId, double value) { return false; } | ||
double get(int propId) const { return -1.0; } | ||
void setExceptionMode(bool enable) noexcept { m_active_exception = enable; } | ||
bool getExceptionMode() const noexcept { return m_active_exception; } | ||
ChaosCamera &operator>>(CV_OUT cv::Mat &image) | ||
{ | ||
read(image); | ||
return *this; | ||
} | ||
ChaosCamera &operator>>(CV_OUT cv::UMat &image) | ||
{ | ||
read(image); | ||
return *this; | ||
} | ||
}; | ||
|
||
} // namespace FairyCam |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
|
||
#include <stdexcept> | ||
|
||
namespace FairyCam | ||
{ | ||
|
||
class NotOpenException : public std::runtime_error | ||
{ | ||
public: | ||
NotOpenException() : std::runtime_error{"FairyCam closed the camera."} {} | ||
}; | ||
|
||
class RetrieveException : public std::runtime_error | ||
{ | ||
public: | ||
RetrieveException() : std::runtime_error{"FairyCam error on retrieve."} {} | ||
}; | ||
|
||
class GrabException : public std::runtime_error | ||
{ | ||
public: | ||
GrabException() : std::runtime_error{"FairyCam error on grab."} {} | ||
}; | ||
|
||
} // namespace FairyCam |
Oops, something went wrong.