在 Python 和 imageai 中使用自定义模型进行多目标检测

Multiple object detection using a custom model in Python and imageai

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

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

这是我得到的输出:

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

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

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

from imageai.Prediction.Custom import ModelTraining

# Instanciating the model
model_trainer = ModelTraining()
model_trainer.setModelTypeAsResNet()
# Setting our dataset directyory
model_trainer.setDataDirectory("characters")
# training the model
model_trainer.trainModel(num_objects=2, num_experiments=100, enhance_data=True, batch_size=10)

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

from imageai.Prediction.Custom import CustomImagePrediction
import os

# get the working directory
execution_path = os.getcwd()
print(execution_path)
# instanciate prediction
prediction = CustomImagePrediction()
prediction.setModelTypeAsResNet()

# Set model path
prediction.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 retrained
prediction.loadModel(num_objects=2)

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

# Print each prediction
for eachPrediction, eachProbability in zip(predictions, probabilities):
    print(eachPrediction, " : ", eachProbability)

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

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

predictImage 函数负责预测相应图像的边界框。但是这个函数不会在图像上绘制边界框,它只是显示不同 类 的概率。查看 predictImage code .

而 detectCustomObjectsFromVideo 函数在结果视频帧上绘制边界框并将结果保存在文件中。见 detectCustomObjectsFromVideo code.

所以这不是模型可以做什么的问题,因为预测函数不支持在图像上绘制边界框。您可以修改代码以在图像上绘制边界框,也可以使用其他一些包或框架来为您提供带边界框的图像。

如有任何问题,请随时发表评论。