Exemple #1
0
 def draw_label(self, image):
     for detect_object in self.objects:
         name = detect_object.get("name", "")
         color = detect_object.get("color", "")
         font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0.0, 2,
                            cv.CV_AA)
         text_size, ymin = cv.GetTextSize(name, font)
         for position in detect_object.get("positions"):
             # confidence = position.get("confidence", 0)
             # name = "%s: %d" % (name, confidence)
             # name += "%"
             xmin = position.get("xmin", "")
             xmax = position.get("xmax", "")
             ymin = position.get("ymin", "")
             ymax = position.get("ymax", "")
             cv2.cv.Rectangle(image, (xmin, ymin), (xmax, ymax), color, 4)
             cv2.cv.Rectangle(image, (xmin, ymin - text_size[1] - 10),
                              (xmin + text_size[0], ymin), color,
                              cv.CV_FILLED)
             cv2.cv.PutText(image, name, (xmin, ymin - 10), font, (0, 0, 0))
 def __init__(self, videoFile, gTruthXmlFile, outputXml, platesDir):
     self.xml = XmlWriter(outputXml)
     self.gtruth = XmlGTruthReader(gTruthXmlFile)
     self.capture = cv.CaptureFromFile(videoFile)
     cv.NamedWindow("Video", cv.CV_WINDOW_AUTOSIZE)
     self.w = cv.GetCaptureProperty(self.capture,
                                    cv.CV_CAP_PROP_FRAME_WIDTH)
     self.h = cv.GetCaptureProperty(self.capture,
                                    cv.CV_CAP_PROP_FRAME_HEIGHT)
     self.nFrames = cv.GetCaptureProperty(self.capture,
                                          cv.CV_CAP_PROP_FRAME_HEIGHT)
     print('Total number of Frames in video: ' + str(self.nFrames))
     self.font = cv.InitFont(cv.CV_FONT_HERSHEY_PLAIN, 1, 1, 0, 1, 1)
     print("resolution: " + str(int(self.w)) + "x" + str(int(self.h)))
     self.fx = 0.85
     self.fy = 0.85
     self.iframe = self.xml.iframe
     self.plate = True
     self.radar = False
     self.semaphore = False
     self.moto = False
     self.currv = None
     self.mouse = MouseEvent()
     self.select = PlaceSelection()
     ' create platesDir if needed '
     self.plateCarRadar = platesDir + "/car-radar"
     self.plateCarNoRad = platesDir + "/car-noradar"
     self.plateMotoRadar = platesDir + "/moto-radar"
     self.plateMotoNoRad = platesDir + "/moto-noradar"
     if not os.path.isdir(platesDir):
         os.mkdir(platesDir)
     if not os.path.isdir(self.plateCarRadar):
         os.mkdir(self.plateCarRadar)
     if not os.path.isdir(self.plateCarNoRad):
         os.mkdir(self.plateCarNoRad)
     if not os.path.isdir(self.plateMotoRadar):
         os.mkdir(self.plateMotoRadar)
     if not os.path.isdir(self.plateMotoNoRad):
         os.mkdir(self.plateMotoNoRad)
    def __init__(self, threshold=70, showWindows=True):

        self.writer = None
        self.font = None

        self.show = showWindows  # Either or not show the 2 windows
        self.frame = None

        self.capture = cv.CaptureFromCAM(0)
        self.frame = cv.QueryFrame(
            self.capture)  # Take a frame to init recorder

        self.frame = self.frame[1:100, 540:640]
        self.frame1gray = cv.CreateMat(self.frame.height, self.frame.width,
                                       cv.CV_8U)  # Gray frame at t-1
        cv.CvtColor(self.frame, self.frame1gray, cv.CV_RGB2GRAY)

        # Will hold the thresholded result
        self.res = cv.CreateMat(self.frame.height, self.frame.width, cv.CV_8U)

        self.frame2gray = cv.CreateMat(self.frame.height, self.frame.width,
                                       cv.CV_8U)  # Gray frame at t

        self.width = self.frame.width
        self.height = self.frame.height
        self.nb_pixels = self.width * self.height
        self.threshold = threshold

        self.trigger_time = 0  # Hold timestamp of the last detection

        codec = cv.CV_FOURCC('M', 'J', 'P', 'G')  # ('W', 'M', 'V', '2')
        self.writer = cv.CreateVideoWriter(
            datetime.now().strftime("%b-%d_%H_%M_%S") + ".wmv", codec, 5,
            cv.GetSize(self.frame), 1)
        # FPS set to 5 because it seems to be the fps of my cam but should be ajusted to your needs
        self.font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 2,
                                8)  # Creates a font
Exemple #4
0
    def __init__(self, camera, image_path="images"):
        # 判断视频是否打开
        if not camera.isOpened():
            print "摄像头未打开"
            exit(1)

        self.image_path = image_path
        if not os.path.exists(self.image_path):
            os.mkdir(self.image_path)
        self.camera = camera
        self.fps = camera.get(cv2.cv.CV_CAP_PROP_FPS)
        self.fps = self.fps if self.fps > 0 else 5
        self.size = (int(camera.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
                     int(camera.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))
        print('fps:' + repr(self.fps) + ' size:' + repr(self.size))

        self.es = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9, 4))
        self.font = cv.InitFont(cv.CV_FONT_HERSHEY_SCRIPT_SIMPLEX, 1, 1, 0, 3,
                                8)

        self.background = None
        self.last_save_at = 0
        self.last_back_at = 0
        self.mp4 = None
