Hubch/cad_recognition | DeepWiki
https://github.com/Hubch/cad_recognition
winget install python.python.3.10
python -m venv venv
micromamba activate ./venv
pip install paddleocr==2.9.0
pip install poetry
pip install moviepy==1.0.3
下次要用conda建环境 或者有wsl
pip install numpy==1.26.4 opencv-python-headless==4.9.0.80 -i https://pypi.tuna.tsinghua.edu.cn/simple
npm install -g pnpm
直接用docker了
这python没半小时下不完,关掉都不用再下了
重启终端
pip install 'uvicorn[standard]' # 包含 WebSocket 支持
cd frontend
pnpm install browserslist@latest caniuse-lite@latest
micromamba activate ./venv
cd backend
poetry run uvicorn main:app --reload --port 8001
winget install Docker.DockerDesktop
网速有问题歇了
✅ 方法二:使用国内 Docker 镜像加速器(推荐)
由于网络原因,国内用户经常难以访问官方 Docker Hub。建议你为 Docker 配置一个镜像加速器。
步骤如下:
1. 打开 Docker Desktop 设置
- 点击系统托盘中的 Docker 图标 → 选择 “Settings” → “Docker Engine”
2. 在 JSON 配置中添加镜像加速器地址
修改为类似如下内容(可选阿里云、腾讯云、DaoCloud 等):
json
深色版本
{
"registry-mirrors": [
"https://docker.m.daocloud.io",
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com"
]
}
保存后重启 Docker Desktop。
Looking at your Dockerfile, I can now identify the exact issue causing the build failure .
查看您的 Dockerfile,我现在可以识别出导致构建失败的准确问题。
The Problem 问题
The issue is with the quotes around the pip install command on line 8 . Your current syntax:
问题出在行 8 的 pip install 命令周围的引号上。你当前的语法:
RUN pip install "-r requirements.txt"
The quotes around -r requirements.txt
are causing the shell to interpret this as a single argument with a leading space, which is why the error shows ' requirements.txt'
instead of
-r requirements.txt
周围的引号导致 shell 将其解释为单个带前导空格的参数,这就是为什么错误显示为 ' requirements.txt'
irequirements.txt
.
-r requirements.txt
周围的引号导致 shell 将其解释为单个带前导空格的参数,这就是为什么错误显示为 ' requirements.txt'
而不是 requirements.txt
The Solution 解决方案
Remove the quotes from the pip install command:
删除 pip install com 中的引号
RUN pip install -r requirements.txt
根据之前的对话,你需要在 backend 服务的 Dockerfile 中添加 RUN pip install setuptools
命令 。
具体修改位置
你需要修改 backend/Dockerfile
文件 docker-compose.yml:4-7 。在该文件中,在 RUN poetry install
命令之前添加 RUN pip install setuptools
:
# 在 poetry install 之前添加 RUN pip install setuptools RUN poetry install
为什么需要这个修改
这是因为 Python 3.12 移除了 distutils
模块,而 Poetry 安装的某些依赖包仍然依赖它 。backend 服务使用 Poetry 进行包管理 docker-compose.yml:20 ,所以需要在 Poetry 安装依赖之前先安装 setuptools 来提供 distutils
的替代功能。
这个错误通常发生在 Docker 构建过程中尝试复制包含复杂符号链接或嵌套 node_modules 结构的文件时 。从错误路径可以看出,这涉及到 pnpm 的 node_modules 结构,其中包含了 @ampproject+remapping
和 @jridgewell/gen-mapping
包 .
解决方案
方案 1: 使用 .dockerignore 文件
在 frontend/
目录下创建或修改 .dockerignore
文件,排除 node_modules:
node_modules .pnpm-store
根据之前的对话,你需要在 backend 服务的 Dockerfile 中修改 Poetry 的安装命令 docker-compose.yml:4-7 .
具体修改位置
你需要修改 backend/Dockerfile
文件。将现有的:
RUN pip install "poetry==1.4.1"
改为:
RUN pip install "poetry>=1.6.0"
docker-compose logs -f
python -m pip install torch torchvision torchaudio --index-url https://mirrors.nju.edu.cn/pytorch/whl/cu126
回答
在 Poetry 的 pyproject.toml
中,^
符号表示兼容版本范围(caret requirements)。
^
符号的含义
"^1.0.3"
意味着:
- 允许的版本范围:
>=1.0.3, <2.0.0
- 具体含义:允许安装 1.0.3 或更高版本,但不能超过下一个主版本(2.0.0)
版本更新规则
使用 ^1.0.3
时,Poetry 会自动更新到:
- ✅
1.0.4
,1.1.0
,1.9.9
(补丁和次要版本更新) - ❌
2.0.0
(主版本更新,可能包含破坏性变更)
推荐方案
最可靠的方法是使用环境变量,因为它避免了 Poetry 配置命令的语法问题:
# 在 Dockerfile 开头添加 ENV POETRY_VIRTUALENVS_CREATE=false ENV POETRY_NO_INTERACTION=1 ENV POETRY_CACHE_DIR=/tmp/poetry_cache
然后删除 RUN poetry config virtualenvs.create "false"
这一行。
错误信息显示:IndentationError: expected an indented block after 'for' statement on line 85
,这意味着第85行的 for
循环语句后面缺少正确缩进的代码块。
查看 pipeline.py
文件中的 sliding_window
方法,可以看到问题所在: pipeline.py:84-86
问题根源
在第85行有一个 for y in range(0, image.shape[0], step_size):
循环,但第86行的 for x in range(0, image.shape[1], step_size):
没有正确缩进。
是的,您理解得完全正确。在您的 CAD 识别系统中,一个 Docker Compose 配置文件定义了 3 个独立的子容器(服务)。
Docker Compose 架构
从您的 docker-compose.yml:3-34 可以看到,系统定义了三个独立的服务:
- backend 服务 - FastAPI 后端应用容器
- frontend 服务 - React 前端应用容器
- modelserve 服务 - ML 模型推理服务容器
容器隔离特性
每个容器都有:
- 独立的文件系统:各自的 Dockerfile 和构建上下文
- 独立的网络端口:backend(8001)、frontend(5173)、modelserve(8000)
- 独立的 Python 环境:各自的依赖管理和运行时环境
为什么需要多容器架构
这种设计的优势:
- 服务解耦:前端、后端、AI 模型服务各自独立开发和部署
- 资源隔离:每个服务的依赖冲突不会相互影响
- 可扩展性:可以单独扩展某个服务的实例数量
poetry install 了一整天
FROM python:3.11-slim-bullseye
ENV POETRY_VIRTUALENVS_CREATE=false
ENV POETRY_NO_INTERACTION=1
ENV POETRY_CACHE_DIR=/tmp/poetry_cache
# Install system dependencies
RUN pip install "poetry>=1.7"
RUN pip install setuptools
# 然后设置平台相关环境变量
ENV PIP_PLATFORM=linux_x86_64
ENV PIP_ONLY_BINARY=:all:
# Set work directory
WORKDIR /app
# Copy only requirements to cache them in docker layer
COPY pyproject.toml /app/
RUN poetry add openai --source pypi-main
#RUN poetry source add tsinghua https://pypi.tuna.tsinghua.edu.cn/simple/ --priority=primary
RUN poetry source remove pypi || true
RUN poetry source remove ustc || true
RUN poetry source remove tsinghua || true
RUN poetry source remove pypi-main || true
RUN poetry source add aliyun https://mirrors.aliyun.com/pypi/simple --priority=primary
RUN poetry source add pypi-main https://pypi.org/simple/ --priority=supplemental
RUN poetry source add pytorch-nju https://mirrors.nju.edu.cn/pytorch/whl/cu126 --priority=supplemental
RUN poetry config --list
RUN poetry lock --no-interaction
# Disable the creation of virtual environments
# Install dependencies
RUN poetry install --no-root
# Copy the current directory contents into the container at /app
COPY ./ /app/
根据您的日志输出,系统遇到了严重的依赖库问题,导致backend和modelserve服务都无法正常启动 。让我分析一下具体的错误:
主要错误分析
Backend服务错误
Backend服务因为缺少OpenGL库而启动失败 :
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
这个错误是由于在导入cv2时触发的,cv2需要OpenGL库支持但Docker容器中缺少相关依赖。
Modelserve服务错误
Modelserve服务因为PaddlePaddle库缺少libgomp.so.1而启动失败 pipeline.py:3 :
ImportError: libgomp.so.1: cannot open shared object file: No such file or directory
这个错误发生在导入PaddleOCR时,PaddlePaddle需要OpenMP库支持。
Frontend服务状态
Frontend服务实际上启动成功了,可以通过 http://localhost:5173/
访问 ,只是有一些非关键性的警告。
解决方案
1. 修复Backend的Dockerfile
需要在backend的Dockerfile中添加OpenGL相关依赖:
RUN apt-get update && apt-get install -y \ libgl1-mesa-glx \ libglib2.0-0 \ libsm6 \ libxext6 \ libxrender-dev \ libgomp1
2. 修复Modelserve的Dockerfile
需要在modelserve的Dockerfile中添加OpenMP库:
RUN apt-get update && apt-get install -y \ libgomp1 \ libgl1-mesa-glx
终于见到新的报错了
清空日志:
docker-compose down
docker-compose up -d
错误根本原因
缺失的模型文件
错误发生在 ObjectDetect
类初始化时,具体在 CADMatchingPipeline
构造函数中尝试加载检测模型 cad_detect.py:56 。系统期望找到以下模型文件:
models/cad_detect.onnx
- YOLOv5 检测模型models/cad_hybrid.onnx
- 特征匹配模型models/cad_threshold_student.npz
- 阈值配置models/class_map.json
- 类别映射 cad_detect.py:11-14
# 从 CAD 训练权重生成检测模型
python det_matrices/export_models.py --model yolov5 --weight_path /path/to/cad/weights.pt --onnx_path models/cad_detect.onnx
# 生成匹配模型
python det_matrices/export_models.py --model match --weight_path ./weights/cad_hybrid --onnx_path models/cad_hybrid.onnx
cd det_matrices/yolov5
# 从 CAD 训练权重生成检测模型
python export_models.py --model yolov5 --onnx_path models/cad_detect.onnx
# 生成匹配模型
python det_matrices/export_models.py --model match --onnx_path models/cad_hybrid.onnx
哈人