问题描述
我能够为图像上传预测相同的模型,但无法为实时视频提要预测相同的模型。我正在尝试从实时视频源中提取一帧并使用 mobilenet 模型对其进行预处理以进行类别预测
import cv2
#importing objects from flask
from flask import Flask,request,render_template,send_from_directory
app = Flask(__name__) #needs this because flask sets up some paths behind the scene : name is the special variable holds the model
# Flask
from flask import Flask,redirect,url_for,Response,jsonify,redirect
from werkzeug.utils import secure_filename
from gevent.pywsgi import WsgiServer
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.applications.imagenet_utils import preprocess_input,decode_predictions
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
# Some utilites
import numpy as np
#importing objects from flask
from flask import Flask,send_from_directory
app = Flask(__name__) #needs this because flask sets up some paths behind the scene : name is the special variable holds the model
APP_ROOT = os.path.dirname(os.path.abspath(__file__)) #file variable having file name: absolute path func / relative path
classes = ['Class1','Class2','Class3','Class4']
new_model = load_model('model.h5') #create var new_model for the file model.h5
new_model.summary()
cap = cv2.VideoCapture(0)
if not cap.isOpened():
raise IOError("We cannot open webcam")
while True:
frame = cap.read()
frame = cv2.flip(frame,cv2.COLOR_BGR2RGB)
test_image = image.load_img(frame,target_size=(224,224))
# Preprocessing the image
test_image = image.img_to_array(test_image)
# x = np.true_divide(x,255)
test_image = np.expand_dims(test_image,axis=0)
# Be careful how your trained model deals with the input
# otherwise,it won't make correct prediction!
test_image = tf.keras.applications.mobilenet.preprocess_input(test_image)
result = new_model.predict(test_image)
prediction = classes[np.argmax(result)]
score = float("%0.2f" % (max(result[0]) * 100))
#score = float("{.2f}".format((classes) * 100))
print(f'Result: {prediction},score: {score}')
cv2.imshow("frame",frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
cap.release()
我在执行代码时遇到以下错误。 v2.imshow("frames",frame) 类型错误:参数“mat”的预期 Ptr<:umat>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)