def draw(self, img, pixmapper, bounds):
        '''draw the icon on the image'''

        if self.hidden:
            return

        if self.trail is not None:
            self.trail.draw(img, pixmapper, bounds)

        icon = self.img()
        (px, py) = pixmapper(self.latlon)

        # find top left
        px -= icon.width / 2
        py -= icon.height / 2
        w = icon.width
        h = icon.height

        (px, py, sx, sy, w, h) = self.clip(px, py, w, h, img)

        cv.SetImageROI(icon, (sx, sy, w, h))
        cv.SetImageROI(img, (px, py, w, h))
        cv.Add(icon, img, img)
        cv.ResetImageROI(img)
        cv.ResetImageROI(icon)

        # remember where we placed it for clicked()
        self.posx = px + w / 2
        self.posy = py + h / 2
Beispiel #2
0
	def area_to_image(self, lat, lon, width, height, ground_width, zoom=None, ordered=True):
		'''return an RGB image for an area of land, with ground_width
		in meters, and width/height in pixels.

		lat/lon is the top left corner. The zoom is automatically
		chosen to avoid having to grow the tiles'''

		img = cv.CreateImage((width,height),8,3)

		tlist = self.area_to_tile_list(lat, lon, width, height, ground_width, zoom)

		# order the display by distance from the middle, so the download happens
		# close to the middle of the image first
		if ordered:
			(midlat, midlon) = self.coord_from_area(width/2, height/2, lat, lon, width, ground_width)
			tlist.sort(key=lambda d: d.distance(midlat, midlon), reverse=True)

		for t in tlist:
			scaled_tile = self.scaled_tile(t)

			w = min(width - t.dstx, scaled_tile.width - t.srcx)
			h = min(height - t.dsty, scaled_tile.height - t.srcy)
			if w > 0 and h > 0:
				cv.SetImageROI(scaled_tile, (t.srcx, t.srcy, w, h))
				cv.SetImageROI(img, (t.dstx, t.dsty, w, h))
				cv.Copy(scaled_tile, img)
				cv.ResetImageROI(img)
				cv.ResetImageROI(scaled_tile)

		# return as an RGB image
		cv.CvtColor(img, img, cv.CV_BGR2RGB)
		return img
    def sample_frame(self, frame):
        # Get an average of the green channel in on the forehead
        cv.SetImageROI(frame, self.face_tracker.get_forehead())
        sample = cv.Avg(frame)[1]
        cv.ResetImageROI(frame)

        return sample
Beispiel #4
0
    def load_tile_lowres(self, tile):
        '''load a lower resolution tile from cache to fill in a
		map while waiting for a higher resolution tile'''
        if tile.zoom == self.min_zoom:
            return None

        # find the equivalent lower res tile
        (lat, lon) = tile.coord()

        width2 = TILES_WIDTH
        height2 = TILES_HEIGHT

        for zoom2 in range(tile.zoom - 1, self.min_zoom - 1, -1):
            width2 /= 2
            height2 /= 2

            if width2 == 0 or height2 == 0:
                break

            tile_info = self.coord_to_tile(lat, lon, zoom2)

            # see if its in the tile cache
            key = tile_info.key()
            if key in self._tile_cache:
                img = self._tile_cache[key]
                if img == self._unavailable:
                    continue
            else:
                path = self.tile_to_path(tile_info)
                try:
                    img = cv.LoadImage(path)
                    # add it to the tile cache
                    self._tile_cache[key] = img
                    while len(self._tile_cache) > self.cache_size:
                        self._tile_cache.popitem(0)
                except IOError as e:
                    continue

            # copy out the quadrant we want
            availx = min(TILES_WIDTH - tile_info.offsetx, width2)
            availy = min(TILES_HEIGHT - tile_info.offsety, height2)
            if availx != width2 or availy != height2:
                continue
            cv.SetImageROI(
                img, (tile_info.offsetx, tile_info.offsety, width2, height2))
            img2 = cv.CreateImage((width2, height2), 8, 3)
            try:
                cv.Copy(img, img2)
            except Exception:
                continue
            cv.ResetImageROI(img)

            # and scale it
            scaled = cv.CreateImage((TILES_WIDTH, TILES_HEIGHT), 8, 3)
            cv.Resize(img2, scaled)
            #cv.Rectangle(scaled, (0,0), (255,255), (0,255,0), 1)
            return scaled
        return None
