code: def main():
      parser = argparse.ArgumentParser(
          description="Speech and backgound data download and preprocess"
      )
      parser.add_argument(
          "--out_dir", required=False, default="./manifest/", type=str
      )
      parser.add_argument(
          "--speech_data_root", required=True, default=None, type=str
      )
      parser.add_argument(
          "--background_data_root",
          required=True,
          default=None,
          type=str,
      )
      parser.add_argument(
          "--test_size", required=False, default=0.1, type=float
      )
      parser.add_argument(
          "--val_size", required=False, default=0.1, type=float
      )
      parser.add_argument(
          "--window_length_in_sec",
          required=False,
          default=0.63,
          type=float,
      )
      parser.add_argument("--log", required=False, action="store_true")
      parser.add_argument(
          "--rebalance_method", required=False, default=None, type=str
      )
      parser.add_argument("--demo", required=False, action="store_true")
      parser.set_defaults(log=False, generate=False)
      args = parser.parse_args()
  
      if not args.rebalance_method:
          rebalance = False
      else:
          if (
              args.rebalance_method != "over"
              and args.rebalance_method != "under"
              and args.rebalance_method != "fixed"
          ):
              raise NameError(
                  "Please select a valid sampling method: over/under/fixed."
              )
          else:
              rebalance = True
  
      if args.log:
          logging.basicConfig(level=logging.DEBUG)
  
      # Download speech data
      speech_data_root = args.speech_data_root
      data_set = "google_speech_recognition_v2"
      speech_data_folder = os.path.join(speech_data_root, data_set)
  
      background_data_folder = args.background_data_root
      logging.info(f"Working on: {data_set}")
  
      # Download and extract speech data
      if not os.path.exists(speech_data_folder):
          file_path = os.path.join(
              speech_data_root, data_set + ".tar.bz2"
          )
          logging.info(f"Getting {data_set}")
          __maybe_download_file(file_path, URL)
          logging.info(f"Extracting {data_set}")
          __extract_all_files(
              file_path, speech_data_root, speech_data_folder
          )
  
      logging.info(f"Split speech data!")
      # dataset provide testing.txt and validation.txt feel free to split data using that with process_google_speech_train
      split_train_val_test(
          speech_data_folder,
          "speech",
          args.test_size,
          args.val_size,
          args.demo,
      )
  
      logging.info(f"Split background data!")
      split_train_val_test(
          background_data_folder,
          "background",
          args.test_size,
          args.val_size,
      )
  
      out_dir = args.out_dir
  
      # Process Speech manifest
      logging.info(f"=== Write speech data to manifest!")
      (
          skip_num_val,
          speech_seg_num_val,
          speech_val,
      ) = load_list_write_manifest(
          speech_data_folder,
          out_dir,
          "validation_list.txt",
          "speech",
          0.2,
          0.8,
          args.window_length_in_sec,
          args.window_length_in_sec,
      )
      (
          skip_num_test,
          speech_seg_num_test,
          speech_test,
      ) = load_list_write_manifest(
          speech_data_folder,
          out_dir,
          "testing_list.txt",
          "speech",
          0.2,
          0.8,
          0.01,
          args.window_length_in_sec,
      )
      (
          skip_num_train,
          speech_seg_num_train,
          speech_train,
      ) = load_list_write_manifest(
          speech_data_folder,
          out_dir,
          "training_list.txt",
          "speech",
          0.2,
          0.8,
          args.window_length_in_sec,
          args.window_length_in_sec,
      )
  
      logging.info(
          f"Val: Skip {skip_num_val} samples. Get {speech_seg_num_val} segments! => {speech_val} "
      )
      logging.info(
          f"Test: Skip {skip_num_test} samples. Get {speech_seg_num_test} segments! => {speech_test}"
      )
      logging.info(
          f"Train: Skip {skip_num_train} samples. Get {speech_seg_num_train} segments!=> {speech_train}"
      )
  
      # Process background manifest
      # if we select to generate more background noise data
      if args.demo:
          logging.info("Start generating more background noise data")
          generate_variety_noise(
              background_data_folder,
              "validation_list.txt",
              "background",
          )
          generate_variety_noise(
              background_data_folder, "training_list.txt", "background"
          )
          generate_variety_noise(
              background_data_folder, "testing_list.txt", "background"
          )
          background_data_folder = os.path.join(
              background_data_folder.split("_background_noise_")[0],
              "_background_noise_more",
          )
  
      logging.info(f"=== Write background data to manifest!")
      (
          skip_num_val,
          background_seg_num_val,
          background_val,
      ) = load_list_write_manifest(
          background_data_folder,
          out_dir,
          "validation_list.txt",
          "background",
          0,
          None,
          0.15,
          args.window_length_in_sec,
      )
      (
          skip_num_test,
          background_seg_num_test,
          background_test,
      ) = load_list_write_manifest(
          background_data_folder,
          out_dir,
          "testing_list.txt",
          "background",
          0,
          None,
          0.01,
          args.window_length_in_sec,
      )
      (
          skip_num_train,
          background_seg_num_train,
          background_train,
      ) = load_list_write_manifest(
          background_data_folder,
          out_dir,
          "training_list.txt",
          "background",
          0,
          None,
          0.15,
          args.window_length_in_sec,
      )
  
      logging.info(
          f"Val: Skip {skip_num_val} samples. Get {background_seg_num_val} segments! => {background_val}"
      )
      logging.info(
          f"Test: Skip {skip_num_test} samples. Get {background_seg_num_test} segments! => {background_test}"
      )
      logging.info(
          f"Train: Skip {skip_num_train} samples. Get {background_seg_num_train} segments! => {background_train}"
      )
      min_val, max_val = min(
          speech_seg_num_val, background_seg_num_val
      ), max(speech_seg_num_val, background_seg_num_val)
      min_test, max_test = (
          min(speech_seg_num_test, background_seg_num_test),
          max(speech_seg_num_test, background_seg_num_test),
      )
      min_train, max_train = (
          min(speech_seg_num_train, background_seg_num_train),
          max(speech_seg_num_train, background_seg_num_train),
      )
  
      logging.info("Finish generating manifest!")
  
      if rebalance:
          # Random Oversampling: Randomly duplicate examples in the minority class.
          # Random Undersampling: Randomly delete examples in the majority class.
          if args.rebalance_method == "under":
              logging.info(
                  f"Rebalancing number of samples in classes using {args.rebalance_method} sampling."
              )
              logging.info(
                  f"Val: {min_val} Test: {min_test} Train: {min_train}!"
              )
  
              rebalance_json(
                  out_dir, background_val, min_val, "balanced"
              )
              rebalance_json(
                  out_dir, background_test, min_test, "balanced"
              )
              rebalance_json(
                  out_dir, background_train, min_train, "balanced"
              )
  
              rebalance_json(out_dir, speech_val, min_val, "balanced")
              rebalance_json(out_dir, speech_test, min_test, "balanced")
              rebalance_json(
                  out_dir, speech_train, min_train, "balanced"
              )
  
          if args.rebalance_method == "over":
              logging.info(
                  f"Rebalancing number of samples in classes using {args.rebalance_method} sampling."
              )
              logging.info(
                  f"Val: {max_val} Test: {max_test} Train: {max_train}!"
              )
  
              rebalance_json(
                  out_dir, background_val, max_val, "balanced"
              )
              rebalance_json(
                  out_dir, background_test, max_test, "balanced"
              )
              rebalance_json(
                  out_dir, background_train, max_train, "balanced"
              )
  
              rebalance_json(out_dir, speech_val, max_val, "balanced")
              rebalance_json(out_dir, speech_test, max_test, "balanced")
              rebalance_json(
                  out_dir, speech_train, max_train, "balanced"
              )
  
          if args.rebalance_method == "fixed":
              fixed_test, fixed_val, fixed_train = 200, 100, 500
              logging.info(
                  f"Rebalancing number of samples in classes using {args.rebalance_method} sampling."
              )
              logging.info(
                  f"Val: {fixed_val} Test: {fixed_test} Train: {fixed_train}!"
              )
  
              rebalance_json(
                  out_dir, background_val, fixed_val, "balanced"
              )
              rebalance_json(
                  out_dir, background_test, fixed_test, "balanced"
              )
              rebalance_json(
                  out_dir, background_train, fixed_train, "balanced"
              )
  
              rebalance_json(out_dir, speech_val, fixed_val, "balanced")
              rebalance_json(
                  out_dir, speech_test, fixed_test, "balanced"
              )
              rebalance_json(
                  out_dir, speech_train, fixed_train, "balanced"
              )
      else:
          logging.info("Don't rebalance number of samples in classes.")
  
imports: import argparse
  import glob
  import json
  import logging
  import os
  import tarfile
  import urllib.request
  import librosa
  import numpy
  import soundfile
  from sklearn.model_selection import train_test_split
label_desc: Use image node_id 10 for calling a global function with example usage: main() without return types