【问题标题】:Python+OpenCV: cv2.imwritePython+OpenCV:cv2.imwrite
【发布时间】:2013-12-23 21:53:20
【问题描述】:

我正在尝试检测人脸并将人脸区域写在单独的文件中。 我该怎么做?我认为我必须使用“面孔”(你可以在代码中看到这个 var)。但是怎么做呢?

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

【问题讨论】:

    标签: python opencv python-2.7 numpy


    【解决方案1】:

    以下代码应在图像中提取人脸并将人脸保存在磁盘上

    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
    

    【讨论】:

      【解决方案2】:

      或者,使用 MTCNN 和 OpenCV(还需要其他依赖项,包括 TensorFlow),您可以:

      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), '鼻子': (129, 142), 'mouth_left': (112, 168), 'mouth_right': (146, 167)}}]

      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 或 绘制所有检测到的人脸的矩形

      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)
      

      【讨论】:

      • 对于大量面孔,我现在正在做的是将所有面孔和边界框保存到列表中,然后使用 h5py 将这些 numpy 数组列表保存到磁盘。有没有更好的办法?
      • @mLstudent33 我不太明白。您可以在 SOF 上提问,提供更详细的信息,例如输入、输出以及你正在做什么来实现它。然后看看有人能帮忙。
      【解决方案3】:

      wtluo,太棒了! 我可以提议对您的代码 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)
      

      【讨论】:

      • 你能分享一下为什么你的修改更好,或者在什么情况下会更好?
      • gar,这个修改是相同代码的一个不那么冗长的版本。因此,它可能读起来更短,更容易理解,处理起来也更快。关于 imwrite 行(因为我现在可以理解这是您的主题),现在不是建议使用函数 format() 而不是操作符 % 在字符串上吗?
      • @Schmouk 感谢您的建议!我的名字是xtluo 而不是wtluo。 ^_^
      猜你喜欢
      • 2017-05-16
      • 1970-01-01
      • 2023-03-21
      • 2020-10-18
      • 1970-01-01
      • 2022-01-15
      • 2020-03-17
      • 2014-06-18
      • 2018-10-23
      相关资源
      最近更新 更多