72719643 2025-01-07 19:15 采纳率: 75%
浏览 38

mfc中wechat_qrcode无法解析二维码,如何修改

这段代码使用了wechat_qrcode,无法解析二维码,如何修改
环境:vs2022创建的mfc项目
使用:opencv - 4.10.0 + opencv_contrib
解析二维码的代码:

// 线程
UINT ThreadProc(LPVOID pParam)
{
    //加载图片解码
    Ptr<wechat_qrcode::WeChatQRCode> detector;
    string basepath = "D:/MyDatas/myapps/MFCApplication2/x64/Debug/";
    string detect_prototxt = basepath + "model/detect.prototxt";
    string detect_caffe_model = basepath + "model/detect.caffemodel";
    string sr_prototxt = basepath + "model/sr.prototxt";
    string sr_caffe_model = basepath + "model/sr.caffemodel";
    try
    {
        detector = makePtr<wechat_qrcode::WeChatQRCode>(detect_prototxt, detect_caffe_model,
            sr_prototxt, sr_caffe_model);
    }
    catch (const std::exception& e)
    {
        AfxMessageBox("Failed to initialize WeChatQRCode.\n"
            "Please, download 'detector.*' and 'sr.*' from\n"
            "https://github.com/WeChatCV/opencv_3rdparty/tree/wechat_qrcode\n"
            "and put them into the current directory.");
    }

    CMFCApplication2Dlg* ti = (CMFCApplication2Dlg*)pParam;
    //1.从摄像头读入视频
    VideoCapture cap(0);
    //2.循环显示每一帧
    while (1)
    {
        bool isrun;
        isrun = !(WaitForSingleObject(ti->event1, 5) == WAIT_OBJECT_0);
        if (!isrun)
        {
            break;
        }
        Mat cam;
        cap >> cam;//获取当前帧图像
        //imshow("camera", cam);//显示当前帧图像


        Mat img = cam;
        vector<Mat> vPoints;
        vector<String> strDecoded;
        strDecoded = detector->detectAndDecode(img, vPoints);
        for (int i = 0; i < strDecoded.size(); i++)
        {
            Point pt1 = Point((int)vPoints[i].at<float>(0, 0), (int)vPoints[i].at<float>(0, 1));
            Point pt2 = Point((int)vPoints[i].at<float>(1, 0), (int)vPoints[i].at<float>(1, 1));
            Point pt3 = Point((int)vPoints[i].at<float>(2, 0), (int)vPoints[i].at<float>(2, 1));
            Point pt4 = Point((int)vPoints[i].at<float>(3, 0), (int)vPoints[i].at<float>(3, 1));
            line(img, pt1, pt2, Scalar(0, 255, 0), 2);
            line(img, pt2, pt3, Scalar(0, 255, 0), 2);
            line(img, pt3, pt4, Scalar(0, 255, 0), 2);
            line(img, pt4, pt1, Scalar(0, 255, 0), 2);
            putText(img, strDecoded[i], pt1, 0, 0.5, Scalar(255, 0, 0), 2);
        }
        imshow("wechat_qrcode", img);

        waitKey(5);
    }
    destroyAllWindows();
    cap.release();
    return 0;
}

Ptr<wechat_qrcode::WeChatQRCode> detector 加载完毕,
detector->detectAndDecode无法解析二维码,如何修改

  • 写回答

