Beispiel #1
0
 def hist_eq(self, frame):
     cv.Split(frame, self.B, self.G, self.R, None)
     cv.EqualizeHist(self.R, self.R)
     cv.EqualizeHist(self.R, self.B)
     cv.EqualizeHist(self.G, self.G)
     cv.Merge(self.B, self.G, self.R, None, self.Ieq)
     return self.Ieq
Beispiel #2
0
def detect_faces(backend, cascade_filename='haarcascade_frontalface_alt2.xml'):
    cv = backend.get_opencv()

    # If a relative path was provided, check local cascades directory
    if not os.path.isabs(cascade_filename):
        cascade_filename = os.path.join(
            os.path.dirname(__file__),
            'face_detection',
            cascade_filename,
        )

    cascade = cv.Load(cascade_filename)
    image = backend.opencv_grey_image()

    cv.EqualizeHist(image, image)

    min_size = (40, 40)
    haar_scale = 1.1
    min_neighbors = 3
    haar_flags = 0

    faces = cv.HaarDetectObjects(image, cascade, cv.CreateMemStorage(0),
                                 haar_scale, min_neighbors, haar_flags,
                                 min_size)

    return [(
        face[0][0],
        face[0][1],
        face[0][0] + face[0][2],
        face[0][1] + face[0][3],
    ) for face in faces]
Beispiel #3
0
def detect(image):
    #cv.Rectangle(image, (10,10), (50,50), cv.RGB(255,0,0))
    image_size = cv.GetSize(image)

    # create grayscale version
    grayscale = cv.CreateImage(image_size, 8, 1)
    cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY)

    # create storage
    storage = cv.CreateMemStorage(0)
    #cv.ClearMemStorage(storage)

    # equalize histogram
    cv.EqualizeHist(grayscale, grayscale)

    # detect objects
    cascade = cv.Load('haarcascade_frontalface_alt.xml')  #, cv.Size(1,1))
    faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2,
                                 cv.CV_HAAR_DO_CANNY_PRUNING, (100, 100))

    if faces:
        #print len(faces), 'face(s) detected!'
        #if len(faces)>1: print '2 faces found'
        for i in faces:
            j = i[0]
            cv.Rectangle(image, (int(j[0]), int(j[1])),
                         (int(j[0] + j[2]), int(j[1] + j[3])),
                         cv.RGB(0, 255, 0), 3, 8, 0)
Beispiel #4
0
    def grayscale(self):
        convert_mode = getattr(cv, 'CV_%s2GRAY' % self.mode)

        gray = cv.CreateImage(self.size, cv.IPL_DEPTH_8U, 1)
        cv.CvtColor(self.image, gray, convert_mode)
        cv.EqualizeHist(gray, gray)
        self.image = gray
def DetectFace(image, faceCascade, returnImage=False):
    # This function takes a grey scale cv image and finds
    # the patterns defined in the haarcascade function
    # modified from: http://www.lucaamore.com/?p=638

    #variables
    min_size = (20, 20)
    haar_scale = 1.1
    min_neighbors = 3
    haar_flags = 0

    # Equalize the histogram
    cv.EqualizeHist(image, image)

    # Detect the faces
    faces = cv.HaarDetectObjects(image, faceCascade, cv.CreateMemStorage(0),
                                 haar_scale, min_neighbors, haar_flags,
                                 min_size)

    # If faces are found
    if faces and returnImage:
        for ((x, y, w, h), n) in faces:
            # Convert bounding box to two CvPoints
            pt1 = (int(x), int(y))
            pt2 = (int(x + w), int(y + h))
            cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 5, 8, 0)

    if returnImage:
        return image
    else:
        return faces
Beispiel #6
0
def work(image):
    image_size = cv.GetSize(image)
    grayscale = cv.CreateImage(image_size, 8, 1)
    cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY)
    storage = cv.CreateMemStorage(0)
    cv.EqualizeHist(grayscale, grayscale)
    return image
