频道分类

DELPHI XE10 百度车牌识别

作者:admin 来源: 日期:2019/4/13 9:35:23 人气: 标签:

 
一、申请百度账号


uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs,System.json, IPPeerClient, REST.Client,
  Data.Bind.Components, Data.Bind.ObjectScope, Vcl.StdCtrls, Vcl.Imaging.jpeg,
  Vcl.ExtCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,EncdDecd,
  IdHTTP;
二、获取access_token

方法1:restclient控件方式获取access_token

var
  strtemp: string;
  temp: tjsonobject;
begin
  RESTClient1.BaseURL := 'https://aip.baidubce.com/oauth/2.0/token';
  RESTRequest1.Params.Clear;
  RESTRequest1.AddParameter('grant_type', 'client_credentials');
  RESTRequest1.AddParameter('client_id', 'SErhXXNrcu6TtZg8ztNYvGDY');
  // baidu后台里的API Key
  RESTRequest1.AddParameter('client_secret',
    'SwYYpCNL6MAcl2hokkgR4rq003P3h1Ku'); // baidu后台里的Secret Key
  RESTRequest1.Execute;
  strtemp := RESTResponse1.Content;
  Memo1.Clear;
  Memo1.Lines.Add(strtemp);
方法2:idhttp控件方式获取access_token

Memo1.Clear;
  Memo1.Lines.Add(Getaccess_token('client_credentials',
    'SErhXXNrcu6TtZg8ztNYvGDY', 'SwYYpCNL6MAcl2hokkgR4rq003P3h1Ku'));
小函数

function Getaccess_token(grant_type, client_id, client_secret: string): string;
// 获取token
var
  idhttp2: TIdHTTP;
  authHost: string;
  tokSl: Tstringlist;
begin
  authHost := 'http://aip.baidubce.com/oauth/2.0/token';
  try
    tokSl := Tstringlist.Create;
    idhttp2 := TIdHTTP.Create(nil);
    tokSl.Add('grant_type=' + grant_type);
    tokSl.Add('client_id=' + client_id);
    tokSl.Add('client_secret=' + client_secret);
    Result := idhttp2.Post(authHost, tokSl);
  finally
    tokSl.Free;
    idhttp2.DisposeOf;
  end;
end;
三、先点击前面的按钮再点,显示token

var
  jo: tjsonobject;
  jv: tjsonvalue;
  jsonstr: string; // 要转换的json字符串
begin
 
  jo := nil;
  jsonstr := Memo1.Text;
 
  try
    jo := tjsonobject.Create;
    jo := tjsonobject.parsejsonvalue(tencoding.utf8.getbytes(jsonstr), 0)
      as tjsonobject;
 
    jv := jo.get('access_token').jsonvalue;
    Edit3.Text := jv.value;
    showmessage(jv.value);
  finally
    jo.Free;
  end;
四、restclient控件方式获取车牌

var
  strtemp: string;
  temp: tjsonobject;
  strm: TMemoryStream;
  ss: TStringStream;
  s: string;
begin
  if Form2.Image1.Picture.Graphic <> nil then
  begin
    strm := TMemoryStream.Create;
    Form2.Image1.Picture.SaveToStream(strm);
    ss := TStringStream.Create('');
    strm.Position := 0;
    EncodeStream(strm, ss); // 将内存流编码为base64字符流
    s := ss.DataString;
    strm.Free;
    ss.Free;
  end;
  RESTClient1.BaseURL :=
    'https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate';
  RESTRequest1.Params.Clear;
  RESTRequest1.AddParameter('access_token', Edit3.Text);
  RESTRequest1.AddParameter('image', s);
  RESTRequest1.Execute;
  strtemp := RESTResponse1.Content;
  Memo1.Clear;
  Memo1.Lines.Add(strtemp);
五、显示车牌号

var
  jo: tjsonobject;
  jo2:tjsonobject;
  jv: tjsonvalue;
  jsonstr: string; // 要转换的json字符串
  jsonstr2: string; // 要转换的json字符串
begin
//先取出嵌套的那个字符串
  jo := tjsonobject.parsejsonvalue(memo1.Text) as tjsonobject;
  jsonstr := jo.GetValue('words_result').ToString;
//再对这个字符串取值
  jo2:= tjsonobject.parsejsonvalue(jsonstr) as tjsonobject;
   jsonstr2:= jo.GetValue('words_result').ToString;
  try
    jv := jo2.get('number').jsonvalue;
    Edit1.Text := jv.value;
    showmessage(jv.value);
  finally
    jo.Free;
  end;
六、总结

过程是曲折的,结果还是可以,必要的时候还得看官方的文档
--------------------- 
作者:以后换名字 
来源:CSDN 
原文:https://blog.csdn.net/weixin_44387646/article/details/88816322