opencv facerecognition:python中的subspaceproject和subspacereconstruct方法

时间:2013-11-03 18:06:44

标签: python opencv face-recognition

我已经在网上搜索过,但到目前为止还没有找到任何答案,因此我想问一下是否有人可以帮我解决这个问题:

基本上我需要的是与PraveenofPersia / Jesse的解决方案相同,但只考虑Fisherface Recognizer的python实现: Any tips on confidence score for face verification (as opposed to face recognition)?

到目前为止我遇到了问题,cv2既没有提供subspaceProject,也没有提供任何其他的。

这里有什么建议吗?

谢谢你!

2 个答案:

答案 0 :(得分:1)

默认情况下,这些函数不会暴露给python api。

如果您从源代码构建cv2.pyd,可以轻松解决问题:

  • 找到他们的分配。声明,opencv / modules / contrib / include / opencv2 / contrib / contrib.hpp
  • CV_EXPORTS Mat subspaceProject(...)更改为CV_EXPORTS_W Mat subspaceProject(...)
  • CV_EXPORTS Mat subspaceReconstruct(...)更改为CV_EXPORTS_W Mat subspaceReconstruct(...)

  • 重新运行cmake / make以重建cv libs和python包装器模块

另外的_W前缀会将这些函数添加到生成的包装器

答案 1 :(得分:1)

我继续用C ++重写了函数python。它不是最干净的,但它有效!如果您使用此python代码并将其与 another C++ example中的高级概念结合使用,那么您可以完全按照您要执行的操作。

 # 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