Add check.py
@@ -0,0 +1,27 @@
|
||||
import os
|
||||
import time
|
||||
import shutil
|
||||
import train
|
||||
import preprocessing
|
||||
import datetime
|
||||
|
||||
p = False
|
||||
while True:
|
||||
for foldername, subfolders, filenames in os.walk(os.path.join('datasets', 'zombobase-current')):
|
||||
p = True
|
||||
for filename in filenames:
|
||||
if p == True:
|
||||
time.sleep(3)
|
||||
print('8' * 8)
|
||||
preprocessing.main(os.path.join('datasets', 'zombobase-current'),'labels', 'images')
|
||||
|
||||
time.sleep(3)
|
||||
train.main()
|
||||
|
||||
time.sleep(3)
|
||||
|
||||
shutil.rmtree(os.path.join('datasets', 'zombobase-current', 'images'))
|
||||
shutil.rmtree(os.path.join('datasets', 'zombobase-current', 'labels'))
|
||||
|
||||
p = False
|
||||
|
||||
|
After Width: | Height: | Size: 36 KiB |
|
After Width: | Height: | Size: 35 KiB |
|
After Width: | Height: | Size: 34 KiB |
|
After Width: | Height: | Size: 35 KiB |
|
After Width: | Height: | Size: 34 KiB |
|
After Width: | Height: | Size: 35 KiB |
|
After Width: | Height: | Size: 32 KiB |
|
After Width: | Height: | Size: 33 KiB |
|
After Width: | Height: | Size: 33 KiB |
@@ -0,0 +1,2 @@
|
||||
0 0.18200000000000002 0.500359375 0.2918125 0.21760937500000002
|
||||
0 0.6288437499999999 0.5477031250000001 0.19801562500000003 0.22165625000000003
|
||||
@@ -0,0 +1,2 @@
|
||||
0 0.18984375 0.571875 0.3328125 0.278125
|
||||
0 0.6359375 0.5171875 0.2421875 0.2609375
|
||||
@@ -0,0 +1,2 @@
|
||||
0 0.76328125 0.678125 0.3640625 0.34375
|
||||
0 0.36640625 0.4671875 0.2875 0.29375
|
||||
@@ -0,0 +1,2 @@
|
||||
0 0.191609375 0.503484375 0.30634375 0.24260937500000002
|
||||
0 0.62753125 0.548640625 0.20317187499999995 0.22976562499999992
|
||||
@@ -0,0 +1,2 @@
|
||||
0 0.2015625 0.4234375 0.359375 0.3140625
|
||||
0 0.6109375 0.5796875 0.25625 0.275
|
||||
@@ -0,0 +1,2 @@
|
||||
0 0.77890625 0.36796875 0.38125 0.353125
|
||||
0 0.40703125 0.6 0.2828125 0.2953125
|
||||
@@ -0,0 +1 @@
|
||||
1 0.5796875 0.62578125 0.3390625 0.1703125
|
||||
@@ -0,0 +1 @@
|
||||
1 0.566125 0.6333125 0.3272343749999999 0.1366249999999999
|
||||
@@ -0,0 +1 @@
|
||||
1 0.49140625 0.6484375 0.3546875 0.253125
|
||||
@@ -2,13 +2,12 @@ import os.path
|
||||
import albumentations as A
|
||||
import cv2
|
||||
from pathlib import Path
|
||||
import datetime
|
||||
|
||||
labels_dir = 'labels'
|
||||
images_dir = 'images'
|
||||
current_dataset_dir = os.path.join('datasets', 'zombobase-current')
|
||||
class ImageAnnotation:
|
||||
|
||||
def read_annotations(self) -> [[]]:
|
||||
|
||||
with open(self.dataset_annotation_path, 'r') as f:
|
||||
rows = f.readlines()
|
||||
arr = []
|
||||
@@ -21,7 +20,7 @@ class ImageAnnotation:
|
||||
|
||||
return arr
|
||||
|
||||
def __init__(self, image_path):
|
||||
def __init__(self, image_path, current_dataset_dir, labels_dir, images_dir):
|
||||
self.image_path = image_path
|
||||
self.image_name = Path(image_path).stem
|
||||
self.dataset_image_path = os.path.join(current_dataset_dir, images_dir, self.image_path + '.jpg')
|
||||
@@ -32,9 +31,9 @@ class ImageAnnotation:
|
||||
self.annotations = self.read_annotations()
|
||||
|
||||
|
||||
def image_processing(img_ann: ImageAnnotation) -> [ImageAnnotation]:
|
||||
def image_processing(img_ann, current_dataset_dir,labels_dir,images_dir: ImageAnnotation) -> [ImageAnnotation]:
|
||||
category_ids = []
|
||||
bboxes = ImageAnnotation(img_ann).read_annotations()
|
||||
bboxes = ImageAnnotation(img_ann,current_dataset_dir,labels_dir,images_dir).read_annotations()
|
||||
for i in range(len(bboxes)):
|
||||
category_ids.append(bboxes[i][4])
|
||||
bboxes[i].pop(4)
|
||||
@@ -45,7 +44,7 @@ def image_processing(img_ann: ImageAnnotation) -> [ImageAnnotation]:
|
||||
], bbox_params=A.BboxParams(format='yolo', label_fields=['category_ids']))
|
||||
|
||||
bboxes = bboxes
|
||||
imag = ImageAnnotation(img_ann).image
|
||||
imag = ImageAnnotation(img_ann,current_dataset_dir,labels_dir,images_dir).image
|
||||
|
||||
transformed = transform(image=imag, bboxes=bboxes, category_ids=category_ids)
|
||||
transformed_image = transformed['image']
|
||||
@@ -54,18 +53,24 @@ def image_processing(img_ann: ImageAnnotation) -> [ImageAnnotation]:
|
||||
|
||||
return transformed_image, transformed_bboxes, transformed_category_ids
|
||||
|
||||
def write_results(img_ann: ImageAnnotation):
|
||||
for i in range(100):
|
||||
transformed_image, transformed_bboxes, transformed_category_ids, = image_processing(img_ann)
|
||||
cv2.imwrite(os.path.join(current_dataset_dir, images_dir, str(i)+ImageAnnotation(img_ann).image_path + '.jpg'), transformed_image)
|
||||
with open(os.path.join(current_dataset_dir, labels_dir, str(i)+ImageAnnotation(img_ann).image_path + '.txt'), 'w') as f:
|
||||
|
||||
def write_results(img_ann, current_dataset_dir, labels_dir, images_dir: ImageAnnotation):
|
||||
|
||||
file_start_save = 'Zombobase-' + str(datetime.date.today())
|
||||
for i in range(5):
|
||||
|
||||
transformed_image, transformed_bboxes, transformed_category_ids, = image_processing(img_ann, current_dataset_dir, labels_dir, images_dir)
|
||||
cv2.imwrite(os.path.join(current_dataset_dir, images_dir,str(i)+ImageAnnotation(img_ann,current_dataset_dir,labels_dir,images_dir).image_path + '.jpg'), transformed_image)
|
||||
with open(os.path.join(current_dataset_dir, labels_dir, str(i)+ImageAnnotation(img_ann, current_dataset_dir, labels_dir, images_dir).image_path + '.txt'), 'w') as f:
|
||||
print(os.path.join(current_dataset_dir, labels_dir, str(i)+ImageAnnotation(img_ann, current_dataset_dir, labels_dir, images_dir).image_path + '.txt'))
|
||||
for bbox, category_id in zip(transformed_bboxes, transformed_category_ids):
|
||||
print(bbox)
|
||||
x_center, y_center, width, height = bbox
|
||||
cla = category_id
|
||||
f.write(f"{cla} {x_center} {y_center} {width} {height}\n")
|
||||
#
|
||||
#
|
||||
def process_image():
|
||||
def process_image(current_dataset_dir,images_dir):
|
||||
file_annotation = []
|
||||
file_annotation_finished =[]
|
||||
for foldername, subfolders, filenames in os.walk(os.path.join(current_dataset_dir,images_dir)):
|
||||
@@ -80,10 +85,9 @@ def process_image():
|
||||
|
||||
return file_annotation_finished
|
||||
|
||||
def main():
|
||||
process_image()
|
||||
for i in process_image():
|
||||
write_results(i)
|
||||
def main(current_dataset_dir, labels_dir, images_dir):
|
||||
process_image(current_dataset_dir,images_dir)
|
||||
for i in process_image(current_dataset_dir,images_dir):
|
||||
write_results(i, current_dataset_dir, labels_dir, images_dir)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
#main(os.path.join('Zombobase-' + str(datetime.date.today()), 'datasets', 'zombobase-current','test'),'labels', 'images')
|
||||
@@ -12,9 +12,10 @@ current_dataset_dir = os.path.join('datasets', 'zombobase-current')
|
||||
|
||||
|
||||
def save(annotations, image, i, f_n):
|
||||
cv2.imwrite(os.path.join(file_start_save, current_dataset_dir, f_n, file_jpg[i]), image)
|
||||
with open(os.path.join(file_start_save, current_dataset_dir, f_n, file_txt[i]), 'w') as f:
|
||||
cv2.imwrite(os.path.join(file_start_save, current_dataset_dir, f_n, images_dir, file_jpg[i]), image)
|
||||
with open(os.path.join(file_start_save, current_dataset_dir, f_n, labels_dir, file_txt[i]), 'w') as f:
|
||||
for iii in range(len(annotations)):
|
||||
|
||||
f.write(annotations[iii])
|
||||
annotations = []
|
||||
|
||||
@@ -33,7 +34,12 @@ def piercing_photo_file():
|
||||
|
||||
|
||||
def file_validation(annotations, file_txt, file_jpg):
|
||||
os.makedirs(os.path.join(file_start_save, current_dataset_dir,'validation'))
|
||||
try:
|
||||
os.makedirs(os.path.join(file_start_save, current_dataset_dir,'validation'))
|
||||
os.makedirs(os.path.join(file_start_save, current_dataset_dir, 'validation', images_dir))
|
||||
os.makedirs(os.path.join(file_start_save, current_dataset_dir, 'validation', labels_dir))
|
||||
except FileExistsError:
|
||||
pass
|
||||
for i in range(len(file_txt)):
|
||||
image = cv2.imread(os.path.join(current_dataset_dir, images_dir, file_jpg[i]))
|
||||
with open(os.path.join(current_dataset_dir, labels_dir, file_txt[i]), 'r') as file:
|
||||
@@ -41,29 +47,41 @@ def file_validation(annotations, file_txt, file_jpg):
|
||||
for line in lines:
|
||||
annotations.append(line)
|
||||
save(annotations, image, i, 'validation')
|
||||
annotations = []
|
||||
|
||||
|
||||
def sort_file():
|
||||
os.makedirs(os.path.join(file_start_save, current_dataset_dir))
|
||||
try:
|
||||
os.makedirs(os.path.join(file_start_save, current_dataset_dir))
|
||||
except FileExistsError:
|
||||
pass
|
||||
annotations = []
|
||||
folder_name = ['test', 'train']
|
||||
percent_file = [0.20, 0.10]
|
||||
for f_n, p_f in zip(folder_name, percent_file):
|
||||
os.makedirs(os.path.join(file_start_save, current_dataset_dir, f_n))
|
||||
|
||||
try:
|
||||
os.makedirs(os.path.join(file_start_save, current_dataset_dir, f_n))
|
||||
os.makedirs(os.path.join(file_start_save, current_dataset_dir, f_n, images_dir))
|
||||
os.makedirs(os.path.join(file_start_save, current_dataset_dir, f_n, labels_dir))
|
||||
except FileExistsError:
|
||||
pass
|
||||
for i in range(math.ceil(len(file_txt) * p_f)):
|
||||
image = cv2.imread(os.path.join(current_dataset_dir, images_dir, file_jpg[i]))
|
||||
with open(os.path.join(current_dataset_dir, labels_dir, file_txt[i]), 'r') as file:
|
||||
lines = file.readlines()
|
||||
|
||||
|
||||
for line in lines:
|
||||
annotations.append(line)
|
||||
save(annotations, image, i, f_n)
|
||||
file_txt.pop(i)
|
||||
file_jpg.pop(i)
|
||||
save(annotations, image, i, f_n)
|
||||
|
||||
annotations = []
|
||||
print(annotations)
|
||||
file_txt.pop(i)
|
||||
file_jpg.pop(i)
|
||||
file_validation(annotations, file_txt, file_jpg)
|
||||
|
||||
|
||||
def main():
|
||||
piercing_photo_file()
|
||||
sort_file()
|
||||
|
||||
main()
|
||||
sort_file()
|
||||