在nodebook上显示摄像头内容,图片的显示就不能用 opencv了 需要用 ipywidgets
import cv2
import ipywidgets
from IPython.display import display
from IPython.display import clear_output as clear
def gstreamer_pipeline(
sensor_id=0,
capture_width=1920,
capture_height=1080,
display_width=960,
display_height=540,
framerate=30,
flip_method=0,
):
return (
"nvarguscamerasrc sensor-id=%d !"
"video/x-raw(memory:NVMM), width=(int)%d, height=(int)%d, framerate=(fraction)%d/1 ! "
"nvvidconv flip-method=%d ! "
"video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
"videoconvert ! "
"video/x-raw, format=(string)BGR ! appsink"
% (
sensor_id,
capture_width,
capture_height,
framerate,
flip_method,
display_width,
display_height,
)
)
def bgr8_to_jpeg(value, quality=75):
return bytes(cv2.imencode('.jpg', value)[1])
clear()
print(gstreamer_pipeline())
RUN = True
video_capture = cv2.VideoCapture(gstreamer_pipeline(), cv2.CAP_GSTREAMER)
if video_capture.isOpened():
try:
image_widget = ipywidgets.Image(format='jpeg')
re, image = video_capture.read()
display(image_widget)
while RUN:
re, image = video_capture.read()
if re:
image_widget.value = bgr8_to_jpeg(image)
finally:
video_capture.release()