text: # -*- coding: utf-8 -*-
  """inference_demo.ipynb
  
  Automatically generated by Colaboratory.
  
  Original file is located at
      https://colab.research.google.com/github/open-mmlab/mmdetection/blob/master/demo/inference_demo.ipynb
  """
  
  from mmdet.apis import init_detector, inference_detector, show_result_pyplot
  from mmdet.apis.inference import inference_detector_Demo
  import os
  import numpy as np
  import mmcv
  os.environ["CUDA_VISIBLE_DEVICES"] = "0"
  
  IMAGE_DIR = '/home/user/demo_30'
  Save_IMAGE_DIR = '/home/user/demo_30/infer'
  save_inferanno_dir = '/home/user/Dataset/Seoulchallenge/paper_0429/OD_infer'
  config_file = '../configs/cascade_mask_rcnn_demo.py'
  # download the checkpoint from model zoo and put it in `checkpoints/`
  checkpoint_file = "log/epoch_43.pth" #epoch_68.pth"
  
  
  show = 1
  makemAPtxt = 0
  save_inferpic = 1
  
  # build the model from a config file and a checkpoint file
  model = init_detector(config_file, checkpoint_file, device='cuda:0')
  CLASSES = ('Ocean','Land','car','building','person','trash')### 1.Ocean 2.Land 3.car 4.building 5.person. 6.trash
  cfg = mmcv.Config.fromfile(config_file)
  model.cfg.data.val.pipeline = cfg.test_pipeline
  model.cfg.data.test.pipeline = cfg.test_pipeline
  
  # test a single image
  if not (os.path.isdir(save_inferanno_dir)):
      os.makedirs(save_inferanno_dir)
  
  
  filenames = [i for i in os.listdir(IMAGE_DIR) if '.jpg' in i.lower() or '.png' in i.lower()]
  for filename in filenames:
      file_dir = os.path.join(IMAGE_DIR, filename)
  
      if ".jpg" or ".png" in filename.lower():
          result = inference_detector_Demo(model, file_dir)
  
          # show the results
          if show:
              print(file_dir)
              show_result_pyplot(file_dir, result, CLASSES, out=os.path.join(Save_IMAGE_DIR, filename))#,fig_size=(1920,1080))
  
  
  
          if isinstance(result, tuple):
              bbox_result, _ = result # bbox_result, segm_result
              test = 0
          else:
              bbox_result, segm_result = result, None
  
          if makemAPtxt:
              # ## make mAP cal txt ##
              with open(save_inferanno_dir+"/" + os.path.splitext(filename)[0]+".txt","w") as f:
                  for class_id in range(len(bbox_result)):
                      for object_num in range(len(bbox_result[class_id])):#range(np.vstack(bbox_result)):
                          bbox_left   = str((bbox_result[class_id][object_num][0]))#str(int(r['rois'][idx][1]))
                          bbox_top    = str((bbox_result[class_id][object_num][1]))
                          bbox_right  = str((bbox_result[class_id][object_num][2]))
                          bbox_bottom = str((bbox_result[class_id][object_num][3]))
                          score       = str((bbox_result[class_id][object_num][4]))
  
                          f.write(str(class_id+1) + " "+ score + " ")
                          f.write(bbox_left + " " + bbox_top + " " + bbox_right + " " + bbox_bottom + '\n')
  
          # if save_inferpic:
  
  
  
  