| 136 | } |
| 137 | |
| 138 | Status SaveImage(const Tensor& tensor, const string& file_path) { |
| 139 | LOG(INFO) << "Saving image to " << file_path; |
| 140 | CHECK(tensorflow::str_util::EndsWith(file_path, ".png")) |
| 141 | << "Only saving of png files is supported."; |
| 142 | |
| 143 | auto root = tensorflow::Scope::NewRootScope(); |
| 144 | using namespace ::tensorflow::ops; // NOLINT(build/namespaces) |
| 145 | |
| 146 | string encoder_name = "encode"; |
| 147 | string output_name = "file_writer"; |
| 148 | |
| 149 | tensorflow::Output image_encoder = |
| 150 | EncodePng(root.WithOpName(encoder_name), tensor); |
| 151 | tensorflow::ops::WriteFile file_saver = tensorflow::ops::WriteFile( |
| 152 | root.WithOpName(output_name), file_path, image_encoder); |
| 153 | |
| 154 | tensorflow::GraphDef graph; |
| 155 | TF_RETURN_IF_ERROR(root.ToGraphDef(&graph)); |
| 156 | |
| 157 | std::unique_ptr<tensorflow::Session> session( |
| 158 | tensorflow::NewSession(tensorflow::SessionOptions())); |
| 159 | TF_RETURN_IF_ERROR(session->Create(graph)); |
| 160 | std::vector<Tensor> outputs; |
| 161 | TF_RETURN_IF_ERROR(session->Run({}, {}, {output_name}, &outputs)); |
| 162 | |
| 163 | return Status::OK(); |
| 164 | } |
| 165 | |
| 166 | // Reads a model graph definition from disk, and creates a session object you |
| 167 | // can use to run it. |
no test coverage detected