Skip to content

Instantly share code, notes, and snippets.

@tik0
Last active March 26, 2019 21:42
Show Gist options
  • Save tik0/a954ad8d85b6fd8b516e539cdfb211d7 to your computer and use it in GitHub Desktop.
Save tik0/a954ad8d85b6fd8b516e539cdfb211d7 to your computer and use it in GitHub Desktop.
matplotlib vs vispy for plotting a sine wave
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
import numpy as np
import cv2
import matplotlib
import matplotlib.pyplot as plt
cap = cv2.VideoCapture(0)
# prepare the image
fig, ax = plt.subplots(1,1)
gui_live_image = ax.imshow(np.zeros(shape=(480,640)))
ax.set_title("Live Image")
ax.axis("off")
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
try:
gui_live_image.set_data(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
fig.canvas.flush_events()
plt.show(block=False)
except:
break
# When everything done, release the capture
cap.release()
import numpy as np
import cv2
from vispy import plot as vp
from vispy import scene
from vispy import app
fig = vp.Fig(size=(640, 480), show=True)
# Image visual
view = fig[0,0].add_view()
image = scene.visuals.Image(np.zeros(shape=(480,640)), parent=view.scene)
# Set 2D camera (the camera will scale to the contents in the scene)
view.camera = scene.PanZoomCamera(aspect=1)
view.camera.set_range()
view.camera.flip = (0, 1, 0)
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Display the resulting frame
try:
image.set_data(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
image.update()
app.process_events()
except:
break
# When everything done, release the capture
cap.release()
import numpy as np
import time
from vispy import plot as vp
import sys
from vispy import scene
from vispy import app
from vispy.io import load_data_file # loads data from https://github.com/vispy/demo-data/
from vispy.io import read_png
import cv2
fig = vp.Fig(size=(600, 500), show=True)
# Plot the target square wave shape
x = np.linspace(0, 2*np.pi, num=100, endpoint=False)
y = np.sin(x)
line = fig[0, 0].plot((x, y), width=4, color='k',
title='sine wave', xlabel='time (s)',
ylabel='amplitude')
labelgrid = fig[0, 0].view.add_grid(margin=10)
bottom_spacer = vp.Widget()
labelgrid.add_widget(bottom_spacer, row=1, col=0)
grid = vp.visuals.GridLines(color=(0, 0, 0, 0.5))
grid.set_gl_state('translucent')
fig[0, 0].view.add(grid)
## mage
# Set up a viewbox to display the image with interactive pan/zoom
view_1 = fig[1,0].add_view()
view_2 = fig[1,1].add_view()
view_cam = fig[0,1].add_view()
# Load the image
img_data_1 = read_png(load_data_file('lena/lena.png'))
img_data_2 = read_png(load_data_file('mona_lisa/mona_lisa_sm.png'))
# Image visual
image_1 = scene.visuals.Image(img_data_1, parent=view_1.scene)
image_2 = scene.visuals.Image(img_data_2, parent=view_2.scene)
image_cam = scene.visuals.Image(np.zeros(shape=(480,640)), parent=view_cam.scene)
# Set 2D camera (the camera will scale to the contents in the scene)
view_1.camera = scene.PanZoomCamera(aspect=1)
view_1.camera.set_range()
view_1.camera.flip = (0, 1, 0)
view_2.camera = scene.PanZoomCamera(aspect=1)
view_2.camera.set_range()
view_2.camera.flip = (0, 1, 0)
view_cam.camera = scene.PanZoomCamera(aspect=1)
view_cam.camera.set_range()
view_cam.camera.flip = (0, 1, 0)
if __name__ == '__main__':
cap = cv2.VideoCapture(0)
while True:
y = np.roll(y,1)
line.set_data((x, y))
# Capture frame-by-frame
ret, frame = cap.read()
# Display the resulting frame
try:
image_cam.set_data(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
image_cam.update() # update the camera image
app.process_events() # redraw the camera image and line plot
except:
break
# When everything done, release the capture
cap.release()
import sys
from vispy import scene
from vispy import app
import numpy as np
from vispy.io import load_data_file, read_png
canvas = scene.SceneCanvas(keys='interactive')
canvas.size = 800, 600
canvas.show()
# Set up a viewbox to display the image with interactive pan/zoom
view = canvas.central_widget.add_view()
# Define a function to tranform a picture to gray
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])
# Load the image
img_data = read_png(load_data_file('mona_lisa/mona_lisa_sm.png'))
# Apply transformation
img_data = rgb2gray(img_data)
# Image visual
image = scene.visuals.Image(img_data, cmap='grays', parent=view.scene)
# Set 2D camera (the camera will scale to the contents in the scene)
view.camera = scene.PanZoomCamera(aspect=1)
view.camera.set_range()
view.camera.flip = (0, 1, 0)
if __name__ == '__main__' and sys.flags.interactive == 0:
app.run()
import numpy as np
import time
import matplotlib
import matplotlib.pyplot as plt
# Plot the target square wave shape
x = np.linspace(0, 2*np.pi, num=1000, endpoint=False)
y = np.sin(x)
fig, ax = plt.subplots(1,1, figsize=(6,5), dpi=100)
ax.set_title("sine wave")
line, = ax.plot(x, y, c='k') # width 4
ax.set_xlabel("time (s)")
ax.set_ylabel("amplitude")
if __name__ == '__main__':
while True:
y = np.roll(y,1)
line.set_ydata(y)
fig.canvas.flush_events()
plt.show(block=False)
time.sleep(0.01)
import numpy as np
import time
from vispy import plot as vp
from vispy import app as app
fig = vp.Fig(size=(600, 500), show=True)
# Plot the target square wave shape
x = np.linspace(0, 2*np.pi, num=1000, endpoint=False)
y = np.sin(x)
line = fig[0, 0].plot((x, y), width=4, color='k',
title='sine wave', xlabel='time (s)',
ylabel='amplitude')
labelgrid = fig[0, 0].view.add_grid(margin=10)
bottom_spacer = vp.Widget()
labelgrid.add_widget(bottom_spacer, row=1, col=0)
grid = vp.visuals.GridLines(color=(0, 0, 0, 0.5))
grid.set_gl_state('translucent')
fig[0, 0].view.add(grid)
if __name__ == '__main__':
while True:
y = np.roll(y,1)
line.set_data((x, y))
app.process_events()
time.sleep(0.01)
import numpy as np
import time
from vispy import plot as vp
import sys
from vispy import scene
from vispy import app
from vispy.io import load_data_file # loads data from https://github.com/vispy/demo-data/
from vispy.io import read_png
fig = vp.Fig(size=(600, 500), show=True)
## Line
# Plot the target square wave shape
x = np.linspace(0, 2*np.pi, num=100, endpoint=False)
y = np.sin(x)
line = fig[0, 0].plot((x, y), width=4, color='k',
title='sine wave', xlabel='time (s)',
ylabel='amplitude')
labelgrid = fig[0, 0].view.add_grid(margin=10)
bottom_spacer = vp.Widget()
labelgrid.add_widget(bottom_spacer, row=1, col=0)
grid = vp.visuals.GridLines(color=(0, 0, 0, 0.5))
grid.set_gl_state('translucent')
fig[0, 0].view.add(grid)
## Image
# Set up a viewbox to display the image with interactive pan/zoom
view = fig[1:3,0].add_view()
# Load the image
img_data_1 = read_png(load_data_file('lena/lena.png'))
img_data_2 = read_png(load_data_file('mona_lisa/mona_lisa_sm.png'))
# Image visual
image = scene.visuals.Image(img_data_1, parent=view.scene)
# Set 2D camera (the camera will scale to the contents in the scene)
view.camera = scene.PanZoomCamera(aspect=1)
view.camera.set_range()
view.camera.flip = (0, 1, 0)
if __name__ == '__main__':
idx = 0
while True:
y = np.roll(y,1)
line.set_data((x, y))
if idx%2:
image.set_data(img_data_1)
else:
image.set_data(img_data_2)
app.process_events()
time.sleep(0.1)
idx += 1
import numpy as np
import time
from vispy import plot as vp
import sys
from vispy import scene
from vispy import app
from vispy.io import load_data_file # loads data from https://github.com/vispy/demo-data/
from vispy.io import read_png
import cv2
fig = vp.Fig(size=(600, 500), show=True)
# Plot the target square wave shape
x = np.linspace(0, 2*np.pi, num=100, endpoint=False)
y = np.sin(x)
line = fig[0, 0].plot((x, y), width=4, color='k',
title='sine wave', xlabel='time (s)',
ylabel='amplitude')
labelgrid = fig[0, 0].view.add_grid(margin=10)
bottom_spacer = vp.Widget()
labelgrid.add_widget(bottom_spacer, row=1, col=0)
grid = vp.visuals.GridLines(color=(0, 0, 0, 0.5))
grid.set_gl_state('translucent')
fig[0, 0].view.add(grid)
## mage
# Set up a viewbox to display the image with interactive pan/zoom
view_1 = fig[1,0].add_view()
view_2 = fig[1,1].add_view()
view_cam = fig[0,1].add_view()
# Load the image
img_data_1 = read_png(load_data_file('lena/lena.png'))
img_data_2 = read_png(load_data_file('mona_lisa/mona_lisa_sm.png'))
# Image visual
image_1 = scene.visuals.Image(img_data_1, parent=view_1.scene)
image_2 = scene.visuals.Image(img_data_2, parent=view_2.scene)
image_cam = scene.visuals.Image(np.zeros(shape=(480,640)), parent=view_cam.scene)
# Set 2D camera (the camera will scale to the contents in the scene)
view_1.camera = scene.PanZoomCamera(aspect=1)
view_1.camera.set_range()
view_1.camera.flip = (0, 1, 0)
view_2.camera = scene.PanZoomCamera(aspect=1)
view_2.camera.set_range()
view_2.camera.flip = (0, 1, 0)
view_cam.camera = scene.PanZoomCamera(aspect=1)
view_cam.camera.set_range()
view_cam.camera.flip = (0, 1, 0)
if __name__ == '__main__':
cap = cv2.VideoCapture(0)
while True:
y = np.roll(y,1)
line.set_data((x, y))
# Capture frame-by-frame
ret, frame = cap.read()
# Display the resulting frame
try:
image_cam.set_data(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
image_cam.update()
app.process_events()
except:
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
  • Install manual: http://api.vispy.org/en/latest/installation.html
    • apt-get install python3-pyqt4 python3-pyqt4.qtopengl
    • python3 -m pip install vispy pytest-cov nose flake8 cython
  • Run the test in a git repo of vispy
    • >>> import vispy
    • >>> vispy.test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment