From d40397b6f2b3c3b9a5322e410737c4169b82b270 Mon Sep 17 00:00:00 2001 From: Mahmoud Ahmed Ali <mahmoudali2929@gmail.com> Date: Tue, 14 Feb 2023 17:57:17 +0000 Subject: [PATCH] Add data label file --- data_label.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 data_label.py diff --git a/data_label.py b/data_label.py new file mode 100644 index 0000000..02cf836 --- /dev/null +++ b/data_label.py @@ -0,0 +1,29 @@ +import cv2 +import numpy as np + + +for im in range(4995): + id_obj = 0.0 + res_all = [] + for obj in ['banana_mask', 'orange_mask', 'pear_mask']: + res = [id_obj] + image = cv2.imread(f"{obj}/{im}.png", 0) + image = image/255.0 + _, contours, _ = cv2.findContours(image.astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) + print(len(contours[0])) + for i in range(len(contours[0])): + x = contours[0][i][0][0]/640.0 + res.append(x) + y = contours[0][i][0][1]/480.0 + res.append(y) + id_obj += 1.0 + res_all.append(res) + # res_ln = len(res_all) + # matrix = np.array([res_all[0], res_all[1], res_all[2]]).reshape(3, 1) + # np.savetxt(f'labels_all/{im}.txt', np.array(res_all).reshape((1,3))) + + a_file = open(f'labels_all/{im}.txt', "w") + for row in res_all: + np.savetxt(a_file, np.array(row).reshape(1, len(row))) + + a_file.close() -- GitLab