目录和目录文件操作
以下示范了Path、路径级连、改换当前目录、取出目录内的文件、打印目录内的文件、将当前目录文件名转换为字符串数组等方法。
import os
from pathlib import Path
from IPython.display import display, Image
pr =r’D:\Setup\Work\Xilinxsetup\ultra96\ILSVRC2012_img_val’
pdir= Path(pr)
#must transfer Path class to string type
print(“pdir=”+str(pdir))
pdir=pdir/‘test’
print(“pdir=”+str(pdir))
name=‘ILSVRC2012_val_00000005.jpeg’
pp=pdir/name
print(“pp=”+str(pp))
#display(Image(str(pp)))
#display(Image(str(pp)))
#print(pp.stat())
print(’\n’)
print(Path.cwd())
print(’\n’)
os.chdir(r’D:\Setup\Work\Xilinxsetup\ultra96\ILSVRC2012_img_val\test’)
#os.chdir(‘D://setup//Work//Xilinxsetup//ultra96//ILSVRC2012_img_val’)
print(os.getcwd())
#display(Image(pp))#pp is window Path, not string, so wrong
##可以这么使用,相当于os.path.join()
#p1 = Path(r’D:\Setup\Work\Xilinxsetup\ultra96’)
#p2 = p1/‘ILSVRC2012_img_val’
#p3= p2/‘ILSVRC2012_val_00000005.jpeg’
##print(p3)
#print(p.stat())
display(Image(str(name)))
#print(os.listdir())
#path=Path.cwd()
#pys = path.glob(’.jpeg’)#
pys = Path.cwd().glob(’.jpeg’)#
#pys = Path.glob(’/’)#pys是经过yield产生的迭代器
for py in pys:
print(py)
#itte=path1.iterdir()
print(’\n’)
for item in pdir.iterdir():
print(item)
#all_image_paths = pdir.glob(’.jpeg’)
#print(all_image_paths)
#先转换成列表
all_image_paths = list(pdir.glob(’.jpeg’))
print(all_image_paths)
#遍历列表,并转换成字符串
print(’\n’)
all_image_paths = [str(i) for i in all_image_paths]
print(all_image_paths)
#排序随机化
import random
random.shuffle(all_image_paths)
image_count = len(all_image_paths)
Python全局变量定义和使用
nm = 0
def nms_boxes(boxes, scores):
“”"Suppress non-maximal boxes.
# Arguments
boxes: ndarray, boxes of objects.
scores: ndarray, scores of objects.
# Returns
keep: ndarray, index of effective boxes.
"""
x1 = boxes[:, 0]
y1 = boxes[:, 1]
x2 = boxes[:, 2]
y2 = boxes[:, 3]
global nm
nm += 1
if nm == 4:
print(f"boxes.shape = {boxes.shape}")
读一个文件的每一行
def get_class(classes_path):
with open(classes_path) as f:
class_names = f.readlines()
class_names = [c.strip() for c in class_names]
return class_names