Exemplo n.º 1
0
    def add_region(self,
                   x,
                   y,
                   w,
                   h,
                   label_id: int,
                   confidence: float = 0.0,
                   region_tensor: Gst.Structure = None,
                   normalized: bool = False) -> RegionOfInterest:
        if normalized:
            x = int(x * self.video_info().width)
            y = int(y * self.video_info().height)
            w = int(w * self.video_info().width)
            h = int(h * self.video_info().height)

        if not self.__is_bounded(x, y, w, h):
            x_init, y_init, w_init, h_init = x, y, w, h
            x, y, w, h = self.__clip(x, y, w, h)
            Gst.debug(
                "ROI coordinates [x, y, w, h] are out of image borders and will be clipped: [{}, {}, {}, {}] -> "
                "[{}, {}, {}, {}]".format(x_init, y_init, w_init, h_init, x, y,
                                          w, h))

        label = self.__get_label_by_label_id(region_tensor, label_id)

        video_roi_meta = GstVideo.buffer_add_video_region_of_interest_meta(
            self.__buffer, label, x, y, w, h)

        if not region_tensor:
            region_tensor = Gst.Structure.new_empty("detection")
        else:
            region_tensor.set_name(
                "detection")  # make sure we're about to add detection Tensor

        region_tensor.set_value('label_id', label_id)
        region_tensor.set_value('confidence', confidence)
        region_tensor.set_value('x_min', x / self.video_info().width)
        region_tensor.set_value('x_max', (x + w) / self.video_info().width)
        region_tensor.set_value('y_min', y / self.video_info().height)
        region_tensor.set_value('y_max', (y + h) / self.video_info().height)

        self.__regions.append(
            RegionOfInterest(
                ctypes.cast(
                    hash(video_roi_meta),
                    ctypes.POINTER(VideoRegionOfInterestMeta)).contents))
        self.__regions[-1].add_tensor(tensor=region_tensor)
        return self.__regions[-1]
Exemplo n.º 2
0
    def add_region(self,
                   x,
                   y,
                   w,
                   h,
                   label: str = "",
                   confidence: float = 0.0,
                   normalized: bool = False) -> RegionOfInterest:
        if normalized:
            x = int(x * self.video_info().width)
            y = int(y * self.video_info().height)
            w = int(w * self.video_info().width)
            h = int(h * self.video_info().height)

        if not self.__is_bounded(x, y, w, h):
            x_init, y_init, w_init, h_init = x, y, w, h
            x, y, w, h = self.__clip(x, y, w, h)
            warn(
                "ROI coordinates [x, y, w, h] are out of image borders and will be clipped: [{}, {}, {}, {}] -> "
                "[{}, {}, {}, {}]".format(x_init, y_init, w_init, h_init, x, y,
                                          w, h),
                stacklevel=2)

        video_roi_meta = GstVideo.buffer_add_video_region_of_interest_meta(
            self.__buffer, label, x, y, w, h)
        roi = RegionOfInterest(
            ctypes.cast(hash(video_roi_meta),
                        ctypes.POINTER(VideoRegionOfInterestMeta)).contents)

        tensor = roi.add_tensor("detection")
        tensor['confidence'] = float(confidence)
        tensor['x_min'] = float(x / self.video_info().width)
        tensor['x_max'] = float((x + w) / self.video_info().width)
        tensor['y_min'] = float(y / self.video_info().height)
        tensor['y_max'] = float((y + h) / self.video_info().height)

        return roi