code: def segment_roi_json(
      roi_json_path,
      segments_name,
      segments_speaker,
      segments_start,
      segments_end,
      total_frames_num,
  ):
      with codecs.open(roi_json_path, "r") as handle:
          roi_dic = json.load(handle)
  
      def get_from_frame_detection(frame_i, target_id):
          if str(frame_i) in roi_dic:
              for roi_info in roi_dic[str(frame_i)]:
                  if roi_info["id"] == target_id:
                      return [
                          roi_info["x1"],
                          roi_info["y1"],
                          roi_info["x2"],
                          roi_info["y2"],
                      ]
          return []
  
      delete_segments_key = []
      segments_roi_bound = {}
      for _, (name, speaker_id, frame_start, frame_end) in enumerate(
          zip(
              segments_name,
              segments_speaker,
              segments_start,
              segments_end,
          )
      ):
          if frame_end >= total_frames_num:
              delete_segments_key.append(name)
              print(
                  "{}: sengment end cross the line, {} but {}, skip".format(
                      name, frame_end, total_frames_num
                  )
              )
          else:
              segment_roi_bound = []
              segment_roi_idx = []
              for frame_idx in range(frame_start, frame_end):
                  segment_roi_bound.append(
                      get_from_frame_detection(frame_idx, speaker_id)
                  )
                  segment_roi_idx.append(frame_idx)
  
              frame_roi_exist_num = np.sum(
                  [*map(bool, segment_roi_bound)]
              ).item()
  
              if (
                  float(frame_roi_exist_num)
                  / float(frame_end - frame_start)
                  < 0.5
              ):
                  delete_segments_key.append(name)
                  print(
                      "{}: {}/{} frames have detection result, skip".format(
                          name,
                          frame_roi_exist_num,
                          frame_end - frame_start,
                      )
                  )
              elif frame_roi_exist_num == frame_end - frame_start:
                  segments_roi_bound[name] = segment_roi_bound
                  print(
                      "{}: {}/{} frames have detection result, prefect".format(
                          name,
                          frame_roi_exist_num,
                          frame_end - frame_start,
                      )
                  )
              else:
                  print(
                      "{}: {}/{} frames have detection result, insert".format(
                          name,
                          frame_roi_exist_num,
                          frame_end - frame_start,
                      )
                  )
                  i = 1
                  forward_buffer = []
                  forward_buffer_idx = -1
                  while frame_start - i >= 0:
                      if get_from_frame_detection(
                          frame_start - i, speaker_id
                      ):
                          forward_buffer = get_from_frame_detection(
                              frame_start - i, speaker_id
                          )
                          forward_buffer_idx = frame_start - i
                          break
                      else:
                          i += 1
  
                  need_insert_idxes = []
                  for i, (frame_idx, frame_roi_bound) in enumerate(
                      zip(segment_roi_idx, segment_roi_bound)
                  ):
                      if frame_roi_bound:
                          while need_insert_idxes:
                              need_insert_idx = need_insert_idxes.pop(0)
                              if forward_buffer_idx == -1:
                                  segment_roi_bound[
                                      need_insert_idx
                                  ] = frame_roi_bound
                                  print(
                                      need_insert_idx,
                                      segment_roi_bound[
                                          need_insert_idx
                                      ],
                                      segment_roi_idx[need_insert_idx],
                                      frame_roi_bound,
                                      frame_idx,
                                  )
                              else:
                                  segment_roi_bound[need_insert_idx] = (
                                      (
                                          np.array(forward_buffer)
                                          + (
                                              segment_roi_idx[
                                                  need_insert_idx
                                              ]
                                              - forward_buffer_idx
                                          )
                                          * (
                                              np.array(frame_roi_bound)
                                              - np.array(forward_buffer)
                                          )
                                          / (
                                              frame_idx
                                              - forward_buffer_idx
                                          )
                                      )
                                      .astype(np.int64)
                                      .tolist()
                                  )
                                  print(
                                      need_insert_idx,
                                      segment_roi_bound[
                                          need_insert_idx
                                      ],
                                      segment_roi_idx[need_insert_idx],
                                      frame_roi_bound,
                                      frame_idx,
                                      forward_buffer,
                                      forward_buffer_idx,
                                  )
                          forward_buffer = frame_roi_bound
                          forward_buffer_idx = frame_idx
                      else:
                          need_insert_idxes.append(i)
  
                  if need_insert_idxes:
                      i = 0
                      backward_buffer = []
                      backward_buffer_idx = -1
                      while frame_end + i < total_frames_num:
                          if get_from_frame_detection(
                              frame_end + i, speaker_id
                          ):
                              backward_buffer = (
                                  get_from_frame_detection(
                                      frame_end + i, speaker_id
                                  )
                              )
                              backward_buffer_idx = frame_end + i
                              break
                          else:
                              i += 1
                      while need_insert_idxes:
                          need_insert_idx = need_insert_idxes.pop(0)
                          if (
                              forward_buffer_idx == -1
                              and backward_buffer_idx == -1
                          ):
                              raise ValueError("no context cannot pad")
                          elif forward_buffer_idx == -1:
                              segment_roi_bound[
                                  need_insert_idx
                              ] = backward_buffer
                              print(
                                  need_insert_idx,
                                  segment_roi_bound[need_insert_idx],
                                  segment_roi_idx[need_insert_idx],
                                  backward_buffer,
                                  backward_buffer_idx,
                              )
                          elif backward_buffer_idx == -1:
                              segment_roi_bound[
                                  need_insert_idx
                              ] = forward_buffer
                              print(
                                  need_insert_idx,
                                  segment_roi_bound[need_insert_idx],
                                  segment_roi_idx[need_insert_idx],
                                  forward_buffer,
                                  forward_buffer_idx,
                              )
                          else:
                              segment_roi_bound[need_insert_idx] = (
                                  (
                                      np.array(forward_buffer)
                                      + (
                                          segment_roi_idx[
                                              need_insert_idx
                                          ]
                                          - forward_buffer_idx
                                      )
                                      * (
                                          np.array(backward_buffer)
                                          - np.array(forward_buffer)
                                      )
                                      / (
                                          backward_buffer_idx
                                          - forward_buffer_idx
                                      )
                                  )
                                  .astype(np.int64)
                                  .tolist()
                              )
                              print(
                                  need_insert_idx,
                                  segment_roi_bound[need_insert_idx],
                                  segment_roi_idx[need_insert_idx],
                                  backward_buffer,
                                  backward_buffer_idx,
                                  forward_buffer,
                                  forward_buffer_idx,
                              )
                  assert not need_insert_idxes
                  segments_roi_bound[name] = segment_roi_bound
      return segments_roi_bound, delete_segments_key
  
file_path: espnet/egs2/misp2021/avsr1/local/prepare_far_video_roi.py
imports: import argparse
  import codecs
  import json
  import os
  import time
  from multiprocessing import Pool
  import cv2
  import numpy
  from tqdm import tqdm
label_desc: Use image node_id 3 for calling a global function with example usage: segment_roi_json(roi_json_path, segments_name, segments_speaker, segments_start, segments_end, total_frames_num) and returns: segments_roi_bound,  delete_segments_key, list, list