def show_photo():
    # Get the photo
    img = c.get_photo() \
        .get_response() \
        .resize((300, 300), Img.ANTIALIAS)

    # Convert the photo for OpenCV
    cv_img = cv.CreateImageHeader(img.size, cv.IPL_DEPTH_8U, 1)
    cv.SetData(cv_img, img.tostring())

    # Find any faces in the image
    storage = cv.CreateMemStorage(0)
    cv.EqualizeHist(cv_img, cv_img)
    faces = cv.HaarDetectObjects(cv_img, cascade, storage, 1.2, 2,
                                 cv.CV_HAAR_DO_CANNY_PRUNING)

    if faces:
        for f in faces:
            # Draw a border around a found face.
            draw = ImageDraw.Draw(img)
            draw.setfill(0)
            draw.rectangle([(f[0][0], f[0][1]),
                            (f[0][0] + f[0][2], f[0][1] + f[0][3])])

    image = PhotoImage(img)
    img_face.config(image=image)
    img_face.img = image

    img_face.after(10, show_photo)
Beispiel #8
0
def detect(image, config):
    angle = None
    image_size = cv.GetSize(image)
    # create grayscale version
    grayscale = cv.CreateImage(image_size, 8, 1)
    cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY)
    if config["EqualizeHist"]:
        cv.EqualizeHist(grayscale, grayscale)

    pattern_width = 5
    pattern_height = 4
    found, corners = cv.FindChessboardCorners(grayscale,
                                              (pattern_width, pattern_height))

    if found:
        new_corners = cv.FindCornerSubPix(
            grayscale, corners, (11, 11), (-1, -1),
            (cv.CV_TERMCRIT_EPS + cv.CV_TERMCRIT_ITER, 30, 0.1))
        angle = corners_to_angle(new_corners)

        #print "angle: ", angle

        def to_int(t):
            return (int(t[0]), int(t[1]))

        #nc = [to_int(corner) for corner in new_corners]
        #cv.Line( grayscale, nc[0], nc[4], (255,0,0), 2)
        #cv.Line( grayscale, nc[4], nc[19], (0,255,0),2)
        #cv.Line( grayscale, nc[19], nc[15], (0,0,255),2)
        #cv.Line( grayscale, nc[15], nc[0], (255,255,0),2)

    #cv.ShowImage('Processed', grayscale)
    return angle
Beispiel #9
0
def detect_and_draw(img, cascade):
    gray = cv.CreateImage((img.width,img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(img.width / image_scale),
			       cv.Round (img.height / image_scale)), 8, 1)
    # convert color input image to grayscale
    cv.CvtColor(img, gray, cv.CV_BGR2GRAY)
    # scale input image for faster processing
    cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)
    cv.EqualizeHist(small_img, small_img)
    if(cascade):
        t = cv.GetTickCount()
        faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0),
                                     haar_scale, min_neighbors, haar_flags, min_size)
        index = 0 
        if faces:
            for ((x, y, w, h), n) in faces:
                # the input to cv.HaarDetectObjects was resized, so scale the 
                # bounding box of each face and convert it to two CvPoints
                x1, y1 = (int(x * image_scale), int(y * image_scale))
                x2, y2 = (int((x + w) * image_scale), int((y + h) * image_scale))

                pi = Image.fromstring("L", cv.GetSize(gray), gray.tostring())
                pi = pi.crop((x1, y1, x2, y2))
                pi = pi.resize((64, 64), Image.ANTIALIAS)
                path = os.path.join(sys.argv[1])
                pi.save(path)
                index += 1 
        else:
            os.remove(sys.argv[1])
Beispiel #10
0
def DetectFace(image, faceCascade, returnImage=False):

    # variables
    min_size = (50, 50)
    haar_scale = 1.1
    min_neighbors = 3
    haar_flags = 0
    DOWNSCALE = 4

    # Equalize the histogram
    cv.EqualizeHist(image, image)

    # Detect the faces
    faces = cv.HaarDetectObjects(image, faceCascade, cv.CreateMemStorage(0),
                                 haar_scale, min_neighbors, haar_flags,
                                 min_size)

    # If faces are found
    if faces and returnImage:
        for ((x, y, w, h), n) in faces:

            # Convert bounding box to two CvPoints
            pt1 = (int(x), int(y))
            pt2 = (int(x + w), int(y + h))
            cv2.rectangle(image, pt1, pt2, (255, 0, 0), 5, 8, 0)

    if returnImage:
        return image
    else:
        return faces
