使用tensorflow模型(更快的rcnn)检测对象时,opencv dnn的结果看起来很奇怪

问题描述 投票:0回答:1

复制步骤

1:通过tf_text_graph_faster_rcnn.py生成配置文件

python tf_text_graph_faster_rcnn.py --input Frozen_inference_graph.pb --configpipeline.config --output fast_rcnn_inception_resnet_v2_atrous_oid.pbtxt

frozen_inference_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()

结果

  1. 输入图像

enter image description here

  1. 结果

enter image description here

this page上的张量流算法检测到的结果>

enter image description here

尽管仍然有很多汽车无法检测到,但是与opencv的api相比,结果却大不相同

编辑:ssd_mobilenet_v1_coco检测到的结果,更好的结果,我猜opencv dnn模块不适用于我发布的模型吗?

enter image description here

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()

模型动物园的链接模型zip文件的链接OS:win10 64 opencv:4.1.2,由anaconda tensorflow安装:1.15,由pip安装步骤重现1的步骤:通过...生成配置文件...]] >>

cvNet.setInput(cv.dnn.blobFromImage(img, size=(300, 300), swapRB=True, crop=False))

300x300输入大小不正确,因为pipeline.config具有以下预处理指令:

image_resizer {
  keep_aspect_ratio_resizer {
    min_dimension: 600
    max_dimension: 1024
  }
}

这意味着您必须保持图像比例。尝试处理原始帧(具有800x600):

cvNet.setInput(cv.dnn.blobFromImage(img, swapRB=True))

具有置信度阈值0.3pipeline.config修改的输出提及here

    height_stride: 16
    width_stride: 16

enter image description here

python opencv tensorflow
1个回答
0
投票
cvNet.setInput(cv.dnn.blobFromImage(img, size=(300, 300), swapRB=True, crop=False))
© www.soinside.com 2019 - 2024. All rights reserved.