本文主要是介绍【数据集使用】3D Mask Attack Dataset(3DMAD数据集)的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
【编辑时间】2018.09.16
【数据集使用】3D Mask Attack Dataset(3DMAD数据集)的使用
【数据集链接】http://www.idiap.ch/dataset/3dmad
一、下载数据库并解压
-
文件夹中包含的文件如下:
文件包含:
session1.tar.gz - Videos and annotations from the first session
session1.tar.gz - Videos and annotations from the second session
session1.tar.gz - Videos and annotations from the third session
documentation.tar.gz - README file and a python script (raw_to_AVI.py) to demonstrate how to access the data and visualize it.
【自述文件和python脚本(RAW_to_AVI.py),演示如何访问数据并将其可视化】
-
解压其中session1.tar.gz文件后,里面的文件的格式是.hdf文件,
-
根据说明文档,知道了了该文件包含以下数据:
【* 每个帧由640x480 8位rgb图像和640x480 11位深度图像组成。
*每个帧相对于rgb图像的眼睛位置】
-
通过网上查询后,知道了HDF Explorer可以打开.hdf5格式的文件,打开后如下:
2、raw_to_AVI.py脚本的使用
1、bob库的安装与使用
-
此脚本包含在documentation.tar.gz中
-
里面需要用到bob库,库地址为:https://github.com/idiap/bob,简介为【Bob是一个免费的信号处理和机器学习工具箱,最初是由瑞士idiap研究所的生物识别组开发的。】
安装方法如下:(https://www.idiap.ch/software/bob/docs/bob/docs/stable/bob/doc/install.html)
【注意:Bob在windows上不工作,因此没有Conda软件包可供使用。即使从源代码安装它也不能工作。】
We offer pre-compiled binary installations of Bob using conda for Linux and MacOS 64-bit operating systems.
-
Please install conda (miniconda is preferred) and get familiar with it.
-
Make sure you have an up-to-date conda installation (conda 4.4 and above is needed) with thecorrect configuration by running the commands below:
$ conda update -n base conda
$ conda config --set show_channel_urls True
-
Create an environment for Bob:
$ conda create --name bob_py3 --override-channels \
-c https://www.idiap.ch/software/bob/conda -c defaults \
python=3 bob
$ conda activate bob_py3
$ conda config --env --add channels defaults
$ conda config --env --add channels https://www.idiap.ch/software/bob/conda
-
Install the Bob packages that you need in that environment:
$ conda install bob.io.image bob.bio.base ...
【使用】在代码中共用到了3个bob库的指令:bob.io.HDF5File()、bob.ip.draw_cross、bob.io.VideoWriter,
此部分详见官方文档:https://www.idiap.ch/software/bob/docs/bob/docs/stable/,貌似代码用的是旧版本的bob,可能需要使用新的指令。
2、argparse库的使用
还用到了argparse库, 是python的一个命令行解析包,可以编写可读性非常好的程序其用法如下(参考https://www.cnblogs.com/lovemyspring/p/3214598.html):
设置一个解析器
使用argparse的第一步就是创建一个解析器对象,并告诉它将会有些什么参数。那么当你的程序运行时,该解析器就可以用于处理命令行参数。解析器类是 ArgumentParser 。构造方法接收几个参数来设置用于程序帮助文本的描述信息以及其他全局的行为或设置。
import argparse
parser = argparse.ArgumentParser(description='This is a PyMOTW sample program')
定义参数
argparse是一个全面的参数处理库。参数可以触发不同的动作,动作由 add_argument() 方法的 action 参数指定。 支持的动作包括保存参数(逐个地,或者作为列表的一部分),当解析到某参数时保存一个常量值(包括对布尔开关真/假值的特殊处理),统计某个参数出现的次数,以及调用一个回调函数。
默认的动作是保存参数值。在这种情况下,如果提供一个类型,那么在存储之前会先把该参数值转换成该类型。如果提供 dest 参数,参数值就保存为命令行参数解析时返回的命名空间对象中该 dest数值的一个属性。
解析一个命令行
定义了所有参数之后,你就可以给 parse_args() 传递一组参数字符串来解析命令行。默认情况下,参数是从 sys.argv[1:] 中获取,但你也可以传递自己的参数列表。选项是使用GNU/POSIX语法来处理的,所以在序列中选项和参数值可以混合。
parse_args() 的返回值是一个命名空间,包含传递给命令的参数。该对象将参数保存其属性,因此如果你的参数 dest 是 "myoption",那么你就可以用args.myoption 来访问该值。
简单示例
以下简单示例带有3个不同的选项:一个布尔选项(-a),一个简单的字符串选项(-b),以及一个整数选项(-c)。
import argparse
parser = argparse.ArgumentParser(description='Short sample app')
parser.add_argument('-a', action="store_true", default=False)
parser.add_argument('-b', action="store", dest="b")
parser.add_argument('-c', action="store", dest="c", type=int)
print(parser.parse_args(['-a', '-bval', '-c', '3']))
【运行结果:】Namespace(a=True, b='val', c=3)
参数动作
argparse内置6种动作可以在解析到一个参数时进行触发:
store 保存参数值,可能会先将参数值转换成另一个数据类型。若没有显式指定动作,则默认为该动作。
store_const 保存一个被定义为参数规格一部分的值,而不是一个来自参数解析而来的值。这通常用于实现非布尔值的命令行标记。
store_ture/store_false 保存相应的布尔值。这两个动作被用于实现布尔开关。
append 将值保存到一个列表中。若参数重复出现,则保存多个值。
append_const 将一个定义在参数规格中的值保存到一个列表中。
version 打印关于程序的版本信息,然后退出。
【例子】:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-s', action='store', dest='simple_value',
help='Store a simple value')
parser.add_argument('-c', action='store_const', dest='constant_value',
const='value-to-store',
help='Store a constant value')
parser.add_argument('-t', action='store_true', default=False,
dest='boolean_switch',
help='Set a switch to true')
parser.add_argument('-f', action='store_false', default=False,
dest='boolean_switch',
help='Set a switch to false')
parser.add_argument('-a', action='append', dest='collection',
default=[],
help='Add repeated values to a list')
parser.add_argument('-A', action='append_const', dest='const_collection',
const='value-1-to-append',
default=[],
help='Add different values to list')
parser.add_argument('-B', action='append_const', dest='const_collection',
const='value-2-to-append',
help='Add different values to list')
parser.add_argument('--version', action='version', version='%(prog)s 1.0')
results = parser.parse_args()
print 'simple_value =', results.simple_value
print 'constant_value =', results.constant_value
print 'boolean_switch =', results.boolean_switch
print 'collection =', results.collection
print 'const_collection =', results.const_collection
【运行结果】
-
$ python argparse_action.py -h
usage: argparse_action.py [-h] [-s SIMPLE_VALUE] [-c] [-t] [-f]
[-a COLLECTION] [-A] [-B] [--version]
optional arguments:
-h, --help show this help message and exit
-s SIMPLE_VALUE Store a simple value
-c Store a constant value
-t Set a switch to true
-f Set a switch to false
-a COLLECTION Add repeated values to a list
-A Add different values to list
-B Add different values to list
--version show program's version number and exit
-
$ python argparse_action.py -s value
simple_value = value
constant_value = None
boolean_switch = False
collection = []
const_collection = []
-
$ python argparse_action.py -c
simple_value = None
constant_value = value-to-store
boolean_switch = False
collection = []
const_collection = []
-
$ python argparse_action.py -t
simple_value = None
constant_value = None
boolean_switch = True
collection = []
const_collection = []
-
$ python argparse_action.py -f
simple_value = None
constant_value = None
boolean_switch = False
collection = []
const_collection = []
-
$ python argparse_action.py -a one -a two -a three
simple_value = None
constant_value = None
boolean_switch = False
collection = ['one', 'two', 'three']
const_collection = []
-
$ python argparse_action.py -B -A
simple_value = None
constant_value = None
boolean_switch = False
collection = []
const_collection = ['value-2-to-append', 'value-1-to-append']
-
$ python argparse_action.py --version
argparse_action.py 1.0
高级参数处理
至今为止的示例展示了简单的布尔标识、字符串或数字参数选项、以及位置参数。对于变长参数列表、枚举类型数据、以及常量,argparse支持复杂的参数规格。
【可变形参列表】
你可以配置单个参数的定义使其能够匹配所解析的命令行的多个参数。根据需要或期望的参数个数,设置nargs为这些标识值之一:
值 含义
N 参数的绝对个数(例如:3)
? 0或1个参数
* 0或所有参数
+ 所有,并且至少一个参数
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--three', nargs=3)
parser.add_argument('--optional', nargs='?')
parser.add_argument('--all', nargs='*', dest='all')
parser.add_argument('--one-or-more', nargs='+')
print parser.parse_args()
【参数类型】
argparse将所有参数值都看作是字符串,除非你告诉它将字符串转换成另一种数据类型。add_argument()的type参数以一个转换函数作为值,被ArgumentParser用来将参数值从一个字符串转换成另一种数据类型。
【文件参数】
虽然文件对象可以单个字符串参数值来实例化,但并不允许你指定访问模式。FileType让你能够更加灵活地指定某个参数应该是个文件,包括其访问模式和缓冲区大小。
上例中与参数名关联的值是一个打开文件句柄。在使用完该文件后应自己负责关闭该文件。
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-i', metavar='in-file', type=argparse.FileType('rt'))
parser.add_argument('-o', metavar='out-file', type=argparse.FileType('wt'))
try:
results = parser.parse_args()
print 'Input file:', results.i
print 'Output file:', results.oexcept IOError, msg:
parser.error(str(msg))
【运行结果】
$ python argparse_FileType.py -h
usage: argparse_FileType.py [-h] [-i in-file] [-o out-file]
optional arguments:
-h, --help show this help message and exit
-i in-file
-o out-file
【下面的代码中没用到,可以暂时跳过】
参数来源
目前为止所见的例子中,提供给解析器的参数列表来自于显式传递的一个列表,或隐式地从sys.argv获取的。显式传递列表在你使用argparse来处理类命令行但并不是来自命令行(比如来自一个配置文件)的指令之时比较有用。
import argparse
from ConfigParser import ConfigParser
import shlex
parser = argparse./span>ArgumentParser(description='Short sample app')
parser.add_argument('-a', action="store_true", default=False)
parser.add_argument('-b', action="store", dest="b")
parser.add_argument('-c', action="store", dest="c", type=int)
config = ConfigParser()
config.read('argparse_witH_shlex.ini')
config_value = config.get('cli', 'options')
print 'Config: ', config_value
argument_list = shlex.split(config_value)
print 'Arg List:', argument_list
print 'Results:', parser.parse_args(argument_list)
shlex使得切分存储在配置文件中的字符串非常容易。
$ python argparse_with_shlex.py
Config: -a -b 2
Arg List: ['-a', '-b', '2']
Results: Namespace(a=True, b='2', c=None)
另一种自己处理配置文件的方法是使用fromfile_prefix_chars指定一个包含一组要待处理参数的输入文件来告诉argparse怎样识别参数。
import argparse
from ConfigParser import ConfigParser
import shlex
parser = argparse.ArgumentParser(description='Short sample app',
fromfile_prefix_chars='@'
)
parser.add_argument('-a', action="store_true", default=False)
parser.add_argument('-b', action="store", dest="b")
parser.add_argument('-c', action="store", dest="c", type=int)
print parser.parse_args(['@argparse_fromfile_prefix_chars.txt'])
该示例代码在找到一个以@为前缀的参数时即停止往下读取,然后从以该参数命名的文件中查找更多的参数。例如,输入文件argparse_fromfile_prefix_chars.txt包含一系列参数,一行一个:
-a
-b
2
那么处理该文件产生的输出为:
$ python argparse_fromfile_prefix_chars.py
Namespace(a=True, b='2', c=None)
3、os库的使用
os.path.split():按照路径将文件名和路径分割开
os.path.split('PATH')
1.PATH指一个文件的全路径作为参数:
2.如果给出的是一个目录和文件名,则输出路径和文件名
3.如果给出的是一个目录名,则输出路径和为空文件名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import os #os.path.split()返回文件的路径和文件名 fname,fename=os.path.split("E:/lpthw/zedshaw/ex19.py")
print(f""" os.path.split()返回文件的路径和文件名 {fname} {fename} """) print()
#os.path.splitext()将文件名和扩展名分开 fname,fename=os.path.splitext('/home/ubuntu/python_coding/split_func/split_function.py') print(f""" os.path.splitext()将文件名和扩展名分开 {fname} {fename} """) |
运行结果如下:
这篇关于【数据集使用】3D Mask Attack Dataset(3DMAD数据集)的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!