|
| 1 | +#include <iostream> |
| 2 | +#include <opencv2/opencv.hpp> |
| 3 | +#include <opencv2/wechat_qrcode.hpp> |
| 4 | +#include <string> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +// function to visualize QR code detection results |
| 8 | +cv::Mat visualize(cv::Mat image, const std::vector<std::string>& results, |
| 9 | + const std::vector<cv::Mat>& points, |
| 10 | + cv::Scalar points_color = cv::Scalar(0, 255, 0), |
| 11 | + cv::Scalar text_color = cv::Scalar(0, 255, 0), |
| 12 | + double fps = -1) { |
| 13 | + cv::Mat output = image.clone(); |
| 14 | + |
| 15 | + if (fps >= 0) { |
| 16 | + cv::putText(output, "FPS: " + std::to_string(fps), cv::Point(0, 15), |
| 17 | + cv::FONT_HERSHEY_SIMPLEX, 0.5, text_color); |
| 18 | + } |
| 19 | + |
| 20 | + double fontScale = 0.5; |
| 21 | + int fontSize = 1; |
| 22 | + |
| 23 | + for (size_t i = 0; i < results.size(); ++i) { |
| 24 | + const auto& p = points[i]; |
| 25 | + |
| 26 | + // iterate through the mat to access points |
| 27 | + for (int r = 0; r < p.rows; ++r) { |
| 28 | + cv::Point point(p.at<float>(r, 0), p.at<float>(r, 1)); |
| 29 | + cv::circle(output, point, 10, points_color, -1); |
| 30 | + } |
| 31 | + |
| 32 | + // calculate QR code center |
| 33 | + int qrcode_center_x = (p.at<float>(0, 0) + p.at<float>(2, 0)) / 2; |
| 34 | + int qrcode_center_y = (p.at<float>(0, 1) + p.at<float>(2, 1)) / 2; |
| 35 | + |
| 36 | + // get text size |
| 37 | + int baseline = 0; |
| 38 | + cv::Size text_size = |
| 39 | + cv::getTextSize(results[i], cv::FONT_HERSHEY_DUPLEX, fontScale, |
| 40 | + fontSize, &baseline); |
| 41 | + |
| 42 | + // position text at the center of QR code |
| 43 | + cv::Point text_pos(qrcode_center_x - text_size.width / 2, |
| 44 | + qrcode_center_y + text_size.height / 2); |
| 45 | + |
| 46 | + // draw text |
| 47 | + cv::putText(output, results[i], text_pos, cv::FONT_HERSHEY_DUPLEX, |
| 48 | + fontScale, text_color, fontSize); |
| 49 | + } |
| 50 | + |
| 51 | + return output; |
| 52 | +} |
| 53 | + |
| 54 | +int main(int argc, char** argv) { |
| 55 | + // argument parsing |
| 56 | + cv::CommandLineParser parser( |
| 57 | + argc, argv, |
| 58 | + "{help h | | Show this help message.}" |
| 59 | + "{input i | | Set path to the input image. Omit for using default camera.}" |
| 60 | + "{detect_prototxt_path | detect_2021nov.prototxt | Set path to detect.prototxt.}" |
| 61 | + "{detect_model_path | detect_2021nov.caffemodel | Set path to detect.caffemodel.}" |
| 62 | + "{sr_model_path | sr_2021nov.caffemodel | Set path to sr.caffemodel.}" |
| 63 | + "{backend_target bt | 0 | Choose one of the backend-target pairs to run this demo.}" |
| 64 | + "{save s | false | Specify to save file with results (i.e. bounding box, confidence level). Invalid in case of camera input.}" |
| 65 | + "{vis v | false | Specify to open a new window to show results. Invalid in case of camera input.}"); |
| 66 | + |
| 67 | + if (parser.has("help")) { |
| 68 | + parser.printMessage(); |
| 69 | + return 0; |
| 70 | + } |
| 71 | + |
| 72 | + // backend-target pairs |
| 73 | + const std::vector<std::pair<int, int>> backend_target_pairs = { |
| 74 | + {cv::dnn::DNN_BACKEND_OPENCV, cv::dnn::DNN_TARGET_CPU}, |
| 75 | + {cv::dnn::DNN_BACKEND_CUDA, cv::dnn::DNN_TARGET_CUDA}, |
| 76 | + {cv::dnn::DNN_BACKEND_CUDA, cv::dnn::DNN_TARGET_CUDA_FP16}, |
| 77 | + {cv::dnn::DNN_BACKEND_TIMVX, cv::dnn::DNN_TARGET_NPU}, |
| 78 | + {cv::dnn::DNN_BACKEND_CANN, cv::dnn::DNN_TARGET_NPU}}; |
| 79 | + |
| 80 | + // get backend-target from arguments |
| 81 | + int backend_target_index = parser.get<int>("backend_target"); |
| 82 | + if (backend_target_index < 0 || |
| 83 | + backend_target_index >= backend_target_pairs.size()) { |
| 84 | + std::cerr << "Invalid backend-target index" << std::endl; |
| 85 | + return -1; |
| 86 | + } |
| 87 | + |
| 88 | + // get paths |
| 89 | + std::string detect_prototxt = parser.get<std::string>("detect_prototxt_path"); |
| 90 | + std::string detect_model = parser.get<std::string>("detect_model_path"); |
| 91 | + std::string sr_prototxt = parser.get<std::string>("sr_prototxt_path"); |
| 92 | + std::string sr_model = parser.get<std::string>("sr_model_path"); |
| 93 | + |
| 94 | + // initialize wechatqrcode detector |
| 95 | + cv::Ptr<cv::wechat_qrcode::WeChatQRCode> detector = |
| 96 | + cv::makePtr<cv::wechat_qrcode::WeChatQRCode>( |
| 97 | + detect_prototxt, detect_model, sr_prototxt, sr_model); |
| 98 | + |
| 99 | + // check if input is specified |
| 100 | + std::string input_path = parser.get<std::string>("input"); |
| 101 | + bool save_result = parser.get<bool>("save"); |
| 102 | + bool visualize_result = parser.get<bool>("vis"); |
| 103 | + |
| 104 | + if (!input_path.empty()) { |
| 105 | + // image processing |
| 106 | + cv::Mat image = cv::imread(input_path); |
| 107 | + if (image.empty()) { |
| 108 | + std::cerr << "Could not read the image" << std::endl; |
| 109 | + return -1; |
| 110 | + } |
| 111 | + |
| 112 | + std::vector<std::string> results; |
| 113 | + std::vector<cv::Mat> points; |
| 114 | + results = detector->detectAndDecode(image, points); |
| 115 | + |
| 116 | + for (const auto& result : results) { |
| 117 | + std::cout << result << std::endl; |
| 118 | + } |
| 119 | + |
| 120 | + // visualize results |
| 121 | + cv::Mat result_image = visualize(image, results, points); |
| 122 | + |
| 123 | + // save results if requested |
| 124 | + if (save_result) { |
| 125 | + cv::imwrite("result.jpg", result_image); |
| 126 | + std::cout << "Results saved to result.jpg" << std::endl; |
| 127 | + } |
| 128 | + |
| 129 | + // show visualization if requested |
| 130 | + if (visualize_result) { |
| 131 | + cv::imshow(input_path, result_image); |
| 132 | + cv::waitKey(0); |
| 133 | + } |
| 134 | + } else { |
| 135 | + // camera processing |
| 136 | + cv::VideoCapture cap(0); |
| 137 | + if (!cap.isOpened()) { |
| 138 | + std::cerr << "Error opening camera" << std::endl; |
| 139 | + return -1; |
| 140 | + } |
| 141 | + |
| 142 | + cv::Mat frame; |
| 143 | + cv::TickMeter tm; |
| 144 | + |
| 145 | + while (true) { |
| 146 | + cap >> frame; |
| 147 | + if (frame.empty()) { |
| 148 | + std::cout << "No frames grabbed" << std::endl; |
| 149 | + break; |
| 150 | + } |
| 151 | + |
| 152 | + std::vector<std::string> results; |
| 153 | + std::vector<cv::Mat> points; |
| 154 | + |
| 155 | + tm.start(); |
| 156 | + results = detector->detectAndDecode(frame, points); |
| 157 | + tm.stop(); |
| 158 | + |
| 159 | + double fps = tm.getFPS(); |
| 160 | + |
| 161 | + // visualize results |
| 162 | + cv::Mat result_frame = |
| 163 | + visualize(frame, results, points, cv::Scalar(0, 255, 0), |
| 164 | + cv::Scalar(0, 255, 0), fps); |
| 165 | + cv::imshow("WeChatQRCode Demo", result_frame); |
| 166 | + |
| 167 | + tm.reset(); |
| 168 | + |
| 169 | + if (cv::waitKey(1) >= 0) break; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return 0; |
| 174 | +} |
0 commit comments