python控制相机拍照
时间: 2023-06-23 14:47:12 浏览: 54
Python 控制相机拍照需要用到一些第三方库,比如 `opencv-python` 和 `pyrealsense2` 等。以下是用 `pyrealsense2` 库控制 Intel RealSense 相机拍照的示例代码:
```python
import pyrealsense2 as rs
import numpy as np
import cv2
# 配置相机
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
# 启动相机
pipeline.start(config)
# 等待相机预热
for i in range(30):
pipeline.wait_for_frames()
# 获取一帧图像
frames = pipeline.wait_for_frames()
color_frame = frames.get_color_frame()
color_image = np.asanyarray(color_frame.get_data())
# 保存图像
cv2.imwrite("photo.jpg", color_image)
# 停止相机
pipeline.stop()
```
以上代码首先使用 `rs.config()` 配置相机,设置要获取的数据流为颜色图像流,分辨率为 640x480,格式为 BGR8,帧率为 30 帧/秒。然后使用 `pipeline.start()` 启动相机,使用 `pipeline.wait_for_frames()` 获取一帧图像,再使用 `cv2.imwrite()` 将图像保存到本地。最后使用 `pipeline.stop()` 停止相机。
注意:使用不同的相机和库时,代码会有所不同。