Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ jobs:
fetch-depth: 0 # copy full .git directory to access full git history in Docker images

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
uses: docker/setup-buildx-action@v4

- name: Login to Docker Hub
uses: docker/login-action@v3
Expand Down
13 changes: 10 additions & 3 deletions README_部署说明.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pip install -r requirements.txt
```

或者单独安装:

```bash
pip install opencv-python numpy ultralytics pyyaml
```
Expand All @@ -37,15 +38,15 @@ pip install opencv-python numpy ultralytics pyyaml

```yaml
data_source:
type: "video" # 可选: video, camera, images
type: "video" # 可选: video, camera, images
video_path: "你的视频路径.mp4"
fps: 24

inference:
model_path: "yolov8n.pt"
fps: 24
conf_threshold: 0.3
classes: [0] # 0=人, 1=自行车, 2=汽车 等
classes: [0] # 0=人, 1=自行车, 2=汽车 等
```

### 3. 运行系统
Expand All @@ -63,11 +64,13 @@ inference:
#### 方式二:手动启动(Windows/Linux/Mac)

**终端1(数据源):**

```bash
python data_source_shm.py
```

**终端2(推理):**

```bash
python inference_shm.py
```
Expand Down Expand Up @@ -148,7 +151,8 @@ Get-Content logs/app.log -Wait -Tail 20

**问题:** `无法连接到共享内存`

**解决:**
**解决:**

- 确保程序A(data_source_shm.py)先启动
- 检查两个程序的 `SHM_NAME` 配置是否一致
- 在Linux上,检查 `/dev/shm` 目录权限
Expand All @@ -158,6 +162,7 @@ Get-Content logs/app.log -Wait -Tail 20
**问题:** `无法打开摄像头`

**解决:**

- 检查摄像头是否连接
- 尝试修改 `camera_index`(0, 1, 2...)
- Windows上可能需要安装摄像头驱动
Expand All @@ -167,6 +172,7 @@ Get-Content logs/app.log -Wait -Tail 20
**问题:** `ModuleNotFoundError: No module named 'xxx'`

**解决:**

```bash
pip install -r requirements.txt
```
Expand All @@ -187,6 +193,7 @@ pip install -r requirements.txt
## 📞 需要帮助?

如果遇到问题,请检查:

1. 日志文件(`logs/` 目录)
2. 配置文件是否正确
3. Python依赖是否安装完整
Expand Down
32 changes: 28 additions & 4 deletions WSL2_操作指南.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
### 1. 确认 WSL2 和 Ubuntu 已安装

在 Windows PowerShell 中运行:

```powershell
wsl --list --verbose
```

应该能看到 Ubuntu,并且 VERSION 是 2。

如果没安装,运行:

```powershell
wsl --install -d Ubuntu
```
Expand Down Expand Up @@ -44,6 +46,7 @@ pip3 install ultralytics opencv-python
```

**注意**:如果提示权限问题,可以加 `--user`:

```bash
pip3 install --user ultralytics opencv-python
```
Expand All @@ -55,6 +58,7 @@ pip3 install --user ultralytics opencv-python
### 2.1 在 WSL2 中访问 Windows 文件

你的 Windows 文件在 WSL2 中的路径是:

- `F:\` 盘 → `/mnt/f/`
- 你的项目路径:
```
Expand All @@ -74,12 +78,14 @@ ls -la
```

应该能看到:

- `data_source.py`
- `inference.py`

### 2.4 确认视频文件路径

检查视频文件是否存在:

```bash
ls -la /mnt/f/Deeplearning/yolo_source8.3.163/ultralytics/datasets/make_dataset/videos/
```
Expand All @@ -93,6 +99,7 @@ ls -la /mnt/f/Deeplearning/yolo_source8.3.163/ultralytics/datasets/make_dataset/
### 3.1 打开两个 WSL2 终端窗口

需要两个终端窗口:

1. **终端1**:运行 `inference.py`(程序B - 推理)
2. **终端2**:运行 `data_source.py`(程序A - 数据源)

Expand All @@ -107,6 +114,7 @@ python3 inference.py
```

**预期输出**:

```
============================================================
程序 B:推理进程(负责从 Socket 读取图像并做实时检测)
Expand Down Expand Up @@ -137,6 +145,7 @@ python3 data_source.py
```

**预期输出**:

```
============================================================
程序 A:数据源进程(负责读取视频/图片/摄像头,并发送给程序 B)
Expand All @@ -157,6 +166,7 @@ Unix Domain Socket 路径: /tmp/yolo_image_socket
### 3.4 查看结果

当程序A连接成功后,程序B的窗口会:

1. 显示"已连接程序 A,开始接收图像并推理。"
2. 弹出一个 OpenCV 窗口,显示检测结果(画框标注人体)
3. 在终端中显示"已推理帧数: X"
Expand All @@ -166,9 +176,11 @@ Unix Domain Socket 路径: /tmp/yolo_image_socket
## 🛑 停止程序

### 方法1:关闭显示窗口

在 OpenCV 显示窗口中按键盘 `q` 键,程序B会退出。

### 方法2:使用 Ctrl+C

在任意一个终端中按 `Ctrl + C`,对应的程序会停止。

**建议**:先停止程序A(终端2),再停止程序B(终端1)。
Expand All @@ -182,11 +194,13 @@ Unix Domain Socket 路径: /tmp/yolo_image_socket
**错误**:`ModuleNotFoundError: No module named 'ultralytics'`

**解决**:

```bash
pip3 install --user ultralytics opencv-python
```

如果还不行,检查 Python 版本:

```bash
python3 --version
# 应该是 Python 3.8 或更高版本
Expand All @@ -199,7 +213,9 @@ python3 --version
**错误**:`无法打开视频文件: /mnt/f/...`

