public void updateData(PXCMFaceData.Face face) { if (face == null) { return; } PXCMFaceData.LandmarksData ldata = face.QueryLandmarks(); if (ldata == null) { return; } PXCMRectI32 rect; face.QueryDetection().QueryBoundingRect(out rect); PXCMFaceData.LandmarkPoint[] points; ldata.QueryPoints(out points); if (points == null || points.Length == 0) { return; } for (int i = 0; i < 78; i++) { landmarksData[i].x = points[i].image.x - rect.x; landmarksData[i].y = points[i].image.y - rect.y; landmarksData[i].z = points[i].world.z; } }
public void updateData(PXCMFaceData.Face face) { PXCMFaceData.LandmarksData ldata = face.QueryLandmarks(); // get the landmark data var landmarkGroupTypes = Enum.GetValues(typeof(PXCMFaceData.LandmarksGroupType)).Cast <PXCMFaceData.LandmarksGroupType>(); int featureIdx = 0; // 对于每个LandmarkPoint转换成成员变量中的world属性 foreach (var landmarkGroupType in landmarkGroupTypes) { PXCMFaceData.LandmarkPoint[] points; ldata.QueryPointsByGroup(landmarkGroupType, out points); PXCMPoint3DF32[] Point3DArray = new PXCMPoint3DF32[points.Length]; for (int i = 0; i < points.Length; i++) { Point3DArray[i] = points[i].world; // 把脸部特征放入数组中 Landmarks[featureIdx++] = points[i].world.x; Landmarks[featureIdx++] = points[i].world.y; Landmarks[featureIdx++] = points[i].world.z; } // 将world坐标加进去 // landmarksData.Add(landmarkGroupType, Point3DArray); } }
/// <summary> /// 保存筛选32点的数据 /// </summary> /// <param name="qface"></param> /// <param name="frameCount"></param> private void Savedata_less(PXCMFaceData.Face qface, int frameCount) { ////zz PXCMFaceData.PoseData posedata = qface.QueryPose(); PXCMFaceData.LandmarksData Idata = qface.QueryLandmarks(); PXCMFaceData.LandmarkPoint[] points; PXCMFaceData.PoseEulerAngles angles; PXCMFaceData.HeadPosition headpostion; posedata.QueryPoseAngles(out angles); posedata.QueryHeadPosition(out headpostion); Idata.QueryPoints(out points); string time = DateTime.Now.ToString("yyyy-MM-dd") + " " + DateTime.Now.ToString("hh:mm:ss:fff"); sw_less.Write(frameCount.ToString().PadRight(5) + '\t' + time + '\t' + expressionNumber.ToString() + '\t'); int[] a = new int[32] { 76, 77, 12, 16, 14, 10, 20, 24, 18, 22, 71, 0, 4, 74, 5, 9, 29, 26, 31, 30, 32, 39, 33, 47, 46, 48, 51, 52, 50, 56, 66, 61 }; int t = 0; for (int i = 0; i < 32; i++) { //string LandmarkPointName = "points[a[t]].source.index"; string LandmarkPointName = MarkPointName(points[a[t]].source.index); float Positionworld_x = points[a[t]].world.x; float Positionworld_y = points[a[t]].world.y; float Positionworld_z = points[a[t]].world.z; float PositionImage_x = points[a[t]].image.x; float PositionImage_y = points[a[t++]].image.y; sw_less.Write((a[i]).ToString() + '\t' + LandmarkPointName.ToString() + '\t' + Positionworld_x.ToString().PadRight(25) + '\t' + Positionworld_y.ToString().PadRight(25) + '\t' + Positionworld_z.ToString().PadRight(25) + '\t' + PositionImage_x.ToString().PadRight(25) + '\t' + PositionImage_y.ToString().PadRight(25) + '\t'); } float HeadCenter_x = headpostion.headCenter.x; float HeadCenter_y = headpostion.headCenter.y; float HeadCenter_z = headpostion.headCenter.z; float PoseEulerAngles_pitch = angles.pitch; float PoseEulerAngles_roll = angles.roll; float PoseEulerAngles_yaw = angles.yaw; sw_less.Write(HeadCenter_x.ToString().PadRight(25) + '\t' + HeadCenter_y.ToString().PadRight(25) + '\t' + HeadCenter_z.ToString().PadRight(25) + '\t' + PoseEulerAngles_pitch.ToString().PadRight(25) + '\t' + PoseEulerAngles_roll.ToString().PadRight(25) + '\t' + PoseEulerAngles_yaw.ToString().PadRight(25) + '\t'); sw_less.WriteLine(); }
public override void Work(Graphics g) { data = module.CreateOutput(); data.Update(); // Get the number of tracked faces Int32 nfaces = data.QueryNumberOfDetectedFaces(); Console.WriteLine("Number of faces : " + nfaces); for (Int32 i = 0; i < nfaces; i++) { // all faces in the picture PXCMFaceData.Face face = data.QueryFaceByIndex(i); //face location PXCMFaceData.DetectionData ddata = face.QueryDetection(); // Retrieve the face landmark data instance PXCMFaceData.Face landmark = data.QueryFaceByIndex(i); PXCMFaceData.LandmarksData ldata = landmark.QueryLandmarks(); // work on DetectionData PXCMRectI32 rect; ddata.QueryBoundingRect(out rect); //draw rect Rectangle rectangle = new Rectangle(rect.x, rect.y, rect.w, rect.h); // Convert to Rectangle g.DrawRectangle(pen, rectangle); // Draw // get the landmark data PXCMFaceData.LandmarkPoint[] points; ldata.QueryPoints(out points); //g.DrawImage(points[0].image,); for (Int32 j = 0; j < points.Length; j++) { //Point p = new Point(); // p = points[0].ToString; Point p = new Point(); p.X = (int)points[j].image.x; p.Y = (int)points[j].image.y; g.DrawEllipse(pen, points[j].image.x, points[j].image.y, 2, 2); } } data.Dispose(); }
private void ElaborateSample(PXCMCapture.Sample sample, PXCMFaceData.Face face) { if (sample == null) { return; } WriteableBitmap imageRGB = null; if (sample.color != null) { imageRGB = sample.color.GetImage(); } if (face != null) { PXCMFaceData.LandmarksData landmarkData = face.QueryLandmarks(); PXCMFaceData.LandmarkPoint[] landmarkPoints = null; if (landmarkData.QueryPoints(out landmarkPoints)) { foreach (var point in landmarkPoints) { if (point.confidenceImage > 50) { if (point.source.alias != PXCMFaceData.LandmarkType.LANDMARK_NOT_NAMED) { imageRGB.FillEllipseCentered((int)point.image.x, (int)point.image.y, 4, 4, Colors.White); } else { imageRGB.FillEllipseCentered((int)point.image.x, (int)point.image.y, 4, 4, Colors.Yellow); } } } } } if (imageRGB != null) { imageRGB.Freeze(); } Dispatcher.Invoke(() => { this.ImageRGB = imageRGB; }); Process.GetCurrentProcess(); }
private void TrackFace(PXCMFaceData.Face face) { PXCMFaceData.LandmarksData landmarksData = face.QueryLandmarks(); if (landmarksData == null) { return; } PXCMFaceData.LandmarkPoint[] facePoints; landmarksData.QueryPoints(out facePoints); if (facePoints == null) { return; } SetLandmarkPoint(landmarksData, facePoints, PXCMFaceData.LandmarkType.LANDMARK_UPPER_LIP_CENTER, _camera.Face.Mouth); SetLandmarkPoint(landmarksData, facePoints, PXCMFaceData.LandmarkType.LANDMARK_EYE_LEFT_CENTER, _camera.Face.LeftEye); SetLandmarkPoint(landmarksData, facePoints, PXCMFaceData.LandmarkType.LANDMARK_EYE_RIGHT_CENTER, _camera.Face.RightEye); SetLandmarkPoint(landmarksData, facePoints, PXCMFaceData.LandmarkType.LANDMARK_NOSE_TOP, _camera.Face); }
private void TrackFace(PXCMFaceData.Face face) { //PXCMFaceData.HeadPosition position; //face.QueryPose().QueryHeadPosition(out position); //PXCMRectI32 rect; //face.QueryDetection().QueryBoundingRect(out rect); //var point = new Point3D(rect.x + rect.w / 2, rect.y + rect.h / 2); //_camera.Face.Position = CreatePosition(point, new Point3D()); //_camera.Face.Position = CreatePosition(position.headCenter.ToPoint3D(), ); PXCMFaceData.LandmarksData landmarksData = face.QueryLandmarks(); if (landmarksData == null) { return; } PXCMFaceData.LandmarkPoint[] facePoints; landmarksData.QueryPoints(out facePoints); if (facePoints == null) { return; } foreach (var item in facePoints) { switch (item.source.alias) { case PXCMFaceData.LandmarkType.LANDMARK_UPPER_LIP_CENTER: _camera.Face.Mouth.Position = CreatePosition(item.image.ToPoint3D(), item.world.ToPoint3D()); break; case PXCMFaceData.LandmarkType.LANDMARK_EYE_LEFT_CENTER: _camera.Face.LeftEye.Position = CreatePosition(item.image.ToPoint3D(), item.world.ToPoint3D()); break; case PXCMFaceData.LandmarkType.LANDMARK_EYE_RIGHT_CENTER: _camera.Face.RightEye.Position = CreatePosition(item.image.ToPoint3D(), item.world.ToPoint3D()); break; case PXCMFaceData.LandmarkType.LANDMARK_NOSE_TOP: _camera.Face.Position = CreatePosition(item.image.ToPoint3D(), item.world.ToPoint3D()); break; } } }
private void GetFaceLandmarks(PXCMFaceData.Face face, out PointF nose, out PointF leftEye, out PointF rightEye, out bool landmarkDetected) { nose = PointF.Empty; leftEye = PointF.Empty; rightEye = PointF.Empty; landmarkDetected = false; PXCMFaceData.LandmarksData landmarksData = face.QueryLandmarks(); if (landmarksData != null) { int counter = 0; PXCMFaceData.LandmarkPoint noseTip; landmarksData.QueryPoint(29, out noseTip); if (noseTip != null && noseTip.confidenceImage != 0) { nose = new PointF(noseTip.image.x + LandmarkAllignment, noseTip.image.y + LandmarkAllignment); counter++; } PXCMFaceData.LandmarkPoint lEye; landmarksData.QueryPoint(76, out lEye); if (lEye != null && lEye.confidenceImage != 0) { leftEye = new PointF(lEye.image.x + LandmarkAllignment, lEye.image.y + LandmarkAllignment); counter++; } PXCMFaceData.LandmarkPoint rEye; landmarksData.QueryPoint(77, out rEye); if (rEye != null && rEye.confidenceImage != 0) { rightEye = new PointF(rEye.image.x + LandmarkAllignment, rEye.image.y + LandmarkAllignment); counter++; } if (counter == 3) { landmarkDetected = true; } } }
public void DrawLandmark(PXCMFaceData.Face face) { if (isRegisteredDetected(face)) { Debug.Assert(face != null); PXCMFaceData.LandmarksData landmarks = face.QueryLandmarks(); if (m_bitmap == null || !Landmarks.Checked || landmarks == null) { return; } lock (m_bitmapLock) { using (Graphics graphics = Graphics.FromImage(m_bitmap)) using (var brush = new SolidBrush(Color.White)) using (var lowConfidenceBrush = new SolidBrush(Color.Red)) using (var font = new Font(FontFamily.GenericMonospace, m_faceTextOrganizer.FontSize, FontStyle.Bold)) { PXCMFaceData.LandmarkPoint[] points; bool res = landmarks.QueryPoints(out points); Debug.Assert(res); var point = new PointF(); foreach (PXCMFaceData.LandmarkPoint landmark in points) { point.X = landmark.image.x + LandmarkAlignment; point.Y = landmark.image.y + LandmarkAlignment; if (landmark.confidenceImage == 0) { graphics.DrawString("x", font, lowConfidenceBrush, point); } else { graphics.DrawString("•", font, brush, point); } } } } } }
public void updateData(PXCMFaceData.Face face) { if (face == null) { return; } PXCMFaceData.LandmarksData ldata = face.QueryLandmarks(); if (ldata == null) { return; } PXCMRectI32 rect; face.QueryDetection().QueryBoundingRect(out rect); // get the landmark data var landmarkGroupTypes = Enum.GetValues(typeof(PXCMFaceData.LandmarksGroupType)).Cast <PXCMFaceData.LandmarksGroupType>(); int count = 0; // 对于每个LandmarkPoint转换成成员变量中的world属性 foreach (var landmarkGroupType in landmarkGroupTypes) { PXCMFaceData.LandmarkPoint[] points; ldata.QueryPointsByGroup(landmarkGroupType, out points); PXCMPoint3DF32[] Point3DArray = new PXCMPoint3DF32[points.Length]; for (int i = 0; i < points.Length; i++) { Point3DArray[i] = points[i].world; Console.WriteLine(String.Join(" ", rect.x, rect.y, rect.w, rect.h)); Console.WriteLine(points[i].image.x + "\t" + points[i].image.y + "\t" + points[i].world.z); } count += points.Length; // 将world坐标加进去 landmarksData.Add(landmarkGroupType, Point3DArray); } Console.WriteLine(count); }
private void SaveFeature(PXCMFaceData.Face qface, int frameCount) { PXCMFaceData.LandmarksData ldata = qface.QueryLandmarks(); PXCMFaceData.ExpressionsData edata = qface.QueryExpressions(); PXCMRectI32 rect; // get the landmark data var landmarkGroupTypes = Enum.GetValues(typeof(PXCMFaceData.LandmarksGroupType)).Cast <PXCMFaceData.LandmarksGroupType>(); qface.QueryDetection().QueryBoundingRect(out rect); //保存时间戳 string time = DateTime.Now.ToString("yyyy-MM-dd") + " " + DateTime.Now.ToString("HH:mm:ss:fff"); sw_feature.Write(frameCount.ToString() + ' ' + time + ' '); // 对于每个LandmarkPoint转换成成员变量中的world属性 PXCMFaceData.LandmarkPoint[] points; ldata.QueryPoints(out points); for (int i = 0; i < 78; i++) { //把脸部特征放入数组中 double position_x = Math.Round(points[i].image.x - rect.x, 4); double position_y = Math.Round(points[i].image.y - rect.y, 4); double position_z = Math.Round(points[i].world.z, 5); sw_feature.Write(position_x.ToString() + ' ' + position_y.ToString() + ' ' + position_z.ToString() + ' '); } for (int i = 0; i < 22; i++) { double emotion = 0; if (edata != null) { PXCMFaceData.ExpressionsData.FaceExpressionResult score; edata.QueryExpression((PXCMFaceData.ExpressionsData.FaceExpression)i, out score); Expression.facialExpressionIndensity[i] = score.intensity; emotion = score.intensity; } sw_feature.Write(emotion.ToString() + ' '); } sw_feature.WriteLine(); }
private static void UpdateFace() { landmarksData = null; if (faceData == null) { return; } faceData.Update(); PXCMFaceData.Face face = faceData.QueryFaceByIndex(0); if (face != null) { landmarksData = face.QueryLandmarks(); expressionData = face.QueryExpressions(); poseData = face.QueryPose(); UpdateFaceOrientation(poseData); UpdateFacePosition(poseData); UpdateFaceGeometry(landmarksData); } }
private void TrackFace(PXCMFaceData.Face face) { PXCMRectI32 rect; face.QueryDetection().QueryBoundingRect(out rect); var point = new Point3D(rect.x + rect.w / 2, rect.y + rect.h / 2); Face.Position = CreatePosition(point, new Point3D()); PXCMFaceData.LandmarksData landmarksData = face.QueryLandmarks(); if (landmarksData == null) { return; } PXCMFaceData.LandmarkPoint[] facePoints; landmarksData.QueryPoints(out facePoints); if (facePoints == null) { return; } foreach (var item in facePoints) { switch (item.source.alias) { case PXCMFaceData.LandmarkType.LANDMARK_UPPER_LIP_CENTER: Face.Mouth.Position = CreatePosition(ToPoint3D(item.image), ToPoint3D(item.world)); break; case PXCMFaceData.LandmarkType.LANDMARK_EYE_LEFT_CENTER: Face.LeftEye.Position = CreatePosition(ToPoint3D(item.image), ToPoint3D(item.world)); break; case PXCMFaceData.LandmarkType.LANDMARK_EYE_RIGHT_CENTER: Face.RightEye.Position = CreatePosition(ToPoint3D(item.image), ToPoint3D(item.world)); break; } } }
private void updateFaceFrame() { PXCMCapture.Sample sample = this.senseManager.QuerySample(); this.image = sample.color; if (this.image != null) { this.invalidate = true; } this.faceData.Update(); // get number of faces FOutFaceLandmarkPoints.SliceCount = 0; FOutFaceExpressionsResult.SliceCount = 0; int numFaces = this.faceData.QueryNumberOfDetectedFaces(); for (int i = 0; i < numFaces; ++i) { // get faces info PXCMFaceData.Face face = this.faceData.QueryFaceByIndex(i); // get face position by Depth var detection = face.QueryDetection(); if (detection != null) { // detection PXCMRectI32 faceRect; detection.QueryBoundingRect(out faceRect); int sliceCount = i + 1; FOutFacePosition.SliceCount = sliceCount; FOutFacePosition[i] = new Vector2D(faceRect.x, faceRect.y); FOutFaceWidth.SliceCount = sliceCount; FOutFaceWidth[i] = faceRect.w; FOutFaceHeight.SliceCount = sliceCount; FOutFaceHeight[i] = faceRect.h; // pose(only use Depth mode) PXCMFaceData.PoseData pose = face.QueryPose(); if (pose != null) { // faces angle PXCMFaceData.PoseEulerAngles poseAngle = new PXCMFaceData.PoseEulerAngles(); pose.QueryPoseAngles(out poseAngle); FOutFacePose.SliceCount = sliceCount; FOutFacePose[i] = new Vector3D(poseAngle.pitch, poseAngle.yaw, poseAngle.roll); } // landmarks PXCMFaceData.LandmarksData landmarks = face.QueryLandmarks(); FOutFaceLandmarkBinSize.SliceCount = sliceCount; if (landmarks != null) { // number of feature points from landmarks int numPoints = landmarks.QueryNumPoints(); FOutFaceLandmarkBinSize[i] = numPoints; PXCMFaceData.LandmarkPoint[] landmarkPoints = new PXCMFaceData.LandmarkPoint[numPoints]; int prevSliceCount = FOutFaceLandmarkPoints.SliceCount; FOutFaceLandmarkPoints.SliceCount = prevSliceCount + numPoints; if (landmarks.QueryPoints(out landmarkPoints)) { for (int j = 0; j < numPoints; j++) { int index = prevSliceCount + j; FOutFaceLandmarkPoints[index] = new Vector2D(landmarkPoints[j].image.x, landmarkPoints[j].image.y); } } } else { FOutFaceLandmarkBinSize[i] = 0; FOutFaceLandmarkPoints.SliceCount = 0; } PXCMFaceData.ExpressionsData expressionData = face.QueryExpressions(); if (expressionData != null) { for (int j = 0; j < FInExpressions.SliceCount; j++) { PXCMFaceData.ExpressionsData.FaceExpressionResult expressionResult; if (expressionData.QueryExpression(FInExpressions[j], out expressionResult)) { FOutFaceExpressionsResult.SliceCount++; FOutFaceExpressionsResult[j] = expressionResult.intensity; } else { // do nothing } } } else { FOutFaceExpressionsResult.SliceCount = 0; } PXCMFaceData.PulseData pulseData = face.QueryPulse(); if (pulseData != null) { FOutPulse.SliceCount = sliceCount; FOutPulse[i] = pulseData.QueryHeartRate(); } } } }
//zz private void Savedata(PXCMFaceData faceOutput) { ////zz PXCMFaceData.Face qface = faceOutput.QueryFaceByIndex(0); PXCMFaceData.PoseData posedata = qface.QueryPose(); PXCMFaceData.LandmarksData Idata = qface.QueryLandmarks(); PXCMFaceData.LandmarkPoint[] points; PXCMFaceData.PoseEulerAngles angles; PXCMFaceData.HeadPosition headpostion; posedata.QueryPoseAngles(out angles); posedata.QueryHeadPosition(out headpostion); Idata.QueryPoints(out points); string connSting; connSting = "server=localhost;database=RealSense;Integrated Security=True "; SqlConnection sConn = new SqlConnection(connSting); try { sConn.Open(); } catch (Exception ex) { Console.WriteLine("链接错误:" + ex.Message); } int[] a = new int[32] { 76, 77, 12, 16, 14, 10, 20, 24, 18, 22, 70, 0, 4, 7, 5, 9, 29, 26, 31, 30, 32, 39, 33, 47, 46, 48, 51, 52, 50, 56, 65, 61 }; int t = 0; for (int i = 0; i < 32; i++) { string sql_Insert; string times = DateTime.Now.ToString("yyyy-MM-dd") + " " + DateTime.Now.ToString("hh:mm:ss"); string LandmarkPointName = MarkPointName(points[a[t]].source.index); float Positionworld_x = points[a[t]].world.x; float Positionworld_y = points[a[t]].world.y; float Positionworld_z = points[a[t]].world.z; float PositionImage_x = points[a[t]].image.x; float PositionImage_y = points[a[t++]].image.y; float HeadCenter_x = headpostion.headCenter.x; float HeadCenter_y = headpostion.headCenter.y; float HeadCenter_z = headpostion.headCenter.z; float PoseEulerAngles_pitch = angles.pitch; float PoseEulerAngles_roll = angles.roll; float PoseEulerAngles_yaw = angles.yaw; sql_Insert = "insert into FaceData(time,LandmarkPointName,[Positionworld.x],[Positionworld.y],[Positionworld.z],[PositionImage.x],[PositionImage.y],[HeadCenter.x],[HeadCenter.y],[HeadCenter.z],[PoseEulerAngles.pitch],[PoseEulerAngles.roll],[PoseEulerAngles.yaw])values('" + times + "','" + LandmarkPointName + "','" + Positionworld_x + "','" + Positionworld_y + "','" + Positionworld_z + "','" + PositionImage_x + "','" + PositionImage_y + "','" + HeadCenter_x + "','" + HeadCenter_y + "','" + HeadCenter_z + "','" + PoseEulerAngles_pitch + "','" + PoseEulerAngles_roll + "','" + PoseEulerAngles_yaw + "')"; SqlCommand sCmd = new SqlCommand(sql_Insert, sConn); sCmd.ExecuteNonQuery(); } sConn.Close(); }
public override bool Process(Trigger trigger) { trigger.ErrorDetected = false; if (!SenseToolkitManager.Instance.IsSenseOptionSet(SenseOption.SenseOptionID.Face)) { trigger.ErrorDetected = true; return(false); } if (!(trigger is TrackTrigger)) { trigger.ErrorDetected = true; return(false); } // make sure we have valid values if (RealWorldBoxDimensions.x <= 0) { RealWorldBoxDimensions.x = 1; } if (RealWorldBoxDimensions.y <= 0) { RealWorldBoxDimensions.y = 1; } if (RealWorldBoxDimensions.z <= 0) { RealWorldBoxDimensions.z = 1; } if (SenseToolkitManager.Instance.Initialized && SenseToolkitManager.Instance.FaceModuleOutput != null) { if (SenseToolkitManager.Instance.FaceModuleOutput.QueryNumberOfDetectedFaces() == 0) { ((TrackTrigger)trigger).Position = Vector3.zero; return(false); } PXCMFaceData.Face singleFaceOutput = null; singleFaceOutput = SenseToolkitManager.Instance.FaceModuleOutput.QueryFaceByIndex(FaceIndex); bool success = false; if (singleFaceOutput != null && singleFaceOutput.QueryUserID() >= 0) { // Process Tracking if (trigger is TrackTrigger) { TrackTrigger specificTrigger = (TrackTrigger)trigger; var landmarksData = singleFaceOutput.QueryLandmarks(); bool hasLandmarks = false; if (landmarksData != null) { PXCMFaceData.LandmarkPoint outpt = null; bool hasPoint = landmarksData.QueryPoint(landmarksData.QueryPointIndex(LandmarkToTrack), out outpt); if (hasPoint) { hasLandmarks = outpt.confidenceWorld != 0; } } if (!hasLandmarks && useBoundingBox) { PXCMRectI32 rect = new PXCMRectI32(); if (singleFaceOutput.QueryDetection() != null && singleFaceOutput.QueryDetection().QueryBoundingRect(out rect)) { float depth; singleFaceOutput.QueryDetection().QueryFaceAverageDepth(out depth); float bbCenterX = (rect.x + rect.w / 2); float bbCenterY = (rect.y + rect.h / 2); Vector3 vec = new Vector3(); if (_pos_ijz == null) { _pos_ijz = new PXCMPoint3DF32[1] { new PXCMPoint3DF32() }; } _pos_ijz[0].x = bbCenterX; _pos_ijz[0].y = bbCenterY; _pos_ijz[0].z = depth; if (_pos3d == null) { _pos3d = new PXCMPoint3DF32[1] { new PXCMPoint3DF32() }; } SenseToolkitManager.Instance.Projection.ProjectDepthToCamera(_pos_ijz, _pos3d); Vector3 position = new Vector3(); vec.x = _pos3d[0].x / 10f; vec.y = _pos3d[0].y / 10f; vec.z = _pos3d[0].z / 10f; // Clamp and normalize to the Real World Box TrackingUtilityClass.ClampToRealWorldInputBox(ref vec, RealWorldBoxCenter, RealWorldBoxDimensions); TrackingUtilityClass.Normalize(ref vec, RealWorldBoxCenter, RealWorldBoxDimensions); if (!float.IsNaN(vec.x) && !float.IsNaN(vec.y) && !float.IsNaN(vec.z)) { specificTrigger.Position = vec; return(true); } } else { specificTrigger.Position = Vector3.zero; return(false); } } else if (landmarksData == null && !useBoundingBox) { specificTrigger.Position = Vector3.zero; return(false); } else { int landmarkId = landmarksData.QueryPointIndex(LandmarkToTrack); PXCMFaceData.LandmarkPoint point = null; landmarksData.QueryPoint(landmarkId, out point); // Translation if (point != null) { Vector3 vec = new Vector3(); vec.x = -point.world.x * 100f; vec.y = point.world.y * 100f; vec.z = point.world.z * 100f; if (vec.x + vec.y + vec.z == 0) { specificTrigger.Position = Vector3.zero; return(false); } // Clamp and normalize to the Real World Box TrackingUtilityClass.ClampToRealWorldInputBox(ref vec, RealWorldBoxCenter, RealWorldBoxDimensions); TrackingUtilityClass.Normalize(ref vec, RealWorldBoxCenter, RealWorldBoxDimensions); if (!float.IsNaN(vec.x) && !float.IsNaN(vec.y) && !float.IsNaN(vec.z)) { specificTrigger.Position = vec; success = true; } } //Rotation PXCMFaceData.PoseData poseData = singleFaceOutput.QueryPose(); if (success && poseData != null) { PXCMFaceData.PoseEulerAngles angles; if (poseData.QueryPoseAngles(out angles)) { if (!float.IsNaN(angles.pitch) && !float.IsNaN(angles.yaw) && !float.IsNaN(angles.roll)) { Quaternion q = Quaternion.Euler(-angles.pitch, angles.yaw, -angles.roll); specificTrigger.RotationQuaternion = q; return(true); } } } } } } } return(false); }
private void ProcessingThread() { string videoName, nameColor, nameDepth, nameIr; int width = 640; int height = 480; int frameIndex = 0; string formatImageFile = ".png"; int nframes = 0; int lostFrames = 0; string landmarks = null; long frameTimeStamp = 0; PXCMImage color; PXCMImage depth; PXCMImage ir; PXCMCapture.Sample sample; PXCMImage.ImageData imageColor; PXCMImage.ImageData imageDepth; PXCMImage.ImageData imageIr; WriteableBitmap wbm1, wbm2, wbm3; Int32Rect rect2crop; PXCMFaceModule faceModule; PXCMFaceConfiguration faceConfig; PXCMFaceData faceData = null; //Offset Cropped rectangle Offset offset = new Offset(0, 0, 0, 0); //For each directory, extract all landmarks or images streams from all videos foreach (var dir in dirsSource) { //If the folder is not empty if (Directory.EnumerateFileSystemEntries(dir).Any()) { dictPaths.TryGetValue(dir, out paths); //This dict contains all source and output dirs List <string> fileList = new List <string>(Directory.GetFiles(dir, "*.rssdk")); //For each video foreach (var inputFile in fileList) { lostFrames = 0; videoName = inputFile.Split('\\').Last().Split('.')[0]; // Create a SenseManager instance sm = PXCMSenseManager.CreateInstance(); // Recording mode: true // Playback mode: false // Settings for playback mode (read rssdk files and extract frames) sm.captureManager.SetFileName(inputFile, false); sm.captureManager.SetRealtime(false); nframes = sm.captureManager.QueryNumberOfFrames(); //Update in realtime the current extraction Application.Current.Dispatcher.BeginInvoke(new Action(() => { textBox2.Text = nframes.ToString(); textBox3.Text = String.Format("Record: {0}\nVideo: {1}", paths.root, videoName); })); sm.EnableStream(PXCMCapture.StreamType.STREAM_TYPE_COLOR, width, height, 0); sm.EnableStream(PXCMCapture.StreamType.STREAM_TYPE_DEPTH, width, height); sm.EnableStream(PXCMCapture.StreamType.STREAM_TYPE_IR, width, height); //Extract Landmarks sm.EnableFace(); faceModule = sm.QueryFace(); faceConfig = faceModule.CreateActiveConfiguration(); faceConfig.landmarks.maxTrackedFaces = 1; faceConfig.landmarks.isEnabled = true; faceConfig.detection.maxTrackedFaces = 1; faceConfig.detection.isEnabled = true; faceConfig.EnableAllAlerts(); faceConfig.ApplyChanges(); sm.Init(); // This string stores all data before saving to csv file landmarks = null; // Start AcquireFrame/ReleaseFrame loop var stopwatch = new Stopwatch(); stopwatch.Start(); while (sm.AcquireFrame(true) >= pxcmStatus.PXCM_STATUS_NO_ERROR) { // Retrieve face data faceModule = sm.QueryFace(); frameIndex = sm.captureManager.QueryFrameIndex(); if (faceModule != null) { // Retrieve the most recent processed data faceData = faceModule.CreateOutput(); faceData.Update(); } if (faceData != null) { Int32 nfaces = faceData.QueryNumberOfDetectedFaces(); frameTimeStamp = sm.captureManager.QueryFrameTimeStamp(); //PXCMCapture.Sample sample = senseManager.QuerySample(); if (nfaces == 0) //If none face was detected, we will consider as a "lost frame" { lostFrames += 1; } for (Int32 i = 0; i < nfaces; i++) { //Retrieve the image sample = sm.QuerySample(); // Work on the images color = sample.color; depth = sample.depth; ir = sample.ir; PXCMFaceData.Face face = faceData.QueryFaceByIndex(i); PXCMFaceData.LandmarksData landmarkData = face.QueryLandmarks(); PXCMFaceData.DetectionData ddata = face.QueryDetection(); PXCMFaceData.PoseData poseData = face.QueryPose(); poseData.QueryHeadPosition(out PXCMFaceData.HeadPosition headPosition); poseData.QueryPoseAngles(out PXCMFaceData.PoseEulerAngles poseEulerAngles); Debug.WriteLine(headPosition.headCenter.x + " " + headPosition.headCenter.y + " " + headPosition.headCenter.z + " " + poseEulerAngles.pitch + " " + poseEulerAngles.roll + " " + poseEulerAngles.yaw); //Rectangle coordenates from detected face ddata.QueryBoundingRect(out PXCMRectI32 rect); //See the offset struct to define the values rect2crop = new Int32Rect(rect.x + offset.x, rect.y + offset.y, rect.w + offset.w, rect.h + offset.h); ddata.QueryFaceAverageDepth(out Single depthDistance); color.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_RGB32, out imageColor); depth.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_DEPTH_RAW, out imageDepth); ir.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_RGB24, out imageIr); //Convert it to Bitmap wbm1 = imageColor.ToWritableBitmap(0, color.info.width, color.info.height, 100.0, 100.0); wbm2 = imageDepth.ToWritableBitmap(0, depth.info.width, depth.info.height, 100.0, 100.0); wbm3 = imageIr.ToWritableBitmap(0, ir.info.width, ir.info.height, 100.0, 100.0); color.ReleaseAccess(imageColor); depth.ReleaseAccess(imageDepth); ir.ReleaseAccess(imageIr); nameColor = paths.rgbFolder + "\\" + videoName + "\\" + videoName + "_color_" + frameIndex + formatImageFile; nameDepth = paths.depthFolder + "\\" + videoName + "\\" + videoName + "_depth_" + frameIndex + formatImageFile; nameIr = paths.irFolder + "\\" + videoName + "\\" + videoName + "_ir_" + frameIndex + formatImageFile; //Crops the face images! CreateThumbnail(nameColor, new CroppedBitmap(wbm1, rect2crop)); CreateThumbnail(nameDepth, new CroppedBitmap(wbm2, rect2crop)); CreateThumbnail(nameIr, new CroppedBitmap(wbm3, rect2crop)); //Debug.WriteLine((depthDistance /1000 ) + " m" + " " + rect.x + " " + rect.y + " " + rect.w + " " + rect.h); /* * x - The horizontal coordinate of the top left pixel of the rectangle. * y - The vertical coordinate of the top left pixel of the rectangle. * w - The rectangle width in pixels. * h -The rectangle height in pixels.*/ if (landmarkData != null) { PXCMFaceData.LandmarkPoint[] landmarkPoints; landmarkData.QueryPoints(out landmarkPoints); Application.Current.Dispatcher.BeginInvoke(new Action(() => textBox1.Text = frameIndex.ToString())); landmarks += inputFile.Split('\\').Last() + ";" + frameIndex + ";" + nameColor + ";" + nameDepth + ";" + nameIr + ";" + frameTimeStamp + ";" + depthDistance.ToString("F") + ";" + poseEulerAngles.yaw.ToString("F") + ";" + poseEulerAngles.pitch.ToString("F") + ";" + poseEulerAngles.roll.ToString("F") + ";"; // Begin line with frame info for (int j = 0; j < landmarkPoints.Length; j++) // Writes landmarks coordinates along the line { //get world coordinates landmarks += /*landmarkPoints[j].source.index + ";" +*/ (landmarkPoints[j].world.x * 1000).ToString("F") + ";" + (landmarkPoints[j].world.y * 1000).ToString("F") + ";" + (landmarkPoints[j].world.z * 1000).ToString("F") + ";"; } for (int j = 0; j < landmarkPoints.Length; j++) { //get coordinate of the image pixel landmarks += /*landmarkPoints[j].confidenceImage + ";" + */ landmarkPoints[j].image.x.ToString("F") + ";" + landmarkPoints[j].image.y.ToString("F") + ";"; } landmarks += '\n'; // Breaks line after the end of the frame coordinates } } } // Release the frame if (faceData != null) { faceData.Dispose(); } sm.ReleaseFrame(); WriteToFile(paths.csvFile, landmarks); landmarks = null; } sm.Dispose(); stopwatch.Stop(); //Update in realtime the current extraction Application.Current.Dispatcher.BeginInvoke(new Action(() => { elapsedLabel.Content = String.Format("Elapsed Time: {0} (s)", stopwatch.Elapsed.TotalSeconds.ToString("F")); })); } } } }
private bool UpdateFace(AnimationTrigger animationTrigger) { animationTrigger.IsAnimationDataValid = false; // Get the closest face PXCMFaceData.Face pxcmFace = SenseToolkitManager.Instance.FaceModuleOutput.QueryFaceByIndex(0); if (pxcmFace == null) { return(false); } if (SenseToolkitManager.Instance.FaceModuleOutput.QueryNumberOfDetectedFaces() <= 0) { // PXCMFaceData.QueryFaceByIndex(0) failed. return(false); } PXCMFaceData.LandmarksData pxcmLandmarksData = pxcmFace.QueryLandmarks(); if (pxcmLandmarksData == null) { // PXCMFaceData.Face.QueryLandmarks() failed. return(false); } Dictionary <PXCMFaceData.LandmarkType, PXCMFaceData.LandmarkPoint> landmarkPoints = new Dictionary <PXCMFaceData.LandmarkType, PXCMFaceData.LandmarkPoint>(); PXCMFaceData.LandmarkType[] landmarkTypes = new PXCMFaceData.LandmarkType[] { PXCMFaceData.LandmarkType.LANDMARK_NOSE_TIP, PXCMFaceData.LandmarkType.LANDMARK_NOSE_TOP, PXCMFaceData.LandmarkType.LANDMARK_EYEBROW_LEFT_CENTER, PXCMFaceData.LandmarkType.LANDMARK_EYE_LEFT_CENTER, PXCMFaceData.LandmarkType.LANDMARK_EYEBROW_RIGHT_CENTER, PXCMFaceData.LandmarkType.LANDMARK_EYE_RIGHT_CENTER, PXCMFaceData.LandmarkType.LANDMARK_UPPER_LIP_CENTER, PXCMFaceData.LandmarkType.LANDMARK_LOWER_LIP_CENTER, PXCMFaceData.LandmarkType.LANDMARK_EYELID_LEFT_TOP, PXCMFaceData.LandmarkType.LANDMARK_EYELID_LEFT_BOTTOM, PXCMFaceData.LandmarkType.LANDMARK_EYELID_RIGHT_TOP, PXCMFaceData.LandmarkType.LANDMARK_EYELID_RIGHT_BOTTOM }; foreach (PXCMFaceData.LandmarkType landmarkType in landmarkTypes) { PXCMFaceData.LandmarkPoint landmarkPoint = GetLandmarkPoint(pxcmLandmarksData, landmarkType); if (landmarkPoint == null) { // PXCMFaceData.LandmarksData.QueryPointIndex() failed. return(false); } landmarkPoints.Add(landmarkType, landmarkPoint); } PXCMFaceData.ExpressionsData pxcmExpressionsData = pxcmFace.QueryExpressions(); if (pxcmExpressionsData == null) { // PXCMFaceData.Face.QueryExpressions() failed. return(false); } animationTrigger.IsAnimationDataValid = true; PXCMCapture.Device.MirrorMode mirrorMode = PXCMCapture.Device.MirrorMode.MIRROR_MODE_DISABLED; PXCMCaptureManager pxcmCaptureManager = SenseToolkitManager.Instance.SenseManager.QueryCaptureManager(); if (pxcmCaptureManager != null) { PXCMCapture.Device pxcmCaptureDevice = pxcmCaptureManager.QueryDevice(); if (pxcmCaptureDevice != null) { mirrorMode = pxcmCaptureDevice.QueryMirrorMode(); } } animationTrigger.Animations.Clear(); float faceHeight = landmarkPoints[PXCMFaceData.LandmarkType.LANDMARK_NOSE_TIP].image.y - landmarkPoints[PXCMFaceData.LandmarkType.LANDMARK_NOSE_TOP].image.y; float leftEyebrowUp = GetNormalizedLandmarksDistance(landmarkPoints[PXCMFaceData.LandmarkType.LANDMARK_EYE_LEFT_CENTER].image.y, landmarkPoints[PXCMFaceData.LandmarkType.LANDMARK_EYEBROW_LEFT_CENTER].image.y, OPTIMAL_RELATIVE_FACE_HEIGHT, faceHeight, EYEBROW_UP_INITIAL_DISTANCE, OPTIMAL_EYEBROW_UP_MAX_DISTANCE, NORMALIZE_MAX_FACIAL_EXPRESSION_VALUE, true); float rightEyebrowUp = GetNormalizedLandmarksDistance(landmarkPoints[PXCMFaceData.LandmarkType.LANDMARK_EYE_RIGHT_CENTER].image.y, landmarkPoints[PXCMFaceData.LandmarkType.LANDMARK_EYEBROW_RIGHT_CENTER].image.y, OPTIMAL_RELATIVE_FACE_HEIGHT, faceHeight, EYEBROW_UP_INITIAL_DISTANCE, OPTIMAL_EYEBROW_UP_MAX_DISTANCE, NORMALIZE_MAX_FACIAL_EXPRESSION_VALUE, true); float leftEyeClose = GetNormalizedLandmarksDistance(landmarkPoints[PXCMFaceData.LandmarkType.LANDMARK_EYELID_LEFT_BOTTOM].image.y, landmarkPoints[PXCMFaceData.LandmarkType.LANDMARK_EYELID_LEFT_TOP].image.y, OPTIMAL_RELATIVE_FACE_HEIGHT, faceHeight, EYE_CLOSE_INITIAL_DISTANCE, OPTIMAL_EYE_CLOSE_MAX_DISTANCE, NORMALIZE_MAX_FACIAL_EXPRESSION_VALUE, true); float rightEyeClose = GetNormalizedLandmarksDistance(landmarkPoints[PXCMFaceData.LandmarkType.LANDMARK_EYELID_RIGHT_BOTTOM].image.y, landmarkPoints[PXCMFaceData.LandmarkType.LANDMARK_EYELID_RIGHT_TOP].image.y, OPTIMAL_RELATIVE_FACE_HEIGHT, faceHeight, EYE_CLOSE_INITIAL_DISTANCE, OPTIMAL_EYE_CLOSE_MAX_DISTANCE, NORMALIZE_MAX_FACIAL_EXPRESSION_VALUE, true); if (mirrorMode == PXCMCapture.Device.MirrorMode.MIRROR_MODE_HORIZONTAL) { animationTrigger.Animations.Add(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_BROW_RAISER_LEFT.ToString(), rightEyebrowUp); animationTrigger.Animations.Add(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_BROW_RAISER_RIGHT.ToString(), leftEyebrowUp); animationTrigger.Animations.Add(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_EYES_CLOSED_LEFT.ToString(), rightEyeClose); animationTrigger.Animations.Add(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_EYES_CLOSED_RIGHT.ToString(), leftEyeClose); } else { animationTrigger.Animations.Add(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_BROW_RAISER_LEFT.ToString(), leftEyebrowUp); animationTrigger.Animations.Add(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_BROW_RAISER_RIGHT.ToString(), rightEyebrowUp); animationTrigger.Animations.Add(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_EYES_CLOSED_LEFT.ToString(), leftEyeClose); animationTrigger.Animations.Add(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_EYES_CLOSED_RIGHT.ToString(), rightEyeClose); } // Instead of LANDMARK_LOWER_LIP_CENTER, we need landmark 51 (lower lip upper center) // Instead of LANDMARK_UPPER_LIP_CENTER, we need landmark 47 (upper lip lower center) animationTrigger.Animations.Add(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_MOUTH_OPEN.ToString(), GetNormalizedLandmarksDistance(landmarkPoints[PXCMFaceData.LandmarkType.LANDMARK_LOWER_LIP_CENTER].image.y, landmarkPoints[PXCMFaceData.LandmarkType.LANDMARK_UPPER_LIP_CENTER].image.y, OPTIMAL_RELATIVE_FACE_HEIGHT, faceHeight, MOUTH_OPEN_INITIAL_DISTANCE, OPTIMAL_MOUTH_OPEN_MAX_DISTANCE, NORMALIZE_MAX_FACIAL_EXPRESSION_VALUE, true)); PXCMFaceData.ExpressionsData.FaceExpressionResult pxcmFaceExpressionResult = new PXCMFaceData.ExpressionsData.FaceExpressionResult(); if (pxcmExpressionsData.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_SMILE, out pxcmFaceExpressionResult)) { animationTrigger.Animations.Add(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_SMILE.ToString(), (float)pxcmFaceExpressionResult.intensity); } else { // Error querying expression: EXPRESSION_SMILE. return(false); } return(true); }
public void DrawLandmark(PXCMFaceData.Face face) { Debug.Assert(face != null); PXCMFaceData.LandmarksData landmarks = face.QueryLandmarks(); ///zz if (isStart) { SaveFeature(face, FaceTracking.m_frame); } bool tag = true; //是否小于阈值的tag if (m_bitmap == null || landmarks == null) { return; } lock (m_bitmapLock) { using (Graphics graphics = Graphics.FromImage(m_bitmap)) using (var brush = new SolidBrush(Color.White)) using (var midConfidenceBrush = new SolidBrush(Color.Blue)) using (var lowConfidenceBrush = new SolidBrush(Color.Red)) using (var font = new Font(FontFamily.GenericMonospace, m_faceTextOrganizer.FontSize, FontStyle.Bold)) { PXCMFaceData.LandmarkPoint[] points; bool res = landmarks.QueryPoints(out points); if (_lastpoints == null) { _lastpoints = points; } Debug.Assert(res); var point = new PointF(); int[] a = new int[32] { 76, 77, 12, 16, 14, 10, 20, 24, 18, 22, 71, 0, 4, 74, 5, 9, 29, 26, 31, 30, 32, 39, 33, 47, 46, 48, 51, 52, 50, 56, 66, 61 }; for (int i = 0; i < 78; i++) { int k = 31; point.X = points[i].image.x + LandmarkAlignment; point.Y = points[i].image.y + LandmarkAlignment; if (points[i].confidenceImage == 0) { graphics.DrawString("x", font, lowConfidenceBrush, point); } graphics.DrawString("•", font, brush, point); while (k > -1) { if (a[k] == i) { graphics.DrawString(i.ToString(), font, lowConfidenceBrush, point); } k--; } if (PositionChange(points[i].image, _lastpoints[i].image) > 1) { //graphics.DrawString("o", font, brush, point); if (tag == true) { tag = false; } } } if (RegisterThisID == true) { Savedata_less(face, FaceTracking.m_frame); RegisterThisID = false; } if (!tag && isStart) { Savedata_less(face, FaceTracking.m_frame); } _lastpoints = points; } } }
public override bool Process(Trigger trigger) { trigger.ErrorDetected = false; if (!SenseToolkitManager.Instance.IsSenseOptionSet(SenseOption.SenseOptionID.Face)) { trigger.ErrorDetected = true; return(false); } if (!(trigger is TrackTrigger)) { trigger.ErrorDetected = true; return(false); } bool success = false; // make sure we have valid values if (RealWorldBoxDimensions.x <= 0) { RealWorldBoxDimensions.x = 1; } if (RealWorldBoxDimensions.y <= 0) { RealWorldBoxDimensions.y = 1; } if (RealWorldBoxDimensions.z <= 0) { RealWorldBoxDimensions.z = 1; } if (SenseToolkitManager.Instance.Initialized && SenseToolkitManager.Instance.FaceModuleOutput != null) { if (SenseToolkitManager.Instance.FaceModuleOutput.QueryNumberOfDetectedFaces() == 0) { return(false); } PXCMFaceData.Face singleFaceOutput = null; singleFaceOutput = SenseToolkitManager.Instance.FaceModuleOutput.QueryFaceByIndex(FaceIndex); if (singleFaceOutput != null && singleFaceOutput.QueryUserID() >= 0) { // Process Tracking if (trigger is TrackTrigger) { TrackTrigger specificTrigger = (TrackTrigger)trigger; var landmarksData = singleFaceOutput.QueryLandmarks(); if (landmarksData == null) { return(false); } int landmarkId = landmarksData.QueryPointIndex(LandmarkToTrack); PXCMFaceData.LandmarkPoint point = null; landmarksData.QueryPoint(landmarkId, out point); // Translation if (point != null) { Vector3 vec = new Vector3(); vec.x = point.world.x * 100; vec.y = point.world.y * 100; vec.z = point.world.z * 100; if (vec.x + vec.y + vec.z == 0) { return(false); } // Clamp and normalize to the Real World Box TrackingUtilityClass.ClampToRealWorldInputBox(ref vec, RealWorldBoxCenter, RealWorldBoxDimensions); TrackingUtilityClass.Normalize(ref vec, RealWorldBoxCenter, RealWorldBoxDimensions); if (!float.IsNaN(vec.x) && !float.IsNaN(vec.y) && !float.IsNaN(vec.z)) { specificTrigger.Position = vec; success = true; } } //Rotation PXCMFaceData.PoseData poseData = singleFaceOutput.QueryPose(); if (poseData != null) { PXCMFaceData.PoseEulerAngles angles; if (poseData.QueryPoseAngles(out angles)) { if (!float.IsNaN(angles.pitch) && !float.IsNaN(angles.yaw) && !float.IsNaN(angles.roll)) { Quaternion q = Quaternion.Euler(-angles.pitch, angles.yaw, -angles.roll); specificTrigger.RotationQuaternion = q; success = true; } } } } } return(success); } return(success); }
private void CaptureProcess() { Sender = new UDPSender(CurrentIpAdress, Convert.ToInt32(currentPort)); while (senseManager.AcquireFrame(true).IsSuccessful()) { PXCMCapture.Sample sample = senseManager.QuerySample(); PXCMImage.ImageData colorImageData; Bitmap colorBitmap; sample.color.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_RGB32, out colorImageData); colorBitmap = colorImageData.ToBitmap(0, sample.color.info.width, sample.color.info.height); if (faceModule != null) { PXCMFaceData faceData = faceModule.CreateOutput(); faceData.Update(); numberTrackedFace = faceData.QueryNumberOfDetectedFaces(); PXCMFaceData.Face faceDataFace = faceData.QueryFaceByIndex(0); if (faceDataFace != null) { PXCMFaceData.DetectionData faceDetectionData = faceDataFace.QueryDetection(); PXCMFaceData.LandmarksData landMarksData = faceDataFace.QueryLandmarks(); if (faceDetectionData != null) //Запись переменных нахождения фейса { PXCMRectI32 faceRectangle; faceDetectionData.QueryFaceAverageDepth(out faceAverageDepth); faceDetectionData.QueryBoundingRect(out faceRectangle); faceRectangleHeight = faceRectangle.h; faceRectangleWidth = faceRectangle.w; faceRectangleX = faceRectangle.x; faceRectangleY = faceRectangle.y; PXCMFaceData.LandmarkPoint[] points; //Херачим точки на фейс if (landMarksData != null) { bool res = landMarksData.QueryPoints(out points); Graphics graphics = Graphics.FromImage(colorBitmap); Font font = new Font(System.Drawing.FontFamily.GenericMonospace, 12, System.Drawing.FontStyle.Bold); foreach (PXCMFaceData.LandmarkPoint landmark in points) { point.X = landmark.image.x + LandMarkAlingment; point.Y = landmark.image.y + LandMarkAlingment; //Console.WriteLine(point.X); if (landmark.confidenceImage == 0) { graphics.DrawString("X", font, System.Drawing.Brushes.Brown, point); } else { graphics.DrawString("*", font, System.Drawing.Brushes.CornflowerBlue, point); } Connect = Math.Min(landmark.confidenceImage, 1); } } } var connectMessage = new SharpOSC.OscMessage("/expressions/connectMessage", Connect); Sender.Send(connectMessage); PXCMFaceData.PoseData facePoseData = faceDataFace.QueryPose(); //переменные поворота для анимации головы if (facePoseData != null) { PXCMFaceData.PoseEulerAngles headAngles; facePoseData.QueryPoseAngles(out headAngles); headRoll = headAngles.roll; headYaw = headAngles.yaw; headPitch = headAngles.pitch; } PXCMFaceData.ExpressionsData expressionData = faceDataFace.QueryExpressions(); if (expressionData != null) { PXCMFaceData.ExpressionsData.FaceExpressionResult score; expressionData.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_KISS, out score); Dispatcher.Invoke(() => kissExpression.Text = Convert.ToString(score.intensity)); var kissMessage = new SharpOSC.OscMessage("/expressions/kiss", score.intensity); Sender.Send(kissMessage); expressionData.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_MOUTH_OPEN, out score); Dispatcher.Invoke(() => mouthExpression.Text = Convert.ToString(score.intensity)); var mouthMessage = new SharpOSC.OscMessage("/expressions/mouth", score.intensity); Sender.Send(mouthMessage); expressionData.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_SMILE, out score); Dispatcher.Invoke(() => smileExpression.Text = Convert.ToString(score.intensity)); var smileMessage = new SharpOSC.OscMessage("/expressions/smile", score.intensity); Sender.Send(smileMessage); expressionData.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_TONGUE_OUT, out score); Dispatcher.Invoke(() => tongueExpression.Text = Convert.ToString(score.intensity)); var tongueOutMessage = new SharpOSC.OscMessage("/expressions/tongueout", score.intensity); Sender.Send(tongueOutMessage); expressionData.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_BROW_LOWERER_LEFT, out score); Dispatcher.Invoke(() => leftBrowLowExpression.Text = Convert.ToString(score.intensity)); var leftBrowLowMessage = new SharpOSC.OscMessage("/expressions/leftBrowLow", score.intensity); Sender.Send(leftBrowLowMessage); expressionData.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_BROW_LOWERER_RIGHT, out score); Dispatcher.Invoke(() => rightBrowLowExpression.Text = Convert.ToString(score.intensity)); var rightBrowLowMessage = new SharpOSC.OscMessage("/expressions/rightBrowLow", score.intensity); Sender.Send(rightBrowLowMessage); expressionData.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_BROW_RAISER_LEFT, out score); Dispatcher.Invoke(() => leftBrowRaiseExpression.Text = Convert.ToString(score.intensity)); var leftBrowRaiseMessage = new SharpOSC.OscMessage("/expressions/leftBrowRaise", score.intensity); Sender.Send(leftBrowRaiseMessage); expressionData.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_BROW_RAISER_RIGHT, out score); Dispatcher.Invoke(() => rightBrowRaiseExpression.Text = Convert.ToString(score.intensity)); var rightBrowRaiseMessage = new SharpOSC.OscMessage("/expressions/rightBrowRaise", score.intensity); Sender.Send(rightBrowRaiseMessage); expressionData.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_EYES_CLOSED_LEFT, out score); Dispatcher.Invoke(() => leftEyeClosedExpression.Text = Convert.ToString(score.intensity)); var leftEyeClosedMessage = new SharpOSC.OscMessage("/expressions/leftEyeClosed", score.intensity); Sender.Send(leftEyeClosedMessage); expressionData.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_EYES_CLOSED_RIGHT, out score); Dispatcher.Invoke(() => rightEyeClosedExpression.Text = Convert.ToString(score.intensity)); var rightEyeClosedMessage = new SharpOSC.OscMessage("/expressions/rightEyeClosed", score.intensity); Sender.Send(rightEyeClosedMessage); expressionData.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_EYES_TURN_LEFT, out score); Dispatcher.Invoke(() => eyesTurnLeftExpression.Text = Convert.ToString(score.intensity)); var eyesTurnLeftMessage = new SharpOSC.OscMessage("/expressions/eyesTurnLeft", score.intensity); Sender.Send(eyesTurnLeftMessage); expressionData.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_EYES_TURN_RIGHT, out score); Dispatcher.Invoke(() => eyesTurnRightExpression.Text = Convert.ToString(score.intensity)); var eyesTurnRightMessage = new SharpOSC.OscMessage("/expressions/eyesTurnRight", score.intensity); Sender.Send(eyesTurnRightMessage); expressionData.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_EYES_DOWN, out score); Dispatcher.Invoke(() => eyesDownExpression.Text = Convert.ToString(score.intensity)); var eyesDownMessage = new SharpOSC.OscMessage("/expressions/eyesDown", score.intensity); Sender.Send(eyesDownMessage); expressionData.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_EYES_UP, out score); Dispatcher.Invoke(() => eyesUpExpression.Text = Convert.ToString(score.intensity)); var eyesUpMessage = new SharpOSC.OscMessage("/expressions/eyesUp", score.intensity); Sender.Send(eyesUpMessage); expressionData.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_PUFF_LEFT, out score); Dispatcher.Invoke(() => puffLeftExpression.Text = Convert.ToString(score.intensity)); var leftPuffMessage = new SharpOSC.OscMessage("/expressions/leftPuff", score.intensity); Sender.Send(leftPuffMessage); expressionData.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_PUFF_RIGHT, out score); Dispatcher.Invoke(() => puffRightExpression.Text = Convert.ToString(score.intensity)); var rightPuffMessage = new SharpOSC.OscMessage("/expressions/rightPuff", score.intensity); Sender.Send(rightPuffMessage); } } faceData.Dispose(); } UpdateUI(colorBitmap); colorBitmap.Dispose(); sample.color.ReleaseAccess(colorImageData); senseManager.ReleaseFrame(); } }
//顔のフレームの更新処理 private void updateFaceFrame() { // フレームデータを取得する PXCMCapture.Sample sample = senceManager.QuerySample(); UpdateColorImage(sample.color); //SenceManagerモジュールの顔のデータを更新する faceData.Update(); //検出した顔の数を取得する int numFaces = faceData.QueryNumberOfDetectedFaces(); //追加:顔のランドマーク(特徴点)のデータの入れ物を用意 PXCMFaceData.LandmarksData[] landmarkData = new PXCMFaceData.LandmarksData[LANDMARK_MAXFACES]; PXCMFaceData.LandmarkPoint[] landmarkPoints; int numPoints = 0; if (senceManager != null) { //それぞれの顔ごとに情報取得および描画処理を行う for (int i = 0; i < numFaces; ++i) { //顔の情報を取得する PXCMFaceData.Face face = faceData.QueryFaceByIndex(i); // 顔の位置を取得:Depthで取得する var detection = face.QueryDetection(); if (detection != null) { PXCMRectI32 faceRect; detection.QueryBoundingRect(out faceRect); //顔の位置に合わせて長方形を変更 TranslateTransform transform = new TranslateTransform(faceRect.x, faceRect.y); rect[i].Width = faceRect.w; rect[i].Height = faceRect.h; rect[i].Stroke = Brushes.Blue; rect[i].StrokeThickness = 3; rect[i].RenderTransform = transform; //追加:フェイスデータからランドマーク(特徴点群)についての情報を得る landmarkData[i] = face.QueryLandmarks(); if (landmarkData[i] != null) { //ランドマークデータから何個の特徴点が認識できたかを調べる numPoints = landmarkData[i].QueryNumPoints(); //認識できた特徴点の数だけ、特徴点を格納するインスタンスを生成する landmarkPoints = new PXCMFaceData.LandmarkPoint[numPoints]; //ランドマークデータから、特徴点の位置を取得、表示 if (landmarkData[i].QueryPoints(out landmarkPoints)) { for (int j = 0; j < numPoints; j++) { tb[i, j].Text = j.ToString(); tb[i, j].RenderTransform = new TranslateTransform(landmarkPoints[j].image.x, landmarkPoints[j].image.y); } } } } } } }
/// <summary> /// Update is called every frame, if the MonoBehaviour is enabled. /// </summary> void Update() { /* Make sure PXCMSenseManager Instance is Initialized */ if (psm == null) { return; } /* Wait until any frame data is available true(aligned) false(unaligned) */ if (psm.AcquireFrame(true) != pxcmStatus.PXCM_STATUS_NO_ERROR) { return; } /* Retrieve face tracking data if ready */ faceAnalyzer = psm.QueryFace(); if (faceAnalyzer != null) { PXCMFaceData _outputData = faceAnalyzer.CreateOutput(); if (_outputData != null) { _outputData.Update(); for (int i = 0; i < _outputData.QueryNumberOfDetectedFaces(); i++) { PXCMFaceData.Face _iFace = _outputData.QueryFaceByIndex(i); if (_iFace != null) { /* Retrieve 78 Landmark Points */ PXCMFaceData.LandmarksData LandmarkData = _iFace.QueryLandmarks(); if (LandmarkData != null) { PXCMFaceData.LandmarkPoint[] landmarkPoints = new PXCMFaceData.LandmarkPoint[MaxPoints]; if (LandmarkData.QueryPoints(out landmarkPoints)) { faceRenderer.DisplayJoints2D(landmarkPoints); } } /* Retrieve Detection Data */ PXCMFaceData.DetectionData detectionData = _iFace.QueryDetection(); if (detectionData != null) { PXCMRectI32 rect; if (detectionData.QueryBoundingRect(out rect)) { faceRenderer.SetDetectionRect(rect); } } /* Retrieve Pose Data */ PXCMFaceData.PoseData poseData = _iFace.QueryPose(); if (poseData != null) { PXCMFaceData.PoseQuaternion poseQuaternion; if (poseData.QueryPoseQuaternion(out poseQuaternion)) { faceRenderer.DisplayPoseQuaternion(poseQuaternion); } } /* Retrieve Expression Data */ PXCMFaceData.ExpressionsData expressionData = _iFace.QueryExpressions(); if (expressionData != null) { PXCMFaceData.ExpressionsData.FaceExpressionResult expressionResult; if (expressionData.QueryExpression(PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_MOUTH_OPEN, out expressionResult)) { faceRenderer.DisplayExpression(expressionResult, PXCMFaceData.ExpressionsData.FaceExpression.EXPRESSION_MOUTH_OPEN); } } } } /* Retrieve Alert Data */ PXCMFaceData.AlertData _alertData; for (int i = 0; i < _outputData.QueryFiredAlertsNumber(); i++) { if (_outputData.QueryFiredAlertData(i, out _alertData) == pxcmStatus.PXCM_STATUS_NO_ERROR) { faceRenderer.DisplayAlerts(_alertData); } } } _outputData.Dispose(); } /* Retrieve a sample from the camera */ PXCMCapture.Sample sample = psm.QueryFaceSample(); if (sample != null) { colorImage = sample.color; if (colorImage != null) { if (colorTexture2D == null) { /* If not allocated, allocate a Texture2D */ colorTexture2D = new Texture2D(colorImage.info.width, colorImage.info.height, TextureFormat.ARGB32, false); /* Associate the Texture2D with a gameObject */ colorPlane.renderer.material.mainTexture = colorTexture2D; //colorPlane.renderer.material.mainTextureScale = new Vector2(-1f, 1f); } /* Retrieve the image data in Texture2D */ PXCMImage.ImageData colorImageData; colorImage.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_RGB32, out colorImageData); colorImageData.ToTexture2D(0, colorTexture2D); colorImage.ReleaseAccess(colorImageData); /* Apply the texture to the GameObject to display on */ colorTexture2D.Apply(); } } /* Realease the frame to process the next frame */ psm.ReleaseFrame(); }