Python 如何从Deepface detectFace保存图像?

Python 如何从Deepface detectFace保存图像?,python,image-processing,cv2,face-recognition,Python,Image Processing,Cv2,Face Recognition,我使用Deepface来检测和对齐面 from deepface import DeepFace import cv2 align_face = DeepFace.detectFace("base/g3.jpg") 所以我需要将图像从align_fac保存到jpg。 我该怎么做?尝试以下方法: from deepface import DeepFace import cv2 align_face = DeepFace.detectFace("base/g3.j

我使用Deepface来检测和对齐面

from deepface import DeepFace
import cv2

align_face = DeepFace.detectFace("base/g3.jpg")
所以我需要将图像从align_fac保存到jpg。 我该怎么做?

尝试以下方法:

from deepface import DeepFace
import cv2

align_face = DeepFace.detectFace("base/g3.jpg")
cv2.imwrite("face.jpg", align_face)

detectFace函数以[0,1]的比例返回。这是matplotlib的预期范围。我的意思是,您可以按原样使用matplotlib绘制它

另一方面,opencv期望图像的比例为[0255]。这就是为什么,你应该用乘以255来反规范化它。此外,opencv使用BGR而不是RGB。您还应该使用[:,:,::-1]进行此转换

from deepface import DeepFace
import matplotlib.pyplot as plt
import cv2

detected_face = DeepFace.detectFace( "base/g3.jpg", detector_backend = 'opencv')

plt.imshow(detected_face)
plt.show()

detected_face = detected_face * 255
cv2.imwrite("face.jpg", detected_face[:, :, ::-1])

谢谢,很有趣。我想,对于那个问这个问题的人来说,情况也是如此。)我更喜欢直接处理dlib、cv2等。所以,这段代码会将人脸图像写入文件。您可能需要对其进行RGB2BGR,并将其反规范化为255。