diff --git a/bbox_txt/img_1.txt b/bbox_txt/img_1.txt deleted file mode 100644 index 564a406..0000000 --- a/bbox_txt/img_1.txt +++ /dev/null @@ -1,4 +0,0 @@ -2 0.49765625 0.46200607902735563 0.9640625 0.8693009118541033 -3 0.2890625 0.1656534954407295 0.071875 0.13677811550151975 -0 0.43125 0.5319148936170213 0.121875 0.7051671732522796 -0 0.790625 0.5030395136778115 0.090625 0.5866261398176292 diff --git a/bbox_txt/img_10.txt b/bbox_txt/img_10.txt deleted file mode 100644 index eb812c3..0000000 --- a/bbox_txt/img_10.txt +++ /dev/null @@ -1,3 +0,0 @@ -2 0.5453125 0.5176470588235295 0.853125 0.7294117647058823 -0 0.51875 0.5329411764705883 0.159375 0.5858823529411765 -2 0.07421875 0.5035294117647059 0.1453125 0.6211764705882353 diff --git a/bbox_txt/img_11.txt b/bbox_txt/img_11.txt deleted file mode 100644 index 7adc155..0000000 --- a/bbox_txt/img_11.txt +++ /dev/null @@ -1,4 +0,0 @@ -0 0.1765625 0.5354166666666667 0.209375 0.5763888888888888 -1 0.831640625 0.5319444444444444 0.19453125 0.5472222222222223 -2 0.499609375 0.5076388888888889 0.99140625 0.7736111111111111 -3 0.325390625 0.4076388888888889 0.03671875 0.06805555555555555 diff --git a/bbox_txt/img_12.txt b/bbox_txt/img_12.txt deleted file mode 100644 index 9aaf1e4..0000000 --- a/bbox_txt/img_12.txt +++ /dev/null @@ -1,2 +0,0 @@ -2 0.56953125 0.5304449648711944 0.7578125 0.47540983606557374 -3 0.24375 0.3805620608899297 0.03125 0.07259953161592506 diff --git a/bbox_txt/img_2.txt b/bbox_txt/img_2.txt deleted file mode 100644 index b9ef4bd..0000000 --- a/bbox_txt/img_2.txt +++ /dev/null @@ -1,4 +0,0 @@ -2 0.54921875 0.5761124121779859 0.7078125 0.585480093676815 -3 0.59609375 0.33840749414519905 0.0390625 0.11943793911007025 -0 0.503125 0.5971896955503513 0.05625 0.4309133489461358 -0 0.303125 0.607728337236534 0.034375 0.3442622950819672 diff --git a/bbox_txt/img_3.txt b/bbox_txt/img_3.txt deleted file mode 100644 index 5a4bf82..0000000 --- a/bbox_txt/img_3.txt +++ /dev/null @@ -1,4 +0,0 @@ -3 0.2828125 0.23055555555555557 0.0390625 0.06388888888888888 -2 0.265625 0.40555555555555556 0.4203125 0.525 -0 0.16796875 0.3972222222222222 0.034375 0.3111111111111111 -0 0.085546875 0.38958333333333334 0.02734375 0.25972222222222224 diff --git a/bbox_txt/img_4.txt b/bbox_txt/img_4.txt deleted file mode 100644 index ae9f537..0000000 --- a/bbox_txt/img_4.txt +++ /dev/null @@ -1,4 +0,0 @@ -3 0.371875 0.5637651821862348 0.034375 0.0708502024291498 -0 0.4609375 0.7773279352226721 0.0625 0.3076923076923077 -0 0.6421875 0.7975708502024291 0.05625 0.2874493927125506 -2 0.51640625 0.6194331983805668 0.7046875 0.6558704453441295 diff --git a/class_list.txt b/class_list.txt index bc29bb0..69a11c9 100644 --- a/class_list.txt +++ b/class_list.txt @@ -1,4 +1,12 @@ -closed_door -opened_door -bus -number \ No newline at end of file +Person +Bike +Car +Small Animal +Large Animal +Bird +Bus +Truck +Micro‑vessel +Small vessel +Medium-size vessel +Large vessel diff --git a/images/img_1.jpg b/images/img_1.jpg deleted file mode 100644 index b44e110..0000000 Binary files a/images/img_1.jpg and /dev/null differ diff --git a/images/img_10.jpg b/images/img_10.jpg deleted file mode 100644 index 567beb8..0000000 Binary files a/images/img_10.jpg and /dev/null differ diff --git a/images/img_11.jpg b/images/img_11.jpg deleted file mode 100644 index 29ab420..0000000 Binary files a/images/img_11.jpg and /dev/null differ diff --git a/images/img_12.jpg b/images/img_12.jpg deleted file mode 100644 index 93a68e2..0000000 Binary files a/images/img_12.jpg and /dev/null differ diff --git a/images/img_2.jpg b/images/img_2.jpg deleted file mode 100644 index c070303..0000000 Binary files a/images/img_2.jpg and /dev/null differ diff --git a/images/img_3.jpg b/images/img_3.jpg deleted file mode 100644 index 86a7bd0..0000000 Binary files a/images/img_3.jpg and /dev/null differ diff --git a/images/img_4.jpg b/images/img_4.jpg deleted file mode 100644 index c8b4335..0000000 Binary files a/images/img_4.jpg and /dev/null differ diff --git a/images/img_5.jpg b/images/img_5.jpg deleted file mode 100644 index 6f2502f..0000000 Binary files a/images/img_5.jpg and /dev/null differ diff --git a/images/img_6.jpg b/images/img_6.jpg deleted file mode 100644 index 60dfc17..0000000 Binary files a/images/img_6.jpg and /dev/null differ diff --git a/images/img_7.jpg b/images/img_7.jpg deleted file mode 100644 index 6209406..0000000 Binary files a/images/img_7.jpg and /dev/null differ diff --git a/images/img_8.jpg b/images/img_8.jpg deleted file mode 100644 index 6fcf677..0000000 Binary files a/images/img_8.jpg and /dev/null differ diff --git a/images/img_9.jpg b/images/img_9.jpg deleted file mode 100644 index 319b11c..0000000 Binary files a/images/img_9.jpg and /dev/null differ diff --git a/refactor.py b/refactor.py new file mode 100644 index 0000000..6897bd8 --- /dev/null +++ b/refactor.py @@ -0,0 +1,17 @@ +import os + +bb_dir = "new_bbox_txt/" + + +if __name__ == "__main__": + c = 0 + for f in os.listdir(bb_dir): + if os.path.getsize(bb_dir + f) != 0: + with open(bb_dir + f, "r+") as f: + elems = f.readline().split() + while elems: + if elems and len(elems) != 5: + f.truncate(0) + c += 1 + elems = f.readline().split() + print(c) diff --git a/relabel_car_ai.py b/relabel_car_ai.py new file mode 100644 index 0000000..c973407 --- /dev/null +++ b/relabel_car_ai.py @@ -0,0 +1,133 @@ +import os +from PIL import Image +from transformers import pipeline + +DATASET_ROOT = "D:/VS_code/Praktika/ModifiedOpenLabelling" + +IMAGES_ROOT = os.path.join(DATASET_ROOT, "images/train") +LABELS_ROOT = os.path.join(DATASET_ROOT, "labels/train") +OUTPUT_LABELS_ROOT = os.path.join(DATASET_ROOT, "labels_new/train") + +OLD_CAR_LABEL = 2 + +NEW_LABELS = { + "car": 2, + "bus": 6, + "truck": 7 +} + +MILITARY_KEYWORDS = [ + "tank", + "armored", + "armoured", + "military", + "apc", + "artillery", + "missile", + "launcher", + "howitzer" +] + +IMAGE_EXTENSIONS = (".jpg", ".jpeg", ".png") + +classifier = pipeline( + "image-classification", + model="google/vit-base-patch16-224" +) + +def yolo_to_pixels(bbox, img_w, img_h): + x_c, y_c, w, h = bbox + x1 = int((x_c - w / 2) * img_w) + y1 = int((y_c - h / 2) * img_h) + x2 = int((x_c + w / 2) * img_w) + y2 = int((y_c + h / 2) * img_h) + return max(0, x1), max(0, y1), min(img_w, x2), min(img_h, y2) + +def classify_crop(crop): + preds = classifier(crop) + + for p in preds: + label = p["label"].lower() + + for kw in MILITARY_KEYWORDS: + if kw in label: + return "truck" + + if "bus" in label: + return "bus" + + if "truck" in label or "lorry" in label: + return "truck" + + if "car" in label: + return "car" + + return "truck" + +for root, _, files in os.walk(IMAGES_ROOT): + for file in files: + if not file.lower().endswith(IMAGE_EXTENSIONS): + continue + + image_path = os.path.join(root, file) + + rel_path = os.path.relpath(image_path, IMAGES_ROOT) + rel_dir = os.path.dirname(rel_path) + + label_path = os.path.join( + LABELS_ROOT, + rel_dir, + os.path.splitext(file)[0] + ".txt" + ) + + if not os.path.exists(label_path): + continue + + image = Image.open(image_path).convert("RGB") + img_w, img_h = image.size + + with open(label_path, "r") as f: + lines = f.readlines() + + new_lines = [] + + for line in lines: + line_strip = line.strip() + if not line_strip: + continue + parts = line.strip().split() + cls = int(parts[0]) + bbox = list(map(float, parts[1:])) + + if cls != OLD_CAR_LABEL: + new_lines.append(line.strip()) + continue + + x1, y1, x2, y2 = yolo_to_pixels(bbox, img_w, img_h) + crop = image.crop((x1, y1, x2, y2)) + + new_class_name = classify_crop(crop) + new_class_id = NEW_LABELS[new_class_name] + + new_line = " ".join( + [str(new_class_id)] + [f"{v:.6f}" for v in bbox] + ) + new_lines.append(new_line) + + print(new_class_name, "←", rel_path) + + output_dir = os.path.join(OUTPUT_LABELS_ROOT, rel_dir) + os.makedirs(output_dir, exist_ok=True) + + output_label_path = os.path.join( + output_dir, + os.path.splitext(file)[0] + ".txt" + ) + + with open(output_label_path, "w") as f: + if new_lines: + f.write("\n".join(new_lines) + "\n") + else: + f.write("\n") + + print(f"✔ processed {rel_path}") diff --git a/rename.py b/rename.py new file mode 100644 index 0000000..fa0d56a --- /dev/null +++ b/rename.py @@ -0,0 +1,40 @@ +import os +import shutil + + +bb_dir = "bbox_txt/" +new_bb_dir = "new_bbox_txt/" + +if os.path.isdir(new_bb_dir) == False: + os.makedirs(new_bb_dir) + + +def is_wrong(txt_path): + return txt_path.count('_txt') != 0 + + +def png_in_name(txt_path): + return txt_path.count('_png.') != 0 or txt_path.count('_PNG.') + + +if __name__ == "__main__": + for f in os.listdir(bb_dir): + f_path = os.path.join(bb_dir, f) + new_f_path = f.replace('_txt', '_jpg') + print(f_path, new_f_path) + if os.path.getsize(f_path) != 0 and is_wrong(f): + new_f_path = os.path.join(new_bb_dir, new_f_path) + shutil.copy(f_path, new_f_path) + print("The file name has been changed: old file path - " + f_path + + " | new file path " + new_f_path) + elif os.path.getsize(f_path) == 0 and is_wrong(f): + right_f_path = os.path.join(bb_dir, new_f_path) + new_f_path = os.path.join(new_bb_dir, new_f_path) + shutil.copy(right_f_path, new_f_path) + print("The file name has been changed: old file path - " + right_f_path + + " | new file path " + new_f_path) + elif png_in_name(f): + new_f_path = os.path.join(new_bb_dir, f) + shutil.copy(f_path, new_f_path) + print("The file name has been changed: old file path - " + f_path + + " | new file path " + new_f_path) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 03e7ae2..f865e66 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ opencv-python -numpy \ No newline at end of file +numpy +natsort \ No newline at end of file diff --git a/run.py b/run.py index abd6180..3da72c7 100644 --- a/run.py +++ b/run.py @@ -1,12 +1,13 @@ import argparse import textwrap -import glob +from pathlib import Path import os +from natsort import natsorted import numpy as np import cv2 -bin_bbox_path = 'bin/bbox_txt' +bin_bbox_path = 'bin/labels' bin_images_path = 'bin/images' if os.path.isdir(bin_bbox_path) == False: @@ -14,7 +15,6 @@ if os.path.isdir(bin_images_path) == False: os.makedirs(bin_images_path) - WITH_QT = True try: cv2.namedWindow("Test") @@ -36,7 +36,7 @@ img_index = 0 img = None img_objects = [] -bb_dir = "bbox_txt/" +bb_dir = "labels" # selected bounding box prev_was_double_click = False @@ -48,6 +48,7 @@ point_1 = (-1, -1) point_2 = (-1, -1) + def change_img_index(x): global img_index, img img_index = x @@ -55,21 +56,23 @@ def change_img_index(x): img = cv2.imread(img_path) if WITH_QT: cv2.displayOverlay(WINDOW_NAME, "Showing image " - "" + str(img_index) + "/" - "" + str(last_img_index), 1000) + "" + str(img_index) + "/" + "" + str(last_img_index), 1000) else: print("Showing image " - "" + str(img_index) + "/" - "" + str(last_img_index) + " path:" + img_path) + "" + str(img_index) + "/" + "" + str(last_img_index) + " path:" + img_path) + def change_class_index(x): global class_index class_index = x if WITH_QT: cv2.displayOverlay(WINDOW_NAME, "Selected class " - "" + str(class_index) + "/" - "" + str(last_class_index) + "" - "\n " + class_list[class_index],3000) + "" + str(class_index) + "/" + "" + str(last_class_index) + "" + "\n " + class_list[ + class_index], 3000) else: print("Selected class :" + class_list[class_index]) @@ -80,7 +83,7 @@ def draw_edges(tmp_img): edges = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB) # Overlap image and edges together tmp_img = np.bitwise_or(tmp_img, edges) - #tmp_img = cv2.addWeighted(tmp_img, 1 - edges_val, edges, edges_val, 0) + # tmp_img = cv2.addWeighted(tmp_img, 1 - edges_val, edges, edges_val, 0) return tmp_img @@ -111,7 +114,7 @@ def yolo_format(class_index, point_1, point_2, width, height): x_width = float(abs(point_2[0] - point_1[0])) / width y_height = float(abs(point_2[1] - point_1[1])) / height return str(class_index) + " " + str(x_center) \ - + " " + str(y_center) + " " + str(x_width) + " " + str(y_height) + + " " + str(y_center) + " " + str(x_width) + " " + str(y_height) def voc_format(class_index, point_1, point_2): @@ -124,14 +127,15 @@ def voc_format(class_index, point_1, point_2): def get_txt_path(img_path): - img_name = os.path.basename(os.path.normpath(img_path)) - img_type = img_path.split('.')[-1] - return bb_dir + img_name.replace(img_type, 'txt') + #img_name = os.path.basename(os.path.normpath(img_path)) + #img_type = img_path.split('.')[-1] + #return bb_dir + img_name[::-1].replace(img_type[::-1], 'txt'[::-1], 1)[::-1] + return str(Path(img_path).with_suffix('.txt')).replace(img_dir, bb_dir) def save_bb(txt_path, line): with open(txt_path, 'a') as myfile: - myfile.write(line + "\n") # append line + myfile.write(line + "\n") # append line def delete_bb(txt_path, line_index): @@ -161,6 +165,7 @@ def draw_text(tmp_img, text, center, color, size): cv2.putText(tmp_img, text, center, font, 0.7, color, size, cv2.FONT_HERSHEY_COMPLEX_SMALL) return tmp_img + def draw_bboxes_from_file(tmp_img, txt_path, width, height): global img_objects img_objects = [] @@ -187,7 +192,7 @@ def draw_bboxes_from_file(tmp_img, txt_path, width, height): "seem to be in a different format. Consider " "removing your old label files.") raise Exception(textwrap.fill(error, 70)) - x1, y1, x2, y2 = x1-1, y1-1, x2-1, y2-1 + x1, y1, x2, y2 = x1 - 1, y1 - 1, x2 - 1, y2 - 1 img_objects.append([class_index, x1, y1, x2, y2]) color = class_rgb[class_index].tolist() cv2.rectangle(tmp_img, (x1, y1), (x2, y2), color, thickness=args.bbox_thickness) @@ -198,7 +203,7 @@ def draw_bboxes_from_file(tmp_img, txt_path, width, height): def get_bbox_area(x1, y1, x2, y2): width = abs(x2 - x1) height = abs(y2 - y1) - return width*height + return width * height def set_selected_bbox(): @@ -224,6 +229,7 @@ def mouse_inside_delete_button(): return True return False + def delete_selected_bbox(): img_path = image_list[img_index] txt_path = get_txt_path(img_path) @@ -239,6 +245,7 @@ def delete_selected_bbox(): new_file.write(line) counter += 1 + # mouse callback function def mouse_listener(event, x, y, flags, param): global is_bbox_selected, prev_was_double_click, mouse_x, mouse_y, point_1, point_2 @@ -248,7 +255,7 @@ def mouse_listener(event, x, y, flags, param): mouse_y = y elif event == cv2.EVENT_LBUTTONDBLCLK: prev_was_double_click = True - #print("Double click") + # print("Double click") point_1 = (-1, -1) # if clicked inside a bounding box set_selected_bbox() @@ -259,15 +266,15 @@ def mouse_listener(event, x, y, flags, param): delete_selected_bbox() elif event == cv2.EVENT_LBUTTONDOWN: if prev_was_double_click: - #print("Finish double click") + # print("Finish double click") prev_was_double_click = False - #print("Normal left click") + # print("Normal left click") is_mouse_inside_delete_button = mouse_inside_delete_button() if point_1[0] == -1: if is_bbox_selected and is_mouse_inside_delete_button: # the user wants to delete the bbox - #print("Delete bbox") + # print("Delete bbox") delete_selected_bbox() else: is_bbox_selected = False @@ -275,7 +282,7 @@ def mouse_listener(event, x, y, flags, param): point_1 = (x, y) else: # minimal size for bounding box to avoid errors - #!!!!!!!!!!!!!!!!!!!! + # !!!!!!!!!!!!!!!!!!!! threshold = 5 if abs(x - point_1[0]) > threshold or abs(y - point_1[1]) > threshold: # second click @@ -296,7 +303,7 @@ def get_close_icon(x1, y1, x2, y2): def draw_close_icon(tmp_img, x1_c, y1_c, x2_c, y2_c): - red = (0,0,255) + red = (0, 0, 255) cv2.rectangle(tmp_img, (x1_c + 1, y1_c - 1), (x2_c, y2_c), red, -1) white = (255, 255, 255) cv2.line(tmp_img, (x1_c, y1_c), (x2_c, y2_c), white, 2) @@ -312,30 +319,30 @@ def draw_info_bb_selected(tmp_img): draw_close_icon(tmp_img, x1_c, y1_c, x2_c, y2_c) return tmp_img + def remove_bad_data(img_path, img_path_txt): img_name = img_path.split('/')[-1] txt_name = img_path_txt.split('/')[-1] - os.rename(img_path,os.path.join('bin/images', img_name)) - os.rename(img_path_txt,os.path.join('bin/bbox_txt', txt_name)) + os.rename(img_path, os.path.join('bin/images', img_name)) + os.rename(img_path_txt, os.path.join('bin/labels', txt_name)) # load all images (with multiple extensions) from a directory using OpenCV -img_dir = "images/" +img_dir = "images" image_list = [] -for f in os.listdir(img_dir): - f_path = os.path.join(img_dir, f) - test_img = cv2.imread(f_path) - if test_img is not None: - image_list.append(f_path) - -#print(image_list) - -#SORT OR NOT? -#image_list.sort() -#if not args.sort: - #np.random.seed(123) # Keep random img order consistent - #np.random.shuffle(image_list) +for (dirpath, folder_names, files) in os.walk(img_dir): + for filename in files: + f_path = os.path.join(dirpath, filename) + test_img = cv2.imread(f_path) + if test_img is not None: + image_list.append(f_path) + +# SORT OR NOT? +image_list = natsorted(image_list) +# if not args.sort: +# np.random.seed(123) # Keep random img order consistent +# np.random.shuffle(image_list) last_img_index = len(image_list) - 1 print(image_list) @@ -347,12 +354,12 @@ def remove_bad_data(img_path, img_path_txt): for img_path in image_list: txt_path = get_txt_path(img_path) if not os.path.isfile(txt_path): - open(txt_path, 'a').close() + open(txt_path, 'a').close() # load class list with open('class_list.txt') as f: class_list = f.read().splitlines() -#print(class_list) +# print(class_list) last_class_index = len(class_list) - 1 # Make the class colors the same each session @@ -365,14 +372,14 @@ def remove_bad_data(img_path, img_path_txt): # If there are still more classes, add new colors randomly num_colors_missing = len(class_list) - len(class_rgb) if num_colors_missing > 0: - more_colors = np.random.randint(0, 255+1, size=(num_colors_missing, 3)) + more_colors = np.random.randint(0, 255 + 1, size=(num_colors_missing, 3)) class_rgb = np.vstack([class_rgb, more_colors]) # create window WINDOW_NAME = 'Bounding Box Labeler' cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_KEEPRATIO) -#cv2.resizeWindow(WINDOW_NAME, 1000, 700) -cv2.resizeWindow(WINDOW_NAME,500, 500) +# cv2.resizeWindow(WINDOW_NAME, 1000, 700) +cv2.resizeWindow(WINDOW_NAME, 500, 500) cv2.setMouseCallback(WINDOW_NAME, mouse_listener) # selected image @@ -382,7 +389,7 @@ def remove_bad_data(img_path, img_path_txt): # selected class TRACKBAR_CLASS = 'Class' if last_class_index != 0: - cv2.createTrackbar(TRACKBAR_CLASS, WINDOW_NAME, 0, last_class_index, change_class_index) + cv2.createTrackbar(TRACKBAR_CLASS, WINDOW_NAME, 0, last_class_index, change_class_index) # initialize change_img_index(0) @@ -402,10 +409,8 @@ def remove_bad_data(img_path, img_path_txt): # draw edges tmp_img = draw_edges(tmp_img) - - #print('MOUSE',mouse_x, mouse_y) - #print('POINTS', point_1, point_2) - + # print('MOUSE',mouse_x, mouse_y) + # print('POINTS', point_1, point_2) img_path = image_list[img_index] txt_path = get_txt_path(img_path) @@ -434,7 +439,8 @@ def remove_bad_data(img_path, img_path_txt): else: if WITH_QT: cv2.displayOverlay(WINDOW_NAME, "Selected label: " + class_list[class_index] + "" - "\nPress [w] or [s] to change.", 120) + "\nPress [w] or [s] to change.", + 120) cv2.imshow(WINDOW_NAME, tmp_img) pressed_key = cv2.waitKey(50) @@ -460,18 +466,15 @@ def remove_bad_data(img_path, img_path_txt): draw_line(tmp_img, mouse_x, mouse_y, height, width, color) cv2.setTrackbarPos(TRACKBAR_CLASS, WINDOW_NAME, class_index) - #REMOVING BAD DATA + # REMOVING BAD DATA elif pressed_key == ord('r'): - bad_path=img_path - bad_text=txt_path - + bad_path = img_path + bad_text = txt_path img_index = increase_index(img_index, last_img_index) cv2.setTrackbarPos(TRACKBAR_IMG, WINDOW_NAME, img_index) - - if img_index == 0: del image_list[last_img_index] last_img_index = len(image_list) - 1 @@ -482,7 +485,7 @@ def remove_bad_data(img_path, img_path_txt): else: del image_list[img_index - 1] - last_img_index = len(image_list)-1 + last_img_index = len(image_list) - 1 remove_bad_data(bad_path, bad_text) @@ -491,11 +494,11 @@ def remove_bad_data(img_path, img_path_txt): cv2.setTrackbarPos(TRACKBAR_IMG, WINDOW_NAME, img_index) - #Num class-switchin' + # Num class-switchin' elif pressed_key == ord('1'): - if len(class_list)>=1: - class_index=0 + if len(class_list) >= 1: + class_index = 0 color = class_rgb[class_index].tolist() draw_line(tmp_img, mouse_x, mouse_y, height, width, color) cv2.setTrackbarPos(TRACKBAR_CLASS, WINDOW_NAME, class_index) @@ -503,7 +506,7 @@ def remove_bad_data(img_path, img_path_txt): elif pressed_key == ord('2'): if len(class_list) >= 2: - class_index=1 + class_index = 1 color = class_rgb[class_index].tolist() draw_line(tmp_img, mouse_x, mouse_y, height, width, color) cv2.setTrackbarPos(TRACKBAR_CLASS, WINDOW_NAME, class_index) @@ -511,49 +514,49 @@ def remove_bad_data(img_path, img_path_txt): elif pressed_key == ord('3'): if len(class_list) >= 3: - class_index=2 + class_index = 2 color = class_rgb[class_index].tolist() draw_line(tmp_img, mouse_x, mouse_y, height, width, color) cv2.setTrackbarPos(TRACKBAR_CLASS, WINDOW_NAME, class_index) elif pressed_key == ord('4'): if len(class_list) >= 4: - class_index=3 + class_index = 3 color = class_rgb[class_index].tolist() draw_line(tmp_img, mouse_x, mouse_y, height, width, color) cv2.setTrackbarPos(TRACKBAR_CLASS, WINDOW_NAME, class_index) elif pressed_key == ord('5'): if len(class_list) >= 5: - class_index=4 + class_index = 4 color = class_rgb[class_index].tolist() draw_line(tmp_img, mouse_x, mouse_y, height, width, color) cv2.setTrackbarPos(TRACKBAR_CLASS, WINDOW_NAME, class_index) elif pressed_key == ord('6'): if len(class_list) >= 6: - class_index=5 + class_index = 5 color = class_rgb[class_index].tolist() draw_line(tmp_img, mouse_x, mouse_y, height, width, color) cv2.setTrackbarPos(TRACKBAR_CLASS, WINDOW_NAME, class_index) elif pressed_key == ord('7'): if len(class_list) >= 7: - class_index=6 + class_index = 6 color = class_rgb[class_index].tolist() draw_line(tmp_img, mouse_x, mouse_y, height, width, color) cv2.setTrackbarPos(TRACKBAR_CLASS, WINDOW_NAME, class_index) elif pressed_key == ord('8'): if len(class_list) >= 8: - class_index=7 + class_index = 7 color = class_rgb[class_index].tolist() draw_line(tmp_img, mouse_x, mouse_y, height, width, color) cv2.setTrackbarPos(TRACKBAR_CLASS, WINDOW_NAME, class_index) elif pressed_key == ord('9'): if len(class_list) >= 9: - class_index=8 + class_index = 8 color = class_rgb[class_index].tolist() draw_line(tmp_img, mouse_x, mouse_y, height, width, color) cv2.setTrackbarPos(TRACKBAR_CLASS, WINDOW_NAME, class_index) @@ -564,16 +567,16 @@ def remove_bad_data(img_path, img_path_txt): elif pressed_key == ord('h'): if WITH_QT: cv2.displayOverlay(WINDOW_NAME, "[e] to show edges;\n" - "[q] to quit;\n" - "[a] or [d] to change Image;\n" - "[w] or [s] to change Class.\n" - "%s" % img_path, 6000) + "[q] to quit;\n" + "[a] or [d] to change Image;\n" + "[w] or [s] to change Class.\n" + "%s" % img_path, 6000) else: print("[e] to show edges;\n" - "[q] to quit;\n" - "[a] or [d] to change Image;\n" - "[w] or [s] to change Class.\n" - "%s" % img_path) + "[q] to quit;\n" + "[a] or [d] to change Image;\n" + "[w] or [s] to change Class.\n" + "%s" % img_path) # show edges key listener elif pressed_key == ord('e'): if edges_on == True: @@ -588,7 +591,7 @@ def remove_bad_data(img_path, img_path_txt): cv2.displayOverlay(WINDOW_NAME, "Edges turned ON!", 1000) else: print("Edges turned ON!") - + # quit key listener elif pressed_key == ord('q'): break @@ -596,7 +599,7 @@ def remove_bad_data(img_path, img_path_txt): if WITH_QT: # if window gets closed then quit - if cv2.getWindowProperty(WINDOW_NAME,cv2.WND_PROP_VISIBLE) < 1: + if cv2.getWindowProperty(WINDOW_NAME, cv2.WND_PROP_VISIBLE) < 1: break cv2.destroyAllWindows() diff --git a/train_test_split.py b/train_test_split.py deleted file mode 100644 index 30d3a7a..0000000 --- a/train_test_split.py +++ /dev/null @@ -1,52 +0,0 @@ -import os -import random -import shutil - -imgList = os.listdir('images') - - -#shuffling images -random.shuffle(imgList) - -split = 0.2 - -train_path = 'custom_dataset/train' -val_path = 'custom_dataset/val' - -if os.path.isdir(train_path) == False: - os.makedirs(train_path) -if os.path.isdir(val_path) == False: - os.makedirs(val_path) - -imgLen = len(imgList) -print("Images in total: ", imgLen) - -train_images = imgList[: int(imgLen - (imgLen*split))] -val_images = imgList[int(imgLen - (imgLen*split)):] -print("Training images: ", len(train_images)) -print("Validation images: ", len(val_images)) - -for imgName in train_images: - og_path = os.path.join('images', imgName) - target_path = os.path.join(train_path, imgName) - - shutil.copyfile(og_path, target_path) - - og_txt_path = os.path.join('bbox_txt', imgName.replace('.jpg', '.txt')) - target_txt_path = os.path.join(train_path, imgName.replace('.jpg', '.txt')) - - shutil.copyfile(og_txt_path, target_txt_path) - -for imgName in val_images: - og_path = os.path.join('images', imgName) - target_path = os.path.join(val_path, imgName) - - shutil.copyfile(og_path, target_path) - - og_txt_path = os.path.join('bbox_txt', imgName.replace('.jpg', '.txt')) - target_txt_path = os.path.join(val_path, imgName.replace('.jpg', '.txt')) - - shutil.copyfile(og_txt_path, target_txt_path) - - -print("Done! ")