Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Feb 16, 2024
1 parent 62b280f commit d201164
Show file tree
Hide file tree
Showing 13 changed files with 342 additions and 0 deletions.
184 changes: 184 additions & 0 deletions ref_project/flask-docker-opencv-nginx-main/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
Skip to content
Search or jump to…

Pull requests
Issues
Marketplace
Explore

@AryanSh98
github
/
gitignore
3.2k
108k58k
Code
Pull requests
200
Actions
Projects
Security
Insights
gitignore/Python.gitignore
@pradyunsg
pradyunsg Remove pip-wheel-metadata/ from Python.gitignore (#3364)
Latest commit 14f8a8b on Apr 12
History
83 contributors
@arcresu@shiftkey@Lucretiel@Harrison-G@jwg4@GabrielC101@misaelnieto@pmsosa@svkampen@vltr@thedrow@matheussl
138 lines (112 sloc) 1.99 KB

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/
© 2020 GitHub, Inc.
Terms
Privacy
Security
Status
Help
Contact GitHub
Pricing
API
Training
Blog
About

.vscode/
.idea/
30 changes: 30 additions & 0 deletions ref_project/flask-docker-opencv-nginx-main/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Flask-OpenCV-Docker
Stream IP camera frames in local network using Flask, Docker and OpenCV.

## Overview
this is a simple Flask web app running on top of a Docker container. The app also has Nginx configured which lets users in your local network access the app using your local IP address.

## Requirements
If you want to run the app on docker the only thing you need is Docker-ce and Docker-compose.
follow the official [guide]("https://docs.docker.com/engine/install/ubuntu/") on installing Docker on Ubuntu.

If you only want to run the python program use the following command to install requirements.

```
cd flask/
pip install -r requirements.txt
```
### Run
Flask web app only: cd to `flask` directory and run the following:

```
python run.py
```
Run the whole thing on Docker and Nginx: cd to the project root directory and run:

```
docker-compose up
```
**Note**: Make sure you change the `CAMERA_STREAM_URL` in docker compose file under Flask service. Default is 0 but webcams are not accessible from a Docker container out of the box! So an RTSP url is required.

It might take some time to download required docker images and then it will run the containers and you and other users in the local network can access the page on any device. Just type the local IP address of your machine in the browser and that's it!
18 changes: 18 additions & 0 deletions ref_project/flask-docker-opencv-nginx-main/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: "3.7"

services:

flask:
build: ./flask
container_name: flask
environment:
- APP_NAME=flask-app
- CAMERA_STREAM_URL=0
expose:
- 8080

nginx:
build: ./nginx
container_name: nginx
ports:
- "80:80"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
env/
__pycache__/
16 changes: 16 additions & 0 deletions ref_project/flask-docker-opencv-nginx-main/flask/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Use the Python3.7.2 image
FROM python:3.7.2

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

RUN apt-get update
RUN apt-get install 'libgl1-mesa-dev' -y
# Install the dependencies
RUN pip install -r requirements.txt

# run the command to start uWSGI
CMD ["uwsgi", "app.ini"]
10 changes: 10 additions & 0 deletions ref_project/flask-docker-opencv-nginx-main/flask/app.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[uwsgi]
wsgi-file = run.py
callable = app
socket = :8080
processes = 4
threads = 2
master = true
chmod-socket = 660
vacuum = true
die-on-term = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from flask import Flask

app = Flask(__name__)

from app import views
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>

<head>
<body>
<img style="width: 500px; height: 500px;" src="{{url_for('live_stream')}}">
<!-- <img style="width: 500px; height: 500px;" src="{{url_for('live_stream')}}"> -->
</body>

</html>
37 changes: 37 additions & 0 deletions ref_project/flask-docker-opencv-nginx-main/flask/app/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from app import app
from flask import Flask, render_template, Response
import os
import cv2

# paste camera stream url in quotations ("url") or use 0 to use webcam
cam_url = os.getenv('CAMERA_STREAM_URL', '0')


def process_frame(frame):
# do the image processing here
return frame


@app.route('/')
def home():
return render_template('index.html')


def stream():
cap = cv2.VideoCapture(cam_url)
while True:
cv2.waitKey(1)
success, frame = cap.read()
frame = process_frame(frame)
if success:
_, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')


@app.route('/live_stream/', methods=["GET"])
def live_stream():
"""Video streaming route. Put this in the src attribute of an img tag."""
return Response(stream(),
mimetype='multipart/x-mixed-replace; boundary=frame')
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
click==7.1.2
Flask==1.1.2
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
numpy==1.19.1
opencv-python==4.4.0.42
uWSGI==2.0.19.1
Werkzeug==1.0.1
4 changes: 4 additions & 0 deletions ref_project/flask-docker-opencv-nginx-main/flask/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from app import app

if __name__ == "__main__":
app.run()
7 changes: 7 additions & 0 deletions ref_project/flask-docker-opencv-nginx-main/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM nginx

# Remove the default nginx.conf
RUN rm /etc/nginx/conf.d/default.conf

# Replace with our own nginx.conf
COPY nginx.conf /etc/nginx/conf.d/
10 changes: 10 additions & 0 deletions ref_project/flask-docker-opencv-nginx-main/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
server {

listen 80;

location / {
include uwsgi_params;
uwsgi_pass flask:8080;
}

}

0 comments on commit d201164

Please sign in to comment.