private async Task ProcessCurrentVideoFrame() { try { if (mediaElement.MediaPlayer == null) { return; } mediaElement.MediaPlayer.takeSnapshot(0, path, InfoSettingFix.FixTakeWidth, InfoSettingFix.FixTakeHeight); data = File.ReadAllBytes(path); int byteLength = data.Length; //Trường hợp data trống và byte 2 Frame liên tiếp bằng nhau if (data == null || byteLength == 0 || byteLength == this.ByteLength) { return; } this.ByteLength = byteLength; CurrentFrame = new CurrentFrameModel() { CaptureTime = DateTime.Now, DataCurrent = data }; RealtimeFixModel.ListFrame4.Enqueue(CurrentFrame); data = null; } catch (Exception ex) { return; } CoreUtil.FreeMemory(); }
public async Task <ImageAnalyzer> CaptureFrameAsync(CurrentFrameModel currentFrame) { try { using (Stream stream = currentFrame.DataCurrent.AsBuffer().AsStream()) { stream.Position = 0; var decoder = await BitmapDecoder.CreateAsync(stream.AsRandomAccessStream()); var softwareBitmap = await decoder.GetSoftwareBitmapAsync(); var detector = await FaceDetector.CreateAsync(); using (SoftwareBitmap convertedBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Gray8)) { faces = await detector.DetectFacesAsync(convertedBitmap, SearchArea); convertedBitmap.Dispose(); } this.NumFacesOnLastFrame = faces.Count(); var previewFrameSize = new Windows.Foundation.Size(softwareBitmap.PixelWidth, softwareBitmap.PixelHeight); this.ShowFaceTrackingVisualization(previewFrameSize, faces); softwareBitmap.Dispose(); stream.Dispose(); } //Không có face thì không phân tích if (this.NumFacesOnLastFrame == 0) { faces = null; CoreUtil.FreeMemory(); return(null); } //Hai khung hình có số lượng khung mật giống nhau quá nửa thì không phân tích nữa if (this.AreFacesStill(this.detectedFacesFromPreviousFrame, faces)) { faces = null; CoreUtil.FreeMemory(); return(null); } this.detectedFacesFromPreviousFrame = faces; imageWithFace = new ImageAnalyzer(currentFrame.DataCurrent); imageWithFace.CameraIPAdres = CameraIPAdres; imageWithFace.imageWidth = InfoSettingFix.FixImageWidth; imageWithFace.imageHeight = InfoSettingFix.FixImageHeight; imageWithFace.CaptureTime = currentFrame.CaptureTime; imageWithFace.ListDetectedFaceJson = JsonConvert.SerializeObject(faces.Select(r => r.FaceBox).ToList()); faces = null; CoreUtil.FreeMemory(); return(imageWithFace); } catch (Exception ex) { CoreUtil.FreeMemory(); return(null); } }