OpenCV create_csv.py使用方法

本文介绍了如何将OpenCV中的create_csv.py脚本从Python2语法修改为Python3,以便在Python3环境下正常使用。并给出了脚本的使用方法以及生成的at.txt文件内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

OpenCV中create_csv.py脚本是用Python2写的,如果你要在Python3下面使用,你需要把脚本里面的Python2语法修改为Python3。代码如下:

Python2

#!/usr/bin/env python

import sys
import os.path

# This is a tiny script to help you creating a CSV file from a face
# database with a similar hierarchie:
#
#  philipp@mango:~/facerec/data/at$ tree
#  .
#  |-- README
#  |-- s1
#  |   |-- 1.pgm
#  |   |-- ...
#  |   |-- 10.pgm
#  |-- s2
#  |   |-- 1.pgm
#  |   |-- ...
#  |   |-- 10.pgm
#  ...
#  |-- s40
#  |   |-- 1.pgm
#  |   |-- ...
#  |   |-- 10.pgm
#

if __name__ == "__main__":

    if len(sys.argv) != 2:
        print "usage: create_csv <base_path>"
        sys.exit(1)

    BASE_PATH=sys.argv[1]
    SEPARATOR=";"

    label = 0
    for dirname, dirnames, filenames in os.walk(BASE_PATH):
        for subdirname in dirnames:
            subject_path = os.path.join(dirname, subdirname)
            for filename in os.listdir(subject_path):
                abs_path = "%s/%s" % (subject_path, filename)
                print "%s%s%d" % (abs_path, SEPARATOR, label)
            label = label + 1

Python3

#!/usr/bin/env python

import sys
import os.path

# This is a tiny script to help you creating a CSV file from a face
# database with a similar hierarchie:
#
#  philipp@mango:~/facerec/data/at$ tree
#  .
#  |-- README
#  |-- s1
#  |   |-- 1.pgm
#  |   |-- ...
#  |   |-- 10.pgm
#  |-- s2
#  |   |-- 1.pgm
#  |   |-- ...
#  |   |-- 10.pgm
#  ...
#  |-- s40
#  |   |-- 1.pgm
#  |   |-- ...
#  |   |-- 10.pgm
#

if __name__ == "__main__":

    if len(sys.argv) != 2:
        print ("usage: create_csv <base_path>")
        sys.exit(1)

    BASE_PATH=sys.argv[1]
    SEPARATOR=";"

    label = 0
    for dirname, dirnames, filenames in os.walk(BASE_PATH):
        for subdirname in dirnames:
            subject_path = os.path.join(dirname, subdirname)
            for filename in os.listdir(subject_path):
                abs_path = "%s/%s" % (subject_path, filename)
                print ("%s%s%d" % (abs_path, SEPARATOR, label))
            label = label + 1

create_csv.py脚本使用方法如下:

python create_csv.py "G:\opencvImage\att_faces" >at.txt

生成at.txt的内容如下:

G:\opencvImage\att_faces\s1/1.pgm;0
G:\opencvImage\att_faces\s1/10.pgm;0
G:\opencvImage\att_faces\s1/2.pgm;0
G:\opencvImage\att_faces\s1/3.pgm;0
G:\opencvImage\att_faces\s1/4.pgm;0
G:\opencvImage\att_faces\s1/5.pgm;0
G:\opencvImage\att_faces\s1/6.pgm;0
G:\opencvImage\att_faces\s1/7.pgm;0
G:\opencvImage\att_faces\s1/8.pgm;0
G:\opencvImage\att_faces\s1/9.pgm;0
G:\opencvImage\att_faces\s10/1.pgm;1
G:\opencvImage\att_faces\s10/10.pgm;1
G:\opencvImage\att_faces\s10/2.pgm;1
G:\opencvImage\att_faces\s10/3.pgm;1
G:\opencvImage\att_faces\s10/4.pgm;1
G:\opencvImage\att_faces\s10/5.pgm;1
G:\opencvImage\att_faces\s10/6.pgm;1
G:\opencvImage\att_faces\s10/7.pgm;1
G:\opencvImage\att_faces\s10/8.pgm;1
G:\opencvImage\att_faces\s10/9.pgm;1
G:\opencvImage\att_faces\s11/1.pgm;2
G:\opencvImage\att_faces\s11/10.pgm;2
G:\opencvImage\att_faces\s11/2.pgm;2
G:\opencvImage\att_faces\s11/3.pgm;2
G:\opencvImage\att_faces\s11/4.pgm;2
G:\opencvImage\att_faces\s11/5.pgm;2
G:\opencvImage\att_faces\s11/6.pgm;2
G:\opencvImage\att_faces\s11/7.pgm;2
G:\opencvImage\att_faces\s1
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值