mirror of
https://github.com/azaion/ai-training.git
synced 2026-04-22 10:16:34 +00:00
fix folder names, refactoring
move to yolov8m use checkpoint.txt instead of yaml
This commit is contained in:
+26
-67
@@ -1,19 +1,15 @@
|
||||
import os.path
|
||||
import time
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
import albumentations as A
|
||||
import cv2
|
||||
import numpy as np
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
from config import Config
|
||||
from constants import current_images_dir, current_labels_dir, annotation_classes, prefix, date_format, \
|
||||
current_dataset_dir
|
||||
from constants import (data_images_dir, data_labels_dir, processed_images_dir, processed_labels_dir,
|
||||
annotation_classes, checkpoint_file, checkpoint_date_format)
|
||||
from dto.imageLabel import ImageLabel
|
||||
|
||||
config = Config()
|
||||
|
||||
|
||||
def image_processing(img_ann: ImageLabel) -> [ImageLabel]:
|
||||
transforms = [
|
||||
@@ -45,12 +41,12 @@ def image_processing(img_ann: ImageLabel) -> [ImageLabel]:
|
||||
img = ImageLabel(
|
||||
image=res['image'],
|
||||
labels=res['bboxes'],
|
||||
image_path=os.path.join(current_images_dir, f'{name}{path.suffix}'),
|
||||
labels_path=os.path.join(current_labels_dir, f'{name}.txt')
|
||||
image_path=os.path.join(processed_images_dir, f'{name}{path.suffix}'),
|
||||
labels_path=os.path.join(processed_labels_dir, f'{name}.txt')
|
||||
)
|
||||
results.append(img)
|
||||
except Exception as e:
|
||||
print(f'Error during transformtation: {e}')
|
||||
print(f'Error during transformation: {e}')
|
||||
return results
|
||||
|
||||
|
||||
@@ -80,6 +76,7 @@ def read_labels(labels_path) -> [[]]:
|
||||
str_coordinates = row.split(' ')
|
||||
class_num = str_coordinates.pop(0)
|
||||
coordinates = [float(n.replace(',', '.')) for n in str_coordinates]
|
||||
# noinspection PyTypeChecker
|
||||
coordinates.append(class_num)
|
||||
arr.append(coordinates)
|
||||
return arr
|
||||
@@ -92,34 +89,24 @@ def process_image(img_ann):
|
||||
write_result(ImageLabel(
|
||||
image=img_ann.image,
|
||||
labels=img_ann.labels,
|
||||
image_path=os.path.join(current_images_dir, Path(img_ann.image_path).name),
|
||||
labels_path=os.path.join(current_labels_dir, Path(img_ann.labels_path).name)
|
||||
image_path=os.path.join(processed_images_dir, Path(img_ann.image_path).name),
|
||||
labels_path=os.path.join(processed_labels_dir, Path(img_ann.labels_path).name)
|
||||
))
|
||||
# os.remove(img_ann.image_path)
|
||||
# os.remove(img_ann.labels_path)
|
||||
|
||||
|
||||
def get_checkpoint():
|
||||
if config.checkpoint is not None:
|
||||
return config.checkpoint
|
||||
|
||||
dates = []
|
||||
for directory in os.listdir('models'):
|
||||
try:
|
||||
dates.append(datetime.strptime(directory[len(prefix):], date_format))
|
||||
except:
|
||||
continue
|
||||
if len(dates) == 0:
|
||||
return datetime.now() - relativedelta(years=1)
|
||||
else:
|
||||
return max(dates)
|
||||
|
||||
|
||||
def main():
|
||||
last_date = checkpoint = get_checkpoint()
|
||||
checkpoint = datetime.now() - timedelta(days=720)
|
||||
try:
|
||||
with open(checkpoint_file, 'r') as f:
|
||||
checkpoint = datetime.strptime(f.read(), checkpoint_date_format)
|
||||
except:
|
||||
pass
|
||||
last_date = checkpoint
|
||||
while True:
|
||||
images = []
|
||||
with os.scandir(config.images_dir) as imd:
|
||||
with os.scandir(data_images_dir) as imd:
|
||||
for image_file in imd:
|
||||
if not image_file.is_file():
|
||||
continue
|
||||
@@ -130,8 +117,8 @@ def main():
|
||||
|
||||
for image_file in images:
|
||||
try:
|
||||
image_path = os.path.join(config.images_dir, image_file.name)
|
||||
labels_path = os.path.join(config.labels_dir, f'{Path(image_path).stem}.txt')
|
||||
image_path = os.path.join(data_images_dir, image_file.name)
|
||||
labels_path = os.path.join(data_labels_dir, f'{Path(image_path).stem}.txt')
|
||||
image = cv2.imdecode(np.fromfile(image_path, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
|
||||
process_image(ImageLabel(
|
||||
image_path=image_path,
|
||||
@@ -142,42 +129,14 @@ def main():
|
||||
except Exception as e:
|
||||
print(f'Error appeared {e}')
|
||||
if last_date != checkpoint:
|
||||
checkpoint = config.checkpoint = last_date
|
||||
config.write()
|
||||
checkpoint = last_date
|
||||
try:
|
||||
with open(checkpoint_file, 'w') as f:
|
||||
f.write(datetime.strftime(checkpoint, checkpoint_date_format))
|
||||
except:
|
||||
pass
|
||||
time.sleep(5)
|
||||
|
||||
|
||||
def check_labels():
|
||||
for label in os.listdir(os.path.join(current_dataset_dir, 'labels')):
|
||||
with open(os.path.join(current_dataset_dir, 'labels', label), 'r') as f:
|
||||
lines = f.readlines()
|
||||
for line in lines:
|
||||
list_c = line.split(' ')[1:]
|
||||
for l in list_c:
|
||||
if float(l) > 1:
|
||||
print('Error!')
|
||||
|
||||
|
||||
def fix_class(folder):
|
||||
for label in os.listdir(folder):
|
||||
if label.startswith('0000'):
|
||||
with open(os.path.join(folder, label), 'r+') as f:
|
||||
lines = f.readlines()
|
||||
truncated = False
|
||||
for i in range(0, len(lines)):
|
||||
if len(lines[i]) < 25:
|
||||
print(lines[i])
|
||||
truncated = True
|
||||
lines.pop(i)
|
||||
if truncated:
|
||||
f.truncate(0)
|
||||
f.seek(0)
|
||||
f.writelines(lines)
|
||||
f.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
fix_class('datasets/zombobase-2024-06-18/test/labels')
|
||||
fix_class('datasets/zombobase-2024-06-18/train/labels')
|
||||
fix_class('datasets/zombobase-2024-06-18/valid/labels')
|
||||
# main()
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user