Ask Your Question

Revision history [back]

New to OpenCV and Python

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