working demo code with significant speed-up #1

Merged
foefl merged 8 commits from demo into main 2026-03-02 14:49:18 +00:00
Showing only changes of commit 98d340ff01 - Show all commits

View File

@ -1,5 +1,6 @@
import os
import re
import sys
import traceback
from pathlib import Path
from pprint import pprint
@ -31,7 +32,12 @@ class Preparation:
# / Path(self.folder_path).parents[1].name
# / Path(self.folder_path).parent.name
# ) # Pfad zu Fileserver/KI/...
self.original_data_path = Path(config.STORING_PATH).joinpath(*self.path.parts[-4:-1])
# the original path's last component is the product type name instead of the package
self.original_data_base_path = Path(config.STORING_PATH).joinpath(
*self.path.parts[-4:-1]
)
self.original_data_path = self.original_data_base_path / self.path.name
print(f"[{self.visper}] {Path(self.folder_path).name} Vorbereitung gestartet...")
@ -105,16 +111,17 @@ class Preparation:
# copytree(src, dst, dirs_exist_ok=True)
src = self.path
dst = self.original_data_path / self.path.name
copytree(src, dst, dirs_exist_ok=True)
# dst = self.original_data_base_path / self.path.name
copytree(src, self.original_data_path, dirs_exist_ok=True)
# ?? Soll hier der Pfad mit den kopierten Originaldaten zurückgegeben werden?
# Das ist aktuell nicht der Fall, da die letzten Pfadkomponenten als absolute Pfade das
# Ergebnis komplett überschreiben
# Zudem wird der Basispfad (also Produktebene, nicht Päckchen zurückgegeben)!
# return Path(config.STORING_PATH) / self.visper / Path(
# self.folder_path
# ).parent.parent / Path(self.folder_path).parent, None
return self.original_data_path, None
return self.original_data_base_path, None
except FileExistsError:
return (
@ -214,10 +221,19 @@ class Preparation:
self, current_folder_to_inspect, checkimg_folders, folder_type
):
for folder_to_inspect in checkimg_folders:
print(
f"\n-------------------------------\n{folder_to_inspect=}\n,{current_folder_to_inspect=}\n"
)
check_1 = Path(folder_to_inspect).parts[-2]
check_2 = Path(current_folder_to_inspect).parts[-2]
print(f"{check_1=},\n{check_2=}\n------------------------------------")
if folder_to_inspect.is_dir() and (
Path(folder_to_inspect).parts[-2] == Path(current_folder_to_inspect).parts[-2]
):
relative_path = current_folder_to_inspect.relative_to(self.original_data_path)
relative_path = current_folder_to_inspect.relative_to(
self.original_data_base_path
)
relative_path = Path(*relative_path.parts[1:])
return relative_path
@ -243,7 +259,7 @@ class Preparation:
# find all checkimg folders within the folder
checkimg_folders = [
p for p in self.original_data_path.rglob("checkimg") if p.is_dir()
p for p in self.original_data_base_path.rglob("checkimg") if p.is_dir()
]
# iterate through all 'checkimg' folders recursively
@ -293,8 +309,10 @@ class Preparation:
# create folder name in our temp folder "Backup" and store it
# therefore, first extract the name of the current folder from the whole path
folder_name = Path(self.folder_path).name
new_folder_path = Path(config.CURRENT_PATH_RGB) / folder_name
# folder_name = self.path.name
# print("folder name: ", folder_name)
new_folder_path = Path(config.CURRENT_PATH_RGB) / self.path.name
# print("new_folder_path: ", new_folder_path)
try:
new_folder_path.mkdir(parents=True, exist_ok=False)
@ -307,21 +325,35 @@ class Preparation:
return None, f"Fehlermeldung: {e}"
# find all checkimg folders within the folder
checkimg_folders = [
# ?? Hier gewinnen wir wieder alle Verzeichnisse oberhalb der Paketebene, d.h.
# ?? unabhängig vom Päckchen
# Annahme: Wir wollen tatsächlich nur auf Päckchenebene arbeiten
checkimg_folders = tuple(
p for p in self.original_data_path.rglob("checkimg") if p.is_dir()
]
)
# print(f">>> {checkimg_folders=}")
# sys.exit(0)
# iterate through all 'checkimg' folders recursively
for current_folder_to_inspect in checkimg_folders:
for checkimg_folder in checkimg_folders:
# ?? Das scheint unnötig, da wir nun nur für das Päckchen relevante checkimg
# ?? Ordner durchsuchen bei jeder Iteration. Damit können wir direkt auf den
# ?? Pfaden arbeiten.
# identify the path starting from self.folder_path until the checkimg folder
relative_path = self.extract_folder_path_within_one_AOI_folder(
current_folder_to_inspect, checkimg_folders, "checkimg"
)
# relative_path = self.extract_folder_path_within_one_AOI_folder(
# checkimg_folder, checkimg_folders, "checkimg"
# )
# save_path_rgb = new_folder_path / relative_path
# save_path_rgb.mkdir(parents=True, exist_ok=True)
save_path_rgb = new_folder_path / relative_path
relative_path = checkimg_folder.parts[-2:]
save_path_rgb = new_folder_path.joinpath(*relative_path)
save_path_rgb.mkdir(parents=True, exist_ok=True)
# print(f">>>> {relative_path=}")
# print(f">>>> {save_path_rgb=}")
for file_path in current_folder_to_inspect.glob("R_NG*_*.jpg"):
for file_path in checkimg_folder.glob("R_NG*_*.jpg"):
# find match according to pattern defined at the very beginning
match = pattern.match(file_path.name)
if not match:
@ -331,14 +363,14 @@ class Preparation:
# find all three images belonging together
r_path = file_path
g_path = current_folder_to_inspect / f"G_NG{num1}_{num2}.jpg"
b_path = current_folder_to_inspect / f"B_NG{num1}_{num2}.jpg"
g_path = checkimg_folder / f"G_NG{num1}_{num2}.jpg"
b_path = checkimg_folder / f"B_NG{num1}_{num2}.jpg"
# open all three images and combine them to RGB
r = pyvips.Image.new_from_file(r_path, access="sequential")
g = pyvips.Image.new_from_file(g_path, access="sequential")
b = pyvips.Image.new_from_file(b_path, access="sequential")
rgb_image = r.bandjoin([g, b])
rgb_image = r.bandjoin([g, b]) # type: ignore
rgb_image = rgb_image.copy(interpretation="srgb")
filename = f"RGB_NG{num1}_{num2}.png"
rgb_image.write_to_file(save_path_rgb / filename)