2条回答 默认 最新

  • 檀越@新空间 2025-01-07 19:15
    关注

    晚上好🌙🌙🌙
    本答案参考通义千问

    根据你的描述,wechat_qrcode::WeChatQRCode 无法正确解析二维码。这可能是由于多种原因造成的,例如模型文件路径错误、模型文件不完整或损坏、或者代码中的某些逻辑问题。以下是一些可能的解决方案:

    解决方案

    1. 检查模型文件路径和完整性

      • 确保模型文件 detect.prototxtdetect.caffemodel 以及 sr.prototxtsr.caffemodel 已经正确下载并且放置在指定的目录中。
      • 确认文件路径是否正确。
    2. 确保模型文件已下载且未损坏

      • 重新下载模型文件,并确保它们没有损坏。
      • 可以尝试重新下载模型文件,确保文件完整且未被修改。
    3. 检查模型文件是否与版本兼容

      • 确认使用的模型文件与 wechat_qrcode 库的版本兼容。
    4. 调试代码中的异常处理

      • 在加载模型时添加更多的调试信息,以便更好地了解加载过程中的问题。
    5. 检查输入图像

      • 确认输入图像是否包含有效的二维码。

    修改后的代码

    UINT ThreadProc(LPVOID pParam)
    {
        // 加载图片解码
        Ptr<wechat_qrcode::WeChatQRCode> detector;
        string basepath = "D:/MyDatas/myapps/MFCApplication2/x64/Debug/";
        string detect_prototxt = basepath + "model/detect.prototxt";
        string detect_caffe_model = basepath + "model/detect.caffemodel";
        string sr_prototxt = basepath + "model/sr.prototxt";
        string sr_caffe_model = basepath + "model/sr.caffemodel";
    
        try
        {
            detector = makePtr<wechat_qrcode::WeChatQRCode>(detect_prototxt, detect_caffe_model,
                sr_prototxt, sr_caffe_model);
        }
        catch (const std::exception& e)
        {
            AfxMessageBox("Failed to initialize WeChatQRCode.\n"
                "Please, download 'detector.*' and 'sr.*' from\n"
                "https://github.com/WeChatCV/opencv_3rdparty/tree/wechat_qrcode\n"
                "and put them into the current directory.\n"
                "Error message: " + string(e.what()));
        }
    
        CMFCApplication2Dlg* ti = (CMFCApplication2Dlg*)pParam;
        // 1. 从摄像头读入视频
        VideoCapture cap(0);
        // 2. 循环显示每一帧
        while (1)
        {
            bool isrun;
            isrun = !(WaitForSingleObject(ti->event1, 5) == WAIT_OBJECT_0);
            if (!isrun)
            {
                break;
            }
            Mat cam;
            cap >> cam; // 获取当前帧图像
            // imshow("camera", cam); // 显示当前帧图像
    
            Mat img = cam.clone(); // 确保 img 是独立的副本
            vector<Mat> vPoints;
            vector<String> strDecoded;
            strDecoded = detector->detectAndDecode(img, vPoints);
    
            if (strDecoded.empty())
            {
                AfxMessageBox("No QR codes detected.");
            }
            else
            {
                for (int i = 0; i < strDecoded.size(); i++)
                {
                    Point pt1 = Point((int)vPoints[i].at<float>(0, 0), (int)vPoints[i].at<float>(0, 1));
                    Point pt2 = Point((int)vPoints[i].at<float>(1, 0), (int)vPoints[i].at<float>(1, 1));
                    Point pt3 = Point((int)vPoints[i].at<float>(2, 0), (int)vPoints[i].at<float>(2, 1));
                    Point pt4 = Point((int)vPoints[i].at<float>(3, 0), (int)vPoints[i].at<float>(3, 1));
                    line(img, pt1, pt2, Scalar(0, 255, 0), 2);
                    line(img, pt2, pt3, Scalar(0, 255, 0), 2);
                    line(img, pt3, pt4, Scalar(0, 255, 0), 2);
                    line(img, pt4, pt1, Scalar(0, 255, 0), 2);
                    putText(img, strDecoded[i], pt1, 0, 0.5, Scalar(255, 0, 0), 2);
                }
            }
            imshow("wechat_qrcode", img);
    
            waitKey(5);
        }
        destroyAllWindows();
        cap.release();
        return 0;
    }
    

    详细说明

    1. 检查模型文件路径和完整性

      • 确保模型文件路径正确无误,并且文件完整且未损坏。
    2. 调试异常处理

      • 在异常处理中添加详细的错误信息,以便更好地定位问题。
    3. 检查输入图像

      • 确保输入图像包含有效的二维码。可以在控制台输出一些调试信息,确认图像是否正确读取。
    4. 图像副本

      • 在处理图像时,使用 img.clone() 确保 img 是独立的副本,避免原图被修改。

    通过这些步骤,你应该能够更好地诊断和解决 wechat_qrcode::WeChatQRCode 无法解析二维码的问题。

    评论

报告相同问题?

问题事件

  • 创建了问题 1月7日