-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1df0e57
commit b8356d1
Showing
9 changed files
with
492 additions
and
129 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
asyncio | ||
fastapi | ||
uvicorn[standard] | ||
jinja2 | ||
opencv-python | ||
uvicorn[standard] | ||
websockets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,40 @@ | ||
# TrentonBot API | ||
# API v1 | ||
|
||
This API allows for interfacing between the webapp and the vehicle code over ROS 2. | ||
This version of the API uses a local venv to install packages, and only runs locally. | ||
|
||
# Contributing | ||
## Creating a venv | ||
|
||
## Testing Code | ||
|
||
You can test this code locally by running the command: | ||
Use the following command to create your venv: | ||
|
||
``` | ||
bazel run //src/api/v1:main | ||
python3.11 -m venv venv | ||
``` | ||
|
||
This will start up the API using uvicorn, running on port 8000 unless the port is in use. | ||
|
||
If you want to use args with the app, you can run the following: | ||
From here, you can source your venv with the following commands. | ||
Windows, probably: | ||
|
||
``` | ||
bazel run //src/api/v1:main -- <ARGS> | ||
./venv/bin/Activate.ps1 | ||
``` | ||
|
||
The extra `--` allows us to skip over the _bazel_ arguments, and start passing arguments directly to our process. | ||
|
||
**NOTE**: Right now, certain args such as `--reload` don't work with bazel, as built files are copied into a `bazel-out` directory instead of using the files directly in the repo. This means, when running with `--reload`, uvicorn watches the foles in the cached build directory and NOT the files that you'd be editing in the repo. Looking for a workaround in this. | ||
|
||
## Building a Docker Image Locally | ||
|
||
The docker image can be built and published locally using the command: | ||
Linux: | ||
|
||
``` | ||
bazel run //src/api/v1:tarball | ||
. venv/bin/activate | ||
``` | ||
|
||
This will publish the image to your local docker images based on the provided tag: | ||
## Installing needed packages | ||
|
||
You can install packages required for the application using the command: | ||
|
||
``` | ||
INFO: Running command line: bazel-bin/src/api/v1/tarball.sh | ||
f4ba9898d2a2: Loading layer 17.56kB/17.56kB | ||
The image trentonbot_api_v1:latest already exists, renaming the old one with ID sha256:ccb7026cdc6bcb8f6a46a86b8e363581f33e5168f5cab5cde88f77cd8e0539b5 to empty string | ||
Loaded image: trentonbot_api_v1:latest | ||
pip install -r requirements.txt | ||
``` | ||
|
||
In this case, if you want to run the image locally, you can used the loaded image above to run: | ||
## Saving pip requirements | ||
|
||
Run the following command to update the `requirements.txt` file, which is needed whenever you install a new package: | ||
|
||
``` | ||
docker run -p 8000:8000 trentonbot_api_v1:latest | ||
pip freeze > requirements.txt | ||
``` | ||
|
||
This will spin up the docker container locally, and allow you to test from it. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
annotated-types==0.6.0 | ||
anyio==4.3.0 | ||
click==8.1.7 | ||
fastapi==0.110.0 | ||
h11==0.14.0 | ||
httptools==0.6.1 | ||
idna==3.6 | ||
Jinja2==3.1.3 | ||
MarkupSafe==2.1.5 | ||
numpy==1.26.4 | ||
opencv-python==4.9.0.80 | ||
pydantic==2.6.4 | ||
pydantic_core==2.16.3 | ||
python-dotenv==1.0.1 | ||
PyYAML==6.0.1 | ||
sniffio==1.3.1 | ||
starlette==0.36.3 | ||
typing_extensions==4.10.0 | ||
uvicorn==0.29.0 | ||
uvloop==0.19.0 | ||
watchfiles==0.21.0 | ||
websockets==12.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# TrentonBot API | ||
|
||
This API allows for interfacing between the webapp and the vehicle code over ROS 2. | ||
|
||
# Contributing | ||
|
||
## Testing Code | ||
|
||
You can test this code locally by running the command: | ||
|
||
``` | ||
bazel run //src/api/v2:main | ||
``` | ||
|
||
This will start up the API using uvicorn, running on port 8000 unless the port is in use. | ||
|
||
If you want to use args with the app, you can run the following: | ||
|
||
``` | ||
bazel run //src/api/v2:main -- <ARGS> | ||
``` | ||
|
||
The extra `--` allows us to skip over the _bazel_ arguments, and start passing arguments directly to our process. | ||
|
||
**NOTE**: Right now, certain args such as `--reload` don't work with bazel, as built files are copied into a `bazel-out` directory instead of using the files directly in the repo. This means, when running with `--reload`, uvicorn watches the foles in the cached build directory and NOT the files that you'd be editing in the repo. Looking for a workaround in this. | ||
|
||
## Building a Docker Image Locally | ||
|
||
The docker image can be built and published locally using the command: | ||
|
||
``` | ||
bazel run //src/api/v2:tarball | ||
``` | ||
|
||
This will publish the image to your local docker images based on the provided tag: | ||
|
||
``` | ||
INFO: Running command line: bazel-bin/src/api/v2/tarball.sh | ||
f4ba9898d2a2: Loading layer 17.56kB/17.56kB | ||
The image trentonbot_api_v2:latest already exists, renaming the old one with ID sha256:ccb7026cdc6bcb8f6a46a86b8e363581f33e5168f5cab5cde88f77cd8e0539b5 to empty string | ||
Loaded image: trentonbot_api_v2:latest | ||
``` | ||
|
||
In this case, if you want to run the image locally, you can used the loaded image above to run: | ||
|
||
``` | ||
docker run -p 8000:8000 trentonbot_api_v2:latest | ||
``` | ||
|
||
This will spin up the docker container locally, and allow you to test from it. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from typing import Union | ||
from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect | ||
from websockets.exceptions import ConnectionClosed | ||
from fastapi.templating import Jinja2Templates | ||
import asyncio | ||
import cv2 | ||
|
||
app = FastAPI() | ||
camera = cv2.VideoCapture(0, cv2.CAP_DSHOW) | ||
templates = Jinja2Templates(directory="templates") | ||
|
||
|
||
@app.get("/") | ||
def read_root(): | ||
return {"Hello": "World"} | ||
|
||
|
||
@app.get("/items/{item_id}") | ||
def read_item(item_id: int, q: Union[str, None] = None): | ||
return {"item_id": item_id, "q": q} | ||
|
||
|
||
# https://stackoverflow.com/a/70626324 | ||
# Not actually sure if this works with bazel | ||
@app.websocket("/ws") | ||
async def get_stream(websocket: WebSocket): | ||
await websocket.accept() | ||
try: | ||
while True: | ||
success, frame = camera.read() | ||
if not success: | ||
break | ||
else: | ||
ret, buffer = cv2.imencode(".jpg", frame) | ||
await websocket.send_bytes(buffer.tobytes()) | ||
await asyncio.sleep(0.03) | ||
except (WebSocketDisconnect, ConnectionClosed): | ||
print("Client disconnected") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import uvicorn | ||
import sys | ||
|
||
if __name__ == "__main__": | ||
sys.exit(uvicorn.main()) |