Skip to content

Commit

Permalink
2.0 overhaul (calebolson123#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
calebolson123 authored Sep 25, 2023
1 parent 120c410 commit 5a238f4
Show file tree
Hide file tree
Showing 24 changed files with 5,105 additions and 2,591 deletions.
14 changes: 9 additions & 5 deletions .env_sample
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
SLEEP_DATA_PATH=/path/to/sleep/data
CAM_IP=<ip_of_your_camera> # ip of the baby camera
CAM_PW=<password> # password for baby camera
APP_DIR=/usr/app/babysleepcoach # location of app root, primarily used for docker
DEBUG=False
OWL=False
VIDEO_PATH=/path/to/video
HATCH_IP=192.168.HATCH.IP
CAM_STREAM_URL=192.168.1.100:554/h264Preview_01_sub
OWL=False # lol
VIDEO_PATH=/usr/app/babysleepcoach/video # for debugging & testing
HATCH_IP=192.168.HATCH.IP # optional
REACT_APP_BACKEND_IP=192.168.0.206:8001 # ip of flask server. This is the ip of "this" machine running the system
REACT_APP_RESOURCE_SERVER_IP=192.168.0.206:8000 # ip of resource server (TODO: align to 1 server). This is the ip of "this" machine running the system
PORT=80 # port web app serves assets from
7 changes: 0 additions & 7 deletions .env_sample_docker

This file was deleted.

63 changes: 17 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,68 +14,39 @@ This work is licensed under a
---
## [As seen on YouTube](https://www.youtube.com/channel/UCzxiOKO3vX1ER_U3Z_eY_yw)

This repo contains code to run the AI Baby Sleep Tracking service as well as a web application which provides the user with analysis and charts based on the recorded sleep data.
This repo contains code to run the The Baby Sleep Coach service + web application which provides sleep analysis of your baby, using only a video feed.

## Pre-requisites

- Docker
- Camera which supports RTSP
- Compute accessible via HTTP requests (I used a Raspberry Pi, but you can use any computer)
- Python 3.10 or lower (3.11 is not supported by MediaPipe [[see](https://github.com/google/mediapipe/issues/1325)])

## Setup
## tl;dr Run it with Docker

There are two components to configure:
1) A sleep tracking python script
2) A web application
Copy the .env_sample template into .env

### Part 1: Sleep Tracking Script
```cp example.env .env```

Install requirements: `pip install -r requirements.txt`
### Configure .env file:

Run: `python main.py`
`CAM_IP` IP of your camera

That's it. Except this is where the fun starts.
`CAM_PW` pw for accessing your camera

Most of the dependencies are self explanatory, the only issue I had was installing [MediaPipe](https://google.github.io/mediapipe/) on Raspbian. I believe I used https://pypi.org/project/mediapipe-rpi4/, but I ran into a number of other issues I won't document here. glhf
`PORT` Port for accessing web app

There are number of environment variables and holes you'll need to fill with info about your environment. Instead of fixing things, I left a lot of comments.
`REACT_APP_BACKEND_IP` IP of your backend/api layer. Likely 192.168.COMPUTER_IP:8001

Alternatively you can `touch .env` and then copy and paste the contents of `.env_sample` into it. Then fill in the blanks.
`REACT_APP_RESOURCE_SERVER_IP` IP of your resource server (runs on launch) Likely 192.168.COMPUTER_IP:8000

The sleep data is written to `sleep_logs.csv`. I primed this file with a few rows as an example. Feel free to remove these and start from scratch.
`HATCH_IP` (optional) IP of your hatch for wake light

### Part 2: The Web App
`VIDEO_PATH` (optional) use to set path to recorded footage for debugging

This one is more straight forward. Just make sure you have [`yarn`](https://yarnpkg.com/getting-started/install).
### Update docker-compose

Execute the following commands:

`cd webapp; yarn install; yarn start;`

And you'll probably get a warning about the app trying to boot on port `80`. You can change it to whatever you want in the package.json.

You'll need to update some paths and IPs in the code.
<br/><br/>
## Someone send me proof you got it all running.

<br/><br/>
## Docker
It is also possible to install the app into a docker container.
Clone the repository. Navigate to the path and build the docker image.
```
docker image build -t babysleepcoach .
```
Run the container (adjust the ports, URLs and path).

With an environment file (for the .env file see env_sample_docker):
```
docker run -d -p7080:80 -p7081:8000 --env-file .env --name babysleepcoach babysleepcoach
```

Or with the parameters direct via command line:
```
docker run -d -p7080:80 -p7081:8000 -e REACT_APP_BACKEND_URL=URL_OF_THE_DOCKER_CONTAINER:7081 -e CAM_STREAM_URL=rtsp://user:password@URL_OF_CAMERA:554/stream1 -e DEBUG=False -e OWL=False -e HATCH_IP=127.0.0.1 -v /PATH_TO_SLEEP_LOGS_ON_HOST/sleep_logs.csv:/usr/app/babysleepcoach/sleep_logs.csv --name babysleepcoach babysleepcoach
```

The frontend can be accessed via http://URL_OF_THE_DOCKER_CONTAINER:7080 (or the port you wrote in the run command).
Update the path in `docker-compose.yml` to be the root of this project on your machine.

### Run it
`docker compose up`
12 changes: 5 additions & 7 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
#Deriving the latest base image
FROM node:slim

WORKDIR /usr/app/babysleepcoach

RUN mkdir -p video
EXPOSE 80

#Copy all files in the container
COPY . .

# Install required packages
ENV PIP_BREAK_SYSTEM_PACKAGES 1
RUN apt-get update && apt-get install python3-pip libgl1 libglib2.0-0 -y
RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt

RUN cd webapp && yarn install
RUN cd webapp && yarn install && cd ..

ENV SLEEP_DATA_PATH=/usr/app/babysleepcoach
ENV VIDEO_PATH=/usr/app/babysleepcoach/video
WORKDIR /usr/app/babysleepcoach

CMD ["./start_docker.sh"]
ENTRYPOINT ["bash", "start_docker.sh"]
4 changes: 2 additions & 2 deletions helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def maintain_aspect_ratio_resize(image, width=None, height=None, inter=cv2.INTER

# Return original image if no need to resize
if width is None and height is None:
return image
return image, None

# We are resizing height if width is none
if width is None:
Expand All @@ -154,7 +154,7 @@ def maintain_aspect_ratio_resize(image, width=None, height=None, inter=cv2.INTER
dim = (width, int(h * r))

# Return the resized image
return cv2.resize(image, dim, interpolation=inter)
return cv2.resize(image, dim, interpolation=inter), dim


def gamma_correction(og, gamma):
Expand Down
Loading

0 comments on commit 5a238f4

Please sign in to comment.