本文转自博客园-Arkenstone
由于opencv自带的VideoCapture函数直接从usb摄像头获取视频数据,所以用这个来作为实时的图像来源用于实体检测识别是很方便的。
- 安装opencv
安装的步骤可以按照之前这个文章操作。如果在测试的时候:
cam = cv2.VideoCapture(0)
print cam.isOpend()
返回了False,很有可能是在安装的时候cmake的配置没有设置后,可以make uninstall之后重新cmake。
-
安装usb摄像头驱动(这个一般都不需要)
如果系统没有预装usb摄像头的驱动,那么根据所用的摄像头安装相应的驱动即可。安装完之后可以用lsusb或者
v4l2-ctl --list-device`命令查看当前链接的usb设备来确认。这里我们使用的摄像头是罗技c930e。 -
设置摄像头参数
设置可以在脚本中用opencv或者在命令行用v4l2-ctl
命令设置:
1). opencv
0. CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
1. CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
3. CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file
4. CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
5. CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
6. CV_CAP_PROP_FPS Frame rate.
7. CV_CAP_PROP_FOURCC 4-character code of codec.
8. CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
9. CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
10. CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
11. CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
12. CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
13. CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
14. CV_CAP_PROP_HUE Hue of the image (only for cameras).
15. CV_CAP_PROP_GAIN Gain of the image (only for cameras).
16. CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
17. CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
18. CV_CAP_PROP_WHITE_BALANCE Currently unsupported
19. CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
set camera properties
cam.set(4, 1280) # img width,第一个数字对应上述属性
cam.set(5, 640) # img height
cam.set(6, 24) # video FPS
2). v4l2-ctl
首先用v4l2-ctl --list-device
确定usb摄像头的device编号(一般为/dev/video0),然后查看该设备可以设置的参数:
v4l2-ctl -d /dev/video0 --list-ctrls
罗技c930e摄像头的参数如下:
brightness (int) : min=0 max=255 step=1 default=-8193 value=128contrast (int) : min=0 max=255 step=1 default=57343 value=128saturation (int) : min=0 max=255 step=1 default=57343 value=128white_balance_temperature_auto (bool) : default=1 value=1gain (int) : min=0 max=255 step=1 default=57343 value=0power_line_frequency (menu) : min=0 max=2 default=2 value=2white_balance_temperature (int) : min=2000 max=6500 step=1 default=57343 value=4000 flags=inactivesharpness (int) : min=0 max=255 step=1 default=57343 value=128backlight_compensation (int) : min=0 max=1 step=1 default=57343 value=0exposure_auto (menu) : min=0 max=3 default=0 value=3exposure_absolute (int) : min=3 max=2047 step=1 default=250 value=250 flags=inactiveexposure_auto_priority (bool) : default=0 value=1pan_absolute (int) : min=-36000 max=36000 step=3600 default=0 value=0tilt_absolute (int) : min=-36000 max=36000 step=3600 default=0 value=0focus_absolute (int) : min=0 max=250 step=5 default=8189 value=0 flags=inactivefocus_auto (bool) : default=1 value=1zoom_absolute (int) : min=100 max=500 step=1 default=57343 value=100
最后可以可以设置参数了:
v4l2-ctl --set-ctrl=zoom_absolute=200
#放大两倍
4. opencv获取图片
这个就很简单了,这里就说明下用waitKey参数来用键盘输入控制视频流:
import cv2cam = cv2.VideoCapture(0)
img_counter = 0
while cam.isOpened():ret, frame = cam.read()cv2.imshow("test", frame)if not ret:breakkey = cv2.waitKey(1) & 0xFFif key == 27: # press ESC to escape (ESC ASCII value: 27)print("Escape hit, closing...")breakelif key == 32:# press Space to capture image (Space ASCII value: 32)img_name = "opencv_frame_{}.png".format(img_counter)cv2.imwrite(img_name, frame)print("{} written!".format(img_name))img_counter += 1else:pass
cam.release()
cv2.destroyAllWindows()
PS:waitKey(1) & 0xFF获取当前按的键的ascii码,如果要用其他键来控制,用相应键的ascii码替换即可。(ascii码查询)。
参考
http://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture
http://stackoverflow.com/questions/11420748/setting-camera-parameters-in-opencv-python
http://kurokesu.com/main/2016/01/16/manual-usb-camera-settings-in-linux/
http://stackoverflow.com/questions/34588464/python-how-to-capture-image-from-webcam-on-click-using-opencv
http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000520.html