阿里云新版人脸识别Go语言调用示例

简介: 本文主要演示基于阿里云现有通用Http Go SDK调用阿里云新版人脸识别服务,以DetectFace方法为示例演示。

概述

本文主要演示基于阿里云现有通用Http Go SDK调用阿里云新版人脸识别服务,分别DetectFace方法使用图片URL、VerifyFace方法使用本地为示例演示。

环境准备

1、Go语言版本

go version go1.12.7 windows/amd64

2、IDE

LiteIDE

3、SDK安装

go get -u github.com/aliyun/alibaba-cloud-sdk-go/sdk

测试代码

1、DetectFace Sample

// demo1 project main.go
package main

import (
    "fmt"

    "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
)

func main() {

    client, err := sdk.NewClientWithAccessKey("cn-shanghai", "********", "********") // ak,sk信息
    if err != nil {
        // Handle exceptions
        panic(err)
    }

    request := requests.NewCommonRequest()                                                                                                                                                                                                                     
    request.Method = "POST"                                                                                                                                                                                                                                      
    request.Product = "FaceAPI"                                                                                                                                                                                                                                 
    request.Domain = "face.cn-shanghai.aliyuncs.com"                                                                                                                                                                                                         
    request.Version = "2018-12-03"                                                                                                                                                                                                                            
    request.Scheme = "https"                                                                                                                                                                                                                                 
    request.ApiName = "DetectFace"                                                                                                                                                                                                                          
    request.QueryParams["ImageUrl"] = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1559655604341&di=3d6995f6dee1c4795d1827e754a00452&imgtype=0&src=http%3A%2F%2Fimg0.ph.126.net%2F90u9atgu46nnziAm1NMAGw%3D%3D%2F6631853916514183512.jpg"                                                                                                                                                                                               // Specify the requested regionId, if not specified, use the client regionId, then default regionId
    request.AcceptFormat = "json"
    request.TransToAcsRequest()
    client.ProcessCommonRequest(request)

    response, err := client.ProcessCommonRequest(request)
    if err != nil {
        fmt.Print(err.Error())
    }
    fmt.Printf(response.GetHttpContentString())
}
运行结果
{"Data":{"landmark":[799.2508544921875,1096.208740234375,1240.654296875,1094.9720458984375,1017.4330444335938,998.57861328125,1019.7756958007812,1073.234375,856.2061767578125,1035.13671875,933.8515625,1003.8469848632812,1100.0830078125,1011.6668701171875,1179.436767578125,1038.3056640625,872.8338623046875,1087.581298828125,945.71942138671875,1074.416015625,1093.3974609375,1081.538818359375,1166.590576171875,1092.97607421875,1493.1463623046875,1106.8787841796875,1803.4925537109375,1122.54248046875,1661.3282470703125,1008.6871948242188,1651.0706787109375,1079.126953125,1537.4063720703125,1056.8326416015625,1597.4547119140625,1028.1773681640625,1726.907470703125,1018.4874267578125,1778.471435546875,1060.5947265625,1546.176025390625,1099.3516845703125,1597.8970947265625,1085.4248046875,1704.5565185546875,1081.686279296875,1754.29541015625,1101.360595703125,950.329833984375,1237.64794921875,1183.385009765625,1267.03759765625,977.181396484375,1217.33203125,1007.8623046875,1203.468505859375,1040.79296875,1196.4024658203125,1074.479736328125,1197.28076171875,1107.569091796875,1203.653076171875,1138.1513671875,1217.7967529296875,1163.763427734375,1239.65087890625,972.8101806640625,1259.625244140625,999.7427978515625,1275.834716796875,1029.82177734375,1285.0888671875,1060.949951171875,1289.74853515625,1092.414306640625,1290.66357421875,1123.592041015625,1286.35107421875,1154.080322265625,1278.5302734375,1505.3389892578125,1268.0709228515625,1708.0179443359375,1249.576416015625,1518.981201171875,1239.8076171875,1538.6033935546875,1215.315185546875,1567.039306640625,1202.20703125,1598.1136474609375,1198.019775390625,1629.2843017578125,1201.669677734375,1659.324462890625,1210.7451171875,1686.067138671875,1227.1563720703125,1530.2646484375,1279.018310546875,1556.6688232421875,1285.6533203125,1583.6409912109375,1289.33984375,1610.8587646484375,1289.97509765625,1638.017578125,1288.05908203125,1664.3843994140625,1281.280517578125,1688.3682861328125,1268.41259765625,1356.669921875,1282.080322265625,1408.8363037109375,1560.64453125,1382.7197265625,1421.0,1381.5010986328125,1632.008056640625,1219.308349609375,1626.333251953125,1504.8509521484375,1610.831787109375,1130.4825439453125,1834.1395263671875,1509.7669677734375,1838.226318359375,1168.39501953125,1840.3800048828125,1477.0972900390625,1842.334228515625,1356.515380859375,1776.99951171875,1318.5791015625,1763.8675537109375,1391.8681640625,1763.096435546875,1223.385498046875,1796.4013671875,1458.71044921875,1788.520751953125,1175.30078125,1811.2015380859375,1270.33349609375,1778.24560546875,1426.817626953125,1771.79443359375,1487.899169921875,1809.6094970703125,1339.509521484375,1975.9508056640625,1215.365234375,1935.4556884765625,1444.841064453125,1933.088623046875,1168.38525390625,1888.6160888671875,1273.85400390625,1966.6778564453125,1396.037353515625,1963.970703125,1483.1046142578125,1889.708984375,1350.7132568359375,1842.592041015625,1342.6649169921875,1877.176025390625,1259.4251708984375,1840.41455078125,1253.5074462890625,1868.7591552734375,1413.9844970703125,1838.3511962890625,1413.215087890625,1872.574462890625,1213.9150390625,1836.5245361328125,1210.5072021484375,1855.8992919921875,1305.033935546875,1843.0133056640625,1297.79638671875,1876.015380859375,1382.399658203125,1841.2255859375,1378.171142578125,1878.257080078125,1445.6971435546875,1837.94091796875,1446.6396484375,1860.6153564453125,622.818603515625,1224.1640625,1785.5914306640625,1214.9752197265625,1357.4207763671875,2194.99365234375,768.70306396484375,1845.98193359375,1715.2325439453125,1736.135498046875,668.5491943359375,1537.0185546875,1775.165771484375,1473.250732421875,1036.9564208984375,2098.42333984375,1579.2042236328125,2001.826416015625],"pose":[10.426637649536133,-4.4992427825927734,-0.58456879854202271],"face_prob":[1.0],"request_id":"FC45F3BE-AE4E-4B4F-9E95-9E585C50AE71","errno":0,"face_num":1,"iris":[1071.598876953125,1235.034912109375,51.706081390380859,1573.8760986328125,1241.315673828125,51.706081390380859],"landmark_num":105,"face_rect":[679,539,1172,1664]},"RequestId":"FC45F3BE-AE4E-4B4F-9E95-9E585C50AE71","Success":true}成功: 进程退出代码 0.