Beispiel #5
0
    def crop(self, image, subRect):

        cv.SetImageROI(image, subRect)
        img2 = cv.CreateImage(cv.GetSize(image), image.depth, image.nChannels)
        #img3 = cv.CreateImage(cv.GetSize(image), image.depth, image.nChannels);
        cv.Copy(image, img2)
        #img3 = self.equilizer.normalize(img2)
        cv.ResetImageROI(image)
        return img2
def DetectRedEyes(image, faceCascade, eyeCascade):
	min_size = (20,20)
	image_scale = 2
	haar_scale = 1.2
	min_neighbors = 2
	haar_flags = 0

	# Allocate the temporary images
	gray = cv.CreateImage((image.width, image.height), 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, gray, cv.CV_BGR2GRAY)

	# Scale input image for faster processing
	cv.Resize(gray, 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:
		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), 3, 8, 0)
			face_region = cv.GetSubRect(image,(x,int(y + (h/4)),w,int(h/2)))

		cv.SetImageROI(image, (pt1[0],
			pt1[1],
			pt2[0] - pt1[0],
			int((pt2[1] - pt1[1]) * 0.7)))
		eyes = cv.HaarDetectObjects(image, eyeCascade,
		cv.CreateMemStorage(0),
		haar_scale, min_neighbors,
		haar_flags, (15,15))	

		if eyes:
			# For each eye found
			for eye in eyes:
				# Draw a rectangle around the eye
				cv.Rectangle(image,
				(eye[0][0],
				eye[0][1]),
				(eye[0][0] + eye[0][2],
				eye[0][1] + eye[0][3]),
				cv.RGB(255, 0, 0), 1, 8, 0)

	cv.ResetImageROI(image)
	return image
Beispiel #7
0
def DetectRedEyes(image, faceCascade):
    min_size = (20, 20)
    image_scale = 2
    haar_scale = 1.1
    min_neighbors = 2
    haar_flags = 0

    # Allocate the temporary images
    gray = cv.CreateImage((image.width, image.height), 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, gray, cv.CV_BGR2GRAY)

    # Scale input image for faster processing
    cv.Resize(gray, 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:

        #print 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
            #print "face"
            global line2
            line2 = n
            pt1 = (int(x * image_scale), int(y * image_scale))
            pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
            # print pt1
            # print pt2
            cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 1, 8, 0)
            cv.PutText(image, "face" + str(h), pt1, font, cv.RGB(255, 0, 0))
            cv.PutText(image, "Come close.", (0, 20), font, cv.RGB(255, 0, 0))
            cv.PutText(image, "Ensure your forehead is well lit.", (0, 40),
                       font, cv.RGB(255, 0, 0))
            cv.PutText(image, "Hit escape when done.", (0, 60), font,
                       cv.RGB(255, 0, 0))

    cv.ResetImageROI(image)
    return image
Beispiel #8
0
    def draw(self, img, pixmapper, bounds):
        '''draw the thumbnail on the image'''
        thumb = self.img()
        (px,py) = pixmapper(self.latlon)

        # find top left
        px -= thumb.width/2
        py -= thumb.height/2
        w = thumb.width
        h = thumb.height

        (px, py, sx, sy, w, h) = self.clip(px, py, w, h, img)

        cv.SetImageROI(thumb, (sx, sy, w, h))
        cv.SetImageROI(img, (px, py, w, h))
        cv.Copy(thumb, img)
        cv.ResetImageROI(img)
        cv.ResetImageROI(thumb)

        # remember where we placed it for clicked()
        self.posx = px+w/2
        self.posy = py+h/2
Beispiel #9
0
def blend_views(bldIm, frame, mask, frec, max_rec):
    maskMat = cv.fromarray(mask)

    dispX = int(np.round(frec.left - max_rec.left))
    dispY = int(np.round(frec.top - max_rec.top))

    bldROI = cv.GetImage(bldIm)

    cv.SetImageROI(bldROI, (dispX, dispY, int(np.round(
        frec.width())), int(np.round(frec.height()))))
    cv.Add(bldROI, cv.fromarray(frame), bldROI, maskMat)
    cv.ResetImageROI(bldROI)
    cv.Copy(bldROI, bldIm)

    return bldIm
