• 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("
    ")
                TT = line.strip("
    ")
                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("
    ")))
                    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("	")
                    # print(linestrlist)
                    # linelist = [int(i) for i in linestrlist]
                    # print(linelist)
    
                line_nu += 1
  • 相关阅读:
    [转]wireshark 实用过滤表达式(针对ip、协议、端口、长度和内容) 实例介绍
    [转]HTTPS连接的前几毫秒发生了什么
    将生成的数据写入文件
    linux查看网络监听端口
    [转] Linux下移动virtualbox虚拟硬盘丢失eth0
    sockaddr结构体
    以十六进制形式打印字符串
    pta5-9 Huffman Codes (30分)
    pat1068. Find More Coins (30)
    hduoj 2955Robberies
  • 原文地址:https://www.cnblogs.com/Anita9002/p/7940619.html
Copyright © 2020-2023  润新知