1

I got a big problem at python code, my code is work correct but my laptop's RAM cant erase unused data, I used 'gc' library but it doesn't any help, this problem went appear that I change it to class mode.

my code:

import cv2 as cv
import mediapipe as mp
import time
import gc

class handDetector():
    def __init__(self, mode=False, maxHands=2, detectionCon=0.5, trackCon=0.5):
        self.mode = mode
        self.maxHands = maxHands
        self.detectionCon = detectionCon
        self.trackCon = trackCon
        self.mpHands = mp.solutions.hands
        self.hands = self.mpHands.Hands(self.mode, self.maxHands, 1, self.detectionCon, self.trackCon)
        self.mpDraw = mp.solutions.drawing_utils
    def findHands(self, image, draw=True):
        imgRGB = cv.cvtColor(image, cv.COLOR_BGR2RGB)
        self.result = self.hands.process(imgRGB)

        if self.result.multi_hand_landmarks:
            for hand in self.result.multi_hand_landmarks:
                if draw:
                    self.mpDraw.draw_landmarks(image, hand, self.mpHands.HAND_CONNECTIONS)
                     cv.FILLED)
        return image
    def findPosition(self, image, handNo=0, draw=True):
        lmList = []
        h, w, c = image.shape
        if self.result.multi_hand_landmarks:
            myHand = self.result.multi_hand_landmarks[handNo]
            for id, land in enumerate(myHand.landmark):
                cx, cy = int(land.x * w), int(land.y * h)
                lmList.append([id, cx, cy])
                if draw:
                    cv.circle(image, (cx,cy), 15, (0,0,255), cv.FILLED)
        return lmList


def main():
    cap = cv.VideoCapture(0)
    ret=1   
    pre_time = time.time()
    gc.enable()
    while ret:
        ret, frame = cap.read()
        crt_time = time.time()
        fps = int(1/(crt_time-pre_time))
        handDtc = handDetector()
        image = handDtc.findHands(frame)
        lmList = handDtc.findPosition(image) 
        cv.putText(image, str(fps), (50,50), cv.FONT_HERSHEY_COMPLEX, 1, (255,255,0))        
        image=frame
        cv.imshow('image', image)
        pre_time = time.time()     
        if cv.waitKey(1) == ord('q'):
            ret = False
            # print(lmList)
        del handDtc
        del image
        gc.collect()
if __name__ == "__main__":
    main()

 

my RAM storage is 16gb and I have 11gb free, after running the code my RAM will full

1
  • I founded, I have to remove handDtc = handDetector() inside the while to the outside. Commented Oct 29, 2021 at 9:23

1 Answer 1

1

I have to remove handDtc = handDetector() inside the while to the outside. this line make many class object and it will full my RAM.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.