金融类网站建设,windows安装wordpress,虚拟邮箱注册网站,成品网站速成网站环境是树莓派3B#xff0c;当然这里安装tensorflow并不是一定要在树莓派环境#xff0c;只需要是ARM架构就行#xff0c;也就是目前市场上绝大部分的嵌入式系统都是用这套精简指令集。 在电脑端的检测#xff0c;有兴趣的可以查阅SSD(Single Shot MultiBox Detector)系列当然这里安装tensorflow并不是一定要在树莓派环境只需要是ARM架构就行也就是目前市场上绝大部分的嵌入式系统都是用这套精简指令集。 在电脑端的检测有兴趣的可以查阅SSD(Single Shot MultiBox Detector)系列计算机视觉之SSD改进版本(平滑L1范数损失与焦点损失)《4》
操作系统属于Linux所以我们来熟悉下自己的硬件环境主要就是查看自己的芯片架构是属于哪种。
1、Linux查看芯片架构
前面提到了需要在ARM架构里面安装如何通过指令来查看当前是什么芯片架构。 因为都是在Linux系统中所以输入的命令都是一样的下面三条命令中的任意一个都可以查看到所属架构 archuname -afile /bin/bash X86图(我这是安装的WSL)
这里是X86_64就是X86的64位扩展最开始是AMD设计的所以也叫做“AMD64”后来被Intel采用它的就叫做“Intel64” ARM图
这里的aarch64是ARMv8-A架构中引入的64位指令集 属于ARM架构。 还有一种Linux系统的收银机由于占用资源少使用的是低配的芯片即可一般是“奔腾”返回的是i686
2、安装环境
安装tensorflow当然这个版本根据自己情况选择目前最新版本是tensorflow-2.4.0-cp35-none-linux_armv6l.whl # https://github.com/lhelontra/tensorflow-on-arm/releasespip 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.gztar -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_device1)
#from jetcam.usb_camera import USBCamera
from jetcam.csi_camera import CSICamera
from jetcam.utils import bgr8_to_jpeg#camera USBCamera(width320, height240, capture_fps30)
camera CSICamera(width320, height240, capture_fps30)
#将相机设置为running True为新帧附加回调
camera.running True
3.3、安装JetCam
当然在这里如果没有安装JetCam可以先进行安装。JetCam是NVIDIA Jetson的一个易于使用的Python相机接口安装jetcam git clone https://github.com/NVIDIA-AI-IOT/jetcamcd jetcamsudo python3 setup.py install 3.4、Image显示组件
摄像头初始化好了之后我们就在Jupyter中新建一个图片组件用来更新摄像头拍摄过来的数据
image_widget widgets.Image(formatjpg, width320, height240)
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_classesNUM_CLASSES, use_display_nameTrue)
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、死循环 # Maint_start time.time()fps 0with detection_graph.as_default():with tf.compat.v1.Session(graphdetection_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, axis0)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_coordinatesTrue,line_thickness8)for i in range(0, 10):if scores[0][i] 0.5:print(category_index[int(classes[0][i])][name])##############fps fps 1mfps 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(graphdetection_graph)
t_start time.time()
fps 0def update_image(change):global fpsglobal sessframe change[new]image_np_expanded np.expand_dims(frame, axis0)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_coordinatesTrue,line_thickness8)for i in range(0, 10):if scores[0][i] 0.5:print(category_index[int(classes[0][i])][name])fps fps 1mfps 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, namesvalue)#停止更新单纯的unobserve不能停止摄像头的继续运行我加了一个stop停止
#camera.unobserve(update_image, namesvalue)
#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 cwith g2.as_default():d tf.constant([11,22,33])e tf.constant([33,44,55])result2 d * e
定义好两个计算图之后我们就可以分别在其下面计算然后通过Session的run来执行计算的结果
with tf.compat.v1.Session(graphg1) as sess:print(a)#Tensor(Const_12:0, shape(2,), dtypeint32)print(result1)#Tensor(add_8:0, shape(2,), dtypeint32)print(sess.run(result1))#[ 9 12]
可以看到在g1计算图下面我们可以查看计算节点与计算节点这里是做相加的操作接着看下g2做相乘的操作
with tf.compat.v1.Session(graphg2) as sess:print(d)#Tensor(Const_12:0, shape(3,), dtypeint32)print(result2)#Tensor(mul:0, shape(3,), dtypeint32)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 tfsess 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], dtypeint32), array([1., 2.], dtypefloat32)]
当然也可以是字典里面的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(aarray([10, 20], dtypeint32), barray([1., 2.], dtypefloat32)), k2: [array([1., 2.], dtypefloat32), array([10, 20], dtypeint32)]}
4.2、run()中feed_dict参数
这个feed_dict参数的输入值需要是字典类型
v sess.run([a, b],feed_dict{b:[33,44]})
print(v)
#[array([10, 20], dtypeint32), array([33., 44.], dtypefloat32)]
更详细源码https://github.com/yihangzhao/SSDMobile