Python creates a CVS file with tag and picture data

Source: Internet
Author: User

In deep learning or machine learning, it is often necessary to organize and classify data, most commonly by writing data paths and labels

to a consolidated txt or CSV file, the training is read.

1 #Coding=utf-82 #!/usr/bin/env python4 ImportSYS5 ImportOs.path6 7 #This was a tiny script to help you creating a CSV file from a face8 #database with a similar hierarchie:9 # One #  . A #|--README - #|--S1 - #  | |--1.PGM the #  | |-- ... - #  | |--10.PGM - #|--S2 - #  | |--1.PGM + #  | |-- ... - #  | |--10.PGM + #  ... A #|--S40 at #  | |--1.PGM - #  | |-- ... - #  | |--10.PGM - # -  - if __name__=="__main__": in  -     ifLen (SYS.ARGV)! = 3: to         Print "usage:create_csv <base_path>" +Sys.exit (1) -  theBase_path=sys.argv[1] *Separator=";" $FH = open (sys.argv[2],'W')Panax Notoginseng  -Label =0 the      forDirName, Dirnames, filenamesinchOs.walk (base_path): +          forSubdirnameinchDirnames: ASubject_path =Os.path.join (dirname, Subdirname) the              forFileNameinchOs.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") theLabel = label + 1 -Fh.close ()

The call method is as follows

  

Python create.py D:/test/jaf d:/a.csv

The resulting file results are as shown

You can adjust the slash to match by replacing

Python creates a CVS file with tag and picture data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.