Python+OpenCV:cv2.imwrite
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20425724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python+OpenCV: cv2.imwrite
提问by MashkovtsevLx
I'm trying to detect a face and write down area with the face in a separate file. How can I do it? I think that i must use "faces" (you can see this var in code). But how?
我正在尝试检测人脸并在单独的文件中用人脸写下区域。我该怎么做?我认为我必须使用“faces”(你可以在代码中看到这个变量)。但是如何?
from ffnet import mlgraph, ffnet, tmlgraph, imlgraph
import pylab
import sys
import cv,cv2
import numpy
cascade = cv.Load('C:\opencv\data\haarcascades\haarcascade_frontalface_alt.xml')
def detect(image):
bitmap = cv.fromarray(image)
faces = cv.HaarDetectObjects(bitmap, cascade, cv.CreateMemStorage(0))
if faces:
for (x,y,w,h),n in faces:
cv2.rectangle(image,(x,y),(x+w,y+h),(255,255,255),3)
return image
if __name__ == "__main__":
cam = cv2.VideoCapture(0)
while 1:
_,frame =cam.read()
frame = numpy.asarray(detect(frame))
cv2.imshow("features", frame)
if cv2.waitKey(1) == 0x1b: # ESC
print 'ESC pressed. Exiting ...'
break
采纳答案by double_g
This following code should extract face in images and save faces on disk
以下代码应提取图像中的人脸并将人脸保存在磁盘上
def detect(image):
image_faces = []
bitmap = cv.fromarray(image)
faces = cv.HaarDetectObjects(bitmap, cascade, cv.CreateMemStorage(0))
if faces:
for (x,y,w,h),n in faces:
image_faces.append(image[y:(y+h), x:(x+w)])
#cv2.rectangle(image,(x,y),(x+w,y+h),(255,255,255),3)
return image_faces
if __name__ == "__main__":
cam = cv2.VideoCapture(0)
while 1:
_,frame =cam.read()
image_faces = []
image_faces = detect(frame)
for i, face in enumerate(image_faces):
cv2.imwrite("face-" + str(i) + ".jpg", face)
#cv2.imshow("features", frame)
if cv2.waitKey(1) == 0x1b: # ESC
print 'ESC pressed. Exiting ...'
break
回答by xtluo
Alternatively, with MTCNNand OpenCV(other dependencies including TensorFlow also required), you can:
或者,使用MTCNN和 OpenCV(还需要其他依赖项,包括 TensorFlow),您可以:
1 Perform face detection(Input an image, output all boxes of detected faces):
1执行人脸检测(输入一张图片,输出所有检测到的人脸框):
from mtcnn.mtcnn import MTCNN
import cv2
face_detector = MTCNN()
img = cv2.imread("Anthony_Hopkins_0001.jpg")
detect_boxes = face_detector.detect_faces(img)
print(detect_boxes)
[{'box': [73, 69, 98, 123], 'confidence': 0.9996458292007446, 'keypoints': {'left_eye': (102, 116), 'right_eye': (150, 114), 'nose': (129, 142), 'mouth_left': (112, 168), 'mouth_right': (146, 167)}}]
[{'box': [73, 69, 98, 123], 'confidence': 0.9996458292007446, 'keypoints': {'left_eye': (102, 116), 'right_eye': (150, 114), 'nose' : (129, 142), 'mouth_left': (112, 168), 'mouth_right': (146, 167)}}]
2 save all detected faces to separate files:
2将所有检测到的人脸保存到单独的文件中:
for i in range(len(detect_boxes)):
box = detect_boxes[i]["box"]
face_img = img[box[1]:(box[1] + box[3]), box[0]:(box[0] + box[2])]
cv2.imwrite("face-{:03d}.jpg".format(i+1), face_img)
3 or Draw rectanglesof all detected faces:
3 或绘制所有检测到的人脸的矩形:
for box in detect_boxes:
box = box["box"]
pt1 = (box[0], box[1]) # top left
pt2 = (box[0] + box[2], box[1] + box[3]) # bottom right
cv2.rectangle(img, pt1, pt2, (0,255,0), 2)
cv2.imwrite("detected-boxes.jpg", img)
回答by Schmouk
wtluo, great ! May I propose a slight modification of your code 2. ? Here it is:
太棒了!我可以建议对您的代码 2 稍作修改吗?这里是:
for i, detected_box in enumerate(detect_boxes):
box = detected_box["box"]
face_img = img[ box[1]:box[1] + box[3], box[0]:box[0] + box[2] ]
cv2.imwrite("face-{:03d}.jpg".format(i+1), face_img)


