Last active
May 21, 2019 13:24
-
-
Save lockdef/896ee49baf970251271a5316f53878c0 to your computer and use it in GitHub Desktop.
リアルタイムでグラフを描写する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from io import BytesIO | |
from PIL import Image | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import cv2 | |
cap = cv2.VideoCapture(0) | |
ret, frame = cap.read() | |
size = 0.4 | |
h1 = int(frame.shape[0]*size) | |
h2 = int(frame.shape[0]*size*2) | |
w1 = int(frame.shape[1]*size) | |
w2 = int(frame.shape[1]*size*2) | |
x = np.arange(0, 10, 0.1) | |
def random_img(): | |
# | |
# random_img | |
# ランダムなグラフの画像データを返します | |
# Returns random graph image data. | |
# | |
byte = BytesIO() | |
y = np.random.randn(100) | |
plt.plot(x, y, c='r') | |
plt.savefig(byte) | |
img = np.array(Image.open(byte)) | |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) | |
img = cv2.resize(img, dsize=(w1, h1)) | |
byte.close() | |
plt.close() | |
return img | |
while True: | |
ret, frame = cap.read() | |
img = random_img() | |
img_comb = cv2.addWeighted(frame[h1:h2, w1:w2], 1, img, 1, 0) | |
frame[h1:h2, w1:w2] = img_comb | |
cv2.imshow('camera', frame) | |
if cv2.waitKey(60) == 27: | |
break | |
cap.release() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment