如何将 OpenCV Videocapture 与保存的 Keras 模型一起使用?
How to use OpenCV Videocapture With Saved Keras Model?
我使用 Keras 和 Tensorflow 制作了一个简单的猫狗检测器。然后我以 .h5 格式保存模型并在检查点中进行训练。现在,我想加载训练和保存的模型,并且 运行 它使用相机与 OpenCV Videocapture 一起使用。我该怎么做?
这是我用来训练的代码:
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
import IPython.display as display
from PIL import Image, ImageSequence
import os
import pathlib
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import cv2
import datetime
tf.compat.v1.enable_eager_execution()
epochs = 200
steps_per_epoch = 10
batch_size = 20
IMG_HEIGHT = 200
IMG_WIDTH = 200
train_dir = "Data/Train"
test_dir = "Data/Val"
train_image_generator = ImageDataGenerator(rescale=1. / 255)
test_image_generator = ImageDataGenerator(rescale=1. / 255)
train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,
directory=train_dir,
shuffle=True,
target_size=(IMG_HEIGHT, IMG_WIDTH),
class_mode='sparse')
test_data_gen = test_image_generator.flow_from_directory(batch_size=batch_size,
directory=test_dir,
shuffle=True,
target_size=(IMG_HEIGHT, IMG_WIDTH),
class_mode='sparse')
model = Sequential([
Conv2D(265, 3, padding='same', activation='relu', input_shape=(IMG_HEIGHT, IMG_WIDTH ,3)),
MaxPooling2D(),
Conv2D(64, 3, padding='same', activation='relu'),
MaxPooling2D(),
Conv2D(32, 3, padding='same', activation='relu'),
MaxPooling2D(),
Flatten(),
keras.layers.Dense(256, activation="relu"),
keras.layers.Dense(144, activation="relu"),
keras.layers.Dense(80, activation="softmax")
])
model.compile(optimizer='adam',
loss="sparse_categorical_crossentropy",
metrics=['accuracy'])
model.summary()
tf.keras.utils.plot_model(model, to_file="model.png", show_shapes=True, show_layer_names=True, rankdir='TB')
checkpoint_path = "training/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
save_weights_only=True,
verbose=1)
os.system("rm -r logs")
log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
history = model.fit(train_data_gen,steps_per_epoch=steps_per_epoch,epochs=epochs,validation_data=test_data_gen,validation_steps=10,callbacks=[cp_callback, tensorboard_callback])
model.load_weights(tf.train.latest_checkpoint(checkpoint_dir))
model.save('model.h5', include_optimizer=True)
test_loss, test_acc = model.evaluate(test_data_gen)
print("Tested Acc: ", test_acc)
print("Tested Acc: ", test_acc*100, "%")
我正在使用 COCO 数据集进行图像分类。我想使用带有 OpenCV 的标准 USB 摄像头来进行视频捕捉,并将其实时流式传输到经过训练和导出的模型中进行预测。我该怎么做?
你可以这样做:
import cv2
import numpy as np
from PIL import Image
from keras import models
import os
import tensorflow as tf
checkpoint_path = "training_gender/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
model = models.load_model('gender.h5')
model.load_weights(tf.train.latest_checkpoint(checkpoint_dir))
video = cv2.VideoCapture(0)
while True:
_, frame = video.read()
#Convert the captured frame into RGB
im = Image.fromarray(frame, 'RGB')
#Resizing into dimensions you used while training
im = im.resize((109,82))
img_array = np.array(im)
#Expand dimensions to match the 4D Tensor shape.
img_array = np.expand_dims(img_array, axis=0)
#Calling the predict function using keras
prediction = model.predict(img_array)#[0][0]
print(prediction)
#Customize this part to your liking...
if(prediction == 1 or prediction == 0):
print("No Human")
elif(prediction < 0.5 and prediction != 0):
print("Female")
elif(prediction > 0.5 and prediction != 1):
print("Male")
cv2.imshow("Prediction", frame)
key=cv2.waitKey(1)
if key == ord('q'):
break
video.release()
cv2.destroyAllWindows()
我使用 Keras 和 Tensorflow 制作了一个简单的猫狗检测器。然后我以 .h5 格式保存模型并在检查点中进行训练。现在,我想加载训练和保存的模型,并且 运行 它使用相机与 OpenCV Videocapture 一起使用。我该怎么做?
这是我用来训练的代码:
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
import IPython.display as display
from PIL import Image, ImageSequence
import os
import pathlib
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import cv2
import datetime
tf.compat.v1.enable_eager_execution()
epochs = 200
steps_per_epoch = 10
batch_size = 20
IMG_HEIGHT = 200
IMG_WIDTH = 200
train_dir = "Data/Train"
test_dir = "Data/Val"
train_image_generator = ImageDataGenerator(rescale=1. / 255)
test_image_generator = ImageDataGenerator(rescale=1. / 255)
train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,
directory=train_dir,
shuffle=True,
target_size=(IMG_HEIGHT, IMG_WIDTH),
class_mode='sparse')
test_data_gen = test_image_generator.flow_from_directory(batch_size=batch_size,
directory=test_dir,
shuffle=True,
target_size=(IMG_HEIGHT, IMG_WIDTH),
class_mode='sparse')
model = Sequential([
Conv2D(265, 3, padding='same', activation='relu', input_shape=(IMG_HEIGHT, IMG_WIDTH ,3)),
MaxPooling2D(),
Conv2D(64, 3, padding='same', activation='relu'),
MaxPooling2D(),
Conv2D(32, 3, padding='same', activation='relu'),
MaxPooling2D(),
Flatten(),
keras.layers.Dense(256, activation="relu"),
keras.layers.Dense(144, activation="relu"),
keras.layers.Dense(80, activation="softmax")
])
model.compile(optimizer='adam',
loss="sparse_categorical_crossentropy",
metrics=['accuracy'])
model.summary()
tf.keras.utils.plot_model(model, to_file="model.png", show_shapes=True, show_layer_names=True, rankdir='TB')
checkpoint_path = "training/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
save_weights_only=True,
verbose=1)
os.system("rm -r logs")
log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
history = model.fit(train_data_gen,steps_per_epoch=steps_per_epoch,epochs=epochs,validation_data=test_data_gen,validation_steps=10,callbacks=[cp_callback, tensorboard_callback])
model.load_weights(tf.train.latest_checkpoint(checkpoint_dir))
model.save('model.h5', include_optimizer=True)
test_loss, test_acc = model.evaluate(test_data_gen)
print("Tested Acc: ", test_acc)
print("Tested Acc: ", test_acc*100, "%")
我正在使用 COCO 数据集进行图像分类。我想使用带有 OpenCV 的标准 USB 摄像头来进行视频捕捉,并将其实时流式传输到经过训练和导出的模型中进行预测。我该怎么做?
你可以这样做:
import cv2
import numpy as np
from PIL import Image
from keras import models
import os
import tensorflow as tf
checkpoint_path = "training_gender/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
model = models.load_model('gender.h5')
model.load_weights(tf.train.latest_checkpoint(checkpoint_dir))
video = cv2.VideoCapture(0)
while True:
_, frame = video.read()
#Convert the captured frame into RGB
im = Image.fromarray(frame, 'RGB')
#Resizing into dimensions you used while training
im = im.resize((109,82))
img_array = np.array(im)
#Expand dimensions to match the 4D Tensor shape.
img_array = np.expand_dims(img_array, axis=0)
#Calling the predict function using keras
prediction = model.predict(img_array)#[0][0]
print(prediction)
#Customize this part to your liking...
if(prediction == 1 or prediction == 0):
print("No Human")
elif(prediction < 0.5 and prediction != 0):
print("Female")
elif(prediction > 0.5 and prediction != 1):
print("Male")
cv2.imshow("Prediction", frame)
key=cv2.waitKey(1)
if key == ord('q'):
break
video.release()
cv2.destroyAllWindows()