|
1 | 1 | #include "build.cpp"
|
2 | 2 | #include "build.hpp"
|
3 | 3 |
|
| 4 | +namespace fs = std::filesystem; |
4 | 5 | using namespace itlab_2023;
|
5 | 6 |
|
6 | 7 | int main() {
|
7 |
| - std::string image_path = IMAGE1_PATH; |
8 |
| - cv::Mat image = cv::imread(image_path); |
9 |
| - if (image.empty()) { |
10 |
| - throw std::runtime_error("Failed to load image"); |
| 8 | + std::string image_folder = IMAGE1_PATH; |
| 9 | + std::vector<std::string> image_paths; |
| 10 | + |
| 11 | + for (const auto& entry : fs::directory_iterator(image_folder)) { |
| 12 | + if (entry.path().extension() == ".png") { |
| 13 | + image_paths.push_back(entry.path().string()); |
| 14 | + } |
11 | 15 | }
|
12 |
| - cv::cvtColor(image, image, cv::COLOR_BGR2GRAY); |
13 |
| - std::vector<cv::Mat> channels; |
14 | 16 |
|
15 |
| - cv::split(image, channels); |
| 17 | + if (image_paths.empty()) { |
| 18 | + throw std::runtime_error("No PNG images found in the folder"); |
| 19 | + } |
| 20 | + |
| 21 | + for (const auto& image_path : image_paths) { |
| 22 | + cv::Mat image = cv::imread(image_path); |
| 23 | + if (image.empty()) { |
| 24 | + std::cerr << "Failed to load image: " << image_path << std::endl; |
| 25 | + continue; |
| 26 | + } |
16 | 27 |
|
17 |
| - int count_pic = 1; |
18 |
| - std::vector<float> res(count_pic * 28 * 28); |
| 28 | + cv::cvtColor(image, image, cv::COLOR_BGR2GRAY); |
| 29 | + std::vector<cv::Mat> channels; |
| 30 | + cv::split(image, channels); |
19 | 31 |
|
20 |
| - for (int i = 0; i < 28; ++i) { |
21 |
| - for (int j = 0; j < 28; ++j) { |
22 |
| - res[i * 28 + j] = channels[0].at<uchar>(j, i); |
| 32 | + std::vector<float> res(28 * 28); |
| 33 | + for (int i = 0; i < 28; ++i) { |
| 34 | + for (int j = 0; j < 28; ++j) { |
| 35 | + res[i * 28 + j] = channels[0].at<uchar>(j, i); |
| 36 | + } |
23 | 37 | }
|
24 |
| - } |
25 |
| - Shape sh({static_cast<size_t>(count_pic), 1, 28, 28}); |
26 |
| - // move to reshape layer |
27 |
| - Tensor t = make_tensor<float>(res, sh); |
28 |
| - Tensor input = t; |
29 |
| - |
30 |
| - Shape sh1({1, 5, 5, 3}); |
31 |
| - std::vector<float> vec; |
32 |
| - vec.reserve(75); |
33 |
| - for (int i = 0; i < 75; ++i) { |
34 |
| - vec.push_back(3); |
35 |
| - } |
36 |
| - Tensor output = make_tensor(vec, sh1); |
37 | 38 |
|
38 |
| - build_graph(input, output, true); |
| 39 | + Shape sh({1, 1, 28, 28}); |
| 40 | + Tensor input = make_tensor<float>(res, sh); |
| 41 | + |
| 42 | + Shape sh1({1, 5, 5, 3}); |
| 43 | + std::vector<float> vec(75, 3); |
| 44 | + Tensor output = make_tensor(vec, sh1); |
| 45 | + |
| 46 | + build_graph(input, output, true); |
39 | 47 |
|
40 |
| - std::vector<float> tmp_output = softmax<float>(*output.as<float>()); |
41 |
| - for (size_t i = 0; i < tmp_output.size(); i++) { |
42 |
| - if (tmp_output[i] >= 1e-6) { |
43 |
| - std::cout << i << std::endl; |
| 48 | + std::vector<float> tmp_output = softmax<float>(*output.as<float>()); |
| 49 | + for (size_t i = 0; i < tmp_output.size(); i++) { |
| 50 | + if (tmp_output[i] >= 1e-6) { |
| 51 | + std::cout << "Image: " << image_path << " -> Class: " << i << std::endl; |
| 52 | + } |
44 | 53 | }
|
45 | 54 | }
|
| 55 | + return 0; |
46 | 56 | }
|
0 commit comments