树莓派3B+(适合树莓派3B)运行Tensorflow Object Detection-程序员宅基地

技术标签: python  tensorflow  linux  

一、系统安装

此处省略XX字,在我的博客树莓派3B+(适合树莓派3B) Qt 使用 Cmake C++ OpenCV此处链接中具有详细的系统安装教程,此处延用当时的环境,源,系统,配置等

二、正式安装

1.Update Raspberry Pi

sudo apt-get update
sudo apt-get upgrade

完成如下,上次升过级,所以这次很快,第一次升级速度会很慢
完成

2.Install TensorFlow

sudo apt-get install libatlas-base-dev
sudo pip3 install tensorflow
sudo pip3 install pillow lxml jupyter matplotlib cython
sudo apt-get install python-tk

TensorFlow 安装特别容易失败,下载内容为:
https://www.piwheels.org/simple/tensorflow/tensorflow-1.14.0-cp35-none-linux_armv7l.whl,注意我的是 Python 3.5,注意查看下载的网址文件
包
失败
我利用PC通过某种方法下载好了此文件,并利用 FileZilla Pro 上传至树莓派,可以使用命令行安装,需要此文件可以评论留言索取
在这里插入图片描述
安装此文件

sudo pip3 install tensorflow-1.14.0-cp35-none-linux_armv7l.whl

ls 为查看当前目录文件
安装
安装完成
完成
导入 tensorflow 包,查看版本,弹出的警告是 tensorflow-1.14.0 在新版本语法更新而已,不用理睬

python
import tensorflow
tensorflow.__version__


安装剩余依赖

sudo pip3 install pillow lxml jupyter matplotlib cython
sudo apt-get install python-tk

安装完成
完成

3.Install OpenCV

此处省略XX字,在我的博客树莓派3B+(适合树莓派3B) Qt 使用 Cmake C++ OpenCV此处链接中具有详细的 Python OpenCV 安装教程,此处延用当时的环境,源,系统,配置等,由于已经安装,所以我可以直接导入

python3
import cv2
cv2.__version

CV

4.Compile and install Protubuf

安装命令

sudo apt-get install protobuf-compiler

安装完成
完成

5.Set up TensorFlow directory structure

文件夹
下载太慢,下载不下来的话,可以使用 PC 本地下载并使用 FileZilla Pro 上传至树莓派
上传
执行如下命令,并且我把文件夹重命名为 TensorFlowModels,反正你用啥方法就是要这个 models

unzip models-master.zip

为了方便管理文件夹,我把之前下载的压缩包都移进了这个文件夹下
文件夹
下面对此 TensorFlowModels 进行环境配置
修改 bashrc 文件

sudo nano ~/.bashrc
export PYTHONPATH=$PYTHONPATH:/home/pi/TensorFlowModels/research:/home/pi/TensorFlowModels/research/slim

然后 Ctrl X → Y 保存退出
修改
输入以下命令使环境变量生效

echo $PYTHONPATH

关闭 Terminal 再打开再次输入以下命令即可看到环境变量

echo $PYTHONPATH

环境变量
进入 object_detection 文件夹并下载模型,点击此处下载模型,ssd_mobilenet_v2_coco 模型较小,选择并下载,可以 PC 本地下载并通过 FileZilla Pro 上传到树莓派,也可以右键复制链接在 Terminal 下载

cd TensorFlowModels/research/object_detection
wget 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

解压后添加label_map.pbtxt,只需要将以下文件复制过来并重命名为 label_map.pbtxt 就行

/home/pi/TensorFlowModels/research/object_detection/data/mscoco_label_map.pbtxt

文件夹
在 research 文件夹下构建 protos