Exemple #5
0
def run(fP, accAvg, threshL, ID):
    # Initialize
    #log_file_name = "tracker_output.log"
    #log_file = file( log_file_name, 'a' )

    cap = cv2.VideoCapture(fP)

    # Capture the first frame from file for image properties
    orig_image = cap.read()[1]

    #For now, just cut off the bottom 5% ####NEEDS TO BE CHANGED
    display_image = orig_image[1:700, 1:1280]
    #cv2.imshow("frame",display_image)
    #cv2.waitKey(1200)
    #cv2.destroyWindow("frame")

    #Define SubArea Based on Mouse Event
    box = [0, 0, 0, 0]

    ##        creating mouse callback function
    #def my_mouse_callback(event,x,y,flags,param):
    #global drawing_box
    #if event==cv2.EVENT_LBUTTONDOWN:
    #drawing_box=True
    #[box[0],box[1],box[2],box[3]]=[x,y,0,0]
    #print box[0]

    #if event==cv2.EVENT_LBUTTONUP:
    #drawing_box=False
    #if box[2]<0:
    #box[0]+=box[2]
    #box[2]*=-1
    #if box[3]<0:
    #box[1]+=box[3]
    #box[3]*=-1

    #if event==cv2.EVENT_MOUSEMOVE:
    #if (drawing_box==True):
    #box[2]=x-box[0]
    #box[3]=y-box[1]

    ## Function to draw the rectangle
    #def draw_box(img,box):
    #cv2.rectangle(img,(box[0],box[1]),(box[0]+box[2],box[1]+box[3]),(255,0,0))

    ##        main program
    #drawing_box=False
    #cv2.namedWindow("Box Example")
    #cv2.setMouseCallback("Box Example",my_mouse_callback,display_image)

    #while(1):
    #temp=display_image.copy()
    #if drawing_box==True:
    #draw_box(temp,box)
    #cv2.imshow("Box Example",temp)
    #if cv.WaitKey(20)%0x100==27:break

    #cv2.destroyWindow("Box Example")

    ##################################################################################################
    #For now, just have it ignore the bottom few rows, since that is where the time diff is being sent.
    ##################################################################################################

    width = np.size(display_image, 1)
    height = np.size(display_image, 0)
    frame_size = (width, height)

    # Greyscale image, thresholded to create the motion mask:
    grey_image = np.uint8(display_image)

    # The RunningAvg() function requires a 32-bit or 64-bit image...
    running_average_image = np.float32(display_image)

    # ...but the AbsDiff() function requires matching image depths:
    running_average_in_display_color_depth = display_image.copy()

    # RAM used by FindContours():
    mem_storage = cv.CreateMemStorage(0)

    # The difference between the running average and the current frame:
    difference = display_image.copy()

    target_count = 1
    last_target_count = 1
    last_target_change_t = 0.0
    k_or_guess = 1
    codebook = []
    frame_count = 0
    last_frame_entity_list = []

    t0 = time.time()

    # For toggling display:
    image_list = ["camera", "difference", "threshold", "display", "faces"]
    image_index = 0  # Index into image_list

    # Prep for text drawing:
    text_font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX, .5, .5, 0.0, 1,
                            cv.CV_AA)
    text_coord = (5, 15)
    text_color = (255, 255, 255)

    # Set this to the max number of targets to look for (passed to k-means):
    max_targets = 1

    while True:

        # Capture frame from file
        ret, camera_imageO = cap.read()
        if not ret:
            break

        #For now, just cut off the bottom 5% ####NEEDS TO BE CHANGED
        camera_image = camera_imageO[1:700, 1:1280]
        frame_count += 1
        frame_t0 = time.time()

        # Create an image with interactive feedback:
        display_image = camera_image.copy()

        # Create a working "color image" to modify / blur
        color_image = display_image.copy()
        cv2.imshow("Initial", color_image)
        cv2.waitKey(1200)
        cv2.destroyWindow("Initial")
        cv2.imwrite(
            file_destination + "/" + str(frame_count) + "Initial" + ".jpg",
            color_image)

        # Smooth to get rid of false positives
        color_image = cv2.GaussianBlur(color_image, (9, 9), 0)
        cv2.imshow("Blur", color_image)
        cv2.waitKey(1200)
        cv2.destroyWindow("Blur")
        cv2.imwrite(
            file_destination + "/" + str(frame_count) + "Blur" + ".jpg",
            color_image)

        # Use the Running Average as the static background
        # a = 0.020 leaves artifacts lingering way too long.
        # a = 0.320 works well at 320x240, 15fps.  (1/a is roughly num frames.)
        #This value is very critical.

        cv2.accumulateWeighted(color_image, running_average_image, accAvg)
        #cv2.imshow("frame",running_average_image)
        ##cv2.waitKey(100)
        #cv2.destroyWindow("frame")

        running_average_in_display_color_depth = cv2.convertScaleAbs(
            running_average_image)
        cv2.imshow("runnAVG", running_average_in_display_color_depth)
        cv2.waitKey(1200)
        cv2.destroyWindow("runnAVG")

        # Subtract the current frame from the moving average.
        difference = cv2.absdiff(color_image,
                                 running_average_in_display_color_depth)
        cv2.imshow("diff", difference)
        cv2.waitKey(1200)
        cv2.destroyWindow("diff")
        cv2.imwrite(
            file_destination + "/" + str(frame_count) + "diff" + ".jpg",
            difference)

        # Convert the image to greyscale.
        grey_image = cv2.cvtColor(difference, cv2.COLOR_BGR2GRAY)
        #cv2.imshow("Convertgray",grey_image)
        #cv2.waitKey(100)
        #cv2.destroyWindow("Convertgray")

        # Threshold the image to a black and white motion mask:
        ret, grey_image = cv2.threshold(grey_image, threshL, 255,
                                        cv2.THRESH_BINARY)
        cv2.imshow("Threshold", grey_image)
        cv2.waitKey(1200)
        cv2.destroyWindow("Threshold")
        cv2.imwrite(
            file_destination + "/" + str(frame_count) + "threshold" + ".jpg",
            grey_image)

        # Smooth and threshold again to eliminate "sparkles"
        #grey_image = cv2.GaussianBlur(grey_image,(9,9),0)
        #ret,grey_image = cv2.threshold(grey_image, 240, 255, cv2.THRESH_BINARY )
        #cv2.imshow("frame",grey_image)
        #cv2.waitKey(1200)
        #cv2.destroyWindow("frame")

        non_black_coords_array = numpy.where(grey_image > 3)
        # Convert from numpy.where()'s two separate lists to one list of (x, y) tuples:
        non_black_coords_array = zip(non_black_coords_array[1],
                                     non_black_coords_array[0])

        points = [
        ]  # Was using this to hold either pixel coords or polygon coords.
        bounding_box_list = []

        # Now calculate movements using the white pixels as "motion" data
        contours, hierarchy = cv2.findContours(grey_image, cv2.RETR_TREE,
                                               cv2.CHAIN_APPROX_SIMPLE)

        if len(contours) == 0:
            continue
        print(len(contours))
        cnt = contours[0]
        len(cnt)

        drawing = np.uint8(display_image)

        ##Draw the initial contours?
        #for cnt in contours:
        #bx,by,bw,bh = cv2.boundingRect(cnt)
        #cv2.drawContours(drawing,[cnt],0,(0,255,0),1)   # draw contours in green color
        #cv2.imshow('output',drawing)
        ##cv2.waitKey(100)

        cv2.destroyWindow("output")

        for cnt in contours:

            bounding_rect = cv2.boundingRect(cnt)
            point1 = (bounding_rect[0], bounding_rect[1])
            point2 = (bounding_rect[0] + bounding_rect[2],
                      bounding_rect[1] + bounding_rect[3])

            bounding_box_list.append((point1, point2))
            polygon_points = cv2.approxPolyDP(cnt,
                                              0.1 * cv2.arcLength(cnt, True),
                                              True)
            approx = cv2.approxPolyDP(cnt, 0.1 * cv2.arcLength(cnt, True),
                                      True)

            ## To track polygon points only (instead of every pixel):
            ##points += list(polygon_points)

            ## Draw the contours:
            ##cv.DrawContours(color_image, contour, cv.CV_RGB(255,0,0), cv.CV_RGB(0,255,0), levels, 3, 0, (0,0) )

            ##cv.FillPoly( grey_image, [ list(polygon_points), ], cv.CV_RGB(255,255,255), 0, 0 )
            ##cv.PolyLine( display_image, [ polygon_points, ], 0, cv.CV_RGB(255,255,255), 1, 0, 0 )

            ##cv.Rectangle( display_image, point1, point2, cv.CV_RGB(120,120,120), 1)

            ##contour = contour.h_next()

        # Find the average size of the bbox (targets), then
        # remove any tiny bboxes (which are prolly just noise).
        # "Tiny" is defined as any box with 1/10th the area of the average box.
        # This reduces false positives on tiny "sparkles" noise.
        box_areas = []
        for box in bounding_box_list:
            box_width = box[right][0] - box[left][0]
            box_height = box[bottom][0] - box[top][0]
            box_areas.append(box_width * box_height)

        average_box_area = 0.0
        if len(box_areas):
            average_box_area = float(sum(box_areas)) / len(box_areas)

        trimmed_box_list = []
        for box in bounding_box_list:
            box_width = box[right][0] - box[left][0]
            box_height = box[bottom][0] - box[top][0]

            # Only keep the box if it's not a tiny noise box:
            if (box_width * box_height) > average_box_area * 0.1:
                trimmed_box_list.append(box)

        ## Draw the trimmed box list:
        #print(len(trimmed_box_list))
        #for box in trimmed_box_list:
        #cv2.rectangle( display_image, box[0], box[1], (0,255,0), 3 )
        #cv2.imshow('output',display_image)
        ##cv2.waitKey(100)
        #cv2.destroyWindow("output")

        bounding_box_list = merge_collided_bboxes(trimmed_box_list)

        # Draw the merged box list:

        for box in bounding_box_list:
            cv2.rectangle(display_image, box[0], box[1], (0, 255, 0), 1)
            cv2.imshow('output', orig_image)
        cv2.waitKey(1200)
        cv2.destroyWindow("output")
        cv2.imwrite(
            file_destination + "/" + str(frame_count) + "output" + ".jpg",
            orig_image)

        # Here are our estimate points to track, based on merged & trimmed boxes:
        estimated_target_count = len(bounding_box_list)

        # Don't allow target "jumps" from few to many or many to few.
        # Only change the number of targets up to one target per n seconds.
        # This fixes the "exploding number of targets" when something stops moving
        # and the motion erodes to disparate little puddles all over the place.

        if frame_t0 - last_target_change_t < .350:  # 1 change per 0.35 secs
            estimated_target_count = last_target_count
        else:
            if last_target_count - estimated_target_count > 1:
                estimated_target_count = last_target_count - 1
            if estimated_target_count - last_target_count > 1:
                estimated_target_count = last_target_count + 1
            last_target_change_t = frame_t0

        # Clip to the user-supplied maximum:
        estimated_target_count = min(estimated_target_count, max_targets)

        # The estimated_target_count at this point is the maximum number of targets
        # we want to look for.  If kmeans decides that one of our candidate
        # bboxes is not actually a target, we remove it from the target list below.

        # Using the numpy values directly (treating all pixels as points):
        points = non_black_coords_array
        center_points = []

        if len(points):

            # If we have all the "target_count" targets from last frame,
            # use the previously known targets (for greater accuracy).
            k_or_guess = max(estimated_target_count,
                             1)  # Need at least one target to look for.
            if len(codebook) == estimated_target_count:
                k_or_guess = codebook

            #points = vq.whiten(array( points ))  # Don't do this!  Ruins everything.
            codebook, distortion = vq.kmeans(array(points), k_or_guess)

            # Convert to tuples (and draw it to screen)
            for center_point in codebook:
                center_point = (int(center_point[0]), int(center_point[1]))
                center_points.append(center_point)
                cv2.circle(display_image, center_point, 10, (255, 0, 0), 2)
                cv2.circle(display_image, center_point, 5, (255, 0, 0), 3)
                cv2.imshow('output', orig_image)
                cv2.waitKey(500)
            cv2.destroyWindow("output")
        # Now we have targets that are NOT computed from bboxes -- just
        # movement weights (according to kmeans).  If any two targets are
        # within the same "bbox count", average them into a single target.
        #
        # (Any kmeans targets not within a bbox are also kept.)
        trimmed_center_points = []
        removed_center_points = []

        for box in bounding_box_list:
            # Find the centers within this box:
            center_points_in_box = []

            for center_point in center_points:
                if        center_point[0] < box[right][0] and center_point[0] > box[left][0] and \
                        center_point[1] < box[bottom][1] and center_point[1] > box[top][1] :

                    # This point is within the box.
                    center_points_in_box.append(center_point)

            # Now see if there are more than one.  If so, merge them.
            if len(center_points_in_box) > 1:
                # Merge them:
                x_list = y_list = []
                for point in center_points_in_box:
                    x_list.append(point[0])
                    y_list.append(point[1])

                average_x = int(float(sum(x_list)) / len(x_list))
                average_y = int(float(sum(y_list)) / len(y_list))

                trimmed_center_points.append((average_x, average_y))

                # Record that they were removed:
                removed_center_points += center_points_in_box

            if len(center_points_in_box) == 1:
                trimmed_center_points.append(
                    center_points_in_box[0])  # Just use it.

        # If there are any center_points not within a bbox, just use them.
        # (It's probably a cluster comprised of a bunch of small bboxes.)
        for center_point in center_points:
            if (not center_point in trimmed_center_points) and (
                    not center_point in removed_center_points):
                trimmed_center_points.append(center_point)

        # Draw what we found:
        #for center_point in trimmed_center_points:
        #        center_point = ( int(center_point[0]), int(center_point[1]) )
        #        cv2.circle(display_image, center_point, 20, (255, 255,255), 1)
        #        cv2.circle(display_image, center_point, 15, (100, 255, 255), 1)
        #        cv2.circle(display_image, center_point, 10, (255, 255, 255), 2)
        #        cv2.circle(display_image, center_point, 5,(100, 255, 255), 3)

        # Determine if there are any new (or lost) targets:
        actual_target_count = len(trimmed_center_points)
        last_target_count = actual_target_count

        # Now build the list of physical entities (objects)
        this_frame_entity_list = []

        # An entity is list: [ name, color, last_time_seen, last_known_coords ]

        for target in trimmed_center_points:

            # Is this a target near a prior entity (same physical entity)?
            entity_found = False
            entity_distance_dict = {}

            for entity in last_frame_entity_list:

                entity_coords = entity[3]
                delta_x = entity_coords[0] - target[0]
                delta_y = entity_coords[1] - target[1]

                distance = sqrt(pow(delta_x, 2) + pow(delta_y, 2))
                entity_distance_dict[distance] = entity

            # Did we find any non-claimed entities (nearest to furthest):
            distance_list = entity_distance_dict.keys()
            distance_list.sort()

            for distance in distance_list:

                # Yes; see if we can claim the nearest one:
                nearest_possible_entity = entity_distance_dict[distance]

                # Don't consider entities that are already claimed:
                if nearest_possible_entity in this_frame_entity_list:
                    #print "Target %s: Skipping the one iwth distance: %d at %s, C:%s" % (target, distance, nearest_possible_entity[3], nearest_possible_entity[1] )
                    continue

                #print "Target %s: USING the one iwth distance: %d at %s, C:%s" % (target, distance, nearest_possible_entity[3] , nearest_possible_entity[1])
                # Found the nearest entity to claim:
                entity_found = True
                nearest_possible_entity[2] = frame_t0  # Update last_time_seen
                nearest_possible_entity[3] = target  # Update the new location
                this_frame_entity_list.append(nearest_possible_entity)
                #log_file.write( "%.3f MOVED %s %d %d\n" % ( frame_t0, nearest_possible_entity[0], nearest_possible_entity[3][0], nearest_possible_entity[3][1]  ) )
                break

            if entity_found == False:
                # It's a new entity.
                color = (random.randint(0, 255), random.randint(0, 255),
                         random.randint(0, 255))
                name = hashlib.md5(str(frame_t0) + str(color)).hexdigest()[:6]
                last_time_seen = frame_t0

                new_entity = [name, color, last_time_seen, target]
                this_frame_entity_list.append(new_entity)
                #log_file.write( "%.3f FOUND %s %d %d\n" % ( frame_t0, new_entity[0], new_entity[3][0], new_entity[3][1]  ) )

        # Now "delete" any not-found entities which have expired:
        entity_ttl = 1.0  # 1 sec.

        for entity in last_frame_entity_list:
            last_time_seen = entity[2]
            if frame_t0 - last_time_seen > entity_ttl:
                # It's gone.
                #log_file.write( "%.3f STOPD %s %d %d\n" % ( frame_t0, entity[0], entity[3][0], entity[3][1]  ) )
                pass
            else:
                # Save it for next time... not expired yet:
                this_frame_entity_list.append(entity)

        # For next frame:
        last_frame_entity_list = this_frame_entity_list

        # Draw the found entities to screen:
        for entity in this_frame_entity_list:
            center_point = entity[3]
            c = entity[1]  # RGB color tuple
            cv2.circle(camera_imageO, center_point, 20, (c[0], c[1], c[2]), 1)
            cv2.circle(camera_imageO, center_point, 15, (c[0], c[1], c[2]), 1)
            cv2.circle(camera_imageO, center_point, 10, (c[0], c[1], c[2]), 2)
            cv2.circle(camera_imageO, center_point, 5, (c[0], c[1], c[2]), 3)
        cv2.imshow('output', camera_imageO)
        cv2.waitKey(1200)
        cv2.destroyWindow("output")
        cv2.imwrite(
            file_destination + "/" + str(frame_count) + "final" + ".jpg",
            camera_imageO)

        ###print ("min_size is: " + str(min_size))
        ### Listen for ESC or ENTER key
        #c = cv.WaitKey(7) % 0x100
        #if c == 27 or c == 10:
        #break

        ## Toggle which image to show
        #if chr(c) == 'd':
        #image_index = ( image_index + 1 ) % len( image_list )

        #image_name = image_list[ image_index ]

        ## Display frame to user
        #if image_name == "camera":
        #image = camera_image
        #cv2.putText( image, "Camera (Normal)", text_coord, text_font, text_color )
        #elif image_name == "difference":
        #image = difference
        #cv2.putText( image, "Difference Image", text_coord, text_font, text_color )
        #elif image_name == "display":
        #image = display_image
        #cv2.putText( image, "Targets (w/AABBs and contours)", text_coord, text_font, text_color )
        #elif image_name == "threshold":
        ## Convert the image to color.
        #cv.CvtColor( grey_image, display_image, cv.CV_GRAY2RGB )
        #image = display_image  # Re-use display image here
        #cv2.putText( image, "Motion Mask", text_coord, text_font, text_color )

        ##cv2.ShowImage( "Target", image )
        #cv2.imshow("Target",image)
        #cv2.waitKey(1200)
        #cv2.destroyWindow("frame")

        ##################################################
        #To Do, write to file if the center is in the box?
        #If it makes it to here, write an image
        cv2.imwrite(fileD + ID + "/" + str(frame_count) + ".jpg",
                    camera_imageO)
