Commit 9c1f1f1b authored by Josh's avatar Josh
Browse files

changed the structure of the database and updated 'initDB' and 'importAndProcess'

parent 379eff76
......@@ -5,28 +5,17 @@ from ai4hdr_utils import *
Database file structure example:
|-- ImageDatabase
│   |-- MeanShift
| | |--image_name-id
| | | |--128x128
| | | | |--image_name-128-smap-0-0.<ext> <- segmented map
| | | | |--image_name-128-simage-0-0.<ext> <- segmented image
| | | |--256x256
| | | |--512x512
│   |-- Original
| | |--uniqueImageId <-- dir name with unique id created on import, used for querying
| | | |--uniqueImageId.<ext>
│   |-- Images
| | |--<autoInc>-<imgExt>-<name>
| | | |--<autoInc>-<imgExt>-<name>-Original.<imgExt>
| | | |--Sliced
| | | | |--128x128
| | | | | |--uniqueSliceId.<ext> <-- used to query database
| | | | | |--image_name-128-simage-0-0.<ext> <- segmented image
| | | | | |--<autoInc>-<imgExt>-<name>-128-xPix-yPix
| | | | | | |--<autoInc>-<imgExt>-<name>-128-xPix-yPix-Slice.<imgExt>
| | | | | | |--Processed
| | | | | | | |--<autoInc>-<imgExt>-<name>-128-xPix-yPix-<procType>-Processed.<imgExt>
| | | | |--256x256
| | | | |--512x512
│   |-- Sliced
| | |--image_name-id
| | | |--128x128
| | | | |--image_name-128-0-0.<ext> <- sliced image
| | | |--256x256
| | | |--512x512
'''
def initDB( dbLoc: str, name: str ) -> bool:
......@@ -34,9 +23,7 @@ def initDB( dbLoc: str, name: str ) -> bool:
Initialized the image database at a given directory
File Struture:
|-- ImageDatabase
│   |-- MeanShift
│   |-- Original
│   |-- Sliced
│   |-- Data
'''
dbLoc = pathlib.Path( dbLoc )
......@@ -44,20 +31,20 @@ def initDB( dbLoc: str, name: str ) -> bool:
if dbDir.is_dir():
return False
else:
originalImagesDir = dbDir.joinpath( "Original" )
slicedImagesDir = dbDir.joinpath( "Sliced" )
meanShiftSegDir = dbDir.joinpath( "MeanShift" )
imagesDir = dbDir.joinpath( "Images" )
#slicedImagesDir = dbDir.joinpath( "Sliced" )
#meanShiftSegDir = dbDir.joinpath( "MeanShift" )
os.mkdir( dbDir )
os.mkdir( originalImagesDir )
os.mkdir( slicedImagesDir )
os.mkdir( meanShiftSegDir )
os.mkdir( imagesDir )
#os.mkdir( slicedImagesDir )
#os.mkdir( meanShiftSegDir )
return True
def importAndProcess( imgPath: str, dbPath: str, newImgName: str ):
'''
Returns: Name used to query DB for image
Slices and segments an image and imports it into the database
'''
imgPath = pathlib.Path( imgPath ).absolute()
dbPath = pathlib.Path( dbPath ).absolute()
......@@ -67,13 +54,11 @@ def importAndProcess( imgPath: str, dbPath: str, newImgName: str ):
print( "ERROR: No database exists at {}".format( dbPath ) )
return False
# Internal database directories
originalPath = dbPath.joinpath( "Original" )
meanShiftPath = dbPath.joinpath( "MeanShift" )
slicedPath = dbPath.joinpath( "Sliced" )
# Internal database directory
imagesPath = dbPath.joinpath( "Images" )
# Ensure database was initialized correctly
if not originalPath.is_dir() or not meanShiftPath.is_dir() or not slicedPath.is_dir():
if not imagesPath.is_dir():
print( "ERROR - Database Not Initialized: Use 'initDB.py' to init database.")
return False
......@@ -84,63 +69,66 @@ def importAndProcess( imgPath: str, dbPath: str, newImgName: str ):
fileExt = imgPath.name.split( "." )[-1]
# Auto increment id
dbID = len( [f for f in originalPath.iterdir() ] )
# Step 2: Save to Original, create MS, Sliced dirs
originalFileName = originalPath.joinpath( "{}-{}.{}".format( newImgName, dbID, fileExt ) )
cv2.imwrite( str( originalFileName ), imgArr )
imgSlicedPath = slicedPath.joinpath( "{}-{}".format( newImgName, dbID ) )
imgMSPath = meanShiftPath.joinpath( "{}-{}".format( newImgName, dbID ) )
dbIDs = [int( f.name.split( "-" )[0] ) for f in imagesPath.iterdir() ]
dbID = max( dbIDs ) + 1
# Create dir for image
baseImagePath = imagesPath.joinpath( "{}-{}-{}".format( dbID, fileExt, newImgName ) )
baseImageName = baseImagePath.name
os.mkdir( baseImagePath )
# Step 2: Save Original File
baseImageFilePath = baseImagePath.joinpath( "{}-{}.{}".format( baseImageName, "Original", fileExt ) )
cv2.imwrite( str( baseImageFilePath ), imgArr )
# Create dir for sliced images
slicedImagePath = baseImagePath.joinpath( "Sliced" )
os.mkdir( slicedImagePath )
# Run processing
processedResult = sliceAndMeanShift( imgArr )
# Create slice and meanshift dirs
os.mkdir( imgSlicedPath )
os.mkdir( imgMSPath )
# Save Slices
sizes = [ "128", "256", "512" ]
print( "|--------------- SLICES ---------------|" )
for size in sizes:
imgSlicedSizePath = imgSlicedPath.joinpath( str( size ) )
os.mkdir( imgSlicedSizePath )
for imgRes in processedResult["slices"][size]:
i = imgRes[0]
j = imgRes[1]
slicedImg = imgRes[2]
finalFileName = "{}-{}-{}".format( newImgName, i, j )
sliceSizePath = imgSlicedSizePath.joinpath( "{}.{}".format( finalFileName, fileExt ) )
print( "SAVING IMAGE:", sliceSizePath )
cv2.imwrite( str( sliceSizePath ), slicedImg )
print( "|--------------- MEAN SHIFT ---------------|" )
# Save Mean Shift Results
for size in sizes:
imgMSSizePath = imgMSPath.joinpath( str( size ) )
os.mkdir( imgMSSizePath )
for imgRes in processedResult["meanShift"][size]:
i = imgRes[0]
j = imgRes[1]
numRegions = imgRes[2]
segImage = imgRes[3]
labelImage = imgRes[4]
finalFileName = "{}-{}-{}".format( newImgName, i, j )
segColorPath = imgMSSizePath.joinpath( "{}-color-seg.{}".format( finalFileName, fileExt ) )
labelImgPath = imgMSSizePath.joinpath( "{}-labels-image".format( finalFileName ) )
print( "SAVING IMAGE:", segColorPath )
print( "SAVING IMAGE:", labelImgPath )
## scale labelsImage to [0,255]
#labelsImage = labelsImage.astype( float )
#labelsImage /= labelsImage.max()
#labelsImage *= 255
## save results to image files
cv2.imwrite( str( segColorPath ), segImage )
#cv2.imwrite( str( labelImgPath ), labelImage )
np.save( str( labelImgPath ), labelImage )
\ No newline at end of file
slicedSizePath = slicedImagePath.joinpath( size )
os.mkdir( slicedSizePath )
# Slices and corresponding processed images
sliceList = processedResult["slices"][size]
processedList = processedResult["meanShift"][size]
for i in range( len( sliceList ) ):
# <autoInc>-<imgExt>-<name>-128-xPix-yPix
x = sliceList[i][0]
y = sliceList[i][1]
sliceArr = sliceList[i][2]
sliceBaseName = "{}-{}-{}-{}".format( baseImageName, size, x, y )
sliceBasePath = slicedSizePath.joinpath( sliceBaseName )
os.mkdir( sliceBasePath )
# Save Sliced Image
sliceFilePath = sliceBasePath.joinpath( "{}-{}.{}".format( sliceBaseName, "Sliced", fileExt ) )
cv2.imwrite( str( sliceFilePath ), sliceArr )
# Create Processed Slice Path
sliceProcPath = sliceBasePath.joinpath( "Processed" )
os.mkdir( sliceProcPath )
# Segmented image and label image
msSegArr = processedList[i][3]
msLabImg = processedList[i][4]
msSegFileDir = sliceProcPath.joinpath( "{}-{}-{}.{}".format( sliceBaseName,
"MeanShiftSegmentation",
"Processed",
fileExt ) )
msLabFileDir = sliceProcPath.joinpath( "{}-{}-{}".format( sliceBaseName,
"MeanShiftLabels",
"Processed" ) )
# Save processed image and label array
cv2.imwrite( str( msSegFileDir ), msSegArr )
np.save( str( msLabFileDir ), msLabImg )
\ No newline at end of file
import sys
import pathlib
from ai4hdr_utils import importAndProcess
from ai4hdrDatabase import importAndProcess
if __name__ == "__main__":
'''
......
import sys
import pathlib
from ai4hdr_utils import initDB
from ai4hdrDatabase import initDB
if __name__ == "__main__":
'''
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment