old_contents: #!/usr/bin/python3
  # Copyright 2022 Google LLC
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  import base64
  import requests
  import numpy as np
  import scipy.ndimage
  import scipy.misc
  
  from PIL import Image
  
  _HEATMAP_MASK_LENGTH = 21
  
  def get_field_or_die(data, field_name):
      if field_name not in data:
          print(f'Unable to parse {field_name} from data: {data}\r\n')
          exit(1)
      return data[field_name]
  
  def main():
      url = "http://10.10.10.1:80/jsonrpc"
  
      payload = {
          "method": "run_bodypix",
          "jsonrpc": "2.0",
          "id": 0,
      }
  
      # Unpack the JSON result
      response = requests.post(url, json=payload).json()
      result = get_field_or_die(response, 'result')
  
      # Get the image size
      width = get_field_or_die(result, 'width')
      height = get_field_or_die(result, 'height')
  
      # Decode the image and mask data
      image_data_base64 = get_field_or_die(result, 'base64_data')
      image_data = base64.b64decode(image_data_base64)
      im = Image.frombytes('RGB', (width, height), image_data, 'raw')
  
      mask_segments_base64 = get_field_or_die(result, 'output_mask1')
      mask_segments_data = base64.b64decode(mask_segments_base64)
  
      predicted_mask = np.frombuffer(
          mask_segments_data, dtype=np.uint8, count=-1)
  
      # Match the dimensions of the original tensor
      predicted_mask = predicted_mask.reshape(21, 21)
  
      # Blow up the heatmap to match the image size, and turn it red
      rgb_heatmap = np.dstack([predicted_mask[:,:]]*3)
      rgb_heatmap[:,:,1:] = 0
  
      rescale_factor = [
        width/_HEATMAP_MASK_LENGTH,
        height/_HEATMAP_MASK_LENGTH,
        1]
      rgb_heatmap = scipy.ndimage.zoom(rgb_heatmap, rescale_factor, order=0)
  
      heatmap_im = Image.fromarray(rgb_heatmap)
  
      # Display the input image and segmentation results.
      output_img = Image.new('RGB', (2 * width, height))
      output_img.paste(im, (0, 0))
      output_img.paste(heatmap_im, (width, 0))
      output_img.show()
  
  if __name__ == '__main__':
      main()
  
new_contents: #!/usr/bin/python3
  # Copyright 2022 Google LLC
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  import base64
  import requests
  import math
  import numpy as np
  
  from PIL import Image
  
  _IMAGE_SIZE = 324
  _HEATMAP_MASK_LENGTH = 21
  
  def get_field_or_die(data, field_name):
      if field_name not in data:
          print(f'Unable to parse {field_name} from data: {data}\r\n')
          exit(1)
      return data[field_name]
  
  def main():
      url = "http://10.10.10.1:80/jsonrpc"
  
      payload = {
          "method": "run_bodypix",
          "jsonrpc": "2.0",
          "id": 0,
      }
  
      # Unpack the JSON result
      response = requests.post(url, json=payload).json()
      result = get_field_or_die(response, 'result')
  
      # Get the image size
      width = get_field_or_die(result, 'width')
      height = get_field_or_die(result, 'height')
  
      # Decode the image and mask data
      image_data_base64 = get_field_or_die(result, 'base64_data')
      image_data = base64.b64decode(image_data_base64)
      im = Image.frombytes('RGB', (width, height), image_data, 'raw')
  
      mask_segments_base64 = get_field_or_die(result, 'output_mask1')
      mask_segments_data = base64.b64decode(mask_segments_base64)
  
      predicted_mask = np.frombuffer(
          mask_segments_data, dtype=np.uint8, count=-1)
  
      # Match the dimensions of the original tensor
      predicted_mask = predicted_mask.reshape(_HEATMAP_MASK_LENGTH, _HEATMAP_MASK_LENGTH)
  
      # Turn the heatmap red
      rgb_heatmap = np.dstack([predicted_mask[:,:]]*3)
      rgb_heatmap[:,:,1:] = 0
  
      # Assume image is square, current model is for 324x324 images
      rescale_factor = math.ceil(width/_HEATMAP_MASK_LENGTH)
  
      # Blow up the heatmap to match the image size
      rgb_heatmap = np.kron(rgb_heatmap, np.ones((rescale_factor, rescale_factor, 1)))
  
      # Blown up version using ceiling, so clip the edges down to the exact size.
      clip_size = int((rgb_heatmap.shape[0] - _IMAGE_SIZE)/2)
      rgb_heatmap = rgb_heatmap[clip_size:_IMAGE_SIZE+clip_size, clip_size:_IMAGE_SIZE+clip_size]
  
      heatmap_im = Image.fromarray(rgb_heatmap.astype('uint8'))
  
      # Display the input image and segmentation results.
      output_img = Image.new('RGB', (2 * width, height))
      output_img.paste(im, (0, 0))
      output_img.paste(heatmap_im, (width, 0))
      output_img.show()
  
  if __name__ == '__main__':
      main()
  
subject: remove scipy dependency from bodypix