当前位置:网站首页>代码保存

代码保存

2020-11-08 23:46:19 WSX_1994

# encoding: utf-8

import requests
import urllib.request
import numpy as np
import os ,stat
import pickle as pkl
import cv2 as cv
import xlrd

def getImg(url, basedir, filename):

    try:
        urllib.request.urlretrieve(url,filename=basedir+"{name}.jpg".format(name = filename))
    except IOError as e:
        print("IOE ERROR")
    except Exception as e:
        print("Exception")


def loadPkl(name):
    if not os.path.exists(name):
        color_feature = {}
        data = open(name, 'wb')
        pkl.dump(color_feature, data)
        data.close()
    data = open(name, 'rb')
    return pkl.load(data)


def savePkl(color_feature, name):
    data = open(name, 'wb')
    pkl.dump(color_feature, data)
    data.close()


def getImgInfoFromCsv(filename):
    data = xlrd.open_workbook(filename)
    try:
        sh = data.sheet_by_name("Sheet1")
    except:
        print("无此文件!!!")
    nrows = sh.nrows  # 获取行数
    ncols = sh.ncols  # 获取列数
    res = {}  # 获取到表格的数据存储到rowlist中
    for i in range(1, nrows):
        img_id = sh.cell_value(i, 0)
        img_url = sh.cell_value(i, 1)
        res[img_id] = img_url
    return res


def getImgInfoFromTxt(filename):
    res = {}
    with open(filename, 'r', encoding='utf-8') as f:
        data = f.readlines()
    for i in data:
        if i.strip().split(" ")[0] not in res:
            res[i.strip().split(" ")[0]] = i.strip().split(" ")[1]
    return res



def getColorFeature(img):

    return 1, 2, 3, 4


# img_info example: {img_id, : "http://ww.png"}
def processColorFeature(img_info, color_feature):

    for i in img_info.keys():
        url = img_info[i]
        file = requests.get(url)
        img = cv.imdecode(np.fromstring(file.content, np.uint8), 1)

        a, b, c, d = getColorFeature(img)
        color_feature[i] = [a, b, c, d]

    return color_feature

def pklToTxt(source, dim):
    data = open(source, 'w')
    feature_info = pkl.load(data)
    with open(dim, 'r', encoding='utf-8') as f:
        for i in feature_info.keys():
            temp = i + " " + " ".join(feature_info[i]) + "\n"
            f.write(temp)


if __name__ == "__main__":
    savename = "banner_color.pickle"
    info_name = ""
    #get history color feature
    color_feature = loadPkl(savename)
    #get origin img information
    img_info = getImgInfoFromCsv(info_name)
    #process
    feature = processColorFeature(img_info, color_feature)
    #save result
    savePkl(feature, savename)

 

版权声明
本文为[WSX_1994]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/WSX1994/p/13946092.html