Beispiel #10
0
	def addText(self, frame, textTop, textBottom):
		s = cv.GetSize(frame)
		offset = 8
		
		## add space for text notations
		textSize = cv.GetTextSize(textTop, self._font)
		textframe = cv.CreateImage( (s[0], s[1] + 4*textSize[1] + 2*offset), frame.depth, frame.channels)
		cv.Set(textframe, 0)
		cv.SetImageROI(textframe, (0, 2*textSize[1] + offset, s[0], s[1]))
		cv.Copy(frame, textframe)
		cv.ResetImageROI(textframe)
				
		## write text
		cv.PutText(textframe, textTop, (5, 2*textSize[1] + offset/2), self._font, self._fontcolor)
		cv.PutText(textframe, textBottom, (5, int(s[1] + 4*textSize[1] + 1.5 * offset)), self._font, self._fontcolor)
		
		return textframe
Beispiel #11
0
def detect_and_draw(img, cascade, mask):
    # 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:

                # Affichage du carré de recherche
                xmoustache = int((x * image_scale) + w * 0.5)
                ymoustache = int((y * image_scale) + h * 1.25)
                wmoustache = int(w * 0.5 * image_scale)
                hmoustache = int(h * 0.19 * image_scale)
                img_mask = cv.CreateImage((wmoustache, hmoustache), mask.depth,
                                          mask.nChannels)
                cv.SetImageROI(
                    img, (xmoustache, ymoustache, wmoustache, hmoustache))
                cv.Resize(mask, img_mask, cv.CV_INTER_LINEAR)

                # Affichage du carré de recherche
                cv.Sub(img, img_mask, img)
                cv.ResetImageROI(img)
                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)
import cv2.cv as cv

im = cv.LoadImage("../img/lena.jpg", 3)

cv.SetImageROI(im, (50, 50, 150, 150))

cv.Zero(im)
#cv.Set(im, cv.RGB(100, 100, 100))

cv.ResetImageROI(im)

cv.ShowImage("Image", im)

