准备
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