[python] temp
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- import xml.etree.ElementTree as ET
- import cv2
- import os
- from openpyxl import Workbook
- class LabelImg():
- def labeling(self, labeled_folder_path):
- ###
- wb = Workbook()
- ws = wb.active
- ws['A1'], ws['B1'], ws['C1'], ws['D1'], ws['E1'], ws['F1'], ws['G1'], ws['H1'], ws['I1'], ws['J1'], ws['K1'] = \
- 'image_name', 'green', 'yellow', 'blue', 'red', 'white', 'orange', 'black', 'gray', 'pink', 'light_blue'
- ###
- path = labeled_folder_path
- labeled_img_folder = os.path.join(path, "img_labeled/")
- for filename in os.listdir(path):
- if filename.split(".")[-1] == "xml":
- xml_name = os.path.join(path, filename)
- img_name = os.path.join(path, filename.split(".")[0] + ".jpg")
- print(xml_name)
- print(img_name)
- tree = ET.parse(xml_name)
- root = tree.getroot()
- image = cv2.imread(img_name)
- image_copy = image.copy()
- label_name=[]
- for x in root.iter('name'):
- label_name.append(x.text)
- for i, x in enumerate(root.iter('bndbox'), start=0):
- xmin, ymin, xmax, ymax = int(x[0].text), int(x[1].text), int(x[2].text), int(x[3].text)
- position = ((xmin + 5), (ymin + 15))
- cv2.rectangle(image_copy, (xmin, ymin), (xmax, ymax), color=(0, 0, 255), thickness=1)
- cv2.putText(image_copy, label_name[i], position, cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)
- if not os.path.exists(labeled_img_folder):
- os.mkdir(labeled_img_folder)
- labeled_img_full_name = os.path.join(labeled_img_folder, filename.split(".")[0] + ".jpg")
- cv2.imwrite(labeled_img_full_name, image_copy)
- ###
- fieldA = filename.split(".")[0] + ".jpg"
- fieldB = label_name.count("green")
- fieldC = label_name.count("yellow")
- fieldD = label_name.count("blue")
- fieldE = label_name.count("red")
- fieldF = label_name.count("white")
- fieldG = label_name.count("orange")
- fieldH = label_name.count("black")
- fieldI = label_name.count("gray")
- fieldJ = label_name.count("pink")
- fieldK = label_name.count("pink")
- ws.append([fieldA, fieldB, fieldC, fieldD, fieldE, fieldF, fieldG, fieldH, fieldI, fieldJ, fieldK])
- ###
- ###
- excel_name = os.path.join(labeled_img_folder, "total.xlsx")
- wb.save(excel_name)
- ###
- return str(labeled_img_folder)
Editor
You can edit this paste and save as new: