Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

VideoWriter output video is not playable

So I tried running this code.. I have a LOGITECH webcam, and it works fine with the imshow, however when I added the VideoWriter feature to save the video feed, the video file appears but is not playable... Can anyone help me with this? Thank you!

import cv2

import sys

import logging as log

import datetime as dt

from time import sleep



cascPath = "haarcascade_frontalface_default.xml"

faceCascade = cv2.CascadeClassifier(cascPath)

log.basicConfig(filename='webcam.log',level=log.INFO)

video_capture = cv2.VideoCapture(0)

anterior = 0

video_capture.set(3, 240)

video_capture.set(4, 240)



fourcc = cv2.VideoWriter_fourcc(*'XVID')

out = cv2.VideoWriter('-Vid.avi', fourcc, 10, (240,240))

while True:

if not video_capture.isOpened():

    print('Unable to load camera.')

    sleep(5)

    pass



ret, frame = video_capture.read()

if ret:

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)



    faces = faceCascade.detectMultiScale(

        gray,

        scaleFactor=1.1,

        minNeighbors=5,

        minSize=(30, 30)

    )

    for (x, y, w, h) in faces:

        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)



    if anterior != len(faces):

        anterior = len(faces)

        log.info("faces: "+str(len(faces))+" at "+str(dt.datetime.now()))



    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):

        break

    cv2.imshow('Video', frame)

    out.write(frame)

else:

    break

video_capture.release()

out.release()

cv2.destroyAllWindows()