目录

  • 项目背景
  • TensorFlow介绍
  • 环境搭建
  • 模型选用
  • Api使用说明
  • 运行路由
  • 小结

项目背景

产品看到竞品可以标记物体的功能,秉承一贯的他有我也要有,他没有我更要有的作风,丢过来一网站,说这个功能很简单,一定可以实现

这时候万能的谷歌发挥了作用,在茫茫的数据大海中发现了Tensorflow机器学习框架,也就是目前非常火爆的的深度学习(人工智能),既然方案已有,就差一个程序员了

Tensorflow介绍

百科介绍:TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,可被用于语音识别或图像识别等多项机器学习和深度学习领域。

翻译成大白话:是一个深度学习和神经网络的框架,底层C++,通过Python进行控制,当然,也是支持Go、Java等语言

环境搭建

  • Linux/Unix(笔者使用Mac)
  • Python3.6
  • protoc 3.5.1
  • tensorflow 1.7.0
1、克隆文件

git clone https://github.com/guandeng/tensorflow.git

文件目录格式如下

└── tensorflow
├── Dockerfile
├── README.md
├── data
│   ├── models
│   ├── pbtxt
│   └── tf_models
├── object_detection_api.py
├── server.py
├── sh
│   ├── download_data.sh
│   └── ods.sh
├── static
├── templates
└── upload
  • data/models 存放
  • data/pbtxt 物体标识名称
  • data/tf_models 存放tensorflow/models数据
2、安装依赖库

pip3 install -r requirements.txt

3、下载模型

sh sh/download_data.sh

4、添加环境变量PYTHONPATH

echo 'export PYTHONPATH=$PYTHONPATH:pwd/data/tf_models/models/research'>> ~/.bashrc && source ~/.bashrc

5、启动服务

python3 server.py

没有报错,说明你已成功搭建环境,使用过程是不是非常简单,下面介绍代码调用逻辑过程

模型选用

我从谷歌提供几种模型选出来对比

  • Speed 是识别物体速度,值越小,识别越快
  • mAP(平均准确率)是精度和检测边界盒的乘积,值越高神经网络的识别精确度越高,对应Speed越大

为了测试方便,笔者选用轻量级(ssd_mobilenet)作为本次识别物体模型

引入Python库

import numpy as np
import os
import tensorflow as tf
import json
import time
from PIL import Image
# 兼容Python2.7版本
try:
import urllib.request as ulib
except Exception as e:
import urllib as ulib
import re
from object_detection.utils import label_map_util

载入模型

MODEL_NAME = 'data/models/ssd_mobilenet_v2_coco_2018_03_29'
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'
PATH_TO_LABELS = os.path.join('data/pbtxt','mscoco_label_map.pbtxt') # CWH: Add object_detection path
# data/pbtxt下mscoco_label_map.pbtxt最大item.id
NUM_CLASSES = 90
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
# 加载模型
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')

载入标签映射,内置函数返回整数会映射到pbtxt字符标签

mscoco_label_map.pbtxt格式如下

item {
name: "/m/01g317"
id: 1
display_name: "person"
}
item {
name: "/m/0199g"
id: 2
display_name: "bicycle"
}
# 加载标签
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(
label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)
with detection_graph.as_default():
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(graph=detection_graph,config=config) as sess:
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
# 物体坐标
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
# 检测到物体的准确度
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
def get_objects(file_name, threshold=0.5):
image = Image.open(file_name)
# 判断文件是否是jpeg格式
if not image.format=='JPEG':
result['status'] = 0
result['msg'] = file_name+ ' is ' + image.format + ' ods system allow jpeg or jpg'
return result
image_np = load_image_into_numpy_array(image)
# 扩展维度
image_np_expanded = np.expand_dims(image_np, axis=0)
output = []
# 获取运算结果
(boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
# 去掉纬度为1的数组
classes = np.squeeze(classes).astype(np.int32)
scores = np.squeeze(scores)
boxes = np.squeeze(boxes)
for c in range(0, len(classes)):
if scores[c] >= threshold:
item = Object()
item.class_name = category_index[classes[c]]['name'] # 物体名称
item.score = float(scores[c]) # 准确率
# 物体坐标轴百分比
item.y1 = float(boxes[c][0])
item.x1 = float(boxes[c][1])
item.y2 = float(boxes[c][2])
item.x2 = float(boxes[c][3])
output.append(item)
# 返回JSON格式
outputJson = json.dumps([ob.__dict__ for ob in output])
return outputJson

运行路由

server.py下的逻辑

def image():
startTime = time.time()
if request.method=='POST':
image_file = request.files['file']
base_path = os.path.abspath(os.path.dirname(__file__))
upload_path = os.path.join(base_path,'static/upload/')
# 保存上传图片文件
file_name = upload_path + image_file.filename
image_file.save(file_name)
# 准确率过滤值
threshold = request.form.get('threshold',0.5)
# 调用Api服务
objects = object_detection_api.get_objects(file_name, threshold)
# 模板显示
return render_template('index.html',json_data = objects,img=image_file.filename)

curl http://localhost:5000 | python -m json.tool

[
{
"y2": 0.9886252284049988,
"class_name": "bed",
"x2": 0.4297400414943695,
"score": 0.9562674164772034,
"y1": 0.5202791094779968,
"x1": 0
},
{
"y2": 0.9805927872657776,
"class_name": "couch",
"x2": 0.4395904541015625,
"score": 0.6422878503799438,
"y1": 0.5051193833351135,
"x1": 0.00021047890186309814
}
]
  • class_name表示物体标签名称
  • score 可信度值
  • x1,y1表示对象所在最左上点位置
  • x2,y2表示对象最右下点位置

在浏览器访问网址体验

http://localhost:5000/upload

小结

  • Tensorflow使用GPU效率提升几个数量级
  • 可以尝试不同的模型比较速度和准确度
  • 本案例也是支持python2,为了跟上时代步伐,建议使用python3
  • 案例有个摄像头演示,需要https支持,且使用安卓系统

大家肯定很好奇,怎么训练自己需要检测的物体,可以期待下一篇文章

Tensorflow之实现物体检测的更多相关文章

  1. Tensorflow物体检测(Object Detection)API的使用

    Tensorflow在更新1.2版本之后多了很多新功能,其中放出了很多用tf框架写的深度网络结构(看这里),大大降低了吾等调包侠的开发难度,无论是fine-tuning还是该网络结构都方便了不少.这里 ...

  2. Tensorflow 之物体检测

    1)安装Protobuf TensorFlow内部使用Protocol Buffers,物体检测需要特别安装一下. # yum info protobuf protobuf-compiler 2.5. ...

  3. 物体检测之FPN及Mask R-CNN

    对比目前科研届普遍喜欢把问题搞复杂,通过复杂的算法尽量把审稿人搞蒙从而提高论文的接受率的思想,无论是著名的残差网络还是这篇Mask R-CNN,大神的论文尽量遵循著名的奥卡姆剃刀原理:即在所有能解决问 ...

  4. 物体检测丨Faster R-CNN详解

    这篇文章把Faster R-CNN的原理和实现阐述得非常清楚,于是我在读的时候顺便把他翻译成了中文,如果有错误的地方请大家指出. 原文:http://www.telesens.co/2018/03/1 ...

  5. OpenCV学习 物体检测 人脸识别 填充颜色

    介绍 OpenCV是开源计算机视觉和机器学习库.包含成千上万优化过的算法.项目地址:http://opencv.org/about.html.官方文档:http://docs.opencv.org/m ...

  6. opencv,关于物体检测

    关于物体检测 环境:opencv 2.4.11+vs2013 参考: http://www.cnblogs.com/tornadomeet/archive/2012/06/02/2531705.htm ...

  7. 『计算机视觉』物体检测之RefineDet系列

    Two Stage 的精度优势 二阶段的分类:二步法的第一步在分类时,正负样本是极不平衡的,导致分类器训练比较困难,这也是一步法效果不如二步法的原因之一,也是focal loss的motivation ...

  8. 后RCNN时代的物体检测及实例分割进展

    https://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&mid=2650736740&idx=3&sn=cdce446703e69b ...

  9. 利用opencv进行移动物体检测

    进行运动物体检测就是将动态的前景从静态的背景中分离出来.将当前画面与假设是静态背景进行比较发现有明显的变化的区域,就可以认为该区域出现移动的物体.在实际情况中由于光照阴影等因素干扰比较大,通过像素直接 ...

随机推荐

  1. UVALive 6859——凸包&&周长

    题目 链接 题意:在一个网格图上,给出$n$个点的坐标,用一个多边形包围这些点(不能接触,且多边形的边只能是对角线或直线),求多边形的最小周长. 分析 对于每个点,我们考虑与之相邻的4个点.一共由 $ ...

  2. [ML] The Basics: Training Your First Model

    The problem we will solve is to convert from Celsius to Fahrenheit, where the approximate formula is ...

  3. 【Android-开发环境】 eclipse开发环境搭建

    1.下载安装JDK jdk下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htm ...

  4. java接口的多继承

    Java类之间并不允许多继承,只可以单继承和实现多接口,一直以为接口也是一样的,但是查阅了相关资料,突然豁然开朗. 一个类只能extends一个父类,但可以implements多个接口. 一个接口则可 ...

  5. SpringMVC拦截静态资源的解决方法

    本文中的各软件版本简要信息: IDE:Myeclise17 JDK:1.8.0_111 spring:5.1.8 springMVC:5.1.8 mybatis:3.2.2 Tomcat:9.0 在使 ...

  6. Vue 事件监听实现导航栏吸顶效果(页面滚动后定位)

    Vue 事件监听实现导航栏吸顶效果(页面滚动后定位) Howie126313 关注 2017.11.19 15:05* 字数 100 阅读 3154评论 0喜欢 0 所说的吸顶效果就是在页面没有滑动之 ...

  7. 数论之同余性质 线性同余方程&拔山盖世BSGS&中国剩余定理

    先记录一下一些概念和定理 同余:给定整数a,b,c,若用c不停的去除a和b最终所得余数一样,则称a和b对模c同余,记做a≡b (mod c),同余满足自反性,对称性,传递性 定理1: 若a≡b (mo ...

  8. I am coming..

    It's so great to start the blog here since it's been a long time that I want to start such kind of l ...

  9. Could not initialize class sun.awt.X11GraphicsEnvironment异常处理

    原因导致: 经过Google发现很多人也出现同样的问题.从了解了X11GraphicEnvironment这个类的功能入手, 一个Java服务器来处理图片的API基本上是需要运行一个X-server以 ...

  10. mac安装genymotion遇到的问题记录

    1.出错内容:An error occured while deploying the file或者使用adb devices连接的时候出现下面的错误 adb server version (40) ...