我使用Tensorflow训练了我的CNN模型,并用它来成功测试图像。现在我想将我的模型用作实时分类器,因此我需要逐帧捕获视频。我该怎么办?
答案 0 :(得分:0)
要使用网络摄像头,您可以使用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
如果你想从网络摄像头分类多个东西,那么你可能需要实现一个滑动窗口,对于每个滑动窗口你都会得到一个预测,但这不是实时的
=======================更新==================== == 强>