许可优化
产品
解决方案
服务支持
关于
软件库
当前位置:服务支持 >  软件文章 >  Unity 3D 手部追踪

Unity 3D 手部追踪

阅读数 12
点赞 0
article_banner

一、前言

Unity是一款强大的跨平台游戏开发引擎,由Unity Technologies开发和维护。它提供了一个综合的开发环境,可以用于创建2D和3D游戏,以及其他交互式内容,如虚拟现实和增强现实应用程序。大学时期的室友Unity玩得很好,开发出了很多有趣的高质量游戏。这里,本人第一次使用Unity,来实现手部的3D检测追踪。

二、实战

1.OpenCV追踪手部

代码:

  1. import cv2
  2. from cvzone.HandTrackingModule import HandDetector
  3. import socket
  4. width, height = 1280, 720
  5. cap = cv2.VideoCapture(0)
  6. cap.set(3, width)
  7. cap.set(4, height)
  8. # Hand Detector
  9. detector = HandDetector(maxHands=1, detectionCon=0.8)
  10. # communication
  11. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  12. serverAddressPort = ("127.0.0.1", 5052)
  13. while True:
  14. success, img = cap.read()
  15. # hands
  16. hands, img = detector.findHands(img)
  17. data = []
  18. # Landmark values -(x,y,z)*21
  19. if hands:
  20. # get the first hand detected
  21. hand = hands[0]
  22. # get the landmark list
  23. lmlist = hand['lmList']
  24. # print(lmlist)
  25. for lm in lmlist:
  26. data.extend([lm[0], height - lm[1], lm[2]])
  27. print(data)
  28. sock.sendto(str.encode(str(data)), serverAddressPort)
  29. img = cv2.resize(img, (0, 0), None, 0.5, 0.5)
  30. cv2.imshow("Image", img)
  31. cv2.waitKey(1)

效果:

2.Unity搭建

(1)根据手部的21个特征点搭建模型

 通过21个点和21条线,构造手部模型,过程较复杂

如下:

(2)Unity代码

在模型构建中,使用到了UDPReceive.cs、HandTracking.cs和LineCode.cs三段代码,分别用于实现信息传输、手部数据追踪和点线相连。

1.UDPReceive.cs

  1. using UnityEngine;
  2. using System;
  3. using System.Text;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Threading;
  7. public class UDPReceive : MonoBehaviour
  8. {
  9. Thread receiveThread;
  10. UdpClient client;
  11. public int port = 5052;
  12. public bool startRecieving = true;
  13. public bool printToConsole = false;
  14. public string data;
  15. public void Start()
  16. {
  17. receiveThread = new Thread(
  18. new ThreadStart(ReceiveData));
  19. receiveThread.IsBackground = true;
  20. receiveThread.Start();
  21. }
  22. // receive thread
  23. private void ReceiveData()
  24. {
  25. client = new UdpClient(port);
  26. while (startRecieving)
  27. {
  28. try
  29. {
  30. IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
  31. byte[] dataByte = client.Receive(ref anyIP);
  32. data = Encoding.UTF8.GetString(dataByte);
  33. if (printToConsole) { print(data); }
  34. }
  35. catch (Exception err)
  36. {
  37. print(err.ToString());
  38. }
  39. }
  40. }
  41. }
cs
运行

2.HandTracking.cs

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class HandTracking : MonoBehaviour
  5. {
  6. // Start is called before the first frame update
  7. public UDPReceive udpReceive;
  8. public GameObject[] handPoints;
  9. void Start()
  10. {
  11. }
  12. // Update is called once per frame
  13. void Update()
  14. {
  15. string data = udpReceive.data;
  16. data = data.Remove(0, 1);
  17. data = data.Remove(data.Length-1, 1);
  18. print(data);
  19. string[] points = data.Split(',');
  20. print(points[0]);
  21. //0 1*3 2*3
  22. //x1,y1,z1,x2,y2,z2,x3,y3,z3
  23. for ( int i = 0; i<21; i++)
  24. {
  25. float x = 7-float.Parse(points[i * 3])/100;
  26. float y = float.Parse(points[i * 3 + 1]) / 100;
  27. float z = float.Parse(points[i * 3 + 2]) / 100;
  28. handPoints[i].transform.localPosition = new Vector3(x, y, z);
  29. }
  30. }
  31. }
cs
运行

 3.LineCode.cs

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class LineCode : MonoBehaviour
  5. {
  6. LineRenderer lineRenderer;
  7. public Transform origin;
  8. public Transform destination;
  9. // Start is called before the first frame update
  10. void Start()
  11. {
  12. lineRenderer = GetComponent<LineRenderer>();
  13. lineRenderer.startWidth = 0.1f;
  14. lineRenderer.endWidth = 0.1f;
  15. }
  16. // Update is called once per frame
  17. void Update()
  18. {
  19. lineRenderer.SetPosition(0, origin.position);
  20. lineRenderer.SetPosition(1, destination.position);
  21. }
  22. }
cs
运行

(3)3D 手部追踪效果

还行

三、最后

第一次使用Unity,不熟悉模型的构建,花费了很多时间,但最终实现了效果,还是很开心的。Unity在游戏开发和交互式领域上有着广泛的应用前景,值得我们学习和探索。


免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删
相关文章
QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空