예제 #1
0
                        dest='queue_size',
                        type=int,
                        default=5,
                        help='Size of the queue.')
    args = parser.parse_args()

    input_q = Queue(maxsize=args.queue_size)
    output_q = Queue(maxsize=args.queue_size)

    video_capture = WebcamVideoStream(src=args.video_source,
                                      width=args.width,
                                      height=args.height).start()

    cap_params = {}
    frame_processed = 0
    cap_params['im_width'], cap_params['im_height'] = video_capture.size()
    cap_params['score_thresh'] = score_thresh

    # max number of hands we want to detect/track
    cap_params['num_hands_detect'] = args.num_hands

    print(cap_params, args)

    # spin up workers to paralleize detection.
    pool = Pool(args.num_workers, worker,
                (input_q, output_q, cap_params, frame_processed))

    start_time = datetime.datetime.now()
    num_frames = 0
    fps = 0
    index = 0
예제 #2
0
def hd_main(eleft,
            eright,
            eup,
            video_source=0,
            num_hands=1,
            display=1,
            num_workers=4):
    print("detector started")

    input_q = Queue(maxsize=5)
    output_q = Queue(maxsize=5)
    output_boxes = Queue(maxsize=5)
    t.sleep(1)
    video_capture = WebcamVideoStream(src=video_source, width=300,
                                      height=200).start()

    cap_params = {}
    frame_processed = 0
    cap_params['im_width'], cap_params['im_height'] = video_capture.size()
    cap_params['score_thresh'] = score_thresh

    # max number of hands we want to detect/track
    cap_params['num_hands_detect'] = num_hands

    print(cap_params)

    # spin up workers to paralleize detection.
    pool = Pool(num_workers, worker,
                (input_q, output_q, output_boxes, cap_params, frame_processed))
    start_time = datetime.datetime.now()
    num_frames = 0
    index = 0
    cv2.namedWindow('Multi-Threaded Detection', cv2.WINDOW_NORMAL)

    while True:
        frame = video_capture.read()
        frame = cv2.flip(frame, 1)
        index += 1

        input_q.put(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
        output_frame = output_q.get()
        output_box = output_boxes.get()
        output_frame = cv2.cvtColor(output_frame, cv2.COLOR_RGB2BGR)

        elapsed_time = (datetime.datetime.now() - start_time).total_seconds()
        num_frames += 1
        fps = num_frames / elapsed_time

        if (output_frame is not None):
            (left, right, top, bottom) = (output_box[1], output_box[3],
                                          output_box[0], output_box[2])
            my_detect_threshold = 0.8  ## How far right/ left hand should be for sending events

            if top < 0.4:  # if the top of bounding box is in the top 40% of the screen
                eup.set()
            else:
                eup.clear()

            if (right > my_detect_threshold):
                eright.set()
            else:
                eright.clear()
            if (left < (1 - my_detect_threshold)):
                eleft.set()
            else:
                eleft.clear()

            if (display > 0):
                detector_utils.draw_fps_on_image("FPS : " + str(int(fps)),
                                                 output_frame)
                cv2.imshow('Multi-Threaded Detection', output_frame)
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
            else:
                if (num_frames == 400):
                    num_frames = 0
                    start_time = datetime.datetime.now()
                else:
                    print("frames processed: ", index, "elapsed time: ",
                          elapsed_time, "fps: ", str(int(fps)))
        else:
            break
    pool.terminate()
    video_capture.stop()
    cv2.destroyAllWindows()
예제 #3
0
def HandPose_main(keep_flg):    #別ファイルから HandPose.py を動かすと if __name__ == '__main__': が動かないっぽいので、関数で代用
#if __name__ == '__main__':
    flg_video = 0   #「1」でカメラが接続されていない
    flg_break = 0   #「1」で最初のループを抜け終了する⇒正常終了
    flg_restart = 0 #「1」でリスタートした際に hand_gui.py で eel が2度起動するのを防ぐ
    flg_start = 0   #「1」で開始時点でのカメラ消失
    cnt_gui=0   #hand_guiにてeelを動かす用に使用(0:初回起動時、1:2回目以降起動時、2:カメラが切断された際にhtmlを閉じるために使用)

    width,height = autopy.screen.size() #eel で立ち上げた際の表示位置を指定するために取得

    while(True):    #カメラが再度接続するまでループ処理
        #try:
            #カメラが接続されていないフラグの場合
            if(flg_video == 1):

                if(cnt_gui == 2):

                    eel.init('GUI/web')
                    eel.start('html/connect.html',
                                mode='chrome',
                                size=(500,600),  #サイズ指定(横, 縦)
                                position=(width/2-250, height/2-300), #位置指定(left, top)
                                block=False)
                    cnt_gui = 0
                    print("connect 接続しているよ!!")
                try:
                    eel.sleep(0.01)
                except:
                    print("エラー発生!!!!")
                    traceback.print_exc()
                    continue
                #カメラが接続されているか確認
                cap2 = cv2.VideoCapture(0)
                ret2, frame2 = cap2.read()
                if(ret2 is True):
                    #カメラが接続されている場合
                    flg_video = 0
                    cnt_gui = 0
                    flg_restart = 1
                    print("webcamあったよ!!")
                    eel.windowclose()
                    continue    #最初の while に戻る
                else:
                #カメラが接続されていない場合
                #print("webcamないよ!!!")
                    continue    #最初の while に戻る
            #正常終了のフラグの場合
            elif(flg_break == 1):
                break   #最初の while を抜けて正常終了
            #パーサを作る
            parser = argparse.ArgumentParser()

            #Webカメラの選択
            parser.add_argument(
                '-src',
                '--source',
                dest='video_source',
                type=int,
                default=0,
                # default=hand_gui.cam_source(),
                help='Device index of the camera.')

            #手を識別する数
            parser.add_argument(
                '-nhands',
                '--num_hands',
                dest='num_hands',
                type=int,
                default=1,
                help='Max number of hands to detect.')

            #FPSを表示するか
            parser.add_argument(
                '-fps',
                '--fps',
                dest='fps',
                type=int,
                default=1,
                help='Show FPS on detection/display visualization')

            #ビデオストリームの横幅
            parser.add_argument(
                '-wd',
                '--width',
                dest='width',
                type=int,
                default=300,
                help='Width of the frames in the video stream.')

            #ビデオストリームの高さ
            parser.add_argument(
                '-ht',
                '--height',
                dest='height',
                type=int,
                default=200,
                help='Height of the frames in the video stream.')

            #OpenCVでのFPSの表示をするか否か
            parser.add_argument(
                '-ds',
                '--display',
                dest='display',
                type=int,
                default=1,
                help='Display the detected images using OpenCV. This reduces FPS')

            #ワーカーが同時に動く数を設定。
            parser.add_argument(
                '-num-w',
                '--num-workers',
                dest='num_workers',
                type=int,
                default=4,
                help='Number of workers.')

            #FIFO Queueの最大サイズを設定。
            parser.add_argument(
                '-q-size',
                '--queue-size',
                dest='queue_size',
                type=int,
                default=5,
                help='Size of the queue.')

            #設定項目を変数argsに代入する。
            args = parser.parse_args()

            #各Queue変数の要素数の上限を設定
            input_q             = Queue(maxsize=args.queue_size)
            output_q            = Queue(maxsize=args.queue_size)
            cropped_output_q    = Queue(maxsize=args.queue_size)
            inferences_q        = Queue(maxsize=args.queue_size)

            pointX_q = Queue(maxsize=args.queue_size)#worker内のp1用
            pointY_q = Queue(maxsize=args.queue_size)#worker内のp2用

            #初期設定したargsパーサーからWebカメラの指定及びサイズを取得
            video_capture = WebcamVideoStream(
                src=args.video_source, width=args.width, height=args.height).start()

            cap_params = {}
            frame_processed = 0
            cap_params['im_width'], cap_params['im_height'] = video_capture.size()
            print(cap_params['im_width'], cap_params['im_height'])
            cap_params['score_thresh'] = score_thresh

            # max number of hands we want to detect/track
            #検出/追跡する手の最大数
            cap_params['num_hands_detect'] = args.num_hands

            print(cap_params, args)

            # Count number of files to increment new example directory
            #新しいサンプルディレクトリをインクリメントするファイルの数を数える
            poses = []
            _file = open("poses.txt", "r")
            lines = _file.readlines()
            for line in lines:
                line = line.strip()
                if(line != ""):
                    print(line)
                    poses.append(line)


            # spin up workers to paralleize detection.
            #ワーカーをスピンアップして検出を並列化します。
            pool = Pool(args.num_workers, worker,
                        (input_q, output_q, cropped_output_q, inferences_q, pointX_q, pointY_q, cap_params, frame_processed))

            #pool2 = Pool(1,hand_gui.start_gui,(output_q, cropped_output_q))

            start_time = datetime.datetime.now()
            num_frames = 0
            fps = 0
            index = 0

            # 切り抜く色範囲の上限下限を設定
            lower_blue = np.array([0, 30, 60])
            upper_blue = np.array([30, 200, 255])

            cv2.namedWindow('Handpose', cv2.WINDOW_NORMAL)
            poseCount = [0,0,0,0,0,0,0]
            moveCount = [0,0,0,0]

            #cnt_gui=0   #hand_guiにてeelを動かす用に使用
            cnt_pose=0  #
            name_pose=""
            flg_end = 0#システム終了フラグ

            while True:
                frame = video_capture.read()
                frame = cv2.flip(frame, 1)

                index += 1
                gamma_config = 1.6

                if(frame is None):
                    #frame が None だと frame = gamma.gamma_correction(frame,gamma_config) で
                    #cv2.error が発生するのでここで判定し、今の while を抜ける
                    traceback.print_exc()
                    #それぞれのフラグを立てて、システムを終了させ、最初の while に戻る
                    flg_video = 1
                    cnt_gui = 2
                    try:
                        #webcam が最初から接続されていない場合は except の動作
                        cnt_gui, flg_end, flg_restart, flg_start, keep_flg = hand_gui.start_gui(output_frame, cnt_gui, cnt_pose, name_pose, flg_restart, flg_start, keep_flg)
                    except NameError as name_e:
                        traceback.print_exc()
                        flg_start = 1
                        print("webcam接続して!!!!")
                    pool.terminate()
                    video_capture.stop()
                    cv2.destroyAllWindows()

                    #eel.init('GUI/web')
                    #eel.start(
                    #'html/connect.html',
                    #     mode='chrome',
                    #    cmdline_args=['--start-fullscreen'],
                    #    block=False)
                    #i=0
                    #while(i < 500):
                    #    eel.sleep(0.01)
                    #    i+=1
                    break

                frame = gamma.gamma_correction(frame,gamma_config)
                # cv2.imwrite('Poses/Save/aaa' + str(num_frames) + '.png', frame)
                #画像切り取るかどうか
                frame_cropped_flag = False
                #画面サイズを縮小させ稼働領域の調整を行う
                #各パラメーターに値を入力することで画像サイズを小さくできる
                if(frame_cropped_flag == True):
                    # print(int(cap_params['im_width']))
                    # print(int(cap_params['im_height']))
                    left_params = int(cap_params['im_width'])//8
                    top_params = int(cap_params['im_height'])//8
                    right_params = int(cap_params['im_width'])-(int(cap_params['im_width'])//8)
                    bottom_params = int(cap_params['im_height'])-(int(cap_params['im_height'])//8)

                    #キャプチャした画像の切り取り
                    # frame = frame[top_params:bottom_params,left_params:right_params].copy()
                    frame = frame[100:200,100:200].copy()

                #背景切り抜きの為画像形式をBGRからHSVへ変更
                hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV_FULL)
                #設定した色範囲を塗りつぶしたマスク画像を生成
                mask = cv2.inRange(hsv, lower_blue, upper_blue)
                #生成したマスクで通常画像を切り抜き
                frame_masked = cv2.bitwise_and(hsv,hsv, mask=mask)

                # print(left_params,top_params,right_params,bottom_params)


                #マスク処理済の画像をHSV形式からRGB形式へ変換
                input_q.put(cv2.cvtColor(frame_masked, cv2.COLOR_HSV2RGB))

                # initialize the folder which contents html,js,css,etc

                output_frame = output_q.get()
                cropped_output = cropped_output_q.get()

                #hand_gui.start_gui(output_frame)
                output_frame = cv2.cvtColor(output_frame, cv2.COLOR_RGB2BGR)
                #output_qの内容表示するためにhand_gui.start_guiへ
                cnt_gui, flg_end, flg_restart, flg_start, keep_flg = hand_gui.start_gui(output_frame, cnt_gui, cnt_pose, name_pose, flg_restart, flg_start,keep_flg)

                inferences      = None

                try:
                    inferences = inferences_q.get_nowait()
                except Exception as e:
                    pass

                elapsed_time = (datetime.datetime.now() - start_time).total_seconds()
                num_frames += 1
                fps = num_frames / elapsed_time

                # Display inferences
                #ポーズの手の形の推測グラフを表示する
                if(inferences is not None):
                    #worker関数内のp1,p2の値を代入
                    x = pointX_q.get_nowait()
                    y = pointY_q.get_nowait()
                    gui.drawInferences(inferences, poses)
                    #ポーズの形の信頼地が0.7を超えたらアクションを実行する
                    for i in range(len(poses)):
                        if(inferences[2] > 0.9):
                            moveCount = PoseAction.pointerMove(x,y,moveCount)
                        if(inferences[i] > 0.7):
                            poseCount,moveCount = PoseAction.checkPose(x, y, poses,poses[i],poseCount,moveCount)#testに7割越え識別したポーズの名称が代入される。
                            cnt_pose = poseCount[i] #全ポーズのゲージを取得したい場合は[i]を外す
                            name_pose = poses[i]
                if (cropped_output is not None):
                    #切り取った画像をBGR形式からRGB形式へ変更する。
                    cropped_output = cv2.cvtColor(cropped_output, cv2.COLOR_RGB2BGR)
                    if (args.display > 0):
                        cv2.namedWindow('Cropped', cv2.WINDOW_NORMAL)
                        cv2.resizeWindow('Cropped', 450, 300)
                        cv2.imshow('Cropped', cropped_output)

                        # cv2.imwrite('Poses/Three/Three_4/Three_4' + str(num_frames) + '.png', cropped_output)
                        if cv2.waitKey(1) & 0xFF == ord('q'):
                            flg_break = 1
                            break
                    else:
                        if (num_frames == 400):
                            num_frames = 0
                            start_time = datetime.datetime.now()
                        else:
                            print("frames processed: ", index, "elapsed time: ",
                                  elapsed_time, "fps: ", str(int(fps)))


                # print("frame ",  index, num_frames, elapsed_time, fps)

                if (output_frame is not None):

        #            _, imencode_image = cv2.imencode('.jpg', output_frame)
        #            base64_image2 = base64.b64encode(imencode_image)
        #            eel.set_base64image2("data:image/jpg;base64," + base64_image2.decode("ascii"))

                    # output_frame = cv2.cvtColor(output_frame, cv2.COLOR_RGB2BGR)
                    if (args.display > 0):
                        if (args.fps > 0):
                            detector_utils.draw_fps_on_image("FPS : " + str(int(fps)),
                                                             output_frame)
                        cv2.imshow('Handpose', output_frame)
                        if cv2.waitKey(1) & 0xFF == ord('q'):
                            flg_break = 1
                            break
                    else:
                        if (num_frames == 400):
                            num_frames = 0
                            start_time = datetime.datetime.now()
                        else:
                            print("frames processed: ", index, "elapsed time: ",
                                  elapsed_time, "fps: ", str(int(fps)))
                else:
                    print("video end")
                    flg_break = 1
                    break

                if(flg_end == 1):
                    flg_break = 1
                    break

        #カメラが切断された際の例外処理(現状、全ての例外をキャッチしている)
#        except:# cv2.error as cv2_e:
#            traceback.print_exc()
            #それぞれのフラグを立てて、システムを終了させ、最初の while に戻る
#            flg_video = 1
#            cnt_gui = 2
#            cnt_gui, flg_end, flg_restart = hand_gui.start_gui(output_frame, cnt_gui, cnt_pose, name_pose, flg_restart)
#            pool.terminate()
#            video_capture.stop()
#            cv2.destroyAllWindows()

    elapsed_time = (datetime.datetime.now() - start_time).total_seconds()
    fps = num_frames / elapsed_time
    print("fps", fps)
    pool.terminate()
    video_capture.stop()
    cv2.destroyAllWindows()
예제 #4
0
    #making queues for process
    input_q = Queue(maxsize=queue_size)
    output_q = Queue(maxsize=queue_size)
    boxes_q = Queue(maxsize=queue_size)
    inferences_q = Queue(maxsize=queue_size)

    #camera initialization
    cap = WebcamVideoStream(src=video_source, width=width, height=height).start()

    # cap = cv2.VideoCapture(video_source)
    # cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    # cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)

    cap_params = {}
    frame_processed = 0
    cap_params['im_width'], cap_params['im_height'] = cap.size()
    # cap_params['im_width'] = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
    # cap_params['im_height'] = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    cap_params['score_thresh'] = score_thresh
    cap_params['num_hands_detect'] = num_hands
    print(cap_params)

    #initialize server requester
    requester = Requester(width=cap_params['im_width'], height=cap_params['im_height'])

    #Set poses for inference
    poses = []
    with open('poses.txt', 'r') as f:
        lines = f.readlines()
        for line in lines:
            line = line.strip()
예제 #5
0
def main():
    video_source = 0
    num_hands = 2
    fps = 1  # set to 0 to hide
    width = 300
    height = 200
    display = 1  # show detected hands
    num_workers = 4
    queue_size = 5

    input_q = Queue(maxsize=queue_size)
    output_q = Queue(maxsize=queue_size)

    video_capture = WebcamVideoStream(src=video_source,
                                      width=width,
                                      height=height).start()

    cap_params = {}
    frame_processed = 0
    cap_params['im_width'], cap_params['im_height'] = video_capture.size()
    cap_params['score_thresh'] = score_thresh

    # max number of hands we want to detect/track
    cap_params['num_hands_detect'] = num_hands

    movement = Value('i', 0)
    movement_threshold = (width + height) / 3

    # spin up workers to paralleize detection.
    pool = Pool(num_workers, worker,
                (input_q, output_q, cap_params, frame_processed, movement,
                 movement_threshold))

    start_time = datetime.datetime.now()
    num_frames = 0
    fps = 0
    index = 0
    old_movement = 0
    history_avg = 4
    move_history = [0] * history_avg
    total_space = width + height

    cv2.namedWindow('Multi-Threaded Detection', cv2.WINDOW_NORMAL)

    while True:
        frame = video_capture.read()
        frame = cv2.flip(frame, 1)
        index += 1

        input_q.put(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
        output_frame = output_q.get()

        output_frame = cv2.cvtColor(output_frame, cv2.COLOR_RGB2BGR)

        elapsed_time = (datetime.datetime.now() - start_time).total_seconds()
        num_frames += 1
        fps = num_frames / elapsed_time

        # Calculate amount moved in the last 5 frames
        frame_rate = 5
        if num_frames % frame_rate == 0:
            cur_movement = movement.value
            moved = (cur_movement - old_movement) / frame_rate
            old_movement = cur_movement

            # Track historical movement
            move_history.append(moved)

            total = 0
            for i in range(len(move_history) - history_avg, len(move_history)):
                total += move_history[i]

            moved_avg = ((total / history_avg) / total_space) * 1000
            print("Movement score: {}".format(moved_avg))

        # print("frame ",  index, num_frames, elapsed_time, fps)
        if (output_frame is not None):
            if (display > 0):
                if (fps > 0):
                    detector_utils.draw_fps_on_image("FPS : " + str(int(fps)),
                                                     output_frame)
                # cv2.imshow('Multi-Threaded Detection', output_frame)
                ret, jpeg = cv2.imencode('.jpg', output_frame)
                jbytes = jpeg.tobytes()
                yield (b'--frame\r\n'
                       b'Content-Type: image/jpeg\r\n\r\n' + jbytes +
                       b'\r\n\r\n')

                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
            else:
                if (num_frames == 400):
                    num_frames = 0
                    start_time = datetime.datetime.now()
                else:
                    print("frames processed: ", index, "elapsed time: ",
                          elapsed_time, "fps: ", str(int(fps)))
        else:
            # print("video end")
            break
    elapsed_time = (datetime.datetime.now() - start_time).total_seconds()
    fps = num_frames / elapsed_time
    print("fps", fps)
    pool.terminate()
    video_capture.stop()
    cv2.destroyAllWindows()
예제 #6
0
if __name__ == '__main__':
    args = get_args()

    dg = load_graphs()

    video_capture = WebcamVideoStream(src=args.video_source,
                                  width=args.width,
                                  height=args.height).start()

    num_hands_detect = 2
    num_frames = 0
    start_time = datetime.datetime.now()
    while True:
        frame = video_capture.read()
        im_width, im_height = video_capture.size()

        # actual detection
        scores, boxes = detection(dg)

        elapsed_time = (datetime.datetime.now() -
                        start_time).total_seconds()
        num_frames += 1
        fps = num_frames / elapsed_time

        if frame is not None:
            if args.display > 0:
                if args.fps > 0:
                    detector_utils.draw_fps_on_image(
                        "FPS : " + str(int(fps)), frame)
            cv2.imshow('Muilti - threaded Detection', frame)