Detectmultiscale need a long time to process

I found in python image processing detectMultiScale takes a long time to process until the video shows up in browser. took 1 minute 30 seconds with detectMultiScale and took 20 seconds without detectMultiScale. how can i still show frame in browser while doing detectMultiScale process to reduce time?
here function show frame video

faceCascade = cv2.CascadeClassifier("C:\\xampp2\\htdocs\\opencv\\webcam\\resources\\haarcascade_frontalface_default.xml")
clf = cv2.face.LBPHFaceRecognizer_create()
clf.read("C:\\xampp2\\htdocs\\opencv\\webcam\\classifier.xml")

cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
cap.set(3,640)
cap.set(4,480)
cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)
cap.set(5,2)
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))
cap.set(7,12)
cap.set(cv2.CAP_PROP_AUTOFOCUS, 0)
while True:
        ret, img = cap.read()
        while ret == False:
            time. sleep(5)
            print('Camera not ready!')
            cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
            cap.set(3,640)
            cap.set(4,480)
            cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)
            cap.set(5,2)
            cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))
            cap.set(7,12)
            cap.set(cv2.CAP_PROP_AUTOFOCUS, 0)
            ret, img = cap.read()
        #below is function for image recognition
        img = draw_boundary(img, faceCascade, 1.2, 5, (60, 205, 250), "Face", clf)
        
        cv2.normalize(img, img, 0, 255, cv2.NORM_MINMAX)
        frame = cv2.imencode('.jpg', img)[1].tobytes()
        #below send frame to browser template
        yield (b'--frame\r\n'
             b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

here feature detectmultiscale

    features = classifier.detectMultiScale(
        gray_image,
        scaleFactor=1.2,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.CASCADE_SCALE_IMAGE
    )
    

is there any other solution if this can’t be done, for example increasing the performance of detectmultiscale in processing so it can be faster?

MRE required. try serving a static picture, with no processing at all. if that takes time already, it’s not OpenCV.

are you dealing with multiple cameras?

please get rid of those magic constants in the VideoCapture::set calls. there are named constants for that, cv.CAP_PROP_...

since this is also a flask problem, you might not get much help here because flask isn’t made to work with OpenCV.