<jupyter_start><jupyter_text><jupyter_code># Modules to use
import numpy as np
import tensorflow as tf
import cv2 
from matplotlib import pyplot as plt
#load my drive
from google.colab import drive
drive.mount('/content/drive')
# Load the model
interpreter = tf.lite.Interpreter("/content/drive/My Drive/innopolis/1st year courses/2nd term/CV/GP/posenet.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()   # the input shape
output_details = interpreter.get_output_details()   # the output shape
floating_model = input_details[0]['dtype'] == np.float32  # type of output
# Load the image
def load_image(image):
    """reshape and convert image to fit the model"""
    img = cv2.imread(image)     # Load images
    img = cv2.resize(img, (257, 257), interpolation=cv2.INTER_LINEAR)  #  resize
    img = img = (np.float32(img) - 127.5) / 127.5   # change image to float and normalize
    img = img.reshape((1, 257, 257, 3))    # resize
    return img
imge = cv2.imread("/content/drive/My Drive/innopolis/1st year courses/2nd term/CV/GP/data/pose.jpg") 
img = load_image("/content/drive/My Drive/innopolis/1st year courses/2nd term/CV/GP/data/pose.jpg")
interpreter.set_tensor(input_details[0]['index'], img)    # feed the image to model
interpreter.invoke()
# The output of the model
output_data = interpreter.get_tensor(output_details[0]['index'])    # the heatmap output
output_offset = interpreter.get_tensor(output_details[1]['index'])  # the offset output

heatmap = output_data[0]
offsets = output_offset[0]
print(heatmap.shape)
print(offsets.shape)<jupyter_output>(9, 9, 17)
(9, 9, 34)<jupyter_text>How to parse the output to get the keypoints1.  A sigmoid activation is done on the heatmap to get the scores.2.  Find the max element(confidence score) in each heatmap layer.3. Find the offset vector of the same element(max element)4. To get the key points, the keypoint positions is multiplied by the stride then added to the offset vectorkeypointPositions = hm_positions * 32 + offset_vectors<jupyter_code>def get_keypoints(heatmap, offsets):
  """
  Get the keypoint output
  params:
  heatmap: the heatmap of the output
  offsets: the offset vectors of the output
  return:
  keypointPositions: list of the (x, y) position of every keypoint
  """
  # store some variables
  confidences =[]
  offset_vectors=[]
  hm_positions = []

  scores = sigmoid(heatmap)   # sigmoid
  for k in range(17):   # no. of keypoints
    x,y = np.unravel_index(np.argmax(scores[:,:,k]), scores[:,:,k].shape)   # find the max 
    hm_positions.append([x,y])
    confidences.append(scores[x,y,k])
    offset_vectors.append([offsets[x,y,k], offsets[x,y,k+17]])
  keypointPositions = np.add(np.array(hm_positions) * 32, offset_vectors)

  return confidences, keypointPositions
def sigmoid(a):
  """sigmoid function"""
  return 1/(1 + np.exp(-a))
con, kp = get_keypoints(heatmap, offsets)
def draw_on(img, con, kp):
  """Draw on image"""
  img2 = cv2.resize(img, (257,257))   # make a copy and resize
  img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
  # draw circles:
  for i in range(len(kp)):
    if con[i]>0.5:
      x, y = int(kp[i,1]), int(kp[i,0])
      centre = (x,y)  
      cv2.circle(img2, centre, 3, (255,0,0), -1)
  return img2
out = draw_on(imge, con, kp)
plt.imshow(out)
# Load the model
model = tf.lite.Interpreter("/content/drive/My Drive/innopolis/1st year courses/2nd term/CV/GP/posenet.tflite")
model.allocate_tensors()
input_details = model.get_input_details()   # the input shape
output_details = model.get_output_details()   # the output shape
# with different photo
# Load the image and convert it to the right size and format
img = cv2.imread("/content/drive/My Drive/innopolis/1st year courses/2nd term/CV/GP/data/balance.jpg")
img2 = load_image("/content/drive/My Drive/innopolis/1st year courses/2nd term/CV/GP/data/balance.jpg")
model.set_tensor(input_details[0]['index'], img2)    # feed the image to model

# The output of the model
output_data2 = model.get_tensor(output_details[0]['index'])[0]    # the heatmap output
output_offset2 = model.get_tensor(output_details[1]['index'])[0]  # the offset output

print(output_data2.shape)
print(output_offset2.shape)

model.invoke()
con2, kp2 = get_keypoints(output_data2, output_offset2)
new_image = draw_on(img, con2, kp2)
new_image.shape
plt.imshow(new_image)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1238594890510949, 'fraction_numerical': 0.02645985401459854, 'mean_word_length': 4.687418936446174, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '149666', 'license': 'MIT', 'n_tokens_mistral': 1622, 'n_tokens_neox': 1524, 'n_words': 403, 'path': 'Posenet_implementation.ipynb', 'repo_name': 'mohamedsayed18/Pose_estimation', 'size': '4384'}