Chức năng chương trình

Một phần của tài liệu nhận dạng tư thế người với camera kinect và máy học vector hỗ trợ (Trang 29 - 33)

7. Bố cục của quyển luận văn

2.4.2Chức năng chương trình

Chức năng trích xuất dữ liệu sẽ thực hiện theo luồng sự kiện như sau:

Hình 2.15: Luồng sự kiện chương trình “Project K”

 Theo dõi khung xương

Đầu tiên khi có người đứng trước camera Kinect, nhờ vào cảm biến Kinect sẽ theo dõi khung xương của người đó. Có thể bắt được sự kiện này dựa theo hàm bên dưới.

Theo dõi

khung xương Vẽ khung xương

Xử lý và trích xuất dữ liệu khung xương

Lưu hình ảnh và

dữ liệu tương ứng Chọn khung hình và lưu dữ liệu vào file

private void kinect_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e) {

using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) {

if (skeletonFrame != null) {

skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength]; skeletonFrame.CopySkeletonDataTo(skeletons);

skeleton = (from s in skeletons

where s.TrackingState==SkeletonTrackingState.Tracked select s).FirstOrDefault();

if (skeleton == null) lblSkeletonState.Text = skeletonTrackingState.NotTracked.ToString(); }

} }

22

 Vẽ khung xương

Tiếp theo đó dữ liệu của khung xương sẽ được truyền vào hàm vẽ khung xương

private void DrawSkeleton(Skeleton skeleton) {

Bitmap bmp = new Bitmap(colorFrame, pbStream.Size); Graphics g = Graphics.FromImage(bmp);

pbStream.Image = bmp;

Pen greenPen = new Pen(Color.Green, 4);

GraphicsPath graphicsPath = new GraphicsPath(); JointCollection joints = skeleton.Joints; Size defaultSize = new Size(4, 4);

//Vẽ phần đầu DrawBone(graphicsPath,joints[JointType.Head],joints[JointType.ShoulderCenter] , defaultSize); //Vẽ tay phải DrawBone(graphicsPath, joints[JointType.ShoulderCenter], joints[JointType.ShoulderRight], defaultSize); DrawBone(graphicsPath, joints[JointType.ShoulderRight], joints[JointType.ElbowRight], defaultSize); DrawBone(graphicsPath, joints[JointType.ElbowRight], joints[JointType.WristRight], defaultSize); DrawBone(graphicsPath, joints[JointType.WristRight], joints[JointType.HandRight], defaultSize); //Vẽ tay trái DrawBone(graphicsPath, joints[JointType.ShoulderCenter], joints[JointType.ShoulderLeft], defaultSize); DrawBone(graphicsPath, joints[JointType.ShoulderLeft], joints[JointType.ElbowLeft], defaultSize); DrawBone(graphicsPath, joints[JointType.ElbowLeft], joints[JointType.WristLeft], defaultSize); DrawBone(graphicsPath, joints[JointType.WristLeft], joints[JointType.HandLeft], defaultSize);

//Tiếp tục thực hiện các hàm trên 20 khớp xương để vẽ toàn bộ khung xương … g.DrawPath(greenPen, graphicsPath);

greenPen.Dispose(); graphicsPath.Dispose(); }

//Hàm vẽ xương

private void DrawBone(GraphicsPath gp, Joint joint1, Joint joint2, System.Drawing.Size jointSize)

{ (adsbygoogle = window.adsbygoogle || []).push({});

DrawJoint(gp, joint1, jointSize); DrawJoint(gp, joint2, jointSize);

if (joint1.TrackingState != JointTrackingState.NotTracked && joint2.TrackingState != JointTrackingState.NotTracked) { gp.AddLine(JointToPoint(joint1.Position), JointToPoint(joint2.Position)); } }

23

 Lưu hình ảnh và dữ liệu tương ứng

Sau khi khung xương được vẽ ra hình ảnh của khung xương sẽ được lưu lại và gán nhãn tương ứng, mỗi nhãn sẽ có một bộ dữ liệu tương ứng với khung xương trong hình. Từ việc gán nhãn này người dùng có thể dễ dàng lưu lại được dữ liệu tương ứng với khung xương phù hợp với tư thế thu mẫu.

Hàm trên được dùng để trích xuất dữ liệu theo 4 phương pháp đã đề ra.

public static List<double> Get(Skeleton skeleton, DataType dataType) { List<double> result = new List<double>();

switch (dataType) {

case DataType.AbsolutePosition:

foreach (Joint joint in skeleton.Joints) { result.Add(joint.Position.X); result.Add(joint.Position.Y); result.Add(joint.Position.Z); } break; case DataType.RelativePosition:

foreach (Joint joint in skeleton.Joints) { result.Add(joint.Position.X - skeleton.Joints[JointType.Head].Position.X); result.Add(joint.Position.Y - skeleton.Joints[JointType.Head].Position.Y); result.Add(joint.Position.Z - skeleton.Joints[JointType.Head].Position.Z); } break; case DataType.AbsoluteRotation:

foreach (BoneOrientation orientation in skeleton.BoneOrientations) { Vector4 vectorAbsoluteRotation = Nomalize(orientation.AbsoluteRotation.Quaternion); result.Add(vectorAbsoluteRotation.X); result.Add(vectorAbsoluteRotation.Y); result.Add(vectorAbsoluteRotation.Z); result.Add(vectorAbsoluteRotation.W); } break; case DataType.HierarchicalRotation:

foreach (BoneOrientation orientation in skeleton.BoneOrientations) { Vector4 vectorHierarchicalRotation = Nomalize(orientation.HierarchicalRotation.Quaternion); result.Add(vectorHierarchicalRotation.X); result.Add(vectorHierarchicalRotation.Y); result.Add(vectorHierarchicalRotation.Z); result.Add(vectorHierarchicalRotation.W); } break; } return result; }

24

 Chức năng nhận dạng

Ngoài ra, chương trình còn cho phép nhận dạng trực tiếp để kiểm tra kết quả các mô hình sau khi huấn luyện. Kết quả trả về của hàm bên dưới là nhãn được phân lớp theo mô hình sau khi huấn luyện máy học SVM

Bật chức năng nhận dạng bằng cách check vào checkbox “Recognition Online” (1) lúc này người đứng trước Kinect sẽ thực hiện các động tác trong phần 1.2. Hệ thống sẽ tự động nhận dạng khung xương và trả về kết quả thông qua bảng “Recognition Result” (2).

public double Predict(List<List<double>> data) {

svm_problem test = ProblemHelper.ReadProblem(data); return svm.Predict(test.x[0]);

}

1

25

CHƯƠNG 3. KIỂM THỬ VÀ ĐÁNH GIÁ

Một phần của tài liệu nhận dạng tư thế người với camera kinect và máy học vector hỗ trợ (Trang 29 - 33)