skipping files if exists (for images only)

This commit is contained in:
Deen
2026-05-11 00:09:42 +03:00
committed by GitHub
parent 9572456e29
commit 355315f12f
@@ -114,44 +114,64 @@ class AnnotationQueueHandler:
is_val = ann.createdRole.is_validator()
try:
# save label anyway
# Зберігаємо текстовий лейбл
lbl = a.lbl_data if is_val else a.lbl_seed
with open(lbl, 'w') as label_file:
label_file.writelines([
f'{detection.cls} {detection.x} {detection.y} {detection.w} {detection.h}'
for detection in ann.detections
])
# Робота із зображенням
if ann.status == AnnotationStatus.Created:
img = a.img_data if is_val else a.img_seed
# ПЕРЕВІРКА: Якщо файл є і розмір збігається — логуємо і пропускаємо
if path.exists(img) and path.getsize(img) == len(ann.image):
logger.info(f"Skipped saving image (already exists with same size): {img}")
else:
with open(img, 'wb') as image_file:
image_file.write(ann.image)
else:
if is_val and path.exists(a.img_seed):
shutil.move(a.img_seed, a.img_data)
self._safe_move(a.img_seed, a.img_data)
if is_val and path.exists(a.lbl_seed):
os.remove(a.lbl_seed)
except IOError as e:
logger.error(f"Error during saving: {e}")
def _safe_move(self, src, dst):
"""Безпечне переміщення з перевіркою розміру файлу"""
if not path.exists(src):
return
actual_dst = path.join(dst, path.basename(src)) if path.isdir(dst) else dst
if path.exists(actual_dst):
# Якщо файли однакові за розміром — логуємо, видаляємо джерело і йдемо далі
if path.getsize(src) == path.getsize(actual_dst):
logger.info(f"Skipped moving file (already exists in destination): {actual_dst}")
os.remove(src)
return
else:
os.remove(actual_dst)
shutil.move(src, actual_dst)
def validate(self, msg):
for name in msg.annotation_names:
a = self.AnnotationName(self, name)
shutil.move(a.img_seed, a.img_data)
shutil.move(a.lbl_seed, a.lbl_data)
self._safe_move(a.img_seed, a.img_data)
self._safe_move(a.lbl_seed, a.lbl_data)
def delete(self, msg):
for name in msg.annotation_names:
a = self.AnnotationName(self, name)
if path.exists(a.img_data):
shutil.move(a.img_data, self.cfg.del_images_dir)
if path.exists(a.img_seed):
shutil.move(a.img_seed, self.cfg.del_images_dir)
if path.exists(a.lbl_data):
shutil.move(a.lbl_data, self.cfg.del_labels_dir)
if path.exists(a.lbl_seed):
shutil.move(a.lbl_seed, self.cfg.del_labels_dir)
self._safe_move(a.img_data, self.cfg.del_images_dir)
self._safe_move(a.img_seed, self.cfg.del_images_dir)
self._safe_move(a.lbl_data, self.cfg.del_labels_dir)
self._safe_move(a.lbl_seed, self.cfg.del_labels_dir)
if __name__ == '__main__':