Exemple #6
0
def run(fP, accAvg, threshL):

    #Report name of file
    sys.stderr.write("Processing file %s\n" % (fP))

    #Define directories, here assuming that we want to append the file structure of the last three folders to the file destination

    normFP = os.path.normpath(fP)
    (filepath, filename) = os.path.split(normFP)
    (shortname, extension) = os.path.splitext(filename)
    (_, IDFL) = os.path.split(filepath)

    #we want to name the output a folder from the output destination with the named extension
    print("Output path will be %s/%s/%s" % (fileD, IDFL, shortname))
    print("AccAvg begin value is: %s" % (accAvg))

    file_destination = os.path.join(fileD, IDFL, shortname)
    if not os.path.exists(file_destination):
        os.makedirs(file_destination)

    # Create a log file with each coordinate
    log_file_name = file_destination + "/" + "tracker_output.log"
    log_file = file(log_file_name, 'a')

    #create hit counter to track number of outputs
    hitcounter = 0

    cap = cv2.VideoCapture(fP)

    # Capture the first frame from file for image properties
    orig_image = cap.read()[1]

    ###Get information about camera and image

    width = np.size(orig_image, 1)
    height = np.size(orig_image, 0)
    frame_size = (width, height)

    #For now, just cut off the bottom 5% if the timing mechanism is on the bottom.
    if plotwatcher:
        display_image = orig_image[1:700, 1:1280]
    else:
        display_image = orig_image

    if vis:
        cv2.imshow("frame", display_image)
        cv2.waitKey(1000)
        cv2.destroyWindow("frame")

    width = np.size(display_image, 1)
    height = np.size(display_image, 0)
    frame_size = (width, height)

    #Get frame rate if the plotwatcher setting hasn't been called
    if plotwatcher:
        frame_rate = 1
    else:
        frame_rate = round(cap.get(cv.CV_CAP_PROP_FPS))

    #get frame time relative to start
    frame_time = cap.get(cv.CV_CAP_PROP_POS_MSEC)

    print("frame rate: " + str(frame_rate))
    sys.stderr.write("frame rate: " + str(frame_rate))

    # Greyscale image, thresholded to create the motion mask:
    grey_image = np.uint8(display_image)

    # The RunningAvg() function requires a 32-bit or 64-bit image...
    running_average_image = np.float32(display_image)

    # ...but the AbsDiff() function requires matching image depths:
    running_average_in_display_color_depth = display_image.copy()

    # RAM used by FindContours():
    mem_storage = cv.CreateMemStorage(0)

    # The difference between the running average and the current frame:
    difference = display_image.copy()

    target_count = 1
    last_target_count = 1
    last_target_change_t = 0.0
    k_or_guess = 1
    codebook = []
    frame_count = 0
    last_frame_entity_list = []

    #Set time
    t0 = time.time()

    # Prep for text drawing:
    text_font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX, .5, .5, 0.0, 1,
                            cv.CV_AA)
    text_coord = (5, 15)
    text_color = (255, 255, 255)

    # Set this to the max number of targets to look for (passed to k-means):
    max_targets = 1

    while True:

        # Capture frame from file
        ret, camera_imageO = cap.read()
        if not ret:
            break

        #For now, just cut off the bottom 5% if the plotwatcher option is called.

        if plotwatcher:
            camera_image = camera_imageO[1:700, 1:1280]
        else:
            camera_image = camera_imageO

        frame_count += 1
        frame_t0 = time.time()

        ####Adaptively set the aggregate threshold, we know that about 95% of data are negatives.
        #set floor flag, we can't have negative accAVG
        floor = 0
        if adapt:

            #Every 15min, reset the agg threshold, depending on expected level of movement
            #how many frames per fiteen minutes?

            #Should be a integer, round it
            fift = round(10 * 60 * frame_rate)

            if frame_count % fift == 0:

                #If the total base is fift (15min window), then assuming 99% of images are junk the threshold should be
                #We've been counting frames output to file in the hitcounter
                log_file.write(
                    str(hitcounter) + "files written in last 10minutes" + "\n")
                print(
                    str(hitcounter) + " files written in last 10minutes" +
                    "\n")

                if hitcounter > (fift * frameHIT):
                    accAvg = accAvg + .05
                if hitcounter < (fift * frameHIT):
                    accAvg = accAvg - .025

                #In my experience its much more important to drop the sensitivity, than to increase it, so i've make the adapt filter move downwards slower than upwards.

                print(file_destination + str(frame_count) +
                      " accAvg is changed to: " + str(accAvg))
                #Write change to log file
                log_file.write(file_destination + str(frame_count) +
                               " accAvg is changed to: " + str(accAvg) + "\n")

                #reset hitcoutner for another fifteen minutes
                hitcounter = 0

                #Build in a floor, the value can't be negative.
                if accAvg < floorvalue:
                    floor = floor + 1

            #Reset if needed.
                if floor == 1:
                    accAvg = floorvalue

                    print(file_destination + str(frame_count) +
                          " accAvg is reset to: " + str(accAvg))
                    #Write change to log file
                    log_file.write(file_destination + str(frame_count) +
                                   " accAvg is reset to: " + str(accAvg) +
                                   "\n")
########################################################

