public async Task <ActionResult> AnalyzeHome(string faceUrl = "", string photoName = "") { string requestId = Guid.NewGuid().ToString(); int? contentLength = null; VisionHelper vision = new VisionHelper(GetVisionAPIkey(), fontPath, DateTime.Now, fontPath); RichResult res = null; try { string postString = string.Empty; //using (Stream stream = Request.InputStream) //{ // byte[] postBytes = new byte[stream.Length]; // stream.Read(postBytes, 0, (int)stream.Length); // postString = Encoding.Unicode.GetString(postBytes); // return Json(JsonConvert.SerializeObject(postString), "application/json"); //} Stopwatch stopwatch = Stopwatch.StartNew(); //Trace.WriteLine(string.Format("Start Analyze Request: RequestId: {0};", requestId)); if (Request.ContentType == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "BadRequest")); } if (string.Equals(Request.ContentType, "application/octet-stream")) { contentLength = Request.ContentLength; Request.InputStream.Seek(0, System.IO.SeekOrigin.Begin); //var img = System.Drawing.Image.FromStream(Request.InputStream); //return Json(JsonConvert.SerializeObject(img.Width), "application/json"); res = await vision.AnalyzeImage(Request.InputStream); } else if (!string.IsNullOrEmpty(faceUrl) && faceUrl != "undefined") { res = await vision.AnalyzeImage(faceUrl); } //Trace.WriteLine(string.Format("Completed Analyze Request: RequestId: {0};", requestId)); return(Json(JsonConvert.SerializeObject(res), "application/json")); } catch (Exception e) { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, e.Message)); } }
private async Task <RichResult> QuickReturn(VisionHelper vision, MsgObject msg) { var ret = await vision.AnalyzeImage(msg.PicUrl, msg.FromUserName); if (ret != null) { //Response.Write(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>12345678</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[{2}]]></Content><DebugInfo><![CDATA[{3}]]></DebugInfo><ErrorInfo><![CDATA[{4}]]></ErrorInfo></xml>", // msg.FromUserName, msg.ToUserName, ret.analyzeImageResult, ret.timeLogs, ret.errorLogs)); if (ret.errorLogs.Equals("")) { // Production mode var returnString = string.Format("{0} 想知道贵图有多火辣么? 请看归图:{1}", ret.analyzeImageResult, ret.uploadedUrl); Response.Write(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>12345678</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[{2}]]></Content></xml>", msg.FromUserName, msg.ToUserName, returnString)); Response.End(); return(ret); } else { //Response.Write(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>12345678</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[{2}]]></Content></xml>", //msg.FromUserName, msg.ToUserName, "机器人很忙,请稍后再试")); //Response.End(); return(ret); } } return(null); }
static void Main(string[] args) { string subscriptionKey = ConfigurationManager.AppSettings["subscriptionKey"]; if (string.IsNullOrWhiteSpace(subscriptionKey)) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("To play this sample, you should firstly get a subscription key and put it into the App.Config file."); Console.WriteLine("If you don't have one, please access"); Console.WriteLine("http://www.projectoxford.ai/doc/general/subscription-key-mgmt"); Console.ResetColor(); Console.WriteLine(); Console.WriteLine("Please enter any key......"); Console.ReadLine(); return; } var vision = new VisionHelper(subscriptionKey); // string imagePathorUrl = "http://a2.assets.nationalgeographic.es/soc_photo/10970.600x450.jpg"; // string imagePathorUrl = "http://www.colins-it.co.uk/blog/wp-content/uploads/2012/08/keep-calm.jpg"; var imagePathorUrl = GetValidImagePathorUrl(); vision.AnalyzeImage(imagePathorUrl); Console.ReadLine(); }
public async Task <ActionResult> AnalyzeOneImage(string faceUrl = "", string photoName = "") { string requestId = Guid.NewGuid().ToString(); //int? contentLength = null; VisionHelper vision = new VisionHelper(GetVisionAPIkey(), fontPath, DateTime.Now, fontPath); RichResult res = null; try { string postString = string.Empty; //using (Stream stream = Request.InputStream) //{ // byte[] postBytes = new byte[stream.Length]; // stream.Read(postBytes, 0, (int)stream.Length); // postString = Encoding.Unicode.GetString(postBytes); // return Json(JsonConvert.SerializeObject(postString), "application/json"); //} Stopwatch stopwatch = Stopwatch.StartNew(); //Trace.WriteLine(string.Format("Start Analyze Request: RequestId: {0};", requestId)); //if (Request.Content.GetType() == null) //{ // return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "BadRequest"); //} if (!string.IsNullOrEmpty(faceUrl) && faceUrl != "undefined") { res = await vision.AnalyzeImage(faceUrl); } else { //if (string.Equals(Request.Headers.GetType(), "application/octet-stream")) //{ // contentLength = Request.Content.; //contentLength = Request.ContentLength; //var stream = new MemoryStream(); //await Request.Content.CopyToAsync(stream); //stream.Seek(0, System.IO.SeekOrigin.Begin); res = await vision.AnalyzeImage(Request.InputStream); } if (res.analysisResult.RichFaces.Length > 0) { res.stepSize = 5; res.minAge = res.stepSize * (int)((int)res.analysisResult.RichFaces[0].Attributes.Age / (float)res.stepSize); res.maxAge = 90; var urls = new List <string>(); for (var age = res.minAge; age <= res.maxAge; age += res.stepSize) { var url = GetImageUrlByAge(age, res.analysisResult.RichFaces[0]); urls.Add(!string.IsNullOrEmpty(url) ? url : ""); } res.agingImgUrls = urls.ToArray(); } HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); return(Json(JsonConvert.SerializeObject(res), "application/json")); } catch (Exception e) { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, e.Message)); } }
public async Task <HttpResponseMessage> Analyze(string faceUrl = "", string photoName = "") { string requestId = Guid.NewGuid().ToString(); //int? contentLength = null; VisionHelper vision = new VisionHelper(GetVisionAPIkey(), fontPath, DateTime.Now, fontPath); RichResult res = null; try { string postString = string.Empty; //using (Stream stream = Request.InputStream) //{ // byte[] postBytes = new byte[stream.Length]; // stream.Read(postBytes, 0, (int)stream.Length); // postString = Encoding.Unicode.GetString(postBytes); // return Json(JsonConvert.SerializeObject(postString), "application/json"); //} Stopwatch stopwatch = Stopwatch.StartNew(); //Trace.WriteLine(string.Format("Start Analyze Request: RequestId: {0};", requestId)); //if (Request.Content.GetType() == null) //{ // return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "BadRequest"); //} if (!string.IsNullOrEmpty(faceUrl) && faceUrl != "undefined") { res = await vision.AnalyzeImage(faceUrl); } else { //if (string.Equals(Request.Headers.GetType(), "application/octet-stream")) //{ // contentLength = Request.Content.; //contentLength = Request.ContentLength; var stream = new MemoryStream(); await Request.Content.CopyToAsync(stream); stream.Seek(0, System.IO.SeekOrigin.Begin); //var img = System.Drawing.Image.FromStream(Request.InputStream); //return Json(JsonConvert.SerializeObject(img.Width), "application/json"); res = await vision.AnalyzeImage(stream); } //Trace.WriteLine(string.Format("Completed Analyze Request: RequestId: {0};", requestId)); //return Json(JsonConvert.SerializeObject(res),(Newtonsoft.Json.JsonSerializerSettings) "application/json", System.Text.Encoding.UTF8); HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); //return Json(JsonConvert.SerializeObject(Json(JsonConvert.SerializeObject(res)).Content)); result.Content = new StringContent(JsonConvert.SerializeObject(Json(JsonConvert.SerializeObject(res)).Content));//new StringContent(JsonConvert.SerializeObject(res)); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return(result); //Error 20 Argument 2: cannot convert from 'string' to 'Newtonsoft.Json.JsonSerializerSettings' } catch (Exception e) { return(null);//new HttpStatusCodeResult(HttpStatusCode.InternalServerError, e.Message); } }