Saving output of object recognition in macOS — OpenCV Python

Venkatesh Chandra
Analytics Vidhya
Published in
3 min readJan 2, 2020
Image Source — Link

This post is important to those of you who have successfully worked on your object detection task.

I did some research on the process of saving the output of object detection on OpenCV in Python in a video format on the hard drive. I realized that there are very few articles which explain the exact way on how to do it. In this article, we will learn how to save the output video in macOS system.

Tip: Saving the video in a windows system is a painful task. The issue is with the output codec, and the CV community often discusses it.

Let us take the pedestrian detection code as a reference.

import cv2cap = cv2.VideoCapture(<enter file path.mp4>)pedestrian_cascade = cv2.CascadeClassifier(cv2.VideoCapture(<enter file path.xml>))while True:
ret, frames = cap.read()
pedestrians = pedestrian_cascade.detectMultiScale( frames, 1.1,
1)
for (x,y,w,h) in pedestrians:
cv2.rectangle(frames,(x,y),(x+w,y+h),(0,255,0),2)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frames, 'Person', (x + 6, y - 6), font, 0.5, (0,
255, 0), 1)
cv2.imshow('Pedestrian detection', frames)
if cv2.waitKey(33) == 13:
break
cap.release()
cv2.destroyAllWindows()

To record the content which is shown in individual frames to the user through the imshow command, we need to define the output where each frame in the while loop is written over the output.

Code to write the video on the hard drive — macOS

The code which saves the output video in the specified path is below with changes in bold with reference to the code shown above — explanation follows after the code

import cv2cap = cv2.VideoCapture( <enter file path.mp4>)fourcc = cv2.VideoWriter_fourcc('m','p','4','v')# note the lower caseframe_width = int(cap.get(3))
frame_height = int(cap.get(4))
out = cv2.VideoWriter(<enter file path.mp4>,fourcc , 10, (frame_width,frame_height), True)pd_cascade = cv2.CascadeClassifier(<enter file path.xml>)while True:
ret, frames = cap.read()
pedestrians = pd_cascade.detectMultiScale( frames, 1.1, 1)for (x,y,w,h) in pedestrians:
cv2.rectangle(frames,(x,y),(x+w,y+h),(0,255,0),2)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frames, 'Person', (x + 6, y - 6), font, 0.5, (0, 255, 0), 1)
cv2.imshow('Pedestrian detection', frames)
out.write(frames)

if cv2.waitKey(33) == 13:
break
cap.release()
out.release()
cv2.destroyAllWindows()

Explanation of the code

Before we start reading the video in frames, we define the output as follows:

fourcc = cv2.VideoWriter_fourcc('m','p','4','v')# note the lower caseframe_width = int(cap.get(3))
frame_height = int(cap.get(4))
out = cv2.VideoWriter(<enter file path.mp4>,fourcc , 10, (frame_width,frame_height), True)

The output of the written video needs specifications like filename, codec, frames per second (fps), frame height and frame width.

Explanation of parameters:

fourcc: Codec which needs to be stated. We can define it as follows:

fourcc = cv2.VideoWriter_fourcc('m','p','4','v')# note the lower case

Please note that you need to write the codec in lowercase

Also, the frame width and frame height can be referenced to the input video/webcam or the user provides it. I have used FPS 10 in the code. However, you can get the input video’s frame dimensions using:

frame_width = int(cap.get(3))
frame_height = int(cap.get(4))

Frames per second can be mapped to the input video’s FPS value using:

get(CAP_PROP_FPS) or get(CV_CAP_PROP_FPS)

To generate a colored video as output, we use the isColor parameter.

isColor= True

Keep in mind

Once you run the code through the Anaconda prompt, you will see the pop-up video which displays the output in frames. The video is being written in the background at the speed at which you are shown the video.

The moment you quit the output video, the video is saved in the path specified until the point where the output was streamed.

That’s all folks! Let me know in the comments section if you face any issues.

Codes

Related links

Pedestrian Detection in Python using OpenCV — Windows and macOS

Vehicle Detection in Python using OpenCV — Windows and macOS

Real-Time Face Detection System in Python — Windows and macOS

Face Detection on recorded videos in Python — Windows and macOS

Where to find me 🤓

  1. Connect with me on LinkedIn/ GitHub / My website
  2. Feeling generous? Buy me a coffee here ☕️

--

--