Python 使用tensorflow模型(更快的rcnn)检测对象时,opencv dnn的结果看起来很奇怪 操作系统:win1064 opencv:4.1.2,由anaconda安装 tensorflow:1.15,由pip安装

Python 使用tensorflow模型(更快的rcnn)检测对象时,opencv dnn的结果看起来很奇怪 操作系统:win1064 opencv:4.1.2,由anaconda安装 tensorflow:1.15,由pip安装,python,opencv,tensorflow,Python,Opencv,Tensorflow,复制步骤 1:通过tf\u text\u graph\u faster\u rcnn.py生成配置文件 python tf_text_graph_faster_rcnn.py--输入冻结的_推断_graph.pb--配置pipeline.config--输出faster_rcnn_inception_resnet_v2_atrus_oid.pbtxt 冻结的\u推断\u graph.pb和pipeline.config是解压后的文件 2:通过示例代码检测对象 import cv2 as cv

复制步骤

1:通过tf\u text\u graph\u faster\u rcnn.py生成配置文件

python tf_text_graph_faster_rcnn.py--输入冻结的_推断_graph.pb--配置pipeline.config--输出faster_rcnn_inception_resnet_v2_atrus_oid.pbtxt

冻结的\u推断\u graph.pb和pipeline.config是解压后的文件

2:通过示例代码检测对象

import cv2 as cv

cvNet = cv.dnn.readNetFromTensorflow('tensorflow/faster_rcnn_inception_resnet_v2_atrous_oid.pb', 'tensorflow/faster_rcnn_inception_resnet_v2_atrous_oid.pbtxt')

img = cv.imread('traffic_jam.jpg')
rows = img.shape[0]
cols = img.shape[1]
cvNet.setInput(cv.dnn.blobFromImage(img, size=(300, 300), swapRB=True, crop=False))
cvOut = cvNet.forward()

for detection in cvOut[0,0,:,:]:
    score = float(detection[2])
    if score > 0.1:
        left = detection[3] * cols
        top = detection[4] * rows
        right = detection[5] * cols
        bottom = detection[6] * rows
        cv.rectangle(img, (int(left), int(top)), (int(right), int(bottom)), (23, 230, 210), thickness=2)

cv.imshow('img', img)
cv.waitKey()
结果

  • 输入图像
  • 结果
  • 通过tensorflow算法在

    虽然仍有很多车无法检测,但与opencv的api相比,结果相差甚远

    编辑:由ssd_mobilenet_v1_coco检测到的结果,更好的结果,我猜opencv dnn模块与我发布的模型不兼容

    Edit2:ssd检测代码

    import numpy as np
    import tensorflow as tf
    import cv2 as cv
    
    # Read the graph.
    #with tf.gfile.FastGFile('tensorflow/faster_rcnn_inception_resnet_v2_atrous_oid.pb', 'rb') as f:
    with tf.gfile.FastGFile("tensorflow/ssd_mobilenet_v1_coco.pb", 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
    
    with tf.Session() as sess:
        # Restore session
        sess.graph.as_default()
        tf.import_graph_def(graph_def, name='')
    
        # Read and preprocess an image.
        img = cv.imread('traffic_jam.jpg')
        rows = img.shape[0]
        cols = img.shape[1]
        inp = cv.resize(img, (300, 300))
        inp = inp[:, :, [2, 1, 0]]  # BGR2RGB
    
        # Run the model
        out = sess.run([sess.graph.get_tensor_by_name('num_detections:0'),
                        sess.graph.get_tensor_by_name('detection_scores:0'),
                        sess.graph.get_tensor_by_name('detection_boxes:0'),
                        sess.graph.get_tensor_by_name('detection_classes:0')],
                        feed_dict={'image_tensor:0': inp.reshape(1, inp.shape[0], inp.shape[1], 3)})
    
        # Visualize detected bounding boxes.
        num_detections = int(out[0][0])
        for i in range(num_detections):
            classId = int(out[3][0][i])
            score = float(out[1][0][i])
            bbox = [float(v) for v in out[2][0][i]]
            if score > 0.1:
                x = bbox[1] * cols
                y = bbox[0] * rows
                right = bbox[3] * cols
                bottom = bbox[2] * rows
                cv.rectangle(img, (int(x), int(y)), (int(right), int(bottom)), (125, 255, 51), thickness=2)
    
    cv.imshow('TensorFlow MobileNet-SSD', img)
    cv.waitKey()
    
    300x300输入大小不正确,因为
    pipeline.config
    具有以下预处理指令:

    image_resizer {
      keep_aspect_ratio_resizer {
        min_dimension: 600
        max_dimension: 1024
      }
    }
    
    这意味着您必须保持图像比例。尝试处理原始帧(其具有800x600):

    使用置信阈值
    0.3
    pipeline.config
    修改提及的输出


    对于该网站上的示例网络,您是否获得了与他们相同的结果?@Miki我不知道在哪里可以找到示例的原始图像,但您可以看到ssd MobileNet检测到的图像的结果可能r-cnn后端在opencv中未正确/完全实现(另一个模型是ssd模型)。我不知道tensorflow检测api,有什么“版本”吗?也许你可以找到opencv中实现的版本和模型使用的版本?另一件事:我不认为使用r-cnn时图像的大小会调整到300x300,但我可能错了。r-cnn不是某种滑动窗口方法吗?ssd模型的tensorflow代码是否在您的测试图像上给出了类似的结果?@Mika最后一张图像是ssd模型检测到的结果(tensorflow代码),比rcnn好得多,后者应提供更好的结果。
    image_resizer {
      keep_aspect_ratio_resizer {
        min_dimension: 600
        max_dimension: 1024
      }
    }
    
    cvNet.setInput(cv.dnn.blobFromImage(img, swapRB=True))
    
        height_stride: 16
        width_stride: 16