RAM Leak Memory (OpenCV/Olympe) issue

Hi
I need to stream image to Computer Vision.
My computer is 32GB RAM.
When process has self.model & self.model.run, the process works but is killed after about 360 seconds with the reason that there is no more memory available .
But process does’t has self.model & self.model.run, memory also continuously rising.

import olympe
from olympe.messages.ardrone3.PilotingState import PositionChanged
#from olympe.messaage.ardrone3.Pilotin4g import TakeOff, moveBy, Landing, moveTo, NavigateHome
import threading
import time
import queue
import cv2
import os
from Mydetect import Detect
import argparse
import gc



class OlympeStreaming():
    def __init__(self, drone, opt):
        self.drone = drone
        self.frame_queue = queue.Queue(3)
        self.frame_num = 0
        self.renderer = None
        self.opt = opt
        #self.model = Detect(opt=self.opt)

    def start(self):
        self.drone.streaming.set_callbacks(
            raw_cb=self.yuv_frame_cb,
            h264_cb=self.h264_frame_cb,
            start_cb=self.start_cb,
            end_cb=self.end_cb,
            flush_raw_cb=self.flush_cb,
        )
        self.drone.streaming.start()

    def stop(self):
        if self.renderer is not None :
            self.renderer.stop()
        self.drone.streaming.stop()

    def yuv_frame_cb(self, yuv_frame):
        """
        This function will be called by Olympe for each decoded YUV frame.
            :type yuv_frame: olympe.VideoFrame
        """
        print('refrefrefrefrefrefrefrefrefrefrefref')
        yuv_frame.ref()
        if self.frame_queue.empty(): #20221027
            self.frame_queue.put(yuv_frame, timeout=0.1)
        else:
            print('hahahahahahahahahahahahahah')
            yuv_frame.unref()
    
    def flush_cb(self, stream):
        if stream["vdef_format"] != olympe.VDEF_I420:
            return True
        while not self.frame_queue.empty():
            print('ccccccccccccccccccccccccccccccc')
            self.frame_queue.get_nowait().unref()
        return True


    def start_cb(self):
        pass
    def end_cb(self):
        pass
    def h264_frame_cb(self, h264_frame):
        pass

    def display_frame(self, yuv_frame):
        # the VideoFrame.info() dictionary contains some useful information
        # such as the video resolution
        info = yuv_frame.info()

        height, width = (  # noqa
            info["raw"]["frame"]["info"]["height"],
            info["raw"]["frame"]["info"]["width"],
        )
        # yuv_frame.vmeta() returns a dictionary that contains additional
        # metadata from the drone (GPS coordinates, battery percentage, ...)
        # convert pdraw YUV flag to OpenCV YUV flag

        
        cv2_cvt_color_flag = {
            olympe.VDEF_I420: cv2.COLOR_YUV2BGR_I420,
            olympe.VDEF_NV12: cv2.COLOR_YUV2BGR_NV12,
        }[yuv_frame.format()]

        #yuv_frame.as_ndarray() is a 2D numpy with the proper "shape"
        # i.e (3 * height / 2, width) because it's a YUV I420 or NV12 frame
        GPS = self.drone.get_state(PositionChanged)
        cv2frame = cv2.cvtColor(yuv_frame.as_ndarray(), cv2_cvt_color_flag)
        #self.model.run(image=cv2frame, GPS = GPS)
        
        #cv2.imshow("frames via Olympe", cv2frame)
        #cv2.waitKey(1)
        del GPS
        del cv2frame
        #cv2.destroyAllWindows()

    def run(self):
        #main_thread = next(filter(lambda t : t.name == "MainThread", threading.enumerate()))
        
        while True:
            try:
                yuv_frame = self.frame_queue.get(timeout=0.01)
            except queue.Empty:
                continue
            try:
                self.display_frame(yuv_frame)
            except Exception as e:
                print(e)
            finally:
                yuv_frame.unref()
                
                    

#logger = logging.getLogger(name)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--weights', nargs='+', type=str, default='yolov7.pt', help='model.pt path(s)')
    parser.add_argument('--source', type=str, default='inference/images', help='source')  # file/folder, 0 for webcam
    parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
    parser.add_argument('--conf-thres', type=float, default=0.25, help='object confidence threshold')
    parser.add_argument('--iou-thres', type=float, default=0.45, help='IOU threshold for NMS')
    parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
    parser.add_argument('--view-img', action='store_true', help='display results')
    parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
    parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
    parser.add_argument('--nosave', action='store_true', help='do not save images/videos')
    parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --class 0, or --class 0 2 3')
    parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
    parser.add_argument('--augment', action='store_true', help='augmented inference')
    parser.add_argument('--update', action='store_true', help='update all models')
    parser.add_argument('--project', default='runs/detect', help='save results to project/name')
    parser.add_argument('--name', default='exp', help='save results to project/name')
    parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
    parser.add_argument('--no-trace', action='store_true', help='don`t trace model')
    opt = parser.parse_args()
    print(opt)

    
    DRONE_IP = os.environ.get("DRONE_IP", "192.168.53.1")
    drone = olympe.Drone(DRONE_IP)
    drone.connect()

    streamer = OlympeStreaming(drone=drone, opt=opt)
    streamer.start()
    print('Hi')
    streamer.run()
    #from pynput import keyboard
    #while key == keyboard.Key.esc:
      ##streamer.stop()
      #print("stop")
 
        #streamer.stop()
    time.sleep(10000)
    streamer.stop()
    drone.disconnect()

How to solve?

Regards,
Tom

This topic was automatically closed after 30 days. New replies are no longer allowed.