New to OpenCV and Python

asked 2019-09-05 13:49:38 -0600

Hello,

I'm new to Python and OpenCV. I'm trying to write a Python script using OpenCV that saves a video file when a face is detected. Its creating output.avi, but its 0 kb. I'm not sure what I'm doing wrong.

import cv2

cascPath = 'haarcascade_frontalface_default.xml'

faceCascade = cv2.CascadeClassifier(cascPath)

camera = cv2.VideoCapture('rtsp://ituser:[email protected]/mpeg4/media.amp')

out = cv2.VideoWriter('output.avi', -1, 20.0, (640, 480))

while (camera.isOpened()):
# Capture frame-by-frame
ret, frame = camera.read()

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

faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.2,
    minNeighbors=5,
    minSize=(30, 30),
)

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
    print("Capturing face")
    out.write(frame)

# Display the resulting frame
cv2.imshow('SCA Server Room', frame)

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

# When everything is done, release the capture
camera.release()
out.release()
cv2.destroyAllWindows()

Thanks,

Tony

edit retag flag offensive close merge delete

Comments

I changed the video format from .AVI to .MP4, and its saving the vido now as output.mp4. I noticed that it will only save the video after I press 'q' to quit the video. How can I make it so that it saves the video even if I don't press 'q'?

tantony gravatar imagetantony ( 2019-09-05 14:06:28 -0600 )edit
1

out.release() save the video

LBerger gravatar imageLBerger ( 2019-09-05 15:52:40 -0600 )edit

problem with your question is mainly, that you did not think clearly about "what should happpen" ?

berak gravatar imageberak ( 2019-09-05 16:49:35 -0600 )edit