Skip to content
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

Image rotation action, usable in chains #677

Merged
merged 4 commits into from Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
112 changes: 102 additions & 10 deletions src/chain_actions.cc
Expand Up @@ -49,6 +49,10 @@ namespace dd
if (_params.has("save_crops"))
save_crops = _params.get("save_crops").get<bool>();

std::string save_path;
if (_params.has("save_path"))
save_path = _params.get("save_path").get<std::string>() + "/";

// iterate image batch
for (size_t i=0;i<vad.size();i++)
{
Expand Down Expand Up @@ -95,7 +99,7 @@ namespace dd
if (save_crops)
{
std::string puri = dd_utils::split(uri,'/').back();
cv::imwrite("crop_" + puri + "_" + std::to_string(j) + ".png",cropped_img);
cv::imwrite(save_path + "crop_" + puri + "_" + std::to_string(j) + ".png",cropped_img);
}
cropped_imgs.push_back(std::move(cropped_img));
}
Expand All @@ -106,7 +110,7 @@ namespace dd
ccls.add("classes",cad_cls);
cvad.push_back(ccls);
}
// store serialized crops into action output store
// store crops into action output store
APIData action_out;
action_out.add("data_raw_img",cropped_imgs);
action_out.add("cids",bbox_ids);
Expand All @@ -116,6 +120,86 @@ namespace dd
model_out.add("predictions",cvad);
}

void ImgsRotateAction::apply(APIData &model_out,
ChainData &cdata)
{
// get label
std::vector<APIData> vad = model_out.getv("predictions");
std::vector<cv::Mat> imgs = model_out.getobj("input").get("imgs").get<std::vector<cv::Mat>>();
std::vector<std::pair<int,int>> imgs_size = model_out.getobj("input").get("imgs_size").get<std::vector<std::pair<int,int>>>();
std::vector<cv::Mat> rimgs;
std::vector<std::string> uris;

// check for action parameters
std::string orientation = "relative"; // other: absolute
if (_params.has("orientation"))
orientation = _params.get("orientation").get<std::string>();

bool save_img = false;
if (_params.has("save_img"))
save_img = _params.get("save_img").get<bool>();

std::string save_path;
if (_params.has("save_path"))
save_path = _params.get("save_path").get<std::string>() + "/";

for (size_t i=0;i<vad.size();i++) // iterate predictions
{
std::string uri = vad.at(i).get("uri").get<std::string>();
uris.push_back(uri);
cv::Mat img = imgs.at(i);
std::vector<APIData> ad_cls = vad.at(i).getv("classes");
std::vector<APIData> cad_cls;

// rotate and make image available to next service
if (ad_cls.size() > 0)
{
std::string cat1 = ad_cls.at(0).get("cat").get<std::string>();
cv::Mat rimg, timg;
if (cat1 == "0") // all tests in absolute orientation
{
rimg = img;
}
else if (cat1 == "90")
{
cv::transpose(img,timg);
int orient = 1;
if (orientation == "relative")
orient = 0; // 270
cv::flip(timg,rimg,orient);
}
else if (cat1 == "180")
{
cv::flip(img,rimg,-1);
}
else if (cat1 == "270")
{
cv::transpose(img,timg);
int orient = 0;
if (orientation == "relative")
orient = 1; // 90
cv::flip(timg,rimg,orient);
}
if (!rimg.empty())
{
rimgs.push_back(rimg);

// save image if requested
if (save_img)
{
std::string puri = dd_utils::split(uri,'/').back();
cv::imwrite(save_path + "rot_" + puri + "_" + cat1 + ".png",rimg);
}
}
}
}
// store rotated images into action output store
APIData action_out;
action_out.add("data_raw_img",rimgs);
action_out.add("cids",uris);
cdata.add_action_data(_action_id,action_out);
}

void ClassFilter::apply(APIData &model_out,
ChainData &cdata)
{
Expand Down Expand Up @@ -168,19 +252,27 @@ namespace dd
if (_adc.has("id"))
action_id = _adc.get("id").get<std::string>();
else action_id = std::to_string(cdata._action_data.size());
if (action_type == "crop") {
if (action_type == "crop")
{
ImgsCropAction act(_adc, action_id, action_type);
act.apply(model_out, cdata);
} else if (action_type == "filter") {
}
else if (action_type == "rotate")
{
ImgsRotateAction act(_adc,action_id,action_type);
act.apply(model_out,cdata);
}
else if (action_type == "filter")
{
ClassFilter act(_adc, action_id, action_type);
act.apply(model_out, cdata);
}
}
#ifdef USE_DLIB
else if (action_type == "dlib_align_crop")
{
DlibAlignCropAction act(_adc,action_id,action_type);
act.apply(model_out,cdata);
}
else if (action_type == "dlib_align_crop")
{
DlibAlignCropAction act(_adc,action_id,action_type);
act.apply(model_out,cdata);
}
#endif
else {
throw ActionBadParamException("unknown action " + action_type);
Expand Down
16 changes: 15 additions & 1 deletion src/chain_actions.h
Expand Up @@ -94,6 +94,20 @@ namespace dd
ChainData &cdata);
};

class ImgsRotateAction : public ChainAction
{
public:
ImgsRotateAction(const APIData &adc,
const std::string &action_id,
const std::string &action_type)
:ChainAction(adc,action_id,action_type) {}

~ImgsRotateAction() {}

void apply(APIData &model_out,
ChainData &cdata);
};

class ClassFilter : public ChainAction
{
public:
Expand All @@ -117,7 +131,7 @@ namespace dd
void apply_action(const std::string &action_type,
APIData &model_out,
ChainData &cdata);

APIData _adc; /**< action ad object. */
};

Expand Down