1.python获取视频人体的姿势,注意 detector.findPosition(img)方法改变了,我按照原先代码中的 lmString += f’{lm[1]},{img.shape[0] - lm[2]},{lm[3]},'会报数组长度上限,我对比一下,原先返回的版本第一个参数是id,因为自己也是新手,所以查了一下代码,
import cv2
from cvzone.PoseModule import PoseDetector
# x1,y1,z1,x2,y2,z2
cap = cv2.VideoCapture('Video.mp4')
detector = PoseDetector()
posList = []
while True:
success, img = cap.read()
img = detector.findPose(img)
lmList, bboxInfo = detector.findPosition(img)
if bboxInfo:
lmString = ''
for lm in lmList:
# print(lm)
# print(len(lm))
lmString += f'{lm[0]},{img.shape[0] - lm[1]},{lm[2]},'
posList.append(lmString)
print(len(posList))
cv2.imshow("Image", img)
key = cv2.waitKey(1)
if key == ord('s'):
with open("AnimationFile.txt", 'w') as f:
f.writelines(["%s\n" % item for item in posList])
2.在Unity3D中,新建33个Sphere
3.新建Line,继承LineCode
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LineCode : MonoBehaviour
{
LineRenderer lineRenderer;
public Transform origin;
public Transform destination;
// Start is called before the first frame update
void Start()
{
lineRenderer = GetComponent<LineRenderer>();
lineRenderer.startWidth = 0.1f;
lineRenderer.endWidth = 0.1f;
}
// Update is called once per frame
void Update()
{
lineRenderer.SetPosition(0, origin.position);
lineRenderer.SetPosition(1, destination.position);
}
}
4、AnimationCode挂在body上
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using System.Threading;
public class AnimationCode : MonoBehaviour
{
public GameObject[] Body;
List<string> lines;
int counter = 0;
// Start is called before the first frame update
void Start()
{
lines = System.IO.File.ReadLines("Assets/AnimationFile.txt").ToList();
}
// Update is called once per frame
void Update()
{
string[] points = lines[counter].Split(',');
for (int i = 0; i <= 32; i++)
{
float x = float.Parse(points[0 + (i * 3)]) / 100;
float y = float.Parse(points[1 + (i * 3)]) / 100;
float z = float.Parse(points[2 + (i * 3)]) / 100;
Body[i].transform.localPosition = new Vector3(x, y, z);
}
counter++;
if (counter == lines.Count) { counter = 0; }
Thread.Sleep(30);
}
}
教程视频:添加链接描述