package test
import (
"encoding/base64"
"encoding/csv"
"encoding/json"
"fmt"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
ocr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ocr/v20181119"
"io"
"io/ioutil"
"log"
"os"
)
//Resp 响应体
type Resp struct {
Response map[string]string
}
//RespFrontInfo 响应体
type RespFrontInfo struct {
Response FrontMsg
}
//FrontMsg 行驶证识别正面
type FrontMsg struct {
FrontInfo map[string]string
}
//ImageCarIdentify 网约车公司信息收集入口函数
func ImageCarIdentify(path string, fileName string, infoType int64) (string, error) {
// 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
// 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
credential := common.NewCredential(
"secretId",
"secretKey",
)
// 实例化一个client选项,可选的,没有特殊需求可以跳过
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "ocr.tencentcloudapi.com"
// 实例化要请求产品的client对象,clientProfile是可选的
client, _ := ocr.NewClient(credential, "ap-guangzhou", cpf)
//读取图片base64
toBase64Str, err := imageToBase64(path + fileName)
resp := ""
if infoType == 1 {
resp, err = imageType1(*client, toBase64Str)
WriteOnlineCarDriverToCsv(resp)
}
if infoType == 2 {
resp, err = imageType2(*client, toBase64Str)
WriteOnlineCarDrivingToCsv(resp)
}
if infoType == 3 {
resp, err = imageType3(*client, toBase64Str)
WriteLicensePlateToCsv(resp)
}
if err != nil {
return "", err
}
fmt.Println(resp)
return resp, nil
}
//imageType1 网约车驾驶证识别
func imageType1(client ocr.Client, toBase64Str string) (string, error) {
// 实例化一个请求对象,每个接口都会对应一个request对象
request := ocr.NewRideHailingDriverLicenseOCRRequest()
request.ImageBase64 = common.StringPtr(toBase64Str)
// 返回的resp是一个ImageEnhancementResponse的实例,与请求对象对应
response, err := client.RideHailingDriverLicenseOCR(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return "", err
}
if err != nil {
return "", err
}
return response.ToJsonString(), nil
}
//imageType2 行驶证识别
func imageType2(client ocr.Client, toBase64Str string) (string, error) {
// 实例化一个请求对象,每个接口都会对应一个request对象
request := ocr.NewVehicleLicenseOCRRequest()
request.ImageBase64 = common.StringPtr(toBase64Str)
request.CardSide = common.StringPtr("FRONT")
// 返回的resp是一个ImageEnhancementResponse的实例,与请求对象对应
response, err := client.VehicleLicenseOCR(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return "", err
}
if err != nil {
return "", err
}
return response.ToJsonString(), nil
}
//imageType3 车牌识别
func imageType3(client ocr.Client, toBase64Str string) (string, error) {
// 实例化一个请求对象,每个接口都会对应一个request对象
request := ocr.NewLicensePlateOCRRequest()
request.ImageBase64 = common.StringPtr(toBase64Str)
// 返回的resp是一个ImageEnhancementResponse的实例,与请求对象对应
response, err := client.LicensePlateOCR(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return "", err
}
if err != nil {
return "", err
}
return response.ToJsonString(), nil
}
//WriteOnlineCarDriverToCsv 网约车驾驶证写入csv
func WriteOnlineCarDriverToCsv(str string) {
jsondata := Resp{}
err := json.Unmarshal([]byte(str), &jsondata)
if err != nil {
fmt.Println(err)
}
nfs, err := os.OpenFile("./data.csv", os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
log.Fatalf("can not create file, err is %+v", err)
}
defer nfs.Close()
nfs.Seek(0, io.SeekEnd)
writer := csv.NewWriter(nfs)
writer.Write([]string{"姓名", "从业资格证号", "发证日期"})
var record []string
record = append(record, jsondata.Response["Name"])
record = append(record, jsondata.Response["LicenseNumber"])
record = append(record, jsondata.Response["ReleaseDate"])
writer.Write(record)
writer.Flush() //将内存中的数据刷回到硬盘保存
}
//WriteLicensePlateToCsv 车牌写入csv
func WriteLicensePlateToCsv(str string) {
jsondata := Resp{}
err := json.Unmarshal([]byte(str), &jsondata)
if err != nil {
fmt.Println(err)
}
nfs, err := os.OpenFile("./data.csv", os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
log.Fatalf("can not create file, err is %+v", err)
}
defer nfs.Close()
nfs.Seek(0, io.SeekEnd)
writer := csv.NewWriter(nfs)
writer.Write([]string{"车牌号", "车牌颜色"})
var record []string
record = append(record, jsondata.Response["Number"])
record = append(record, jsondata.Response["Color"])
writer.Write(record)
writer.Flush() //将内存中的数据刷回到硬盘保存
}
//WriteOnlineCarDrivingToCsv 网约车行驶证写入csv
func WriteOnlineCarDrivingToCsv(str string) {
jsondata := RespFrontInfo{}
err := json.Unmarshal([]byte(str), &jsondata)
if err != nil {
fmt.Println(err)
}
nfs, err := os.OpenFile("./data.csv", os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
log.Fatalf("can not create file, err is %+v", err)
}
defer nfs.Close()
nfs.Seek(0, io.SeekEnd)
writer := csv.NewWriter(nfs)
writer.Write([]string{"号牌号码", "车辆类型", "所有人", "地址", "使用性质", "品牌型号", "车辆识别代号", "发动机编号", "注册日期", "发证单位"})
var record []string
record = append(record, jsondata.Response.FrontInfo["PlateNo"])
record = append(record, jsondata.Response.FrontInfo["VehicleType"])
record = append(record, jsondata.Response.FrontInfo["Owner"])
record = append(record, jsondata.Response.FrontInfo["Address"])
record = append(record, jsondata.Response.FrontInfo["UseCharacter"])
record = append(record, jsondata.Response.FrontInfo["Model"])
record = append(record, jsondata.Response.FrontInfo["Vin"])
record = append(record, jsondata.Response.FrontInfo["EngineNo"])
record = append(record, jsondata.Response.FrontInfo["RegisterDate"])
record = append(record, jsondata.Response.FrontInfo["Seal"])
writer.Write(record)
writer.Flush() //将内存中的数据刷回到硬盘保存
}
//imageToBase64 img转base64
func imageToBase64(filePath string) (string, error) {
srcByte, err := ioutil.ReadFile(filePath)
if err != nil {
return "", err
}
res := base64.StdEncoding.EncodeToString(srcByte)
return res, nil
}
评论