示例#1
0
    def _process_image_request(self, conn, msg_data):
        """
        Deserialization protobuf and process display image request
        Args:
            conn: a socket connection
            msg_data: a protobuf struct, include image request.

        Returns:

        protobuf structure like this:
         ------------------------------------
        |data          |    byts             |
        |------------------------------------
        |width         |    uint32           |
        |------------------------------------
        enum ImageFormat {
            kImageFormatJpeg = 0;
        }
        """
        request = pb2.PresentImageRequest()
        response = pb2.PresentImageResponse()

        # Parse msg_data from protobuf
        try:
            request.ParseFromString(msg_data)
        except DecodeError:
            logging.error("ParseFromString exception: Error parsing message")
            err_code = pb2.kPresentDataErrorOther
            return self._response_image_request(conn, response, err_code)

        sock_fileno = conn.fileno()
        handler = self.channel_manager.get_channel_handler_by_fd(sock_fileno)
        if handler is None:
            logging.error("get channel handler failed")
            err_code = pb2.kPresentDataErrorOther
            return self._response_image_request(conn, response, err_code)

        rectangle_list = []
        if request.rectangle_list:
            for one_rectangle in request.rectangle_list:
                rectangle = []
                rectangle.append(one_rectangle.left_top.x)
                rectangle.append(one_rectangle.left_top.y)
                rectangle.append(one_rectangle.right_bottom.x)
                rectangle.append(one_rectangle.right_bottom.y)
                rectangle.append(one_rectangle.label_text)
                # add the detection result to list
                rectangle_list.append(rectangle)

        handler.save_image(request.data, request.width, request.height,
                           rectangle_list)
        return self._response_image_request(conn, response,
                                            pb2.kPresentDataErrorNone)
def response_image_request(client):
    """func"""
    try:
        message_head = client.recv(5)
        s = struct.Struct('IB')
        (message_len, messagename_len) = s.unpack(message_head)
        message_len = socket.ntohl(message_len)
        client.recv(messagename_len)
        message_body = client.recv(message_len - 5 - messagename_len)
        image_request_response = pb.PresentImageResponse()
        image_request_response.ParseFromString(message_body)
        if image_request_response.error_code == PB_PRESENT_DATA_ERROR_NONE:
            return True
        return image_request_response.error_code

    except socket.error:
        return False
    def _process_image_request(self, conn, msg_data):
        """
        Deserialization protobuf and process image_request
        Args:
            conn: a socket connection
            msg_data: a protobuf struct, include image request.

        Returns:

        protobuf structure like this:
         ------------------------------------
        |format        |    ImageFormat      |
        |------------------------------------
        |width         |    uint32           |
        |------------------------------------
        |height        |    uint32           |
        |------------------------------------
        |data          |    bytes            |
         ------------------------------------
        enum ImageFormat {
            kImageFormatJpeg = 0;
        }
        """
        request = pb2.PresentImageRequest()
        response = pb2.PresentImageResponse()

        # Parse msg_data from protobuf
        try:
            request.ParseFromString(msg_data)
        except DecodeError:
            logging.error("ParseFromString exception: Error parsing message")
            err_code = pb2.kPresentDataErrorOther
            return self._response_image_request(conn, response, err_code)

        sock_fileno = conn.fileno()
        handler = self.channel_manager.get_channel_handler_by_fd(sock_fileno)
        if handler is None:
            logging.error("get channel handler failed")
            err_code = pb2.kPresentDataErrorOther
            return self._response_image_request(conn, response, err_code)

        # Currently, image format only support jpeg
        if request.format != pb2.kImageFormatJpeg:
            logging.error("image format %s not support", request.format)
            err_code = pb2.kPresentDataErrorUnsupportedFormat
            return self._response_image_request(conn, response, err_code)

        rectangle_list = []
        if request.rectangle_list:
            for one_rectangle in request.rectangle_list:
                rectangle = []
                rectangle.append(one_rectangle.left_top.x)
                rectangle.append(one_rectangle.left_top.y)
                rectangle.append(one_rectangle.right_bottom.x)
                rectangle.append(one_rectangle.right_bottom.y)
                rectangle.append(one_rectangle.label_text)
                # add the detection result to list
                rectangle_list.append(rectangle)

        imageData = request.data[:1382400]
        maskData = request.data[-100352:]

        # convert yuv420sp(nv12) to jpg
        yuvNum = len(
            [lists for lists in os.listdir('yuv') if lists.endswith(".yuv")])
        with open(os.path.join('yuv', str(yuvNum) + '.yuv'), 'wb') as f:
            f.write(imageData)
        ff = ffmpy.FFmpeg(
            inputs={
                os.path.join('yuv',
                             str(yuvNum) + '.yuv'):
                '-s 1280*720 -pix_fmt nv12'
            },
            outputs={os.path.join('yuv',
                                  str(yuvNum) + '.jpg'): None})
        ff.run()

        # read data
        imgNow = cv2.imread(os.path.join('yuv', str(yuvNum) + '.jpg'))
        background = cv2.imread("background.jpg")

        maskarr = np.fromstring(maskData, np.uint8).astype(np.float32)
        maskarr = np.reshape(maskarr, (224, 224, 2))
        alphargb = cv2.resize(maskarr[:, :, 1],
                              (request.width, request.height))

        # output the mask to compare result with the output of pb model
        cv2.imwrite(os.path.join('yuv', str(yuvNum) + 'alpha.jpg'), alphargb)

        alphargb = alphargb / 255
        alphargb = np.repeat(alphargb[..., np.newaxis], 3, 2)
        result = np.uint8(imgNow * alphargb + background * (1 - alphargb))

        img_decode = cv2.imencode('.jpg', result)[1].tostring()
        handler.save_image(img_decode, request.width, request.height,
                           rectangle_list)
        return self._response_image_request(conn, response,
                                            pb2.kPresentDataErrorNone)