code: def image2template_feature(
      img_feats=None,
      templates=None,
      medias=None,
      choose_templates=None,
      choose_ids=None,
  ):
      if choose_templates is not None:  # 1:N
          unique_templates, indices = np.unique(
              choose_templates, return_index=True
          )
          unique_subjectids = choose_ids[indices]
      else:  # 1:1
          unique_templates = np.unique(templates)
          unique_subjectids = None
  
      # template_feats = np.zeros((len(unique_templates), img_feats.shape[1]), dtype=img_feats.dtype)
      template_feats = np.zeros(
          (len(unique_templates), img_feats.shape[1])
      )
      for count_template, uqt in tqdm(
          enumerate(unique_templates),
          "Extract template feature",
          total=len(unique_templates),
      ):
          (ind_t,) = np.where(templates == uqt)
          face_norm_feats = img_feats[ind_t]
          face_medias = medias[ind_t]
          unique_medias, unique_media_counts = np.unique(
              face_medias, return_counts=True
          )
          media_norm_feats = []
          for u, ct in zip(unique_medias, unique_media_counts):
              (ind_m,) = np.where(face_medias == u)
              if ct == 1:
                  media_norm_feats += [face_norm_feats[ind_m]]
              else:  # image features from the same video will be aggregated into one feature
                  media_norm_feats += [
                      np.mean(face_norm_feats[ind_m], 0, keepdims=True)
                  ]
          media_norm_feats = np.array(media_norm_feats)
          # media_norm_feats = media_norm_feats / np.sqrt(np.sum(media_norm_feats ** 2, -1, keepdims=True))
          template_feats[count_template] = np.sum(media_norm_feats, 0)
      template_norm_feats = normalize(template_feats)
      return template_norm_feats, unique_templates, unique_subjectids
  
imports: import os
  import cv2
  import numpy
  import pandas
  from tqdm import tqdm
  from skimage import transform
  from sklearn.preprocessing import normalize
  from sklearn.metrics import roc_curve, auc
label_desc: Use image node_id 8 for calling a global function with example usage: image2template_feature(img_feats, templates, medias, choose_templates, choose_ids) and returns: template_norm_feats,  unique_templates,  unique_subjectids