Can't open the CSI camera on board via some sample python codes

Sorry, I am not good at English. My device is TX2, Ubuntu18.04 ,Jetpack4.2.3, I want use the csi camera on TX2 board to capture some images,this is my python code:

[b]# -- coding:utf-8 --
import cv2

#打开摄像头
cap=cv2.VideoCapture(0)
#设定摄像头采集分辨率为320x230
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)

n = 0;
#如果摄像头成功打开
while(cap.isOpened()):
#读取摄像头采集的当前帧图片数据
success, frame=cap.read()
#如果成功读取摄像头
if (success):
#显示当前帧图片
cv2.imshow(‘testCamera’, frame)
#获取用户按键
key = cv2.waitKey(1);
#如果用户按下回车键
if ( key & 0xFF == 10 ):
filename = ‘\home\nvidia\workspace\banana/’ + str(n) + ‘.jpg’
#按照指定文件名存储当前帧图片
cv2.imwrite(filename, frame)
print ('Capture ’ + filename)
n = n + 1
#如果用户按下取消键
elif ( key & 0xFF == 27 ):
break
#如果读取摄像头失败
else:
break

#释放摄像头
cap.release()[/b]

when I run command: python file_name.py, error occured:

hld@hld-desktop:/media/hld/PKBACK# 001/workspace/banana$ python test_camera.py
select timeout
select timeout
OpenCV Error: Assertion failed (total() == 0 || data != NULL) in Mat, file /build/opencv-XDqSFW/opencv-3.2.0+dfsg/modules/core/include/opencv2/core/mat.inl.hpp, line 431
Traceback (most recent call last):
File “test_camera.py”, line 15, in
success, frame=cap.read()
cv2.error: /build/opencv-XDqSFW/opencv-3.2.0+dfsg/modules/core/include/opencv2/core/mat.inl.hpp:431: error: (-215) total() == 0 || data != NULL in function Mat

but when i run command: nvgstcapture-1.0 --prev-res=3
the camera can work well.
I don’t know how to solve it, please help me ,thx u!
1582654247.png
1582654291(1).png

oh,the code format is wrong. I will upload some screenshots

Reference to this source.

https://jkjung-avt.github.io/tx2-camera-with-python/