一、系统安装
此处省略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
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 就完成啦