2、VerifyFace Code Sample

// demo1 project main.go
package main

import (
    "bufio"
    "encoding/base64"
    "fmt"
    "log"
    "os"

    "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
)

func main() {

    client, err := sdk.NewClientWithAccessKey("cn-shanghai", "******", "********")
    if err != nil {
        // Handle exceptions
        panic(err)
    }

    //图片本地路径  测试仅用一张图片测试,如果实际人脸对比,请使用两张图片
    path := "timg.jpg"
    content1, _ := base64demo(path)

    var countryCapitalMap map[string]string /*创建集合 */
    countryCapitalMap = make(map[string]string)
    countryCapitalMap["Content1"] = content1
    countryCapitalMap["Content2"] = content1

    request := requests.NewCommonRequest()
    request.FormParams = countryCapitalMap
    request.Method = "POST"
    request.Product = "FaceAPI"
    request.Domain = "face.cn-shanghai.aliyuncs.com"
    request.Version = "2018-12-03"
    request.Scheme = "https"
    request.ApiName = "VerifyFace"
    request.AcceptFormat = "json"
    request.TransToAcsRequest()
    client.ProcessCommonRequest(request)

    response, err := client.ProcessCommonRequest(request)
    if err != nil {
        fmt.Print(err.Error())
    }
    fmt.Printf(response.GetHttpContentString())
}

