Face recognition ip camera latency 6-10 ms

hello,
I am new to OpenCV. The code below shows the latency of of ip camera stream for face recognition project. Can anybody help me improve this?

Face Recognition

os.environ[“OPENCV_FFMPEG_CAPTURE_OPTIONS”] = “rtsp_transport;udp”
def TrackImages():
assure_path_exists(“Attendance/”)
assure_path_exists(“EmployeeDetails/”)
i = 0
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read(“TrainingImageLabel\Trainner.yml”)
harcascadePath = “haarcascade_frontalface_default.xml”
faceCascade = cv2.CascadeClassifier(harcascadePath)

# df = pd.read_csv("EmployeeDetails\EmployeeDetails.csv")

#cam = cv2.VideoCapture(0)
cam = cv2.VideoCapture('rtsp://admin:q1w2e3r4@172.30.80.154:554/Streaming/Channels/101')  # ipcamera
font = cv2.FONT_HERSHEY_COMPLEX

a = cam.get(cv2.CAP_PROP_BUFFERSIZE)
cam.set(cv2.CAP_PROP_BUFFERSIZE, 3)
start_frame_number = 30
cam.set(cv2.CAP_PROP_POS_FRAMES, start_frame_number)

print("buffer" + str(a))


col_names = ['Id', 'Name', 'Date', 'Time']
exists1 = os.path.isfile("EmployeeDetails\EmployeeDetails.csv")
if exists1:
    df = pd.read_csv("EmployeeDetails\EmployeeDetails.csv")
else:
    # mess._show(title='Details Missing', message='Employees details are missing, please check!')
    cam.release()
    cv2.destroyAllWindows()
    window.destroy()

# attendance = pd.DataFrame(columns=col_names)

while True:
    ret, im = cam.read()
    gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(gray, 1.2, 5)
    for (x, y, w, h) in faces:
        cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), 2)
        Id, conf = recognizer.predict(gray[y:y + h, x:x + w])
        if (conf < 50):
            ts = time.time()
            date = datetime.datetime.fromtimestamp(ts).strftime('%d-%m-%Y')
            timeStamp = datetime.datetime.fromtimestamp(ts).strftime('%H:%M')
            # aa = df.loc[df['SERIAL NO.'] == Id]['NAME'].values
            aa = df.loc[df['Id'] == Id]['Name'].values
            # ID = df.loc[df['Id'] == Id]['ID'].values
            # ID = str(ID)
            # ID = ID[1:-1]
            bb = str(aa)
            bb = bb[2:-2]
            attendance = [str(Id), bb, str(date), str(timeStamp)]
        else:
            Id = 'Unknown'
            cv2.rectangle(im, (x, y), (x + w, y + h), (0, 0, 255), 3)
            bb = str(Id)
        cv2.putText(im, str(bb), (x, y + h), font, 1, (255, 255, 255), 2)

    #attendance = attendance.drop_duplicates(subset=['Id'], keep='first')
    
    cv2.imshow('Taking Attendance', im)

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


ts = time.time()
date = datetime.datetime.fromtimestamp(ts).strftime('%d-%m-%Y')
exists = os.path.isfile("Attendance\Attendance_" + date + ".csv")
if exists:
    with open("Attendance\Attendance_" + date + ".csv", 'a+') as csvFile1:
        writer = csv.writer(csvFile1)
        writer.writerow(attendance)
    csvFile1.close()
else:
    with open("Attendance\Attendance_" + date + ".csv", 'a+') as csvFile1:
        writer = csv.writer(csvFile1)
        writer.writerow(col_names)
        writer.writerow(attendance)
    csvFile1.close()


csvFile1.close()

cam.release()
cv2.destroyAllWindows()



res = "Attendance Updated"
message.configure(text=res)
playsound('Thank-you-Your-attendance-updated.mp3')
cam.release()
cv2.destroyAllWindows()
# print(attendance)
res = attendance
message2.configure(text=res)

welcome.

define “improve”. improve in what way? what’s the current state, what do you want to achieve, and do you think that’s possible?

I need to stream faster, without latency

why do you need this latency reduced?

6-10 ms are less than one hundredth of a second. what are you doing that needs this eliminated?

you can wish for “without latency” all you want but you won’t get it. latency can rarely be eliminated. you’re dealing with compressed video streams over a network. you should be very satisfied to only have 6-10 milliseconds, and not more.

your first step is to profile everything. find out where that time is spent.

Oops I was incorrect about milliseconds. Its seconds. Stream like slow motion.