Spaces:
Runtime error
Runtime error
Commit ·
eb8f9d9
1
Parent(s): 80446b6
Widgets to display FPS, flags to disable camera.
Browse files- template_styles/cogs.jpg +0 -0
- template_styles/cow.jpg +0 -0
- template_styles/crystals.jpg +0 -0
- template_styles/eyess.jpg +0 -0
- template_styles/fire.jpg +0 -0
- template_styles/gems.jpg +0 -0
- template_styles/ice.jpg +0 -0
- template_styles/leaves.jpeg +0 -0
- template_styles/leaves.png +0 -0
- template_styles/oranges.jpg +0 -0
- template_styles/pattern.jpeg +0 -0
- template_styles/pebbles.jpg +0 -0
- template_styles/penrose.jpg +0 -0
- template_styles/polka.jpeg +0 -0
- template_styles/pumpkin.jpg +0 -0
- template_styles/rainbow.png +0 -0
- template_styles/rainbow_squares.jpg +0 -0
- template_styles/rocks.jpeg +0 -0
- template_styles/screm.jpg +0 -0
- template_styles/spaceship.jpg +0 -0
- template_styles/squares.png +0 -0
- template_styles/swirl.jpg +0 -0
- template_styles/technology.jpg +0 -0
- template_styles/tiles.jpg +0 -0
- template_styles/trees.jpg +0 -0
- template_styles/zigzag.jpg +0 -0
- webcam_stream.py +69 -15
template_styles/cogs.jpg
DELETED
|
Binary file (8.14 kB)
|
|
|
template_styles/cow.jpg
DELETED
|
Binary file (23.6 kB)
|
|
|
template_styles/crystals.jpg
DELETED
|
Binary file (523 kB)
|
|
|
template_styles/eyess.jpg
DELETED
|
Binary file (291 kB)
|
|
|
template_styles/fire.jpg
DELETED
|
Binary file (455 kB)
|
|
|
template_styles/gems.jpg
DELETED
|
Binary file (257 kB)
|
|
|
template_styles/ice.jpg
DELETED
|
Binary file (36.1 kB)
|
|
|
template_styles/leaves.jpeg
DELETED
|
Binary file (27.3 kB)
|
|
|
template_styles/leaves.png
DELETED
|
Binary file (256 kB)
|
|
|
template_styles/oranges.jpg
DELETED
|
Binary file (96.7 kB)
|
|
|
template_styles/pattern.jpeg
DELETED
|
Binary file (9.25 kB)
|
|
|
template_styles/pebbles.jpg
DELETED
|
Binary file (91.2 kB)
|
|
|
template_styles/penrose.jpg
DELETED
|
Binary file (664 kB)
|
|
|
template_styles/polka.jpeg
DELETED
|
Binary file (27.1 kB)
|
|
|
template_styles/pumpkin.jpg
DELETED
|
Binary file (189 kB)
|
|
|
template_styles/rainbow.png
DELETED
|
Binary file (8.45 kB)
|
|
|
template_styles/rainbow_squares.jpg
DELETED
|
Binary file (305 kB)
|
|
|
template_styles/rocks.jpeg
DELETED
|
Binary file (12 kB)
|
|
|
template_styles/screm.jpg
DELETED
|
Binary file (215 kB)
|
|
|
template_styles/spaceship.jpg
DELETED
|
Binary file (112 kB)
|
|
|
template_styles/squares.png
DELETED
|
Binary file (4.18 kB)
|
|
|
template_styles/swirl.jpg
DELETED
|
Binary file (1.17 MB)
|
|
|
template_styles/technology.jpg
DELETED
|
Binary file (67.6 kB)
|
|
|
template_styles/tiles.jpg
DELETED
|
Binary file (474 kB)
|
|
|
template_styles/trees.jpg
DELETED
|
Binary file (13.8 kB)
|
|
|
template_styles/zigzag.jpg
DELETED
|
Binary file (1.23 MB)
|
|
|
webcam_stream.py
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
-
import
|
| 3 |
import numpy as np
|
|
|
|
|
|
|
| 4 |
from threading import Thread
|
| 5 |
|
| 6 |
|
|
@@ -29,30 +32,81 @@ class WebcamVideoStream:
|
|
| 29 |
|
| 30 |
|
| 31 |
def style_transfer_direct(image, style_image, model, resize=None):
|
|
|
|
| 32 |
if resize:
|
| 33 |
image = cv2.resize(np.array(image, dtype=np.float32), (512, 512))
|
| 34 |
else:
|
| 35 |
image = np.array(image, dtype=np.float32)
|
| 36 |
-
|
|
|
|
| 37 |
stylized_image = resp[0]
|
| 38 |
return stylized_image.numpy()[0]
|
| 39 |
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
def main():
|
| 42 |
model = tf.saved_model.load('style/1')
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
import os
|
| 2 |
import cv2
|
| 3 |
+
import time
|
| 4 |
import numpy as np
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import tensorflow as tf
|
| 7 |
from threading import Thread
|
| 8 |
|
| 9 |
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
def style_transfer_direct(image, style_image, model, resize=None):
|
| 35 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 36 |
if resize:
|
| 37 |
image = cv2.resize(np.array(image, dtype=np.float32), (512, 512))
|
| 38 |
else:
|
| 39 |
image = np.array(image, dtype=np.float32)
|
| 40 |
+
image = tf.convert_to_tensor(image[np.newaxis, ...] / 255.)
|
| 41 |
+
resp = model(image, style_image)
|
| 42 |
stylized_image = resp[0]
|
| 43 |
return stylized_image.numpy()[0]
|
| 44 |
|
| 45 |
|
| 46 |
+
def get_style_dictionary():
|
| 47 |
+
style_lookup = {}
|
| 48 |
+
for file in os.listdir('assets/template_styles'):
|
| 49 |
+
with open(os.path.join('assets/template_styles', file), 'rb') as f:
|
| 50 |
+
style = f.read()
|
| 51 |
+
style_image = tf.io.decode_image(style)
|
| 52 |
+
style_image = np.array(style_image[np.newaxis, ...], dtype=np.float32) / 255.
|
| 53 |
+
style_image = tf.image.resize(style_image, (256, 256))
|
| 54 |
+
style_image = tf.convert_to_tensor(style_image)
|
| 55 |
+
style_lookup[file] = style_image
|
| 56 |
+
return style_lookup
|
| 57 |
+
|
| 58 |
+
@st.cache(persist=True)
|
| 59 |
+
def get_custom_style(image_bytes):
|
| 60 |
+
style_image = tf.io.decode_image(image_bytes)
|
| 61 |
+
style_image = np.array(style_image[np.newaxis, ...], dtype=np.float32) / 255.
|
| 62 |
+
style_image = tf.image.resize(style_image, (256, 256))
|
| 63 |
+
style_image = tf.convert_to_tensor(style_image)
|
| 64 |
+
return style_image
|
| 65 |
+
|
| 66 |
+
|
| 67 |
def main():
|
| 68 |
model = tf.saved_model.load('style/1')
|
| 69 |
+
st.title("Neural Style-Transfer Webcam")
|
| 70 |
+
st.subheader('Style Transfer')
|
| 71 |
+
webcam_flag = st.sidebar.checkbox('Enable Webcam', value=False)
|
| 72 |
+
style_flag = st.sidebar.checkbox('Enable Style Transfer', value=False)
|
| 73 |
+
with open('assets/bonk.png', 'rb') as f:
|
| 74 |
+
default_bytes = f.read()
|
| 75 |
+
placeholder_image = st.image(default_bytes)
|
| 76 |
+
|
| 77 |
+
style_dictionary = get_style_dictionary()
|
| 78 |
+
style_options = st.sidebar.selectbox(label='Example Styles', options=list(style_dictionary.keys()))
|
| 79 |
+
custom_style = st.sidebar.file_uploader('Upload Style:', type=['.jpg', '.jpeg', '.png'])
|
| 80 |
+
frame_rate = st.text(f'Frames per second: 0')
|
| 81 |
+
|
| 82 |
+
if webcam_flag:
|
| 83 |
+
video_capture = WebcamVideoStream('http://192.168.0.10:4747/video')
|
| 84 |
+
start = time.time()
|
| 85 |
+
total_frames = 0
|
| 86 |
+
try:
|
| 87 |
+
video_capture.start()
|
| 88 |
+
if custom_style is not None:
|
| 89 |
+
custom_style_bytes = custom_style.getvalue()
|
| 90 |
+
style_image = get_custom_style(custom_style_bytes)
|
| 91 |
+
else:
|
| 92 |
+
style_image = style_dictionary[style_options]
|
| 93 |
+
|
| 94 |
+
st.sidebar.subheader("Style Image:")
|
| 95 |
+
st.sidebar.image(np.array(style_image.numpy()[0] * 255., dtype=np.uint8), use_column_width=True)
|
| 96 |
+
|
| 97 |
+
while webcam_flag:
|
| 98 |
+
content_image = video_capture.read()
|
| 99 |
+
if style_flag:
|
| 100 |
+
transfer = style_transfer_direct(content_image, style_image, model)
|
| 101 |
+
placeholder_image.image(transfer)
|
| 102 |
+
else:
|
| 103 |
+
placeholder_image.image(content_image, channels='BGR')
|
| 104 |
+
total_frames += 1
|
| 105 |
+
end = time.time()
|
| 106 |
+
frame_rate.text(f'Frames per second: {total_frames / (end - start)}')
|
| 107 |
+
finally:
|
| 108 |
+
video_capture.stop()
|
| 109 |
+
del video_capture
|
| 110 |
|
| 111 |
|
| 112 |
if __name__ == "__main__":
|