用Python自動生成CSV文件

1.進入OpenCV的擴展模塊中的face模塊

D:\opencv\opencv_contrib-3.1.0\modules\face\samples\etc
在這裏插入圖片描述

2.利用Python腳本自動生成“訓練數據集絕對路徑”+“;”+“標籤”

#!/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

3.用Anaconda安裝Python

3.1進入官網下載對應操作系統的安裝包,一直下一步傻瓜式安裝。

https://www.anaconda.com/distribution/#download-section
在這裏插入圖片描述

4.使用用cmd進行運行

圖片

圖片

4.1建立一個py27虛擬環境

4.2使用 conda activate Python27 (激活虛擬環境)

圖片

圖片

使用命令:python create_csv.py “對應數據集文件目錄的絕對路徑”

python create_csv.py D:\pic\facerecog

圖片

5.將cmd窗口的輸出流數據進行復制,新建一個.txt文件進行保存,就是我們的CSV文件;

D:\pic\facerecog\s1/1.pgm;0

D:\pic\facerecog\s1/10.pgm;0

D:\pic\facerecog\s1/2.pgm;0

D:\pic\facerecog\s1/3.pgm;0

D:\pic\facerecog\s1/4.pgm;0

D:\pic\facerecog\s1/5.pgm;0

D:\pic\facerecog\s1/6.pgm;0

D:\pic\facerecog\s1/7.pgm;0

D:\pic\facerecog\s1/8.pgm;0

D:\pic\facerecog\s1/9.pgm;0

D:\pic\facerecog\s10/1.pgm;1

…很多

圖片圖片

紅框內是打錯的直接跳過

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章