**解决**:

1. 检查视频文件是否存在:

```bash
ls -la /mnt/f/Deeplearning/yolo_source8.3.163/ultralytics/datasets/make_dataset/videos/
```
Expand All @@ -215,6 +231,7 @@ python3 --version
**错误**:`Connection refused` 或 `No such file or directory`

**解决**:

1. 确保先运行程序B(inference.py),再运行程序A(data_source.py)
2. 检查 socket 文件:
```bash
Expand Down Expand Up @@ -283,7 +300,7 @@ source ~/.bashrc
```bash
# 测试 X11 是否可用(如果安装了 x11-apps)
sudo apt install -y x11-apps
xeyes # 应该会弹出眼睛窗口
xeyes # 应该会弹出眼睛窗口

# 或者测试 OpenCV
python3 -c "import cv2; cv2.namedWindow('test'); cv2.destroyAllWindows(); print('X11 可用')"
Expand All @@ -294,6 +311,7 @@ python3 -c "import cv2; cv2.namedWindow('test'); cv2.destroyAllWindows(); print(
如果仍然无法连接,需要在 Windows 防火墙中允许 VcXsrv:

在 **Windows PowerShell(管理员)** 中运行:

```powershell
New-NetFirewallRule -DisplayName "VcXsrv X11 Server" -Direction Inbound -Program "C:\Program Files\VcXsrv\vcxsrv.exe" -Action Allow
```
Expand All @@ -312,7 +330,6 @@ export DISPLAY=:0

# 安装必要的库


# 运行程序
python3 inference.py
```
Expand All @@ -322,10 +339,11 @@ python3 inference.py
代码已支持自动检测 X11 可用性。只需设置:

```python
DISPLAY_MODE = 'auto' # 在 inference.py 中
DISPLAY_MODE = "auto" # 在 inference.py 中
```

程序会自动检测:

- 如果 X11 可用 → 显示窗口
- 如果 X11 不可用 → 自动保存到文件

Expand All @@ -338,7 +356,9 @@ DISPLAY_MODE = 'auto' # 在 inference.py 中
**错误**:`FileNotFoundError: yolo12n.pt`

**解决**:

1. 检查模型文件是否存在:

```bash
ls -la /mnt/f/Deeplearning/yolo_source8.3.163/ultralytics/yolo12n.pt
```
Expand All @@ -354,20 +374,23 @@ DISPLAY_MODE = 'auto' # 在 inference.py 中
### 更改数据源类型

编辑 `data_source.py`,修改:

```python
SOURCE_TYPE = "video" # 改为 "camera" 或 "images"
```

### 更改发送/推理频率

在两个文件中修改:

```python
FPS = 10 # 改为其他数字,比如 5(5Hz)或 20(20Hz)
```

### 更改视频路径

编辑 `data_source.py`,修改:

```python
VIDEO_PATH = "/mnt/f/你的/视频/路径.mp4"
```
Expand All @@ -392,13 +415,14 @@ VIDEO_PATH = "/mnt/f/你的/视频/路径.mp4"
## 🔄 下一步

现在你的系统已经可以:

- ✅ 使用 Unix Domain Socket 进行进程间通信
- ✅ 程序A持续发送图像数据
- ✅ 程序B持续接收并推理
- ✅ 实时显示检测结果

接下来可以:

1. 尝试使用摄像头(修改 `SOURCE_TYPE = "camera"`)
2. 尝试使用图片文件夹(修改 `SOURCE_TYPE = "images"`)
3. 接入真实相机(需要配置相机驱动和路径)

18 changes: 9 additions & 9 deletions camera_detect.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from ultralytics import YOLO
import cv2

from ultralytics import YOLO

# 加载训练好的模型
model = YOLO(r'runs\detect\train11\weights\best.pt')
model = YOLO(r"runs\detect\train11\weights\best.pt")

# 打开摄像头(0表示默认摄像头,如果是外接摄像头可以尝试1、2等)
cap = cv2.VideoCapture(0)
Expand All @@ -19,25 +20,24 @@
if not ret:
print("无法读取摄像头画面,请检查摄像头是否连接正常")
break

# 使用模型进行预测
# conf=0.5 表示置信度阈值,只有置信度大于0.5的检测结果才会显示
# 你可以根据需要调整这个值(0.0-1.0之间)
results = model(frame, conf=0.25, imgsz=1280)

# 在画面上绘制检测结果(包括边界框、类别名称、置信度)
annotated_frame = results[0].plot()

# 显示结果
cv2.imshow('摄像头实时检测 - kunkun/fan/xiang', annotated_frame)
cv2.imshow("摄像头实时检测 - kunkun/fan/xiang", annotated_frame)

# 按 'q' 键退出
if cv2.waitKey(1) & 0xFF == ord('q'):
if cv2.waitKey(1) & 0xFF == ord("q"):
print("正在退出...")
break

# 释放资源
cap.release()
cv2.destroyAllWindows()
print("程序已退出")

Loading