opencv facerecognition:python中的subspaceproject和subspacecreconstruct方法

2024-09-30 20:26:11 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

到目前为止,我面临的问题是,cv2既没有提供子空间项目,也没有提供任何其他项目。在

有什么建议吗?在

谢谢你!在


Tags: 项目答案foronasany解决方案face
2条回答

我继续写了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

不幸的是,默认情况下,这些函数没有公开给pythonapi。在

如果您从源代码构建cv2.pyd,有一个简单的补救方法:

  • 找到他们的负责人。声明,opencv/modules/contrib/include/opencv2/contrib/出资水电站在
  • CV_EXPORTS Mat subspaceProject(...)更改为CV_EXPORTS_W Mat subspaceProject(...)
  • CV_EXPORTS Mat subspaceReconstruct(...)改为CV_EXPORTS_W Mat subspaceReconstruct(...)

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

W additional-W前缀将把这些函数添加到生成的包装器中

相关问题 更多 >