[python] temp

Viewer

  1. import xml.etree.ElementTree as ET
  2. import cv2
  3. import os
  4. from openpyxl import Workbook
  5.  
  6. class LabelImg():
  7.  
  8.     def labeling(self, labeled_folder_path):
  9.  
  10.         ###
  11.         wb = Workbook()
  12.         ws = wb.active
  13.         ws['A1'], ws['B1'], ws['C1'], ws['D1'], ws['E1'], ws['F1'], ws['G1'], ws['H1'], ws['I1'], ws['J1'], ws['K1'] = \
  14.             'image_name', 'green', 'yellow', 'blue', 'red', 'white', 'orange', 'black', 'gray', 'pink', 'light_blue'
  15.         ###
  16.  
  17.         path = labeled_folder_path
  18.         labeled_img_folder = os.path.join(path, "img_labeled/")
  19.  
  20.         for filename in os.listdir(path):
  21.             if filename.split(".")[-1] == "xml":
  22.                 xml_name = os.path.join(path, filename)
  23.                 img_name = os.path.join(path, filename.split(".")[0] + ".jpg")
  24.                 print(xml_name)
  25.                 print(img_name)
  26.  
  27.                 tree = ET.parse(xml_name)
  28.                 root = tree.getroot()
  29.  
  30.                 image = cv2.imread(img_name)
  31.                 image_copy = image.copy()
  32.  
  33.                 label_name=[]
  34.                 for x in root.iter('name'):
  35.                     label_name.append(x.text)
  36.  
  37.                 for i, x in enumerate(root.iter('bndbox'), start=0):
  38.                     xmin, ymin, xmax, ymax = int(x[0].text), int(x[1].text), int(x[2].text), int(x[3].text)
  39.                     position = ((xmin + 5), (ymin + 15))
  40.                     cv2.rectangle(image_copy, (xmin, ymin), (xmax, ymax), color=(0, 0, 255), thickness=1)
  41.                     cv2.putText(image_copy, label_name[i], position, cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)
  42.  
  43.                 if not os.path.exists(labeled_img_folder):
  44.                     os.mkdir(labeled_img_folder)
  45.  
  46.                 labeled_img_full_name = os.path.join(labeled_img_folder, filename.split(".")[0] + ".jpg")
  47.                 cv2.imwrite(labeled_img_full_name, image_copy)
  48.  
  49.                 ###
  50.                 fieldA = filename.split(".")[0] + ".jpg"
  51.                 fieldB = label_name.count("green")
  52.                 fieldC = label_name.count("yellow")
  53.                 fieldD = label_name.count("blue")
  54.                 fieldE = label_name.count("red")
  55.                 fieldF = label_name.count("white")
  56.                 fieldG = label_name.count("orange")
  57.                 fieldH = label_name.count("black")
  58.                 fieldI = label_name.count("gray")
  59.                 fieldJ = label_name.count("pink")
  60.                 fieldK = label_name.count("pink")
  61.                 ws.append([fieldA, fieldB, fieldC, fieldD, fieldE, fieldF, fieldG, fieldH, fieldI, fieldJ, fieldK])
  62.                 ###
  63.         ###
  64.         excel_name = os.path.join(labeled_img_folder, "total.xlsx")
  65.         wb.save(excel_name)
  66.         ###
  67.         return str(labeled_img_folder)
  68.  

Editor

You can edit this paste and save as new:


File Description
  • temp
  • Paste Code
  • 15 Jun-2021
  • 2.77 Kb
You can Share it: