C#NET7 调用科大讯飞WebApi实现人脸检测睁眼闭眼

 基本参数

        public static String requestUrl = "https://api.xf-yun.com/v1/private/s67c9c78c";
        public static String appid = "xxxxxx"; //请填写控制台获取的APPID,
        public static String apiSecret = "xxxxxx";  //请填写控制台获取的APISecret;
        public static String apiKey = "xxxxxxxx";  //请填写控制台获取的APIKey
        public static String serviceId = "xxxxxx";

测试调用

static void Main(string[] args)
        {
            string imagePath = "D:\\person_front_low.jpg";
            string url = AssembleRequestUrl(Property.requestUrl, Property.apiKey, Property.apiSecret);
            string imageBase641 = ImageToBase64(imagePath);
            string ret = handleFaceContrastRes(url, GetInputParam(imageBase641));
        }

构建URL

//构建url
        private static string AssembleRequestUrl(string requestUrl, string apiKey, string apiSecret)
        {
            Uri url = new Uri(requestUrl);
            try
            {
                //获取当前日期并格式化
                string date = DateTime.Now.AddHours(-8).ToString("R");
                string host = url.Host;
                if (url.Port != 80 && url.Port != 443)
                {
                    host = $"{host}:{url.Port}";
                }
                StringBuilder builder = new StringBuilder("host: ").Append(host).Append("\n").//
                Append("date: ").Append(date).Append("\n").//
                        Append("POST ").Append(url.LocalPath).Append(" HTTP/1.1");
                Console.WriteLine(builder.ToString());
                string sha = HmacSHA256(builder.ToString(), apiSecret);//
                string authorization = string.Format("api_key=\"{0}\", algorithm=\"{1}\", headers=\"{2}\", signature=\"{3}\"", apiKey, "hmac-sha256", "host date request-line", sha);
                string authBase = ToBase64Str(authorization);
                return string.Format("{0}?authorization={1}&host={2}&date={3}", requestUrl, System.Net.WebUtility.UrlEncode(authBase), System.Net.WebUtility.UrlEncode(host), System.Net.WebUtility.UrlEncode(date));
            }
            catch (Exception e)
            {
                throw new Exception("assemble requestUrl error:" + e.Message);
            }
        }

完整Demo下载地址 NET7

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值