使用Python和imageai中的自定义模型进行多对象检测

我已经使用自己的图像数据集训练了一个自定义模型来识别图像中的对象,但似乎无法识别所有对象。我只有两种对象(一个人以不同方式书写特定字母的图像)。例如,字母“a”和字母“o”如下所示。

字母 'a' 字母 'o'

当我在手写文本样本上运行测试代码时,它在一定程度上确实显示了准确率的百分比,但没有边界框。这是手写文本的图像:

手写文本

这是我得到的输出:

我得到的输出

我使用imageai来训练自定义模型。我想知道是否可以使用这个训练好的模型在手写图像上进行多对象检测,并可能显示边界框?

以防提供额外帮助,这是我的工作目录的外观:

工作目录

这是我用于训练模型的代码(custom_detector.py):

from imageai.Prediction.Custom import ModelTraining# Instanciating the modelmodel_trainer = ModelTraining()model_trainer.setModelTypeAsResNet()# Setting our dataset directyorymodel_trainer.setDataDirectory("characters")# training the modelmodel_trainer.trainModel(num_objects=2, num_experiments=100, enhance_data=True, batch_size=10)

这是我用于测试训练模型的代码(test.py):

from imageai.Prediction.Custom import CustomImagePredictionimport os# get the working directoryexecution_path = os.getcwd()print(execution_path)# instanciate predictionprediction = CustomImagePrediction()prediction.setModelTypeAsResNet()# Set model pathprediction.setModelPath(os.path.join(execution_path, "characters", "models", "myModel.h5"))# Set JSON path# This is how the JSON file looks like:#{#   "0" : "A",#   "1" : "O"#}prediction.setJsonPath(os.path.join(execution_path, "characters", "json", "model_class.json"))# Initialize the number of objects you have retrainedprediction.loadModel(num_objects=2)# run predictionpredictions, probabilities = prediction.predictImage(os.path.join(execution_path, "HandTextTest.jpg"), result_count=2)# Print each predictionfor eachPrediction, eachProbability in zip(predictions, probabilities):    print(eachPrediction, " : ", eachProbability)

任何帮助或建议将不胜感激。


回答:

# run predictionpredictions, probabilities = prediction.predictImage(os.path.join(execution_path, "HandTextTest.jpg"), result_count=2)

predictImage函数负责预测相应图像的边界框。但此函数不会在图像上绘制边界框,它只显示不同类别的概率。参见predictImage代码

而detectCustomObjectsFromVideo函数则在结果视频帧上绘制边界框,并将结果保存到文件中。参见detectCustomObjectsFromVideo代码

因此,这不是模型能做什么的问题,因为predict函数不支持在图像上绘制边界框。你可以修改代码以在图像上绘制边界框,或者使用其他能够为你提供带边界框图像的包或框架。

如果你有任何问题,请随时评论。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注