Go车牌识别(网络图片|本地图片)-创新互联
我们提供的服务有:网站设计、网站制作、微信公众号开发、网站优化、网站认证、安丘ssl等。为上1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的安丘网站制作公司
网站题目:Go车牌识别(网络图片|本地图片)-创新互联
转载注明:http://lswzjz.com/article/disdpc.html
先注册一个百度云账号,然后选择 人工智能 文字识别,创建一个应用。获取appkey secret 。
网络图片
package main
import (
"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 err!=nil{
log.Fatal("获取车牌失败",err)
}
log.Println("获取到的车牌:",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("请求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("请求车牌返回的数据:",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
}
本地图片
package main
import (
"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" // 路径,从根开始写
plate,err:=handler.GetPlate(pictureUrl,accessToken)
if err!=nil{
log.Fatal("获取车牌失败",err)
}
log.Println("获取到的车牌:",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("请求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压缩
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("请求车牌返回的数据:",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
}
结果
2019/12/25 11:48:34 请求车牌返回的数据: {"log_id": 8926648569804002425, "words_result": {"color": "blue", "number": "豫A99999", "probability": [0.9014493227005005, 0.9014158248901367, 0.900929868221283, 0.9012478590011597, 0.901341438293457, 0.9010871052742004, 0.9010393619537354], "vertexes_location": [{"y": 181, "x": 241}, {"y": 173, "x": 439}, {"y": 229, "x": 442}, {"y": 236, "x": 244}]}}
2019/12/25 11:48:34 {{豫A99999}}
2019/12/25 11:48:34 获取到的车牌: 豫A99999
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网站题目:Go车牌识别(网络图片|本地图片)-创新互联
转载注明:http://lswzjz.com/article/disdpc.html