Ejemplo n.º 1
0
def main(args):
    """Sets up object detection according to the provided args."""

    # If no number of workers are specified, use all available GPUs
    input_q = Queue(maxsize=args.queue_size)
    output_q = Queue(maxsize=args.queue_size)
    draw_proc = Process(target=draw_worker,
                        args=(
                            input_q,
                            output_q,
                            args.detect_workers,
                            args.track_gpu_id,
                            args.rows,
                            args.cols,
                            args.detect_rate,
                        ))
    draw_proc.start()

    if args.stream:
        print('Reading from hls stream.')
        video_capture = HLSVideoStream(src=args.stream).start()
    elif args.video_path:
        print('Reading from local video.')
        video_capture = LocalVideoStream(src=args.video_path,
                                         width=args.width,
                                         height=args.height).start()
    else:
        print('Reading from webcam.')
        video_capture = LocalVideoStream(src=args.video_source,
                                         width=args.width,
                                         height=args.height).start()

    video_out = None
    if args.video_out_fname is not None:
        video_out = cv2.VideoWriter(
            args.video_out_fname, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'),
            OUTPUT_FRAME_RATE, (video_capture.WIDTH, video_capture.HEIGHT))

    fps = FPS().start()
    while True:  # fps._numFrames < 120
        try:
            frame = video_capture.read()
            input_q.put(frame)
            start_time = time.time()

            output_rgb = cv2.cvtColor(output_q.get()[0], cv2.COLOR_RGB2BGR)
            if args.show_frame:
                cv2.imshow('Video', output_rgb)
            if video_out is not None:
                video_out.write(output_rgb)
            fps.update()

            print('[INFO] elapsed time: {:.2f}'.format(time.time() -
                                                       start_time))

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        except (KeyboardInterrupt, SystemExit):
            if video_out is not None:
                video_out.release()
            break

    fps.stop()
    print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed()))
    print('[INFO] approx. FPS: {:.2f}'.format(fps.fps()))

    if video_out is not None:
        video_out.release()
    draw_proc.join()
    video_capture.stop()
    cv2.destroyAllWindows()
    pool = Pool(args.num_workers, worker, (input_q, output_q))

    if (args.stream):
        print('Reading from hls stream.')
        video_capture = HLSVideoStream(src=args.stream).start()
    else:
        print('Reading from webcam.')
        video_capture = WebcamVideoStream(src=args.video_source,
                                          width=args.width,
                                          height=args.height).start()

    fps = FPS().start()

    while True:  # fps._numFrames < 120
        receiving = True
        frame = video_capture.read()
        input_q.put(frame)

        t = time.time()

        output_rgb = cv2.cvtColor(output_q.get(), cv2.COLOR_RGB2BGR)
        cv2.imshow('Video', output_rgb)
        fps.update()

        print('[INFO] elapsed time: {:.2f}'.format(time.time() - t))

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    fps.stop()
    print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed()))
    fps = FPS().start()


    args.width = int(video_capture.WIDTH * FEED_IMAGE_HEIGHT / video_capture.HEIGHT) # int(video_capture.WIDTH * IMAGE_SCALE_FACTOR)
    args.height = FEED_IMAGE_HEIGHT # int(video_capture.HEIGHT * IMAGE_SCALE_FACTOR)

    print("args width" + str(args.width))
    print("args height" + str(args.height))

    last_bus_points = []
    
    saved_buses = 100 # I set this manually to start overwriting at the right place

    while True:

        original_frame = video_capture.read()
        
        # frame = cv2.resize(original_frame.copy(), (int(video_capture.WIDTH * IMAGE_SCALE_FACTOR), int(video_capture.HEIGHT * IMAGE_SCALE_FACTOR)))
        frame = cv2.resize(original_frame.copy(), (args.width, args.height))


        # print(str(original_frame.shape))
        # print(str(frame.shape))
        input_q.put((frame, original_frame))

        t = time.time()

        if output_q.empty():
            pass  # fill up queue
        else:
            font = cv2.FONT_HERSHEY_SIMPLEX