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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值