• 代码保存


    # 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]) + "
    "
                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)
  • 相关阅读:
    list与map的区别
    [BJOI2019]光线——递推
    [BJOI2019]奥术神杖——AC自动机+DP+分数规划+二分答案
    [BJOI2019]送别——非旋转treap
    [BJOI2019]排兵布阵——分组背包
    [LOJ3014][JOI 2019 Final]独特的城市——树的直径+长链剖分
    BZOJ2300[HAOI2011]防线修建——非旋转treap+凸包(平衡树动态维护凸包)
    BZOJ3033太鼓达人——哈密顿回路/欧拉回路
    BZOJ3236[Ahoi2013]作业——莫队+树状数组/莫队+分块
    BZOJ2339[HNOI2011]卡农——递推+组合数
  • 原文地址:https://www.cnblogs.com/WSX1994/p/13946092.html
Copyright © 2020-2023  润新知