1. 程式人生 > >Python人臉識別之——建立csv檔案 create_csv.py 程式碼 Python3.7

Python人臉識別之——建立csv檔案 create_csv.py 程式碼 Python3.7

 這是一個很小的指令碼,可以幫助你從具有類似層次結構的人臉資料庫建立CSV檔案

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]
    BASE_PATH="D:\Downloads\orl databases"
    
    SEPARATOR=";"

    fh = open("at.txt",'w')

    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))
                fh.write(abs_path)
                fh.write(SEPARATOR)
                fh.write(str(label))
                fh.write("\n")      
            label = label + 1
    fh.close()