Commit fc4a9eb6 authored by josh's avatar josh
Browse files

db ingestion implemented, examples in readme

parent 779db4f8
......@@ -46,42 +46,11 @@ python ./Scripts/sliceImage.py ./TestResults/Slice/GeoEye_Original.jpg GeoEye_Sl
- arg5: range radius for ms algo
- arg6: min density for ms algo
## Example run:
## Example run:
```bash
python ./Scripts/meanShiftSeg.py ./TestResults/MS/GeoEye_MS_Original.jpg ./TestResults/MS pyms 6 6 50
```
## "initDB.py" program:
- Sets up the base file structure for an image database at a given location.
## Directory Visual with no data:
```
|-- ImageDatabase
│   |-- MeanShift
│   |-- Original
│   |-- Sliced
```
## Directory Visual with an image:
```
|-- 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
| | |--image_name-id.<ext> <- original image, id = auto increment integer
│   |-- Sliced
| | |--image_name-id
| | | |--128x128
| | | | |--image_name-128-0-0.<ext> <- sliced image
| | | |--256x256
| | | |--512x512
```
## Example run result:
## Original Image:
![](TestResults/MS/GeoEye_MS_Original.jpg)
......@@ -93,6 +62,109 @@ python ./Scripts/meanShiftSeg.py ./TestResults/MS/GeoEye_MS_Original.jpg ./TestR
![](TestResults/MS/pyms-labels-image.jpg)
## "initDB.py" program:
- Sets up the base file structure for an image database at a given location.
## Example run:
```bash
python Scripts/initDB.py ../ ImageDatabase
```
## Directory Structure Result:
```
├── ImageDatabase
│   ├── MeanShift
│   ├── Original
│   └── Sliced
```
## "importAndProcess.py"
- Import and process image
## Example run:
```bash
python Scripts/importAndProcessImage.py TestResults/MS/GeoEye_MS_Original.jpg ../ImageDatabase/ GeoEye
```
## Database Directory Result:
```
├── MeanShift
│   └── GeoEye-0
│   ├── 128
│   │   ├── GeoEye-0-0-color-seg.jpg
│   │   ├── GeoEye-0-0-labels-image.jpg
│   │   ├── GeoEye-0-128-color-seg.jpg
│   │   ├── GeoEye-0-128-labels-image.jpg
│   │   ├── GeoEye-0-256-color-seg.jpg
│   │   ├── GeoEye-0-256-labels-image.jpg
│   │   ├── GeoEye-0-384-color-seg.jpg
│   │   ├── GeoEye-0-384-labels-image.jpg
│   │   ├── GeoEye-128-0-color-seg.jpg
│   │   ├── GeoEye-128-0-labels-image.jpg
│   │   ├── GeoEye-128-128-color-seg.jpg
│   │   ├── GeoEye-128-128-labels-image.jpg
│   │   ├── GeoEye-128-256-color-seg.jpg
│   │   ├── GeoEye-128-256-labels-image.jpg
│   │   ├── GeoEye-128-384-color-seg.jpg
│   │   ├── GeoEye-128-384-labels-image.jpg
│   │   ├── GeoEye-256-0-color-seg.jpg
│   │   ├── GeoEye-256-0-labels-image.jpg
│   │   ├── GeoEye-256-128-color-seg.jpg
│   │   ├── GeoEye-256-128-labels-image.jpg
│   │   ├── GeoEye-256-256-color-seg.jpg
│   │   ├── GeoEye-256-256-labels-image.jpg
│   │   ├── GeoEye-256-384-color-seg.jpg
│   │   ├── GeoEye-256-384-labels-image.jpg
│   │   ├── GeoEye-384-0-color-seg.jpg
│   │   ├── GeoEye-384-0-labels-image.jpg
│   │   ├── GeoEye-384-128-color-seg.jpg
│   │   ├── GeoEye-384-128-labels-image.jpg
│   │   ├── GeoEye-384-256-color-seg.jpg
│   │   ├── GeoEye-384-256-labels-image.jpg
│   │   ├── GeoEye-384-384-color-seg.jpg
│   │   └── GeoEye-384-384-labels-image.jpg
│   ├── 256
│   │   ├── GeoEye-0-0-color-seg.jpg
│   │   ├── GeoEye-0-0-labels-image.jpg
│   │   ├── GeoEye-0-256-color-seg.jpg
│   │   ├── GeoEye-0-256-labels-image.jpg
│   │   ├── GeoEye-256-0-color-seg.jpg
│   │   ├── GeoEye-256-0-labels-image.jpg
│   │   ├── GeoEye-256-256-color-seg.jpg
│   │   └── GeoEye-256-256-labels-image.jpg
│   └── 512
│   ├── GeoEye-0-0-color-seg.jpg
│   └── GeoEye-0-0-labels-image.jpg
├── Original
│   └── GeoEye-0.jpg
└── Sliced
└── GeoEye-0
├── 128
│   ├── GeoEye-0-0.jpg
│   ├── GeoEye-0-128.jpg
│   ├── GeoEye-0-256.jpg
│   ├── GeoEye-0-384.jpg
│   ├── GeoEye-128-0.jpg
│   ├── GeoEye-128-128.jpg
│   ├── GeoEye-128-256.jpg
│   ├── GeoEye-128-384.jpg
│   ├── GeoEye-256-0.jpg
│   ├── GeoEye-256-128.jpg
│   ├── GeoEye-256-256.jpg
│   ├── GeoEye-256-384.jpg
│   ├── GeoEye-384-0.jpg
│   ├── GeoEye-384-128.jpg
│   ├── GeoEye-384-256.jpg
│   └── GeoEye-384-384.jpg
├── 256
│   ├── GeoEye-0-0.jpg
│   ├── GeoEye-0-256.jpg
│   ├── GeoEye-256-0.jpg
│   └── GeoEye-256-256.jpg
└── 512
└── GeoEye-0-0.jpg
```
## "meanShift3Channel.py" program:
Uses the mean shift algorithm to create a segmentation mask for an image.
......@@ -100,7 +172,7 @@ Uses the mean shift algorithm to create a segmentation mask for an image.
- arg1: image file used for segmentation
- arg2: file name to save segmentation
## Example run:
## Example run:
```bash
python ./Scripts/meanShift3Channel.py ./TestResults/MS/GeoEye_MS_Original.jpg ./TestResults/MS/three-channel-seg
```
......
......@@ -10,6 +10,7 @@ from matplotlib.colors import to_rgb
from sklearn.cluster import MeanShift, estimate_bandwidth
from skimage import color
def sliceImage( imgArr: np.array, newSize: tuple ) -> dict:
'''
Creates slices of an image.
......@@ -57,7 +58,7 @@ def sliceImage( imgArr: np.array, newSize: tuple ) -> dict:
}
def meanShiftSegmentation( imgArr: np.array, spatialRadius: int=6, rangeRadius: int=6, minDensity: int=50 ):
def meanShiftSegmentation( imgArr: np.array, spatialRadius: int=6, rangeRadius: int=6, minDensity: int=50 ) -> tuple:
'''
SOURCE - https://github.com/fjean/pymeanshift
......@@ -123,6 +124,98 @@ def initDB( dbLoc: str, name: str ) -> bool:
return True
def importAndProcess( imgPath: str, dbPath: str, newImgName: str ):
'''
Returns: Name used to query DB for image
'''
imgPath = pathlib.Path( imgPath ).absolute()
dbPath = pathlib.Path( dbPath ).absolute()
# Ensure database exists
if not dbPath.is_dir():
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" )
# Ensure database was initialized correctly
if not originalPath.is_dir() or not meanShiftPath.is_dir() or not slicedPath.is_dir():
print( "ERROR - Database Not Initialized: Use 'initDB.py' to init database.")
return False
# PROCESS IMAGE
# Step 1: Read in image
imgArr = cv2.imread( str( imgPath ) )
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 ) )
os.mkdir( imgSlicedPath )
os.mkdir( imgMSPath )
sizes = [ 128, 256, 512 ]
# Loop: 128, 256, 512
for size in sizes:
print( "Processing Size:", size )
# Step 3: Create Dirs - always create dir even if image wont generate slices of a size
# MeanShift/image_name-id/<size>
# Sliced/image_name-id/<size>
imgSlicedSizePath = imgSlicedPath.joinpath( str( size ) )
imgMSSizePath = imgMSPath.joinpath( str( size ) )
os.mkdir( imgSlicedSizePath )
os.mkdir( imgMSSizePath )
# Step 4: Slice
sliceResults = sliceImage( imgArr, ( size, size ) )
# Step 5: Save slices
for res in sliceResults["slicedImages"]:
i = res[0]
j = res[1]
slicedImg = res[2]
finalFileName = "{}-{}-{}".format( newImgName, i, j )
sliceSizePath = imgSlicedSizePath.joinpath( "{}.{}".format( finalFileName, fileExt ) )
print( "SAVING IMAGE:", sliceSizePath )
cv2.imwrite( str( sliceSizePath ), slicedImg )
# Step 6: Mean Shift
( segmentedImage, labelsImage, numberRegions ) = meanShiftSegmentation( slicedImg )
# Step 7: Save MS results
segColorPath = imgMSSizePath.joinpath( "{}-color-seg.{}".format( finalFileName, fileExt ) )
labelImgPath = imgMSSizePath.joinpath( "{}-labels-image.{}".format( finalFileName, fileExt ) )
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 ), segmentedImage )
cv2.imwrite( str( labelImgPath ), labelsImage )
'''
OTHER IMPLEMENTATIONS
'''
......@@ -201,7 +294,3 @@ def meanshift3Channel( imagePath: str, outPath: str, quantile=0.2, samples=500,
1: ( classColors[1], color2 ),
2: ( classColors[2], color3 )
}
if __name__ == "__main__":
dbLoc = "../"
initDB( dbLoc, "ImageDatabase" )
\ No newline at end of file
import sys
import pathlib
from ai4hdr_utils import importAndProcess
if __name__ == "__main__":
'''
Required arguments:
- arg1: path to the image
- arg2: path to database
'''
if len( sys.argv ) == 4:
imgPath = sys.argv[1]
dbPath = sys.argv[2]
newName = sys.argv[3]
importAndProcess( imgPath=imgPath, dbPath=dbPath, newImgName=newName )
else:
print( "Invalid arg count" )
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