Esempio n. 1
0
def simple_manager(video, stream, fps):
    # Create a manager passing video input
    # Also I inform if it is an stream or not (it is not completly necessary but
    # in some cases is usefull)
    # Finally I put a limit of FPS (this is more important if it is not an stream,
    # by default it will try to consume the video as fast as possible, and you could
    # want to show it at real time)
    manager_cv2 = ManagerCV2(cv2.VideoCapture(video),
                             is_stream=stream,
                             fps_limit=fps)

    # I decide that I want to exit by pressing `esc` (if you want to see more
    # about this, check the example `keystroke.py`)
    manager_cv2.add_keystroke(27, 1, exit=True)

    # With that simple for you will get your video, frame by frame 52% faster
    # than if you use the typical `while True`
    for frame in manager_cv2:
        cv2.imshow('Example easy manager', frame)

    cv2.destroyAllWindows()

    # You can check easily your frame rate at any time (you don't need to wait
    # unitl the for ends)
    print('FPS: {}'.format(manager_cv2.get_fps()))
Esempio n. 2
0
def key_manager(video, stream, fps):
    # Step 1: Create ManagerCV2 as in `basci.py`
    manager_cv2 = ManagerCV2(cv2.VideoCapture(video),
                             is_stream=stream,
                             fps_limit=fps)

    # Step 2: Add as much keystrokes as you want
    # Have in mind that each keystroke will be associated to a list of attributes
    # Each time you press an added key, the associated attributes will change theis
    # values, so if it was at False, it will change to True and vice versa
    # The second param means miliseconds to wait (if <= 0 it will stop until prressed)

    # I decide to add an exit keystroke pressing `esc` (it is asociated with an empty list of attributes)
    manager_cv2.add_keystroke(27, 1, exit=True)
    # Press `h` to the attribute `hello` to True
    # Then the same with the other ones...
    manager_cv2.add_keystroke(ord('h'), 1, 'hello')
    manager_cv2.add_keystroke(ord('f'), 1, 'flip')
    manager_cv2.add_keystroke(ord('g'), 1, 'gray')
    manager_cv2.add_keystroke(ord('c'), 1, 'cartoon')

    # Basic for (check `basic.py`)
    for frame in manager_cv2:
        # I want to print only one time hello when someone activates this flag
        if manager_cv2.key_manager.hello:
            print('Hello world')
            manager_cv2.key_manager.hello = False

        # Manage the flip
        if manager_cv2.key_manager.flip:
            frame = cv2.flip(frame, 1)

        # Manage if you want to draw the cartoon
        if manager_cv2.key_manager.cartoon:
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            gray_blur = cv2.medianBlur(gray, 5)
            edges = cv2.adaptiveThreshold(gray_blur, 255,
                                          cv2.ADAPTIVE_THRESH_MEAN_C,
                                          cv2.THRESH_BINARY, 9, 5)

            # If it is also gray have in mind
            if manager_cv2.key_manager.gray:
                color = cv2.bilateralFilter(gray, 9, 300, 300)
            else:
                color = cv2.bilateralFilter(frame, 9, 300, 300)

            frame = cv2.bitwise_and(color, color, mask=edges)

        # If it is not a cartoon but it is gray
        elif manager_cv2.key_manager.gray:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # Finally show the generated frame
        cv2.imshow("Frame", frame)

    cv2.destroyAllWindows()
Esempio n. 3
0
    def test_fast_counter(self):
        cap = cv2.VideoCapture('media/sample_video.mp4')
        total_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
        manager_cv2 = ManagerCV2(cap)

        for _ in manager_cv2:
            pass

        self.assertEqual(int(total_frames), manager_cv2.count_frames,
                         'Examinated frames should be equals')
