OpenCV的脸部识别:在Python subspaceproject和subspacerecons

2019-10-18 21:53发布

我已经通过网络搜索,但没有发现任何答案至今以下问题,所以我想在这里问,如果有人可以帮助我认为:

基本上我需要的是一样的,从PraveenofPersia /杰西有解决方案,但只有Python实现考虑费歇尔识别器: 置信度得分的人脸验证任何提示(相对于人脸识别)?

到现在为止,我所面临的问题,即CV2不提供任何subspaceProject也没有任何其他。

这里有没有人建议吗?

谢谢!

Answer 1:

这些功能是不幸的是没有暴露在默认情况下Python API中。

如果您正在构建从源头cv2.pyd,有一个简单的补救措施:

  • 找到自己的RESP。 声明,OpenCV的/模块/的contrib /包含/ opencv2 /的contrib / contrib.hpp
  • 改变CV_EXPORTS Mat subspaceProject(...)CV_EXPORTS_W Mat subspaceProject(...)
  • 改变CV_EXPORTS Mat subspaceReconstruct(...)CV_EXPORTS_W Mat subspaceReconstruct(...)

  • 重新运行cmake的/使重建的简历库和python封装模块

thew附加_W前缀将添加的那些功能所产生的包装纸



Answer 2:

我继续在C RE-写的功能++作为蟒蛇。 这不是最干净的,但它的工程! 如果从借此Python代码,并与高层次的概念夫妇它的另一个C ++例子 ,你可以做正是你想做什么。

 # 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


文章来源: opencv facerecognition: subspaceproject and subspacereconstruct methods in python