opencv+tensorFlow探索实现目标检测

前述:

本文为了记载自己在开发过程中的心得以及问题解决而写,阅读者在参考时尽量加入自己的思考,毕竟依赖库以及主程序随着时间的变化正在不断升级;
另外,牢骚一下,对于初学者,很多作者在写文章的时候,会漏掉诸多环节,对于他们而言,那是不言而喻的东西,对于初学者,那些是什么,怎么做?

参考地址:

https://tensorflow.google.cn/install/pip#windows
https://blog.csdn.net/kh971024/article/details/103391094


重新安装python:

python-3.7.9.exe是32位版本的python,在寻找对应的tensorflow时,无法找到,试了很多的方法均无法匹配,因此删除3.7.9版本,改为使用【python-3.7.9-amd64.exe】;同样的,openCV也需要安装64位版本;
安装文件如何寻找,可以参见博主之前的文章python+openCV安装(WINDOWS环境)


安装tensorflow:

命令行中执行:pip3 install tensorflow
在下图中可以看到可以自动匹配到tensorflow的版本并且进行自动下载安装,但是由于网速有限,依然需要在https://pypi.org/上找到【tensorflow-2.3.0-cp37-cp37m-win_amd64.whl】文件下载后本地安装。
在这里插入图片描述

根据博主之前的文章python+openCV安装(WINDOWS环境)目前初始环境已经具备,即:Python 3.7.9;tensorflow 2.3.0;opencv 4.4.0


COCO数据集:

博主把下边这个表格拷贝过来,这个表格代表了每一种模型的速度、识别率等情况,表格中的超链接可以直接链接到下载地址,如果无法下载,可以拷贝超链接后,使用百度网盘等工具中的离线下载,亲测好用。
另外,其他模型暂时不考虑,主要是初学,贪多嚼不烂。

Model nameSpeed (ms)COCO mAP1Outputs
ssd_mobilenet_v1_coco 3021Boxes
ssd_mobilenet_v1_0.75_depth_coco ☆ 2618Boxes
ssd_mobilenet_v1_quantized_coco ☆ 2918Boxes
ssd_mobilenet_v1_0.75_depth_quantized_coco ☆ 2916Boxes
ssd_mobilenet_v1_ppn_coco ☆ 2620Boxes
ssd_mobilenet_v1_fpn_coco ☆ 5632Boxes
ssd_resnet_50_fpn_coco ☆ 7635Boxes
ssd_mobilenet_v2_coco 3122Boxes
ssd_mobilenet_v2_quantized_coco 2922Boxes
ssdlite_mobilenet_v2_coco 2722Boxes
ssd_inception_v2_coco 4224Boxes
faster_rcnn_inception_v2_coco 5828Boxes
faster_rcnn_resnet50_coco 8930Boxes
faster_rcnn_resnet50_lowproposals_coco 64 Boxes
rfcn_resnet101_coco 9230Boxes
faster_rcnn_resnet101_coco 10632Boxes
faster_rcnn_resnet101_lowproposals_coco 82 Boxes
faster_rcnn_inception_resnet_v2_atrous_coco 62037Boxes
faster_rcnn_inception_resnet_v2_atrous_lowproposals_coco 241 Boxes
faster_rcnn_nas 183343Boxes
faster_rcnn_nas_lowproposals_coco 540 Boxes
mask_rcnn_inception_resnet_v2_atrous_coco 77136Masks
mask_rcnn_inception_v2_coco 7925Masks
mask_rcnn_resnet101_atrous_coco 47033Masks
mask_rcnn_resnet50_atrous_coco 34329Masks

这里以ssd_mobilenet_v2_coco_2018_03_29模型为例,
1、将其解压后,文件夹样式如图:
在这里插入图片描述
2、增加一个文件classes.txt,这个里面是将需要识别的类别名称列举到其中,博主把已知的所有名称都列举进去了(自己理解越少,识别起来越速度)。
3、书写python代码,并且调整某些参数:

########################################################
## DIC:tensorFlow auto recognition SSD Demo
## Base on ssd_mobilenet_v2_coco
## Depend On:Python 3.7.9;tensorflow 2.3.0;opencv 4.4.0
## Author:J.Y.Zhang 2020-09-11
## Ver:1.0
## See:## See: https://editor.csdn.net/md/?articleId=108280933
########################################################
import os
import numpy as np
import cv2
import matplotlib.pyplot as plt
import tensorflow as tf

# 加载coco数据集模型 TODO 需要改为自己文件路径
model_path = "F:/traindata/ssd_mobilenet_v2_coco_2018_03_29"
frozen_pb_file = os.path.join(model_path, 'frozen_inference_graph.pb')

# 加载coco数据集分类 TODO 需要改为自己文件路径
f = open("F:/traindata/ssd_mobilenet_v2_coco_2018_03_29/classes.txt", "r")
class_names = f.readlines()

# 此值决定了何种情况下判定为元素,取值越低认定的可能性越高,相应的图中的框就越多,识别为假的可能性越高
# TODO 如果需要则调整
score_threshold = 0.3
# 需要识别的图片的位置以及名称 TODO 需要改为自己文件路径
img_file = 'F:/traindata/labelImgdata/img/4.jpg'
# resize img for a format size TODO 可以修改成为自己想要的尺寸
RESIZE_IMG_WIDTH = 500
RESIZE_IMG_HEIGHT = 330

# Read the graph.
with tf.gfile.FastGFile(frozen_pb_file, '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_cv2 = cv2.imread(img_file)
    img_height, img_width, _ = img_cv2.shape
    # 对图片的长宽进行格式化,防止图片过大
    img_in = cv2.resize(img_cv2, (RESIZE_IMG_WIDTH, RESIZE_IMG_HEIGHT))
    img_in = img_in[:, :, [2, 1, 0]]  # BGR2RGB

    # Run the model
    outputs = 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': img_in.reshape(1, img_in.shape[0],
                            img_in.shape[1], 3)})

    # Visualize detected bounding boxes.
    num_detections = int(outputs[0][0])
    for i in range(num_detections):
        classId = int(outputs[3][0][i])
        score = float(outputs[1][0][i])
        bbox = [float(v) for v in outputs[2][0][i]]
        if score > score_threshold:
            x = bbox[1] * img_width
            y = bbox[0] * img_height
            right = bbox[3] * img_width
            bottom = bbox[2] * img_height
            # 标框
            cv2.rectangle(img_cv2, (int(x), int(y)), (int(right), int(bottom)), (125, 255, 51), thickness=3)
            # 文字"class_name, score"
            cv2.putText(img_cv2, class_names[classId - 1][:-1] + "," + str("%.2f" % score), (int(x), int(y)),
                        cv2.FONT_HERSHEY_DUPLEX, .5, (0, 0, 255), 1)
            print(str(classId) + ",class:" + class_names[classId - 1][:-1] + ",score:%.2f" % score)

# figsize : 指定figure的宽和高,单位为英寸
plt.figure(figsize=(10, 8))
plt.imshow(img_cv2[:, :, ::-1])
plt.title("TensorFlow MobileNetV2-SSD")
plt.axis("off")
plt.show()

4、执行代码:在这里插入图片描述
我们可以尝试使用不同的图片、修改代码中的score_threshold值等方式查看执行情况。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值