# Create an image with interactive feedback:
        display_image = camera_image.copy()

        # Create a working "color image" to modify / blur
        color_image =  display_image.copy()\

        if vis:

            cv2.imshow("Initial", color_image)
            cv2.waitKey(1000)
            cv2.destroyWindow("Initial")

        # Smooth to get rid of false positives
        color_image = cv2.GaussianBlur(color_image, (9, 9), 0)
        #cv2.imshow("Blur",color_image)
        #cv2.waitKey(1000)
        #cv2.destroyWindow("Blur")

        # Use the Running Average as the static background

        #This value is very critical.

        cv2.accumulateWeighted(color_image, running_average_image, accAvg)
        if vis:
            cv2.imshow("frame", running_average_image)
            cv2.waitKey(1000)
            cv2.destroyWindow("frame")

        running_average_in_display_color_depth = cv2.convertScaleAbs(
            running_average_image)
        if vis:
            cv2.imshow("runnAVG", running_average_in_display_color_depth)
            cv2.waitKey(1000)
            cv2.destroyWindow("runnAVG")

        # Subtract the current frame from the moving average.
        difference = cv2.absdiff(color_image,
                                 running_average_in_display_color_depth)
        if vis:
            cv2.imshow("diff", difference)
            cv2.waitKey(1000)
            cv2.destroyWindow("diff")

        # Convert the image to greyscale.
        grey_image = cv2.cvtColor(difference, cv2.COLOR_BGR2GRAY)
        #cv2.imshow("Convertgray",grey_image)
        #cv2.waitKey(1000)
        #cv2.destroyWindow("Convertgray")

        # Threshold the image to a black and white motion mask:
        ret, grey_image = cv2.threshold(grey_image, threshL, 255,
                                        cv2.THRESH_BINARY)
        if vis:
            cv2.imshow("Threshold", grey_image)
            cv2.waitKey(1000)
            cv2.destroyWindow("Threshold")

        non_black_coords_array = numpy.where(grey_image > 3)
        # Convert from numpy.where()'s two separate lists to one list of (x, y) tuples:
        non_black_coords_array = zip(non_black_coords_array[1],
                                     non_black_coords_array[0])

        points = [
        ]  # Was using this to hold either pixel coords or polygon coords.
        bounding_box_list = []

        # Now calculate movements using the white pixels as "motion" data
        contours, hierarchy = cv2.findContours(grey_image, cv2.RETR_TREE,
                                               cv2.CHAIN_APPROX_SIMPLE)

        if len(contours) == 0:
            continue
        #print(len(contours))
        cnt = contours[0]
        len(cnt)

        drawing = np.uint8(display_image)

        ##Draw the initial contours?
        #for cnt in contours:
        if vis:
            bx, by, bw, bh = cv2.boundingRect(cnt)
            cv2.drawContours(drawing, [cnt], 0, (0, 255, 0),
                             1)  # draw contours in green color
            cv2.imshow('output', drawing)
            ##cv2.waitKey(100)

        cv2.destroyWindow("output")

        for cnt in contours:

            bounding_rect = cv2.boundingRect(cnt)
            point1 = (bounding_rect[0], bounding_rect[1])
            point2 = (bounding_rect[0] + bounding_rect[2],
                      bounding_rect[1] + bounding_rect[3])

            bounding_box_list.append((point1, point2))
            polygon_points = cv2.approxPolyDP(cnt,
                                              0.1 * cv2.arcLength(cnt, True),
                                              True)
            approx = cv2.approxPolyDP(cnt, 0.1 * cv2.arcLength(cnt, True),
                                      True)

        # Find the average size of the bbox (targets), then
        # remove any tiny bboxes (which are probably just noise).
        # "Tiny" is defined as any box with 1/10th the area of the average box.
        # This reduces false positives on tiny "sparkles" noise.
        box_areas = []
        for box in bounding_box_list:
            box_width = box[right][0] - box[left][0]
            box_height = box[bottom][0] - box[top][0]
            box_areas.append(box_width * box_height)

        average_box_area = 0.0
        if len(box_areas):
            average_box_area = float(sum(box_areas)) / len(box_areas)

        trimmed_box_list = []
        for box in bounding_box_list:
            box_width = box[right][0] - box[left][0]
            box_height = box[bottom][0] - box[top][0]

            # Only keep the box if it's not a tiny noise box:
            if (box_width * box_height) > average_box_area * .3:
                trimmed_box_list.append(box)

        ## Draw the trimmed box list:
        #print(len(trimmed_box_list))
        for box in trimmed_box_list:
            if vis:
                cv2.rectangle(display_image, box[0], box[1], (0, 255, 0), 3)
                cv2.imshow('output', display_image)
                cv2.waitKey(100)
                cv2.destroyWindow("output")

        try:
            bounding_box_list = merge_collided_bboxes(trimmed_box_list)
        except Exception, e:
            print 'Error:', e
            print 'Box Merge Fail:'
            continue
        # Draw the merged box list:

        for box in bounding_box_list:
            cv2.rectangle(display_image, box[0], box[1], (0, 255, 0), 1)
            if vis:
                cv2.imshow('output', orig_image)
                cv2.waitKey(1000)
                cv2.destroyWindow("output")

        # Here are our estimate points to track, based on merged & trimmed boxes:
        estimated_target_count = len(bounding_box_list)

        # Don't allow target "jumps" from few to many or many to few.
        # Only change the number of targets up to one target per n seconds.
        # This fixes the "exploding number of targets" when something stops moving
        # and the motion erodes to disparate little puddles all over the place.

        if frame_t0 - last_target_change_t < .350:  # 1 change per 0.35 secs
            estimated_target_count = last_target_count
        else:
            if last_target_count - estimated_target_count > 1:
                estimated_target_count = last_target_count - 1
            if estimated_target_count - last_target_count > 1:
                estimated_target_count = last_target_count + 1
            last_target_change_t = frame_t0

        # Clip to the user-supplied maximum:
        estimated_target_count = min(estimated_target_count, max_targets)

        # The estimated_target_count at this point is the maximum number of targets
        # we want to look for.  If kmeans decides that one of our candidate
        # bboxes is not actually a target, we remove it from the target list below.

        # Using the numpy values directly (treating all pixels as points):
        points = non_black_coords_array
        center_points = []

        if len(points):

            # If we have all the "target_count" targets from last frame,
            # use the previously known targets (for greater accuracy).
            k_or_guess = max(estimated_target_count,
                             1)  # Need at least one target to look for.
            if len(codebook) == estimated_target_count:
                k_or_guess = codebook

            codebook, distortion = vq.kmeans(array(points), k_or_guess)

            # Convert to tuples (and draw it to screen)
            for center_point in codebook:
                center_point = (int(center_point[0]), int(center_point[1]))
                center_points.append(center_point)
                cv2.circle(display_image, center_point, 10, (255, 0, 0), 2)
                cv2.circle(display_image, center_point, 5, (255, 0, 0), 3)
                if vis:
                    cv2.imshow('output', orig_image)
                    cv2.waitKey(500)
                    cv2.destroyWindow("output")
        # Now we have targets that are NOT computed from bboxes -- just
        # movement weights (according to kmeans).  If any two targets are
        # within the same "bbox count", average them into a single target.
        #
        # (Any kmeans targets not within a bbox are also kept.)
        trimmed_center_points = []
        removed_center_points = []

        for box in bounding_box_list:
            # Find the centers within this box:
            center_points_in_box = []

            for center_point in center_points:
                if        center_point[0] < box[right][0] and center_point[0] > box[left][0] and \
                        center_point[1] < box[bottom][1] and center_point[1] > box[top][1] :

                    # This point is within the box.
                    center_points_in_box.append(center_point)

            # Now see if there are more than one.  If so, merge them.
            if len(center_points_in_box) > 1:
                # Merge them:
                x_list = y_list = []
                for point in center_points_in_box:
                    x_list.append(point[0])
                    y_list.append(point[1])

                average_x = int(float(sum(x_list)) / len(x_list))
                average_y = int(float(sum(y_list)) / len(y_list))

                trimmed_center_points.append((average_x, average_y))

                # Record that they were removed:
                removed_center_points += center_points_in_box

            if len(center_points_in_box) == 1:
                trimmed_center_points.append(
                    center_points_in_box[0])  # Just use it.

        # If there are any center_points not within a bbox, just use them.
        # (It's probably a cluster comprised of a bunch of small bboxes.)
        for center_point in center_points:
            if (not center_point in trimmed_center_points) and (
                    not center_point in removed_center_points):
                trimmed_center_points.append(center_point)

        # Determine if there are any new (or lost) targets:
        actual_target_count = len(trimmed_center_points)
        last_target_count = actual_target_count

        # Now build the list of physical entities (objects)
        this_frame_entity_list = []

        # An entity is list: [ name, color, last_time_seen, last_known_coords ]

        for target in trimmed_center_points:

            # Is this a target near a prior entity (same physical entity)?
            entity_found = False
            entity_distance_dict = {}

            for entity in last_frame_entity_list:

                entity_coords = entity[3]
                delta_x = entity_coords[0] - target[0]
                delta_y = entity_coords[1] - target[1]

                distance = sqrt(pow(delta_x, 2) + pow(delta_y, 2))
                entity_distance_dict[distance] = entity

            # Did we find any non-claimed entities (nearest to furthest):
            distance_list = entity_distance_dict.keys()
            distance_list.sort()

            for distance in distance_list:

                # Yes; see if we can claim the nearest one:
                nearest_possible_entity = entity_distance_dict[distance]

                # Don't consider entities that are already claimed:
                if nearest_possible_entity in this_frame_entity_list:
                    #print "Target %s: Skipping the one iwth distance: %d at %s, C:%s" % (target, distance, nearest_possible_entity[3], nearest_possible_entity[1] )
                    continue

                #print "Target %s: USING the one with distance: %d at %s, C:%s" % (target, distance, nearest_possible_entity[3] , nearest_possible_entity[1])
                # Found the nearest entity to claim:
                entity_found = True
                nearest_possible_entity[2] = frame_t0  # Update last_time_seen
                nearest_possible_entity[3] = target  # Update the new location
                this_frame_entity_list.append(nearest_possible_entity)
                break

            if entity_found == False:
                # It's a new entity.
                color = (random.randint(0, 255), random.randint(0, 255),
                         random.randint(0, 255))
                name = hashlib.md5(str(frame_t0) + str(color)).hexdigest()[:6]
                last_time_seen = frame_t0

                new_entity = [name, color, last_time_seen, target]
                this_frame_entity_list.append(new_entity)

        # Now "delete" any not-found entities which have expired:
        entity_ttl = 1.0  # 1 sec.

        for entity in last_frame_entity_list:
            last_time_seen = entity[2]
            if frame_t0 - last_time_seen > entity_ttl:
                # It's gone.
                pass
            else:
                # Save it for next time... not expired yet:
                this_frame_entity_list.append(entity)

        # For next frame:
        last_frame_entity_list = this_frame_entity_list

        # Draw the found entities to screen:
        for entity in this_frame_entity_list:
            center_point = entity[3]
            c = entity[1]  # RGB color tuple
            cv2.circle(camera_imageO, center_point, 20, (c[0], c[1], c[2]), 1)
            cv2.circle(camera_imageO, center_point, 15, (c[0], c[1], c[2]), 1)
            cv2.circle(camera_imageO, center_point, 10, (c[0], c[1], c[2]), 2)
            cv2.circle(camera_imageO, center_point, 5, (c[0], c[1], c[2]), 3)
        if vis:

            cv2.imshow('output', camera_imageO)
            cv2.waitKey(1000)
            cv2.destroyWindow("output")

        #Show final image

        ##cv2.ShowImage( "Target", image )

        if vis:
            cv2.imshow("Target", camera_imageO)
            cv2.waitKey(1000)
            cv2.destroyWindow("frame")

        ##################################################
        #Write image to file
        cv2.imwrite(file_destination + "/" + str(frame_count) + ".jpg",
                    camera_imageO)

        #Log the frame count and the time in video, in case user wants to check in the original
        #create a time object, this relies on the frame_rate being correct!
        #set seconds
        sec = timedelta(seconds=int(frame_count / frame_rate))
        d = datetime(1, 1, 1) + sec
        log_file.write("%d %d:%d:%d " %
                       (int(frame_count), d.hour, d.minute, d.second) + "\n")
        #If a file has been written, flush the log to read
        sys.stdout.flush()

        ##################################################
        #Have a returned counter to balance hitRate
        hitcounter = hitcounter + 1
Exemple #7
0
import cv2.cv as cv

image=cv.LoadImage('../img/lena.jpg', cv.CV_LOAD_IMAGE_COLOR) #Load the image

font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 3, 8) #Creates a font

y = image.height / 2 # y position of the text
x = image.width / 4 # x position of the text

cv.PutText(image,"Hello World !", (x,y),font, cv.RGB(255, 255, 255)) #Draw the text

cv.ShowImage('Hello World', image) #Show the image
cv.WaitKey(0)
 def draw_label(self, img, pixmapper):
     pix1 = pixmapper(self.point)
     cv.PutText(img, self.label, pix1,
                cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0),
                self.colour)
Exemple #9
0
def compute(playerList, video):
    videoName = video
    capture = cv.CaptureFromFile(videoName)

    count = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_COUNT))
    fps = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FPS)
    width = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH))
    height = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT))

    # store the last frame
    preFrame = cv.CreateImage((width, height), 8, 1)
    # store the current frame
    curFrame = cv.CreateImage((width, height), 8, 1)

    prePyr = cv.CreateImage((height / 3, width + 8), 8, cv.CV_8UC1)
    curPyr = cv.CreateImage((height / 3, width + 8), 8, cv.CV_8UC1)

    numOfPlayers = len(playerList)

    # store players moving distance
    players = np.zeros(numOfPlayers)

    # store players position of last frame
    prePlayers = playerList
    # store players position of current frame
    curPlayers = []

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

    #flag of storing player info
    flagInfo = True

    for f in xrange(count):
        frame = cv.QueryFrame(capture)

        if (flagInfo):
            cv.CvtColor(frame, img, cv.CV_BGR2GRAY)
            for i in range(numOfPlayers):
                font = cv.InitFont(cv.CV_FONT_HERSHEY_SCRIPT_SIMPLEX, 0.4, 0.4,
                                   0, 2, 3)

                cv.PutText(
                    img, str(i),
                    (int(prePlayers[i][0][0]), int(prePlayers[i][0][1])), font,
                    (255, 255, 255))
            cv.SaveImage(playerInfo, img)
            flagInfo = False

        #Convert to gray
        cv.CvtColor(frame, curFrame, cv.CV_BGR2GRAY)

        #Calculate the movement using the previous and the current frame using the previous points
        curPlayers, status, err = cv.CalcOpticalFlowPyrLK(
            preFrame, curFrame, prePyr, curPyr, prePlayers, (10, 10), 3,
            (cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03), 0)

        ###temp = frame
        # add new distance to list
        for i in range(numOfPlayers):
            players[i] += getDistance(prePlayers[i], curPlayers[i])
            ###cv.Line(temp, (int(prePlayers[i][0]), int(prePlayers[i][1])), (int(curPlayers[i][0]), int(curPlayers[i][1])), (255,122,122),3)

        ###cv.ShowImage("test", temp)
        ###cv2.waitKey(20)

        #Put the current frame preFrame
        cv.Copy(curFrame, preFrame)
        prePlayers = curPlayers
    ###cv2.destroyAllWindows()
    # print distance
    i = 0
    f = open(recordFile, 'w')
    for player in players:
        i += 1
        print "player", i, "running distance: ", player, "\n"
        f.write("player" + str(i) + " running distance: " + str(player) +
                "meters\n")
        # The difference between the running average and the current frame:
        difference =  display_image.copy()
        
        target_count = 1
        last_target_count = 1
        last_target_change_t = 0.0
        k_or_guess = 1
        codebook=[]
        frame_count=0
        last_frame_entity_list = []
        
        #Set time
        t0 = time.time()
        
        # Prep for text drawing:
        text_font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX, .5, .5, 0.0, 1, cv.CV_AA )
        text_coord = ( 5, 15 )
        text_color = (255,255,255)

        # Set this to the max number of targets to look for (passed to k-means):
        max_targets = 1
                
        while True:
                
                # Capture frame from file
                ret,camera_imageO = cap.read()
                if not ret:
                        break    
                
                #For now, just cut off the bottom 5% if the plotwatcher option is called. 
                
    def run(self):
        # Initialize
        # log_file_name = "tracker_output.log"
        # log_file = file( log_file_name, 'a' )

        print "hello"

        frame = cv.QueryFrame(self.capture)
        frame_size = cv.GetSize(frame)

        # Capture the first frame from webcam for image properties
        display_image = cv.QueryFrame(self.capture)

        # Greyscale image, thresholded to create the motion mask:
        grey_image = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 1)

        # The RunningAvg() function requires a 32-bit or 64-bit image...
        running_average_image = cv.CreateImage(cv.GetSize(frame),
                                               cv.IPL_DEPTH_32F, 3)

        # ...but the AbsDiff() function requires matching image depths:
        running_average_in_display_color_depth = cv.CloneImage(display_image)

        # RAM used by FindContours():
        mem_storage = cv.CreateMemStorage(0)

        # The difference between the running average and the current frame:
        difference = cv.CloneImage(display_image)

        target_count = 1
        last_target_count = 1
        last_target_change_t = 0.0
        k_or_guess = 1
        codebook = []
        frame_count = 0
        last_frame_entity_list = []

        t0 = time.time()

        # For toggling display:
        image_list = ["camera", "difference", "threshold", "display", "faces"]
        image_index = 3  # Index into image_list

        # Prep for text drawing:
        text_font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX, .5, .5, 0.0, 1,
                                cv.CV_AA)
        text_coord = (5, 15)
        text_color = cv.CV_RGB(255, 255, 255)

        # Set this to the max number of targets to look for (passed to k-means):
        max_targets = 5

        while True:

            # Capture frame from webcam
            camera_image = cv.QueryFrame(self.capture)

            frame_count += 1
            frame_t0 = time.time()

            # Create an image with interactive feedback:
            display_image = cv.CloneImage(camera_image)

            # Create a working "color image" to modify / blur
            color_image = cv.CloneImage(display_image)

            # Smooth to get rid of false positives
            cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 19, 0)

            # Use the Running Average as the static background
            # a = 0.020 leaves artifacts lingering way too long.
            # a = 0.320 works well at 320x240, 15fps.  (1/a is roughly num frames.)
            cv.RunningAvg(color_image, running_average_image, 0.320, None)

            # Convert the scale of the moving average.
            cv.ConvertScale(running_average_image,
                            running_average_in_display_color_depth, 1.0, 0.0)

            # Subtract the current frame from the moving average.
            cv.AbsDiff(color_image, running_average_in_display_color_depth,
                       difference)

            cv.ShowImage("difference ", difference)

            # Convert the image to greyscale.
            cv.CvtColor(difference, grey_image, cv.CV_RGB2GRAY)

            # Threshold the image to a black and white motion mask:
            cv.Threshold(grey_image, grey_image, 2, 255, cv.CV_THRESH_BINARY)
            # Smooth and threshold again to eliminate "sparkles"
            cv.Smooth(grey_image, grey_image, cv.CV_GAUSSIAN, 19, 0)
            cv.Threshold(grey_image, grey_image, 240, 255, cv.CV_THRESH_BINARY)

            grey_image_as_array = numpy.asarray(cv.GetMat(grey_image))
            non_black_coords_array = numpy.where(grey_image_as_array > 3)
            # Convert from numpy.where()'s two separate lists to one list of (x, y) tuples:
            non_black_coords_array = zip(non_black_coords_array[1],
                                         non_black_coords_array[0])

            #             cv.SegmentMotion(non_black_coords_array, None, storage, timestamp, seg_thresh)

            # print "min_size is: " + str(min_size)
            # Listen for ESC or ENTER key
            c = cv.WaitKey(7) % 0x100
            if c == 27 or c == 10:
                break

            # Toggle which image to show


