#33: Switch to mono-thread

This commit is contained in:
Benjamin Loison 2024-04-06 00:00:24 +02:00
parent a83e8bb1e0
commit b18080e9f5
No known key found for this signature in database

View File

@ -6,44 +6,29 @@ import numpy as np
from PIL import Image
import os
from tqdm import tqdm
from multiprocessing import Pool
from threading import Lock
imagesFolderPath = 'flat-field'
npArrayFilePath = 'mean.npy'
mutex = Lock()
mean = None#np.zeros((3264, 4928, 3))
mean = None
numberOfImagesInMean = 0
imagesFileNames = os.listdir(imagesFolderPath)
pbar = tqdm(total = len(imagesFileNames))
imagesFileNames = os.listdir(imagesFolderPath)#[:2]
def treatImage(imageFileName):
global mean, numberOfImagesInMean, pbar
global mean, numberOfImagesInMean
imageFilePath = f'{imagesFolderPath}/{imageFileName}'
imagePil = Image.open(imageFilePath)
imageNpArray = img_as_float(np.array(imagePil))
print('Before mean computation')
imageNoiseNpArray = imageNpArray - denoise_tv_chambolle(imageNpArray, weight=0.2, channel_axis=-1)
print('After mean computation')
with mutex:
print('Start mutex section')
if mean is None:
print('Inter A')
mean = imageNoiseNpArray
else:
print('Inter B')
mean = ((mean * numberOfImagesInMean) + imageNoiseNpArray) / (numberOfImagesInMean + 1)
print('Intermediary 0')
numberOfImagesInMean += 1
print('Intermediary 1')
pbar.update(numberOfImagesInMean)
print('End mutex section')
if mean is None:
mean = imageNoiseNpArray
else:
mean = ((mean * numberOfImagesInMean) + imageNoiseNpArray) / (numberOfImagesInMean + 1)
numberOfImagesInMean += 1
with Pool(5) as p:
p.map(treatImage, imagesFileNames)
for imageFileName in tqdm(imagesFileNames):
treatImage(imageFileName)
with open(npArrayFilePath, 'wb') as f:
np.save(f, mean)