Python 读取数据

将一个文件夹下面的图像和对应的pts读进来,然后把pts文件里面的数据读入一个数组,然后画到图像上:

 

# -*- coding:utf-8 -*-

"""

测试数据样例

"""
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 = 0
        for line in fp.xreadlines():
            #print line.strip("\n")
            TT = line.strip("\n")
            if i>2 and i<71:
                # print TT
                TT_temp = TT.split(" ")
                x = int(float(TT_temp[0]))
                y = int(float(TT_temp[1].strip("\r")))
                landmarks.append((x,y))
            i += 1
    return landmarks


def main():
    for temp in list_dir:
        for dirpath,dirnames,filenames in os.walk(temp):
            for file in filenames:
                if file.split('.')[1] not in ['jpg','png']:
                    continue
                print file
                fullpath = os.path.join(temp,file)
                ptsFile = temp + "000"+file.split('.')[0] + ".pts"

                # 读取图片
                image = cv2.imread(fullpath)
                # 读取对应的pts文件
                landmarks = readLmk(ptsFile)
                for point in landmarks:

                    cv2.circle(image,point,1,(255,0,0))

                width = image.shape[0]
                height = image.shape[1]
                print (width,height)
                if width > 800:
                    # 归一化到1000
                    image = cv2.resize(image,(800,height*800/width))

                cv2.imshow("WW",image)
                cv2.waitKey(10)
                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 = 1
        for line in f1.xreadlines():
            if line_nu > 51 and line_nu<72:
                # print(line)
                linestr = line.strip()
                # print(linestr)
                linestrlist = linestr.split("\t")
                # print(linestrlist)
                # linelist = [int(i) for i in linestrlist]
                # print(linelist)

            line_nu += 1

 

posted @ 2017-12-01 10:09  Anita-ff  阅读(2828)  评论(0编辑  收藏  举报