예제 #1
0
 void Update()
 {
     //This is to take the picture, save it and stop capturing the camera image.
     if (_webCamTexture.isPlaying && Input.GetKeyDown(KeyCode.Space))
     {
         _webCamTexture.Pause();
         Texture2D snap = new Texture2D(_webCamTexture.width, _webCamTexture.height);
         snap.SetPixels(_webCamTexture.GetPixels());
         snap.Apply();
         var bytes = snap.EncodeToPNG();
         _faceApiHelper.Detect(bytes, Callback);
     }
 }
예제 #2
0
        public async Task <IActionResult> Detect([Bind("PhotoFile")] IFormFile photoFile)
        {
            if (photoFile != null && photoFile.Length > 0)
            {
                using (var photoStream = new MemoryStream())
                {
                    await photoFile.CopyToAsync(photoStream);

                    byte[]          photoData = photoStream.ToArray();
                    List <FaceInfo> faces     = await _faceApiHelper.Detect(photoData);

                    return(Json(faces));
                }
            }
            else
            {
                return(BadRequest("No image file found."));
            }
        }
예제 #3
0
        public async Task <IActionResult> DetectFaces([Bind("PhotoFile")] IFormFile photoFile)
        {
            using (var photoStream = new MemoryStream())
            {
                await photoFile.CopyToAsync(photoStream);

                byte[] photoData = photoStream.ToArray();
                var    faces     = await _faceApiHelper.Detect(photoData);

                var result = new DetectFaceResult();
                if (faces.Count == 0)
                {
                    result.Data = "No face is detected, please choose a face photo.";
                }
                else if (faces.Count > 1)
                {
                    result.Data = $"{faces.Count} faces are detected, please choose a photo with one face.";
                }
                else
                {
                    // mark the face in the image
                    result.HasDetected = true;

                    var FaceInfo = faces.First <FaceInfo>();
                    int left     = FaceInfo.faceRectangle.left;
                    int top      = FaceInfo.faceRectangle.top;
                    int width    = FaceInfo.faceRectangle.width;
                    int height   = FaceInfo.faceRectangle.height;

                    string base64 = ImageHelper.DrawRectangle(photoData, new PointF[] {
                        new PointF(left, top),
                        new PointF(left, top + height),
                        new PointF(left + width, top + height),
                        new PointF(left + width, top)
                    });

                    result.Data = $"data:image/jpeg;base64,{base64}";
                }

                return(Json(result));
            }
        }