Opencv ip camera video stream is very slow and lagging how do I fix this?

I’m doing face recognition with python opencv. I will run this project on a server. When running on my computer, there is a delay of more than 10 seconds and fps drops. To solve this, I took the last frame and made it skip the following frames. Now the image is skipping. How can I make it more fluent? The camera I use is Hikvision. No face recognition feature.

ip='rtsp://admin:@10.29.214.120'

    cap = cv2.VideoCapture(1)
    i,j = 0,0
    while 1:
        _,frame = cap.read()
        faces = extract_faces(frame)
        for (x,y,w,h) in faces:
            cv2.rectangle(frame,(x, y), (x+w, y+h), (255, 0, 20), 2)
            cv2.putText(frame,f'Images Captured: {i}/100',(50,50),cv2.FONT_HERSHEY_SIMPLEX,1,(255, 0, 20),2,cv2.LINE_AA)
            if j%10==0:
                name = newusername+'_'+str(i)+'.jpg'
                cv2.imwrite(userimagefolder+'/'+name,frame[y:y+h,x:x+w])
                i+=1
            j+=1
        if j==1000:
            break
        cv2.imshow('Adding new User',frame)
        if cv2.waitKey(1)==27:
            break
    cap.release()
    cv2.destroyAllWindows()
    print('Training Model')
    train_model()

how do you expect to connect a webcam there, and will there be any gui at all ?

(this seems like the typical noob pitfall)

this connects to your local usb webcam, NOT to the ip address above

did you ? it’s still detecting faces (which is probably your main cpu load)

I will connect an ip camera, not a webcam. I got it to skip the last frame. Thus, he reads faces. the model i am using is “‘res10_300x300_ssd_iter_140000.caffemodel’”

This was the fastest model I found among them. just sometimes misunderstood. For example, it can define a shoe as a face and give it a name. I set a smile requirement for this. Even if he detects that there is a face without a smile, he will not take attendance.

ip2 = 'rtsp://admin:@10.29.214.10'

    cap = cv2.VideoCapture(ip2)
    cap.set(cv2.CAP_PROP_FPS, 60)
    cap.set(cv2.CAP_PROP_BUFFERSIZE, 0)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
    ret = True
    frame_count = 0
    while ret:
        ret, frame = cap.read()
        frame_count += 1
        if frame_count % 10 != 0:
            continue

        faces = extract_faces(frame)
        if len(faces) > 0:
            (x, y, w, h) = faces[0]
            cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 20), 2)
            face_roi = cv2.resize(frame[y:y+h, x:x+w], (50, 50))

            # Yüzü tanıma işlemi
            identified_person = identify_face(face_roi.reshape(1, -1))[0]

            # Yüz üzerinde gülümseme tespiti
            roi_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)[y:y+h, x:x+w]
            smiles = smile_cascade.detectMultiScale(roi_gray, scaleFactor=1.5, minNeighbors=20)

            if len(smiles) > 0:
                # Tanınan kişinin adını yazdırma
                cv2.putText(frame, f'{identified_person}', (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 20), 2,
                            cv2.LINE_AA)

                # Tespit edilen gülümsemelerin etrafına dikdörtgen çizme
                for (sx, sy, sw, sh) in smiles:
                    cv2.rectangle(frame, (x+sx, y+sy), (x+sx+sw, y+sy+sh), (0, 255, 0), 2)

                # Yoklama alma işlemi
                add_attendance(identified_person)

the problem is still not resolved