예제 #1
0
    def backProject(self):
        work = self.work1[0]
        #cv.Smooth(self.hsvImage,self.hsvImage,CVtypes.CV_GAUSSIAN,7,7);
        cv.InRangeS(self.hsvImage, cv.Scalar(0, 30, 200, 0),
                    cv.Scalar(180, 256, 256, 0), self.maskImage)

        cv.InRangeS(self.hsvImage, cv.Scalar(0, 30, 100, 0),
                    cv.Scalar(180, 256, 256, 0), work)

        if self.queHistSnap:
            cv.CalcHist([self.hsvPlanes[0]], self.histogram, 0, self.maskImage)
            values = cv.GetMinMaxHistValue(self.histogram)
            self.histSnapTaken = True
            self.queHistSnap = False
        if self.histSnapTaken:
            cv.CalcBackProject([self.hsvPlanes[0]], self.backprojectImage,
                               self.histogram)
            cv.And(self.backprojectImage, work, self.backprojectImage, 0)
        if self.displayType == 'color':
            cv.Copy(self.image, self.modifiedImage)
        elif self.displayType == 'back':
            cv.CvtColor(self.backprojectImage, self.modifiedImage,
                        CVtypes.CV_GRAY2RGB)
        elif self.displayType == 'gray':
            cv.CvtColor(self.grayImage, self.modifiedImage,
                        CVtypes.CV_GRAY2RGB)
        elif self.displayType == 'mask':
            cv.CvtColor(self.maskImage, self.modifiedImage,
                        CVtypes.CV_GRAY2RGB)

        self.getContours(self.backprojectImage)
예제 #2
0
def detect(image):
    image_size = cv.GetSize(image)

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

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

    # equalize histogram
    cv.EqualizeHist(grayscale, grayscale)

    # detect objects
    cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml',
                                           cv.Size(1, 1))
    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)
예제 #3
0
    def preRender(self):
        frame = self.getFrame()
        capture = self.getCapture()
        #print _AR.GetMultiMV('data/19_20_21_22_23_24.cfg')
        #_AR.GetMultiMV('data/13_14_15_16_17_18.cfg')
        #if 'darwin' in os.uname()[0].lower():
        if 0:
            settings = [
                cv.GetCaptureProperty(capture, CVtypes.CV_CAP_PROP_FOURCC),
                cv.GetCaptureProperty(capture, CVtypes.CV_CAP_PROP_BRIGHTNESS),
                cv.GetCaptureProperty(capture, CVtypes.CV_CAP_PROP_CONTRAST),
                cv.GetCaptureProperty(capture, CVtypes.CV_CAP_PROP_SATURATION),
                cv.GetCaptureProperty(capture, CVtypes.CV_CAP_PROP_HUE),
            ]
            print settings

        size = cv.GetSize(frame)
        fsize = size.width, size.height
        size = cv.GetSize(self.image)
        isize = size.width, size.height
        if fsize == isize:
            cv.Copy(frame, self.image)
        else:
            cv.Resize(
                frame,
                self.image,
                CVtypes.CV_INTER_LINEAR,
                #CVtypes.CV_INTER_CUBIC,
            )

        cv.CvtColor(self.image, self.hsvImage, CVtypes.CV_RGB2HSV)
        cv.Split(self.hsvImage, self.hsvPlanes[0], self.hsvPlanes[1],
                 self.hsvPlanes[2], 0)
        cv.CvtColor(self.image, self.grayImage, CVtypes.CV_RGB2GRAY)
        if 1:
            #cv.EqualizeHist(self.hsvPlanes[0],self.hsvPlanes[0])
            #cv.EqualizeHist(self.hsvPlanes[2],self.hsvPlanes[2])
            #cv.Merge(self.hsvPlanes[0],self.hsvPlanes[1],
            #         self.hsvPlanes[2],0, self.hsvImage)
            #cv.CvtColor(self.hsvImage,self.image,CVtypes.CV_HSV2RGB)
            cv.EqualizeHist(self.grayImage, self.grayImage)
        cv.Split(self.image, self.rgbPlanes[0], self.rgbPlanes[1],
                 self.rgbPlanes[2], 0)
        cv.Copy(self.image, self.modifiedImage)
예제 #4
0
    def detect(self, image):
        size = cv.GetSize(image)

        # create grayscale version
        grayscale = cv.CreateImage(size, 8, 1)
        cv.CvtColor(image, grayscale, cv.BGR2GRAY)

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

        # equalize histogram
        cv.EqualizeHist(grayscale, grayscale)

        # detect faces
        faces = cv.HaarDetectObjects(grayscale, self.face_cascade, storage,
                                     1.2, 2, cv.HAAR_DO_CANNY_PRUNING,
                                     self.face_size)

        if faces:
            # faces 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)

            detected = True
            is_face = True
        else:
            # detect body
            bodies = cv.HaarDetectObjects(grayscale, self.body_cascade,
                                          storage, 1.1, 3, 0, self.body_size)

            if bodies:
                # body detected
                for i in bodies:
                    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)

                detected = True
                is_face = False
            else:
                detected = False
                is_face = False

        # release resources we don't need any more
        cv.ReleaseImage(grayscale)
        cv.ReleaseMemStorage(storage)

        return (detected, is_face)
