code: def segment_video_roi_json(
      video_path,
      roi_json_path,
      roi_store_dir,
      segments_name,
      segments_speaker,
      segments_start,
      segments_end,
      file_handle,
  ):
      segments_num = len(segments_start)
      assert segments_num > 0
      assert segments_num == len(segments_end)
  
      video_capture = cv2.VideoCapture(video_path)
      total_frames_num = int(video_capture.get(7))
      print(
          "using roi info from {}, all {} frames, generating {} segments".format(
              roi_json_path, total_frames_num, segments_num
          )
      )
  
      segments_roi_bound, delete_segments_key = segment_roi_json(
          roi_json_path,
          segments_name,
          segments_speaker,
          segments_start,
          segments_end,
          total_frames_num,
      )
      frame2segment_roi_bound = {}
      for i, segment_name in enumerate(segments_name):
          if segment_name not in delete_segments_key:
              segments_path = os.path.join(
                  os.path.abspath(roi_store_dir),
                  "{}.npz".format(segment_name),
              )
              file_handle.write(
                  "{} {}\n".format(segment_name, segments_path)
              )
              if not os.path.exists(segments_path):
                  for in_frame_idx in range(
                      segments_end[i] - segments_start[i]
                  ):
                      if (
                          segments_start[i] + in_frame_idx
                          in frame2segment_roi_bound
                      ):
                          frame2segment_roi_bound[
                              segments_start[i] + in_frame_idx
                          ].append(
                              [
                                  segment_name,
                                  in_frame_idx,
                                  segments_end[i] - segments_start[i],
                                  segments_roi_bound[segment_name][
                                      in_frame_idx
                                  ],
                              ]
                          )
                      else:
                          frame2segment_roi_bound[
                              segments_start[i] + in_frame_idx
                          ] = [
                              [
                                  segment_name,
                                  in_frame_idx,
                                  segments_end[i] - segments_start[i],
                                  segments_roi_bound[segment_name][
                                      in_frame_idx
                                  ],
                              ]
                          ]
  
      if not os.path.exists(roi_store_dir):
          os.makedirs(roi_store_dir, exist_ok=True)
  
      if frame2segment_roi_bound:
          segments_roi_frames_buffer = {}
  
          # segment_video_writer = None
          frames_idx = 0
  
          # frames_bar = tqdm(total=total_frames_num, leave=True, desc='Frame')
          while video_capture.isOpened():
              ret, frame = video_capture.read()
              if ret and frame2segment_roi_bound:
                  if frames_idx in frame2segment_roi_bound:
                      frame_info_list = frame2segment_roi_bound.pop(
                          frames_idx
                      )
                      for frame_info in frame_info_list:
                          if frame_info[1] == 0:
                              assert (
                                  frame_info[0]
                                  not in segments_roi_frames_buffer
                              )
                              segments_roi_frames_buffer[
                                  frame_info[0]
                              ] = [
                                  crop_frame_roi(
                                      frame, frame_info[3], (96, 96)
                                  )
                              ]
                          else:
                              segments_roi_frames_buffer[
                                  frame_info[0]
                              ].append(
                                  crop_frame_roi(
                                      frame, frame_info[3], (96, 96)
                                  )
                              )
  
                          if frame_info[1] == frame_info[2] - 1:
                              np.savez(
                                  os.path.join(
                                      roi_store_dir,
                                      "{}.npz".format(frame_info[0]),
                                  ),
                                  data=segments_roi_frames_buffer.pop(
                                      frame_info[0]
                                  ),
                              )
                  frames_idx += 1
                  # frames_bar.update(1)
              else:
                  break
          # frames_bar.close()
          assert not frame2segment_roi_bound
          video_capture.release()
          print(
              "skip {} segments: {}".format(
                  len(delete_segments_key),
                  ",".join(delete_segments_key),
              )
          )
      return None
  
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 4 for calling a global function with example usage: segment_video_roi_json(video_path, roi_json_path, roi_store_dir, segments_name, segments_speaker, segments_start, segments_end, file_handle) and returns: None