Up Tensorflow-YOLOv3/detect.py から webcam モードの部分を抜き出す 作成: 2021-05-13
更新: 2021-05-16


detect.py
detect.py のテスト : "webcam" モード

    PC から ssh -X 接続
      起動メッセージで,-X 接続に対する部分: The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law.

    $ source ./venv/bin/activate
    (venv) $ cd ~/Tensorflow-YOLOv3
    (venv) $ vi detect_webcam.py
    #!/usr/bin/env python ##### モデル tiny YOLO の設定 ################# from core.utils import load_class_names, load_image, draw_boxes, draw_boxes_frame from core.yolo_tiny import YOLOv3_tiny from core.yolo import YOLOv3 # object のクラス class_names, n_classes = load_class_names() iou_threshold = 0.1 confidence_threshold = 0.25 model = YOLOv3_tiny(n_classes=n_classes, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) import tensorflow as tf inputs = tf.placeholder(tf.float32, [1, *model.input_size, 3]) detections = model(inputs) saver = tf.train.Saver(tf.global_variables(scope=model.scope)) ##### START ########################## import cv2 import sys with tf.Session() as sess: # モデル tiny YOLO の読み込み saver.restore(sess, './weights/model-tiny.ckpt') # カメラ映像の読み込み # カメラの番号:0 cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() # ヨコ,タテ frame_size = (frame.shape[1], frame.shape[0]) resized_frame = cv2.resize(frame, dsize=tuple((x) for x in model.input_size[::-1]), interpolation=cv2.INTER_NEAREST) result = sess.run(detections, feed_dict={inputs: [resized_frame]}) #カメラ映像の表示 draw_boxes_frame(frame, frame_size, result, class_names, model.input_size) cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break #カメラキャプチャを停止 cap.release() #ストリーミングウインドを閉じる cv2.destroyAllWindows() #プログラムを終了 sys.exit()


    (venv) $ chmod +x detect_webcam.py
    (venv) $ ./detect_webcam.py 2021-05-16 10:32:40.455671: E tensorflow/core/platform/hadoop/hadoop_file_system.cc:132] HadoopFileSystem load error: libhdfs.so: cannot open shared object file: No such file or directory WARNING:tensorflow:From ./detect_webcam.py:20: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead. WARNING:tensorflow:From /home/pi/Tensorflow-YOLOv3/core/yolo_tiny.py:76: The name tf.variable_scope is deprecated. Please use tf.compat.v1.variable_scope instead. WARNING:tensorflow:From /home/pi/Tensorflow-YOLOv3/core/layers.py:25: The name tf.layers.Conv2D is deprecated. Please use tf.compat.v1.layers.Conv2D instead. WARNING:tensorflow:From /home/pi/Tensorflow-YOLOv3/core/layers.py:34: The name tf.layers.BatchNormalization is deprecated. Please use tf.compat.v1.layers.BatchNormalization instead. WARNING:tensorflow:From /home/pi/Tensorflow-YOLOv3/core/layers.py:41: The name tf.layers.MaxPooling2D is deprecated. Please use tf.compat.v1.layers.MaxPooling2D instead. WARNING:tensorflow:From /home/pi/Tensorflow-YOLOv3/core/layers.py:97: The name tf.image.resize_nearest_neighbor is deprecated. Please use tf.compat.v1.image.resize_nearest_neighbor instead. WARNING:tensorflow:From ./detect_webcam.py:23: The name tf.train.Saver is deprecated. Please use tf.compat.v1.train.Saver instead. WARNING:tensorflow:From ./detect_webcam.py:23: The name tf.global_variables is deprecated. Please use tf.compat.v1.global_variables instead. WARNING:tensorflow:From ./detect_webcam.py:26: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead. (カメラ映像の Xウィンドウが開く)
    エラーメッセージおよび WARNING が返されるタイミング