generated from dopt-python/py311
rework unneeded checks and iterations
This commit is contained in:
parent
cc664b0369
commit
98d340ff01
@ -1,5 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
@ -31,7 +32,12 @@ class Preparation:
|
|||||||
# / Path(self.folder_path).parents[1].name
|
# / Path(self.folder_path).parents[1].name
|
||||||
# / Path(self.folder_path).parent.name
|
# / Path(self.folder_path).parent.name
|
||||||
# ) # Pfad zu Fileserver/KI/...
|
# ) # 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...")
|
print(f"[{self.visper}] {Path(self.folder_path).name} Vorbereitung gestartet...")
|
||||||
|
|
||||||
@ -105,16 +111,17 @@ class Preparation:
|
|||||||
# copytree(src, dst, dirs_exist_ok=True)
|
# copytree(src, dst, dirs_exist_ok=True)
|
||||||
|
|
||||||
src = self.path
|
src = self.path
|
||||||
dst = self.original_data_path / self.path.name
|
# dst = self.original_data_base_path / self.path.name
|
||||||
copytree(src, dst, dirs_exist_ok=True)
|
copytree(src, self.original_data_path, dirs_exist_ok=True)
|
||||||
|
|
||||||
# ?? Soll hier der Pfad mit den kopierten Originaldaten zurückgegeben werden?
|
# ?? 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
|
# Das ist aktuell nicht der Fall, da die letzten Pfadkomponenten als absolute Pfade das
|
||||||
# Ergebnis komplett überschreiben
|
# Ergebnis komplett überschreiben
|
||||||
|
# Zudem wird der Basispfad (also Produktebene, nicht Päckchen zurückgegeben)!
|
||||||
# return Path(config.STORING_PATH) / self.visper / Path(
|
# return Path(config.STORING_PATH) / self.visper / Path(
|
||||||
# self.folder_path
|
# self.folder_path
|
||||||
# ).parent.parent / Path(self.folder_path).parent, None
|
# ).parent.parent / Path(self.folder_path).parent, None
|
||||||
return self.original_data_path, None
|
return self.original_data_base_path, None
|
||||||
|
|
||||||
except FileExistsError:
|
except FileExistsError:
|
||||||
return (
|
return (
|
||||||
@ -214,10 +221,19 @@ class Preparation:
|
|||||||
self, current_folder_to_inspect, checkimg_folders, folder_type
|
self, current_folder_to_inspect, checkimg_folders, folder_type
|
||||||
):
|
):
|
||||||
for folder_to_inspect in checkimg_folders:
|
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 (
|
if folder_to_inspect.is_dir() and (
|
||||||
Path(folder_to_inspect).parts[-2] == Path(current_folder_to_inspect).parts[-2]
|
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:])
|
relative_path = Path(*relative_path.parts[1:])
|
||||||
return relative_path
|
return relative_path
|
||||||
|
|
||||||
@ -243,7 +259,7 @@ class Preparation:
|
|||||||
|
|
||||||
# find all checkimg folders within the folder
|
# find all checkimg folders within the folder
|
||||||
checkimg_folders = [
|
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
|
# iterate through all 'checkimg' folders recursively
|
||||||
@ -293,8 +309,10 @@ class Preparation:
|
|||||||
|
|
||||||
# create folder name in our temp folder "Backup" and store it
|
# create folder name in our temp folder "Backup" and store it
|
||||||
# therefore, first extract the name of the current folder from the whole path
|
# therefore, first extract the name of the current folder from the whole path
|
||||||
folder_name = Path(self.folder_path).name
|
# folder_name = self.path.name
|
||||||
new_folder_path = Path(config.CURRENT_PATH_RGB) / folder_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:
|
try:
|
||||||
new_folder_path.mkdir(parents=True, exist_ok=False)
|
new_folder_path.mkdir(parents=True, exist_ok=False)
|
||||||
@ -307,21 +325,35 @@ class Preparation:
|
|||||||
return None, f"Fehlermeldung: {e}"
|
return None, f"Fehlermeldung: {e}"
|
||||||
|
|
||||||
# find all checkimg folders within the folder
|
# 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()
|
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
|
# 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
|
# identify the path starting from self.folder_path until the checkimg folder
|
||||||
relative_path = self.extract_folder_path_within_one_AOI_folder(
|
# relative_path = self.extract_folder_path_within_one_AOI_folder(
|
||||||
current_folder_to_inspect, checkimg_folders, "checkimg"
|
# 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)
|
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
|
# find match according to pattern defined at the very beginning
|
||||||
match = pattern.match(file_path.name)
|
match = pattern.match(file_path.name)
|
||||||
if not match:
|
if not match:
|
||||||
@ -331,14 +363,14 @@ class Preparation:
|
|||||||
|
|
||||||
# find all three images belonging together
|
# find all three images belonging together
|
||||||
r_path = file_path
|
r_path = file_path
|
||||||
g_path = current_folder_to_inspect / f"G_NG{num1}_{num2}.jpg"
|
g_path = checkimg_folder / f"G_NG{num1}_{num2}.jpg"
|
||||||
b_path = current_folder_to_inspect / f"B_NG{num1}_{num2}.jpg"
|
b_path = checkimg_folder / f"B_NG{num1}_{num2}.jpg"
|
||||||
|
|
||||||
# open all three images and combine them to RGB
|
# open all three images and combine them to RGB
|
||||||
r = pyvips.Image.new_from_file(r_path, access="sequential")
|
r = pyvips.Image.new_from_file(r_path, access="sequential")
|
||||||
g = pyvips.Image.new_from_file(g_path, access="sequential")
|
g = pyvips.Image.new_from_file(g_path, access="sequential")
|
||||||
b = pyvips.Image.new_from_file(b_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")
|
rgb_image = rgb_image.copy(interpretation="srgb")
|
||||||
filename = f"RGB_NG{num1}_{num2}.png"
|
filename = f"RGB_NG{num1}_{num2}.png"
|
||||||
rgb_image.write_to_file(save_path_rgb / filename)
|
rgb_image.write_to_file(save_path_rgb / filename)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user