python将图像特征向量浮点值转换为正整数

时间:2019-07-11 15:07:53

标签: python numpy elasticsearch

我正在尝试引用本文的代码:

https://www.linkedin.com/pulse/hacking-elasticsearch-image-retrieval-ashwin-saval

如文章所述

  

注意:尽管我们将图像特征作为简单数组存储在Elasticsearch上,但是如果我们也要存储为术语向量,则需要对图像特征进行量化。由于术语值只能是正整数。

pip install numpy scipy scikit-learn face_recognition opencv-python

python face-quantization.py test.jpg

import os
import sys
import cv2
import face_recognition
import numpy as np
from sklearn.preprocessing import normalize

def get_median_values_for_bins(bins):
    median_values = {}
    for binidx in range(1, bins.shape[0]):
        binval = bins[binidx]
        binval_prev = bins[binidx - 1]
        median_values[binidx] = binval_prev

    median_values[bins.shape[0]] = bins[bins.shape[0]-1]
    return median_values

def get_quantized_features(features, quantization_factor=30):
    normalized_features = normalize(features, axis=1)
    offset = np.abs(np.min(normalized_features))
    offset_features = normalized_features + offset # Making all feature values positive

    # Let's proceed to quantize these positive feature values
    min_val = np.min(offset_features)
    max_val = np.max(offset_features)

    bins = np.linspace(start=min_val, stop=max_val, num=quantization_factor)
    median_values = get_median_values_for_bins(bins)
    original_quantized_features = np.digitize(offset_features, bins)

    quantized_features = np.apply_along_axis(lambda row: map(lambda x: median_values[x], row), 1, original_quantized_features)

    quantized_features = np.floor(quantization_factor*quantized_features)
    return quantized_features


if len(sys.argv) < 2:
    print("Usage: face-encode <image>")
    exit(1)

# Take the image file name from the command line
file_name = sys.argv[1]

# Load the image
image = cv2.imread(file_name)
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# detect the (x, y)-coordinates of the bounding boxes
# corresponding to each face in the input img
boxes = face_recognition.face_locations(rgb)

# compute the facial embedding for the face
encodings = face_recognition.face_encodings(rgb, boxes)

quantization_factor = 5000 # Adjust this depending on accuracy of quantized features.

for idx,encoding in enumerate(encodings):
    quantized_features = get_quantized_features(encoding.reshape(-1, 1), quantization_factor)

我收到此错误

 Traceback (most recent call last):
   File "face-quantization.py", line 58, in <module>
   quantized_features = get_quantized_features(encoding.reshape(-1, 1), quantization_factor)
   File "face-quantization.py", line 33, in get_quantized_features
quantized_features = np.floor(quantization_factor*quantized_features)
TypeError: unsupported operand type(s) for *: 'int' and 'map'

任何帮助将不胜感激

谢谢

0 个答案:

没有答案