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