Python code to extract BBox: OpenCV

1 minute read

Python Code

PyTorch

  • Draw bbox while reading image in PIL-Image and display contents in iPython notebook

    • import segments
      from PIL import Image, ImageDraw
      import PIL
      import matplotlib.pyplot as plt
      %matplotlib inline
          
      pil_img = Image.open(act_img_path)
      img_draw = ImageDraw.Draw(pil_img)
      img_draw.rectangle((xmin, ymin, xmax, ymax), outline='red')
      box = (xmin, ymin, xmax, ymax)
      cropped_image = pil_img.crop(box)
      plt.imshow(pil_img)
      # draw only cropped-image
      plt.imshow(cropped_image)
      
    • Ploting image using matplotlib for multiple image in a grid
      # here figsize will be size of the image in a grid; first 2 params are `rows` and `cols` respectively.
      _, axarr = plt.subplots(1,2, figsize=(8, 8))
      image = cv2.imread(act_img_path)
      sh_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
      test_image = sh_image.copy()
      # this function gets the `face` detected using MTCNN-face detector
      bb_image = get_multiface(test_image)
      
      axarr[0].imshow(sh_image)
      axarr[1].imshow(bb_image)
      
  • Draw bbox while reading image in CV2-OpenCV based drawing in iPython notebook

    • import segements
      import cv2
      import matplotlib.pyplot as plt
      %matplotlib inline
      def open_image(filename):
        img = cv2.imread(filename)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        plt.imshow(img)
      open_image(act_img_path)
      
  • Reading Data using Pandas for training

    • For data visualization/analysis
      img_file = os.path.join(txt_file_root_path, 'lifestyleimages.txt')
      pd_data = pd.read_csv(img_file, header=None, names=['origIdPerson','imgPath','age','gender','emotion','recognitionDB',
                                                      'ageAnnot','genderAnnot','emotionAnnot'])
      pd_data.head()
      # Removing rows which have 'other' as gender
      pd_data = pd_data[pd_data.genderAnnot!='OTHER']
      pd_data['is_train'] = np.random.choice(2, size=len(pd_data), p=[0.2, 0.8])
      i = np.random.choice(len(pd_data))
      dfi = pd_data.iloc[i]
      print(f"age:{dfi['ageAnnot']}::genderAnnot:{dfi['genderAnnot']}::emotionAnnot:{dfi['emotionAnnot']}")
      print(f"img name : {dfi['imgPath']}")
      act_img_path = os.path.join(image_root_path, str(dfi["imgPath"]))
      open_image(act_img_path)
      
  • Displaying the image with bouding-box read through OpenCV cv2 and drawing the attributes using matplotlib to display it in iPython

    • Consider it that you already have all the bounding-boxes or rather pretend it in a np.array.
      import cv2
      import matplotlib.pyplot as plt
      import matplotlib.patches as patches
      %matplotlib inline
          
      plt.figure()
      fig, ax = plt.subplots(1, figsize=(40,50))
      ax.imshow(orig_image)
          
      for i in range(boxes.size(0)):
          box = boxes[i, :]
          xmin = box[0]
          ymin = box[1]
          xmax = box[2]
          ymax = box[3]
          box_w = xmax-xmin+1
          box_h = ymax-ymin+1
          bbox = patches.Rectangle((xmin, ymin), box_w, box_h, linewidth=2, edgecolor='red', facecolor='none')
          ax.add_patch(bbox)
          #cv2.rectangle(orig_image, (box[0], box[1]), (box[2], box[3]), (255, 255, 0), 4)
          #label = f"""{voc_dataset.class_names[labels[i]]}: {probs[i]:.2f}"""
          label_name = f"{class_names[labels[i]]}: {probs[i]:.2f}"
          # cv2.putText(orig_image, label, (box[0] + 20, box[1] + 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 255),2)
          plt.text(xmin, ymin, s=label_name, color='white', verticalalignment='top',
                  bbox={'color': 'brown', 'pad': 0})
          
      plt.axis('off')
      plt.show()