10 examples of 'cv2.videocapture(0)' in Python

Every line of 'cv2.videocapture(0)' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
46def init_camera(self):
47 # create the device
48 self._device = hg.cvCreateCameraCapture(self._index)
49
50 # Set preferred resolution
51 cv.SetCaptureProperty(self._device, cv.CV_CAP_PROP_FRAME_WIDTH,
52 self.resolution[0])
53 cv.SetCaptureProperty(self._device, cv.CV_CAP_PROP_FRAME_HEIGHT,
54 self.resolution[1])
55
56 # and get frame to check if it's ok
57 frame = hg.cvQueryFrame(self._device)
58 # Just set the resolution to the frame we just got, but don't use
59 # self.resolution for that as that would cause an infinite recursion
60 # with self.init_camera (but slowly as we'd have to always get a frame).
61 self._resolution = (int(frame.width), int(frame.height))
62
63 #get fps
64 self.fps = cv.GetCaptureProperty(self._device, cv.CV_CAP_PROP_FPS)
65 if self.fps <= 0:
66 self.fps = 1 / 30.
67
68 if not self.stopped:
69 self.start()
4def main():
5 capture = cv2.VideoCapture(0)
6
7 if capture.isOpened():
8 flag, frame = capture.read()
9 else:
10 flag = False
11
12 while flag:
13
14 flag, frame = capture.read()
15
16 frame = abs(255 - frame)
17
18 cv2.imshow("Video Camera", frame)
19
20 if cv2.waitKey(1) & 0xFF == 27:
21 break
22
23 cv2.destroyAllWindows()
191def startCamera(self, source_name):
192
193 try:
194 source = int(source_name)
195 except:
196 IkaUtils.dprint('%s: Looking up device name %s' %
197 (self, source_name))
198 try:
199 source_name = source_name.encode('utf-8')
200 except:
201 pass
202
203 try:
204 source = self.enumerateInputSources().index(source_name)
205 except:
206 IkaUtils.dprint("%s: Input '%s' not found" %
207 (self, source_name))
208 return False
209
210 IkaUtils.dprint('%s: initalizing capture device %s' % (self, source))
211 self.realtime = True
212 if self.isWindows():
213 self.initCapture(700 + source)
214 else:
215 self.initCapture(0 + source)
22def __enter__(self):
23 self.cap = cv2.VideoCapture(self.dev)
24 self.running = True
25 self.start()
26 return self
42def setupUsbCam(self):
43 # initialize the camera and grab a reference to the raw camera capture
44 self.rawCapture = cv2.VideoCapture(self.cameraNum)
45
46 # wait for camera to warm up
47 time.sleep(0.1)
19def __init__(self, instance):
20
21 # health
22 self.healthy = False;
23
24 # record instance
25 self.instance = instance
26 self.config_group = "camera%d" % self.instance
27
28 # get image resolution
29 self.img_width = sc_config.config.get_integer(self.config_group,'width',640)
30 self.img_height = sc_config.config.get_integer(self.config_group,'height',480)
31
32 # background image processing variables
33 self.img_counter = 0 # num images requested so far
34
35 # latest image captured
36 self.latest_image = None
37
38 # setup video capture
39 self.camera = cv2.VideoCapture(self.instance)
40
41 # check we can connect to camera
42 if not self.camera.isOpened():
43 print("failed to open webcam %d" % self.instance)
4def main():
5 capture = cv2.VideoCapture(0)
6 eye_path = "../classifier/haarcascade_eye.xml"
7 face_path = "../classifier/haarcascade_frontalface_default.xml"
8
9 eye_cascade = cv2.CascadeClassifier(eye_path)
10 face_cascade = cv2.CascadeClassifier(face_path)
11
12 while (True):
13 _, frame = capture.read()
14
15 gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
16
17 eyes = eye_cascade.detectMultiScale(gray_frame, scaleFactor=1.05, minNeighbors=5, minSize=(10,10))
18 faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.05, minNeighbors=5, minSize=(40, 40))
19
20 print("Number of eyes : " + str(len(eyes)))
21 print("Number of faces : " + str(len(faces)))
22
23 for (x, y, w, h) in eyes:
24 cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
25
26 for (x, y, w, h) in faces:
27 cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
28
29 cv2.imshow("Live Capture", frame)
30
31 if cv2.waitKey(1) == 27:
32 break
33
34 cv2.destroyAllWindows()
35 capture.release()
51def __init__(self, source=None):
52 self.previous_frame = None
53 self.current_frame = None
54 operating_system = os.uname()[-1]
55
56 # if an rpi
57 if source is None:
58 if 'arm' in operating_system:
59 self.camera_type = CameraType.pi
60 else:
61 self.camera_type = CameraType.cv
62 else:
63 self.camera_type = CameraType.custom
64
65 if self.camera_type == CameraType.pi:
66 self.camera = picamera.PiCamera()
67 time.sleep(2)
68 self.camera.resolution = (1280, 720)
69 self.camera.framerate = Fraction(1, 6)
70 self.camera.exposure_mode = 'off'
71 self.camera.shutter_speed = 6000000
72 self.camera.iso = 800
73 elif self.camera_type == CameraType.cv:
74 self.camera = cv2.VideoCapture(0)
75 elif self.camera_type == CameraType.custom:
76 self.camera = cv2.VideoCapture(source)
12def __init__(self, video_file):
13 vid = cv2.VideoCapture(video_file)
14 # sometimes video capture seems to give the wrong dimensions read the
15 # first image and try again
16 # get video frame, stop program when no frame is retrive (end of file)
17 ret, image = vid.read()
18 vid.release()
19
20 if ret:
21 self.height = image.shape[0]
22 self.width = image.shape[1]
23 self.dtype = image.dtype
24 self.vid = cv2.VideoCapture(video_file)
25 self.video_file = video_file
26 else:
27 raise OSError(
28 'Cannot get an image from %s.\n It is likely that either the file name is wrong, the file is corrupt or OpenCV was not installed with ffmpeg support.' %
29 video_file)
18def detect():
19 face = cv2.CascadeClassifier("data/haarcascade_frontalface_default.xml")
20 eye = cv2.CascadeClassifier("data/haarcascade_eye.xml")
21
22 camera = cv2.VideoCapture(0) # 0表示使用第一个摄像头
23
24 while True:
25 ret, frame = camera.read() # ret:布尔值表示是否读取帧成功, frame为帧本身
26 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 检测人脸需要基于灰度图像
27
28 faces = face.detectMultiScale(gray, 1.3, 5)
29 # faces = face.detectMultiScale(gray, scaleFactor, minNeighbors)
30 # scaleFactor: 每次迭代时图像的压缩率
31 # minNeighbors: 每个人脸矩形保留近似邻近数目的最小值
32
33 for x,y,w,h in faces:
34 img = cv2.rectangle(frame, (x,y), (x + w, y + h), (250, 0, 0), 2)
35
36 eye_area = gray[y : y + h, x : x + w]
37 eyes = eye.detectMultiScale(eye_area, 1.03, 5, 0, (40, 40))
38 # eye.detectMultiScale(eye_area, 1.03, 5, 0, (40, 40))中
39 # (40, 40)参数目的是为了消除假阳性(false positive)的影响, 将眼睛搜索的最小尺寸现实为40x40
40 for ex,ey,ew,eh in eyes:
41 cv2.rectangle(frame, (x + ex, y + ey),(x + ex + ew, y + ey + eh), (0, 255, 0), 2)
42
43 cv2.imshow("face", frame)
44 if cv2.waitKey(1000 // 12) & 0xff == ord("q"):
45 break
46 camera.release()
47 cv2.destroyAllWindows()

Related snippets