Spaces:
Runtime error
Runtime error
Commit
•
1bf7a1c
0
Parent(s):
Duplicate from MarcSkovMadsen/awesome-panel
Browse filesCo-authored-by: Marc Skov Madsen <MarcSkovMadsen@users.noreply.huggingface.co>
- .gitattributes +28 -0
- .gitignore +160 -0
- README.md +20 -0
- app.py +39 -0
- pages/index.py +22 -0
- pages/videostream.py +116 -0
- pages/videostream_utils.py +305 -0
- requirements.txt +3 -0
.gitattributes
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bin.* filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
20 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
26 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Distribution / packaging
|
10 |
+
.Python
|
11 |
+
build/
|
12 |
+
develop-eggs/
|
13 |
+
dist/
|
14 |
+
downloads/
|
15 |
+
eggs/
|
16 |
+
.eggs/
|
17 |
+
lib/
|
18 |
+
lib64/
|
19 |
+
parts/
|
20 |
+
sdist/
|
21 |
+
var/
|
22 |
+
wheels/
|
23 |
+
share/python-wheels/
|
24 |
+
*.egg-info/
|
25 |
+
.installed.cfg
|
26 |
+
*.egg
|
27 |
+
MANIFEST
|
28 |
+
|
29 |
+
# PyInstaller
|
30 |
+
# Usually these files are written by a python script from a template
|
31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
32 |
+
*.manifest
|
33 |
+
*.spec
|
34 |
+
|
35 |
+
# Installer logs
|
36 |
+
pip-log.txt
|
37 |
+
pip-delete-this-directory.txt
|
38 |
+
|
39 |
+
# Unit test / coverage reports
|
40 |
+
htmlcov/
|
41 |
+
.tox/
|
42 |
+
.nox/
|
43 |
+
.coverage
|
44 |
+
.coverage.*
|
45 |
+
.cache
|
46 |
+
nosetests.xml
|
47 |
+
coverage.xml
|
48 |
+
*.cover
|
49 |
+
*.py,cover
|
50 |
+
.hypothesis/
|
51 |
+
.pytest_cache/
|
52 |
+
cover/
|
53 |
+
|
54 |
+
# Translations
|
55 |
+
*.mo
|
56 |
+
*.pot
|
57 |
+
|
58 |
+
# Django stuff:
|
59 |
+
*.log
|
60 |
+
local_settings.py
|
61 |
+
db.sqlite3
|
62 |
+
db.sqlite3-journal
|
63 |
+
|
64 |
+
# Flask stuff:
|
65 |
+
instance/
|
66 |
+
.webassets-cache
|
67 |
+
|
68 |
+
# Scrapy stuff:
|
69 |
+
.scrapy
|
70 |
+
|
71 |
+
# Sphinx documentation
|
72 |
+
docs/_build/
|
73 |
+
|
74 |
+
# PyBuilder
|
75 |
+
.pybuilder/
|
76 |
+
target/
|
77 |
+
|
78 |
+
# Jupyter Notebook
|
79 |
+
.ipynb_checkpoints
|
80 |
+
|
81 |
+
# IPython
|
82 |
+
profile_default/
|
83 |
+
ipython_config.py
|
84 |
+
|
85 |
+
# pyenv
|
86 |
+
# For a library or package, you might want to ignore these files since the code is
|
87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
88 |
+
# .python-version
|
89 |
+
|
90 |
+
# pipenv
|
91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
94 |
+
# install all needed dependencies.
|
95 |
+
#Pipfile.lock
|
96 |
+
|
97 |
+
# poetry
|
98 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
100 |
+
# commonly ignored for libraries.
|
101 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
102 |
+
#poetry.lock
|
103 |
+
|
104 |
+
# pdm
|
105 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
106 |
+
#pdm.lock
|
107 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
108 |
+
# in version control.
|
109 |
+
# https://pdm.fming.dev/#use-with-ide
|
110 |
+
.pdm.toml
|
111 |
+
|
112 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
113 |
+
__pypackages__/
|
114 |
+
|
115 |
+
# Celery stuff
|
116 |
+
celerybeat-schedule
|
117 |
+
celerybeat.pid
|
118 |
+
|
119 |
+
# SageMath parsed files
|
120 |
+
*.sage.py
|
121 |
+
|
122 |
+
# Environments
|
123 |
+
.env
|
124 |
+
.venv
|
125 |
+
env/
|
126 |
+
venv/
|
127 |
+
ENV/
|
128 |
+
env.bak/
|
129 |
+
venv.bak/
|
130 |
+
|
131 |
+
# Spyder project settings
|
132 |
+
.spyderproject
|
133 |
+
.spyproject
|
134 |
+
|
135 |
+
# Rope project settings
|
136 |
+
.ropeproject
|
137 |
+
|
138 |
+
# mkdocs documentation
|
139 |
+
/site
|
140 |
+
|
141 |
+
# mypy
|
142 |
+
.mypy_cache/
|
143 |
+
.dmypy.json
|
144 |
+
dmypy.json
|
145 |
+
|
146 |
+
# Pyre type checker
|
147 |
+
.pyre/
|
148 |
+
|
149 |
+
# pytype static type analyzer
|
150 |
+
.pytype/
|
151 |
+
|
152 |
+
# Cython debug symbols
|
153 |
+
cython_debug/
|
154 |
+
|
155 |
+
# PyCharm
|
156 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
157 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
158 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
159 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
160 |
+
#.idea/
|
README.md
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Awesome Panel
|
3 |
+
emoji: 🐠
|
4 |
+
colorFrom: indigo
|
5 |
+
colorTo: pink
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 2.9.4
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: mit
|
11 |
+
duplicated_from: MarcSkovMadsen/awesome-panel
|
12 |
+
---
|
13 |
+
|
14 |
+
# Awesome Panel on Hugging Face Spaces
|
15 |
+
|
16 |
+
Awesome Panel supports [Panel](panel.holoviz.org) and its users. We want to show how awesome the framework is and help push it forward. Now on Hugging Face Spaces 🤗.
|
17 |
+
|
18 |
+
[Panel](panel.holoviz.org) is a very powerful data app framework that enables you to use the tools you know and love.
|
19 |
+
|
20 |
+
You can deploy Panel apps on [Hugging Face](https://huggingface.co/) [Spaces](https://huggingface.co/spaces), if you use the Gradio deployment option and a customized `app.py` file. This repository shows how. For more details check out the [Panel Hugging Face Deployment guide](https://github.com/holoviz/panel/blob/feature/hugging-face-deployment/doc/user_guide/Server_Deployment.md#hugging-face).
|
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""This files enables serving Panel apps on Hugging Face Spaces"""
|
2 |
+
import os
|
3 |
+
from subprocess import Popen
|
4 |
+
|
5 |
+
# CONFIGURE YOUR SETTINGS HERE
|
6 |
+
|
7 |
+
# Space separated list of .py or .ipynb files to serve
|
8 |
+
APPS_TO_SERVE = "pages/index.py pages/videostream.py"
|
9 |
+
# Prefix of the index .py or .ipynb file. Must be in APPS_TO_SERVE too
|
10 |
+
INDEX_PAGE = "index"
|
11 |
+
|
12 |
+
# NORMALLY NO NEED TO CHANGE THE BELOW
|
13 |
+
PORT = os.environ.get("PORT", "7860")
|
14 |
+
ADDRESS = "0.0.0.0"
|
15 |
+
command = [
|
16 |
+
"panel",
|
17 |
+
"serve",
|
18 |
+
*APPS_TO_SERVE.split(" "),
|
19 |
+
"--index",
|
20 |
+
INDEX_PAGE,
|
21 |
+
"--port",
|
22 |
+
PORT,
|
23 |
+
"--address",
|
24 |
+
ADDRESS,
|
25 |
+
"--allow-websocket-origin",
|
26 |
+
"localhost",
|
27 |
+
"--allow-websocket-origin",
|
28 |
+
"*.hf.space",
|
29 |
+
"--allow-websocket-origin",
|
30 |
+
"*.huggingface.co",
|
31 |
+
# "--log-level",
|
32 |
+
# "debug"
|
33 |
+
]
|
34 |
+
if os.name != "nt":
|
35 |
+
command = command + ["--num-procs", "4", "--num-threads", "4"]
|
36 |
+
|
37 |
+
print(" ".join(command))
|
38 |
+
worker = Popen(command)
|
39 |
+
worker.wait()
|
pages/index.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import panel as pn
|
2 |
+
|
3 |
+
pn.extension(sizing_mode="stretch_width")
|
4 |
+
|
5 |
+
INTRO = """
|
6 |
+
# Awesome Panel on Hugging Face Spaces
|
7 |
+
|
8 |
+
<a href="https://awesome-panel.org" target="_blank">Awesome Panel</a> supports the powerful data app framework <a href="https://panel.holoviz.org" target="_blank">Panel</a> and its users. Now on Hugging Face Spaces 🤗.
|
9 |
+
|
10 |
+
You can also deploy Panel apps on Hugging Face Spaces. For details check out the <a href="https://github.com/holoviz/panel/blob/feature/hugging-face-deployment/doc/user_guide/Server_Deployment.md#hugging-face" target="_blank">Panel Hugging Face Deployment guide</a>.
|
11 |
+
|
12 |
+
Now check out some of our example apps by clicking the links below.
|
13 |
+
|
14 |
+
[videostream](videostream), ... more is coming soon 🤗
|
15 |
+
"""
|
16 |
+
|
17 |
+
some_component = pn.panel(INTRO)
|
18 |
+
|
19 |
+
pn.template.FastListTemplate(
|
20 |
+
site="Awesome Panel 🤗", title="Hello Hugging Face World", main=[some_component],
|
21 |
+
favicon="https://sharing.awesome-panel.org/favicon.ico", accent="#fef3c7", header_color="#4b5563"
|
22 |
+
).servable()
|
pages/videostream.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""The VideoStreamInterface provides an easy way to apply transforms to a video stream"""
|
2 |
+
import numpy as np
|
3 |
+
import panel as pn
|
4 |
+
import param
|
5 |
+
import skimage
|
6 |
+
from PIL import Image, ImageFilter
|
7 |
+
from skimage import data, filters
|
8 |
+
from skimage.color.adapt_rgb import adapt_rgb, each_channel
|
9 |
+
from skimage.draw import rectangle
|
10 |
+
from skimage.exposure import rescale_intensity
|
11 |
+
from skimage.feature import Cascade
|
12 |
+
from videostream_utils import PILImageTransform, NumpyImageTransform, VideoStreamInterface
|
13 |
+
|
14 |
+
pn.extension("terminal", sizing_mode="stretch_width")
|
15 |
+
|
16 |
+
ACCENT = "#fef3c7"
|
17 |
+
|
18 |
+
|
19 |
+
class GaussianBlur(PILImageTransform):
|
20 |
+
"""Gaussian Blur
|
21 |
+
|
22 |
+
https://pillow.readthedocs.io/en/stable/reference/ImageFilter.html#PIL.ImageFilter.GaussianBlur
|
23 |
+
"""
|
24 |
+
|
25 |
+
radius = param.Integer(default=2, bounds=(0, 10))
|
26 |
+
|
27 |
+
def transform(self, image: Image):
|
28 |
+
return image.filter(ImageFilter.GaussianBlur(radius=self.radius))
|
29 |
+
|
30 |
+
|
31 |
+
class GrayscaleTransform(NumpyImageTransform):
|
32 |
+
"""GrayScale transform
|
33 |
+
|
34 |
+
https://scikit-image.org/docs/0.15.x/auto_examples/color_exposure/plot_rgb_to_gray.html
|
35 |
+
"""
|
36 |
+
|
37 |
+
def transform(self, image: np.ndarray):
|
38 |
+
grayscale = skimage.color.rgb2gray(image[:, :, :3])
|
39 |
+
return skimage.color.gray2rgb(grayscale)
|
40 |
+
|
41 |
+
|
42 |
+
class SobelTransform(NumpyImageTransform):
|
43 |
+
"""Sobel Transform
|
44 |
+
|
45 |
+
https://scikit-image.org/docs/0.15.x/auto_examples/color_exposure/plot_adapt_rgb.html
|
46 |
+
"""
|
47 |
+
|
48 |
+
def transform(self, image):
|
49 |
+
@adapt_rgb(each_channel)
|
50 |
+
def sobel_each(image):
|
51 |
+
return filters.sobel(image)
|
52 |
+
|
53 |
+
return rescale_intensity(1 - sobel_each(image))
|
54 |
+
|
55 |
+
|
56 |
+
@pn.cache()
|
57 |
+
def get_detector():
|
58 |
+
"""Returns the Cascade detector"""
|
59 |
+
trained_file = data.lbp_frontal_face_cascade_filename()
|
60 |
+
return Cascade(trained_file)
|
61 |
+
|
62 |
+
class FaceDetectionTransform(NumpyImageTransform):
|
63 |
+
"""Face detection using a cascade classifier.
|
64 |
+
|
65 |
+
https://scikit-image.org/docs/0.15.x/auto_examples/applications/plot_face_detection.html
|
66 |
+
"""
|
67 |
+
|
68 |
+
scale_factor = param.Number(1.4, bounds=(1.0, 2.0), step=0.1)
|
69 |
+
step_ratio = param.Integer(1, bounds=(1, 10))
|
70 |
+
size_x = param.Range(default=(60, 322), bounds=(10, 500))
|
71 |
+
size_y = param.Range(default=(60, 322), bounds=(10, 500))
|
72 |
+
|
73 |
+
def transform(self, image):
|
74 |
+
detector = get_detector()
|
75 |
+
detected = detector.detect_multi_scale(
|
76 |
+
img=image,
|
77 |
+
scale_factor=self.scale_factor,
|
78 |
+
step_ratio=self.step_ratio,
|
79 |
+
min_size=(self.size_x[0], self.size_y[0]),
|
80 |
+
max_size=(self.size_x[1], self.size_y[1]),
|
81 |
+
)
|
82 |
+
|
83 |
+
for patch in detected:
|
84 |
+
rrr, ccc = rectangle(
|
85 |
+
start=(patch["r"], patch["c"]),
|
86 |
+
extent=(patch["height"], patch["width"]),
|
87 |
+
shape=image.shape[:2],
|
88 |
+
)
|
89 |
+
image[rrr, ccc, 0] = 200
|
90 |
+
|
91 |
+
return image
|
92 |
+
|
93 |
+
|
94 |
+
component = VideoStreamInterface(
|
95 |
+
transforms=[
|
96 |
+
GaussianBlur,
|
97 |
+
GrayscaleTransform,
|
98 |
+
SobelTransform,
|
99 |
+
FaceDetectionTransform,
|
100 |
+
]
|
101 |
+
)
|
102 |
+
|
103 |
+
component.video_stream.timeout=1000
|
104 |
+
component.video_stream.param.timeout.bounds=(250, 2000)
|
105 |
+
|
106 |
+
pn.template.FastListTemplate(
|
107 |
+
site="Awesome Panel 🤗",
|
108 |
+
title="VideoStream with transforms",
|
109 |
+
sidebar=[component.settings],
|
110 |
+
main=[
|
111 |
+
"""Try a much, much faster version <a href="https://sharing.awesome-panel.org/MarcSkovMadsen/videostream-interface/app.html" target="_blank">here</a> powered by Webassembly.""",
|
112 |
+
component],
|
113 |
+
favicon="https://sharing.awesome-panel.org/favicon.ico",
|
114 |
+
accent=ACCENT,
|
115 |
+
header_color="#4b5563"
|
116 |
+
).servable()
|
pages/videostream_utils.py
ADDED
@@ -0,0 +1,305 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""The VideoStreamInterface provides an easy way to apply transforms to a video stream"""
|
2 |
+
import base64
|
3 |
+
import io
|
4 |
+
import time
|
5 |
+
|
6 |
+
import numpy as np
|
7 |
+
import panel as pn
|
8 |
+
import param
|
9 |
+
import PIL
|
10 |
+
from PIL import Image
|
11 |
+
|
12 |
+
HEIGHT = 400
|
13 |
+
WIDTH = 400
|
14 |
+
TIMEOUT = 250
|
15 |
+
ACCENT = "#fef3c7"
|
16 |
+
|
17 |
+
|
18 |
+
def to_instance(value, **params):
|
19 |
+
"""Converts the value to an instance
|
20 |
+
|
21 |
+
Args:
|
22 |
+
value: A param.Parameterized class or instance
|
23 |
+
|
24 |
+
Returns:
|
25 |
+
An instance of the param.Parameterized class
|
26 |
+
"""
|
27 |
+
if isinstance(value, param.Parameterized):
|
28 |
+
value.param.update(**params)
|
29 |
+
return value
|
30 |
+
return value(**params)
|
31 |
+
|
32 |
+
|
33 |
+
class Timer(pn.viewable.Viewer):
|
34 |
+
"""Helper Component used to show duration trends"""
|
35 |
+
|
36 |
+
_trends = param.Dict()
|
37 |
+
|
38 |
+
def __init__(self, **params):
|
39 |
+
super().__init__()
|
40 |
+
|
41 |
+
self.last_updates = {}
|
42 |
+
self._trends = {}
|
43 |
+
|
44 |
+
self._layout = pn.Row(**params)
|
45 |
+
|
46 |
+
def time_it(self, name, func, *args, **kwargs):
|
47 |
+
"""Measures the duration of the execution of the func function and reports it under the
|
48 |
+
name specified"""
|
49 |
+
start = time.time()
|
50 |
+
result = func(*args, **kwargs)
|
51 |
+
end = time.time()
|
52 |
+
duration = round(end - start, 2)
|
53 |
+
self._report(name=name, duration=duration)
|
54 |
+
return result
|
55 |
+
|
56 |
+
def inc_it(self, name):
|
57 |
+
"""Measures the duration since the last time `inc_it` was called and reports it under the
|
58 |
+
specified name"""
|
59 |
+
start = self.last_updates.get(name, time.time())
|
60 |
+
end = time.time()
|
61 |
+
duration = round(end - start, 2)
|
62 |
+
self._report(name=name, duration=duration)
|
63 |
+
self.last_updates[name] = end
|
64 |
+
|
65 |
+
def _report(self, name, duration):
|
66 |
+
if not name in self._trends:
|
67 |
+
self._trends[name] = pn.indicators.Trend(
|
68 |
+
title=name,
|
69 |
+
data={"x": [1], "y": [duration]},
|
70 |
+
plot_color=ACCENT,
|
71 |
+
height=100,
|
72 |
+
width=150,
|
73 |
+
sizing_mode="fixed",
|
74 |
+
)
|
75 |
+
self.param.trigger("_trends")
|
76 |
+
else:
|
77 |
+
trend = self._trends[name]
|
78 |
+
next_x = max(trend.data["x"]) + 1
|
79 |
+
trend.stream({"x": [next_x], "y": [duration]}, rollover=10)
|
80 |
+
|
81 |
+
@pn.depends("_trends")
|
82 |
+
def _panel(self):
|
83 |
+
self._layout[:] = list(self._trends.values())
|
84 |
+
return self._layout
|
85 |
+
|
86 |
+
def __panel__(self):
|
87 |
+
return self._panel
|
88 |
+
|
89 |
+
|
90 |
+
class ImageTransform(pn.viewable.Viewer):
|
91 |
+
"""Base class for image transforms."""
|
92 |
+
|
93 |
+
def __init__(self, **params):
|
94 |
+
super().__init__(**params)
|
95 |
+
|
96 |
+
with param.edit_constant(self):
|
97 |
+
self.name = self.__class__.name.replace("Transform", "")
|
98 |
+
self.view = self.create_view()
|
99 |
+
|
100 |
+
def __panel__(self):
|
101 |
+
return self.view
|
102 |
+
|
103 |
+
def run(self, image: str, height: int = HEIGHT, width: int = WIDTH) -> str:
|
104 |
+
"""Transforms the base64 encoded jpg image to a base64 encoded jpg BytesIO object"""
|
105 |
+
raise NotImplementedError()
|
106 |
+
|
107 |
+
def create_view(self):
|
108 |
+
"""Creates a view of the parameters of the transform to enable the user to configure them"""
|
109 |
+
return pn.Param(self, name=self.name)
|
110 |
+
|
111 |
+
def transform(self, image):
|
112 |
+
"""Transforms the image"""
|
113 |
+
raise NotImplementedError()
|
114 |
+
|
115 |
+
|
116 |
+
class PILImageTransform(ImageTransform):
|
117 |
+
"""Base class for PIL image transforms"""
|
118 |
+
|
119 |
+
@staticmethod
|
120 |
+
def to_pil_img(value: str, height=HEIGHT, width=WIDTH):
|
121 |
+
"""Converts a base64 jpeg image string to a PIL.Image"""
|
122 |
+
encoded_data = value.split(",")[1]
|
123 |
+
base64_decoded = base64.b64decode(encoded_data)
|
124 |
+
image = Image.open(io.BytesIO(base64_decoded))
|
125 |
+
image.draft("RGB", (height, width))
|
126 |
+
return image
|
127 |
+
|
128 |
+
@staticmethod
|
129 |
+
def from_pil_img(image: Image):
|
130 |
+
"""Converts a PIL.Image to a base64 encoded JPG BytesIO object"""
|
131 |
+
buff = io.BytesIO()
|
132 |
+
image.save(buff, format="JPEG")
|
133 |
+
return buff
|
134 |
+
|
135 |
+
def run(self, image: str, height: int = HEIGHT, width: int = WIDTH) -> io.BytesIO:
|
136 |
+
pil_img = self.to_pil_img(image, height=height, width=width)
|
137 |
+
|
138 |
+
transformed_image = self.transform(pil_img)
|
139 |
+
|
140 |
+
return self.from_pil_img(transformed_image)
|
141 |
+
|
142 |
+
def transform(self, image: PIL.Image) -> PIL.Image:
|
143 |
+
"""Transforms the PIL.Image image"""
|
144 |
+
raise NotImplementedError()
|
145 |
+
|
146 |
+
|
147 |
+
class NumpyImageTransform(ImageTransform):
|
148 |
+
"""Base class for np.ndarray image transforms"""
|
149 |
+
|
150 |
+
@staticmethod
|
151 |
+
def to_np_ndarray(image: str, height=HEIGHT, width=WIDTH) -> np.ndarray:
|
152 |
+
"""Converts a base64 encoded jpeg string to a np.ndarray"""
|
153 |
+
pil_img = PILImageTransform.to_pil_img(image, height=height, width=width)
|
154 |
+
return np.array(pil_img)
|
155 |
+
|
156 |
+
@staticmethod
|
157 |
+
def from_np_ndarray(image: np.ndarray) -> io.BytesIO:
|
158 |
+
"""Converts np.ndarray jpeg image to a jpeg BytesIO instance"""
|
159 |
+
if image.dtype == np.dtype("float64"):
|
160 |
+
image = (image * 255).astype(np.uint8)
|
161 |
+
pil_img = PIL.Image.fromarray(image)
|
162 |
+
return PILImageTransform.from_pil_img(pil_img)
|
163 |
+
|
164 |
+
def run(self, image: str, height: int = HEIGHT, width: int = WIDTH) -> io.BytesIO:
|
165 |
+
np_array = self.to_np_ndarray(image, height=height, width=width)
|
166 |
+
|
167 |
+
transformed_image = self.transform(np_array)
|
168 |
+
|
169 |
+
return self.from_np_ndarray(transformed_image)
|
170 |
+
|
171 |
+
def transform(self, image: np.ndarray) -> np.ndarray:
|
172 |
+
"""Transforms the nd.array image"""
|
173 |
+
raise NotImplementedError()
|
174 |
+
|
175 |
+
|
176 |
+
class VideoStreamInterface(pn.viewable.Viewer):
|
177 |
+
"""An easy to use interface for a VideoStream and a set of transforms"""
|
178 |
+
|
179 |
+
video_stream = param.ClassSelector(
|
180 |
+
class_=pn.widgets.VideoStream, constant=True, doc="The source VideoStream"
|
181 |
+
)
|
182 |
+
|
183 |
+
height = param.Integer(
|
184 |
+
HEIGHT,
|
185 |
+
bounds=(10, 2000),
|
186 |
+
step=10,
|
187 |
+
doc="""The height of the image converted and shown""",
|
188 |
+
)
|
189 |
+
width = param.Integer(
|
190 |
+
WIDTH,
|
191 |
+
bounds=(10, 2000),
|
192 |
+
step=10,
|
193 |
+
doc="""The width of the image converted and shown""",
|
194 |
+
)
|
195 |
+
|
196 |
+
transform = param.Selector(doc="The currently selected transform")
|
197 |
+
|
198 |
+
def __init__(
|
199 |
+
self,
|
200 |
+
transforms,
|
201 |
+
timeout=TIMEOUT,
|
202 |
+
paused=False,
|
203 |
+
**params,
|
204 |
+
):
|
205 |
+
super().__init__(
|
206 |
+
video_stream=pn.widgets.VideoStream(
|
207 |
+
name="Video Stream",
|
208 |
+
timeout=timeout,
|
209 |
+
paused=paused,
|
210 |
+
height=0,
|
211 |
+
width=0,
|
212 |
+
visible=False,
|
213 |
+
format="jpeg",
|
214 |
+
),
|
215 |
+
**params,
|
216 |
+
)
|
217 |
+
self.image = pn.pane.JPG(
|
218 |
+
height=self.height, width=self.width, sizing_mode="fixed"
|
219 |
+
)
|
220 |
+
self._updating = False
|
221 |
+
transforms = [to_instance(transform) for transform in transforms]
|
222 |
+
self.param.transform.objects = transforms
|
223 |
+
self.transform = transforms[0]
|
224 |
+
self.timer = Timer(sizing_mode="stretch_width")
|
225 |
+
self.settings = self._create_settings()
|
226 |
+
self._panel = self._create_panel()
|
227 |
+
|
228 |
+
def _create_settings(self):
|
229 |
+
return pn.Column(
|
230 |
+
pn.Param(
|
231 |
+
self.video_stream,
|
232 |
+
parameters=["timeout", "paused"],
|
233 |
+
widgets={
|
234 |
+
"timeout": {
|
235 |
+
"widget_type": pn.widgets.IntSlider,
|
236 |
+
"start": 10,
|
237 |
+
"end": 2000,
|
238 |
+
"step": 10,
|
239 |
+
}
|
240 |
+
},
|
241 |
+
),
|
242 |
+
self.timer,
|
243 |
+
pn.Param(self, parameters=["height", "width"], name="Image"),
|
244 |
+
pn.Param(
|
245 |
+
self,
|
246 |
+
parameters=["transform"],
|
247 |
+
expand_button=False,
|
248 |
+
expand=False,
|
249 |
+
widgets={
|
250 |
+
"transform": {
|
251 |
+
"widget_type": pn.widgets.RadioButtonGroup,
|
252 |
+
"orientation": "vertical",
|
253 |
+
"button_type": "success",
|
254 |
+
}
|
255 |
+
},
|
256 |
+
name="Transform",
|
257 |
+
),
|
258 |
+
self._get_transform,
|
259 |
+
)
|
260 |
+
|
261 |
+
def _create_panel(self):
|
262 |
+
return pn.Row(
|
263 |
+
self.video_stream,
|
264 |
+
pn.layout.HSpacer(),
|
265 |
+
self.image,
|
266 |
+
pn.layout.HSpacer(),
|
267 |
+
sizing_mode="stretch_width",
|
268 |
+
align="center",
|
269 |
+
)
|
270 |
+
|
271 |
+
@pn.depends("height", "width", watch=True)
|
272 |
+
def _update_height_width(self):
|
273 |
+
self.image.height = self.height
|
274 |
+
self.image.width = self.width
|
275 |
+
|
276 |
+
@pn.depends("transform")
|
277 |
+
def _get_transform(self):
|
278 |
+
# Hack: returning self.transform stops working after browsing the transforms for a while
|
279 |
+
return self.transform.view
|
280 |
+
|
281 |
+
def __panel__(self):
|
282 |
+
return self._panel
|
283 |
+
|
284 |
+
@pn.depends("video_stream.value", watch=True)
|
285 |
+
def _handle_stream(self):
|
286 |
+
if self._updating:
|
287 |
+
return
|
288 |
+
|
289 |
+
self._updating = True
|
290 |
+
if self.transform and self.video_stream.value:
|
291 |
+
value = self.video_stream.value
|
292 |
+
try:
|
293 |
+
image = self.timer.time_it(
|
294 |
+
name="Transform",
|
295 |
+
func=self.transform.run,
|
296 |
+
image=value,
|
297 |
+
height=self.height,
|
298 |
+
width=self.width,
|
299 |
+
)
|
300 |
+
self.image.object = image
|
301 |
+
except PIL.UnidentifiedImageError:
|
302 |
+
print("unidentified image")
|
303 |
+
|
304 |
+
self.timer.inc_it("last update")
|
305 |
+
self._updating = False
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
panel==0.14.1
|
2 |
+
scikit-image
|
3 |
+
Pillow
|