gpt4 book ai didi

opencv - 如何通过网络摄像头实时识别图像并使用Keras实时准确显示标签

转载 作者:太空宇宙 更新时间:2023-11-03 23:10:50 24 4
gpt4 key购买 nike

我正在使用带有 tf 后端和 opencv 3 的 keras 2,

我想让网络摄像头识别图像而不是这个

test_image = cv2.imread('cat.jpg')"

此外,我还堆满了实时提要中的打印标签。我只是不知道如何通过循环和识别来连续获取图像。我知道可以通过 opencv VideoCapture 来完成,只是不知道该怎么做。

我的其他信息是

num_classes = 4

labels[0:202] = 0
labels[202:404] = 1
labels[404:606] = 2
labels[606:] = 3

names = ['cats', 'dogs', 'horses', 'humans']

希望大家帮帮我。提前致谢。

这是我的示例代码,用于测试来自路径的图像

# Testing a new image
test_image = cv2.imread('cat.jpg')
test_image = cv2.cvtColor(test_image, cv2.COLOR_BGR2GRAY)
test_image = cv2.resize(test_image, (128, 128))
test_image = np.array(test_image)
test_image = test_image.astype('float32')
test_image /= 255
print(test_image.shape)

if num_channel == 1:
if K.image_dim_ordering() == 'th':
test_image = np.expand_dims(test_image, axis=0)
test_image = np.expand_dims(test_image, axis=0)
print(test_image.shape)
else:
test_image = np.expand_dims(test_image, axis=3)
test_image = np.expand_dims(test_image, axis=0)
print(test_image.shape)
else:

if K.image_dim_ordering() == 'th':
test_image = np.rollaxis(test_image, 2, 0)
test_image = np.expand_dims(test_image, axis=0)
print(test_image.shape)
else:
test_image = np.expand_dims(test_image, axis=0)
print(test_image.shape)


# Predicting the test image
print(model.predict(test_image))
print(model.predict_classes(test_image))

最佳答案

如您所说,您可以使用 cv2.VideoCapture(0) 进行网络摄像头输入。流程是这样的

cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if ret:
# Your image is frame. Use this and continue with the prediction
# Pre Processing of frame
# model.predict(frame)

关于opencv - 如何通过网络摄像头实时识别图像并使用Keras实时准确显示标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50537047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com