Esempio n. 4
0
def main():
    manager_cv2 = ManagerCV2(cv2.VideoCapture(0), is_stream=True, fps_limit=60)
    manager_cv2.add_keystroke(27, 1, exit=True)
    manager_cv2.add_keystroke(ord('q'), 1, 'q')
    for frame in manager_cv2:
        if manager_cv2.key_manager.q:
            print('Q pressed')
            manager_cv2.key_manager.q = False
        frame = cv2.flip(frame, 1)
        cv2.imshow('Example easy manager', frame)
    cv2.destroyAllWindows()
    print('FPS: {}'.format(manager_cv2.get_fps()))
Esempio n. 5
0
def main(video_path, images_output_path):
    classes_to_sub_images = {
        'car': [],
        'motorbike': [],
        'pickup': [],
        #'truck':[],
        #'van':[]
    }

    frames_to_skip = 50
    limit_per_class = 100

    if isdir(video_path):
        for frame_path in glob(join(video_path, '*.jpg')):
            frame = cv2.imread(frame_path)
            extra_info = '(Frame: {})'.format(frame_path.split('/')[-1])
            classes_to_sub_images = process_frame(frame,
                                                  classes_to_sub_images,
                                                  limit_per_class,
                                                  extra_info=extra_info)

    else:
        manager_cv2 = ManagerCV2(cv2.VideoCapture(video_path))
        manager_cv2.add_keystroke(27, 1, exit=True)  # Exit when `Esc`
        manager_cv2.add_keystroke(ord(' '), 1, 'action')

        #min_width, min_height = 1000, 1000 # dummy values
        #nearest_power_of_2 = lambda x: 2**(x-1).bit_length()

        for frame in manager_cv2:
            if manager_cv2.count_frames % frames_to_skip != 0:
                continue

            extra_info = '(Frame: {})'.format(manager_cv2.count_frames)
            classes_to_sub_images = process_frame(frame,
                                                  classes_to_sub_images,
                                                  limit_per_class,
                                                  extra_info=extra_info)

    # Change min width and height to the nearest power of 2 (bigger or equal than the value)
    #min_width = nearest_power_of_2(min_width)
    #min_height = nearest_power_of_2(min_height)

    min_width = 64
    min_height = 64

    for current_class, images in classes_to_sub_images.items():
        save_images_by_class(
            images_output_path, current_class,
            [cv2.resize(x, (min_width, min_height)) for x in images])
 def __init__(self,
              video,
              stream,
              fps,
              dilate=False,
              detect_scenes=False,
              name='MovementDetector'):
     self.dilate = dilate
     self.name = name
     self.manager_cv2 = ManagerCV2(cv2.VideoCapture(video),
                                   is_stream=stream,
                                   fps_limit=fps,
                                   detect_scenes=detect_scenes)
     self.manager_cv2.add_keystroke(27, 1, exit=True)  # Exit when `Esc`
     self.manager_cv2.add_keystroke(ord(' '), 1, 'action')
Esempio n. 7
0
def simple_selector(video, stream, fps):
    manager_cv2 = ManagerCV2(cv2.VideoCapture(video),
                             is_stream=stream,
                             fps_limit=fps)
    manager_cv2.add_keystroke(27, 1, exit=True)
    manager_cv2.add_keystroke(ord('+'), 1, 'grow')
    manager_cv2.add_keystroke(ord('-'), 1, 'shrink')
    manager_cv2.add_keystroke(ord('w'), 1, 'up')
    manager_cv2.add_keystroke(ord('a'), 1, 'left')
    manager_cv2.add_keystroke(ord('s'), 1, 'down')
    manager_cv2.add_keystroke(ord('d'), 1, 'right')
    manager_cv2.add_keystroke(ord(' '), 1, 'track')

    # Im preparing a selection with the coordinates (x1,y1,x2,y2):
    #   x1,y1 -------- x2,y1
    #    |               |
    #   x1,y2 -------- x2,y2
    selection = (0.1, 0.1, 0.3, 0.3)
    tracking = False

    for frame in manager_cv2:
        '''
            For this example controles are:
                - `+` to grow up selection
                - `-` to shrink down selection
                - `w`, `a`, `s`, `d` to move the selection
        '''
        selection = manage_keys(manager_cv2, selection)

        selector = SelectorCV2(color=(0, 0, 200), filled=True, normalized=True)
        selector.add_zone(selection)

        if manager_cv2.key_manager.track:
            if not tracking:
                manager_cv2.set_tracking(selector, frame)
            else:
                selector = manager_cv2.get_tracking(frame)
                selection = selector.zones[0]
            tracking = True
        else:
            tracking = False

        # Finally when you execute the draw function it actually prints all the selections
        # until now it didn't print anything, only store the data.
        frame = selector.draw(frame)
        cv2.imshow("Frame", frame)

    print(manager_cv2.get_fps())
