Fix Opencv slow face recognition on RTSP? (CameraIP)

This is my code using an IP camera to detect faces via RTSP (On the Hikvision IP camera, I have adjusted the frame rate and FPS to low) But it’s still very slow. But when I use USB Camera the speed is very fast. Can anyone who has used CameraIP for facial recognition fix this error for me? Where did my code go wrong? Thank you!

faceCascade = cv2.CascadeClassifier(
clf = cv2.face.LBPHFaceRecognizer_create()“classifier.xml”)

cv2.VideoCapture(‘rtsp://root:1q2w3e4r5t6y@’, 0)
cap.set(cv2.CAP_PROP_FPS, 10)
cap.set(cv2.CAP_PROP_BUFFERSIZE, 0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

while True:
    ret, img =
    imgs = recognize(img, clf, faceCascade)

    frame = cv2.imencode('.jpg', imgs)[1].tobytes()
    yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

    key = cv2.waitKey(1)
    if key == 27:

see also:

Here is my post on I tried removing imgs = recognize(img, clf, faceCascade). The speed without facial recognition is very fast. I think it’s because of faceCascade. Do you have a solution for this problem?