def detect(self, image): # image size is needed by underlying opencv lib to allocate memory image_size = opencv.cvGetSize(image) # the algorithm works with grayscale images grayscale = opencv.cvCreateImage(image_size, 8, 1) opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY) # more underlying c lib memory allocation storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # equalize histogram opencv.cvEqualizeHist(grayscale, grayscale) # detect faces using haar cascade, the used file is trained to # detect frontal faces cascade = opencv.cvLoadHaarClassifierCascade( 'haarcascade_frontalface_alt.xml', opencv.cvSize(1, 1)) faces = opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(100, 100)) # draw rectangles around faces for face in faces: opencv.cvRectangle( image, opencv.cvPoint(int(face.x), int(face.y)), opencv.cvPoint(int(face.x + face.width), int(face.y + face.height)), opencv.CV_RGB(127, 255, 0), 2) # return faces casted to list here, otherwise some obscure bug # in opencv will make it segfault if the casting happens later return image, list(faces)
def detect(self, image): # image size is needed by underlying opencv lib to allocate memory image_size = opencv.cvGetSize(image) # the algorithm works with grayscale images grayscale = opencv.cvCreateImage(image_size, 8, 1) opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY) # more underlying c lib memory allocation storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # equalize histogram opencv.cvEqualizeHist(grayscale, grayscale) # detect faces using haar cascade, the used file is trained to # detect frontal faces cascade = opencv.cvLoadHaarClassifierCascade( 'haarcascade_frontalface_alt.xml', opencv.cvSize(1, 1)) faces = opencv.cvHaarDetectObjects( grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(100, 100)) # draw rectangles around faces for face in faces: opencv.cvRectangle( image, opencv.cvPoint( int(face.x), int(face.y)), opencv.cvPoint(int(face.x + face.width), int(face.y + face.height)), opencv.CV_RGB(127, 255, 0), 2) # return faces casted to list here, otherwise some obscure bug # in opencv will make it segfault if the casting happens later return image, list(faces)
def get_frame(self, face_rec = False): image = highgui.cvQueryFrame(self.device) face_matches = False if face_rec: grayscale = cv.cvCreateImage(cv.cvSize(640, 480), 8, 1) cv.cvCvtColor(image, grayscale, cv.CV_BGR2GRAY) storage = cv.cvCreateMemStorage(0) cv.cvClearMemStorage(storage) cv.cvEqualizeHist(grayscale, grayscale) for cascade in self.haarfiles: matches = cv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, cv.cvSize(100,100)) if matches: face_matches = True for i in matches: cv.cvRectangle(image, cv.cvPoint( int(i.x), int(i.y)), cv.cvPoint(int(i.x+i.width), int(i.y+i.height)), cv.CV_RGB(0,0,255), 2, 5, 0) image = cv.cvGetMat(image) return (image, face_matches)
def detect(image): # Find out how large the file is, as the underlying C-based code # needs to allocate memory in the following steps image_size = opencv.cvGetSize(image) # create grayscale version - this is also the point where the allegation about # facial recognition being racist might be most true. A caucasian face would have more # definition on a webcam image than an African face when greyscaled. # I would suggest that adding in a routine to overlay edge-detection enhancements may # help, but you would also need to do this to the training images as well. grayscale = opencv.cvCreateImage(image_size, 8, 1) opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY) # create storage (It is C-based so you need to do this sort of thing) storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # equalize histogram opencv.cvEqualizeHist(grayscale, grayscale) # detect objects - Haar cascade step # In this case, the code uses a frontal_face cascade - trained to spot faces that look directly # at the camera. In reality, I found that no bearded or hairy person must have been in the training # set of images, as the detection routine turned out to be beardist as well as a little racist! cascade = opencv.cvLoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', opencv.cvSize(1,1)) faces = opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(50, 50)) if faces: for face in faces: # Hmm should I do a min-size check? # Draw a Chartreuse rectangle around the face - Chartruese rocks opencv.cvRectangle(image, opencv.cvPoint( int(face.x), int(face.y)), opencv.cvPoint(int(face.x + face.width), int(face.y + face.height)), opencv.CV_RGB(127, 255, 0), 2) # RGB #7FFF00 width=2
def removeMinimum(img, x, y, w, h): """Utility function for removing the global minimum from a cvArray. This finds the global maximum of the same array and then blits a rectangle of the same color over the area designated by x,y and w,h. This way to find the next _local_ minimum, we can just remove the current global minimum and search for the global one again using cvMinMaxLoc - should be faster than looking for all local minima with python.""" minmax = cvMinMaxLoc(img) cvRectangle(img, cvPoint(x-int(w/2), y-int(h/2)), cvPoint(x+int(w/2), y+int(h/2)), cvScalar(minmax[1], 0, 0, 0), CV_FILLED)
def draw_boxes(self,input_data,bboxmin,bboxmax): """ Draws three boxes arond the returned location. """ opencv.cvRectangle(self.input_data, opencv.cvPoint(np.int(bboxmin[0]),np.int(bboxmin[1])),opencv.cvPoint(np.int(bboxmax[0]) ,np.int(bboxmax[1])),0,1) opencv.cvRectangle(self.input_data, opencv.cvPoint(np.int(bboxmin[0])-1,np.int(bboxmin[1])-1),opencv.cvPoint(np.int(bboxmax[0]) +1,np.int(bboxmax[1])+1),0,1) opencv.cvRectangle(self.input_data, opencv.cvPoint(np.int(bboxmin[0])-2,np.int(bboxmin[1])-2),opencv.cvPoint(np.int(bboxmax[0]) +2,np.int(bboxmax[1])+2),1,1)
def draw_boxes(self,input_data,bboxmin,bboxmax): """ Draws three boxes arond the returned location. """ opencv.cvRectangle(self.input_data, opencv.cvPoint(np.int(bboxmin[0]),np.int(bboxmin[1])),opencv.cvPoint(np.int(bboxmax[0]) ,np.int(bboxmax[1])),0,1) opencv.cvRectangle(self.input_data, opencv.cvPoint(np.int(bboxmin[0])-1,np.int(bboxmin[1])-1),opencv.cvPoint(np.int(bboxmax[0]) +1,np.int(bboxmax[1])+1),0,1) opencv.cvRectangle(self.input_data, opencv.cvPoint(np.int(bboxmin[0])-2,np.int(bboxmin[1])-2),opencv.cvPoint(np.int(bboxmax[0]) +2,np.int(bboxmax[1])+2),1,1)