将模型连接到边界框

问题描述

首先,感谢您花时间阅读我的问题。

我正在尝试预测边界框中的文本。不知何故,盒子和模型之间似乎没有联系。

我使用 EAST 文本检测来创建边界框并使用 Keras 训练了一个 CNN 模型。现在我正在使用 opencv 通过我的网络摄像头检索输入。 readNet EAST 模型和 load_model 我训练的 CNN。

我用白底黑字训练了我的模型。

当我尝试将两者连接起来时,它会在视频捕获中创建正确的框,但是它没有提供正确的预测类。

这是我代码的最后一部分:

   # loop over the bounding boxes
    for (startX,startY,endX,endY) in boxes:
        # scale the bounding box coordinates based on the respective
        # ratios
        startX = int(startX * rW)
        startY = int(startY * rH)
        endX = int(endX * rW)
        endY = int(endY * rH)
    
        orig_res =cv2.resize(orig,(200,200))
        orig_res = orig_res.reshape(1,200,3)
        
        prediction = model.predict(orig_res)
        max_prediction = np.argmax(prediction[0])
        my_prediction = myList[max_prediction]
            
        cv2.putText(orig,str(my_prediction),(startX,startY),cv2.FONT_HERSHEY_SIMPLEX,1.2,(0,255),3)
        
        # draw the bounding box on the image
        cv2.rectangle(orig,(endX,endY),255,0),2)
        
    # show the output image
    cv2.imshow("Detect_text",orig)

myList 代表不同单词的文件夹名称。这些是模型应该预测的名称。正如您在此屏幕截图中所见,它不会预测“100%”..

enter image description here

这是数据集的选择:

enter image description here

有人可以为此提供解决方案吗?非常感谢!

解决方法

您在训练期间使用的标签顺序与您在从文件夹名称列表推断时创建的标签列表之间似乎不匹配。 原因是 os.listdir 以任意顺序返回文件名,而 keras 图像生成器将在将文件夹名称映射到类索引之前对其进行排序。

您可以通过创建如下标签列表来确保具有相同的顺序:

# suppose your training data generator is named train_gen
myList = [item[0] for item in sorted(list(train_gen.class_indices.items()),key=lambda x: x[1])]

通过这种方式,您可以在训练和推理期间使用标签名称对齐

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...