图像识别系统 - Ubuntu部署指南(香橙派开发板测试)-学习记录1

图像识别系统 - Ubuntu部署指南-学习记录1

本文档提供在Ubuntu系统上部署图像识别系统的详细步骤。

系统要求

  • Ubuntu 18.04 LTS 或更高版本
  • Python 3.6+
  • 至少2GB内存
  • 至少1GB可用磁盘空间

部署步骤

1. 安装系统依赖

bash 复制代码
# 更新系统包
sudo apt update
sudo apt upgrade -y

# 安装Python及相关工具
sudo apt install -y python3 python3-pip python3-dev
sudo apt install -y python3-venv
sudo apt install -y build-essential libssl-dev libffi-dev

# 安装OpenCV依赖
sudo apt install -y libsm6 libxext6 libxrender-dev libgl1-mesa-glx

2. 创建项目目录

bash 复制代码
# 创建项目目录
mkdir -p ~/pd_recognition_system
cd ~/pd_recognition_system

3. 设置Python虚拟环境

bash 复制代码
# 创建虚拟环境
python3 -m venv svm_venv

# 激活虚拟环境
source svm_venv/bin/activate

4. 获取项目代码

方法一:克隆Git仓库(如果使用Git)

bash 复制代码
git clone <项目Git仓库URL> .

方法二:手动上传项目文件

使用SCP或SFTP等工具将项目文件上传到Ubuntu服务器的~/pd_recognition_system目录下。确保上传以下内容:

  • svm_fastapi.py
  • requirements.txt
  • svm_pd_model/ 目录及其中的所有文件
  • test_dataset/ 目录(可选,仅用于测试)

5. 安装项目依赖

bash 复制代码
# 确保虚拟环境已激活
pip install --upgrade pip
pip install -r requirements.txt

6. 测试服务

bash 复制代码
# 运行服务
python svm_fastapi.py

访问 http://<服务器IP>:9000/docs 检查API文档界面是否正常加载。

7. 配置系统服务(使服务在后台运行)

创建systemd服务文件:

bash 复制代码
sudo nano /etc/systemd/system/pd-recognition.service

添加以下内容:

复制代码
[Unit]
Description=PD Recognition System API Service
After=network.target

[Service]
User=<你的用户名>
Group=<你的用户组>
WorkingDirectory=/home/<你的用户名>/pd_recognition_system
Environment="PATH=/home/<你的用户名>/pd_recognition_system/svm_venv/bin"
ExecStart=/home/<你的用户名>/pd_recognition_system/svm_venv/bin/python svm_fastapi.py

[Install]
WantedBy=multi-user.target

注意:替换 <你的用户名><你的用户组> 为实际值。可以通过 whoamigroups 命令获取。

实测例子-以香橙派开发版安装ubuntu系统的系统服务配置如下(已测试成功):

我是root(管理员)运行方式。

添加内容如下:

复制代码
[Unit]
Description=PD Recognition System API Service
After=network.target

[Service]
User=root
Group=root
WorkingDirectory=/root/pd_recognition_system
Environment="PATH=/root/pd_recognition_system/svm_venv/bin"
ExecStart=/root/pd_recognition_system/svm_venv/bin/python svm_fastapi.py

[Install]
WantedBy=multi-user.target

启用和运行服务:

bash 复制代码
sudo systemctl daemon-reload
sudo systemctl enable pd-recognition.service
sudo systemctl start pd-recognition.service

检查服务状态:

bash 复制代码
sudo systemctl status pd-recognition.service

8. 配置防火墙(如需对外提供服务)

bash 复制代码
# 允许9000端口通过防火墙
sudo ufw allow 9000/tcp
sudo ufw status

API测试

使用curl测试API

bash 复制代码
curl -X POST -F "file=@./test_dataset/corona/corona111.png" http://localhost:9000/api/v1/predict

使用Python脚本测试

创建测试脚本 test_api.py

python 复制代码
import requests

url = 'http://localhost:9000/api/v1/predict'
file_path = './test_dataset/surface/surface57.png'  # 替换为实际图像路径
files = {'file': open(file_path, 'rb')}

response = requests.post(url, files=files)
print(response.json())

运行测试:

bash 复制代码
python test_api.py

系统维护

查看日志

bash 复制代码
sudo journalctl -u pd-recognition.service

重启服务

bash 复制代码
sudo systemctl restart pd-recognition.service

停止服务

bash 复制代码
sudo systemctl stop pd-recognition.service

可能遇到的问题及解决方案

1. 依赖安装失败

问题:安装某些Python依赖包时出错。

解决方案:

bash 复制代码
# 安装Python开发包
sudo apt install -y python3-dev

# 安装编译工具
sudo apt install -y build-essential

2. OpenCV导入错误

问题:导入OpenCV库时出现错误。

解决方案:

bash 复制代码
# 安装OpenCV系统依赖
sudo apt install -y libsm6 libxext6 libxrender-dev libgl1-mesa-glx

3. 服务无法访问

问题:无法从外部访问API服务。

解决方案:

  1. 检查防火墙设置
bash 复制代码
sudo ufw status
sudo ufw allow 9000/tcp
  1. 确保FastAPI服务绑定到0.0.0.0而不是127.0.0.1
  2. 检查云服务提供商的安全组/网络ACL设置

4. 内存不足

问题:服务运行一段时间后崩溃,日志显示内存不足。

解决方案:

  1. 增加虚拟机内存
  2. 配置swap空间
bash 复制代码
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

性能优化建议

  1. 考虑使用Gunicorn作为WSGI服务器与Uvicorn配合使用,提高并发处理能力
bash 复制代码
pip install gunicorn
gunicorn -w 4 -k uvicorn.workers.UvicornWorker app:app
  1. 使用Nginx作为反向代理,提供更好的负载均衡和安全性
bash 复制代码
sudo apt install -y nginx

配置Nginx:

复制代码
server {
    listen 80;
    server_name your_domain.com;

    location / {
        proxy_pass http://127.0.0.1:9000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
相关推荐
Johny_Zhao2 小时前
Docker + CentOS 部署 Zookeeper 集群 + Kubernetes Operator 自动化运维方案
linux·网络安全·docker·信息安全·zookeeper·kubernetes·云计算·系统运维
小毛驴8502 小时前
Linux 后台启动java jar 程序 nohup java -jar
java·linux·jar
DKPT3 小时前
Java桥接模式实现方式与测试方法
java·笔记·学习·设计模式·桥接模式
一心0923 小时前
ubuntu 20.04.6 sudo 源码包在线升级到1.9.17p1
运维·ubuntu·sudo·漏洞升级
好好学习啊天天向上3 小时前
世上最全:ubuntu 上及天河超算上源码编译llvm遇到的坑,cmake,ninja完整过程
linux·运维·ubuntu·自动性能优化
tan180°4 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
典学长编程5 小时前
Linux操作系统从入门到精通!第二天(命令行)
linux·运维·chrome
wuk9985 小时前
基于MATLAB编制的锂离子电池伪二维模型
linux·windows·github
好好研究5 小时前
学习栈和队列的插入和删除操作
数据结构·学习
新中地GIS开发老师6 小时前
新发布:26考研院校和专业大纲
学习·考研·arcgis·大学生·遥感·gis开发·地理信息科学