使用 OpenCV + 微信二维码引擎实现二维码识别

背景

今年自疫情以来,我都没有写过文章。一方面是疫情导致居家办公比较烦躁,另一方面最近有点懒了。但是工作还是要继续,趁这几天优化了一下最近的项目,我整理了一下如何使用 OpenCV 和微信二维码引擎来实现二维码的识别。

微信开源了其二维码的解码功能,并贡献给 OpenCV 社区。其开源的 wechat_qrcode 项目被收录到 OpenCV contrib 项目中。从 OpenCV 4.5.2 版本开始,就可以直接使用。

该项目 github 地址:github.com/opencv/open…

模型文件的地址:github.com/WeChatCV/op…

微信的扫码引擎,很早就支持了远距离二维码检测、自动调焦定位、多码检测识别等功能,它是基于 CNN 的二维码检测。

基于CNN的二维码检测器

二维码识别的封装

首先,定义一个 AlgoQrCode.h

#pragma once
#include <opencv2/opencv.hpp>
#include <opencv2/wechat_qrcode.hpp>
using namespace cv;
using namespace std;

class AlgoQRCode
{
private:
	Ptr<wechat_qrcode::WeChatQRCode> detector;

public:
	bool initModel(string modelPath);

	string detectQRCode(string strPath);

	bool compression(string inputFileName, string outputFileName, int quality);

	void release();
};
复制代码

该头文件定义了一些方法,包含了加载模型、识别二维码、释放资源等方法,以及一个 detector 对象用于识别二维码。

然后编写对应的源文件 AlgoQrCode.cpp

bool AlgoQRCode::initModel(string modelPath) {
	string detect_prototxt = modelPath + "detect.prototxt";
	string detect_caffe_model = modelPath + "detect.caffemodel";
	string sr_prototxt = modelPath + "sr.prototxt";
	string sr_caffe_model = modelPath + "sr.caffemodel";
	try
	{
		detector = makePtr<wechat_qrcode::WeChatQRCode>(detect_prototxt, detect_caffe_model, sr_prototxt, sr_caffe_model);
	}
	catch (const std::exception& e)
	{
		cout << e.what() << endl;
		return false;
	}

	return true;
}

string AlgoQRCode::detectQRCode(string strPath)
{
	if (detector == NULL) {
		return "-1";
	}

	vector<Mat> vPoints;
	vector<cv::String> vStrDecoded;
	Mat imgInput = imread(strPath, IMREAD_GRAYSCALE);
//	vStrDecoded = detector->detectAndDecode(imgInput, vPoints);
        ....
}

bool AlgoQRCode::compression(string inputFileName, string outputFileName, int quality) {
	Mat srcImage = imread(inputFileName);

	if (srcImage.data != NULL)
	{
		vector<int>compression_params;
		compression_params.push_back(IMWRITE_JPEG_QUALITY);
		compression_params.push_back(quality);     //图像压缩参数,该参数取值范围为0-100,数值越高,图像质量越高

		bool bRet = imwrite(outputFileName, srcImage, compression_params);

		return bRet;
	}

	return false;
}

void AlgoQRCode::release() {
	detector = NULL;
}
复制代码

其中:

  • initModel() 方法用于加载算法模型文件,必须先调用,并且只需要调用一次即可。

模型文件

  • detectQRCode() 方法需要根据业务场景,先对图像做很多预处理的工作,然后再进行二维码的识别。这些预处理的过程,不再本文的讨论范围之列,以后有机会单独写一篇文章。
  • compression() 方法用于压缩图像,因为我们使用工业相机拍摄,图片会很大大概30M+,所以在使用之前会先压缩一下。
  • release() 方法可以在程序结束时,释放 detector 对象。

识别二维码,其实就是调用 detector 对象的 detectAndDecode() 方法。

最后,写一个 main() 函数测试一下,是否可用:

20220216851652_compress

int main()
{
    AlgoQRCode algoQrCode = AlgoQRCode();
    algoQrCode.initModel("/Users/tony/IdeaProjects/creative-mirror-watcher/mirror/src/main/resources/");
    string value = algoQrCode.detectQRCode("/Users/tony/20220216851652_compress.jpeg");
    cout<<"value="<<value<<endl;
}
复制代码

执行结果,识别二维码的内容:

value={
  "osVersion" : "iOS 13.3",
  "model" : "苹果 iPhone X",
  "ip" : "10.184.17.170",
  "port" : 10123
}
复制代码

写到这里,基本上完成了二维码识别的封装,可以给上层平台编译对应的算法包了。

我们最终是需要使用 Java/Kotlin 在 Windows 平台上调用该 cv 程序。因为该项目是一款智能设备的上位机程序。所以还需要编写一个 jni 程序供 Java/Kotlin 调用,这个过程就不再阐述了。

最后,将 cv 程序和 jni 相关的代码最终编译成一个 dll 文件,供上位机程序调用,实现最终的需求。

总结

其实,上述代码可以供各种平台使用,无论是移动端、桌面端、服务端。微信开源了一款非常快速的二维码引擎,节省了我们原先大量的工作。

猜你喜欢

转载自juejin.im/post/7079313321446506532