在RaspberryPi上使用TensorFlow做目标检测
信息来源
系统安装
使用arch检查系统类型,如何是armv7l的话,可能会有些难操作,至少如果你要使用的框架是pytorch的话,会相当麻烦。
连接网络
sudo nmcli dev wifi list
# list all wifi available
sudo nmcli dev wifi connect "your_wifi_name" password "your_wifi_password"
环境配置
sudo apt-get update
sudo apt-get upgrade
# create working directory
mkdir tLite && cd tLite
# download TensorFlow automatically through script
git clone https://github.com/tensorflow/examples --depth 1
cd examples/lite/examples/object_detection/raspberry_pi
# install dependencies
sh setup.sh
Then, you can move utils.py and tflite files that you need to your working directory.
Camera setting
sudo raspi-config
# Camera -> Camera -> Enable Camera -> OK
v4l2-ctl --list-devices
convert YOLO model to .tflite
from ultralytics import YOLO
model = YOLO("YourModel.pt")
model.export(format="tflite")
Specific operation on my project
change yield to return, so that I can debug
Use tflite model
import tensorflow as tf
# 加载模型
interpreter = tf.lite.Interpreter(model_path="your_model.tflite")
# 分配内存
interpreter.allocate_tensors()
# 获取输入输出张量的索引
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# 获取输入数据的形状
input_shape = input_details[0]['shape']
# 准备输入数据(例如,你的输入数据是一个图像或张量)
input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)
# 设置输入数据
interpreter.set_tensor(input_details[0]['index'], input_data)
# 运行推理
interpreter.invoke()
# 获取输出结果
output_data = interpreter.get_tensor(output_details[0]['index'])
print("Output:", output_data)
- preprocess frame, because I used cv2 to get frame
def preprocess_frame(self, frame):
frame = cv2.resize(frame, (self.input_size, self.input_size))
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = frame.astype(np.float32) / 255.0
return frame
- show the result
The result has a extra dimension, so we need to remove it with result_image = yolo_results[0]