【发布时间】:2018-03-10 01:56:51
【问题描述】:
我使用 Tensorflow 训练了我的 CNN 模型,并使用它成功地测试了图像。现在我想将我的模型用作实时分类器,因此我需要逐帧为其提供捕获的视频。我该怎么做?
【问题讨论】:
标签: video tensorflow deep-learning
我使用 Tensorflow 训练了我的 CNN 模型,并使用它成功地测试了图像。现在我想将我的模型用作实时分类器,因此我需要逐帧为其提供捕获的视频。我该怎么做?
【问题讨论】:
标签: video tensorflow deep-learning
要使用网络摄像头,您可以使用 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
如果您想对来自网络摄像头的多个事物进行分类,那么您可能需要实现一个滑动窗口,并且对于每个滑动窗口,您都会得到一个预测,但这不是实时的
李>========================更新==================== ==
【讨论】: