Tensorflow无人车使用移动端的SSD(单发多框检测)来识别物体及Graph的认识

环境是树莓派3B+,当然这里安装tensorflow并不是一定要在树莓派环境,只需要是ARM架构就行,也就是目前市场上绝大部分的嵌入式系统都是用这套精简指令集。
在电脑端的检测,有兴趣的可以查阅SSD(Single Shot MultiBox Detector)系列:计算机视觉之SSD改进版本(平滑L1范数损失与焦点损失)《4》

操作系统属于Linux,所以我们来熟悉下自己的硬件环境,主要就是查看自己的芯片架构是属于哪种。

1、Linux查看芯片架构

前面提到了需要在ARM架构里面安装,如何通过指令来查看当前是什么芯片架构。
因为都是在Linux系统中,所以输入的命令都是一样的,下面三条命令中的任意一个都可以查看到所属架构: 

arch
uname -a
file /bin/bash

X86图(我这是安装的WSL):

这里是X86_64,就是X86的64位扩展,最开始是AMD设计的,所以也叫做“AMD64”,后来被Intel采用,它的就叫做“Intel64”
ARM图:

这里的aarch64ARMv8-A架构中引入的64位指令集 ,属于ARM架构。
还有一种Linux系统的收银机,由于占用资源少,使用的是低配的芯片即可,一般是“奔腾”,返回的是i686 

2、安装环境

安装tensorflow,当然这个版本根据自己情况选择,目前最新版本是tensorflow-2.4.0-cp35-none-linux_armv6l.whl
# https://github.com/lhelontra/tensorflow-on-arm/releases
pip install tensorflow-1.8.0-cp27-none-linux_armv7l.whl

2.1、SSD移动端模型

#http://download.tensorflow.org/models/object_detection/ssdlite_mobilenet_v2_coco_2018_05_09.tar.gz
tar -xzvf ssdlite_mobilenet_v2_coco_2018_05_09.tar.gz .

2.2、安装OpenCV视觉库

pip install opencv-python

2.3、运行Demo,命令行运行

python opencv_camera.py
# 或者如果是在Jupyter中加载.py文件,会将其内容拷贝过来,点击运行
%load opencv_camera.py

3、物体识别

接下来我们就来实际操作下,让摄像头检测对象并标注对象类别,这里使用的是CSI摄像头,如果你的是USB接口,代码也有注释。

3.1、导入相关库

导入相关的库,其中的object_detection等源码在最后给出了地址,有兴趣测试的,可以clone下来玩玩。

import numpy as np
import cv2
import os,time
import tensorflow as tf
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_utils
import ipywidgets.widgets as widgets
from image_fun import bgr8_to_jpeg

3.2、摄像头初始化

摄像头初始化,这里也给出了USB接口的摄像头,我这里就借用无人车上面的摄像头,为CSI接口。如果你的摄像头是USB的情况,
可以通过 ls /dev/video* 命令查看设备索引,camera = USBCamera(capture_device=1)

#from jetcam.usb_camera import USBCamera
from jetcam.csi_camera import CSICamera
from jetcam.utils import bgr8_to_jpeg

#camera = USBCamera(width=320, height=240, capture_fps=30)
camera = CSICamera(width=320, height=240, capture_fps=30)
#将相机设置为running = True,为新帧附加回调
camera.running = True

3.3、安装JetCam

当然在这里如果没有安装JetCam,可以先进行安装。JetCam是NVIDIA Jetson的一个易于使用的Python相机接口,安装jetcam

git clone https://github.com/NVIDIA-AI-IOT/jetcam
cd jetcam

sudo python3 setup.py install

3.4、Image显示组件

摄像头初始化好了之后,我们就在Jupyter中新建一个图片组件,用来更新摄像头拍摄过来的数据

image_widget = widgets.Image(format='jpg', width=320, height=240)
display(image_widget)
image_widget.value = bgr8_to_jpeg(camera.value)

运行之后,这里会出现画面,不过是静止的第一帧,想要持续的更新,就会用到后面的两种更新方法。

3.5、初始化模型

将轻量级的SSD模型与标签加载进来

MODEL_NAME = 'ssdlite_mobilenet_v2_coco_2018_05_09'
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb' 
PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')

NUM_CLASSES = 90
IMAGE_SIZE = (12, 8)
fileAlreadyExists = os.path.isfile(PATH_TO_CKPT)

if not fileAlreadyExists:
    print('Model does not exsist !')
    exit

3.6、加载Graph

这里是加载上面的计算图,是一种冻结的序列化图,意思就是说不能训练,就是说拿来做预测的,是一个预训练模型。
另外也加载标签图,将这些标签进行分类,做成一个id与name对应的字典类型。后面将详细介绍Graph的用法!

print('Loading...')
detection_graph = tf.Graph()
with detection_graph.as_default(): #语句下定义属于计算图detection_graph的张量和操作
    od_graph_def = tf.compat.v1.GraphDef()
    with tf.io.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='')
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)
print('Finish Load Graph..')

print(len(category_index),category_index)
print(category_index[1]['name'])#person

这里的category_index是一个80个分类的字典,类似于 {1: {'id': 1, 'name': 'person'}, 2: {'id': 2, 'name': 'bicycle'},...} 如下图:

3.7、摄像头检测

调用摄像头实时检测的方法有两种,一种是一直循环的读取摄像头的值,也就是在死循环里面处理;另外一种就是通过回调函数,当摄像头的值有变化的时候就会自动更新值到图片组件里面。 

3.7.1、死循环 

    # Main
    t_start = time.time()
    fps = 0

    with detection_graph.as_default():
        with tf.compat.v1.Session(graph=detection_graph) as sess:
            while True:
                frame = camera.value
               # ret, frame = cap.read()
    #            frame = cv2.flip(frame, -1) # Flip camera vertically
    #             frame = cv2.resize(frame,(320,240))
                ##############
                image_np_expanded = np.expand_dims(frame, axis=0)
                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')
    #             print('Running detection..')
                (boxes, scores, classes, num) = sess.run( 
                    [detection_boxes, detection_scores, detection_classes, num_detections], 
                    feed_dict={image_tensor: image_np_expanded})
    #             print('Done.  Visualizing..')
                vis_utils.visualize_boxes_and_labels_on_image_array(
                        frame,
                        np.squeeze(boxes),
                        np.squeeze(classes).astype(np.int32),
                        np.squeeze(scores),
                        category_index,
                        use_normalized_coordinates=True,
                        line_thickness=8)
        
                for i in range(0, 10):
                    if scores[0][i] >= 0.5:
                        print(category_index[int(classes[0][i])]['name'])
                ##############
                fps = fps + 1
                mfps = fps / (time.time() - t_start)
                cv2.putText(frame, "FPS " + str(int(mfps)), (10,10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 2)
                image_widget.value = bgr8_to_jpeg(frame)

3.7.2、回调函数

创建一个更新函数,当摄像头启动时就调用它,这样就可以做到摄像头的值变化时,更新函数就会做出处理。一般推荐使用这种方法!

detection_graph.as_default()
sess = tf.compat.v1.Session(graph=detection_graph)
t_start = time.time()
fps = 0

def update_image(change):
    global fps
    global sess
    frame = change['new']
    image_np_expanded = np.expand_dims(frame, axis=0)
    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')

    (boxes, scores, classes, num) = sess.run(
        [detection_boxes, detection_scores, detection_classes, num_detections],
        feed_dict={image_tensor: image_np_expanded})

    # 锚框与标签
    vis_utils.visualize_boxes_and_labels_on_image_array(
        frame,
        np.squeeze(boxes),
        np.squeeze(classes).astype(np.int32),
        np.squeeze(scores),
        category_index,
        use_normalized_coordinates=True,
        line_thickness=8)
    '''
    for i in range(0, 10):
        if scores[0][i] >= 0.5:
            print(category_index[int(classes[0][i])]['name'])
    '''

    fps = fps + 1
    mfps = fps / (time.time() - t_start)
    cv2.putText(frame, "FPS " + str(int(mfps)), (10,10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 2)
    image_widget.value = bgr8_to_jpeg(frame)

#摄像头更新函数
camera.observe(update_image, names='value')

#停止更新,单纯的unobserve不能停止摄像头的继续运行,我加了一个stop停止
#camera.unobserve(update_image, names='value')
#camera.stop()

效果图如下:

当然也存在识别有误的情况,比如下面这张图,将后面的烟,识别成了书,这个也正常,跟数据集有关,毕竟看起来像书的陈列形状。

4、tf.Graph()

这里对上述代码中出现的tf.Graph函数做一个相关介绍,它是Tensorflow中的计算图,对里面张量有专门的计算模块,还可以新建多张计算图,通常表示为G(V,E),其中G表示一个图,V是图G中顶点的集合,E是图G中边的集合。这样的构建就可以并行化处理,对于提升性能很有帮助。
Graph实质是一种集合数组(Tensor张量)与Operation操作的数据结构,可以运行在非Python环境中,只需要对这个数据结构进行解释即可。所以说通过构建Graph,我们可以方便地表示和优化计算过程,从而实现深度学习模型的训练和推断。
一旦Graph被构建完成,我们就可以使用TensorFlow的Session对象来执行这个Graph。在执行过程中,Session会按照Graph中定义的节点和边的关系,将数据从一个节点传递到另一个节点,并执行每个节点的操作,最终得到我们需要的计算结果。
我们来看一个例子:

g1 = tf.Graph()
g2 = tf.Graph()

with g1.as_default():
    a = tf.constant([1,2])
    b = tf.constant([3,4])
    c = tf.constant([5,6])
    result1 = a + b + c

with g2.as_default():
    d = tf.constant([11,22,33])
    e = tf.constant([33,44,55])
    result2 = d * e

定义好两个计算图之后,我们就可以分别在其下面计算,然后通过Sessionrun来执行计算的结果

with tf.compat.v1.Session(graph=g1) as sess:
    print(a)#Tensor("Const_12:0", shape=(2,), dtype=int32)
    print(result1)#Tensor("add_8:0", shape=(2,), dtype=int32)
    print(sess.run(result1))#[ 9 12]

可以看到在g1计算图下面,我们可以查看计算节点与计算节点,这里是做相加的操作,接着看下g2做相乘的操作:

with tf.compat.v1.Session(graph=g2) as sess:
    print(d)#Tensor("Const_12:0", shape=(3,), dtype=int32)
    print(result2)#Tensor("mul:0", shape=(3,), dtype=int32)
    print(sess.run(result2))#[ 363  968 1815]

我们也可以对这些张量的计算图属于哪一个做一个验证:

print(a.graph is g1)#True
print(a.graph is g2)#False
print(d.graph is g1)#False
print(d.graph is g2)#True

4.1、run()中fetches参数

当然这个run函数里面的fetches参数值除了上面求和求积操作,还可以是单元素,列表,元组

import tensorflow as tf

sess = tf.compat.v1.Session()
a = tf.constant([10, 20])
b = tf.constant([1.0, 2.0])

v = sess.run(a)
print(type(v),v)#<class 'numpy.ndarray'> [10 20]

v = sess.run([a, b])
print(v)#[array([10, 20], dtype=int32), array([1., 2.], dtype=float32)]

当然也可以是字典,里面的a,b值将转成numpy数组

import collections
MyData = collections.namedtuple('MyData', ['a', 'b'])
v = sess.run({'k1': MyData(a, b), 'k2': [b, a]})
print(v)
#{'k1': MyData(a=array([10, 20], dtype=int32), b=array([1., 2.], dtype=float32)), 'k2': [array([1., 2.], dtype=float32), array([10, 20], dtype=int32)]}

4.2、run()中feed_dict参数

这个feed_dict参数的输入值需要是字典类型:

v = sess.run([a, b],feed_dict={b:[33,44]})
print(v)
#[array([10, 20], dtype=int32), array([33., 44.], dtype=float32)]

更详细源码:https://github.com/yihangzhao/SSDMobile

  • 24
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 23
    评论
使用TensorFlow识别物体并框出来,可以使用目标检测模型,如Faster R-CNN,SSD或YOLO等。这些模型可以识别图像中的物体,并用边界框框出它们。 以下是实现此功能的一些步骤: 1. 安装 TensorFlow Object Detection API 您需要安装TensorFlow Object Detection API,该API提供了训练和推断目标检测模型所需的工具和库。您可以在此处找到安装说明:https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md 2. 下载预训练模型 TensorFlow Object Detection API提供了一些预训练的目标检测模型,您可以从以下链接中选择一个模型并下载其预训练权重:https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md 3. 加载模型 使用TensorFlow Object Detection API加载预训练模型,并将其用于图像中的目标检测。您可以使用以下代码段加载模型: ```python import tensorflow as tf from object_detection.utils import label_map_util from object_detection.utils import visualization_utils as vis_util # Path to frozen detection graph. This is the actual model that is used for the object detection. PATH_TO_CKPT = 'path/to/frozen_inference_graph.pb' # List of the strings that is used to add correct label for each box. PATH_TO_LABELS = 'path/to/label_map.pbtxt' NUM_CLASSES = 90 # Load the frozen detection graph into memory 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='') # Load label map 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) ``` 4. 运行推断 使用加载的模型运行推断,并绘制边界框以框出检测到的物体。您可以使用以下代码段运行推断: ```python import numpy as np import cv2 # Load image image = cv2.imread('path/to/image.jpg') # Define input and output tensors (i.e., image and bounding boxes) 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') # Run inference with detection_graph.as_default(): with tf.Session() as sess: # Expand dimensions since the model expects images to have shape: [1, None, None, 3] image_expanded = np.expand_dims(image, axis=0) (boxes, scores, classes, num) = sess.run( [detection_boxes, detection_scores, detection_classes, num_detections], feed_dict={image_tensor: image_expanded}) # Visualize the results vis_util.visualize_boxes_and_labels_on_image_array( image, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, use_normalized_coordinates=True, line_thickness=8) # Display the resulting image cv2.imshow('Object detection', cv2.resize(image, (800, 600))) cv2.waitKey(0) cv2.destroyAllWindows() ``` 这将在图像中绘制边界框,以框出检测到的物体。您可以根据需要调整绘制边界框的参数,例如线条宽度和颜色。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寅恪光潜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值