#             if chr(c) == 'd':
#                 image_index = ( image_index + 1 ) % len( image_list )
#
#             image_name = image_list[ image_index ]
#
#             # Display frame to user
#             if image_name == "camera":
#                 image = camera_image
#                 cv.PutText( image, "Camera (Normal)", text_coord, text_font, text_color )
#             elif image_name == "difference":
#                 image = difference
#                 cv.PutText( image, "Difference Image", text_coord, text_font, text_color )
#             elif image_name == "display":
#                 image = display_image
#                 cv.PutText( image, "Targets (w/AABBs and contours)", text_coord, text_font, text_color )
#             elif image_name == "threshold":
#                 # Convert the image to color.
#                 cv.CvtColor( grey_image, display_image, cv.CV_GRAY2RGB )
#                 image = display_image  # Re-use display image here
#                 cv.PutText( image, "Motion Mask", text_coord, text_font, text_color )
#             elif image_name == "faces":
#                 # Do face detection
#                 detect_faces( camera_image, haar_cascade, mem_storage )
#                 image = camera_image  # Re-use camera image here
#                 cv.PutText( image, "Face Detection", text_coord, text_font, text_color )
#             cv.ShowImage( "Target", image )

            image1 = display_image

            cv.ShowImage("Target 1", image1)

            #             if self.writer:
            #                 cv.WriteFrame( self.writer, image );

            # log_file.flush()

            # If only using a camera, then there is no time.sleep() needed,
            # because the camera clips us to 15 fps.  But if reading from a file,
            # we need this to keep the time-based target clipping correct:
            frame_t1 = time.time()

            # If reading from a file, put in a forced delay:
            if not self.writer:
                delta_t = frame_t1 - frame_t0
                if delta_t < (1.0 / 15.0): time.sleep((1.0 / 15.0) - delta_t)

        t1 = time.time()
        time_delta = t1 - t0
        processed_fps = float(frame_count) / time_delta
        print "Got %d frames. %.1f s. %f fps." % (frame_count, time_delta,
                                                  processed_fps)
    blink = cv.CloneImage(drawing)
    if hands:
      for id in hands:
        cv.Circle(blink, hands[id]['current_position'], 10, hands[id]['color']['cv'], -1, cv.CV_AA, 0)
        if hands[id]['drawing'] == True:
          cv.Line(drawing, hands[id]['previous_position'], hands[id]['current_position'], hands[id]['color']['cv'], 10, cv.CV_AA, 0) 
    cv.ShowImage('Drawing', blink)

########################### MAIN ##################################

cv.NamedWindow('Video',1)
cv.MoveWindow('Video',0,0)
cv.NamedWindow('Drawing',1)
cv.MoveWindow('Drawing',720,0)

text_font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.6, 0.6, 0, 1, 4)

drawing = cv.CreateImage((640,480), cv.IPL_DEPTH_8U, 3)
cv.Set(drawing, (255.0,255.0,255.0))

cv_image = cv.CreateImage((640,480), cv.IPL_DEPTH_8U, 3)
hands = {}
buttons_size = (100, 60)
buttons = {'White': {'color': cv.CV_RGB(255,255,255), 'start': (500, 30), 'end': (500 + buttons_size[0], 30 + buttons_size[1])},
          'Black': {'color': cv.CV_RGB(0,0,0), 'start': (500, 100), 'end': (500 + buttons_size[0], 100 + buttons_size[1])},
          'Red': {'color': cv.CV_RGB(255,0,0), 'start': (500, 170), 'end': (500 + buttons_size[0], 170 + buttons_size[1])},
          'Green': {'color': cv.CV_RGB(0,255,0), 'start': (500, 240), 'end': (500 + buttons_size[0], 240 + buttons_size[1])},
          'Blue': {'color': cv.CV_RGB(0,0,255), 'start': (500, 310), 'end': (500 + buttons_size[0], 310 + buttons_size[1])},
          }

ni = Context()
Exemple #13
0
    0x10L: "ITF",
    0x20L: "EAN_13",
    0x40L: "EAN_8",
    0x80L: "UPC_A",
    0x100L: "UPC_E",
}

title = "Dynamsoft Barcode Reader"
# Create a window with OpenCV
cv.NamedWindow(title, 1)
capture = cv.CaptureFromCAM(0)

is_saved = False
is_camera_paused = False
line_type = cv.CV_AA
font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX, 0.1, 1, 1, 1, line_type)
key_code = -1
fileName = '/home/caratred/test.jpg'

while True:
    if is_camera_paused:
        if not is_saved:
            is_saved = True
            # Capture a frame from Webcam
            img = cv.QueryFrame(capture)

            # fileName = 'dynamsoft_barcode_test.jpg'
            # img = cv.LoadImage('dynamsoft_barcode_test.jpg')
            # Save captured frame to local disk
            cv.SaveImage(fileName, img)
            # Decode the captured image by Dynamsoft Barcode library
Exemple #14
0
                    FPim += 1
                    cv.Circle(im, (xcentre, ycentre), 10, [0, 0, 255])

            for bb in tmpbb:
                FNim += 1
                cv.Rectangle(im, (bb[0], bb[2]), (bb[1], bb[3]),
                             [255, 255, 255], 1)

            TP += TPim
            FP += FPim
            FN += FNim

            if len(matchingcoords) > 0:
                cv.PutText(im, 'TP=%d, FP=%d, FN=%d' % (TPim, FPim, FNim),
                           (11, 21),
                           cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5,
                                       0.5), (0, 0, 0))
                cv.PutText(im, 'TP=%d, FP=%d, FN=%d' % (TPim, FPim, FNim),
                           (11, 21),
                           cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5,
                                       0.5), (255, 255, 255))

            if show_images:
                cv.ShowImage('result', im)

                if cv.WaitKey(0) == 27:
                    break

    print('TP=%d, FP=%d, FN=%d' % (TP, FP, FN))
    precision = (TP * 1.0) / (TP + FP)
    recall = (TP * 1.0) / (TP + FN)
    fscore = 2 * precision * recall / (precision + recall)
Exemple #15
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from openni import *
import cv2.cv as cv

cv.NamedWindow('Video', 1)
cv.MoveWindow('Video', 0, 0)
cv.NamedWindow('Quadro', 1)
cv.MoveWindow('Quadro', 720, 0)

fonte_do_texto = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.6, 0.6, 0, 1, 4)

quadro = cv.CreateImage((640, 480), cv.IPL_DEPTH_8U, 3)
cv.Set(quadro, (255.0, 255.0, 255.0))

imagem_cv = cv.CreateImage((640, 480), cv.IPL_DEPTH_8U, 3)
maos = {}
traduz = {'Wave': 'Apagador', 'Click': 'Caneta'}
efeito = 'Nenhum'

ni = Context()
ni.init()
ni.open_file_recording("BothGestures.oni")
video = ni.find_existing_node(NODE_TYPE_IMAGE)
depth = ni.find_existing_node(NODE_TYPE_DEPTH)

gesture_generator = GestureGenerator()
gesture_generator.create(ni)
gesture_generator.add_gesture('Wave')
gesture_generator.add_gesture('Click')
Exemple #16
0
  polar = cv.CreateImage((360, 360), 8, 3)
  img = cv.CreateImage((320, 240), 8, 3)
  im = cv.CreateImage((320, 240), 8, 1)
  rt = cv.CreateImage((320, 240), 8, 1)
  lt = cv.CreateImage((320, 240), 8, 1)
  lm = cv.LoadImageM("leftmask-K-2013-02-27.bmp", cv.CV_LOAD_IMAGE_GRAYSCALE)
  rm = cv.LoadImageM("rightmask-K-2013-02-27.bmp", cv.CV_LOAD_IMAGE_GRAYSCALE)
  
  cv.NamedWindow('cam')
  cv.NamedWindow('left')
  cv.NamedWindow('right')

  cv.SetMouseCallback("cam", on_mouse)
  on_mouse(cv.CV_EVENT_LBUTTONDOWN, centerX, centerY, None, None)

  font = cv.InitFont(cv.CV_FONT_HERSHEY_PLAIN, 1.0, 1.0) 

  M = 60

  while True:
    img = cv.QueryFrame(capture)
    cv.CvtColor(img, im, cv.CV_RGB2GRAY)
    #cv.LogPolar(img, polar, (centerX, centerY), M+1, cv.CV_INTER_NN + cv.CV_WARP_FILL_OUTLIERS )

    cv.And(im, lm, lt)
    leftBrightness = cv.Avg(im, mask=lm)
    cv.Rectangle(lt, (0, 0), (32, 32), leftBrightness, thickness=-1)
    cv.PutText(lt, "%3.0f" % leftBrightness[0], (3,20), font, cv.RealScalar(0))

    cv.And(im, rm, rt)
    rightBrightness = cv.Avg(im, mask=rm)
    for i in range(number):
        pt1 = (random.randrange(-width, 2 * width),
               random.randrange(-height, 2 * height))
        cv.Circle(image, pt1, random.randrange(0, 300), random_color(random),
                  random.randrange(-1, 9), line_type, 0)

        cv.ShowImage(window_name, image)
        cv.WaitKey(delay)

    # draw some text
    for i in range(number):
        pt1 = (random.randrange(-width, 2 * width),
               random.randrange(-height, 2 * height))
        font = cv.InitFont(random.randrange(0, 8),
                           random.randrange(0, 100) * 0.05 + 0.01,
                           random.randrange(0, 100) * 0.05 + 0.01,
                           random.randrange(0, 5) * 0.1,
                           random.randrange(0, 10), line_type)

        cv.PutText(image, "Testing text rendering!", pt1, font,
                   random_color(random))

        cv.ShowImage(window_name, image)
        cv.WaitKey(delay)

    # prepare a text, and get it's properties
    font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX, 3, 3, 0.0, 5, line_type)
    text_size, ymin = cv.GetTextSize("OpenCV forever!", font)
    pt1 = ((width - text_size[0]) / 2, (height + text_size[1]) / 2)
    image2 = cv.CloneImage(image)
