Command line parameters
import sys import cv2 def show_pic(img): cv2.imshow('image',img) cv2.waitKey(0) cv2.destroyAllWindows() def main(): print("start") print (sys.argv) image_1 = sys.argv[1] image_2 = sys.argv[2] src = cv2.imread(image_1) show_pic(src) print("end") if __name__ == "__main__": main()
Operation mode
Note: learn more about the command line parameters and the argparse module. tensortflow also has built-in API for command line parameters,
Many in-depth learning codes cannot be run directly and need to set parameters, paths, etc. If you can't write the interface, you mostly need to use command-line parameters.
Directory operation
import sys import os def root_ope(): current_file = __file__ #Get the full file name of the executed Python, D: \ server \ root py 1 print (current_file,"1") base_name = os.path.basename(current_file) #Get the file name, excluding the directory, root py 2 print (base_name,"2") current_root = os.path.dirname(__file__) #Get the parent directory of the current file, D:\server 3 print (current_root,"3") current_up = os.path.dirname(current_root) #Get the parent directory of the current file, D:\ 4 print (current_up,"4") file_list = os.listdir(current_root) #Get all files and directories in this directory, and return the list, ['a.png ','a1. PNG',....] five print (file_list,"5") file_root1 = os.path.join(current_root,file_list[0]) #Splicing directory, D:\server\a.png 6 file_root2 = current_root+"\\"+file_list[0] #Splicing directory, D:\server\a.png 7 print (file_root1,"6") print (file_root2,"7") replace_file_root1 = file_root1 .replace("\\","/") #Replace character, D:/server/a.png 8 print (replace_file_root1,"8") split_file_root1 = file_root1.split("\\") #Separate strings according to characters, ['d: ','server','a.png '] 9 print (split_file_root1,"9") split_file_root2 = file_root1.split(".") #Separate strings according to characters, ['d: \ \ server \ \ a ','png'] 10 print (split_file_root2,"10") child_name_ext = os.path.splitext(file_root1) #Get the file suffix, ('d: \ \ server \ \ a ','. PNG ') 11 print (child_name_ext,"11") def getListFiles(path): """ Traverse all files in the directory, including all levels of directories, os.walk Similar to an iterator. """ print(type(os.walk(path))) ret = [] for root, dirs, files in os.walk(path): #print (root,dirs) for filespath in files: #print(os.path.join(root,filespath)) ret.append(os.path.join(root,filespath)) return ret def main(): root_ope() file = getListFiles("D:/server/pic_xiangsi") print ("file_counter:",len(file)) if __name__ == "__main__": main()
os.walk
Run for (root, dirs, files) in OS walk(‘a’):
#When running for the first time, the current traversal directory is a
So root = = 'a'
dirs == [ 'b', 'c', 'd']
files == [ '4.txt', '5.txt']
#Then traverse each directory in dirs
b: root = 'a\b'
dirs = []
files = [ '1.txt', '2.txt']
#dirs is null, return
#Traversal c
c: root = 'a\c'
dirs = []
files = [ '3.txt' ]
#Traversal d
d: root = 'a\b'
dirs = []
files = []
After traversal, exit the loop
Note: files need to be processed during deep learning, image processing, target detection, neural network and other operations. It is required to master the basic directory
Document processing and basic ability of documents.
File operation
import sys import cv2 import os import csv import json import xml import random import pandas as pd import xml.etree.ElementTree as ET from xml.dom import minidom from xml.dom.minidom import parse """ In addition, file reading and writing can also be written in this form, which is more concise and convenient, and will automatically close the file. with open("text.txt","w") as f: f.write("data") with open("text.txt","r") as f: data = f.read() data = f.readlines() json.dumps(test_dict) #Convert data to string ## type print (type(120)) ## help print (help(json)) ## dir print (dir(json)) """ """ 0001.xml <annotation verified="no"> <folder>pic</folder> <filename>0001</filename> <path>E:\Download\object_detection_training\object_detection\dataset\pic\0001.jpg</path> <source> <database>Unknown</database> </source> <size> <width>356</width> <height>240</height> <depth>3</depth> </size> <segmented>0</segmented> <object> <type>bndbox</type> <name>tortoise</name> <pose>Unspecified</pose> <truncated>0</truncated> <difficult>0</difficult> <bndbox> <xmin>38</xmin> <ymin>27</ymin> <xmax>329</xmax> <ymax>234</ymax> </bndbox> </object> </annotation> """ def file_read(filename): f = open(filename,"r") data = f.read() print (data) f.close() return data def file_write(filename,data): f = open(filename,"w") f.write(data) f.close() def list_line_write(filename,list): f = open(filename,"w") for i,data in enumerate(list): f.write(str(data)+"\n") f.close() def list_line_read(filename): f = open(filename,"r") list = f.readlines() for i,data in enumerate(list): print (i,data) f.close() return list def json_read(filename): with open(filename,"r") as f: data = json.load(f) return data def json_write(filename,data): with open(filename,"w") as f: json.dump(data,f,indent = 4) def csv_read(filename): all_list = [] with open(filename) as f: f_csv = csv.reader(f) for i,row in enumerate(f_csv): all_list.append(row) return all_list def csv_write(filename,data): column_name = ['1', '2', '3', '4', '5', '6', '7', '8','9','10','11', '12', '13', '14', '1', '16', '17', '18','19','20'] data_pd = pd.DataFrame(data, columns=column_name) data_pd.to_csv(filename, index=None) def xml_read(filename): xml_list = [] tree = ET.parse(filename) root = tree.getroot() for member in root.findall('object'): value = ( root.find('filename').text, int(root.find('size').find("width").text), int(root.find('size').find("height").text), member.find("name").text, int(member.find("bndbox")[0].text), int(member.find("bndbox")[1].text), int(member.find("bndbox")[2].text), int(member.find("bndbox")[3].text) ) xml_list.append(value) return xml_list def xml_write(filename,data): #xml_obj = minidom.getDOMImplementation() #doc = xml_obj.createDocument(None, None, None) doc = minidom.Document() xmemlElement = doc.createElement('annotation') doc.appendChild(xmemlElement) filename_element = doc.createElement('filename') filename_text = doc.createTextNode(data[0][0]) filename_element.appendChild(filename_text) xmemlElement.appendChild(filename_element) size_element = doc.createElement('size') size_element_width = doc.createElement("width") size_element_height = doc.createElement("height") size_element_width_text = doc.createTextNode(str(data[0][2])) size_element_height_text = doc.createTextNode(str(data[0][1])) size_element_width.appendChild(size_element_width_text) size_element_height.appendChild(size_element_height_text) size_element.appendChild(size_element_width) size_element.appendChild(size_element_height) xmemlElement.appendChild(size_element) object_element = doc.createElement('object') object_element_name = doc.createElement("name") object_element_name_text = doc.createTextNode(data[0][3]) object_element_bndbox = doc.createElement("bndbox") object_element_bndbox_xmin = doc.createElement("xmin") object_element_bndbox_ymin = doc.createElement("ymin") object_element_bndbox_xmax = doc.createElement("xmax") object_element_bndbox_ymax = doc.createElement("ymax") object_element_bndbox.appendChild(object_element_bndbox_xmin) object_element_bndbox.appendChild(object_element_bndbox_ymin) object_element_bndbox.appendChild(object_element_bndbox_xmax) object_element_bndbox.appendChild(object_element_bndbox_ymax) object_element_bndbox_xmin_text = doc.createTextNode(str(data[0][4])) object_element_bndbox_ymin_text = doc.createTextNode(str(data[0][5])) object_element_bndbox_xmax_text = doc.createTextNode(str(data[0][6])) object_element_bndbox_ymax_text = doc.createTextNode(str(data[0][7])) object_element_bndbox_xmin.appendChild(object_element_bndbox_xmin_text) object_element_bndbox_ymin.appendChild(object_element_bndbox_ymin_text) object_element_bndbox_xmax.appendChild(object_element_bndbox_xmax_text) object_element_bndbox_ymax.appendChild(object_element_bndbox_ymax_text) object_element_name.appendChild(object_element_name_text) object_element.appendChild(object_element_name) object_element.appendChild(object_element_bndbox) xmemlElement.appendChild(object_element) with open(filename,"w") as f: doc.writexml(f, indent='\t', addindent='\t', newl='\n', encoding="utf-8") def create_list(): list_all = [] for i in range(10): list = [] for j in range(20): list.append(random.randint(0,20)) list_all.append(list) return list_all def create_dict(): dict_all = {} for i in range(10): list = [] for j in range(20): list.append(random.randint(0,20)) dict_all[str(i)] = list return dict_all def main(): print ("satrt".center(30,"-")) list_data = create_list() #print (list_data) dict_data = create_dict() #print (dict_data) file_write("list.txt",str(list_data)) file_read("list.txt") list_line_write("list_lines.txt",list_data) list_line_read("list_lines.txt") json_write("dict_data.json",dict_data) json_write("list_data.json",list_data) list_data = json_read("list_data.json") #print (list_data) dict_data = json_read("dict_data.json") #print (dict_data) csv_write("list_data.csv",list_data ) list_data = csv_read("list_data.csv") #print (list_data) xml_data = xml_read("0001.xml") print (xml_data) xml_write("new.xml",xml_data) print ("end".center(30,"-")) if __name__ == "__main__": main()
explain:
In depth learning, image processing, target detection, neural network and other operations, files need to be processed, which requires mastering the basic directory
Document processing and basic ability of documents. Contains basic file read-write, list, dictionary, tuple operation.
1. Basic document reading and writing mode and simple data processing
2.json file reading and writing mode and simple data processing
3.xml file reading and writing mode and simple data processing
4.csv file reading and writing mode and simple data processing
Custom module call
root_ look. Py (custom module, do not run it)
import sys import cv2 import os class GetRootFiles(object): def __init__(self): print ("look root file") def look_root(self,path): ret = [] for root, dirs, files in os.walk(path): #print (root,dirs) for filespath in files: #print(os.path.join(root,filespath)) ret.append(os.path.join(root,filespath)) return ret def getListFiles(path): """ Traverse all files in the directory, including all levels of directories, os.walk Similar to an iterator. """ ret = [] for root, dirs, files in os.walk(path): #print (root,dirs) for filespath in files: #print(os.path.join(root,filespath)) ret.append(os.path.join(root,filespath)) return ret def main(): root_ope() file = getListFiles("D:/server/pic_xiangsi") print ("file_counter:",len(file)) if __name__ == "__main__": main()
use_moudle.py (run this file and call the custom module)
import root_look from root_look import getListFiles from root_look import GetRootFiles def main(): #data = root_look.getListFiles("D:/server/pic_xiangsi") #data = getListFiles("D:/server/pic_xiangsi") #data = GetRootFiles().look_root("D:/server/pic_xiangsi") data = root_look.GetRootFiles().look_root("D:/server/pic_xiangsi") for i,li in enumerate(data): print (i,li) main() if __name__ == "__main__": main() """ look root file 0 D:/server/pic_xiangsi\alcremie.png 1 D:/server/pic_xiangsi\altaria.png 2 D:/server/pic_xiangsi\current.json 3 D:/server/pic_xiangsi\dataset_xsd.py 4 D:/server/pic_xiangsi\display_rect_check.py 5 D:/server/pic_xiangsi\get_record_num_file.py 6 D:/server/pic_xiangsi\network.py 7 D:/server/pic_xiangsi\network1.py ... ... """
explain:
Put the two files into the same level directory and call the custom function or class module.
It can also be placed in the path of Python module package.
When there are two modules with the same name, the module of the same level directory shall be called first.
This method can be used when there are different versions of modules.
Sometimes we need to modify the module written by others. We must know the basic principle and the structure of the module package.
Build project engineering module package
Wait for now and update later