Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions bbox_txt/img_1.txt

This file was deleted.

3 changes: 0 additions & 3 deletions bbox_txt/img_10.txt

This file was deleted.

4 changes: 0 additions & 4 deletions bbox_txt/img_11.txt

This file was deleted.

2 changes: 0 additions & 2 deletions bbox_txt/img_12.txt

This file was deleted.

4 changes: 0 additions & 4 deletions bbox_txt/img_2.txt

This file was deleted.

4 changes: 0 additions & 4 deletions bbox_txt/img_3.txt

This file was deleted.

4 changes: 0 additions & 4 deletions bbox_txt/img_4.txt

This file was deleted.

16 changes: 12 additions & 4 deletions class_list.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
closed_door
opened_door
bus
number
Person
Bike
Car
Small Animal
Large Animal
Bird
Bus
Truck
Micro‑vessel
Small vessel
Medium-size vessel
Large vessel
Binary file removed images/img_1.jpg
Binary file not shown.
Binary file removed images/img_10.jpg
Binary file not shown.
Binary file removed images/img_11.jpg
Binary file not shown.
Binary file removed images/img_12.jpg
Binary file not shown.
Binary file removed images/img_2.jpg
Binary file not shown.
Binary file removed images/img_3.jpg
Binary file not shown.
Binary file removed images/img_4.jpg
Binary file not shown.
Binary file removed images/img_5.jpg
Binary file not shown.
Binary file removed images/img_6.jpg
Binary file not shown.
Binary file removed images/img_7.jpg
Binary file not shown.
Binary file removed images/img_8.jpg
Binary file not shown.
Binary file removed images/img_9.jpg
Binary file not shown.
17 changes: 17 additions & 0 deletions refactor.py
Original file line number Diff line number Diff line change
@@ -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)
133 changes: 133 additions & 0 deletions relabel_car_ai.py
Original file line number Diff line number Diff line change
@@ -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}")
40 changes: 40 additions & 0 deletions rename.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
opencv-python
numpy
numpy
natsort
Loading