Beispiel #11
0
    def detect_faces(self,
                     cascade_filename='haarcascade_frontalface_alt2.xml'):
        cv = _cv()

        # If a relative path was provided, check local cascades directory
        if not os.path.isabs(cascade_filename):
            cascade_filename = os.path.join(
                os.path.dirname(os.path.dirname(__file__)),
                'data/cascades',
                cascade_filename,
            )

        # Load cascade file
        cascade = cv.Load(cascade_filename)

        # Equalise the images histogram
        equalised_image = cv.CloneImage(self.image)
        cv.EqualizeHist(self.image, equalised_image)

        # Detect faces
        min_size = (40, 40)
        haar_scale = 1.1
        min_neighbors = 3
        haar_flags = 0

        faces = cv.HaarDetectObjects(equalised_image, cascade,
                                     cv.CreateMemStorage(0), haar_scale,
                                     min_neighbors, haar_flags, min_size)

        return [(
            face[0][0],
            face[0][1],
            face[0][0] + face[0][2],
            face[0][1] + face[0][3],
        ) for face in faces]
Beispiel #12
0
Datei: test.py Projekt: wy51r/kod
    def run(self):
        while True:
            frame = cv.QueryFrame(self.capture)
            image_size = cv.GetSize(frame)
            gray = cv.CreateImage((image_size[0], image_size[1]), 8, 1)
            cv.CvtColor(frame, gray, cv.CV_BGR2GRAY)
            # scale input image for faster processing
            small_img = cv.CreateImage((cv.Round(image_size[0] / __scale__),
                                        cv.Round(image_size[1] / __scale__)),
                                       8, 1)
            cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)
            cv.EqualizeHist(small_img, small_img)

            s_res = sift(np.array(cv.GetMat(small_img)).flatten('C'))
            n_res = np.array(s_res)
            for item in n_res:
                xx = item[0] * __scale__
                yy = item[1] * __scale__
                label_sift_point(frame, xx, yy)

            cv.ShowImage("CamShiftDemo", frame)

            c = cv.WaitKey(7)
            if c == 27:
                break
            elif c == ord("t"):
                frame = cv.QueryFrame(self.capture)
                self.save(frame)
                self.i += 1
Beispiel #13
0
def detect_face(image, face_cascade, return_image=False):
    # This function takes a grey scale cv image and finds
    # the patterns defined in the haarcascade function
    min_size = (20, 20)
    haar_scale = 1.1
    min_neighbors = 5
    haar_flags = 0

    # Equalize histogram
    cv.EqualizeHist(image, image)

    # Detect faces
    faces = cv.HaarDetectObjects(image, face_cascade, cv.CreateMemStorage(0),
                                 haar_scale, min_neighbors, haar_flags,
                                 min_size)

    # If faces are found
    if faces and return_image:
        for ((x, y, w, h), n) in faces:
            # Convert bounding box to two CvPoints
            pt1 = (int(x), int(y))
            pt2 = (int(x + w), int(y + h))
            cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 5, 8, 0)
    if return_image:
        return image
    else:
        return faces
Beispiel #14
0
    def DetectFace(self,image, faceCascade):
     
        min_size = (20,20)
        image_scale = 2
        haar_scale = 1.1
        min_neighbors = 3
        haar_flags = 0
     
        # Allocate the temporary images
        grayscale = cv.CreateImage((image.width, image.height), 8, 1)
        #grayscale = cv.CreateImage((image.shape[1], image.shape[0]), 8, 1)
        smallImage = cv.CreateImage(
                (
                    cv.Round(image.width / image_scale),
                    cv.Round(image.height / image_scale)
                ), 8 ,1)
     
        # Convert color input image to grayscale
        cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY)
     
        # Scale input image for faster processing
        cv.Resize(grayscale, smallImage, cv.CV_INTER_LINEAR)
     
        # Equalize the histogram
        cv.EqualizeHist(smallImage, smallImage)
     
        # Detect the faces
        faces = cv.HaarDetectObjects(
                smallImage, faceCascade, cv.CreateMemStorage(0),
                haar_scale, min_neighbors, haar_flags, min_size
            )
     
        # If faces are found
        if faces:
            payload=targets()
            payload.ref_img_width=smallImage.width
            payload.ref_img_height=smallImage.height
            for ((x, y, w, h), n) in faces:
                # the input to cv.HaarDetectObjects was resized, so scale the
                # bounding box of each face and convert it to two CvPoints
                pt1 = (int(x * image_scale), int(y * image_scale))
                pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
                cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 5, 8, 0)
                fbox=face_box()
                fbox.top_left.x=float(x)/float(smallImage.width)
                fbox.top_left.y=float(y)/float(smallImage.height)
                fbox.width_height.x=float(w)/float(smallImage.width)
                fbox.width_height.y=float(h)/float(smallImage.height)
                payload.faces.append(fbox)
                #fpt=Point()
                #fpt.x=(float(x) + float(w)/2.0)/float(smallImage.width)
                #fpt.y=(float(y) + float(h)/2.0)/float(smallImage.height)
                #payload.faces.append(fpt)

            msg = payload

            rospy.loginfo(msg)
            self.pub.publish(msg)
     
        return image
