Exemplo n.º 1
0
def to_pb_image(input_image, encode_format='.jpeg', compression_level=0.8):
    """Converts a numpy array image to ProtoBuf Object.
    Args:
        input_image (Numpy Array): image
        encode_format (str): standard .jpeg
        compression_level (float): standard 0.8
    Returns:
        Image (Image): Protobuf object from is_msgs.
    """
    if isinstance(input_image, np.ndarray):
        if encode_format == '.jpeg':
            params = [
                cv2.IMWRITE_JPEG_QUALITY,
                int(compression_level * (100 - 0) + 0)
            ]
        elif encode_format == '.png':
            params = [
                cv2.IMWRITE_PNG_COMPRESSION,
                int(compression_level * (9 - 0) + 0)
            ]
        else:
            return Image()
        cimage = cv2.imencode(ext=encode_format,
                              img=input_image,
                              params=params)
        return Image(data=cimage[1].tobytes())
    elif isinstance(input_image, Image):
        return input_image
    else:
        return Image()
def get_pb_image(input_image, encode_format='.jpeg', compression_level=0.8):
    if isinstance(input_image, np.ndarray):
        if encode_format == '.jpeg':
            params = [cv2.IMWRITE_JPEG_QUALITY, int(compression_level * (100 - 0) + 0)]
        elif encode_format == '.png':
            params = [cv2.IMWRITE_PNG_COMPRESSION, int(compression_level * (9 - 0) + 0)]
        else:
            return Image()        
        cimage = cv2.imencode(ext=encode_format, img=input_image, params=params)
        return Image(data=cimage[1].tobytes())
    elif isinstance(input_image, Image):
        return input_image
    else:
        return Image()
Exemplo n.º 3
0
    def grab_image(self):
        with self.lock:
            diff = self.deadline - time.time()

        if diff > 0:
            time.sleep(diff)

        with self.lock:
            if diff < 0:
                self.deadline = time.time()

            before_get = time.time()
            frame = self.video.getImageRemote(self.camera)

            proc_begin = time.time()

            width, height, buffer = frame[0], frame[1], frame[6]
            mat = np.frombuffer(buffer,
                                dtype=np.uint8).reshape(height, width, -1)
            if self.color_space == ColorSpaces.Value("GRAY"):
                mat = cv2.cvtColor(mat, cv2.COLOR_BGR2GRAY)
            image = cv2.imencode(ext=self.encode_format,
                                 img=mat,
                                 params=self.encode_parameters)

            proc_end = time.time()

            self.logger.debug(
                "[GrabImage] New Frame (get={:.1f}ms, proc={:.1f}ms, next_in={:.1f}ms)",
                (proc_begin - before_get) * 1000,
                (proc_end - proc_begin) * 1000, diff * 1000)
            self.deadline += 1.0 / self.fps

        return Image(data=image[1].tobytes())
if len(sys.argv) > 1:
    broker_uri = sys.argv[1]

channel = Channel(broker_uri)
subscription = Subscription(channel)
exporter = ZipkinExporter(
    service_name='SkeletonsDetectorRequester',
    host_name='localhost',
    port=9411,
    transport=BackgroundThreadTransport(max_batch_size=100),
)

image = cv2.imread('../image.png')

tracer = Tracer(exporter)
with tracer.span(name='image') as span:
    cimage = cv2.imencode(ext='.jpeg', img=image, params=[cv2.IMWRITE_JPEG_QUALITY, 80])
    data = cimage[1].tobytes()
    im = Image(data=data)
    msg = Message(content=im, reply_to=subscription)
    msg.inject_tracing(span)
    channel.publish(message=msg, topic='SkeletonsDetector.Detect')

    cid = msg.correlation_id
    while True:
        msg = channel.consume()
        if msg.correlation_id == cid:
            skeletons = msg.unpack(ObjectAnnotations)
            print(skeletons)
            sys.exit(0)