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

Fix fimg noblob (#90) #96

Merged
merged 2 commits into from
Apr 1, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 55 additions & 50 deletions src/ImageCommand.cc
Original file line number Diff line number Diff line change
Expand Up @@ -235,85 +235,90 @@ Json::Value FindImage::construct_responses(

Json::Value& findImage = responses[0];

assert(findImage.isMember("entities"));

if (findImage["status"] != 0) {
findImage["status"] = RSCommand::Error;
// Uses PMGD info error.
return error(findImage);
}

bool flag_empty = true;
Json::Value results = get_value<Json::Value>(cmd, "results");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I am quite confused with this commit. Shouldn't we check in construct_protobuf if blob == false and no list/count/average requested? Or is this relying on PMGDQueryHandler throwing an error and then handling it construct_responses? Ideally we should put it in JSON checks but I am guessing that's not possible. What am I missing?


for (auto& ent : findImage["entities"]) {
bool flag_empty = false;

if(!ent.isMember(VDMS_IM_PATH_PROP)){
continue;
}
std::string im_path = ent[VDMS_IM_PATH_PROP].asString();
ent.removeMember(VDMS_IM_PATH_PROP);
// Check if blob (image) must be returned
if (get_value<bool>(results, "blob", true)) {

if (ent.getMemberNames().size() > 0) {
flag_empty = false;
}
for (auto& ent : findImage["entities"]) {

try {
VCL::Image img(im_path);
assert(ent.isMember(VDMS_IM_PATH_PROP));

if (cmd.isMember("operations")) {
enqueue_operations(img, cmd["operations"]);
std::string im_path = ent[VDMS_IM_PATH_PROP].asString();
ent.removeMember(VDMS_IM_PATH_PROP);

if (ent.getMemberNames().size() == 0) {
flag_empty = true;
}

// We will return the image in the format the user
// request, or on its format in disk, except for the case
// of .tdb, where we will encode as png.
VCL::Image::Format format = img.get_image_format() != VCL::Image::Format::TDB ?
img.get_image_format() : VCL::Image::Format::PNG;
try {
VCL::Image img(im_path);

if (cmd.isMember("format")) {
std::string requested_format =
get_value<std::string>(cmd, "format");
if (cmd.isMember("operations")) {
enqueue_operations(img, cmd["operations"]);
}

if (requested_format == "png") {
format = VCL::Image::Format::PNG;
// We will return the image in the format the user
// request, or on its format in disk, except for the case
// of .tdb, where we will encode as png.
VCL::Image::Format format =
img.get_image_format() != VCL::Image::Format::TDB ?
img.get_image_format() : VCL::Image::Format::PNG;

if (cmd.isMember("format")) {
std::string requested_format =
get_value<std::string>(cmd, "format");

if (requested_format == "png") {
format = VCL::Image::Format::PNG;
}
else if (requested_format == "jpg") {
format = VCL::Image::Format::JPG;
}
else {
Json::Value return_error;
return_error["status"] = RSCommand::Error;
return_error["info"] = "Invalid Format for FindImage";
return error(return_error);
}
}
else if (requested_format == "jpg") {
format = VCL::Image::Format::JPG;

std::vector<unsigned char> img_enc;
img_enc = img.get_encoded_image(format);

if (!img_enc.empty()) {

std::string* img_str = query_res.add_blobs();
img_str->resize(img_enc.size());
std::memcpy((void*)img_str->data(),
(void*)img_enc.data(),
img_enc.size());
}
else {
Json::Value return_error;
return_error["status"] = RSCommand::Error;
return_error["info"] = "Invalid Format for FindImage";
return_error["info"] = "Image Data not found";
return error(return_error);
}
}

std::vector<unsigned char> img_enc;
img_enc = img.get_encoded_image(format);

if (!img_enc.empty()) {

std::string* img_str = query_res.add_blobs();
img_str->resize(img_enc.size());
std::memcpy((void*)img_str->data(),
(void*)img_enc.data(),
img_enc.size());
}
else {
} catch (VCL::Exception e) {
print_exception(e);
Json::Value return_error;
return_error["status"] = RSCommand::Error;
return_error["info"] = "Image Data not found";
return_error["info"] = "VCL Exception";
return error(return_error);
}
} catch (VCL::Exception e) {
print_exception(e);
Json::Value return_error;
return_error["status"] = RSCommand::Error;
return_error["info"] = "VCL Exception";
return error(return_error);
}
}


if (flag_empty) {
findImage.removeMember("entities");
}
Expand Down
38 changes: 38 additions & 0 deletions tests/python/TestImages.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,44 @@ def test_findImageNoBlob(self):
self.assertEqual(response[1]["FindImage"]["status"], 0)
self.assertEqual(len(img_array), 0)

def test_findImageRefNoBlobNoPropsResults(self):

db = self.create_connection()

prefix_name = "fimg_no_blob_no_res"

for i in range(0,2):
props = {}
props["name"] = prefix_name + str(i)
props["id"] = i
self.insertImage(db, props=props)

all_queries = []

for i in range(0,1):
constraints = {}
constraints["name"] = ["==", prefix_name + str(i)]

results = {}
results["blob"] = False
# results["list"] = ["name", "id"]

img_params = {}
img_params["constraints"] = constraints
img_params["results"] = results
img_params["_ref"] = 22

query = {}
query["FindImage"] = img_params

all_queries.append(query)

response, img_array = db.query(all_queries)
# print(db.get_last_response_str())

self.assertEqual(response[0]["FindImage"]["status"], 0)
self.assertEqual(len(img_array), 0)

def test_updateImage(self):

db = self.create_connection()
Expand Down