Python学习笔记5--人体姿势和Unity3D

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);


    }
}

在这里插入图片描述
教程视频:添加链接描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值