예제 #5
0
def detect_and_draw(img ,cascade):
    global age
    global trackedFaces
    global plotpoints

    t = cv.GetTickCount() ## start counter    
    cv.CvtColor( img, gray, cv.BGR2GRAY )
    cv.Resize( gray, small_img, cv.INTER_LINEAR )
    cv.ClearMemStorage( storage )

    #Ages all trackedFaces
    for f in trackedFaces:
        f.updateLife()
    #Remove expired faces
    for f in trackedFaces:
        if (f.isTooOld()):
            trackedFaces.remove(f)
    
    faces = cv.HaarDetectObjects( small_img, cascade, storage, haar_scale, min_neighbors, haar_flags, min_size )
    drawline = 0
    if faces:
        #found a face
        for r in faces:
            matchedFace = False;
            pt1 = cv.Point( int(r.x*image_scale), int(r.y*image_scale))
            pt2 = cv.Point( int((r.x+r.width)*image_scale), int((r.y+r.height)*image_scale) )
            
            #check if there are trackedFaces
            if (len(trackedFaces) > 0):
                #each face being tracked
                for f in trackedFaces:
                    #the face is found (small movement)
                    if ((abs(f.xpt - pt1.x) < FACE_MAX_MOVEMENT) and (abs(f.ypt - pt1.y) < FACE_MAX_MOVEMENT)):
                        matchedFace = True;
                        f.updateFace(int(r.width*image_scale), int(r.height*image_scale), pt1.x, pt1.y);
                        #f.updateFace(r.width*image_scale, r.height*image_scale, pt1.x, pt1.y);
                        mf = f;
                        break;
                        
                #if face not found, add a new face
                if (matchedFace == False):
                    f = Face(0,int(r.width*image_scale), int(r.height*image_scale), pt1.x, pt1.y,0);
                    trackedFaces.append(f);
                    mf = f;
            #No tracked faces: adding one                            
            else:
                f = Face(0,int (r.width*image_scale), int (r.height*image_scale), pt1.x, pt1.y,0);
                trackedFaces.append(f);
                mf = f;
            #where to draw face and properties
            if (mf.age > 5):
                #draw attention line
                lnpt1 = cv.Point (int (mf.xpt*scale), int(mf.ypt*scale-5)-5)
                if (mf.age > mf.width):
                    lnpt2 = cv.Point (int (mf.xpt*scale+mf.width), int(mf.ypt*scale-5))
                else:
                    lnpt2 = cv.Point (int (mf.xpt*scale+mf.age), int(mf.ypt*scale-5))
                #cv.Line(img, lnpt1, lnpt2, RED, 2, 8, 0) ## drawing attention line
                cv.Rectangle(img, lnpt1, lnpt2, RED, 4, 8, 0) ## drawing bolded attention line
                
                ### draw eyes
                cv.Rectangle(img, mf.eyeLeft1, mf.eyeLeft2, MAGENTA, 3,8,0)
                cv.Rectangle(img, mf.eyeRight1, mf.eyeRight2, MAGENTA, 3,8,0)
                #
                ### draw mouth
                cv.Rectangle(img, mf.mouthTopLeft, mf.mouthBotRight, ORANGE, 3, 8, 0)
                #
                ### draw face
                cv.Rectangle( img, pt1, pt2, getColor(mf), 3, 8, 0 )
                drawline = mf.age
                
    if(CAPTURING): saveAsJPG(img) 
    if (osName == "nt"): cv.Flip(img, img, 0)
    cv.ShowImage ('Camera', img)
    t = cv.GetTickCount() - t ## counter for FPS
    print "%i fps." % (cv.GetTickFrequency()*1000000./t) ## print FPS
예제 #6
0
def detect(image):
    image_size = cv.GetSize(image)

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

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

    # equalize histogram
    cv.EqualizeHist(grayscale, grayscale)

    # detect objects
    cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml',
                                           cv.Size(1, 1))
    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)

    # create windows
    cv.NamedWindow('Camera', cv.WINDOW_AUTOSIZE)

    # create capture device
    device = 0  # assume we want first device
    capture = cv.CreateCameraCapture(0)
    cv.SetCaptureProperty(capture, cv.CAP_PROP_FRAME_WIDTH, 640)
    cv.SetCaptureProperty(capture, cv.CAP_PROP_FRAME_HEIGHT, 480)

    # check if capture device is OK
    if not capture:
        print "Error opening capture device"
        sys.exit(1)

    while 1:
        # do forever

        # capture the current frame
        frame = cv.QueryFrame(capture)
        if frame is None:
            break

        # mirror
        cv.Flip(frame, None, 1)

        # face detection
        detect(frame)
        # display webcam image
        cv.ShowImage('Camera', frame)

        # handle events
        k = cv.WaitKey(10)

        if k == 0x1b:  # ESC
            print 'ESC pressed. Exiting ...'
            break
예제 #7
0
    hist = cv.CreateHist(1, [hdims], cv.HIST_ARRAY, hranges, 1)

    while 1:
        # do forever

        # 1. capture the current image
        frame = cv.QueryFrame(capture)
        if frame is None:
            # no image captured... end the processing
            break

        # mirror the captured image
        cv.Flip(frame, None, 1)

        # compute the hsv version of the image
        cv.CvtColor(frame, hsv, cv.BGR2HSV)

        # compute which pixels are in the wanted range
        cv.InRangeS(hsv, hsv_min, hsv_max, mask)

        # extract the hue from the hsv array
        cv.Split(hsv, hue, None, None, None)

        # select the rectangle of interest in the hue/mask arrays
        cv.SetImageROI(hue, selection)
        cv.SetImageROI(mask, selection)

        # it's time to compute the histogram
        cv.CalcHist([hue], hist, 0, mask)

        # clear the ROIs