import cv2
import time
# 使用GStreamer管道打开CSI摄像头的函数
def open_cam_on_jetson(width, height):
# 这里的管道配置根据具体的摄像头和Jetson型号,可能需要做适当修改
pipeline = (
"nvarguscamerasrc ! "
"video/x-raw(memory:NVMM), "
"width=(int)%d, height=(int)%d, "
"format=(string)NV12, framerate=(fraction)30/1 ! "
"nvvidconv flip-method=0 ! "
"video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
"videoconvert ! "
"video/x-raw, format=(string)BGR ! appsink"
) % (width, height, width, height)
return cv2.VideoCapture(pipeline, cv2.CAP_GSTREAMER)
cap = open_cam_on_jetson(1920, 1080) # 以1280x720分辨率打开摄像头
if not cap.isOpened():
print("Cannot open camera!")
exit()
while True:
ret, frame = cap.read()
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
# 显示图像
cv2.imshow('Camera', frame)
# 等待按键
key = cv2.waitKey(1)
if key == ord(' '): # 空格键拍照
# 使用时间戳创建一个唯一的文件名
timestamp = time.strftime("%Y%m%d-%H%M%S")
filename = f"photo_{timestamp}.jpg"
cv2.imwrite(filename, frame) # 保存照片
print(f"Photo taken! Saved as {filename}")
elif key == 27: # ESC键退出
break
# 释放资源
cap.release()
cv2.destroyAllWindows()
linux系统通过csi摄像头拍照代码
最新推荐文章于 2024-03-27 16:13:56 发布