Beispiel #15
0
def detect(image):
    image_size = cv.GetSize(image)

    # create grayscale version
    grayscale = cv.CreateImage(image_size, 8, 1)
    cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY)

    # create storage
    storage = cv.CreateMemStorage(0)

    # equalize histogram
    cv.EqualizeHist(grayscale, grayscale)

    # show processed image
    #cv.ShowImage('Processed', grayscale)

    # detect objects
    cascade = cv.Load(
        '/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml')
    faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2,
                                 cv.CV_HAAR_DO_CANNY_PRUNING)

    if faces:
        for i in faces:
            cv.Rectangle(image, (i[0][0], i[0][1]),
                         (i[0][0] + i[0][2], i[0][1] + i[0][3]), (0, 0, 255),
                         1, 8, 0)
Beispiel #16
0
def detect(image):
    image_size = cv.GetSize(image)

    #create greyscale version
    grayscale = cv.CreateImage(image_size, 8, 1)
    cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY)

    # create storage
    storage = cv.CreateMemStorage(0)
    #cv.ClearMemStorage(storage)

    # equalize histogram
    cv.EqualizeHist(grayscale, grayscale)

    # detect objects
    cascade = cv.Load('haarcascade_frontalface_alt.xml')
    faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2,
                                 cv.HAAR_DO_CANNY_PRUNING, CV.SIZE(50, 50))

    if faces:
        print 'face detected!'
        for i in faces:
            cv.Rectangle(image, cv.Point(int(i.x), int(i.y)),
                         cv.Point(int(i.x + i.width), int(i.y + i.height)),
                         cv.RGB(0, 255, 0), 3, 8, 0)

    cv.ShowImage('Video', image)

    return faces
Beispiel #17
0
def detectObjects(image):
    """Converts an image to grayscale and prints the locations of any 
    faces found"""
    grayscale = cv.CreateImage(cv.GetSize(image), 8, 1)
    cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY)

    # Deprecated v1 code???
    storage = cv.CreateMemStorage(0)
    #cv.ClearMemStorage(storage)
    cv.EqualizeHist(grayscale, grayscale)
    cascade = cv.Load(
        '/home/brian/code/twisted-faces/haarcascade_frontalface_default.xml')
    faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2,
                                 cv.CV_HAAR_DO_CANNY_PRUNING)

    #    if faces:
    coords = []
    if faces.total > 0:
        for f in faces:
            #print("[(%d,%d) -> (%d,%d)]" % (f.x, f.y, f.x+f.width, f.y+f.height))
            coords.append({
                'start': {
                    'x': f.x,
                    'y': f.y
                },
                'end': {
                    'x': f.x + f.width,
                    'y': f.y + f.height
                }
            })
    return coords
    def __init__(self, image_name, cascade):
        try:
            image = cv.LoadImage(image_name, 1)
        except IOError:
            return
        except:
            return
        else:
            self.faces = []
            #Allocate Space for grayscale image and tiny image
            #Dramatically reduces computation time in exchange for temporary space
            grayscale = cv.CreateImage((image.width, image.height), 8, 1)
            img = cv.CreateImage((cv.Round(image.width / IMAGE_SCALE),
                                  cv.Round(image.height / IMAGE_SCALE)), 8, 1)

            cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY)
            cv.Resize(grayscale, img, cv.CV_INTER_LINEAR)
            cv.EqualizeHist(img, img)

            matches = cv.HaarDetectObjects(img, cascade,
                                           cv.CreateMemStorage(0), HAAR_SCALE,
                                           IMAGE_SCALE, HAAR_FLAGS, MIN_SIZE)
            for ((x, y, width, height), wat) in matches:
                self.faces.append({
                    "x": x,
                    "y": y,
                    "width": width,
                    "height": height
                })
            self.name = image_name
