使用Pthon中的Opencv获取人体关键点并再在unity中展现

为了给同门们做备忘录。细到扣脚没有你打我。细节细节细节(重要的……)

目录

一、环境配置

二、pycharm的操作

三、Unity中的操作

一、环境配置

电脑配置

使用Pthon中的Opencv获取人体关键点并再在unity中展现

1.1Python的下载安装

使用Pthon中的Opencv获取人体关键点并再在unity中展现

1.2Pycharm的下载安装

使用Pthon中的Opencv获取人体关键点并再在unity中展现

1.3Unity Hub的下载

使用Pthon中的Opencv获取人体关键点并再在unity中展现

在Hub里面下载Unity

1.3.1Unity的下载

1.3.2项目创建

1.4VisualStudio的下载

使用Pthon中的Opencv获取人体关键点并再在unity中展现

二、pycharm的操作

1建立项目(补上?)

2配置工具包

Pycharm专业版(professional)本来整的社区版的我也不知道为啥变成了这样。

先来配置环境进入File>Project:userfiles(项目名称)Python Interpreter(这里已经是装了几个工具包的样子了)看看有哪些工具包,一会安装工具包也需要在这里。

使用Pthon中的Opencv获取人体关键点并再在unity中展现

书写代码,这里有个”BUG”能看出来的举手

from cvzone.PoseModule import PoseDetector
import cv2

cap = cv2.VideoCapture("Video.mp4")
detector = PoseDetector()
while True:
    success, img = cap.read()
    img = detector.findPose(img)
    lmList, bboxInfo = detector.findPosition(img)
    cv2.imshow("Image", img)
    cv2.waitkey(1)

调出视频看看成成功不

使用Pthon中的Opencv获取人体关键点并再在unity中展现

但是报错了

使用Pthon中的Opencv获取人体关键点并再在unity中展现

File>Project:userfiles(项目名称)>Python Interpreter>左下角的”+”号>

使用Pthon中的Opencv获取人体关键点并再在unity中展现

点开Manage Repositories更镜像源网站

使用Pthon中的Opencv获取人体关键点并再在unity中展现

使用Pthon中的Opencv获取人体关键点并再在unity中展现
https://pypi.tuna.tsinghua.edu.cn/simple/

返回到这个页面再搜索框里输入想要的安装包名称

使用Pthon中的Opencv获取人体关键点并再在unity中展现

so 我们安装一下google

使用Pthon中的Opencv获取人体关键点并再在unity中展现

还有 protobuf

使用Pthon中的Opencv获取人体关键点并再在unity中展现

但是又有错误了

使用Pthon中的Opencv获取人体关键点并再在unity中展现

所以再安装matplotlib

使用Pthon中的Opencv获取人体关键点并再在unity中展现

So红色我的最爱

使用Pthon中的Opencv获取人体关键点并再在unity中展现

使用Pthon中的Opencv获取人体关键点并再在unity中展现

成功了但是视频一闪而过并提示 ,错误在”waitKey”K要大写,(对这就是那个BUG)

使用Pthon中的Opencv获取人体关键点并再在unity中展现
from cvzone.PoseModule import PoseDetector
import cv2

cap = cv2.VideoCapture("Video.mp4")
detector = PoseDetector()
while True:
    success, img = cap.read()
    img = detector.findPose(img)
    lmList, bboxInfo = detector.findPosition(img)
    cv2.imshow("Image", img)
    cv2.waitKey(1)

这样就成功了~ 模特是你们可爱的琦学长

使用Pthon中的Opencv获取人体关键点并再在unity中展现

这里只调动了CPU,并为启动GPU所以you’dai

使用Pthon中的Opencv获取人体关键点并再在unity中展现

运行

from cvzone.PoseModule import PoseDetector
import cv2

cap = cv2.VideoCapture("Video.mp4")
detector = PoseDetector()
while True:
    success, img = cap.read()
    img = detector.findPose(img)
    lmList, bboxInfo = detector.findPosition(img)

    if bboxInfo:
        lmString = ""
        for lm in lmList:
            print(lm)
    cv2.imshow("Image", img)
    cv2.waitKey(1)

得出

使用Pthon中的Opencv获取人体关键点并再在unity中展现

下一步将数据存在文件中

import cv2
from cvzone.PoseModule import PoseDetector
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:
            lmString += f'{lm[1]},{img.shape[0] - lm[2]},{lm[3]} , '
        posList.append(lmString)
    print(len(posList))
    cv2.imshow("Image",img)
    key = cv2.waitKey(1)
    if key == ord("1"):#按“1”按键这个整了4个小时,w...(简直是天才~)
        with open("data.txt","w") as f:
            f.writelines(['%s\n'% item for item in posList])

这个”1″要在英文输入法中按切记。就这样我们得到了数据啦啦啦啦啦~

使用Pthon中的Opencv获取人体关键点并再在unity中展现

三、Unity中的操作

3.1Unity的配置

申请账号、

3.1建立项目

使用Pthon中的Opencv获取人体关键点并再在unity中展现

点击Create project

建立C#文件

这是导入并执行文件的代码可以显示动画咯

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
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/data.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; y="float.Parse(points[1" z="float.Parse(points[2" body[i].transform.localposition="new" vector3(x, y, z); } counter if (counter="=" lines.count) thread.sleep(30); }< code></=></string>

下一步就是在球球中间连线编辑C#文件

使用Pthon中的Opencv获取人体关键点并再在unity中展现

使用Pthon中的Opencv获取人体关键点并再在unity中展现

使用Pthon中的Opencv获取人体关键点并再在unity中展现

上代码了Script:点的


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
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/202204131410(1).mp4.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; y="float.Parse(points[1" z="float.Parse(points[2" 300; body[i].transform.localposition="new" vector3 (x,y,z); debug.log(body.length); } counter if (counter="=" lines.count) thread.sleep(90); }< code></=32;></string>

线的Script:

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.05f;
        lineRenderer.endWidth = 0.05f;
    }

    // Update is called once per frame
    void Update()
    {
        lineRenderer.SetPosition(0, origin.position);
        lineRenderer.SetPosition(1, destination.position);
    }
}</linerenderer>

环境配置

  1. 安装python、IDE(pycharm)、Unity Hub、Unity、visual studio就不在这做讲解了(有其他的道友。)

得空了再填加。

参考网站:(科学上网)

​​​​​​https://www.youtube.com/watch?v=BtMs0ysTdkM

Original: https://blog.csdn.net/CHENHONGYING93/article/details/123623505
Author: 宏英(홍잉)
Title: 使用Pthon中的Opencv获取人体关键点并再在unity中展现

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/704603/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球