Object_Detector_API使用(静态图像识别)

环境搭建




1. 从GitHub上下载models,网址:https://github.com/tensorflow/models


2. Protobuf下载,我下载的版本为 Object_Detector_API使用(静态图像识别) (3.5.0版本后面运行有问题),网址:https://github.com/google/protobuf/releases


Object_Detector_API使用(静态图像识别)


3. 解压下载的压缩包,并将bin文件夹中的Object_Detector_API使用(静态图像识别) 复制到C:\Windows目录下,如下:


Object_Detector_API使用(静态图像识别)

Object_Detector_API使用(静态图像识别)


4. 打开cmd窗口批,cd到models/research/目录下,如下:


Object_Detector_API使用(静态图像识别)


5. 输入如下命令:


Object_Detector_API使用(静态图像识别)


6. 不报错即可,如下:


Object_Detector_API使用(静态图像识别)


7. 在路径下生成一堆python文件,如下图所示:


Object_Detector_API使用(静态图像识别)


8. 测试安装,输入如下命令:


Object_Detector_API使用(静态图像识别)


提示错误如下:


Object_Detector_API使用(静态图像识别)


解决方法:到C:\ProgramFiles\Anaconda3\Lib\site-packages目录下,找到tensorflow_model.pth文件,如下:


Object_Detector_API使用(静态图像识别)


在文件内添加如下路径:


Object_Detector_API使用(静态图像识别)


9. 再次运行即可,提示OK:


Object_Detector_API使用(静态图像识别)


以上即环境搭建完成;



读取实例程序



1. 进入object_detection文件中,点右键新建一object_detection_tutorial.py文件;


Object_Detector_API使用(静态图像识别)


用Notepad++打开,选择上面的“语言”—“P”—“Python”;


2. 然后将下面的代码复制进去;


Object_Detector_API使用(静态图像识别)

Object_Detector_API使用(静态图像识别)

Object_Detector_API使用(静态图像识别)

Object_Detector_API使用(静态图像识别)

Object_Detector_API使用(静态图像识别)


3. 然后,点“编码”——“以UTF-8无BOM格式保存”;


4. 打开Spyder软件,打开object_detection_tutorial.py文件,运行,主要是读取object_detection\test_images中的图片:image1和image2,做目标检测;



模型的选择





可用的模型在object_detection\samples\configs目录下,主要使用热门的FasterR-CNN、R-FCN、SSD模型,文件命令形式为:“模型+框架+数据集.config”,如下:


Object_Detector_API使用(静态图像识别)


3种模型运行准确率如下表,Speed表示速度,COCO mAP表示在COCO数据集上的平均准确率,第一个ssd_mobilenet就是我们默认使用的pre-train模型


Object_Detector_API使用(静态图像识别)


程序默认使用的是SSD模型,可以对其进行修改,可用的模型为:


Object_Detector_API使用(静态图像识别)


代码修改如下:


Object_Detector_API使用(静态图像识别)



运行结果



主要时间用于下载模型,目标识别还是蛮快的(使用训练好的模型进行目标识别);


Object_Detector_API使用(静态图像识别)

Object_Detector_API使用(静态图像识别)


测试自己的图片



修改【PATH_TO_TEST_IMAGES_DIR】路径,要在models-master\object_detection目录下。相应的数量for i in range(1, 3)也要进行改变。


源码为:


Object_Detector_API使用(静态图像识别)


只需修改图片路径及路径下的哪些图像(例如:修改为D盘下的图),代码为:


Object_Detector_API使用(静态图像识别)


Object_Detector_API使用(静态图像识别)

Object_Detector_API使用(静态图像识别) 运行,结果如下:



Object_Detector_API使用(静态图像识别) Object_Detector_API使用(静态图像识别)


Object_Detector_API使用(静态图像识别) Object_Detector_API使用(静态图像识别)

Object_Detector_API使用(静态图像识别) Object_Detector_API使用(静态图像识别)


代码修改为:


Object_Detector_API使用(静态图像识别)


运行结果如下:


Object_Detector_API使用(静态图像识别) Object_Detector_API使用(静态图像识别)

Object_Detector_API使用(静态图像识别)


Object_Detector_API使用(静态图像识别) Object_Detector_API使用(静态图像识别)

Object_Detector_API使用(静态图像识别)


Object_Detector_API使用(静态图像识别)

Object_Detector_API使用(静态图像识别)



修改模型并测试



1. 将模型修改为:faster_rcnn_resnet101,代码修改如下:


Object_Detector_API使用(静态图像识别)


2. 在工程目录下,会下载此模型的压缩包,如下:


Object_Detector_API使用(静态图像识别)


3. 测试,修改测试图片,如下:


Object_Detector_API使用(静态图像识别)


4. 测试效果如下:


第一行图为faster_rcnn模型测试效果,第二行是SSD模型测试效果图


Object_Detector_API使用(静态图像识别) Object_Detector_API使用(静态图像识别)

Object_Detector_API使用(静态图像识别) Object_Detector_API使用(静态图像识别)