Exemple #18
0
    def __init__(self):
        rospy.init_node('avi2ros', anonymous=True)
        
        self.input = rospy.get_param("~input", "")
        self.output = rospy.get_param("~output", "video_output")       
        self.fps = rospy.get_param("~fps", 25)
        self.loop = rospy.get_param("~loop", False)
        self.width = rospy.get_param("~width", "")
        self.height = rospy.get_param("~height", "")
        self.start_paused = rospy.get_param("~start_paused", False)
        self.show_viz = not rospy.get_param("~headless", False)
        self.show_text = True

        image_pub = rospy.Publisher(self.output, Image, queue_size=10)
        
        rospy.on_shutdown(self.cleanup)
        
        video = cv.CaptureFromFile(self.input)
        fps = int(cv.GetCaptureProperty(video, cv.CV_CAP_PROP_FPS))
        
        """ Bring the fps up to the specified rate """
        try:
            fps = int(fps * self.fps / fps)
        except:
            fps = self.fps
    
        if self.show_viz:
            cv.NamedWindow("AVI Video", True) # autosize the display
            cv.MoveWindow("AVI Video", 650, 100)

        bridge = CvBridge()
                
        self.paused = self.start_paused
        self.keystroke = None
        self.restart = False
        
        # Get the first frame to display if we are starting in the paused state.
        frame = cv.QueryFrame(video)
        image_size = cv.GetSize(frame)
        
        if self.width and self.height and (self.width != image_size[0] or self.height != image_size[1]):
            rospy.loginfo("Resizing! " + str(self.width) + " x " + str(self.height))
            resized_frame = cv.CreateImage((self.width, self.height), frame.depth, frame.channels)
            cv.Resize(frame, resized_frame)
            frame = cv.CloneImage(resized_frame)
                        
        text_frame = cv.CloneImage(frame)
        cv.Zero(text_frame)
    
        while not rospy.is_shutdown():
            """ Handle keyboard events """
            self.keystroke = cv.WaitKey(1000 / fps)

            """ Process any keyboard commands """
            if 32 <= self.keystroke and self.keystroke < 128:
                cc = chr(self.keystroke).lower()
                if cc == 'q':
                    """ user has press the q key, so exit """
                    rospy.signal_shutdown("User hit q key to quit.")
                elif cc == ' ':
                    """ Pause or continue the video """
                    self.paused = not self.paused
                elif cc == 'r':
                    """ Restart the video from the beginning """
                    self.restart = True
                elif cc == 't':
                    """ Toggle display of text help message """
                    self.show_text = not self.show_text
                
            if self.restart:
                #video = cv.CaptureFromFile(self.input)
                print "restarting video from beginning"
                cv.SetCaptureProperty(video, cv.CV_CAP_PROP_POS_AVI_RATIO, 0)
                self.restart = None
    
            if not self.paused:
                frame = cv.QueryFrame(video)
                if frame and self.width and self.height:
                    if self.width != image_size[0] or self.height != image_size[1]:
                        cv.Resize(frame, resized_frame)
                        frame = cv.CloneImage(resized_frame)
                
            if frame == None:
                if self.loop:
                    self.restart = True
            else:
                if self.show_text:
                    frame_size = cv.GetSize(frame)
                    text_font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.2, 1, 0, 1, 8)
                    cv.PutText(text_frame, "Keyboard commands:", (20, int(frame_size[1] * 0.6)), text_font, cv.RGB(255, 255, 0))
                    cv.PutText(text_frame, " ", (20, int(frame_size[1] * 0.65)), text_font, cv.RGB(255, 255, 0))
                    cv.PutText(text_frame, "space - toggle pause/play", (20, int(frame_size[1] * 0.72)), text_font, cv.RGB(255, 255, 0))
                    cv.PutText(text_frame, "     r - restart video from beginning", (20, int(frame_size[1] * 0.79)), text_font, cv.RGB(255, 255, 0))
                    cv.PutText(text_frame, "     t - hide/show this text", (20, int(frame_size[1] * 0.86)), text_font, cv.RGB(255, 255, 0))
                    cv.PutText(text_frame, "     q - quit the program", (20, int(frame_size[1] * 0.93)), text_font, cv.RGB(255, 255, 0))
                
                cv.Add(frame, text_frame, text_frame)
                if self.show_viz:
                    cv.ShowImage("AVI Video", text_frame)
                cv.Zero(text_frame)
                
                try:
                    test = np.asarray(frame[:,:])
                    publishing_image = bridge.cv2_to_imgmsg(test, "bgr8")
                    image_pub.publish(publishing_image)
                except CvBridgeError, e:
                    print e         
