首页 > 解决方案 > 如何解决 Python TypeError:“只能将整数标量数组转换为标量索引”

问题描述

为什么我会收到以下异常?我该如何解决这个问题?

scores = [(dists[id], img_paths[id]) for id in ids]

TypeError:只有整数标量数组可以转换为标量索引

    img = Image.open(file.stream)  # PIL image
    uploaded_img_path = "static/uploaded/" + datetime.now().isoformat() + "_" + file.filename
    img.save(uploaded_img_path)

    img = img.resize((224, 224))  # VGG must take a 224x224 img as an input
    query = fe.extract(img)
    dists = np.linalg.norm(features - query.reshape((64,64,-1)), axis=1)  # Do search
    ids = np.argsort(dists)[:30] # Top 30 results

    scores = [(dists[id], img_paths[id]) for id in ids]

    return render_template('index.html',
                           query_path=uploaded_img_path,
                           scores=scores)
else:
    return render_template('index.html')

标签: pythonintegerscalar

解决方案


推荐阅读