如何使用OpenCV将实时视频发送到Tensorflow?

时间:2019-11-25 03:03:15

标签: python-3.x opencv tensorflow

我基本上使用过Teachable Machine,这是Google的网站,可让您训练自己的机器学习模型。它还具有可以导出代码的功能。我从未使用过Tensorflow或OpenCV / cv2,所以这对我来说是个艰巨的过程。

这是我的代码:

from PIL import Image
import numpy as np
import cv2
import numpy as np
import matplotlib.pyplot as plt 
# Disable scientific notation for clarity
np.set_printoptions(suppress=True)

cap = cv2.VideoCapture(0)
# Load the model
model = tensorflow.keras.models.load_model('keras_model.h5', compile=False)

# Create the array of the right shape to feed into the keras model
# The 'length' or number of images you can put into the array is
# determined by the first position in the shape tuple, in this case 1.
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)

# Replace this with the path to your image
image = Image.open('Path to your image')

# Make sure to resize all images to 224, 224 otherwise they won't fit in the array
image = image.resize((224, 224))
image_array = np.asarray(image)

# Normalize the image
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1

# Load the image into the array
data[0] = normalized_image_array

# run the inference
prediction = model.predict(data)
print(prediction)

直接从可教导的机器中取出。目前,该程序仅允许我上传该程序将分类的单个图像。

是否可以使用我的程序(最好是openCV)将实时视频流式传输到该程序

如果有时间,还可以向我展示如何在程序识别的类周围放置彩色框。

很抱歉,如果要求太多!

1 个答案:

答案 0 :(得分:0)

在这个github问题中,有一个如何处理您所要询问的示例:https://github.com/googlecreativelab/teachablemachine-community/issues/55#issuecomment-560447633

希望有效