日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > pytorch >内容正文

pytorch

阿里云人脸属性API,我已经成功了(高兴高兴)

發布時間:2025/3/18 pytorch 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 阿里云人脸属性API,我已经成功了(高兴高兴) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

想學習一下阿里的人臉識別API,Java版的參照https://help.aliyun.com/document_detail/67818.html?spm=a2c4g.11186623.6.559.a31e4bd9BDsnO9 已經沒有問題,成功了。當我把這些代碼轉換為C#時,說死是不成功啊。
Java版中只是把其中的main函數改了一下,如下:

public static void main(String[] args) throws Exception {setProxyofNeusoft();// 發送POST請求示例String ak_id = "my ak_id"; // 用戶akString ak_secret = "my secret"; // 用戶ak_secretString url = "https://dtplus-cn-shanghai.data.aliyuncs.com/face/attribute";String body = "{\"type\": \"0\", \"image_url\":\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1540359146297&di=3218a5eefe6b89b990144478fe078e25&imgtype=0&src=http%3A%2F%2Fhimg2.huanqiu.com%2Fattachment2010%2F2015%2F0311%2F20150311030317329.jpg\"}";// String body = "{\"type\": \"1\", \"content\":\"xxx\"}";System.out.println("response body:" + sendPost(url, body, ak_id, ak_secret));body = "{\"type\": \"1\", \"content\":\"" + Base64.getEncoder().encodeToString(toByteArray("E:\\0531\\face\\WIN_20181023_16_31_35_Pro.jpg")) + "\"}";System.out.println("response body:" + sendPost(url, body, ak_id, ak_secret));}

當我使用C#來實現時,打死不成功啊。
Main函數如下:

static void Main(string[] args){string ak_id = "my ak_id"; // 用戶akstring ak_secret = "my secret"; // 用戶ak_secretstring url = "https://dtplus-cn-shanghai.data.aliyuncs.com/face/attribute";// 通過URL判斷string body = "{\"type\": \"0\", \"image_url\":\"http://g.hiphotos.baidu.com/image/pic/item/730e0cf3d7ca7bcb944f655cb3096b63f624a889.jpg\"}"; Console.WriteLine("response body:" + DoPost(url, body, ak_id, ak_secret));Console.ReadLine();}

其中的DoPost函數如下:

public static string DoPost(string url, string body, string ak_id, string ak_secret){ string result = string.Empty;HttpClient httpClient = new HttpClient();httpClient.MaxResponseContentBufferSize = 256000;httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36");DateTime dateNow = DateTime.Now;string bodyMd5 = MD5Base64(body);string method = "POST";string accept = "application/json";string content_type = "application/json";string date = ToGMTstring(dateNow);//HttpContent httpContent = new StringContent(bodyMd5);//此處一定要用ByteArrayContent,用二進制數組發送數據組API。HttpContent httpContent = new ByteArrayContent(Encoding.UTF8.GetBytes(body));httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");//設置Http的內容標頭的字符//httpContent.Headers.ContentType.CharSet = "utf-8";httpClient.DefaultRequestHeaders.Date = dateNow;httpClient.DefaultRequestHeaders.Accept.Clear();httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(accept));string stringToSign = method + "\n" + accept + "\n" + bodyMd5 + "\n" + content_type + "\n" + date + "\n"+ "/face/attribute";// 2.計算 HMAC-SHA1string signature = HMACSha1(stringToSign, ak_secret);httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Dataplus", ak_id + ":" + signature);HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;Console.WriteLine(response.StatusCode);result = response.Content.ReadAsStringAsync().Result;return result;}

引用的計算MD5和SHA的函數轉換為C#如下:

public static string ToGMTstring(DateTime date){return date.ToUniversalTime().ToString("r");}/// 計算MD5+BASE64public static string MD5Base64(string s){if (s == null)return null;string encodeStr = "";byte[] utfBytes = System.Text.Encoding.UTF8.GetBytes(s);MD5 md5 = new MD5CryptoServiceProvider();try{byte[] md5Bytes = md5.ComputeHash(utfBytes);encodeStr = Convert.ToBase64String(md5Bytes);}catch (Exception e){throw new Exception("Failed to generate MD5 : " + e.Message);}return encodeStr;}/// 計算 HMAC-SHA1public static string HMACSha1(string data, string key){try{HMACSHA1 myHMACSHA1 = new HMACSHA1(Encoding.UTF8.GetBytes(key));byte[] byteText = myHMACSHA1.ComputeHash(Encoding.UTF8.GetBytes(data));return System.Convert.ToBase64String(byteText);}catch (Exception e){throw new Exception("Failed to generate HMAC : " + e.Message);}}

很郁悶的就是調用不成功,取得的響應內容是
response body:The authorization(Dataplus my_ak_id:my_signature) of this request Invalid! {"url":"/face/attribute","headers":{"date":"Wed, 24 Oct 2018 05:50:10 GMT","accept":"application/json","authorization":"Dataplus my_ak_id:my_signature","content-type":"application/json"},"method":"POST"}
{"url":"/face/attribute","headers":{"date":"Wed, 24 Oct 2018 05:50:10 GMT","accept":"application/json","authorization":"Dataplus my_ak_id:my_signature","content-type":"application/json"},"method":"POST"}

其中輸出已做替換my_ak_id:my_signature實際輸出是我的ak_id和計算的簽名。

在大佬在嗎?指導一下。

總結

以上是生活随笔為你收集整理的阿里云人脸属性API,我已经成功了(高兴高兴)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。