Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use opencv to call camera and display, read video file and display, save video and so on.

Shulou Source: shulou.com Published: 2022-06-01 14:08:55 09月12日 Update

This article will explain in detail how opencv can call the camera and display, read the video file and display, save the video and so on. The editor finds it very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Call the camera and display the video:

Import numpy as npimport cv2 as cv# cap = cv.VideoCapture (- 1) # cap = cv.VideoCapture (1) cap = cv.VideoCapture (0) # pass in a device index that represents different cameras Or pass in a video file name # cap = cv.VideoCapture ('testVideo.mp4') if not cap.isOpened (): print ("Cannot open camera") exit () width = cap.get (cv.CAP_PROP_FRAME_WIDTH) height = cap.get (cv.CAP_PROP_FRAME_HEIGHT) print (' width and height are:', width, height) # width and height are: 640.0 480.0ret = cap.set (cv.CAP_PROP_FRAME_WIDTH) Set width ret = cap.set (cv.CAP_PROP_FRAME_HEIGHT,240) # set height while True:# Capture frame-by-frameret, frame = cap.read () # returns True or False, if read correctly is True, you can use False to determine whether to reach the end of the video # if frame is read correctly ret is Trueif not ret:print ("Can't receive frame (stream end?). Exiting... ") break# Our operations on the frame come heregray = cv.cvtColor (frame, cv.COLOR_BGR2GRAY) # Display the resulting framecv.imshow ('LinMaZi-frame-LinZuQuan', gray) # set the display window title text if cv.waitKey (1) = = ord (' q'): # determine whether the user enters the Q key break# When everything done, release the capturecap.release () # release resource cv.destroyAllWindows ()

Read the video file and display:

Import numpy as npimport cv2 as cvcap = cv.VideoCapture ('graduation party .MP4') # cap = cv.VideoCapture (0) while cap.isOpened (): ret, frame = cap.read () # if frame is read correctly ret is Trueif not ret:print ("Can't receive frame (stream end?). Exiting. ") breakgray = cv.cvtColor (frame, cv.COLOR_BGR2GRAY) cv.imshow ('frame', gray) if cv.waitKey (25) = = ord (' q'): # choose the appropriate refresh interval, 25 milliseconds is not bad breakcap.release () cv.destroyAllWindows ()

Call the camera and display it, save it as a video file:

Import numpy as npimport cv2 as cvimport timecap = cv.VideoCapture (0) # Define the codec and create VideoWriter objectfourcc = cv.VideoWriter_fourcc (* 'XVID') # both are equivalent # fourcc = cv.VideoWriter_fourcc ("% Yy-%mm-%dd@%Hh%Mm%Ss", time.localtime ()) out = cv.VideoWriter (' output_' + saveTime + '.mp4', fourcc, 20.0,640) Parameter output file name, FourCC code, frames per second (fps), frame sizewhile cap.isOpened (): ret, frame = cap.read () if not ret:print ("Can't receive frame (stream end?). Exiting. ") break# frame = cv.flip (frame, 1) # left and right horizontal flip frame = cv.flip (frame, 0) # up and down vertical flip # write the flipped frameout.write (frame) cv.imshow ('LinMaZi-frame-LinZuQuan', frame) if cv.waitKey (1) = ord (' q'): break# Release everything if job is finishedcap.release () out.release () cv.destroyAllWindows () about" opencv how to call the camera and display Read the video file and display, save the video and other operations. "this article ends here." Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Tags: Video camera file camera width article height good more different appropriate practical up and down representative content parameters file name text article time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Information Docker macOS vpn