问题
I have searched through the web, but haven't found any answer so far to the following question, therefore I want to ask here if someone could help me out with that:
basically what i need is the same as in the solution from PraveenofPersia/Jesse there, but only the python implementation considering a Fisherface Recognizer: Any tips on confidence score for face verification (as opposed to face recognition)?
up to now I am facing the problem, that cv2 does not offer either subspaceProject nor any other.
has anyone suggestions here?
thank you!
回答1:
those functions are unfortunately not exposed to the python api by default.
if you're building the cv2.pyd from source, there's an easy remedy :
- locate their resp. declarations, opencv/modules/contrib/include/opencv2/contrib/contrib.hpp
- change
CV_EXPORTS Mat subspaceProject(...)
toCV_EXPORTS_W Mat subspaceProject(...)
change
CV_EXPORTS Mat subspaceReconstruct(...)
toCV_EXPORTS_W Mat subspaceReconstruct(...)
rerun cmake / make to rebuild the cv libs and the python wrapper module
thew additional _W prefix will add those functions to the generated wrappers
回答2:
I went ahead and re-wrote the functions in C++ as python. It's not the cleanest, but it works! If you take this python code and couple it with the high level concepts from another C++ example you can do exactly what you're trying to do.
# projects samples into the LDA subspace
def subspace_project(eigenvectors_column, mean, source):
source_rows = len(source)
source_cols = len(source[0])
if len(eigenvectors_column) != source_cols * source_rows:
raise Exception("wrong shape")
flattened_source = []
for row in source:
flattened_source += [float(num) for num in row]
flattened_source = np.asarray(flattened_source)
delta_from_mean = cv2.subtract(flattened_source, mean)
# flatten the matrix then convert to 1 row by many columns
delta_from_mean = np.asarray([np.hstack(delta_from_mean)])
empty_mat = np.array(eigenvectors_column, copy=True) # this is required for the function call but unused
result = cv2.gemm(delta_from_mean, eigenvectors_column, 1.0, empty_mat, 0.0)
return result
# reconstructs projections from the LDA subspace
def subspace_reconstruct(eigenvectors_column, mean, projection, image_width, image_height):
if len(eigenvectors_column[0]) != len(projection[0]):
raise Exception("wrong shape")
empty_mat = np.array(eigenvectors_column, copy=True) # this is required for the function call but unused
# GEMM_2_T transposes the eigenvector
result = cv2.gemm(projection, eigenvectors_column, 1.0, empty_mat, 0.0, flags=cv2.GEMM_2_T)
flattened_array = result[0]
flattened_image = np.hstack(cv2.add(flattened_array, mean))
flattened_image = np.asarray([np.uint8(num) for num in flattened_image])
all_rows = []
for row_index in xrange(image_height):
row = flattened_image[row_index * image_width: (row_index + 1) * image_width]
all_rows.append(row)
image_matrix = np.asarray(all_rows)
image = normalize_hist(image_matrix)
return image
def normalize_hist(face):
face_as_mat = np.asarray(face)
equalized_face = cv2.equalizeHist(face_as_mat)
equalized_face = cv.fromarray(equalized_face)
return equalized_face
来源:https://stackoverflow.com/questions/19756176/opencv-facerecognition-subspaceproject-and-subspacereconstruct-methods-in-pytho