将一个文件夹下面的图像和对应的pts读进来,然后把pts文件里面的数据读入一个数组,然后画到图像上:
# -*- coding:utf- -*- """ 测试数据样例 """
import os
import cv2 list_dir = [
"/Users/anitafang/Downloads/Datasets/jiaozheng/2017sdm/SDM/001/",
"/Users/anitafang/Downloads/Datasets/jiaozheng/2017sdm/SDM/002/",
"/Users/anitafang/Downloads/Datasets/jiaozheng/2017sdm/SDM/003/",
"/Users/anitafang/Downloads/Datasets/jiaozheng/2017sdm/SDM/004/",
"/Users/anitafang/Downloads/Datasets/jiaozheng/2017sdm/SDM/005/"
] def readLmk(fileName):
landmarks = []
if not os.path.exists(fileName):
return landmarks
else:
fp = open(fileName)
i =
for line in fp.xreadlines():
#print line.strip("\n")
TT = line.strip("\n")
if i> and i<:
# print TT
TT_temp = TT.split(" ")
x = int(float(TT_temp[]))
y = int(float(TT_temp[].strip("\r")))
landmarks.append((x,y))
i +=
return landmarks def main():
for temp in list_dir:
for dirpath,dirnames,filenames in os.walk(temp):
for file in filenames:
if file.split('.')[] not in ['jpg','png']:
continue
print file
fullpath = os.path.join(temp,file)
ptsFile = temp + ""+file.split('.')[] + ".pts" # 读取图片
image = cv2.imread(fullpath)
# 读取对应的pts文件
landmarks = readLmk(ptsFile)
for point in landmarks: cv2.circle(image,point,,(,,)) width = image.shape[]
height = image.shape[]
print (width,height)
if width > :
# 归一化到1000
image = cv2.resize(image,(,height*/width)) cv2.imshow("WW",image)
cv2.waitKey()
input = raw_input('请输入: ')
if input == " ":
print ("copy正确的数据源")
newpath="/Users/anitafang/Downloads/Datasets/jiaozheng/2017sdm/SDM2/001/" cmd = "cp " + fullpath + " " + ptsFile+ " "+ newpath print cmd
os.system(cmd) if __name__ == '__main__':
main()
讲一下Python按行读取文件内容:
filename = "/Users/anitafang/Downloads/Datasets/jiaozheng/annot/sdm/001/annot/"+cnt+".pts" f1 = open(filename)
print(filename)
line_nu =
for line in f1.xreadlines():
if line_nu > and line_nu<:
# print(line)
linestr = line.strip()
# print(linestr)
linestrlist = linestr.split("\t")
# print(linestrlist)
# linelist = [int(i) for i in linestrlist]
# print(linelist) line_nu +=