【问题标题】:How to use a webcam for image classifier?如何使用网络摄像头进行图像分类?
【发布时间】:2018-03-10 01:56:51
【问题描述】:

我使用 Tensorflow 训练了我的 CNN 模型,并使用它成功地测试了图像。现在我想将我的模型用作实时分类器,因此我需要逐帧为其提供捕获的视频。我该怎么做?

【问题讨论】:

    标签: video tensorflow deep-learning


    【解决方案1】:
    • 要使用网络摄像头,您可以使用 OpenCV:

      import cv2
      import numpy as np
      
      #capture via webcam 0
      cap = cv2.VideoCapture(0)
      
      while True:
          # true or false for ret if the capture is there or not
          ret, frame = cap.read()
      
    • 你必须通过创建一个 .ckpt 文件来保存你训练过的模型,你可以很容易地做到这一点:

      saver = tf.train.Saver()
      
      # create your model 
      
      with tf.Session() as sess:
          # perform you training here 
      
          saver.save(sess, './my_trained_model')   # Save your Model
      
    • 保存模型后,使用 OpenCV 检测来自凸轮的帧,在检测帧之前,您必须加载 my_trained_model

      import tensorflow as tf
      import cv2
      
      cap = cv2.VideoCapture(0)
      with tf.Session() as sess:
          saver.restore(sess, './my_trained_model')  # Restore your model 
          detection_graph = tf.get_default_graph()
          input_tensor = detection_graph.get_tensor_by_name('input_tensor:0') # Get the input tensor 
          output_tensor = detection_graph.get_tensor_by_name('output_tensor:0')  # Get the output tensor 
          while True:
              # true or false for ret if the capture is there or not
              ret, frame = cap.read()  # read fram from the webcam
              feed = {input_tensor: frame}
              prediction = sess.run(tf.argmax(output_tensor, 1), feed_dict=feed)  # make prediction
      
    • 如果您想对来自网络摄像头的多个事物进行分类,那么您可能需要实现一个滑动窗口,并且对于每个滑动窗口,您都会得到一个预测,但这不是实时的

      李>
    • 我建议将上述解决方案用于图像分类而不是用于对象检测
    • 如果您应用 YOLO、SSD、R-CNN 等对象检测技术,它会为您进行实时分类
    • 在对象检测中,您首先检测对象,然后对检测到的对象执行分类

    ========================更新==================== ==

    【讨论】:

    • 是否有可能每两秒踩一次框架?我可以对我训练的模型使用对象检测技术吗?
    • @SalhiFedi ...是的,您可以更改 FPS(每秒帧率)...是的,您可以将模型与 R-CNN 等对象检测技术一起使用
    • @Jai 了解您正在研究 AI,请您检查我的问题并请告知我如何使用 YOLO 改进检测,stackoverflow.com/questions/57839115/…
    • 图像分类和物体检测有很多区别。
    猜你喜欢
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多