1import cv2
2
3cap = cv2.VideoCapture(0)
4
5# Check if the webcam is opened correctly
6if not cap.isOpened():
7 raise IOError("Cannot open webcam")
8
9while True:
10 ret, frame = cap.read()
11 frame = cv2.resize(frame, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
12 cv2.imshow('Input', frame)
13
14 c = cv2.waitKey(1)
15 if c == 27:
16 break
17
18cap.release()
19cv2.destroyAllWindows()
1import cv2
2
3cam = cv2.VideoCapture(0)
4
5cv2.namedWindow("test")
6
7img_counter = 0
8
9while True:
10 ret, frame = cam.read()
11 if not ret:
12 print("failed to grab frame")
13 break
14 cv2.imshow("test", frame)
15
16 k = cv2.waitKey(1)
17 if k%256 == 27:
18 # ESC pressed
19 print("Escape hit, closing...")
20 break
21 elif k%256 == 32:
22 # SPACE pressed
23 img_name = "opencv_frame_{}.png".format(img_counter)
24 cv2.imwrite(img_name, frame)
25 print("{} written!".format(img_name))
26 img_counter += 1
27
28cam.release()
29
30cv2.destroyAllWindows()
31
1from cv2 import *
2# initialize the camera
3cam = VideoCapture(0) # 0 -> index of camera
4s, img = cam.read()
5if s: # frame captured without any errors
6 namedWindow("cam-test",CV_WINDOW_AUTOSIZE)
7 imshow("cam-test",img)
8 waitKey(0)
9 destroyWindow("cam-test")
10 imwrite("filename.jpg",img) #save image
11