-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcapture_camera.py
70 lines (40 loc) · 1.64 KB
/
capture_camera.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#####################################################################
# Example : capture an image from an attached camera
# Author : Toby Breckon, toby.breckon@durham.ac.uk
# Copyright (c) 2015 School of Engineering & Computing Science,
# Durham University, UK
# License : LGPL - http://www.gnu.org/licenses/lgpl.html
#####################################################################
import numpy as np
import cv2
import sys
#####################################################################
camera_to_use = 1; # 0 if you have one camera, 1 or > 1 otherwise
#####################################################################
# define video capture object
cap = cv2.VideoCapture();
# define display window name
windowName = "Live Camera Input"; # window name
# open camera device (and check it worked)
if not(cap.open(camera_to_use)):
print("Cannot open camera - check connection and operation as suggested.");
sys.exit;
# read an image from the camera
ret, frame = cap.read();
# to avoid the black/blank first frame from many cameras
# with some (not all) cameras you need to read the first frame twice (first frame only)
ret, frame = cap.read();
# check it has loaded
if not frame is None:
# create window by name (as resizable)
cv2.namedWindow(windowName, cv2.WINDOW_NORMAL);
# display image
cv2.imshow(windowName,frame);
# start the event loop - essential
# wait indefinitely for any key press
cv2.waitKey(0);
else:
print("No image successfully loaded from camera.")
# close all windows
cv2.destroyAllWindows()
#####################################################################