API in pyobjc-framework-Vision always return an empty result after running for a while

31 Views Asked by At

I have a piece of code for obtaining the rectangular area of a person. Assuming that there are 1000 images with dimension 192x256x3(RGB), it works perfectly in a short period, for example, 257 images with dimension 192x256x3(rgb). However, performRequests_onCIImage_error_ always return an empty result for the next images. Note that the issue still exists if I change the set of image.

I am new to PyObjc and have no idea how to debug it. Help me!

import Quartz
import Vision


class Device:

    def __init__(self):
        # Init Vision request
        self.person_segmentation_request = Vision.VNGeneratePersonSegmentationRequest.alloc().init()
        self.person_segmentation_request.setQualityLevel_(Vision.VNGeneratePersonSegmentationRequestQualityLevelFast)
        self.person_rectangle_request = Vision.VNDetectHumanRectanglesRequest.alloc().init()
        self.person_rectangle_request.setUpperBodyOnly_(False)
        self.person_rectangle_request.setRevision_(Vision.VNDetectHumanRectanglesRequestRevision2)
        self.requestHandler = Vision.VNSequenceRequestHandler.alloc().init()
        self.general_info = {"person_segment_cost": 0.0, "person_rectangle_cost": 0.0}

    def person_rectangle(self, image):
        start_time = time.time()
        # Make sure that image is a matrix  consisting of 3-Dimensions: H x W x RGB/...
        # Create a CIImage for the given image
        h, w, b = image.shape
        image = np.uint8(np.dstack((image, np.ones((h, w)))))
        h, w, b = image.shape
        cgsize = Quartz.CGSize(width=w, height=h)
        colorSpace = Quartz.CGColorSpaceCreateWithName(Quartz.kCGColorSpaceSRGB)
        ciimage = Quartz.CIImage.alloc().initWithBitmapData_bytesPerRow_size_format_colorSpace_(
            image.tobytes(),  # data
            w * b,  # bytesPerRow
            cgsize,  # size
            Quartz.kCIFormatRGBA8,  # format
            colorSpace  # colorSpace
        )
        # Person Segmentation
        self.requestHandler.performRequests_onCIImage_error_([self.person_rectangle_request], ciimage, None)
        results = self.person_rectangle_request.results()

        # For multiple persons
        rects = []

        for result in results:
            cgrect = result.boundingBox()
            rx = int(cgrect.origin.x * w)
            ry = int(cgrect.origin.y * h)
            rw = int(cgrect.size.width * w)
            rh = int(cgrect.size.height * h)
            # Transform to python coordinate
            ry = h - (ry + rh)
            rect = (rx, ry, rw, rh)
            rects.append(rect)

        # General info: performance
        self.general_info["person_rectangle_cost"] = time.time() - start_time
        print("person_rectangle_cost: {}".format(self.general_info["person_rectangle_cost"]))
        return rects

I try to manually delete the variables returned from the api but the issue remains same. Besides, the memory monitor shows the memory is not full.

0

There are 0 best solutions below