Beispiel #19
0
def detect_and_draw(img, cascade):
    # allocate temporary images
    gray = cv.CreateImage((img.width, img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(
        img.width / image_scale), cv.Round(img.height / image_scale)), 8, 1)

    # convert color input image to grayscale
    cv.CvtColor(img, gray, cv.CV_BGR2GRAY)

    # scale input image for faster processing
    cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)

    cv.EqualizeHist(small_img, small_img)

    if (cascade):
        t = cv.GetTickCount()
        faces = cv.HaarDetectObjects(small_img, cascade,
                                     cv.CreateMemStorage(0), haar_scale,
                                     min_neighbors, haar_flags, min_size)
        t = cv.GetTickCount() - t
        print "detection time = %gms" % (t / (cv.GetTickFrequency() * 1000.))
        if faces:
            for ((x, y, w, h), n) in faces:
                # the input to cv.HaarDetectObjects was resized, so scale the
                # bounding box of each face and convert it to two CvPoints
                pt1 = (int(x * image_scale), int(y * image_scale))
                pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
                cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)

    cv.ShowImage("result", img)
Beispiel #20
0
def Detect_Face(Img, faceCascade, Give_Image=False):
    # Detects face with haarcascade -- http://www.lucaamore.com/?p=638

    # Set haarcascade object options
    HaarScale = 1.1
    HaarFlags = 0
    MinSize = (20, 20)
    MinNeighbouring = 3

    # Equalize the histogram
    cv.EqualizeHist(Img, Img)

    # Detection of the user's face
    Faces = cv.HaarDetectObjects(Img, faceCascade, cv.CreateMemStorage(0),
                                 HaarScale, MinNeighbouring, HaarFlags,
                                 MinSize)

    # Draw rectangles when found
    if Faces and Give_Image:
        for ((x, y, w, h), n) in Faces:
            # Set two CV Points in order to draw the rectangular box.
            Point1 = (int(x), int(y))
            Point2 = (int(x + w), int(y + h))
            cv.Rectangle(Img, Point1, Point2, cv.RGB(255, 0, 0), 5, 8, 0)

    if Give_Image:
        return Img
    else:
        return Faces
Beispiel #21
0
def detectFaces(im):
    # This function takes a PIL image and finds the patterns defined in the
    # haarcascade function modified from: http://www.lucaamore.com/?p=638

    # Convert a PIL image to a greyscale cv image
    # from: http://pythonpath.wordpress.com/2012/05/08/pil-to-opencv-image/
    im = im.convert('L')
    cv_im = cv.CreateImageHeader(im.size, cv.IPL_DEPTH_8U, 1)
    cv.SetData(cv_im, im.tobytes(), im.size[0])

    # variables
    min_size = (20, 20)
    haar_scale = 1.1
    min_neighbors = 3
    haar_flags = 0

    # Equalize the histogram
    cv.EqualizeHist(cv_im, cv_im)

    # Detect the faces
    faces = cv.HaarDetectObjects(cv_im, faceCascade, cv.CreateMemStorage(0),
                                 haar_scale, min_neighbors, haar_flags,
                                 min_size)

    return faces
Beispiel #22
0
    def detect_faces(self):
        if opencv_available:
            cascade_filename = os.path.join(
                os.path.dirname(__file__), 'face_detection',
                'haarcascade_frontalface_alt2.xml')
            cascade = cv.Load(cascade_filename)
            image = self.opencv_grey_image()

            cv.EqualizeHist(image, image)

            min_size = (40, 40)
            haar_scale = 1.1
            min_neighbors = 3
            haar_flags = 0

            faces = cv.HaarDetectObjects(image, cascade,
                                         cv.CreateMemStorage(0), haar_scale,
                                         min_neighbors, haar_flags, min_size)

            if faces:
                return [
                    FocalPoint.from_square(face[0][0], face[0][1], face[0][2],
                                           face[0][3]) for face in faces
                ]

        return []
