Openpose学习笔记(一) 01_body_from_image.py

01_body_from_image.py 是Openpose官方给出的demo运行文件,这篇随笔仅记载个人学习记录

代码如下:

复制代码
 1 # From Python
 2 # It requires OpenCV installed for Python
 3 import sys
 4 import cv2
 5 import os
 6 from sys import platform
 7 import argparse
 8 
 9 try:
10     # Import Openpose (Windows/Ubuntu/OSX)
11     dir_path = os.path.dirname(os.path.realpath(__file__))
12     try:
13         # Windows Import
14         if platform == "win32":
15             # Change these variables to point to the correct folder (Release/x64 etc.)
16             sys.path.append(dir_path + '/../../python/openpose/Release');
17             os.environ['PATH']  = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' +  dir_path + '/../../bin;'
18             import pyopenpose as op
19         else:
20             # Change these variables to point to the correct folder (Release/x64 etc.)
21             sys.path.append('../../python');
22             # If you run `make install` (default path is `/usr/local/python` for Ubuntu), you can also access the OpenPose/python module from there. This will install OpenPose and the python library at your desired installation path. Ensure that this is in your python path in order to use it.
23             # sys.path.append('/usr/local/python')
24             from openpose import pyopenpose as op
25     except ImportError as e:
26         print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?')
27         raise e
28 
29     # Flags
30     parser = argparse.ArgumentParser()
31     parser.add_argument("--image_path", default="../../../examples/media/COCO_val2014_000000000192.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).")
32     args = parser.parse_known_args()
33 
34     # Custom Params (refer to include/openpose/flags.hpp for more parameters)
35     params = dict()
36     params["model_folder"] = "../../../models/"
37 
38     # Add others in path?
39     for i in range(0, len(args[1])):
40         curr_item = args[1][i]
41         if i != len(args[1])-1: next_item = args[1][i+1]
42         else: next_item = "1"
43         if "--" in curr_item and "--" in next_item:
44             key = curr_item.replace('-','')
45             if key not in params:  params[key] = "1"
46         elif "--" in curr_item and "--" not in next_item:
47             key = curr_item.replace('-','')
48             if key not in params: params[key] = next_item
49 
50     # Construct it from system arguments
51     # op.init_argv(args[1])
52     # oppython = op.OpenposePython()
53 
54     # Starting OpenPose
55     opWrapper = op.WrapperPython()
56     opWrapper.configure(params)
57     opWrapper.start()
58 
59     # Process Image
60     datum = op.Datum()
61     imageToProcess = cv2.imread(args[0].image_path)
62     datum.cvInputData = imageToProcess
63     opWrapper.emplaceAndPop(op.VectorDatum([datum]))
64 
65     # Display Image
66     print("Body keypoints: \n" + str(datum.poseKeypoints))
67     cv2.imshow("OpenPose 1.7.0 - Tutorial Python API", datum.cvOutputData)
68     cv2.waitKey(0)
69 except Exception as e:
70     print(e)
71     sys.exit(-1)
复制代码

 

下面逐行分析

导入模块部分:

# From Python
# It requires OpenCV installed for Python
import sys
import cv2
import os
from sys import platform
import argparse

 

[1]

try:
    # Import Openpose (Windows/Ubuntu/OSX)
    dir_path = os.path.dirname(os.path.realpath(__file__))

1 __file__

  __file__表示显示文件当前的位置, 如果当前文件包含在sys.path里面,那么,__file__返回一个相对路径.如果当前文件不包含在sys.path里面,那么__file__返回一个绝对路径.

2 os.path.dirname(path)

  语法:os.path.dirname(path)
  功能:去掉文件名,返回目录,若print os.path.dirname(file)所在脚本是以绝对路径运行的,则会输出该脚本所在的绝对路径,若以相对路径运行,输出空目录

1
2
3
print(os.path.dirname("E:/Read_File/read_yaml.py"))
#结果:
E:/Read_File

3 os.path.realpath(path)

  功能:返回path的标准路径

4 os.path.dirname(os,path.realname(__file__)):指的是,获得该脚本中__file__所在的绝对路径,__file__为内置属性。 

[2]

复制代码
    try:
        # Windows Import
        if platform == "win32":
            # Change these variables to point to the correct folder (Release/x64 etc.)
            sys.path.append(dir_path + '/../../python/openpose/Release');
            os.environ['PATH']  = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' +  dir_path + '/../../bin;'
            import pyopenpose as op
        else:
            # Change these variables to point to the correct folder (Release/x64 etc.)
            sys.path.append('../../python');
            # If you run `make install` (default path is `/usr/local/python` for Ubuntu), you can also access the OpenPose/python module from there. This will install OpenPose and the python library at your desired installation path. Ensure that this is in your python path in order to use it.
            # sys.path.append('/usr/local/python')
            from openpose import pyopenpose as op
    except ImportError as e:
        print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?')
        raise e
复制代码

1 检查platform是不是Win32 如果是的话就该脚本所在的目录的的对应'/../../python/openpose/Release'目录加入到sys.path中,以便下面使用,然后还要把这个路径加入到环境变量里,再导入 pyopenpose模块。

2 如果不是的话,就把对应的python脚本目录加入到sys.path

3 中间有异常则抛出

[3]

    # Flags
    parser = argparse.ArgumentParser()
    parser.add_argument("--image_path", default="../../../examples/media/COCO_val2014_000000000192.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).")
    args = parser.parse_known_args()

  • argparse是python用于解析命令行参数的标准模块。
  • 我们很多时候,需要用到解析命令行参数的程序,例如在终端窗口输入(深度学习)训练的参数和选项。

  我们常常可以把argparse的使用简化成下面四个步骤

  • import argparse ;首先导入该模块
  • parser = argparse.ArgumentParser();创建一个解析对象
  • parser.add_argument();然后向该对象中添加要关注的命令行参数和选项,每一个add_argument方法对应一个要关注的参数或选项;
  • parser.parse_args();调用parse_args()方法进行解析,解析成功之后即可使用。

2

args = parser.parse_known_args()
  • 参考 https://www.cnblogs.com/wanghui-garcia/p/11267160.html
  • parse_known_args()方法的用处就是有些时候,你的选项配置可能不会在一个函数或包中调用完成。在很多使用,我们可能会需要根据一些输入的选项,比如在深度学习中,我们可能会根据传入的模型设置--model来决定我们调用的是那个模型代码,然后在该模型中还会有一部分的选项设置,那么这时候就会出现一种情况就是运行命令中会传入所有需要设置的选项值,但是有时候仅获取到基本设置时可能要进行一些操作然后才继续导入剩下的参数设置,parse_known_args()方法的作用就是当仅获取到基本设置时,如果运行命令中传入了之后才会获取到的其他配置,不会报错;而是将多出来的部分保存起来,留到后面使用。

 

posted @   戒不掉的尼古丁  阅读(331)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示