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

你可能感兴趣的:(NET,c#,开发语言,图像处理,目标检测)