Search code examples
opencvmachine-learningcomputer-visionface-recognitionimage-recognition

How do I improve my OpenCV recognition code to find upper body and also cut down on resources while having a CCTV stream as input?


How do I make my opencv recognition more accurate, I was just using haarcascade to find the upper body in a gray frame of my cctv, but it is taking a lot of resources and isn't giving a good enough result. Should I use outlines or something to cut down on the resources? I also have a GPU but I don't know how to link it so that my computation uses my GPU.

import cv2


body_casc = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_upperbody.xml')
vcap= cv2.VideoCapture("rtsp://192.168.29.99")

while True:
    try:
        
        ret,frame=vcap.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        body = body_casc.detectMultiScale(gray,1.1,4)

        for (x,y,w,h) in body:
            cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),5)

        cv2.imshow("VIDEO",frame)
    except Exception as e:
        print("ERROR : "+str(e))

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

    

cv2.destroyAllWindows()

And my camera is connecting through IPC to my CCTV and after sometime it just stops giving out the frames, and returns this error.

error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

Solution

  • Lets start from the end to beginning.

    error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

    The error comes up because there is no available frame to convert. This can be caused by camera disconnection or non-appropriate frames. I suggest you to check the any frame before processing:

        ret,frame=vcap.read()
    
        # if frame is read correctly ret is True
        if not ret:
               print("Can't receive frame (stream end?). Exiting ...")
               break
    

    On the other hand, you are asking about how to make detection more accurate. First of all, haarcascade is not a perfect tool you can use instead of it is really faster. According to your hardware specs, you may choose other types of AI algorithm such as YOLO,R-CNN etc. Or if you insist on using haarcascades, you should define the parameters according to your situations. You can check the parameters which detectMultiscale includes.