Beispiel #23
0
    def detect(self, im):
        ''' Runs the cascade classifer on an image. '''
        image = im.asOpenCV()

        min_size = (self.min_size[0], self.min_size[1])

        # Create a resized gray scale image
        if image.nChannels == 3:
            gray = cv.CreateImage((image.width, image.height), image.depth, 1)
            cv.CvtColor(image, gray, cv.CV_BGR2GRAY)
            image = gray

        image = self._resizeImage(image, self.image_scale)

        # Equalize the image
        cv.EqualizeHist(image, image)

        # Detect faces
        faces = cv.HaarDetectObjects(image, self.cascade, self.storage,
                                     self.haar_scale, self.min_neighbors,
                                     self.haar_flags, min_size)

        # Transform and return the points
        result = []
        for r in faces:
            rect = pv.Rect(r[0][0] / self.image_scale,
                           r[0][1] / self.image_scale,
                           r[0][2] / self.image_scale,
                           r[0][3] / self.image_scale)
            result.append(rect)

        return result
Beispiel #24
0
def detect_and_save(img, cascade, output_name):
    # allocate temporary images
    gray = cv.CreateImage((img.width, img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(
        img.width / image_scale), cv.Round(img.height / image_scale)), 8, 1)

    # convert color input image to grayscale
    cv.CvtColor(img, gray, cv.CV_BGR2GRAY)

    # scale input image for faster processing
    cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)

    cv.EqualizeHist(small_img, small_img)

    if (cascade):
        t = cv.GetTickCount()
        faces = cv.HaarDetectObjects(small_img, cascade,
                                     cv.CreateMemStorage(0), haar_scale,
                                     min_neighbors, haar_flags, min_size)
        t = cv.GetTickCount() - t
        print "detection time = %gms" % (t / (cv.GetTickFrequency() * 1000.))
        f = open(output_name, 'w')
        if faces:
            for ((x, y, w, h), n) in faces:
                f.write("%d %d %d %d\n" % (x, y, w, h))
Beispiel #25
0
 def detect(self, obj, event):
     # First, reset image, in case of previous detections:
     active_handle = self.get_active('Media')
     media = self.dbstate.db.get_media_from_handle(active_handle)
     self.load_image(media)
     min_face_size = (50, 50)  # FIXME: get from setting
     self.cv_image = cv.LoadImage(self.full_path,
                                  cv.CV_LOAD_IMAGE_GRAYSCALE)
     o_width, o_height = self.cv_image.width, self.cv_image.height
     cv.EqualizeHist(self.cv_image, self.cv_image)
     cascade = cv.Load(HAARCASCADE_PATH)
     faces = cv.HaarDetectObjects(self.cv_image, cascade,
                                  cv.CreateMemStorage(0), 1.2, 2,
                                  cv.CV_HAAR_DO_CANNY_PRUNING,
                                  min_face_size)
     references = self.find_references()
     rects = []
     o_width, o_height = [
         float(t) for t in (self.cv_image.width, self.cv_image.height)
     ]
     for ((x, y, width, height), neighbors) in faces:
         # percentages:
         rects.append((x / o_width, y / o_height, width / o_width,
                       height / o_height))
     self.draw_rectangles(rects, references)
Beispiel #26
0
def main():
    global val1, val2
    img = cv.LoadImage(sys.argv[1])
    if img:
        cv.NamedWindow("bar")
        img2 = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 1)
        img21 = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 1)
        img3 = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_16S, 1)
        img4 = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 1)
        cv.CvtColor(img, img2, cv.CV_BGR2GRAY)
        cv.EqualizeHist(img2, img21)
        stor = cv.CreateMemStorage()
        cv.AdaptiveThreshold(img21, img4, 255,
                             cv.CV_ADAPTIVE_THRESH_GAUSSIAN_C,
                             cv.CV_THRESH_BINARY_INV, 7, 7)
        cont = cv.FindContours(img4, stor, cv.CV_RETR_LIST,
                               cv.CV_CHAIN_APPROX_NONE)
        img5 = cv.CloneImage(img)
        while cont:
            if validate_contour(cont):
                cv.DrawContours(img5, cont, (255, 255, 255), (255, 255, 255),
                                0, 2, 8, (0, 0))
            cont = cont.h_next()
        cv.ShowImage("bar", img5)
        cv.WaitKey(0)
