Skip to content

Commit 870cf7d

Browse files
committed
Restructured backend with appropriate tests
Signed-off-by: anuunchin <anuun.ch@gmail.com>
1 parent dd48b10 commit 870cf7d

53 files changed

Lines changed: 1512 additions & 844 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ jobs:
104104
- name: Install dependencies
105105
run: make install-backend
106106

107-
# use py_compile instead of running main.py
108-
# because main.py contains a web server that runs forever
109-
- name: Build (syntax check)
107+
- name: Build (import check)
110108
working-directory: src/backend
111-
run: uv run python -m py_compile main.py
109+
run: |
110+
uv run python -c "import webcam.main"
111+
uv run python -c "import analyzer.main"
112112
113113
# Test stage
114114

.github/workflows/dco.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# SPDX-FileCopyrightText: 2025 robot-visual-perception
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
name: DCO Check
6+
7+
on:
8+
pull_request:
9+
branches: [main]
10+
11+
permissions:
12+
contents: read
13+
pull-requests: read
14+
15+
jobs:
16+
check-signoff:
17+
name: Check commit sign-off
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- name: Get PR commits
22+
id: get_commits
23+
uses: tim-actions/get-pr-commits@v1.1.0
24+
with:
25+
token: ${{ secrets.GITHUB_TOKEN }}
26+
27+
- name: DCO check
28+
uses: tim-actions/dco@v1.1.0
29+
with:
30+
commits: ${{ steps.get_commits.outputs.commits }}

Makefile

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,36 @@ test-frontend:
101101
cd src/frontend && npm test
102102

103103
test-backend:
104-
cd src/backend && uv run pytest
104+
cd src/backend && uv run pytest -s
105105

106-
run-backend-local:
107-
cd src/backend && uv run uvicorn webrtc.server:app --host 0.0.0.0 --port 8000
106+
run-webcam-local:
107+
@echo "Starting webcam service on port 8000..."
108+
cd src/backend && uv run uvicorn webcam.main:app --host 0.0.0.0 --port 8000 --reload
109+
110+
run-analyzer-local:
111+
@echo "Starting analyzer service on port 8001..."
112+
cd src/backend && uv run uvicorn analyzer.main:app --host 0.0.0.0 --port 8001 --reload
113+
114+
run-backend-local: run-webcam-local
115+
@echo "Note: To run analyzer, use 'make run-analyzer-local' in another terminal"
108116

109117
run-frontend-local:
110-
cd src/frontend && VITE_BACKEND_URL=http://localhost:8000 npm run dev
118+
cd src/frontend && VITE_BACKEND_URL=http://localhost:8001 npm run dev
111119

112120
docker-build: docker-build-frontend docker-build-backend
113121

114122
docker-build-frontend:
115-
docker build -t robot-frontend:latest src/frontend
123+
docker build \
124+
--build-arg VITE_BACKEND_URL=http://host.docker.internal:8001 \
125+
-t robot-frontend:latest src/frontend
126+
127+
docker-build-backend: docker-build-webcam docker-build-analyzer
128+
129+
docker-build-webcam:
130+
docker build -f src/backend/Dockerfile.webcam -t robot-webcam:latest src/backend
116131

117-
docker-build-backend:
118-
docker build -t robot-backend:latest src/backend
132+
docker-build-analyzer:
133+
docker build -f src/backend/Dockerfile.analyzer -t robot-analyzer:latest src/backend
119134

120135
docker-run-frontend:
121136
@echo "Starting frontend container..."
@@ -125,17 +140,25 @@ docker-run-frontend:
125140
@open http://localhost:8080 || echo "Please open http://localhost:8080 in your browser"
126141
@echo "To stop: docker stop robot-frontend-dev"
127142

128-
docker-run-backend:
129-
@echo "Starting backend container..."
130-
@docker run -d --rm -p 8000:8000 --name robot-backend-dev robot-backend:latest
143+
docker-run-backend: docker-run-webcam
144+
145+
docker-run-webcam:
146+
@echo "Starting webcam service container..."
147+
@docker run -d --rm -p 8000:8000 --name robot-webcam-dev robot-webcam:latest
148+
@sleep 1
149+
@echo "Webcam service running at http://localhost:8000"
150+
@echo "To stop: docker stop robot-webcam-dev"
151+
152+
docker-run-analyzer:
153+
@echo "Starting analyzer service container..."
154+
@docker run -d --rm -p 8001:8001 --env WEBCAM_OFFER_URL=http://host.docker.internal:8000/offer --name robot-analyzer-dev robot-analyzer:latest
131155
@sleep 1
132-
@echo "Opening browser at http://localhost:8000"
133-
@open http://localhost:8000 || echo "Please open http://localhost:8000 in your browser"
134-
@echo "To stop: docker stop robot-backend-dev"
156+
@echo "Analyzer service running at http://localhost:8001"
157+
@echo "To stop: docker stop robot-analyzer-dev"
135158

