public User ValidateUser(User user, Skeleton skel) { if (skel == null) return user; var actualHeight = PointDistance(skel.Joints[JointType.HipCenter].Position, skel.Joints[JointType.Head].Position); var actualWidth = PointDistance(skel.Joints[JointType.ShoulderLeft].Position, skel.Joints[JointType.ShoulderRight].Position); var actualLength = PointDistance(skel.Joints[JointType.ElbowLeft].Position, skel.Joints[JointType.ShoulderLeft].Position); var bestMatch = FindBestMatch(actualHeight, actualWidth, actualLength); if(bestMatch.Name.Equals(user.Name)) { user.Confidence = (user.FaceConfidence/100 + bestMatch.Confidence)/2; user.HipHeight = bestMatch.HipHeight; user.ShoulderWidth = bestMatch.ShoulderWidth; user.ArmLength = bestMatch.ArmLength; }else { user.Confidence = (user.FaceConfidence/100 - bestMatch.Confidence)/2; user.HipHeight = bestMatch.HipHeight; user.ShoulderWidth = bestMatch.ShoulderWidth; user.ArmLength = bestMatch.ArmLength; } return user; }
public void UserDetected(User user) { if (_comInterface != null && user != null) _comInterface.UserFound(user.Name,user.FaceConfidence,user.TrackingID); }
void IKinect.UserLost(User user) { if (_comInterface != null && user != null) _comInterface.UserLost(user.Name); }
private void LoadUsers() { var userData = FileLoader.LoadAllUsers(); if (userData == null) return; foreach (var userInfo in userData) { var user = new User { Name = userInfo.Key, TrackingID = -1, IsActive = false }; if (userInfo.Value != null) foreach (var feature in userInfo.Value) { if (feature.Key.Equals(FeatureType.ArmLength)) { user.ArmLength = float.Parse(feature.Value); } else if (feature.Key.Equals(FeatureType.HipHeadHeight)) { user.HipHeight = float.Parse(feature.Value); } else if (feature.Key.Equals(FeatureType.ShoulderWidth)) { user.ShoulderWidth = float.Parse(feature.Value); } } _users.Add(user); } }
private int FindCurrentUser(bool forceRecognition) { if (_recognitionEngine == null) return -1; if (_skeletons == null) return -1; foreach (var user in _users.Cast<User>().Where(user => user.IsActive)) { foreach(var skel in _skeletons) { if(skel.TrackingId == user.TrackingID && skel.TrackingState == SkeletonTrackingState.Tracked) { return user.TrackingID; } } user.IsActive = false; user.TrackingID = -1; return -1; } foreach(var skel in _skeletons) { if(skel.TrackingId == _activeTID && skel.TrackingState != SkeletonTrackingState.Tracked) { return -1; } } if(_activeTID != -1 && _activeSkeleton != null && !_recognitionEngine.IsRecognizing) { if(_activeUser == null) { _activeUser = new User(); _activeUser.TrackingID = _activeTID; _activeUser.Skeleton = _activeSkeleton; }else if(_activeUser.TrackingID != _activeTID || _activeSkeleton.TrackingId != _activeUser.Skeleton.TrackingId) { _activeUser.TrackingID = _activeTID; _activeUser.Skeleton = _activeSkeleton; _activeUser.Attempts = 0; } if(_activeUser.Attempts >= User.MAX_ATTEMPTS && _activeUser.Attempts <= User.MAX_ATTEMPTS+1) { Log.Info("Gave up on recognition for user with id: " + _activeUser.TrackingID); _activeUser.Attempts++; return -1; } if(!_recSoundPlayed) { _recStart.Play(); _recSoundPlayed = true; } if(_recognitionEngine.Recognize(_activeSkeleton,Coding4Fun.Kinect.WinForm.BitmapExtensions.ToBitmap(_pixelData,640,480))) { if(_activeUser != null) _activeUser.Attempts++; } return _activeTID; }else if(_recognitionEngine.IsRecognizing){ return _activeTID; } return FindClosestSkeleton(); }
public void Train(string name, int skelID) { foreach(var skeleton in _skeletons) { if(skeleton.TrackingId == skelID && skeleton.TrackingState == SkeletonTrackingState.Tracked && skeleton.TrackingId == _activeTID) { var exists = _recognitionEngine.AddUser(name,skeleton); _recognitionEngine.Train(name, skeleton, Coding4Fun.Kinect.WinForm.BitmapExtensions.ToBitmap(_pixelData, 640, 480)); if(!exists) { var u = new User {Name = name, IsActive = false, TrackingID = skeleton.TrackingId}; _users.Add(u); } } } }
public void RecognitionResult(User user) { if (user == null) return; foreach(var skeleton in _skeletons) { if (skeleton.TrackingId != user.TrackingID || skeleton.TrackingState == SkeletonTrackingState.NotTracked) continue; _kinectHandler.UserDetected(user); foreach (User userL in _users) { if (!userL.Name.Equals(user.Name)) continue; userL.TrackingID = user.TrackingID; userL.Confidence = user.Confidence; userL.FaceConfidence = user.FaceConfidence; userL.IsActive = true; userL.ShoulderWidth = user.ShoulderWidth; userL.HipHeight = user.HipHeight; userL.ArmLength = user.ArmLength; _activeSkeleton = skeleton; _recDone.Play(); _recSoundPlayed = false; //zLog.Info("User skeleton found. Starting tracking of id : " + userL.TrackingID); _activeTID = userL.TrackingID; _kinectHandler.StartTracking(userL.TrackingID); _kinectHandler.TrackingStarted(userL.TrackingID); _activeUser = null; } } }
void _recognizer_RecognitionCompletedEvent(System.ComponentModel.RunWorkerCompletedEventArgs e) { IsRecognizing = false; if (e.Cancelled) { _trackingEngine.RecognitionResult(null); return; } var users = e.Result as Dictionary<string, float>; var bestMatch = ""; var bestConf = 50f; foreach (var user in users) { if (user.Value > bestConf) { bestMatch = user.Key; bestConf = user.Value; } } if ("".Equals(bestMatch)) { _trackingEngine.RecognitionResult(null); return; } bestMatch = bestMatch.Replace("@homerKinect.com", ""); //Log.Info(bestMatch); User userF = new User(); userF.Name = bestMatch; userF.FaceConfidence = bestConf; userF.TrackingID = _skelToMatch.TrackingId; userF = _featureDetector.ValidateUser(userF, _skelToMatch); _trackingEngine.RecognitionResult(userF); }
private User FindBestMatch(float actualHeight, float actualWidth, float actualLength) { var bestUser = new User(); var bestConfidence = float.MinValue; foreach (var storedFeatures in _users) { var userLabel = storedFeatures.Key; float sumConfidence = 0; foreach (var featureType in storedFeatures.Value) { switch (featureType.Key) { case FeatureType.HipHeadHeight: { var perDiff = Math.Abs(1 - (actualHeight/float.Parse(featureType.Value))); var confidence = 1 - perDiff; sumConfidence += confidence; } break; case FeatureType.ShoulderWidth: { var perDiff = Math.Abs(1 - (actualWidth/float.Parse(featureType.Value))); var confidence = 1 - perDiff; sumConfidence += confidence; } break; case FeatureType.ArmLength: { var perDiff = Math.Abs(1 - (actualLength/float.Parse(featureType.Value))); var confidence = 1 - perDiff; sumConfidence += confidence; } break; } } sumConfidence = sumConfidence/3; if (bestConfidence >= sumConfidence) continue; bestConfidence = sumConfidence; bestUser.Name = userLabel; bestUser.HipHeight = actualHeight; bestUser.ShoulderWidth = actualWidth; bestUser.ArmLength = actualLength; bestUser.Confidence = bestConfidence; } return bestUser; }