cv.WaitKey(0)
Beispiel #13
0
def DetectRedEyes(image, faceCascade, smileCascade):
    min_size = (20, 20)
    image_scale = 2
    haar_scale = 1.2
    min_neighbors = 2
    haar_flags = 0

    # Allocate the temporary images
    gray = cv.CreateImage((image.width, image.height), 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, gray, cv.CV_BGR2GRAY)

    # Scale input image for faster processing
    cv.Resize(gray, 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:

        #print 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
            #print "face"
            pt1 = (int(x * image_scale), int(y * image_scale))
            pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
            # print pt1
            # print pt2
            #cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 1, 8, 0)
            #cv.PutText(image, "face", pt1, font, cv.RGB(255, 0, 0))
            face_region = cv.GetSubRect(image,
                                        (x, int(y + (h / 4)), w, int(h / 2)))

            #split face
            #cv.Rectangle(image, (pt1[0],(pt1[1] + (abs(pt1[1]-pt2[1]) / 2 ))), pt2, cv.RGB(0,255,0), 1, 8, 0)
            #cv.PutText(image, "lower", (pt1[0],(pt1[1] + (abs(pt1[1]-pt2[1]) / 2 ))), font, cv.RGB(0, 255, 0))
            cv.SetImageROI(
                image, (pt1[0],
                        (pt1[1] + (abs(pt1[1] - pt2[1]) / 2)), pt2[0] - pt1[0],
                        int((pt2[1] - (pt1[1] + (abs(pt1[1] - pt2[1]) / 2))))))

            smiles = cv.HaarDetectObjects(image, smileCascade,
                                          cv.CreateMemStorage(0), 1.1, 5, 0,
                                          (15, 15))

            if smiles:
                #print smiles

                for smile in smiles:
                    cv.Rectangle(
                        image, (smile[0][0], smile[0][1]),
                        (smile[0][0] + smile[0][2], smile[0][1] + smile[0][3]),
                        cv.RGB(0, 0, 255), 1, 8, 0)

                    cv.PutText(image, "smile", (smile[0][0], smile[0][1]),
                               font, cv.RGB(0, 0, 255))

                    cv.PutText(image, str(smile[1]),
                               (smile[0][0], smile[0][1] + smile[0][3]), font,
                               cv.RGB(0, 0, 255))
                    #print ((abs(smile[0][1] - smile[0][2]) / abs(pt1[0] - pt2[0])) * 100)

                    global smileness
                    smileness = smile[1]
            cv.ResetImageROI(image)
            #if smile[1] > 90:
            #    mqttc.publish("smiles", "got smile", 1)
            #    time.sleep(5)

        #eyes = cv.HaarDetectObjects(image, eyeCascade,
        #cv.CreateMemStorage(0),
        #haar_scale, min_neighbors,
        #haar_flags, (15,15))

        #if eyes:
        # For each eye found

        #print eyes

        #for eye in eyes:
        # Draw a rectangle around the eye
        #   cv.Rectangle(image,
        #   (eye[0][0],
        #   eye[0][1]),
        #   (eye[0][0] + eye[0][2],
        #   eye[0][1] + eye[0][3]),
        #   cv.RGB(255, 0, 0), 1, 8, 0)

    cv.ResetImageROI(image)
    return image
Beispiel #14
0
    cv.Sobel(grayIPimage,sobel,2,0,7)         # 进行x方向的sobel检测
    temp  = cv.CreateImage(cv.GetSize(sobel),cv2.IPL_DEPTH_8U, 1)       #图像格式转换回8位深度已进行下一步处理
    cv.ConvertScale(sobel, temp,0.00390625, 0)
    cv.Threshold(temp, temp, 0, 255, cv2.THRESH_OTSU)
    kernal = cv.CreateStructuringElementEx(3,1, 1, 0, 0)
    cv.Dilate(temp, temp,kernal,2)
    cv.Erode(temp, temp,kernal,4)
    cv.Dilate(temp, temp,kernal,2)
#     cv.ShowImage('1', temp)
    kernal = cv.CreateStructuringElementEx(1,3, 0, 1, 0)
    cv.Erode(temp, temp,kernal,1)
    cv.Dilate(temp, temp,kernal,3)
#     cv.ShowImage('2', temp)
    temp = np.asarray(cv.GetMat(temp))
    contours, heirs  = cv2.findContours(temp,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    
    for tours in contours:
        rc = cv2.boundingRect(tours)
        if rc[2]/rc[3] >= 2:
            #rc[0] 表示图像左上角的纵坐标,rc[1] 表示图像左上角的横坐标,rc[2] 表示图像的宽度,rc[3] 表示图像的高度,
            cv2.rectangle(image, (rc[0],rc[1]),(rc[0]+rc[2],rc[1]+rc[3]),(255,0,255))
            imageIp = cv.GetImage(cv.fromarray(image))
            cv.SetImageROI(imageIp, rc)
            imageCopy = cv.CreateImage((rc[2], rc[3]),cv2.IPL_DEPTH_8U, 3)
            cv.Copy(imageIp, imageCopy)
            cv.ResetImageROI(imageIp)
            cv.SaveImage('D:/pic/result/' + str(i) + '.jpg',imageCopy)
            i = i +1
# cv2.imshow("黑底白字",image)      
cv2.waitKey(0)      #暂停用于显示图片
Beispiel #15
0
    frame = cv.QueryFrame(camera)
    cv.Smooth(frame, frame, cv.CV_BLUR, 3)
    cv.Flip(frame, frame, 1)

    #drawing the rectangle and writing the text
    temp1 = cv.CloneImage(frame)
    cv.Rectangle(temp1, point1, point2, color, 1)
    cv.PutText(temp1, "Place in box", (430, 240), font, color)
    cv.PutText(temp1, "then hit q", (430, 260), font, color)

    #taking snapshot after q is pressed
    if cv.WaitKey(10) == 113:
        flag = 1
        cv.SetImageROI(temp1, (300, 200, 100, 100))
        template = cv.CloneImage(temp1)
        cv.ResetImageROI(temp1)
        cv.DestroyWindow("Image")

    if flag == 0:
        cv.NamedWindow("Image", 1)
        cv.MoveWindow("Image", 300, 0)
        cv.ShowImage("Image", temp1)
        continue

    W, H = cv.GetSize(frame)
    w, h = cv.GetSize(template)

    width = W - w + 1
    height = H - h + 1
    result = cv.CreateImage((width, height), 32, 1)
    #matching the template by searching for the given region in the image
def DetectRedEyes(image, faceCascade, smileCascade, eyeCascade):
    min_size = (20,20)
    image_scale = 2
    haar_scale = 1.1
    min_neighbors = 2
    haar_flags = 0

    # Allocate the temporary images
    gray = cv.CreateImage((image.width, image.height), 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, gray, cv.CV_BGR2GRAY)

    # Scale input image for faster processing
    cv.Resize(gray, 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)
    global norm
    # If faces are found
    if faces:
        
        #print faces
        ratio = 1.
        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
            #print "face"
            if h!=0:
                ratio = h/norm

            pt1 = (int(x * image_scale), int(y * image_scale))
            pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
            # print pt1
            # print pt2
            #cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 1, 8, 0)
            #cv.PutText(image, "face"+str(h), pt1, font, cv.RGB(255, 0, 0))
            face_region = cv.GetSubRect(image,(x,int(y + (h/4)),w,int(h/2)))

            #split face
            #cv.Rectangle(image, (pt1[0],(pt1[1] + (abs(pt1[1]-pt2[1]) / 2 ))), pt2, cv.RGB(0,255,0), 1, 8, 0)
            #cv.PutText(image, "lower", (pt1[0],(pt1[1] + (abs(pt1[1]-pt2[1]) / 2 ))), font, cv.RGB(0, 255, 0))
            cv.SetImageROI(image, (pt1[0],
                               (pt1[1] + int(abs(pt1[1]-pt2[1]) * 0.625 )),
                               pt2[0] - pt1[0],
                               int((pt2[1] - (pt1[1] + int(abs(pt1[1]-pt2[1]) * 0.625 ))))))
            
            smiles = cv.HaarDetectObjects(image, smileCascade, cv.CreateMemStorage(0), 1.1, 5, 0, (15,15))
        
            if smiles:
                #print smiles          
                for smile in smiles:
                    cv.Rectangle(image,
                    (smile[0][0],smile[0][1]),
                    (smile[0][0] + smile[0][2], smile[0][1] + smile[0][3]),
                    cv.RGB(0, 0, 255), 1, 8, 0)
                    sizer = (smile[0][2]/ratio+smile[0][3]/ratio)#+(smile[1]/ratio))
                    #sizer = math.trunc(sizer)
                    #cv.PutText(image, "smile", (smile[0][0],smile[0][1]), font, cv.RGB(0, 0, 255))

                    cv.PutText(image,str(math.trunc(sizer**2)), (smile[0][0], smile[0][1] + smile[0][3] + 10), font, cv.RGB(0, 0, 255))
                    #print ((abs(smile[0][1] - smile[0][2]) / abs(pt1[0] - pt2[0])) * 100) 
                    
                    global smileneighbour 
                    smileneighbour = sizer**2*2
            cv.ResetImageROI(image)
            #############################################################################
            #############################################################################
            cv.SetImageROI(image, (pt1[0], pt1[1], int(pt2[0]-pt1[0]), int(pt2[1] - pt1[1])) )
            eyes = cv.HaarDetectObjects(image, eyeCascade,cv.CreateMemStorage(0),haar_scale, 5,haar_flags, (15,15))
            if eyes:
                # For each eye found
                iii = 0
                #print eyes
                for eye in eyes:
                    # Draw a rectangle around the eye
                   cv.Rectangle(image,(eye[0][0],eye[0][1]),(eye[0][0] + eye[0][2],eye[0][1] + eye[0][3]), cv.RGB(0, 0, 255), 1, 8, 0)
                   a = math.trunc(float(eye[1])/ratio)
                   cv.PutText(image,str(a), (eye[0][0], eye[0][1] + eye[0][3]), font, cv.RGB(0, 0, 255))
                   global eyetot
                   eyetot += float(eye[1]*eye[1])/ratio
                   iii+=1
                   if iii==2:
                       iii = 0
                       break
            cv.ResetImageROI(image)
    cv.ResetImageROI(image)
    return image
Beispiel #17
0
    def detectFace(self, cam_img, faceCascade, eyeCascade,
                   mouthCascade):  #cam_img should be cv2.cv.iplcam_img
        min_size = (20, 20)
        image_scale = 2
        haar_scale = 1.2
        min_neighbors = 2
        haar_flags = 0
        image_width = int(cam_img.get(cv.CV_CAP_PROP_FRAME_WIDTH))
        image_height = int(cam_img.get(cv.CV_CAP_PROP_FRAME_HEIGHT))
        # Allocate the temporary images
        gray = cv.CreateImage((image_width, image_height), 8,
                              1)  #tuple as the first arg
        smallImage = cv.CreateImage((cv.Round(
            image_width / image_scale), cv.Round(image_height / image_scale)),
                                    8, 1)

        (ok, img) = cam_img.read()
        #print 'gray is of ',type(gray) >>> gray is of  <type 'cv2.cv.iplimage'>
        #print type(smallImage)  >>> <type 'cv2.cv.iplimage'>
        #print type(image) >>> <type 'cv2.VideoCapture'>
        #print type(img) >>> <type 'numpy.ndarray'>

        #convert numpy.ndarray to iplimage
        ipl_img = cv2.cv.CreateImageHeader((img.shape[1], img.shape[0]),
                                           cv.IPL_DEPTH_8U, 3)
        cv2.cv.SetData(ipl_img, img.tostring(),
                       img.dtype.itemsize * 3 * img.shape[1])

        # Convert color input image to grayscale
        cv.CvtColor(ipl_img, gray, cv.CV_BGR2GRAY)

        # Scale input image for faster processing
        cv.Resize(gray, 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)
        # => The function returns a list of tuples, (rect, neighbors) , where rect is a CvRect specifying the object’s extents and neighbors is a number of neighbors.
        # => CvRect cvRect(int x, int y, int width, int height)
        # If faces are found
        if faces:
            face = faces[0]
            self.faceX = face[0][0]
            self.faceY = face[0][1]

            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(ipl_img, pt1, pt2, cv.RGB(0, 0, 255), 3, 8, 0)
                #face_region = cv.GetSubRect(ipl_img,(x,int(y + (h/4)),w,int(h/2)))

            cv.SetImageROI(
                ipl_img,
                (pt1[0], pt1[1], pt2[0] - pt1[0], int(
                    (pt2[1] - pt1[1]) * 0.7)))

            eyes = cv.HaarDetectObjects(ipl_img, eyeCascade,
                                        cv.CreateMemStorage(0), haar_scale,
                                        min_neighbors, haar_flags, (15, 15))

            if eyes:
                # For each eye found
                for eye in eyes:

                    # Draw a rectangle around the eye
                    cv.Rectangle(
                        ipl_img,  #image
                        (
                            eye[0][0],  #vertex pt1
                            eye[0][1]),
                        (
                            eye[0][0] + eye[0][2],  #vertex pt2 opposite to pt1
                            eye[0][1] + eye[0][3]),
                        cv.RGB(255, 0, 0),
                        1,
                        4,
                        0)  #color,thickness,lineType(8,4,cv.CV_AA),shift

        cv.ResetImageROI(ipl_img)

        return ipl_img
Beispiel #18
0
        else:
            y_flag = 0
        if pre_y_flag != y_flag:
            h_pos.append(y)

match_img = []
tmp_mg = []

for i in range(0, leng / 2):
    cv2.waitKey(3)
    cv2.rectangle(image, (w_pos[i], h_pos[i]), (w_pos[i + 1], h_pos[i + 1]),
                  (255, 255, 0))
    imgIP = cv.GetImage(cv.fromarray(image))
    cv.SetImageROI(imgIP, (w_pos[2 * i], h_pos[2 * i], w_pos[2 * i + 1] -
                           w_pos[2 * i], h_pos[2 * i + 1] - h_pos[2 * i]))
    img_cache = cv.CreateImage(
        (w_pos[2 * i + 1] - w_pos[2 * i], h_pos[2 * i + 1] - h_pos[2 * i]),
        cv.IPL_DEPTH_8U, 3)
    cv.Copy(imgIP, img_cache)
    match_img.append(img_cache)
    #    cv.SaveImage('ss'+bytes(i)+'.jpg',match_img[i])
    cv.ResetImageROI(imgIP)
#    cv.ShowImage('window'+bytes(i),match_img[i])

#aa=image.copy((range(10,20),range(20,100)))
#cv2.imshow("hello",aa)
#cv2.imshow("img",image)
print w_pos
print h_pos
cv2.waitKey(0)