Пример #1
0
def get_range():
    sr = StrictRedis()
    res_photos = sr.lrange('final_photos', 0, -1)

    res_map = sr.lrange('map', 0, -1)

    for p, m in zip(res_photos, res_map):
        path = m.decode('utf-8')
        key = os.path.split(path)[0]
        sr.rpush(key, p)
        sr.rpush('{}_index'.format(key), path)
        logger.info('处理完成》》》{}'.format(path))
Пример #2
0
def readPhotos(dir_path='h:/photo', count=-1):
    if os.path.exists(dir_path):
        droot = os.listdir(dir_path)
        count = count if count != -1 else len(droot)
        for d_name in droot:
            d_path = os.path.join(dir_path, d_name)
            d = os.listdir(d_path)
            for file_name in d[:count]:
                file = os.path.join(d_path, file_name)
                yield d_name, file

    else:
        logger.info(f'目录{dir_path}:不存在')
Пример #3
0
def deal_again(start=0, end=-1):
    sr = StrictRedis()
    if start == 0:
        sr.delete('final_photos')
    res = np.array(
        list(
            np.fromstring(i, dtype=np.float64)
            for i in sr.lrange('deal_photos', start, end)))
    logger.info('训练矩阵:{}'.format(res.shape))
    pca = joblib.load('h:/static/model/pca.model')
    newX = pca.transform(res)
    for v in newX:
        sr.rpush('final_photos', v.tostring())

    logger.info("{}》》》{}成功".format(res.shape, newX.shape))
Пример #4
0
def getData(dir_path='h:/static/photo', train_len=6, size=(64, 64)):
    sr = StrictRedis()
    # 清除
    sr.delete('deal_photos', 'map')
    count = 1
    for v in readPhotos(dir_path, train_len):
        d_name, file = v
        try:
            im = Image.open(file, 'r')
            # 预处理
            data = pre_deal(im, size)
            sr.rpush('deal_photos', data.tostring())
            name = os.path.basename(file)
            sr.rpush('map', os.path.join(d_name, name))
        except Exception as e:
            logger.warning("file:{}    error:{}".format(file, e))
        logger.info("成功处理:{:0>5d}-{}-{}-{}".format(count, d_name, data.shape,
                                                   data.dtype))
        count += 1
Пример #5
0
def get_photo():
    # print(request.headers)
    img = request.files.get('file', None)
    if img:
        im = Image.open(img)
        msg = "{}   {}  {}".format(im.size, im.format, im.mode)
        logger.info(msg)
        data = np.array(im.resize((32, 32))) / 255.0
        datas = np.array([data])
        # 定义输入和Label以填充容器,测试时dropout为0
        test_feed_dict = {datas_placeholder: datas, dropout_placeholdr: 0}
        predicted_index = [(i, v) for i, v in enumerate(
            sess.run(logits, feed_dict=test_feed_dict)[0])]
        three_index = heapSort(predicted_index,
                               key=lambda x: x[-1],
                               count_num=10)
        labels = [(sr.lindex('cnn_mapping', int(_[0])).decode('utf-8'), _[1])
                  for _ in three_index if _[1] > 5]
        logger.info("可能的种类:{}".format(labels))
        if not labels:
            return ''
        # 读取图片以及索引
        res = list()
        mapping = list()
        key_score = list()
        max_score = 0
        for label in labels:
            key, score = label
            if max_score < score:
                max_score = max_score
            photos = sr.lrange(key, 0, -1)
            res.extend(photos)
            key_score.extend(score for _ in range(len(photos)))
            mapping.extend(sr.lrange('{}_index'.format(key), 0, -1))
        # 计算pca相似度
        result = []
        for v in use_model(im, pca, res, mapping, key_score, max_score):
            paths, kind_score, cos_score = v
            path = url_for('static',
                           filename='baiduPhoto/{}/{}'.format(*paths))
            logger.info(paths)
            result.append({
                'kind_name': paths[0],
                'url': path,
                'kind_score': kind_score,
                'cos_score': cos_score
            })
        return json.dumps(result, ensure_ascii=False)
    return ''
Пример #6
0
def final_deal():
    # 获得图片矩阵
    sr = StrictRedis()
    res = np.array(
        list(
            np.fromstring(i, dtype=np.float64)
            for i in sr.lrange('deal_photos', 0, -1)))
    logger.info('训练矩阵:{}'.format(res.shape))
    # # pca降维
    pca = PCA(n_components=0.9)
    newX = pca.fit_transform(res)

    sr.delete('final_photos')
    for v in newX:
        sr.rpush('final_photos', v.tostring())
    logger.info("所占特征百分比:{}".format(np.sum(pca.explained_variance_ratio_)))
    logger.info("特征矩阵大小:{}".format(pca.components_.shape))

    path = 'H:/static/model'
    if not os.path.exists(path):
        os.mkdir(path)
    # 保存pca模型
    joblib.dump(pca, os.path.join(path, 'pca.model'))
Пример #7
0
fc = tf.layers.dense(flatten, 400, activation=tf.nn.relu)

# 加上DropOut,防止过拟合
dropout_fc = tf.layers.dropout(fc, dropout_placeholdr)

# 未激活的输出层
logits = tf.layers.dense(dropout_fc, 133)

predicted_labels = tf.arg_max(logits, 1)

sess = tf.Session()
# 如果是测试,载入参数
# 用于保存和载入模型
saver = tf.train.Saver()
saver.restore(sess, model_path)
logger.info("从{}载入模型".format(model_path))


# 自动跳转到静态上传网页
@app.route('/')
def hello_world():
    return send_file('h:/static/update.html')


@app.route('/getPhoto/', methods=['POST', 'GET'])
def get_photo():
    # print(request.headers)
    img = request.files.get('file', None)
    if img:
        im = Image.open(img)
        msg = "{}   {}  {}".format(im.size, im.format, im.mode)