コード例 #1
0
def main():
    input_q = Queue(maxsize=5)
    output_q = Queue(maxsize=5)

    cap = cv2.VideoCapture(0)
    ret, frame = cap.read()

    height, width, _ = frame.shape
    frame = cv2.resize(frame, (width // 2, height // 2))
    height, width, _ = frame.shape
    pong = Pong(h=height,
                w=width,
                default_ball_dx=width // 100,
                default_ball_dy=height // 100,
                default_paddle_speed=height // 100,
                default_half_paddle_height=height // 10)
    i = 0

    # parallelize
    cap_params = {}
    frame_processed = 0
    cap_params['im_width'], cap_params['im_height'] = (width, height)
    cap_params['score_thresh'] = 0.5
    cap_params['num_hands_detect'] = 1
    cap_params['pong'] = pong
    pool = Pool(2, worker, (input_q, output_q, cap_params, frame_processed))

    while True:
        i += 1
        ret, frame = cap.read()
        frame = cv2.resize(frame, (width, height))
        frame = cv2.flip(frame, 1)  # flip across vertical axis

        # wait for keys
        key = cv2.waitKey(100)
        pong.on_key(key)

        input_q.put(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
        frame, box, score = output_q.get()
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
        update_pong_with_boxes_scores([box], [score], pong, height)

        # update game
        ended = pong.update()
        pong.draw(frame)

        # Display the resulting frame
        cv2.imshow('frame', frame)
        if pong.is_key(key, 'q') or ended:
            break
コード例 #2
0
ファイル: main.py プロジェクト: rtb7syl/hand-tracking-pong
def main():
    cap = cv2.VideoCapture(0)
    detection_graph, sess = detector_utils.load_inference_graph()
    ret, frame = cap.read()
    height, width, _ = frame.shape
    frame = cv2.resize(frame, (width // 2, height // 2))
    height, width, _ = frame.shape

    pong = Pong(h=height,
                w=width,
                default_ball_dx=width // 100,
                default_ball_dy=height // 100,
                default_paddle_speed=height // 100,
                default_half_paddle_height=height // 10)
    i = 0

    while True:
        i += 1
        ret, frame = cap.read()
        frame = cv2.resize(frame, (width, height))
        frame = cv2.flip(frame, 1)  # flip across vertical axis

        # wait for keys
        key = cv2.waitKey(100)
        pong.on_key(key)

        boxes, scores = detector_utils.detect_objects(
            cv2.resize(frame, (320, 180)), detection_graph, sess)
        if boxes is not None and scores is not None:
            # draw bounding boxes
            detector_utils.draw_box_on_image(1, 0.5, scores, boxes, width,
                                             height, frame)
            update_pong_with_boxes_scores(boxes, scores, pong, height)

        # update game
        ended = pong.update()
        pong.draw(frame)

        # Display the resulting frame
        cv2.imshow('frame', frame)
        if pong.is_key(key, 'q') or ended:
            break