• 代码保存


    # 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)
  • 相关阅读:
    python中type、object与class之间关系(一切皆对象)
    为什么在python中推荐使用多进程而不是多线程(转载)
    CPU密集型 VS IO密集型
    多CPU,多核,多进程,多线程
    Mac下brew安装与配置mysql
    mac安装navicat mysql破解版
    微信公众号-h5调用微信支付
    为什么js中0.1+0.2不等于0.3,怎样处理使之相等?(转载)
    gitlab安装和汉化
    PyPI使用国内源
  • 原文地址:https://www.cnblogs.com/WSX1994/p/13946092.html
Copyright © 2020-2023  润新知