-
Notifications
You must be signed in to change notification settings - Fork 339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Have the (cli) encoder skip unextractable frames #114
Changes from all commits
7c7e271
b97cedd
8ec60b8
6b540f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,7 @@ TEST_CASE( "EncoderTest/testFountain.4c", "[unit]" ) | |
enc.set_legacy_mode(); | ||
assertEquals( 3, enc.encode_fountain(inputFile, outPrefix, 0) ); | ||
|
||
std::vector<uint64_t> hashes = {0xbb1cc62b662abfe5, 0xf586f6466a5b194, 0x8c2f0f40e6ecb08b}; | ||
std::vector<uint64_t> hashes = {0xbb1cc62b662abfe5, 0xf586f6466a5b194, 0x93a3830d042966e1}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Turns out we were hitting the error case here! |
||
for (unsigned i = 0; i < hashes.size(); ++i) | ||
{ | ||
DYNAMIC_SECTION( "are we correct? : " << i ) | ||
|
@@ -93,7 +93,7 @@ TEST_CASE( "EncoderTest/testFountain.Compress", "[unit]" ) | |
Encoder enc(30, 4, 2); | ||
assertEquals( 1, enc.encode_fountain(inputFile, outPrefix) ); | ||
|
||
uint64_t hash = 0xb36b65402eec434e; | ||
uint64_t hash = 0xa66a666543280e8e; | ||
std::string path = fmt::format("{}_0.png", outPrefix); | ||
cv::Mat img = cv::imread(path); | ||
assertEquals( hash, image_hash::average_hash(img) ); | ||
|
@@ -131,12 +131,12 @@ TEST_CASE( "EncoderTest/testFountain.Size", "[unit]" ) | |
std::string outPrefix = tempdir.path() / "encoder.fountain"; | ||
|
||
Encoder enc(30, 4, 2); | ||
assertEquals( 1, enc.encode_fountain(inputFile, outPrefix, 16, 1.6, 1080) ); | ||
assertEquals( 1, enc.encode_fountain(inputFile, outPrefix, 16, 1.6, 1024) ); | ||
|
||
uint64_t hash = 0xbdc232c714226fe6; | ||
uint64_t hash = 0xa66a666543280e8e; | ||
std::string path = fmt::format("{}_0.png", outPrefix); | ||
cv::Mat img = cv::imread(path); | ||
assertEquals( 1080, img.rows ); | ||
assertEquals( 1080, img.cols ); | ||
assertEquals( 1024, img.rows ); | ||
assertEquals( 1024, img.cols ); | ||
assertEquals( hash, image_hash::average_hash(img) ); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
#pragma once | ||
|
||
#include "Anchor.h" | ||
#include "Corners.h" | ||
#include "Point.h" | ||
#include "ScanState.h" | ||
|
||
|
@@ -10,7 +11,6 @@ | |
#include <iostream> | ||
#include <vector> | ||
|
||
class Corners; | ||
class Midpoints; | ||
|
||
class Scanner | ||
|
@@ -33,6 +33,10 @@ class Scanner | |
|
||
static unsigned nextPowerOfTwoPlusOne(unsigned v); // helper | ||
|
||
// external helper method | ||
template <typename MAT> | ||
static bool will_it_scan(const MAT& img); | ||
|
||
// rest of public interface | ||
std::vector<Anchor> scan(); | ||
std::vector<point<int>> scan_edges(const Corners& corners, Midpoints& mps) const; | ||
|
@@ -98,6 +102,27 @@ inline unsigned Scanner::nextPowerOfTwoPlusOne(unsigned v) | |
return std::max(3U, v + 2); | ||
} | ||
|
||
template <typename MAT> | ||
inline bool Scanner::will_it_scan(const MAT& unpadded_img) | ||
{ | ||
Scanner scanner(unpadded_img); | ||
std::vector<Anchor> points = scanner.scan(); | ||
if (points.size() < 4) | ||
return false; | ||
|
||
constexpr int limit = 50; | ||
Corners corners(points); | ||
if (corners.top_left().x() > limit or corners.top_left().y() > limit) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The check here is pretty simple. We run an extract on a freshly encoded image -- no blurring or skewing or any other camera-induced weirdness. We don't even need error correction for an image like this, it's pristine. The corners the scanner finds should be right where we expect them: at the edges of the image. But if the (essentially random) data pattern is adversarial enough, the scanner will find a "corner" elsewhere, and we can detect/reject this frame as bad. |
||
return false; | ||
if (corners.top_right().x() < (unpadded_img.cols - limit) or corners.top_right().y() > limit) | ||
return false; | ||
if (corners.bottom_left().x() > limit or corners.bottom_left().y() < (unpadded_img.rows - limit)) | ||
return false; | ||
if (corners.bottom_right().x() < (unpadded_img.cols - limit) or corners.bottom_right().y() < (unpadded_img.rows - limit)) | ||
return false; | ||
return true; | ||
} | ||
|
||
template <typename MAT, typename MAT2> | ||
inline void Scanner::threshold_fast(const MAT& img, MAT2& out) | ||
{ | ||
|
@@ -141,10 +166,10 @@ inline void Scanner::preprocess_image(const MAT& img, MAT2& out, bool fast) | |
|
||
template <typename MAT> | ||
inline Scanner::Scanner(const MAT& img, bool fast, bool dark, int skip) | ||
: _dark(dark) | ||
, _skip(skip? skip : std::min(img.rows, img.cols) / 60) | ||
, _mergeCutoff(img.cols / 30) | ||
, _anchorSize(30) | ||
: _dark(dark) | ||
, _skip(skip? skip : std::min(img.rows, img.cols) / 60) | ||
, _mergeCutoff(img.cols / 30) | ||
, _anchorSize(30) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking it might be time to |
||
{ | ||
_img = preprocess_image(img, fast); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I expect that the likely way we hit this message is that the Scanner code gets messed up and everything (or many things) fail to scan. We could risk the infinite loop since that kind of failure should get caught by tests/etc, but... this seems smarter to me.