2

I have a QGIS 3.2 workflow that helps me to mass produce image classiication labels for some deep learning work. The workflow creates multiple files with various prefixes in tif format. At the end of it all, the python script below does checks for bugs in some files, applies compression to some tif files, moves the needed files into a designated folder for training a CNN and then deletes files that are no longer needed. If I run this in Spyder (and quit QGIS) it works fine. But for the sake of simplicity and productivity, I'm trying to run it in the QGIS python console. I've successfully installed skimage in the QGIS environment, so no problem with modules. But, even if I remove all layers from QGIS, I still get permission errors that are presumably from a file lock imposed by QGIS. I need to reboot QGIS which cancels out any time gained when compared to running this in Spyder. Is there a quick pyqgis command I could issue at the end of the code below to just release all locks? note that I don't even know a priori which layers are locked.

import os
import shutil
import numpy as np
from skimage.external.tifffile import imsave
from skimage import io

#############################################################
#User data input. Fill in the info below before running
PreProcessPath = "D:\\DeepRiverscapes\\PreProcess\\" 
TrainPath = "D:\\DeepRiverscapes\\Predict\\"  
RiverName = "StMarg" 

################################################################
################################################################
#Reconvert the segments to a 1 band set of 1 label objects with integer labels without gaps 

def RebuildSegments(Segments):
    FloatValues = np.unique(Segments)
    IntSegments = np.zeros(Segments.shape)
    S=1
    for n in range(0,len(FloatValues)):
        IntSegments[Segments == FloatValues[n]] = S
        S+=1
    return np.int32(IntSegments)

#Check for anomalies in the classification
def CheckClass(Class, name):
        if np.max(Class) > 7:
            print('WARNING some training class labels are greater than expected')
            print(name)
        elif np.min(Class) < 0:
            print('WARNING some training class labels are negative')
            print(name)
        elif np.count_nonzero(Class != np.int32(Class)):
            print('WARNING some training class labels are non-integers')
            print(name)



for i in range(0,99999): #goes to 99999 but do not use that many images!  It will just scan through the possible names
       ImPath = PreProcessPath + RiverName + format(i,'05d') +'_modified' + '.tif'
       ImPathIn = PreProcessPath + RiverName + format(i,'05d') + '.jpg'
       ClassPath = PreProcessPath + 'R_VECT_' + RiverName + format(i,'05d') +'_modified'+'.tif'
       SegPath = PreProcessPath + 'SEG_' + RiverName + format(i,'05d') +'_modified' + '.tif'
       ImPathOut = TrainPath + RiverName + format(i,'05d')  + '.jpg'
       ClassPathOut = TrainPath + 'CLS_' + RiverName + format(i,'05d') +'.tif'
       SegPathOut = TrainPath + 'SEG_' + RiverName + format(i,'05d') + '.tif'
       if os.path.exists(ClassPath):

           Class = io.imread(ClassPath)
           CheckClass(Class,ClassPath)
           imsave(ClassPathOut, Class, compress=7)
           Segments = io.imread(SegPath, as_grey = True)
           Segments_int = RebuildSegments(Segments)
           imsave(SegPathOut, Segments_int, compress=7)
           #Cleanup and move operations
           shutil.move(ImPathIn,ImPathOut)
           os.remove(SegPath)
           os.remove(ImPath)
           os.remove(ClassPath)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.