【发布时间】:2020-03-28 02:58:02
【问题描述】:
- Link of the model zoo
- Link of the model zip file
- 操作系统:win10 64
- opencv:4.1.2,由 anaconda 安装
- tensorflow:1.15,由 pip 安装
复制步骤
1 : 通过 tf_text_graph_faster_rcnn.py 生成配置文件
python tf_text_graph_faster_rcnn.py --input freeze_inference_graph.pb --config pipeline.config --output faster_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()
结果
- 输入图片
- 结果
张量流算法在this page上检测到的结果
虽然还是有很多车检测不到,但是和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()
【问题讨论】:
-
使用该网站上的示例网络,您得到的结果与他们得到的结果相同吗?
-
@MickI 不知道在哪里可以找到例子的原图,但是可以看到ssd mobileNet检测到的图像结果
-
可能是 r-cnn 后端没有在 opencv 中正确/完全实现(另一个模型是 ssd 模型)。我不知道 tensorflow 检测 api,是否有某种“版本”?也许你可以找出opencv中实现的版本和你的模型使用的版本?
-
另一件事:我认为使用 r-cnn 时图像不会调整为 300x300,但我可能错了。 r-cnn 不是某种滑动窗口方法吗? ssd 模型的 tensorflow 代码是否在您的测试图像上给出了类似的结果?
-
@Micka 最后一张图是ssd模型(tensorflow代码)检测到的结果,比rcnn好很多,应该会提供更好的结果。
标签: python opencv tensorflow