|
Anaconda+opencv打开摄像头:
软件环境:win7-32bit-Anaconda4.3.1-opencv2.4.8-python2.7.13(Python 2.7.13 |Anaconda 4.3.1 (32-bit)| (default, Dec 19 2016, 13:36:02) [MSC v.1500 32 bit (Intel)])
- import cv2
- cap = cv2.VideoCapture(0)
- while(1):
- # get a frame
- ret, frame = cap.read()
- # show a frame
- cv2.imshow("ysw", frame)
-
- # 按键盘p停止打开摄像头
- if cv2.waitKey(1) & 0xFF == ord('p'):
- cv2.imwrite("./ysw.jpeg", frame)
- break
- cap.release()
- cv2.destroyAllWindows()
复制代码 我的笔记本电脑配置一个内置摄像头,默认为VideoCapture(0);现在我外接一个USB摄像头,利用python来读取如下。- # -*- coding: utf-8 -*-
- """
- Created on Mon Sep 25 21:56:43 2017
- @author: ysw
- """
- import cv2
- import numpy as np
- cap = cv2.VideoCapture(1)
- #cap.release()
- #cap = cv2.VideoCapture(1)
- # 前一个返回值是是否成功,后一个返回值是图像本身
- cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH,640)
- cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT,480)
- ret = 0
- while(ret<=0):
- ret, frame = cap.read()
- #while(1):
- while(ret):
- # get a frame
- ret, frame = cap.read()
- # show a frame
- cv2.imshow("ysw", frame)
- # 按键盘p停止打开摄像头
- if cv2.waitKey(1) & 0xFF == ord('p'):
- cv2.imwrite("./ysw.jpeg", frame)
- break
- cap.release()
- cv2.destroyAllWindows()
复制代码 在该段代码中,较为重要的一点为:
图像帧初始化操作:
- ret = 0
- while(ret<=0):
- ret, frame = cap.read()
复制代码
参考:
【1】Anaconda安装opencv
【2】http://blog.csdn.net/xwm1000/article/details/71788344
|
|