In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to capture, play and save camera video based on Python". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Read video
To capture video, you need to create a VideoCapture object. Its parameters can be the device index or the name of the video file. Therefore, there are two ways to read video, one is to read video from the camera and the other is to read video from a file.
Read video from the camera
For devices with cameras, such as laptops with cameras, we can directly adjust the camera of the computer and read the video stream of the camera.
Import cv2 as cvcap = cv.VideoCapture (0) if not cap.isOpened (): print ("Cannot open camera") exit () while True: # capture ret frame by frame, frame = cap.read () # if the frame is read correctly Ret displays the result frame cv.imshow ('frame', frame) if cv.waitKey (1) for True if not ret: break# = = ord (' q'): after break# has completed all operations, release the trap cap.release () cv.destroyAllWindows ()
Here I pass the parameter 0 to the VideoCapture object, which represents the device index, which is the number that specifies which camera. Normally, a camera is connected (as in my case). So I simply pass zero. You can select the second camera by passing 1, and so on.
Cap.isOpened () is used to determine whether a video has been captured.
Cap.read () returns a Boolean value (True/ False). If the frame is read correctly, it will be True. Therefore, you can check the end of the video by checking this return value.
The cv.imshow method is used to display the frame of the video. The way we play videos is to play them frame by frame.
In the end, don't forget to release the prisoners through cap.release ().
Run this code and you can see a pop-up window that plays the image from your computer camera in real time.
Play a video from a file
Same as capturing from the camera, except that the camera index is changed with the video file name.
In addition, when displaying video, you can use cv.waitKey () to control the speed of video playback. If the setting is too small, the video will be very fast, equivalent to double-speed playback, and if too large, the video will become very slow, equivalent to delayed playback. Normally, 25 milliseconds will be fine.
Import cv2 as cvcap = cv.VideoCapture ('video.mp4') while cap.isOpened (): ret, frame = cap.read () # if the frame is read correctly, ret is True if not ret: break cv.imshow (' frame', frame) if cv.waitKey (1) = ord ('q'): breakcap.release () cv.destroyAllWindows ()
Run this code and you can see a pop-up window to play the video file of your choice.
Save video
By reading the video from the camera, we can save the video locally. We capture a video and process it frame by frame. If we want to save this video, it's very simple, just use cv.VideoWriter ().
Cv.VideoWriter () has five parameters:
Parameter 1: output file name, for example: output.mp4.
Parameter 2:FourCC code, FourCC is a 4-byte code used to specify a video codec.
Parameter 3: the number of frame rates.
Parameter 4: frame size.
Parameter 5: color flag. If True, the normal color output, otherwise it is the gray image output.
With regard to the comparison between FourCC and video formats, I list some common formats:
Cv2.VideoWriter_fourcc ('pawning, recording, writing, recording, writing, etc.) = MPEG-1 codec
Cv2.VideoWriter_fourcc ('Mauremeng') = motion-jpeg codec-- > mp4v
Cv2.VideoWriter_fourcc ('Mr.,' Mr., '4percent,' 2') = MPEG-4.2 codec
Cv2.VideoWriter_fourcc ('Downs,' Illustrated, 'Vince,' 3') = MPEG-4.3 codec
Cv2.VideoWriter_fourcc ('Dache,' iTunes, 'Vince,' X') = MPEG-4 codec-- > avi
Cv2.VideoWriter_fourcc ('Utility,' 2,'6,'3') = H263 codec
Cv2.VideoWriter_fourcc ('I,'2,'6,'3') = H263I codec
Cv2.VideoWriter_fourcc ('faded,' lumped, 'Vested,' 1') = FLV1 codec
Save the code for the video:
Import cv2 as cvcap = cv.VideoCapture (0) # define the codec and create the VideoWriter object fourcc = cv.VideoWriter_fourcc (* 'MJPG') out = cv.VideoWriter (' output.mp4', fourcc, 20.0,640,480) while cap.isOpened (): ret, frame = cap.read () if not ret: break frame = cv.flip (frame, 1) # write flipped frame out.write (frame) cv.imshow ('frame') Frame) if cv.waitKey (1) = ord ('q'): release all content cap.release () out.release () cv.destroyAllWindows () after break# finishes its work
Run this code and you can find an output.mp4 video file in the code directory.
In the above code snippets, if you want to exit the video operation, just click Q on the keyboard.
This is the end of the content of "how to capture, play and save camera video based on Python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.