public void OnPictureTaken(byte[] data) { Task.Run(async() => { try { isProcessing = true; Console.WriteLine("face detected: "); var imageAnalyzer = new ImageAnalyzer(data); await LiveCamHelper.ProcessCameraCapture(imageAnalyzer); } finally { isProcessing = false; } }); }
public void OnPictureTaken(byte[] data) { Task.Run(async() => { try { isProcessing = true; Console.WriteLine("face detected: "); var bitmap = Bitmap.CreateScaledBitmap(BitmapFactory.DecodeByteArray(data, 0, data.Length), 240, 320, false); if (_face != null) { //240width //320height //bitmap.Height = 320; //bitmap.Width = 240; Console.WriteLine($"position x: {_face.Position.X} position y: {_face. Position.Y} width: {_face.Width} height: {_face.Height} bitmapWidth: {bitmap.Width} bitmapHeight: {bitmap.Height}"); //var bitmapScalled = Bitmap.CreateScaledBitmap(bitmap, 128, 128, true); //bitmap = Bitmap.CreateBitmap(bitmap, (int)_face.Position.X, (int)_face.Position.Y, (int)_face.Width, (int)_face.Height); #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed Task.Factory.StartNew(() => { System.Threading.Thread.Sleep(200); bitmap = Bitmap.CreateBitmap(bitmap, (int)_face.Position.X / 2, (int)_face.Position.Y / 2, (int)_face.Width / 2, (int)_face.Height / 2); bitmap = Bitmap.CreateScaledBitmap(bitmap, 240, 320, false); _img.SetImageBitmap(bitmap); PhotoProxy.LastPhoto = bitmap; //Task<string> task = PostRecognition(bitmap); //task.Wait(); //var x = task.Result; //Console.WriteLine(x+" --- Post recognition response"); Task.Run(async() => { //convert to base64 var client = new HttpClient(); //Iskelt kad ne ant kiekvieno siuntimo kurtu client.BaseAddress = new Uri("http://88.119.27.98:55555"); //var stream = new MemoryStream(); byte[] byteArray = LiveCamHelper.BitmapToGrayscaleBytes(bitmap); //stream.Write(byteArray, 0, byteArray.Length); //bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, stream); var content = new ByteArrayContent(byteArray); //content.Add(new StreamContent(stream)); var response = await client.PostAsync("api/recognize", content); Console.WriteLine("Response from /api/recognize is " + response.StatusCode); Console.WriteLine(await response.Content.ReadAsStringAsync()); //if (stream.Equals(null)) Console.WriteLine("The stream is null"); //else Console.WriteLine("the stream is not null"); //stream.Dispose(); responseObject = JObject.Parse(await response.Content.ReadAsStringAsync()); if (!string.IsNullOrWhiteSpace((string)responseObject.GetValue("Name"))) { newestResponse = responseObject.GetValue("Name") + ", " + responseObject.GetValue("Bio") + ", " + responseObject.GetValue("Likes"); } }); }); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed } var imageAnalyzer = new ImageAnalyzer(data); await LiveCamHelper.ProcessCameraCapture(imageAnalyzer); } catch (Exception ex) { Console.WriteLine("======================================"); Console.WriteLine(ex); throw; } finally { isProcessing = false; } }); }
async void ProcessingImage(NSData uIImage) { lock (processingobjlocker) { if (isAPIprocessing) { return; } isAPIprocessing = true; } cancellationTokenSource?.Cancel(); cancellationTokenSource = new CancellationTokenSource(); cancellationTokenSource.Token.ThrowIfCancellationRequested(); //UIImage uIImage = null; //DispatchQueue.MainQueue.DispatchSync(() => { // uIImage = ivPictureTaken.Image; //}); try { if (task != null && (task.Status == TaskStatus.Running || task.Status == TaskStatus.WaitingToRun || task.Status == TaskStatus.WaitingForActivation)) { Console.WriteLine("Task has attempted to start while already running"); } else { Console.WriteLine("running api face recognition: "); task = await Task.Factory.StartNew(async() => { //await Task.Delay(10000); //await Task.Delay(10000); //await Task.Delay(10000); //UIImage uIImage = UIImage.FromFile(ruta); //using (uIImage) { ImageAnalyzer imageAnalyzer = new ImageAnalyzer(() => Task.FromResult <Stream>(uIImage.AsStream()), null); await LiveCamHelper.ProcessCameraCapture(imageAnalyzer).ConfigureAwait(false); //} return(true); }, cancellationTokenSource.Token, TaskCreationOptions.None, TaskScheduler.Default).ConfigureAwait(false);; await task; } } catch (Exception e) { Console.WriteLine("error api face recogniion "); } finally { //processingFaceDetection = false; //lock (lockerobj) { await Task.Delay(2000); processingFaceDetection = false; isAPIprocessing = false; setupAVFoundationFaceDetection(); } Console.WriteLine("finished processing "); }