protoc object_detection/protos/*.proto --python_out=.

6.Test out object detector

程序部分
demo.image.py

import numpy as np
import matplotlib
import os
import sys
import tensorflow as tf
from PIL import Image
from matplotlib import pyplot as plt

# This is needed since the python file is stored in the object_detection folder.
sys.path.append("..")

from utils import label_map_util
from utils import visualization_utils as vis_util

matplotlib.use('TkAgg')

# What model to download.
MODEL_NAME = 'ssdlite_mobilenet_v2_coco_2018_05_09'

# Path to frozen detection graph. This is the actual model that is used for the object detection.
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'

# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = os.path.join('ssdlite_mobilenet_v2_coco_2018_05_09/label_map.pbtxt')

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='')
    
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)

def load_image_into_numpy_array(image):
  (im_width, im_height) = image.size
  return np.array(image.getdata()).reshape(
      (im_height, im_width, 3)).astype(np.uint8)

# For the sake of simplicity we will use only 2 images:
# image1.jpg
# image2.jpg
# If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS.
PATH_TO_TEST_IMAGES_DIR = 'images'
TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 2) ]

# Size, in inches, of the output images.
IMAGE_SIZE = (12, 8)

with detection_graph.as_default():
  with tf.Session(graph=detection_graph) as sess:
    # Definite input and output Tensors for detection_graph
    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
    # Each box represents a part of the image where a particular object was detected.
    detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
    # Each score represent how level of confidence for each of the objects.
    # Score is shown on the result image, together with the class label.
    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')
    for image_path in TEST_IMAGE_PATHS:
      image = Image.open(image_path)
      # the array based representation of the image will be used later in order to prepare the
      # result image with boxes and labels on it.
      
      image_np = load_image_into_numpy_array(image)
      
      # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
      
      #image_np_expanded = np.expand_dims(image_np, axis=0)
      image_np_expanded = np.expand_dims(image_np, axis=0)
      
      # Actual detection.
      (boxes, scores, classes, num) = sess.run(
          [detection_boxes, detection_scores, detection_classes, num_detections],
          feed_dict={
    image_tensor: image_np_expanded})
      # Visualization of the results of a detection.
      vis_util.visualize_boxes_and_labels_on_image_array(
          image_np,
          np.squeeze(boxes),
          np.squeeze(classes).astype(np.int32),
          np.squeeze(scores),
          category_index,
          use_normalized_coordinates=True,
          line_thickness=16,)
      plt.figure(figsize=IMAGE_SIZE)
      plt.imshow(image_np)
      plt.show()

运行结果
图片显示

三、TensorFlow 1.14.0源码修正与字体大小修改

留意博主下篇文章

到此为止,在树莓派上运行 TensorFlow 就完成啦

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_39567427/article/details/104046114

智能推荐

AndroidManifest.xml配置文件详解_androidmainfest.xml声明内容提供其的标签名称是-程序员宅基地

AndroidManifest.xml配置文件对于Android应用开发来说是非常重要的基础知识,本文旨在总结该配置文件中重点的用法,以便日后查阅。下面是一个标准的AndroidManifest.xml文件样例。[html] view plaincopyxml version="1.0" encoding="utf-8"?_androidmainfest.xml声明内容提供其的标签名称是

如何获取高精度CV模型?快来试试百度EasyDL超大规模视觉预训练模型-程序员宅基地

在深度学习领域,有一个名词正在被越来越频繁地得到关注:迁移学习。它相比效果表现好的监督学习来说,可以减去大量的枯燥标注过程,简单来说就是在大数据集训练的预训练模型上进行小数据集的迁移,以..._cv怎么找到适合的模型

粒子群算法及其改进_粒子群算法的改进_无人机开发的博客-程序员宅基地

1 粒子群算法介绍求解非线性最优化问题时,有一种比较常用的算法为智能体算法,这里我们介绍的粒子群算法就隶属于智能体算法。粒子群算法是模拟鸟寻找食物:一群鸟在随机的搜索食物。在这个区域里只有一块食物,所有的鸟都不知道食物在那。但是它们知道自己当前的位置距离食物还有多远。 然后他们根据群体中个体之间的协作和信息共享来寻找到食物。2、粒子群算法介绍在粒子群优化算法中,每个解可用一只鸟(粒子)表示,目标函数就是鸟群所需要寻找的食物源。寻找最优解的过程中,粒子包含两种行为:个体行为和群体行为。个体行为:粒_粒子群算法的改进

OVS vswitchd启动(三十五)_ovsrec_init-程序员宅基地

vswitchd 作为守护进程和ovsdb 通信以及和controller 进行openflow 通信,并完成和底层内核的交互。代码在vswitchd/目录下面,可以从main 函数入口分析,整体处理流程如下:_ovsrec_init

mysql 根据父Id 递归查询所有的子类Id,根据子类Id 递归查询所有的父类 Id_mysql查询父id下的子id-程序员宅基地

一、根据父Id 递归查询所有的子类Id 应用场景: 在树形组件的开关中会用到,如下图所示:技术手段:用mysql函数实现,如下:函数名:getSonIdsBEGIN DECLARE sTemp VARCHAR(1000); DECLARE sTempChd VARCHAR(1000); SET sTemp = ''; SET sTempChd =cast(rootId as CHAR); WHILE sTem..._mysql查询父id下的子id

第三种宽带WiMAX-程序员宅基地

WiMAX(World Interoperability for Microwave Access)是全球微波接入互操作性技术的简称,旨在为基于IEEE 802.16标准和ETSI HiperMAN标准的宽带无线接入技术和产品提供一套测试认证的规范和认证体系,对其进行一致性和互操作性的认证。WiMAX的基本目标是提供一种在城域网一点对多点的多厂商环境下,可有效进行互操作的宽带无线接入手段。WiMA

随便推点

WinHex数据恢复 (FAT16基础)_winhex查看fat16-程序员宅基地

WinHex数据恢复-FAT16(基础)_winhex查看fat16

静态表单提交到php数据库_如何将联系表单添加到静态网站-程序员宅基地

静态表单提交到php数据库 该帖子最初发布在 Codementor上 。 随着静态网站的兴起,开发人员需要一种可以处理表单的服务。 静态网站联系表单是最常见的情况,在本文中,您将学习如何使用Kwes form builder添加表单 。 将联系表单添加到静态站点可能是一个挑战,因为静态站点通常没有可以处理表单提交的后端。 在这种情况下,我们可以使用可以为我们做到这一点的服务。 有许多服务..._php 没有服务器 静态提交表单

android人脸检测开发——基于百度离线SDK开发-程序员宅基地

官方SDK教程离线-安卓-基础版https://ai.baidu.com/ai-doc/FACE/sk37c1p6e离线-安卓-活体版https://ai.baidu.com/ai-doc/FACE/Mk37c1pue人脸检测API(v3)https://ai.baidu.com/ai-doc/FACE/yk37c1u4t百度也在卖集成SDK的软硬件开发套件,例如:https://aim.baidu.com/product/83d5eab9-a414-4...

php表单、上传文件-程序员宅基地

<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>用户注册</title></head><body> <h2>用户注册</h2><div class="rigester"><form action="check.php" method="post" ><fieldset&g

【CSS】从一个CSS属性开始说起_css md-程序员宅基地

前言作为一名前端开发者,想必你也经常使用CSS background 属性,那你知道这个属性是哪些属性的简写吗?知道 background-color 背景颜色永远是最低?知道 background 属性设置值有什么注意事项吗?带着上面的问题,我们一起揭开 background 神秘的面纱~Background知多少background 是一个 CSS 简写属性,用于设置与背景有关的内容。例如:图片,颜色、尺寸等。它是多个 CSS 属性的集合:background-color: 设置背景颜色_css md

从vue到react入门_鱼&渔的博客-程序员宅基地

写在前面的话有人说前端技术更新日新月异,框架版本更新快,前面你刚配置好环境,一不留神,不知道什么时候作者悄悄更新了插件,API,你的项目就跑不动了,留下你一脸茫然找bug......但对于前端框架,其实不管是vue从1.0到2.0,再到翻天覆地的3.0版本;或是Angular半年更新一个版本,亦或者React从2013年的v0.3.0到今天的v17.0.2,前端又是一直未变的,依旧还是html+css+js的三剑客组合。前端一直关注和完善的都是这三块的内容。对于一个用过一年Angular,后.._从vue到react