Example #1
0
 def run(self):
     global Prediction_Flag, Prediction_Perfect, exitFlag
     while exitFlag:
         if Prediction_Flag:
             print("Strat Prediction")
             for i in range(10):
                 threadLock.acquire()
                 ret_1, frame_1 = cap.read()
                 threadLock.release()
                 time.sleep(0.1)
                 if ret_1:
                     frame_1 = cv2.flip(frame_1, 1)  # 第二个参数大于0:就表示是沿y轴翻转
                     Box = dection.binaryMask(frame_1, dection.x0,
                                              dection.y0, 300, 300)
                     Box_P = cv2.resize(Box,
                                        (dection.width, dection.height),
                                        interpolation=cv2.INTER_CUBIC)
                     Box_P = np.reshape(Box_P,
                                        [dection.width, dection.height, 1])
                     Gesture.append(CNN.Gussgesture(Box_P))
                 else:
                     print('Predictive thread read failed, unpredictable!')
             Prediction_Perfect = True
             Prediction_Flag = False
         key = cv2.waitKey(5) & 0xFF
         if (key == 27):
             break
     cv2.destroyWindow('Thread_box')
Example #2
0
def Main():
    global x0, y0, binaryMode, saveImg, gesturename, banner, guessGesturees, path
    # 选择模式
    while (True):
        ans = int(input(banner))
        if (ans == 1):  # 训练模型 并保存
            #mod = CNN.load()
            print("········开始训练模型········")
            CNN.TRAIN()
            input("按任意键继续")
            break
        if (ans == 2):
            print("········载入模型·······")
            break
        else:
            print("········输入有误!········")
            return 0
    #打开摄像头
    cap = cv2.VideoCapture(0)
    while (True):
        #一帧一帧的捕捉视频
        ret, frame = cap.read()
        frame = cv2.flip(frame, 2)  # 图像翻转  如果不翻转,视频中看起来的刚好和我们是左右对称的

        roi = binaryMask(frame, x0, y0, width, height)

        cv2.putText(frame, "select: ", (fx, fy), font, size,
                    (0, 255, 0))  # 标注字体
        cv2.putText(frame, "b-'Binary Model'/ r- 'RGB Model' ", (fx, fy + fh),
                    font, size, (0, 255, 0))  # 标注字体
        cv2.putText(frame, "p-'predict Model", (fx, fy + 2 * fh), font, size,
                    (0, 255, 0))  # 标注字体
        cv2.putText(frame, "q-'quit'", (fx, fy + 3 * fh), font, size,
                    (0, 255, 0))  # 标注字体

        key = cv2.waitKey(1) & 0xFF
        if key == ord('b'):
            """切换到二值模式"""
            binaryMode = True
            print("已切换到二值模式")
        elif key == ord('r'):
            """切换到RGB模式"""
            binaryMode = False
            print("已切换到RGB模式……")
        if key == ord('p'):
            """调用模型开始预测, 对二值图像预测,所以要在二值函数里面调用,预测新采集的手势"""
            print()
            Roi = np.reshape(roi, [width, height, 1])
            gesture = CNN.Gussgesture(Roi)
            gesture_copy = gesture
            cv2.putText(frame, gesture_copy, (480, 440), font, 1,
                        (0, 0, 255))  # 标注字体
        if key == ord('q'):
            break
        #展示处理之后的视频帧
        cv2.imshow('frame', frame)
        if (binaryMode):
            cv2.imshow('ROI', roi)
        else:
            cv2.imshow("ROI", frame[y0:y0 + height, x0:x0 + width])
    #释放捕捉
    cap.release()
    cv2.destroyAllWindows()