Beispiel #27
0
def normalisation(src):

    res = []

    # On fait une copie l'image pour le traitement (en gris)
    gris = cv.CreateImage((src.width, src.height), cv.IPL_DEPTH_8U, 1)
    normal = cv.CreateImage((NORM_W, NORM_H), cv.IPL_DEPTH_8U, 1)
    cv.CvtColor(src, gris, cv.CV_BGR2GRAY)

    # On detecte les visages (objects) sur l'image copiee
    faces = cv.HaarDetectObjects(gris, face_path, cv.CreateMemStorage())
    print "Nombre faces detectes : " + str(len(faces))
    for (x, y, w, h), n in faces:
        tmp = cv.CreateImage((w, h), cv.IPL_DEPTH_8U, 1)
        cv.GetRectSubPix(gris, tmp, (float(x + w / 2), float(y + h / 2)))

        cv.Resize(tmp, normal)
        cv.EqualizeHist(normal, normal)

        #Detection oeil nez bouche sur l'image source:
        d = detection(normal)

        # On detecte au moins 2 yeux "normaux", au moins un oeil avec lunette, au moins une bouche et au moins un nez
        if ((len(d['eyes']) >= 2 or len(d['eyes2']) >= 1)
                and len(d['mouth']) >= 1 and len(d['nose']) >= 1):
            print "Visage detecte dans la photo"
            res.append((normal, (x, y, w, h)))
    return res
def detect(image):
    image_size = cv.GetSize(image)

    # create grayscale version
    grayscale = cv.CreateImage(image_size, 8, 1)
    cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY)

    # create storage
    storage = cv.CreateMemStorage(0)
    # cv.ClearMemStorage(storage)

    # equalize histogram
    cv.EqualizeHist(grayscale, grayscale)

    # detect objects
    cascade = cv.Load('haarcascade_frontalface_alt.xml')
    faces = cv.HaarDetectObjects(
        grayscale,
        cascade,
        storage,
        1.2,
        2,
    )

    if faces:
        print 'face detected!'
        for i in faces:
            print i
            cv.Rectangle(image, (i[0][0], i[0][1]),
                         (int(i[0][0] + i[0][2]), int(i[0][1] + i[0][3])),
                         cv.RGB(0, 255, 0), 3, 8, 0)
Beispiel #29
0
    def get_features(self):
        engine = self.context.modules.engine

        mode, converted_image = engine.image_data_as_rgb(False)
        size = engine.size

        image = cv.CreateImageHeader(size, cv.IPL_DEPTH_8U, 3)
        cv.SetData(image, converted_image)

        gray = cv.CreateImage(size, 8, 1)
        convert_mode = getattr(cv, 'CV_%s2GRAY' % mode)
        cv.CvtColor(image, gray, convert_mode)

        min_size = self.get_min_size_for(size)
        haar_scale = 1.2
        min_neighbors = 3

        cv.EqualizeHist(gray, gray)

        faces = cv.HaarDetectObjects(gray, self.__class__.cascade,
                                     cv.CreateMemStorage(0), haar_scale,
                                     min_neighbors,
                                     cv.CV_HAAR_DO_CANNY_PRUNING, min_size)

        faces_scaled = []

        for ((x, y, w, h), n) in faces:
            # the input to cv.HaarDetectObjects was resized, so scale the
            # bounding box of each face and convert it to two CvPoints
            x2, y2 = (x + w), (y + h)
            faces_scaled.append(((x, y, x2 - x, y2 - y), n))

        return faces_scaled
def detect_and_draw(img, cascade):
    # allocate temporary images
    gray = cv.CreateImage((img.width,img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(img.width / image_scale),
			       cv.Round (img.height / image_scale)), 8, 1)

    # convert color input image to grayscale
    cv.CvtColor(img, gray, cv.CV_BGR2GRAY)

    # scale input image for faster processing
    cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)

    cv.EqualizeHist(small_img, small_img)

    if(cascade):
        objects = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0),
                                     haar_scale, min_neighbors, haar_flags, min_size)
        if objects:
            for ((x, y, w, h), n) in objects:
                # the input to cv.HaarDetectObjects was resized, so scale the 
                # bounding box of each object and convert it to two points
                pt1 = (int(x * image_scale), int(y * image_scale))
                pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))

                # check the diagonal of the bounding box is within max_size    
                if w*image_scale<max_size[0] and h*image_scale<max_size[1] :
                    cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)

    cv.ShowImage("result", img)