Exemple #19
0
    def run(self):
        # Initialize
        # log_file_name = "tracker_output.log"
        # log_file = file( log_file_name, 'a' )

        print "hello"

        frame = cv.QueryFrame(self.capture)
        frame_size = cv.GetSize(frame)

        # Capture the first frame from webcam for image properties
        display_image = cv.QueryFrame(self.capture)

        # Greyscale image, thresholded to create the motion mask:
        grey_image = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 1)

        # The RunningAvg() function requires a 32-bit or 64-bit image...
        running_average_image = cv.CreateImage(cv.GetSize(frame),
                                               cv.IPL_DEPTH_32F, 3)

        # ...but the AbsDiff() function requires matching image depths:
        running_average_in_display_color_depth = cv.CloneImage(display_image)

        # RAM used by FindContours():
        mem_storage = cv.CreateMemStorage(0)

        # The difference between the running average and the current frame:
        difference = cv.CloneImage(display_image)

        target_count = 1
        last_target_count = 1
        last_target_change_t = 0.0
        k_or_guess = 1
        codebook = []
        frame_count = 0
        last_frame_entity_list = []

        t0 = time.time()

        # For toggling display:
        image_list = ["camera", "difference", "threshold", "display", "faces"]
        image_index = 3  # Index into image_list

        # Prep for text drawing:
        text_font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX, .5, .5, 0.0, 1,
                                cv.CV_AA)
        text_coord = (5, 15)
        text_color = cv.CV_RGB(255, 255, 255)

        # Set this to the max number of targets to look for (passed to k-means):
        max_targets = 5

        while True:

            # Capture frame from webcam
            camera_image = cv.QueryFrame(self.capture)

            frame_count += 1
            frame_t0 = time.time()

            # Create an image with interactive feedback:
            display_image = cv.CloneImage(camera_image)

            # Create a working "color image" to modify / blur
            color_image = cv.CloneImage(display_image)

            # Smooth to get rid of false positives
            cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 19, 0)

            # Use the Running Average as the static background
            # a = 0.020 leaves artifacts lingering way too long.
            # a = 0.320 works well at 320x240, 15fps.  (1/a is roughly num frames.)
            cv.RunningAvg(color_image, running_average_image, 0.320, None)

            #             cv.ShowImage("background ", running_average_image)

            # Convert the scale of the moving average.
            cv.ConvertScale(running_average_image,
                            running_average_in_display_color_depth, 1.0, 0.0)

            # Subtract the current frame from the moving average.
            cv.AbsDiff(color_image, running_average_in_display_color_depth,
                       difference)

            cv.ShowImage("difference ", difference)

            # Convert the image to greyscale.
            cv.CvtColor(difference, grey_image, cv.CV_RGB2GRAY)

            # Threshold the image to a black and white motion mask:
            cv.Threshold(grey_image, grey_image, 2, 255, cv.CV_THRESH_BINARY)
            # Smooth and threshold again to eliminate "sparkles"
            cv.Smooth(grey_image, grey_image, cv.CV_GAUSSIAN, 19, 0)

            cv.Threshold(grey_image, grey_image, 240, 255, cv.CV_THRESH_BINARY)

            grey_image_as_array = numpy.asarray(cv.GetMat(grey_image))
            non_black_coords_array = numpy.where(grey_image_as_array > 3)
            # Convert from numpy.where()'s two separate lists to one list of (x, y) tuples:
            non_black_coords_array = zip(non_black_coords_array[1],
                                         non_black_coords_array[0])

            points = [
            ]  # Was using this to hold either pixel coords or polygon coords.
            bounding_box_list = []

            # Now calculate movements using the white pixels as "motion" data
            contour = cv.FindContours(grey_image, mem_storage,
                                      cv.CV_RETR_CCOMP,
                                      cv.CV_CHAIN_APPROX_SIMPLE)

            levels = 10
            while contour:

                bounding_rect = cv.BoundingRect(list(contour))
                point1 = (bounding_rect[0], bounding_rect[1])
                point2 = (bounding_rect[0] + bounding_rect[2],
                          bounding_rect[1] + bounding_rect[3])

                bounding_box_list.append((point1, point2))
                polygon_points = cv.ApproxPoly(list(contour), mem_storage,
                                               cv.CV_POLY_APPROX_DP)

                # To track polygon points only (instead of every pixel):
                # points += list(polygon_points)

                # Draw the contours:
                cv.DrawContours(color_image, contour, cv.CV_RGB(255, 0, 0),
                                cv.CV_RGB(0, 255, 0), levels, 3, 0, (0, 0))
                cv.FillPoly(grey_image, [
                    list(polygon_points),
                ], cv.CV_RGB(255, 255, 255), 0, 0)
                cv.PolyLine(display_image, [
                    polygon_points,
                ], 0, cv.CV_RGB(255, 255, 255), 1, 0, 0)
                # cv.Rectangle( display_image, point1, point2, cv.CV_RGB(120,120,120), 1)

                contour = contour.h_next()

            # Find the average size of the bbox (targets), then
            # remove any tiny bboxes (which are prolly just noise).
            # "Tiny" is defined as any box with 1/10th the area of the average box.
            # This reduces false positives on tiny "sparkles" noise.
            box_areas = []
            for box in bounding_box_list:
                box_width = box[right][0] - box[left][0]
                box_height = box[bottom][0] - box[top][0]
                box_areas.append(box_width * box_height)

                # cv.Rectangle( display_image, box[0], box[1], cv.CV_RGB(255,0,0), 1)

            average_box_area = 0.0
            if len(box_areas):
                average_box_area = float(sum(box_areas)) / len(box_areas)

            trimmed_box_list = []
            for box in bounding_box_list:
                box_width = box[right][0] - box[left][0]
                box_height = box[bottom][0] - box[top][0]

                # Only keep the box if it's not a tiny noise box:
                if (box_width * box_height) > average_box_area * 0.1:
                    trimmed_box_list.append(box)

            # Draw the trimmed box list:
            # for box in trimmed_box_list:
            #    cv.Rectangle( display_image, box[0], box[1], cv.CV_RGB(0,255,0), 2 )

            bounding_box_list = merge_collided_bboxes(trimmed_box_list)

            # Draw the merged box list:
            for box in bounding_box_list:
                cv.Rectangle(display_image, box[0], box[1],
                             cv.CV_RGB(0, 255, 0), 1)

            # Here are our estimate points to track, based on merged & trimmed boxes:
            estimated_target_count = len(bounding_box_list)

            # Don't allow target "jumps" from few to many or many to few.
            # Only change the number of targets up to one target per n seconds.
            # This fixes the "exploding number of targets" when something stops moving
            # and the motion erodes to disparate little puddles all over the place.

            if frame_t0 - last_target_change_t < .350:  # 1 change per 0.35 secs
                estimated_target_count = last_target_count
            else:
                if last_target_count - estimated_target_count > 1:
                    estimated_target_count = last_target_count - 1
                if estimated_target_count - last_target_count > 1:
                    estimated_target_count = last_target_count + 1
                last_target_change_t = frame_t0

            # Clip to the user-supplied maximum:
            estimated_target_count = min(estimated_target_count, max_targets)

            # The estimated_target_count at this point is the maximum number of targets
            # we want to look for.  If kmeans decides that one of our candidate
            # bboxes is not actually a target, we remove it from the target list below.

            # Using the numpy values directly (treating all pixels as points):
            points = non_black_coords_array
            center_points = []

            if len(points):

                # If we have all the "target_count" targets from last frame,
                # use the previously known targets (for greater accuracy).
                k_or_guess = max(estimated_target_count,
                                 1)  # Need at least one target to look for.
                if len(codebook) == estimated_target_count:
                    k_or_guess = codebook

                # points = vq.whiten(array( points ))  # Don't do this!  Ruins everything.
                codebook, distortion = vq.kmeans(array(points), k_or_guess)

                # Convert to tuples (and draw it to screen)
                for center_point in codebook:
                    center_point = (int(center_point[0]), int(center_point[1]))
                    center_points.append(center_point)
                    # cv.Circle(display_image, center_point, 10, cv.CV_RGB(255, 0, 0), 2)
                    # cv.Circle(display_image, center_point, 5, cv.CV_RGB(255, 0, 0), 3)

            # Now we have targets that are NOT computed from bboxes -- just
            # movement weights (according to kmeans).  If any two targets are
            # within the same "bbox count", average them into a single target.
            #
            # (Any kmeans targets not within a bbox are also kept.)
            trimmed_center_points = []
            removed_center_points = []

            for box in bounding_box_list:
                # Find the centers within this box:
                center_points_in_box = []

                for center_point in center_points:
                    if    center_point[0] < box[right][0] and center_point[0] > box[left][0] and \
                        center_point[1] < box[bottom][1] and center_point[1] > box[top][1] :

                        # This point is within the box.
                        center_points_in_box.append(center_point)

                # Now see if there are more than one.  If so, merge them.
                if len(center_points_in_box) > 1:
                    # Merge them:
                    x_list = y_list = []
                    for point in center_points_in_box:
                        x_list.append(point[0])
                        y_list.append(point[1])

                    average_x = int(float(sum(x_list)) / len(x_list))
                    average_y = int(float(sum(y_list)) / len(y_list))

                    trimmed_center_points.append((average_x, average_y))

                    # Record that they were removed:
                    removed_center_points += center_points_in_box

                if len(center_points_in_box) == 1:
                    trimmed_center_points.append(
                        center_points_in_box[0])  # Just use it.

            # If there are any center_points not within a bbox, just use them.
            # (It's probably a cluster comprised of a bunch of small bboxes.)
            for center_point in center_points:
                if (not center_point in trimmed_center_points) and (
                        not center_point in removed_center_points):
                    trimmed_center_points.append(center_point)

            # Draw what we found:
            # for center_point in trimmed_center_points:
            #    center_point = ( int(center_point[0]), int(center_point[1]) )
            #    cv.Circle(display_image, center_point, 20, cv.CV_RGB(255, 255,255), 1)
            #    cv.Circle(display_image, center_point, 15, cv.CV_RGB(100, 255, 255), 1)
            #    cv.Circle(display_image, center_point, 10, cv.CV_RGB(255, 255, 255), 2)
            #    cv.Circle(display_image, center_point, 5, cv.CV_RGB(100, 255, 255), 3)

            # Determine if there are any new (or lost) targets:
            actual_target_count = len(trimmed_center_points)
            last_target_count = actual_target_count

            # Now build the list of physical entities (objects)
            this_frame_entity_list = []

            # An entity is list: [ name, color, last_time_seen, last_known_coords ]

            for target in trimmed_center_points:

                # Is this a target near a prior entity (same physical entity)?
                entity_found = False
                entity_distance_dict = {}

                for entity in last_frame_entity_list:

                    entity_coords = entity[3]
                    delta_x = entity_coords[0] - target[0]
                    delta_y = entity_coords[1] - target[1]

                    distance = sqrt(pow(delta_x, 2) + pow(delta_y, 2))
                    entity_distance_dict[distance] = entity

                # Did we find any non-claimed entities (nearest to furthest):
                distance_list = entity_distance_dict.keys()
                distance_list.sort()

                for distance in distance_list:

                    # Yes; see if we can claim the nearest one:
                    nearest_possible_entity = entity_distance_dict[distance]

                    # Don't consider entities that are already claimed:
                    if nearest_possible_entity in this_frame_entity_list:
                        # print "Target %s: Skipping the one iwth distance: %d at %s, C:%s" % (target, distance, nearest_possible_entity[3], nearest_possible_entity[1] )
                        continue

                    # print "Target %s: USING the one iwth distance: %d at %s, C:%s" % (target, distance, nearest_possible_entity[3] , nearest_possible_entity[1])
                    # Found the nearest entity to claim:
                    entity_found = True
                    nearest_possible_entity[
                        2] = frame_t0  # Update last_time_seen
                    nearest_possible_entity[
                        3] = target  # Update the new location
                    this_frame_entity_list.append(nearest_possible_entity)
                    # log_file.write( "%.3f MOVED %s %d %d\n" % ( frame_t0, nearest_possible_entity[0], nearest_possible_entity[3][0], nearest_possible_entity[3][1]  ) )
                    break

                if entity_found == False:
                    # It's a new entity.
                    color = (random.randint(0, 255), random.randint(0, 255),
                             random.randint(0, 255))
                    name = hashlib.md5(str(frame_t0) +
                                       str(color)).hexdigest()[:6]
                    last_time_seen = frame_t0

                    new_entity = [name, color, last_time_seen, target]
                    this_frame_entity_list.append(new_entity)
                    # log_file.write( "%.3f FOUND %s %d %d\n" % ( frame_t0, new_entity[0], new_entity[3][0], new_entity[3][1]  ) )

            # Now "delete" any not-found entities which have expired:
            entity_ttl = 1.0  # 1 sec.

            for entity in last_frame_entity_list:
                last_time_seen = entity[2]
                if frame_t0 - last_time_seen > entity_ttl:
                    # It's gone.
                    # log_file.write( "%.3f STOPD %s %d %d\n" % ( frame_t0, entity[0], entity[3][0], entity[3][1]  ) )
                    pass
                else:
                    # Save it for next time... not expired yet:
                    this_frame_entity_list.append(entity)

            # For next frame:
            last_frame_entity_list = this_frame_entity_list

            # Draw the found entities to screen:
            for entity in this_frame_entity_list:
                center_point = entity[3]
                c = entity[1]  # RGB color tuple
                cv.Circle(display_image, center_point, 20,
                          cv.CV_RGB(c[0], c[1], c[2]), 1)
                cv.Circle(display_image, center_point, 15,
                          cv.CV_RGB(c[0], c[1], c[2]), 1)
                cv.Circle(display_image, center_point, 10,
                          cv.CV_RGB(c[0], c[1], c[2]), 2)
                cv.Circle(display_image, center_point, 5,
                          cv.CV_RGB(c[0], c[1], c[2]), 3)

            # print "min_size is: " + str(min_size)
            # Listen for ESC or ENTER key
            c = cv.WaitKey(7) % 0x100
            if c == 27 or c == 10:
                break

            # Toggle which image to show


#             if chr(c) == 'd':
#                 image_index = ( image_index + 1 ) % len( image_list )
#
#             image_name = image_list[ image_index ]
#
#             # Display frame to user
#             if image_name == "camera":
#                 image = camera_image
#                 cv.PutText( image, "Camera (Normal)", text_coord, text_font, text_color )
#             elif image_name == "difference":
#                 image = difference
#                 cv.PutText( image, "Difference Image", text_coord, text_font, text_color )
#             elif image_name == "display":
#                 image = display_image
#                 cv.PutText( image, "Targets (w/AABBs and contours)", text_coord, text_font, text_color )
#             elif image_name == "threshold":
#                 # Convert the image to color.
#                 cv.CvtColor( grey_image, display_image, cv.CV_GRAY2RGB )
#                 image = display_image  # Re-use display image here
#                 cv.PutText( image, "Motion Mask", text_coord, text_font, text_color )
#             elif image_name == "faces":
#                 # Do face detection
#                 detect_faces( camera_image, haar_cascade, mem_storage )
#                 image = camera_image  # Re-use camera image here
#                 cv.PutText( image, "Face Detection", text_coord, text_font, text_color )
#             cv.ShowImage( "Target", image )

            image1 = display_image

            cv.ShowImage("Target 1", image1)

            #             if self.writer:
            #                 cv.WriteFrame( self.writer, image );

            # log_file.flush()

            # If only using a camera, then there is no time.sleep() needed,
            # because the camera clips us to 15 fps.  But if reading from a file,
            # we need this to keep the time-based target clipping correct:
            frame_t1 = time.time()

            # If reading from a file, put in a forced delay:
            if not self.writer:
                delta_t = frame_t1 - frame_t0
                if delta_t < (1.0 / 15.0): time.sleep((1.0 / 15.0) - delta_t)

        t1 = time.time()
        time_delta = t1 - t0
        processed_fps = float(frame_count) / time_delta
        print "Got %d frames. %.1f s. %f fps." % (frame_count, time_delta,
                                                  processed_fps)
Exemple #20
0
def detect(img, cascade):
    rects = cascade.detectMultiScale(img, scaleFactor=1.1,
                                     minNeighbors=3, minSize=(10, 10),
                                     flags = cv.CV_HAAR_SCALE_IMAGE)
    if len(rects) == 0:
        return []
    rects[:,2:] += rects[:,:2]
    return rects


def draw_rects(img, rects, color):
    for x1, y1, x2, y2 in rects:
        cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)

font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, .5, .5, lineType=cv.CV_AA)
base_data_dir = 'faces/'
log_file = 'capture_log.txt'
img_ext = 'jpg'
join = os.path.join

def dirname(base_dir=base_data_dir):
    """ Generate dir name in directory for the day (inside base_dir)
        Dir name format: AAAA_MM_DD
        If the dir do not exists, it will be created.
    """
    now = datetime.datetime.now()
    dir = '%02d_%02d_%02d' % (now.year, now.month, now.day)
    dir = join(base_dir, dir, '')
    if not os.path.exists(dir):
        os.makedirs(dir)
Exemple #21
0

succ = 0  #number of frames in a row that we were successful in finding the outline
tracking = 0
win_size = 5
flags = 0
detected = 0

grey = cv.CreateImage((W, H), 8, 1)
prev_grey = cv.CreateImage((W, H), 8, 1)
pyramid = cv.CreateImage((W, H), 8, 1)
prev_pyramid = cv.CreateImage((W, H), 8, 1)

ff = cv.InitFont(cv.CV_FONT_HERSHEY_PLAIN,
                 1,
                 1,
                 shear=0,
                 thickness=1,
                 lineType=8)

counter = 0  #global iteration counter
undetectednum = 100
stage = 1  #1: learning colors
extract = False
selected = 0
colors = [[] for i in range(6)]
hsvs = [[] for i in range(6)]
assigned = [[-1 for i in range(9)] for j in range(6)]
for i in range(6):
    assigned[i][4] = i

didassignments = False
Exemple #22
0
Target = cv.CreateImage(cv.GetSize(Img), cv.IPL_DEPTH_8U, 3)
Grid = cv.CreateImage(cv.GetSize(Img), cv.IPL_DEPTH_8U, 3)
Mask = cv.CreateImage(cv.GetSize(Img), cv.IPL_DEPTH_8U, 3)
Peephole = cv.CreateImage(cv.GetSize(Img), cv.IPL_DEPTH_8U, 3)
cv.Set(Mask, cv.Scalar(0x00, 0x00, 0xff))
Display = cv.CreateImage(cv.GetSize(Img), cv.IPL_DEPTH_8U, 3)
cv.Set(Grid, cv.Scalar(0, 0, 0))
Blank = cv.CreateImage(cv.GetSize(Img), cv.IPL_DEPTH_8U, 3)
cv.Set(Blank, cv.Scalar(0, 0, 0))
Hex = cv.CreateImage(cv.GetSize(Img), cv.IPL_DEPTH_8U, 3)
cv.Set(Hex, cv.Scalar(0, 0, 0))

