From 16cc08528cd725d75e873b46258c53396c54a48e Mon Sep 17 00:00:00 2001 From: Tetiana Yemelianenko <tyemel.mzeom@gmail.com> Date: Tue, 17 Sep 2024 14:10:22 +0000 Subject: [PATCH] Upload New File --- annotate_with_yolo.py | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 annotate_with_yolo.py diff --git a/annotate_with_yolo.py b/annotate_with_yolo.py new file mode 100644 index 0000000..27c7afe --- /dev/null +++ b/annotate_with_yolo.py @@ -0,0 +1,57 @@ +#predict +from ultralytics import YOLO +from PIL import Image +import pandas as pd +from glob import glob +import os + +model = YOLO('path_to_the_finetuned_yolo8.pt') + +parentpath = 'path_to_the_images_annotated_with_owl' +i = 0 +ext = '.jpg' +imgPaths = glob(parentpath + "/*" + ext) + +for fname in imgPaths: + try: + raw_image = Image.open(fname) + except: + continue + + txtfile = fname.replace("jpg", "txt") + txtfile = txtfile.replace("images", "labels") + with open(txtfile, "r") as f: + lines = f.readlines() + current_classes = [] + for line in lines: + current_classes.append(int(line[0])) + + results=model.predict(source=raw_image, save=True) + + a=results[0].boxes.data + a = a.detach().cpu().numpy() + px=pd.DataFrame(a).astype("float") + + height, width = results[0].orig_shape + + lines=[] + + for index,row in px.iterrows(): + x1 = row[0] + y1 = row[1] + x2 = row[2] + y2 = row[3] + d = int(row[5]) + xcen = float((x1 + x2)) / 2 / width + ycen = float((y1 + y2)) / 2 / height + w = float((x2 - x1)) / width + h = float((y2 - y1)) / height + if d not in current_classes: + lines.append([d, xcen, ycen, w, h]) + if os.path.exists(txtfile): + with open(txtfile, "a") as f: + for line in lines: + f.write("%d %.06f %.06f %.06f %.06f\n" % (line[0], line[1], line[2], line[3], line[4])) + + i += 1 + print(i) -- GitLab