Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Go license plate recognition (network picture | local picture)

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

First register a Baidu Cloud account, and then select artificial intelligence text recognition to create an application. Get the appkey secret.

Network picture package mainimport ("encoding/base64", "encoding/json", "io/ioutil", "log", "net/http"net/url") func main () {handler: = PlateHandler {} appKey: = "11111" secret: = "11111111z" accessToken: = handler.GetAccessToken (appKey, secret) log.Println ("accessToken:" AccessToken) pictureUrl: = "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2181674788,2215933125&fm=26&gp=0.jpg" plate,err:=handler.GetPlate (pictureUrl,accessToken) if erratic license nil {log.Fatal (" failed to get license plate ", err)} log.Println (" license plate obtained: " Plate)} type accessTokenInfo struct {AccessToken string `json: "access_token" `ExpiresIn int64 `json: "expires_in" `} type WordResult struct {Number string `json: "number"`} type Data struct {WordsResult WordResult `json: "words_result" `} type PlateHandler struct {} func (handler * PlateHandler) GetAccessToken (appKey string, appSecret string) (accessToken string) {url: = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id="+appKey+"&client_secret="+appSecret response Err: = http.Get (url) if err! = nil {log.Fatal (err) return ""} data, err: = ioutil.ReadAll (response.Body) if err! = nil {log.Fatal (err) return ""} info: = accessTokenInfo {} json.Unmarshal (data, & info) log.Print ("data returned by request accessToken:", string (data) return info.AccessToken} func (handler * PlateHandler) GetPlate (picture_url string) AccessToken string) (plate string, err error) {rsp, err: = http.Get (picture_url) if err! = nil {log.Fatal (err) return ", err} image, _: = ioutil.ReadAll (rsp.Body) image_value, err2: = url.Parse (base64.StdEncoding.EncodeToString (image)) if err2! = nil {log.Fatal (err) return" Err} to_url: = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate?access_token="+accessToken values: = url.Values {} values.Add (" image ", image_value.EscapedPath ()) values.Add (" multi_detect "," false ") rsp2, err: = http.PostForm (to_url, values) defer rsp2.Body.Close () if err! = nil {log.Fatal (err) return" Err} data, err: = ioutil.ReadAll (rsp2.Body) if err! = nil {log.Fatal (err) return ", err} log.Println (" data returned by request for license plate: ", string (data)) m: = Data {} err = json.Unmarshal (data, & m) if err! = nil {log.Fatal (err) return", err} log.Println (m) return m.WordsResult.Number Nil} Local Picture package mainimport ("encoding/base64"encoding/json"io/ioutil"log"net/http"net/url"os") func main () {handler: = PlateHandler {} appKey: = "111111111" secret: = "111111111" accessToken: = handler.GetAccessToken (appKey, secret) log.Println ("accessToken:", accessToken) pictureUrl: = "day02/img/1.png" / / path Write plate,err:=handler.GetPlate (pictureUrl,accessToken) if erratic license nil {log.Fatal ("failed to get license plate", err)} log.Println ("license plate obtained:" Plate)} type accessTokenInfo struct {AccessToken string `json: "access_token" `ExpiresIn int64 `json: "expires_in" `} type WordResult struct {Number string `json: "number"`} type Data struct {WordsResult WordResult `json: "words_result" `} type PlateHandler struct {} func (handler * PlateHandler) GetAccessToken (appKey string, appSecret string) (accessToken string) {url: = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id="+appKey+"&client_secret="+appSecret response Err: = http.Get (url) if err! = nil {log.Fatal (err) return ""} data, err: = ioutil.ReadAll (response.Body) if err! = nil {log.Fatal (err) return ""} info: = accessTokenInfo {} json.Unmarshal (data, & info) log.Print ("data returned by request accessToken:", string (data) return info.AccessToken} func (handler * PlateHandler) GetPlate (picture_url string) AccessToken string) (plate string, err error) {ff, _: = os.Open (picture_url) sourcebuffer: = make ([] byte, 50000000000) n, _: = ff.Read (sourcebuffer) / / base64 Compression sourcestring: = base64.StdEncoding.EncodeToString (sourcebuffer [: n]) to_url: = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate?access_token="+accessToken values: = url.Values {} values.Add (" image ") Sourcestring) values.Add ("multi_detect", "false") rsp2, err: = http.PostForm (to_url, values) defer rsp2.Body.Close () if err! = nil {log.Fatal (err) return ", err} data, err: = ioutil.ReadAll (rsp2.Body) if err! = nil {log.Fatal (err) return", err} log.Println ("data returned by request license plate:" String (data)) m: = Data {} err = json.Unmarshal (data, & m) if err! = nil {log.Fatal (err) return ", err} log.Println (m) return m.WordsResult.Number, nil} result 11:48:34 request license plate return data: {" log_id ": 8926648569804002425," words_result ": {" color ":" blue "," number ":" Yu A99999 " "probability": [0.9014493227005005,0.9014158248901367,0.900929868221283, 0.9012478590011597,0.901341438293457, 0.9010871052742004,0.9010393619537354], "vertexes_location": [{"y": 181,181,241,173,439}, {"y": 229," x ": 442}, {" y ": 236} 11:48:34 on 2019-12-25 {{Yu A99999}} license plate obtained at 11:48:34 on 2019-12-25: Yu A99999

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report