利用百度AI接口实现车牌识别功能(三)

  • Post author:
  • Post category:其他


这里我简单做了一个客户端,方便识别车牌照片和查看车牌信息

实现代码如下

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("车牌识别");
    window_Init();
    QString API_Key = "uouz4ooWGueBdby82jzGzRBS";
    QString Secret_Key = "MtCoFde0OWMurzSY9ffxudH1Gqd475at";

    manager= new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(read_ack(QNetworkReply*)));

    connect(ui->pushButton_open,SIGNAL(clicked(bool)),this,SLOT(pushButton_open_clicked()));
    connect(ui->pushButton_discern,SIGNAL(clicked(bool)),this,SLOT(pushButton_discern_clicked()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::window_Init(){
    ui->label_show->setText("打开文件");
    ui->label_show->setAlignment(Qt::AlignCenter | Qt::AlignLeft);
    ui->label->setText("展示区域");
    ui->pushButton_open->setText("打开文件");
    ui->pushButton_discern->setText("识别");
    ui->label_result->setText("识别结果");
    ui->label_path->setText("文件路径");
}

与上篇文章不同的是,我又重新重复了一次创建应用的过程,如下


不同的地方是要将这个地方的车牌识别勾选上,如果这里不勾选的话是识别不成功的,会出现 Unsupported openapi method 等错误信息

然后将AK,SK,URL地址填入就可以了

最后将完整代码奉上

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("车牌识别");
    window_Init();
    QString API_Key = "uouz4ooWGueBdby82jzGzRBS";
    QString Secret_Key = "MtCoFde0OWMurzSY9ffxudH1Gqd475at";

    manager= new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(read_ack(QNetworkReply*)));

    connect(ui->pushButton_open,SIGNAL(clicked(bool)),this,SLOT(pushButton_open_clicked()));
    connect(ui->pushButton_discern,SIGNAL(clicked(bool)),this,SLOT(pushButton_discern_clicked()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::window_Init(){
    ui->label_show->setText("打开文件");
    ui->label_show->setAlignment(Qt::AlignCenter | Qt::AlignLeft);
    ui->label->setText("展示区域");
    ui->pushButton_open->setText("打开文件");
    ui->pushButton_discern->setText("识别");
    ui->label_result->setText("识别结果");
    ui->label_path->setText("文件路径");
}

void MainWindow::pushButton_open_clicked(){
    QString fileName = QFileDialog::getOpenFileName(this,tr("选择图片"),"./",tr("Image files(*.bmp *.jpg *.pbm *.pgm *.png *.ppm *.xbm *.xpm);All files (*.*)"));
    if(fileName.isEmpty()){
        QMessageBox msg;
        msg.warning(this,"error","打开失败");
        return;
    }
    ui->lineEdit_path->setText(fileName);
    QImage image(fileName);
    ui->label_show->setPixmap(QPixmap::fromImage(image));
    ui->label_show->setScaledContents(true);
    if(image.isNull()){
        QMessageBox::information(this,"信息","打开失败");
    }
}


void MainWindow::pushButton_discern_clicked(){
    myrequset.setUrl(QUrl("https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate?""access_token=24.4b315a9672a43bf28a3e941c50bfa938.2592000.1666411013.282335-27580391"));
    QString data =QString("grant_type=client_credentials&client_id=uOReQ28AusCHtFtxNXPvkNFM&client_secret=o3pPzoONBo0t189GC3mPUssa6Y82azkG&");
    myrequset.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");//设置http的头

    QFile file(ui->lineEdit_path->text());
    file.open(QIODevice::ReadOnly);
    QByteArray buf = file.readAll();

    //2.转成base64及urlcode
    QByteArray buf64 = buf.toBase64().toPercentEncoding();
    QByteArray body = "image="+buf64;

    //发送完整请求
    manager->post(myrequset,body);


}

void MainWindow::read_ack(QNetworkReply* r)
{
    //提取json格式的返回
    QByteArray buf = r->readAll();
    qDebug() << buf;

    //提取json中的感兴趣的内容
    QJsonDocument myjson = QJsonDocument::fromJson(buf);
    QJsonObject node = myjson.object();
    QJsonObject arr = node.value("words_result").toObject();
    QString text = arr.value("number").toString();
    ui->textEdit_show->setText(text);
}

最后效果



版权声明:本文为m0_72647108原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。