Error in Writing Video with OpenCV for Video Classification

Question:

I am building a video classification model same as in this video: https://www.youtube.com/watch?v=l8XQsxlGxiY&list=PLxefhmF0pcPl_v-lLsqF3drP6NOoSYJR0&index=9

The model predicts which sport is being shown in the video: Tennis, swimming or Boxing.

I already built my model and has a 96% accuracy.
Now, I have a sample video to test my model.

from keras.models import load_model
from collections import deque
import numpy as np
import pickle
import cv2
model = load_model(r"C:Usersyudishteer.cDesktopVideoClassificationvideo_classification_modelvideoclassificationModel")
lb = pickle.loads(open(r"C:Usersyudishteer.cDesktopVideoClassificationvideo_classification_modelvideoclassificationBinarizer.pickle", "rb").read())
outputvideo = r"C:Usersyudishteer.cDesktopVideoClassificationvideo_classification_modeloutputvideodemo_output.avi"
mean = np.array([123.68, 116.779, 103.939], dtype = "float32")
Queue = deque(maxlen=128)
capture_video=cv2.VideoCapture(r"C:Usersyudishteer.cDesktopVideoClassificationvideo_classification_modeldemo_video.mp4")
writer = None
(Width, Height) = (None, None)

while True:
    (taken, frame) = capture_video.read()
    if not taken:
        break
    if Width is None or Height is None:
        (Width, Height) = frame.shape[:2]
        
    output = frame.copy()
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    frame = cv2.resize(frame, (224,224)).astype("float32")
    frame -= mean
    preds = model.predict(np.expand_dims(frame, axis=0))[0]
    Queue.append(preds)
    results = np.array(Queue).mean(axis = 0)
    i = np.argmax(results)
    label = lb.classes_[i]
    text = "The game is {}".format(label)
    cv2.putText(output, text, (45,60), cv2.FONT_HERSHEY_SIMPLEX, 1.25, (255,0,0),5)
    
    if writer is None:
        fourcc = cv2.VideoWriter_fourcc(*'MJPG')
        writer = cv2.VideoWriter('demo_output.mp4', fourcc, 10, (Width, Height), True)
    writer.write(output)
    cv2.imshow("In progress", output)
    key = cv2.waitKey(1) & 0xFF
    
    if key == ord("q"):
        break
        
print("Finalizing.......")
writer.release()
capture_video.release()
# Closes all the frames
cv2.destroyAllWindows()

When I run the above code, I can see it being classified correctly. 
That is, the demo video(demo_video.mp4) is opened and I can see either swimming, tennis, or boxing being displayed at the top of the video depending on the sport being shown.

However, since I have a:
writer = cv2.VideoWriter(‘demo_output.mp4’, fourcc, 10, (Width, Height), True)

When I open my demo_output.mp4 (or even avi), I get:

error

Can someone help what is wrong?
thanks!

Answers:

You have mixed Width and Height

Instead of (Width, Height) = frame.shape[:2], it should be:

(Height, Width) = frame.shape[:2]

In my system, I am getting the following warning:

OpenCV: FFMPEG: tag 0x47504a4d/’MJPG’ is not supported with codec id 7 and format ‘mp4 / MP4 (MPEG-4 Part 14)’
OpenCV: FFMPEG: fallback to use tag 0x7634706d/’mp4v’

The result is a black video.

One of the following combinations of codec/container works:

  • 'MJPG' FOURCC and AVI container:

     fourcc = cv2.VideoWriter_fourcc(*'MJPG')
     writer = cv2.VideoWriter('demo_output.avi', fourcc, 10, (Width, Height), True)
    
  • 'mp4v' FOURCC and MP4 container:

     fourcc = cv2.VideoWriter_fourcc(*'mp4v')
     writer = cv2.VideoWriter('demo_output.mp4', fourcc, 10, (Width, Height), True)
    
Answered By: Rotem