python realizes the function of capturing pictures from local cameras and webcams
- 2021-07-13 05:44:53
- OfStack
python--Capture images from local cameras and webcams with the following code:
import cv2
# Get the local camera
# folder_path Storage directory of intercepted pictures
def get_img_from_camera_local(folder_path):
cap = cv2.VideoCapture(0)
i = 1
while True:
ret, frame = cap.read()
cv2.imshow("capture", frame)
print str(i)
cv2.imwrite(folder_path + str(i) + '.jpg', frame) # Store as an image
if cv2.waitKey(1) & 0xFF == ord('q'):
break
i += 1
cap.release()
cv2.destroyAllWindows()
# Get webcam, format: rtsp://username:pwd@ip/
# folder_path Storage directory of intercepted pictures
def get_img_from_camera_net(folder_path):
cap = cv2.VideoCapture('rtsp://username:pwd@ip/')
i = 1
while True:
ret, frame = cap.read()
cv2.imshow("capture", frame)
print str(i)
cv2.imwrite(folder_path + str(i) + '.jpg', frame) # Store as an image
if cv2.waitKey(1) & 0xFF == ord('q'):
break
i += 1
cap.release()
cv2.destroyAllWindows()
# Test
if __name__ == '__main__':
folder_path = 'D:\\img_from_camera\\'
get_img_from_camera_local(folder_path)
PS: Let's take a look at opening the webcam with python and opencv to read the image
Open the webcam with the following code:
###########################################
import cv2
url = 'rtsp://admin:password@192.168.1.104:554/11'
cap = cv2.VideoCapture(url)
while(cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
############################################
Note: Change the image taken from the camera from bgr to rgb: image = cv2.cvtColor (image, cv2.COLOR_BGR2RGB)
1. cap = cv2.VideoCapture (0)
A parameter of 0 in VideoCapture () indicates that the built-in camera of the notebook is turned on, and a parameter of 1 indicates that the usb camera is turned on.
If the parameter is the video file path, the video is opened, such as cap = cv2.VideoCapture ("../test. avi")
2. ret, frame = cap. read ()
cap. read () reads the video in frames, ret and frame are the two return values of the cap. read () method. Where ret is a Boolean value, True is returned if the read frame is correct, and False is returned if the file is read to the end. frame is the image of every frame, which is a three-dimensional matrix.
3. The cv2. waitKey (1), waitKey () method itself means waiting for keyboard input,
The parameter is 1, which means that the delay is 1ms to switch to the next frame image, for video;
The parameter is 0, for example, cv2.waitKey (0) only displays the current frame image, which is equivalent to video pause;
If the parameter is too large, such as cv2.waitKey (1000), it will feel stuck because of the long delay.
ASCII code inputted by keyboard is obtained by c, and ASCII code corresponding to esc key is 27, that is, when esc key is pressed, if conditional sentence holds
4. Call release () to release the camera and call destroyAllWindows () to close all image windows.
Summarize