delphi restrequest 调用百度车牌识别接口

原文:DELPHI XE10 百度车牌识别_DELPHI 各种代码-CSDN博客

批量获取pic目录下的车牌。memo2给出识别出的车牌

需要百度账号 申请免费的车牌识别 sk  ak 。

delphi restrequest 调用百度车牌识别接口

 

原文是单个的。我做成批量的

你可以先用postman测试下。postman测试通了。中间好多问题。主要是就rest控件的错误返回值一直不知道什么意思,直接就是感叹号。postman可以返回错误值。所以 每一步都用button来测试。通过后改函数。再给穿起来

 

unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    IdHTTP1: TIdHTTP;
    RESTClient1: TRESTClient;
    RESTResponse1: TRESTResponse;
    btn1: TButton;
    memo1: TMemo;
    RESTRequest1: TRESTRequest;
    Button1: TButton;
    Edit1: TEdit;
    Button2: TButton;
    Edit2: TEdit;
    Edit3: TEdit;
    Button3: TButton;
    Button4: TButton;
    Image1: TImage;
    Memo2: TMemo;
    Button5: TButton;
    Button6: TButton;
    procedure btn1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);


  private
    { Private declarations }
  public
    { Public declarations }
     procedure GetDirFileList(listcss: tstrings);
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}


{ 车牌测试

25043853

2kTzwML1LiRUbhbF1ABtukAM

1GUAQ2T20PUfKmupFk40gC8HahljhGDc  }
function getchepaistream(img:TImage;tk:string): string;
var
  strtemp: string;
  temp: tjsonobject;
  strm: TMemoryStream;
  ss: TStringStream;
  s: string;
begin
with Form2 do
begin
        if Form2.Image1.Picture.Graphic <> nil then
  begin
    strm := TMemoryStream.Create;
    img.Picture.Graphic.SaveToStream(strm);
    ss := TStringStream.Create('');
    strm.Position := 0;
    EncodeStream(strm, ss); // 将内存流编码为base64字符流
    s := ss.DataString;
    memo1.Clear;
    memo1.Lines.Add(s);
    Result:=s;
    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.AddParameter('image', Memo2.Lines.Text);  //这个是已经转码base64的图片。我一直以为是转码的问题。没想到是restrequest空间的属性问题 method=rmPost才可以
  RESTRequest1.Execute;
  strtemp := RESTResponse1.Content;
  Memo1.Clear;
  Memo1.Lines.Add(strtemp);
  Result:=strtemp;
  Sleep(5000);

end;

end;
function getchepai(s:string): string;
var
  jo: tjsonobject;
  jo2:tjsonobject;
  jv: tjsonvalue;
  jsonstr: string; // 要转换的json字符串
  jsonstr2: string; // 要转换的json字符串
begin
//先取出嵌套的那个字符串
  jo := tjsonobject.parsejsonvalue(s) 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;
    Result := jv.value;
    //showmessage(jv.value);
  finally
    jo.Free;
  end;

end;

procedure TForm2.GetDirFileList(listcss: tstrings);
var
  lSearchRec: TSearchRec;
  lFind: integer;
  lPath: string;
  dirName : string;
begin
  lPath := ExtractFileDir(ParamStr(0))+'\pic\';
  if DirectoryExists(lPath) then
  begin
    lFind := FindFirst(lPath+'*.*',faAnyFile, lSearchRec);  //这里可以设置,获取文件类型
    listcss.Clear;
    while lFind = 0 do
    begin
      dirName:=lSearchRec.Name;
      if (dirName <> '.')
      and (dirName <> '..')
      and (FileExists(lPath+dirName)) then
      listcss.Add(dirName);
      lFind := FindNext(lSearchRec);
    end;
  end;

end;
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;

procedure TForm2.btn1Click(Sender: TObject);
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', '2kTzwML1LiRUbhbF1ABtukAM');
  // baidu后台里的API Key
  RESTRequest1.AddParameter('client_secret',
    '1GUAQ2T20PUfKmupFk40gC8HahljhGDc'); // baidu后台里的Secret Key
  RESTRequest1.Execute;
  strtemp := RESTResponse1.Content;
  Memo1.Clear;
  Memo1.Lines.Add(strtemp);
  Sleep(5000);
  Button2.Click;
end;


procedure TForm2.Button1Click(Sender: TObject);
begin
Memo1.Clear;
  Memo1.Lines.Add(Getaccess_token('client_credentials','2kTzwML1LiRUbhbF1ABtukAM', '1GUAQ2T20PUfKmupFk40gC8HahljhGDc'));
end;

procedure TForm2.Button2Click(Sender: TObject);
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;
end;
procedure TForm2.Button3Click(Sender: TObject);
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;

end;



procedure TForm2.Button4Click(Sender: TObject);
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.Graphic.SaveToStream(strm);
    ss := TStringStream.Create('');
    strm.Position := 0;
    EncodeStream(strm, ss); // 将内存流编码为base64字符流
    s := ss.DataString;
    memo1.Clear;
    memo1.Lines.Add(s);
    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.AddParameter('image', Memo2.Lines.Text);  //这个是已经转码base64的图片。我一直以为是转码的问题。没想到是restrequest空间的属性问题 method=rmPost才可以,base64可以用在线等工具直接转好。你可以先用postman测试下。postman测试通了。中间好多问题。主要是就rest控件的错误返回值一直不知
  RESTRequest1.Execute;
  strtemp := RESTResponse1.Content;
  Memo1.Clear;
  Memo1.Lines.Add(strtemp);
  Sleep(5000);
  Edit2.Text:=getchepai(memo1.Text);
end;

procedure TForm2.Button5Click(Sender: TObject);
var
img:TImage;
sstream:string;
begin
img:=TImage.Create(self);
img.Picture.LoadFromFile(ExtractFileDir(ParamStr(0))+'\pic\'+'mmexport1634290282264.jpg');
sstream:=getchepaistream(img,Edit3.Text);
sleep(2000);
Edit1.Text:=getchepai(sstream);
img.Free;
end;

procedure TForm2.Button6Click(Sender: TObject);
var
  lSearchRec: TSearchRec;
  lFind: integer;
  s,lPath: string;
  dirName : string;
  //listcss: tstrings;
  img:TImage;

begin
  img:=TImage.Create(self);
  lPath := ExtractFileDir(ParamStr(0))+'\pic\';
  if DirectoryExists(lPath) then
  begin
    lFind := FindFirst(lPath+'*.*',faAnyFile, lSearchRec);  //这里可以设置,获取文件类型
    //listcss.Clear;
    while lFind = 0 do
    begin
      dirName:=lSearchRec.Name;
      if (dirName <> '.')
      and (dirName <> '..')
      and (FileExists(lPath+dirName)) then
      //listcss.Add(dirName);
      begin
      Image1.Picture.LoadFromFile(lPath+dirName); //用对象也可以,用页面控件最好。直观点
      //img.Picture.LoadFromFile(lPath+dirName);
      s:=getchepaistream(Image1,Edit3.Text);
      sleep(2000);
      Memo2.Lines.Add(getchepai(s));
      end;
      lFind := FindNext(lSearchRec);
      Application.ProcessMessages;  //这里不加,页面的图片就不会跟着加载
    end;
  end;
  img.Free;

end;

procedure TForm2.FormCreate(Sender: TObject);
begin
//GetDirFileList(Memo2.Lines);
Memo2.Clear;
end;

end.

上一篇:Delphi DBGrid TGridDrawState


下一篇:PhpStorm和WAMP配置调试参数,问题描述Error. Interpreter is not specified or invalid. Press “Fix” to edit your project configuration.