Esempio n. 8
0
def simple_selector(video, stream, fps):
    manager_cv2 = ManagerCV2(cv2.VideoCapture(video),
                             is_stream=stream,
                             fps_limit=fps)
    manager_cv2.add_keystroke(27, 1, exit=True)
    manager_cv2.add_keystroke(ord('+'), 1, 'grow')
    manager_cv2.add_keystroke(ord('-'), 1, 'shrink')
    manager_cv2.add_keystroke(ord('w'), 1, 'up')
    manager_cv2.add_keystroke(ord('a'), 1, 'left')
    manager_cv2.add_keystroke(ord('s'), 1, 'down')
    manager_cv2.add_keystroke(ord('d'), 1, 'right')

    # Im preparing a selection with the coordinates (x1,y1,x2,y2):
    #   x1,y1 -------- x2,y1
    #    |               |
    #   x1,y2 -------- x2,y2
    selection = (10, 10, 60, 60)

    for frame in manager_cv2:
        '''
            For this example controles are:
                - `+` to grow up selection
                - `-` to shrink down selection
                - `w`, `a`, `s`, `d` to move the selection
        '''
        selection = manage_keys(manager_cv2, selection)

        # The best way to make it works is creating the selection inside the loop
        # You can specify lot of optional parameters like:
        # alpha, color, polygon_color, normalized, thickness, filled, peephole, closed_polygon and show_vertexes
        # Check documentation for more information of each one
        selector = SelectorCV2(color=(0, 0, 200), filled=True)

        # You can add multiple zones to a selector
        # Each zone must receive a `selection`, it also could receive `tags` (it could be
        # a string or a list of strings)
        # There is another optional parameter `specific_properties`
        # This is a dictionary. The keys are the name of the attributes that you want
        # to modify from the original selector (so if you want to draw with the same selector
        # two selections in a different way, you can do it), ofcourse the value is
        # the specific value that you want for this parameter
        selector.add_zone(selection, tags=['Some tags', 'With\ngroups'])

        # Finally when you execute the draw function it actually prints all the selections
        # until now it didn't print anything, only store the data.
        frame = selector.draw(frame)
        cv2.imshow("Frame", frame)
Esempio n. 9
0
def scenes_detector(video, stream, fps):
    manager_cv2 = ManagerCV2(cv2.VideoCapture(video),
                             is_stream=stream,
                             fps_limit=fps,
                             detect_scenes=True)
    manager_cv2.add_keystroke(27, 1, exit=True)

    count = 0
    for frame in manager_cv2:
        selector = SelectorCV2(color=(200, 90, 0), filled=True)
        if manager_cv2.new_scene:
            count += 1
        selector.add_free_tags((-10, -10), 'Scene: {}'.format(count))
        frame = selector.draw(frame)
        cv2.imshow('Example easy manager', frame)

    cv2.destroyAllWindows()

    # You can check easily your frame rate at any time (you don't need to wait
    # unitl the for ends)
    print('FPS: {}'.format(manager_cv2.get_fps()))
