Skip to content

Commit

Permalink
KeyframeSelection: Add an option to name output keyframes consecutively
Browse files Browse the repository at this point in the history
By default, the selected keyframes are written with their index within
the input sequence / video as their name. If frames at index 15, 294 and
825 are selected as keyframes, they will be written as 00015.exr, 00294.exr
and 00825.exr.

This commit adds an option that allows to name them as consecutive frames
instead. Frames at index 15, 294 and 825 are now written as 00000.exr,
00001.exr and 00002.exr if the option is enabled.
  • Loading branch information
cbentejac committed Feb 8, 2023
1 parent 71a2089 commit 94beb5c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/aliceVision/keyframe/KeyframeSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ bool KeyframeSelector::computeScores(const std::size_t rescaledWidthSharpness, c
bool KeyframeSelector::writeSelection(const std::vector<std::string>& brands,
const std::vector<std::string>& models,
const std::vector<float>& mmFocals,
const bool renameKeyframes,
const std::string& outputExtension,
const image::EStorageDataType storageDataType) const
{
Expand Down Expand Up @@ -411,6 +412,7 @@ bool KeyframeSelector::writeSelection(const std::vector<std::string>& brands,
}
}

unsigned int outputKeyframeCnt = 0; // Used if the "renameKeyframes" option is enabled
for (const auto pos : _selectedKeyframes) {
if (!feed.goToFrame(pos)) {
ALICEVISION_LOG_ERROR("Invalid frame position. Ignoring this frame.");
Expand All @@ -431,7 +433,10 @@ bool KeyframeSelector::writeSelection(const std::vector<std::string>& brands,

fs::path folder = _outputFolder;
std::ostringstream filenameSS;
filenameSS << std::setw(5) << std::setfill('0') << pos << "." << outputExtension;
if (renameKeyframes)
filenameSS << std::setw(5) << std::setfill('0') << outputKeyframeCnt++ << "." << outputExtension;
else
filenameSS << std::setw(5) << std::setfill('0') << pos << "." << outputExtension;
const auto filepath = (processedOutputFolder / fs::path(filenameSS.str())).string();

image::ImageWriteOptions options;
Expand Down
3 changes: 2 additions & 1 deletion src/aliceVision/keyframe/KeyframeSelector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,13 @@ class KeyframeSelector
* @param[in] brands brand name for each camera
* @param[in] models model name for each camera
* @param[in] mmFocals focal in millimeters for each camera
* @param[in] renameKeyframes name output keyframes as consecutive frames instead of using their index as a name
* @param[in] outputExtension file extension of the written keyframes
* @param[in] storageDataType EXR storage data type for the output keyframes (ignored when the extension is not EXR)
* @return true if all the selected keyframes were successfully written, false otherwise
*/
bool writeSelection(const std::vector<std::string>& brands, const std::vector<std::string>& models,
const std::vector<float>& mmFocals, const std::string& outputExtension,
const std::vector<float>& mmFocals, const bool renameKeyframes, const std::string& outputExtension,
const image::EStorageDataType storageDataType = image::EStorageDataType::Undefined) const;

/**
Expand Down
8 changes: 7 additions & 1 deletion src/software/utils/main_keyframeSelection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ int aliceVision_main(int argc, char** argv)
std::string outputExtension = "exr"; // file extension of the written keyframes
image::EStorageDataType exrDataType = // storage data type for EXR output files
image::EStorageDataType::Float;
bool renameKeyframes = false; // name selected keyframes as consecutive frames instead of using their index as a name

// Debug options
bool exportScores = false; // export the sharpness and optical flow scores to a CSV file
Expand Down Expand Up @@ -87,6 +88,11 @@ int aliceVision_main(int argc, char** argv)
"so combining them with this parameter might cause the selection to stop before reaching the end of the "
"input sequence(s).\n"
"\t- For the smart method, the default value is set to 2000.")
("renameKeyframes", po::value<bool>(&renameKeyframes)->default_value(renameKeyframes),
"Instead of naming the keyframes according to their index in the input sequence / video, rename them as "
"consecutive frames, starting from 0.\n"
"If the selected keyframes should have originally be written as [00015.exr, 00294.exr, 00825.exr], they "
"will instead be written as [00000.exr, 00001.exr, 00002.exr] if this option is enabled.")
("outputExtension", po::value<std::string>(&outputExtension)->default_value(outputExtension),
"File extension of the output keyframes.")
("storageDataType", po::value<image::EStorageDataType>(&exrDataType)->default_value(exrDataType),
Expand Down Expand Up @@ -233,7 +239,7 @@ int aliceVision_main(int argc, char** argv)
selector.processRegular();

// Write selected keyframes
selector.writeSelection(brands, models, mmFocals, outputExtension, exrDataType);
selector.writeSelection(brands, models, mmFocals, renameKeyframes, outputExtension, exrDataType);

// If debug options are set, export the scores as a CSV file and / or the motion vectors as images
if (exportScores)
Expand Down

0 comments on commit 94beb5c

Please sign in to comment.