Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python+OpenCV: cv2.imwrite

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?

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 
like image 558
MashkovtsevLx Avatar asked Dec 06 '13 13:12

MashkovtsevLx


People also ask

What is cv2 Imwrite?

OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2. imwrite() method is used to save an image to any storage device. This will save the image according to the specified format in current working directory. Syntax: cv2.imwrite(filename, image)

How do I save an output image in OpenCV Python?

When working with OpenCV Python, images are stored in numpy ndarray. To save an image to the local file system, use cv2. imwrite() function of opencv python library.

Does cv2 Imwrite overwrite Python?

imwrite will overwrite existing files without outputting an error or asking for confirmation. Image of any format can be saved using this method.


Video Answer


2 Answers

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 
like image 100
double_g Avatar answered Sep 21 '22 06:09

double_g


enter image description here enter image description here enter image description here

Alternatively, with MTCNN and OpenCV(other dependencies including TensorFlow also required), you can:

1 Perform face detection(Input an image, output all boxes of detected faces):

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)}}]

2 save all detected faces to separate files:

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 rectangles of all detected faces:

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) 
like image 35
xtluo Avatar answered Sep 21 '22 06:09

xtluo