Ask Your Question
0

VideoWriter output video is not playable

asked 2019-05-28 03:40:00 -0500

levinpqr gravatar image

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()
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
-1

answered 2019-05-28 07:18:17 -0500

supra56 gravatar image

updated 2019-05-28 08:23:38 -0500

I didn't test it. I'm using OpenCV 4.1.0. You cannot put outside of if/else condition.

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)
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-05-28 03:40:00 -0500

Seen: 31 times

Last updated: May 28