// 读取本地图片转base64 string
func base64demo(path string) (string, error) {
    imgFile, err := os.Open(path)
    if err != nil {
        log.Fatalln(err)
    }

    defer imgFile.Close()

    //create a new buffer base on file size
    fInfo, _ := imgFile.Stat()
    var size int64 = fInfo.Size()
    buf := make([]byte, size)

    //read file content into buffer
    fReader := bufio.NewReader(imgFile)
    fReader.Read(buf)

    //convert the buffer bytes to base64 string - use buf.Bytes() for new image
    var imgBase64str = base64.StdEncoding.EncodeToString(buf)
    return imgBase64str, nil
}
运行结果
{"Data":{"rectA":[679,539,1172,1664],"err_msg":"","thresholds":[60.03,67.05,75.45],"rectB":[679,539,1172,1664],"request_id":"2786FA3F-72CD-4993-8A0D-49BB019330A1","errno":0,"confidence":0.9999999},"RequestId":"2786FA3F-72CD-4993-8A0D-49BB019330A1","Success":true}

参考链接

Alibaba Cloud SDK for Go
Azure Go Management SDK 中国版使用示例
阿里云人脸识别公测使用说明

相关文章
|
5天前
|
存储 JSON 监控
Viper,一个Go语言配置管理神器!
Viper 是一个功能强大的 Go 语言配置管理库,支持从多种来源读取配置,包括文件、环境变量、远程配置中心等。本文详细介绍了 Viper 的核心特性和使用方法,包括从本地 YAML 文件和 Consul 远程配置中心读取配置的示例。Viper 的多来源配置、动态配置和轻松集成特性使其成为管理复杂应用配置的理想选择。
23 2
|
4天前
|
Go 索引
go语言中的循环语句
【11月更文挑战第4天】
13 2
|
4天前
|
Go C++
go语言中的条件语句
【11月更文挑战第4天】
15 2
|
4天前
|
Go
go语言中的 跳转语句
【11月更文挑战第4天】
12 4
|
4天前
|
JSON 安全 Go
Go语言中使用JWT鉴权、Token刷新完整示例,拿去直接用!
本文介绍了如何在 Go 语言中使用 Gin 框架实现 JWT 用户认证和安全保护。JWT(JSON Web Token)是一种轻量、高效的认证与授权解决方案,特别适合微服务架构。文章详细讲解了 JWT 的基本概念、结构以及如何在 Gin 中生成、解析和刷新 JWT。通过示例代码,展示了如何在实际项目中应用 JWT,确保用户身份验证和数据安全。完整代码可在 GitHub 仓库中查看。
17 1
|
5天前
|
Go 调度 开发者
探索Go语言中的并发模式:goroutine与channel
在本文中,我们将深入探讨Go语言中的核心并发特性——goroutine和channel。不同于传统的并发模型,Go语言的并发机制以其简洁性和高效性著称。本文将通过实际代码示例,展示如何利用goroutine实现轻量级的并发执行,以及如何通过channel安全地在goroutine之间传递数据。摘要部分将概述这些概念,并提示读者本文将提供哪些具体的技术洞见。
|
6月前
|
弹性计算 Java PHP
新手用户注册阿里云账号、实名认证、购买云服务器图文教程参考
对于初次购买阿里云产品的用户来说,第一步要做的是注册账号并完成实名认证,然后才是购买阿里云服务器或者其他云产品,本文为大家以图文形式展示一下新手用户从注册阿里云账号、实名认证到购买云服务器完整详细教程,以供参考。
新手用户注册阿里云账号、实名认证、购买云服务器图文教程参考
|
5月前
|
文字识别 算法 API
视觉智能开放平台产品使用合集之uniapp框架如何使用阿里云金融级人脸识别
视觉智能开放平台是指提供一系列基于视觉识别技术的API和服务的平台,这些服务通常包括图像识别、人脸识别、物体检测、文字识别、场景理解等。企业或开发者可以通过调用这些API,快速将视觉智能功能集成到自己的应用或服务中,而无需从零开始研发相关算法和技术。以下是一些常见的视觉智能开放平台产品及其应用场景的概览。
137 0
|
机器学习/深度学习 搜索推荐 计算机视觉
【阿里云OpenVI-人脸感知理解系列之人脸识别】基于Transformer的人脸识别新框架TransFace ICCV-2023论文深入解读
本文介绍 阿里云开放视觉智能团队 被计算机视觉顶级国际会议ICCV 2023接收的论文 "TransFace: Calibrating Transformer Training for Face Recognition from a Data-Centric Perspective"。TransFace旨在探索ViT在人脸识别任务上表现不佳的原因,并从data-centric的角度去提升ViT在人脸识别任务上的性能。
2163 341