It seems that png is quite slow. (Refer to the reference site)
from io import BytesIO
import os
import IPython
from PIL import Image
import cv2
#Convert image to jpeg binary and display with Ipython
def show(a, fmt='jpeg'):
f = BytesIO()
Image.fromarray(a).save(f, fmt) # (3)
IPython.display.display(IPython.display.Image(data=f.getvalue())) # (4)
cap = cv2.VideoCapture(0)
assert cap.isOpened(), 'Could not open video device'
try:
while(True):
ret, frame = cap.read() # (1)
if ret:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # (2)
show(frame) # (3-4)
IPython.display.clear_output(wait=True)
except KeyboardInterrupt:
cap.release()
print('Stream stopped')
Recommended Posts