From aff8f7558d3115983db88b65ae601275cfab7ed6 Mon Sep 17 00:00:00 2001 From: Vader Date: Mon, 1 May 2023 20:11:17 +0200 Subject: [PATCH 1/4] added dockerfile and put URLs in env vars --- README.md | 16 +++++++++++++++- dockerfile | 22 ++++++++++++++++++++++ main.py | 9 ++++----- start_docker.sh | 5 +++++ webapp/src/App.js | 4 ++-- 5 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 dockerfile create mode 100644 start_docker.sh diff --git a/README.md b/README.md index 419a57a..48d5621 100644 --- a/README.md +++ b/README.md @@ -61,4 +61,18 @@ And you'll probably get a warning about the app trying to boot on port `80`. You You'll need to update some paths and IPs in the code.

-## Someone send me proof you got it all running. \ No newline at end of file +## Someone send me proof you got it all running. + +## Docker +It is also possible to install the app into a docker container. +Clone the reporsitory. Navigate to the path and build the docker image. + ``` +docker image build -t babysleepcoach . +``` +Run the container (adjust the ports, URLs and path). +``` +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). + diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..e8ee975 --- /dev/null +++ b/dockerfile @@ -0,0 +1,22 @@ +#Deriving the latest base image +FROM node:latest + +WORKDIR /usr/app/babysleepcoach + +RUN mkdir -p video + +#Copy all files in the container +COPY . . + +# Install required packages +RUN apt-get update +RUN apt-get update && apt-get install python3-pip libgl1 -y +RUN pip3 install --upgrade pip +RUN pip3 install -r requirements.txt + +RUN cd webapp && yarn install + +ENV SLEEP_DATA_PATH=/usr/app/babysleepcoach +ENV VIDEO_PATH=/usr/app/babysleepcoach/video + +CMD ["./start_docker.sh"] \ No newline at end of file diff --git a/main.py b/main.py index 2c39615..388fafb 100644 --- a/main.py +++ b/main.py @@ -471,7 +471,7 @@ def live(self, consumer_q): w = 800 if img.shape[0] > 1080 and img.shape[1] > 1920: # max res 1080p - img = maintain_aspect_ratio_resize(img, width=self.frame_dim[0], height=self.frame_dim[1]) + img = maintain_aspect_ratio_resize(self=self, image=img, width=self.frame_dim[0], height=self.frame_dim[1]) img_to_process = img[y:y+h, x:x+w] @@ -545,10 +545,9 @@ def start_server(): def receive(producer_q): print("Start receiving frames.") - cam_ip = os.environ['CAM_IP'] - cam_pw = os.environ['CAM_PW'] - connect_str = "rtsp://admin:" + cam_pw + "@" + cam_ip - connect_str2 = connect_str + ":554" + "//h264Preview_01_main" # this might be different depending on camera used + + cam_stream_url = os.environ['CAM_STREAM_URL'] + connect_str = cam_stream_url os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'rtsp_transport;tcp' # Use tcp instead of udp if stream is unstable c = cv2.VideoCapture(connect_str) diff --git a/start_docker.sh b/start_docker.sh new file mode 100644 index 0000000..453a5f1 --- /dev/null +++ b/start_docker.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +python3 main.py & +cd /usr/app/babysleepcoach/webapp +yarn start \ No newline at end of file diff --git a/webapp/src/App.js b/webapp/src/App.js index 8b5908b..fe410e3 100644 --- a/webapp/src/App.js +++ b/webapp/src/App.js @@ -133,7 +133,7 @@ const createCrazyCircles = (events, forecast) => { const offset = 50; const img = new Image(); - img.src = "http://this_device_ip:8000//absolute_path_to_project_dir/webapp/public/baby_face_192.png"; + img.src = process.env.REACT_APP_BACKEND_URL + "/webapp/public/baby_face_192.png"; img.addEventListener("load", (e) => { context.save(); context.beginPath(); @@ -371,7 +371,7 @@ const processSleepLogs = async (forecast) => { const file = forecast ? 'sleep_logs_forecasted' : 'sleep_logs'; // Request sleep log data from HTTP server on the device (somewhere on LAN) which is running with sleep tracking service - const sleepLogs = await d3.csv(`http://ip_address_of_device_running_sleep_tracker:8000/path_on_device_to_sleep_log_csv_file/${file}.csv`); + const sleepLogs = await d3.csv(process.env.REACT_APP_BACKEND_URL + `/${file}.csv`); // convert timestamps to date objects sleepLogs.forEach((d) => { From 792eee1fac8da6460d86035f24656c30a1b0e4ca Mon Sep 17 00:00:00 2001 From: Vader Date: Mon, 1 May 2023 20:15:53 +0200 Subject: [PATCH 2/4] added new env var to sample --- .env_sample | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.env_sample b/.env_sample index 3661206..c391add 100644 --- a/.env_sample +++ b/.env_sample @@ -2,4 +2,5 @@ SLEEP_DATA_PATH=/path/to/sleep/data DEBUG=False OWL=False VIDEO_PATH=/path/to/video -HATCH_IP=192.168.HATCH.IP \ No newline at end of file +HATCH_IP=192.168.HATCH.IP +CAM_STREAM_URL=192.168.1.100:554/h264Preview_01_main \ No newline at end of file From 0d3a92180a20392a683d80bce8ad6f80b7ca5958 Mon Sep 17 00:00:00 2001 From: Vader Date: Tue, 16 May 2023 19:49:24 +0200 Subject: [PATCH 3/4] added env file for docker and necessary library --- .env_sample | 2 +- .env_sample_docker | 7 +++++++ .gitignore | 1 + README.md | 13 ++++++++++--- dockerfile | 5 ++--- 5 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 .env_sample_docker diff --git a/.env_sample b/.env_sample index c391add..09b1666 100644 --- a/.env_sample +++ b/.env_sample @@ -3,4 +3,4 @@ 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_main \ No newline at end of file +CAM_STREAM_URL=192.168.1.100:554/h264Preview_01_sub \ No newline at end of file diff --git a/.env_sample_docker b/.env_sample_docker new file mode 100644 index 0000000..718f35f --- /dev/null +++ b/.env_sample_docker @@ -0,0 +1,7 @@ +SLEEP_DATA_PATH=/usr/app/babysleepcoach +DEBUG=False +OWL=False +VIDEO_PATH=/usr/app/babysleepcoach/video +HATCH_IP=192.168.HATCH.IP +CAM_STREAM_URL=192.168.1.100:554/h264Preview_01_sub +REACT_APP_BACKEND_URL=http://10.0.0.2:7081 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 72fd018..6288ecd 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ build !*.ipynb !requirements.txt !.env_sample +!.env_sample_docker diff --git a/README.md b/README.md index 48d5621..dc6e510 100644 --- a/README.md +++ b/README.md @@ -62,17 +62,24 @@ And you'll probably get a warning about the app trying to boot on port `80`. You You'll need to update some paths and IPs in the code.

## Someone send me proof you got it all running. - +

## Docker It is also possible to install the app into a docker container. -Clone the reporsitory. Navigate to the path and build the docker image. +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). diff --git a/dockerfile b/dockerfile index e8ee975..0a74b23 100644 --- a/dockerfile +++ b/dockerfile @@ -1,5 +1,5 @@ #Deriving the latest base image -FROM node:latest +FROM node:slim WORKDIR /usr/app/babysleepcoach @@ -9,8 +9,7 @@ RUN mkdir -p video COPY . . # Install required packages -RUN apt-get update -RUN apt-get update && apt-get install python3-pip libgl1 -y +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 From 8c1547c07ac777a20e323f0ac92ad78a0a43ba06 Mon Sep 17 00:00:00 2001 From: Vader Date: Sat, 10 Jun 2023 12:00:02 +0200 Subject: [PATCH 4/4] added dockerfiles --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 6288ecd..c665612 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ build !requirements.txt !.env_sample !.env_sample_docker +!dockerfile +!start_docker.sh