Пример #1
0
 def test_FPS():
     import time
     p_fps = pb.FPS()
     key = -1
     time.time()
     for x in range(100):
         begin = time.time()
         time.sleep(1 / 30)
         if x % 5 == 0:
             print('fps = {:.6f}, {:.6f}'.format(p_fps.get(5),
                                                 1 / (time.time() - begin)))
     return
Пример #2
0
 def test_VideoCaptureThreadRelink():
     cap = pb.video.VideoCaptureThreadRelink(r'G:\hand\246.mp4')
     key = 0
     fps = pb.FPS()
     while (key != 27):
         print(fps.get())
         ret, img = cap.read()
         if ret:
             cv2.imshow('img', img)
         key = cv2.waitKey(1)
     cap.release()
     print('finish')
Пример #3
0
    def test_opticalFlow():
        cap = cv2.VideoCapture(r'G:\hand\246.mp4')
        fps = pb.FPS()
        key = 0
        ret = False
        begin_num = 900
        while ret == False or begin_num > 0:
            begin_num -= 1
            ret, preimg = cap.read()
            prevgray = cv2.cvtColor(preimg, cv2.COLOR_BGR2GRAY)
        while key != 27:
            ret, img = cap.read()
            if ret:
                #cal flow
                gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
                flow = cv2.calcOpticalFlowFarneback(prevgray, gray, None, 0.5,
                                                    3, 15, 3, 5, 1.2, 0)
                #draw
                drew_img = img.copy()
                pb.video.drawOpticalFlow(drew_img, flow, 20)
                img2show = cv2.resize(drew_img, (720, 480))
                cv2.putText(img2show,
                            str(img.shape[1]) + ' x ' + str(img.shape[0]),
                            (360, 30), cv2.FONT_HERSHEY_COMPLEX, 0.75,
                            (0, 255, 0))
                cv2.putText(img2show, str(round(fps.get(), 3)), (525, 30),
                            cv2.FONT_HERSHEY_COMPLEX, 0.75, (0, 0, 255))
                cv2.imshow('show', img2show)
                #make flow color
                color_flow = pb.video.opticalFlowToColorMap(flow)
                color2show = cv2.resize(color_flow, (720, 480))
                cv2.imshow('color_flow', color2show)
                cv2.imshow('color_ring', pb.video.colorRing())

            else:
                break
            key = cv2.waitKey(1)
            prevgray = gray
Пример #4
0
def record_videos(cap_list, save_dir, config):
    print('************** Control ***************')
    print('q    quit')
    print('r    run video recording')
    print('s    stop video recording')
    print('**************************************')
    import time
    ret, key = True, 0
    save_path = os.path.join(save_dir, config['SAVE_FILE_NAME'])
    wcap_list = list()
    state = 'WAITING'
    p_fps = pb.FPS()
    while key != ord('q') and key != ord('Q'):
        # read
        fps = p_fps.get()
        imgs = list()
        ret = False
        for i, cap in enumerate(cap_list):
            _ret, _img = cap.read()
            ret = ret or _ret
            if _ret:
                imgs.append((i, _ret, _img))
            else:
                cv2.destroyWindow('view{0}'.format(i))

        # show while waiting
        if state == 'WAITING':
            for i, _ret, _img in imgs:
                if _ret:
                    cv2.imshow('view{0}'.format(i), _img)
            key = cv2.waitKey(1)

        #create video
        if key == ord('r') or key == ord('R'):
            if state == 'WAITING':
                date_time = get_date_time_string()
                date_time = date_time[0] + '_' + date_time[1]
                for wcap in wcap_list:
                    if wcap is not None:
                        wcap.release()
                wcap_list.clear()
                for i, _ret, _img in imgs:
                    if _ret:
                        v_name = config['PRIVATE_WRITER_SET'][i][
                            'filename'].format(i, date_time)
                        v_path = os.path.join(save_dir, v_name)
                        v_fourcc = config['PRIVATE_WRITER_SET'][i]['fourcc']
                        v_fps = int(fps) - 1 if config['PRIVATE_WRITER_SET'][
                            i].get('fps') is None else config[
                                'PRIVATE_WRITER_SET'][i]['fps']
                        v_wh = (_img.shape[1], _img.shape[0])
                        wcap = cv2.VideoWriter(v_path, v_fourcc, v_fps, v_wh)
                        if wcap.isOpened():
                            print('Created: {0}'.format(v_path))
                            wcap_list.append(wcap)
                            state = 'RUNNING'
                        else:
                            print('create error')
                            wcap_list.append(None)
            else:
                print("ERROR: please stop recording first.")

        # stop recording
        if key == ord('s') or key == ord('S'):
            if state == 'RUNNING':
                state = 'WAITING'
                print('Record video finish')
            else:
                print("ERROR: please run recording first.")

        # show while running
        if state == 'RUNNING':
            for i, _ret, _img in imgs:
                wcap = wcap_list[i]
                if wcap is not None and _ret:
                    wcap.write(_img)
                    cv2.circle(_img, (int(_img.shape[1] / 2), 30), 10,
                               (0, 0, 255), 20)
                    cv2.imshow('view{0}'.format(i), _img)
            key = cv2.waitKey(1)
    return