FontSize = 1.0
Font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX,
                   hscale=FontSize,
                   vscale=1.0,
                   shear=0,
                   thickness=1,
                   lineType=8)


def get_pixel(x, y):
    return Target[x, y][0] + Target[x, y][1] + Target[x, y][2]


# create binary printable string
def to_bin(x):
    return ''.join(x & (1 << i) and '1' or '0' for i in range(7, -1, -1))


def redraw_grid():
    global Grid
Exemple #23
0
 def initRecorder(self): #Create the recorder
     codec = cv.CV_FOURCC('M', 'J', 'P', 'G')
     self.writer=cv.CreateVideoWriter(datetime.now().strftime("%b-%d_%H_%M_%S")+".wmv", codec, 5, cv.GetSize(self.frame), 1)
     #FPS set to 5 because it seems to be the fps of my cam but should be ajusted to your needs
     self.font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 2, 8) #Creates a font
#!/usr/bin/python
from openni import *
import cv2.cv as cv

cv.NamedWindow('Video', 1)
cv.MoveWindow('Video', 0, 0)
cv.NamedWindow('Profundidade', 1)
cv.MoveWindow('Profundidade', 650, 0)
fonte_do_texto = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.7, 0.7, 0, 2, 8)

depth_cv = cv.CreateImage((640, 480), cv.IPL_DEPTH_8U, 1)
imagem_cv = cv.CreateImage((640, 480), cv.IPL_DEPTH_8U, 3)

ctx = Context()
ctx.init()

video = ImageGenerator()
video.create(ctx)
video.set_resolution_preset(RES_VGA)
video.fps = 30

depth = DepthGenerator()
depth.create(ctx)
depth.set_resolution_preset(RES_VGA)
depth.fps = 30

#depth.alternative_view_point_cap.set_view_point(video)

gesture_generator = GestureGenerator()
gesture_generator.create(ctx)
gesture_generator.add_gesture('Wave')
Exemple #25
0
import math
'''
Find 2 D barcode based on up to 3 channel datamatrix
'''


def absnorm8(im, im8):
    """ im may be any single-channel image type.  Return an 8-bit version, absolute value, normalized so that max is 255 """
    (minVal, maxVal, _, _) = cv.MinMaxLoc(im)
    cv.ConvertScaleAbs(im, im8, 255 / max(abs(minVal), abs(maxVal)), 0)
    return im8


font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX,
                   1.0,
                   1.0,
                   thickness=2,
                   lineType=cv.CV_AA)
if 0:
    started = time.time()
    print dm_write.decode(bg.width,
                          bg.height,
                          buffer(bg.tostring()),
                          max_count=1,
                          min_edge=12,
                          max_edge=13,
                          shape=DataMatrix.DmtxSymbol10x10)  # , timeout = 10)
    print "took", time.time() - started


class DmtxFinder:
Exemple #26
0
import pyautogui
import cv2
import cv2.cv as cv

#using opencv1.0 functions
camera = cv.CaptureFromCAM(0)

#initializing font, color and variables
font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 1, 0, 2, 8)
color = (0, 0, 0)
point1 = (300, 200)
point2 = (400, 300)
flag = 0
while True:

    #grabbing a frame, applying blur, flipping the image
    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)
Exemple #27
0
 def initRecorder(self): #Create the recorder
     codec = cv.CV_FOURCC('D', 'I', 'V', 'X')
     #codec = cv.CV_FOURCC("D", "I", "B", " ")
     self.writer=cv.CreateVideoWriter(datetime.now().strftime("%b-%d_%H:%M:%S")+".avi", codec, 15, cv.GetSize(self.frame), 1)
     #FPS set at 15 because it seems to be the fps of my cam but should be ajusted to your needs
     self.font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 2, 8) #Creates a font
Exemple #28
0
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("7.jpg", dst)
# LPLS ruihua
img = cv2.imread("test.jpg")

gray_lap = cv2.Laplacian(img, cv2.CV_16S, ksize=3)
dst2 = cv2.convertScaleAbs(gray_lap)

cv2.imshow('laplacian', dst2)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("8.jpg", dst2)

# add number

#cv.CV_WINDOW_AUTOSIZE这个参数设定显示窗口虽图片大小自动变化
cv.NamedWindow('You need to struggle', cv.CV_WINDOW_AUTOSIZE)
image = cv.LoadImage('test.jpg')

#创建一个矩形,来让我们在图片上写文字,参数依次定义了文字类型,高,宽,字体厚度等。。
font = cv.InitFont(cv.CV_FONT_HERSHEY_SCRIPT_SIMPLEX, 1, 1, 0, 3, 8)

#将文字框加入到图片中,(5,20)定义了文字框左顶点在窗口中的位置,最后参数定义文字颜色
cv.PutText(image, "3160602004", (30, 30), font, (255, 0, 0))

cv.ShowImage('You need to struggle', image)
cv.WaitKey(0)
cv.SaveImage('9.jpg', image)
Exemple #29
0
subject = str(input("Subject: "))
video = str(input("Video: "))

if (os.path.isfile('data/' + subject + '/' + video +
                   '/diagnostics.txt')) == True:
    print(
        "Diagnostics file exists! System will not overwrite without deleting it first."
    )
    sys.exit()

cv.NamedWindow("camera", 1)
capture = cv.CreateCameraCapture(0)

f = open('data/' + subject + '/' + video + '/diagnostics.txt', 'w')
#font = cv.CvFont
font = cv.InitFont(1, 1, 1, 1, 1, 1)

width = None
height = None
width = 320
height = 240

if width is None:
    width = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH))
else:
    cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, width)

if height is None:
    height = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT))
else:
    cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, height)
Exemple #30
0
    def sd_loop(self):
        """
        The main seizure detector loop - call this function to start
        the seizure detector.
        """
        self.timeSeries = []  # array of times that data points were collected.
        self.maxFreq = None
        if (self.X11):
            cv.NamedWindow('Seizure_Detector', cv.CV_WINDOW_AUTOSIZE)
            cv.CreateTrackbar('FeatureTrackbar', 'Seizure_Detector', 0,
                              self.MAX_COUNT, self.onTrackbarChanged)
        font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, 8)

        # Intialise the video input source
        # ('camera' - may be a file or network stream though).
        #camera = cv.CaptureFromFile("rtsp://192.168.1.18/live_mpeg4.sdp")
        #camera = cv.CaptureFromFile("../testcards/testcard.mpg")
        #camera = cv.CaptureFromFile("/home/graham/laura_sample.mpeg")
        camera = cv.CaptureFromCAM(0)

        # Set the VideoWriter that produces the output video file.
        frameSize = (640, 480)
        videoFormat = cv.FOURCC('p', 'i', 'm', '1')
        # videoFormat = cv.FOURCC('l','m','p','4')
        vw = cv.CreateVideoWriter(self.videoOut, videoFormat, self.outputfps,
                                  frameSize, 1)
        if (vw == None):
            print "ERROR - Failed to create VideoWriter...."

        # Get the first frame.
        last_analysis_time = datetime.datetime.now()
        last_feature_search_time = datetime.datetime.now()
        last_frame_time = datetime.datetime.now()
        frame = cv.QueryFrame(camera)

        print "frame="
        print frame

        # Main loop - repeat forever
        while 1:
            # Carry out initialisation, memory allocation etc. if necessary
            if self.image is None:
                self.image = cv.CreateImage(cv.GetSize(frame), 8, 3)
                self.image.origin = frame.origin
                grey = cv.CreateImage(cv.GetSize(frame), 8, 1)
                prev_grey = cv.CreateImage(cv.GetSize(frame), 8, 1)
                pyramid = cv.CreateImage(cv.GetSize(frame), 8, 1)
                prev_pyramid = cv.CreateImage(cv.GetSize(frame), 8, 1)
                # self.features = []

            # copy the captured frame to our self.image object.
            cv.Copy(frame, self.image)

            # create a grey version of the image
            cv.CvtColor(self.image, grey, cv.CV_BGR2GRAY)

            # Look for features to track.
            if self.need_to_init:
                #cv.ShowImage ('loop_grey',grey)
                self.initFeatures(grey)
                self.timeSeries = []
                self.maxFreq = None
                last_analysis_time = datetime.datetime.now()
                self.need_to_init = False

            # Now track the features, if we have some.
            if self.features != []:
                # we have points to track, so track them and add them to
                # our time series of positions.
                self.features, status, track_error = cv.CalcOpticalFlowPyrLK(
                    prev_grey, grey, prev_pyramid, pyramid, self.features,
                    (self.win_size, self.win_size), 3,
                    (cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03),
                    self.flags)
                self.timeSeries.append((last_frame_time, self.features))
                print "Features..."
                for featNo in range(len(self.features)):
                    if (status[featNo] == 0):
                        self.features[featNo] = (-1, -1)
                    print status[featNo], self.features[featNo]
                # and plot them.
                for featNo in range(len(self.features)):
                    pointPos = self.features[featNo]
                    cv.Circle(self.image, (int(pointPos[0]), int(pointPos[1])),
                              3, (0, 255, 0, 0), -1, 8, 0)
                    if (self.alarmActive[featNo] == 2):
                        cv.Circle(self.image,
                                  (int(pointPos[0]), int(pointPos[1])), 10,
                                  (0, 0, 255, 0), 5, 8, 0)
                    if (self.alarmActive[featNo] == 1):
                        cv.Circle(self.image,
                                  (int(pointPos[0]), int(pointPos[1])), 10,
                                  (0, 0, 255, 0), 2, 8, 0)

                    # there will be no maxFreq data until we have
                    # run doAnalysis for the first time.
                    if (not self.maxFreq == None):
                        msg = "%d-%3.1f" % (featNo, self.maxFreq[featNo])
                        cv.PutText(
                            self.image, msg,
                            (int(pointPos[0] + 5), int(pointPos[1] + 5)), font,
                            (255, 255, 255))
                # end of for loop over features
            else:
                #print "Oh no, no features to track, and you haven't told me to look for more."
                # no features, so better look for some more...
                self.need_to_init = True

            # Is it time to analyse the captured time series.
            if ((datetime.datetime.now() - last_analysis_time).total_seconds()
                    > self.Analysis_Period):
                if (len(self.timeSeries) > 0):
                    self.doAnalysis()
                    self.doAlarmCheck()
                    last_analysis_time = datetime.datetime.now()
                else:
                    # print "Not doing analysis - no time series data..."
                    a = True

            # Is it time to re-acquire the features to track.
            if ((datetime.datetime.now() -
                 last_feature_search_time).total_seconds() >
                    self.Feature_Search_Period):
                print "resetting..."
                last_feature_search_time = datetime.datetime.now()
                self.need_to_init = True

            # save current data for use next time around.
            prev_grey, grey = grey, prev_grey
            prev_pyramid, pyramid = pyramid, prev_pyramid

            # we can now display the image
            if (self.X11): cv.ShowImage('Seizure_Detector', self.image)
            cv.WriteFrame(vw, self.image)

            # handle events
            c = cv.WaitKey(10)
            if c == 27:
                # user has press the ESC key, so exit
                break

            # Control frame rate by pausing if we are going too fast.
            frameTime = (datetime.datetime.now() - last_frame_time)\
                .total_seconds()
            actFps = 1.0 / frameTime
            if (frameTime < 1 / self.inputfps):
                cv.WaitKey(1 + int(1000. * (1. / self.inputfps - frameTime)))

            # Grab the next frame
            last_frame_time = datetime.datetime.now()
            frame = cv.QueryFrame(camera)