136159
docker-stop:
137-
@docker stop robot-frontend-dev robot-backend-dev 2>/dev/null || true
160+
@docker stop robot-frontend-dev robot-webcam-dev robot-analyzer-dev 2>/dev/null || true
138161

139162
docker-clean: docker-stop
140-
@docker rmi robot-frontend:latest robot-backend:latest || true
163+
@docker rmi robot-frontend:latest robot-webcam:latest robot-analyzer:latest || true
141164

README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,20 @@ Minimal real-time object and distance detection via YOLO on a WebRTC video strea
1616
- React + Vite frontend showing the remote stream and detection stats
1717

1818
## Run backend (WebRTC)
19-
Prereqs: Python 3.11+
19+
Prereqs: Python 3.11
2020

21-
1) Install dependencies (Mac)
21+
1) Install dependencies
2222
```
23-
cd src/backend
24-
python3 -m venv venv
25-
source venv/bin/activate
26-
pip3 install -r requirements.txt
23+
make dev
2724
```
2825

2926
2) Start the webcam service
3027
```
31-
uv run uvicorn server:app --host 0.0.0.0 --port 8000
28+
make run-webcam-local
3229
```
3330
3) Start the analyzer service (separate terminal)
3431
```
35-
uv run uvicorn analyzer:app --host 0.0.0.0 --port 8001
32+
make run-analyzer-local
3633
```
3734
The first analyzer start will download `yolov8n.pt` automatically (this will take some time)
3835

@@ -41,21 +38,25 @@ Optional environment variables (more relevant later):
4138
- `CAMERA_HFOV_DEG` (default 60) – horizontal field of view used for distance estimate
4239
- `OBJ_WIDTH_M` (default 0.5) – nominal object width used in distance estimate
4340

41+
> Check `src/backend/common/config.py`.
42+
4443
## Run frontend
45-
Prereqs: Node 18+.
44+
Prereqs: Node 20.
4645

4746
1) Install deps
4847
```
49-
cd src/frontend
50-
npm install
48+
make dev
5149
```
50+
> `make dev` installs both frontend and backend.
5251
5352
2) Start dev server
5453
```
55-
VITE_BACKEND_URL=http://localhost:8001 npm run dev
54+
make run-frontend-local
5655
```
5756
Open the shown URL in your console.
5857

5958
## Notes
6059
- The webcam service mirrors and streams raw frames only; the analyzer handles YOLO inference and overlays.
6160
- Analyzer inference is throttled to ~10 Hz to keep latency low.
61+
62+
> IMPORTANT: Please read the `CONTRIBUTING.md`.

src/backend/Dockerfile

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/backend/Dockerfile.analyzer

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# SPDX-FileCopyrightText: 2025 robot-visual-perception
2+
#
3+
# SPDX-License-Identifier: MIT
4+
FROM python:3.11-slim
5+
6+
WORKDIR /app
7+
8+
# Install system dependencies for opencv
9+
RUN apt-get update && apt-get install -y \
10+
libgl1 \
11+
libglib2.0-0 \
12+
&& rm -rf /var/lib/apt/lists/*
13+
14+
COPY requirements.txt .
15+
RUN pip install --no-cache-dir -r requirements.txt
16+
17+
COPY common/ ./common/
18+
COPY analyzer/ ./analyzer/
19+
COPY models/ ./models/
20+
21+
EXPOSE 8001
22+
23+
CMD ["uvicorn", "analyzer.main:app", "--host", "0.0.0.0", "--port", "8001"]
24+

src/backend/Dockerfile.webcam

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# SPDX-FileCopyrightText: 2025 robot-visual-perception
2+
#
3+
# SPDX-License-Identifier: MIT
4+
FROM python:3.11-slim
5+
6+
WORKDIR /app
7+
8+
# Install system dependencies for opencv
9+
RUN apt-get update && apt-get install -y \
10+
libgl1 \
11+
libglib2.0-0 \
12+
&& rm -rf /var/lib/apt/lists/*
13+
14+
COPY requirements.txt .
15+
RUN pip install --no-cache-dir -r requirements.txt
16+
17+
COPY common/ ./common/
18+
COPY webcam/ ./webcam/
19+
COPY models/ ./models/
20+
21+
EXPOSE 8000
22+
23+
CMD ["uvicorn", "webcam.main:app", "--host", "0.0.0.0", "--port", "8000"]
24+

0 commit comments

Comments
 (0)