Esempio n. 10
0
def simple_selector(video, stream, fps):
    manager_cv2 = ManagerCV2(cv2.VideoCapture(video),
                             is_stream=stream,
                             fps_limit=fps)
    manager_cv2.add_keystroke(27, 1, exit=True)
    manager_cv2.add_keystroke(
        ord('c'), 1,
        'close')  # It will manage if we want to close the polygon or not
    manager_cv2.add_keystroke(ord('b'), 1,
                              'box')  # It will add or not the surrounding box

    cv2.namedWindow("Polygon")
    cv2.setMouseCallback("Polygon", mouse_drawing)

    for frame in manager_cv2:
        # The best way to make it works is creating the selection inside the loop
        # You can specify lot of optional parameters like:
        # alpha, color, polygon_color, normalized, thickness, filled, peephole, closed_polygon and show_vertexes
        # Check documentation for more information of each one
        selector = SelectorCV2(color=(0, 100, 220),
                               filled=True,
                               thickness=3,
                               closed_polygon=manager_cv2.key_manager.close,
                               polygon_color=(170, 25, 130),
                               show_vertexes=True)

        # We add the polygon specifying the vertexes (list of touples of two elements)
        # Also optionaly, we decide to add a surrounding box to the polygon if we press the box key,
        # and for this example we also add a tag, but this is not necessary
        selector.add_polygon(vertex,
                             surrounding_box=manager_cv2.key_manager.box,
                             tags='This is the first\nPolygon')

        # Finally when you execute the draw function it actually prints all the selections
        # until now it didn't print anything, only store the data.
        frame = selector.draw(frame)
        cv2.imshow("Polygon", frame)
Esempio n. 11
0
def detection(video, stream, fps, scale, details):
    manager_cv2 = ManagerCV2(cv2.VideoCapture(video),
                             is_stream=stream,
                             fps_limit=fps)
    manager_cv2.add_keystroke(27, 1, exit=True)
    manager_cv2.add_keystroke(ord('d'), 1, 'detect')
    manager_cv2.key_manager.detect = True  # I want to start detecting
    selector = None
    frames_tracking = 0

    for frame in manager_cv2:
        if manager_cv2.key_manager.detect:
            # I'm using the frame counter from the manager
            # So I will recalculate de detection each 20 frames
            # The other ones, I will continue using the same selector (so it will not change)
            #   NOTE: It is == 1 (and not == 0) because in the first iteration we
            #    get the first frame, so count_frames = 1.
            #    This is how I manage to ensure that in the first loop it is True
            #    so selector exist and doesn't raise an error.
            if manager_cv2.count_frames % 20 == 1:
                new_selector = face_detector(frame, scale, details)
                if not selector or frames_tracking >= 30 or len(
                        new_selector.zones) >= len(selector.zones):
                    selector = new_selector
                    frames_tracking = 0
                manager_cv2.set_tracking(selector, frame)
            else:
                # The other frames I wil get the tracking of the last detections
                selector = manager_cv2.get_tracking(frame)
                frames_tracking += 1
            frame = selector.draw(frame)

        cv2.imshow('Face detection example', frame)

    print('FPS: {}'.format(manager_cv2.get_fps()))
    cv2.destroyAllWindows()
Esempio n. 12
0
        '-d',
        '--details',
        help=
        'if you pass it, it means that you want to see the facial vertexes',
        action='store_true')

    parser.add_argument(
        '--scale',
        type=float,
        default=1,
        help=
        'optional parameter to resize the input to the face_recognition library'
    )

    parser.add_argument(
        '-f',
        '--fps',
        default=0,
        help=
        'int parameter to indicate the limit of FPS (default 0, it means no limit)',
        type=int)

    args = parser.parse_args()

    if type(args.video) is str and args.video.isdigit():
        args.video = int(args.video)
    manager_cv2 = ManagerCV2(cv2.VideoCapture(0), is_stream=True)
for frame in manager_cv2:
    cv2.imshow('Mug Shots', frame)
cv2.destroyAllWindows()
print(manager_cv2.get_fps())