Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
OpenCV 3 Computer Vision with Python Cookbook

You're reading from   OpenCV 3 Computer Vision with Python Cookbook Leverage the power of OpenCV 3 and Python to build computer vision applications

Arrow left icon
Product type Paperback
Published in Mar 2018
Publisher Packt
ISBN-13 9781788474443
Length 306 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Aleksandr Rybnikov Aleksandr Rybnikov
Author Profile Icon Aleksandr Rybnikov
Aleksandr Rybnikov
Aleksei Spizhevoi Aleksei Spizhevoi
Author Profile Icon Aleksei Spizhevoi
Aleksei Spizhevoi
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. I/O and GUI FREE CHAPTER 2. Matrices, Colors, and Filters 3. Contours and Segmentation 4. Object Detection and Machine Learning 5. Deep Learning 6. Linear Algebra 7. Detectors and Descriptors 8. Image and Video Processing 9. Multiple View Geometry 10. Other Books You May Enjoy

Writing a frame stream into video

In this recipe, you will learn how to capture frames from a USB camera live and simultaneously write frames into a video file using a specified video codec.

Getting ready

You need to have OpenCV 3.x installed with Python API support.

How to do it...

Here are the steps we need to execute in order to complete this recipe:

  1. First, we create a camera capture object, as in the previous recipes, and get the frame height and width:
import cv2
capture = cv2.VideoCapture(0)
frame_width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
print('Frame width:', frame_width)
print('Frame height:', frame_height)
  1. Create a video writer:
video = cv2.VideoWriter('../data/captured_video.avi', cv2.VideoWriter_fourcc(*'X264'),
25, (frame_width, frame_height))
  1. Then, in an infinite while loop, capture frames and write them using the video.write method:
while True:
has_frame, frame = capture.read()
if not has_frame:
print('Can\'t get frame')
break

video.write(frame)

cv2.imshow('frame', frame)
key = cv2.waitKey(3)
if key == 27:
print('Pressed Esc')
break
  1. Release all created VideoCapture and VideoWriter objects, and destroy the windows:
capture.release()
writer.release()
cv2.destroyAllWindows()

How it works...

Writing video is performed using the cv2.VideoWriter class. The constructor takes the output video path, four characted code (FOURCC) specifying video code, desired frame rate and frame size. Examples of codec codes include P, I, M, and 1 for MPEG-1; M, J, P, and G for motion-JPEG; X, V, I, and D for XVID MPEG-4; and H, 2, 6, and 4 for H.264.

You have been reading a chapter from
OpenCV 3 Computer Vision with Python Cookbook
Published in: Mar 2018
Publisher: Packt
ISBN-13: 9781788474443
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image