【无标题】

准备

1.下载pycharm 和unity
2.在pycharm中创建虚拟环境并且下载好mediapipe,cvzone,opencv几个包
3.我会提供本项目的几个文件,有人要评论留下联系方式
4.相应B站视频链接:待更新

游戏效果

获取摄像头

import cv2
width,height=1280,720
cap=cv2.VideoCapture(0)
cap.set(3,width)
cap.set(4,height)
while True:
   _,img=cap.read()
   img=cv2.resize(img,(0,0),None,0.5,0.5)
   cv2.imshow("Image",img)
   cv2.waitKey(1)

手势识别

//添加在while循环之外
from cvzone.HandTrackingModule import HandDetector
detector=HandDetector(maxhands=2,detectionCpn=0.8)

//添加在while循环之内
hands,img=detector.findHands(img)

准备数据

//添加在while循环之内
data=[]
if hands:
   hand=hands[0] #获取检测到的第一只手
   lmlist=hand['lmList'] #获取手的landmark表单
   print(lmlist) #Landmark值-(x,y,z)*21
   for lm in lmlist:
      data.extend(lm[0],height-lm[1],lm[2])
   print(data)

发送数据给unity

//添加在while循环外
import socket
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
serverAddressPort=("127.0.0.1",5052)

//添加在while循环内
sock.sendto(str.encode(str(data)),serverAddressPort)

unity接收数据

1.create empty object Manager in which we attach a script, so whenever you have a script you wanna run when the game starts, you need to attach it to an object. So Manager is what we attach all scripts to.
2.in the Assets create a C# script named UDPReceive.cs which i will provide to you.
3.click and drag the script to Manager
4.run the pycharm and unity

在unity中画一只手

1.create an empty object hand, int the hand we create a 3d object sphere and call it point, change the scale of hand x,y,z from 1 to 0.2
2.in the Assets create a Material green and change its color to green and a Material red. We wanna make the point red and line green
3.drag the material red to point and create another 20 same points
4.create 21 lines and make it green.

在unity中创建handTracking script

public UDPReceive udpReceive;
//1.drag the handtracking srcipt to manager
//2.drag the manager to udp receiver which makes data received from udp script sent to handtracking script because manager has been attached to udpreceive script
public GameObject[] handPoints;
//at this time in the inspector you will find a new variable called hand Points
//lock the inpector and drag the 21 points in the SampleScene to the inspector
//in the handtracking script we have data and points, now we link them.
void Update()
{
   string data =udpReceive.data;
   print(data);
   //remove the useless brackets
   data=data.Remove(0,1);
   data=data.Remove(data.length-1,1)
   print(data);
   //split the string and convert data into float
   string[] points=data.split(',');
   print(points[0]);
   for(int i=0;i<21;i++)
   {
   //in the unity position using float numbers
   //and the number in the unity is much smaller than that in pycharm
    float x=5-float.Parse(points[i*3])/100;
    float y=float.Parse(points[i*3+1])/100;
    float z=float.Parse(points[i*3+2])/100;
    //click the point in the left you will see it has a property called transform,by accessing the transform we can change position
    handPoints[i].transform.localPosition=new Vector3(x,y,z);
   }
}

在unity创建lineCode script

LineRenderer lineRenderer;
public Transform origin;//treat transform as location
public Transform destination;
void Start()
{
   lineRenderer =GetComponent<LineRenderer>();
   lineRenderer.startWidth=0.1f;
   lineRenderer.endWidth=0.1f;
}
void Update()
{
   lineRenderer.SetPosition(0,origin.position);
   lineRenderer.SetPosition(1,destination.position);
}
//drag script to each object line
//drag starting point and ending point to each line in the script

创建环境

1.create a 3d object cube and squeeze it to the floor and make it red
2.create tower made of several red cubes

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值