Created
April 28, 2016 20:38
-
-
Save patricksnape/7ee2a0b9535c4375f428bcd60b09d3ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "%matplotlib inline\nimport numpy as np\nfrom pathlib import Path\n\nimport menpo.io as mio\nfrom menpo.io.input.extensions import image_landmark_types\nfrom menpo.shape import PointCloud\n\nfrom menpofit.feature import normalize_std, normalize_norm\nfrom menpofit.clm import CLM, GradientDescentCLMFitter\n\nfrom menpowidgets import visualize_images\nfrom menpowidgets import visualize_shape_model\n\nDATASET_PATH = Path('/home/pts08/Downloads/dental/Challenge1')\nIMAGES_PATH = DATASET_PATH / 'RawImage/TrainingData'\nLANDMARKS_PATH = DATASET_PATH / 'GroundTruthData/Training'", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": true | |
}, | |
"cell_type": "code", | |
"source": "class MedicalTXTImporter(object):\n \n def __init__(self, filepath):\n self.filepath = filepath\n \n def build(self, asset=None):\n # Load the first 19 lines which are landmarks\n with open(self.filepath) as f:\n data = np.array([map(int, x.strip().split(',')) \n for x in f.readlines()[:19]])\n # Flip the x,y coordinates\n return PointCloud(data[:, ::-1])\n \nimage_landmark_types['.txt'] = MedicalTXTImporter\n\ndef medical_landmark_resolver(filepath):\n return {'txt': LANDMARKS_PATH / (filepath.stem + '.txt')}", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "def preprocess(image):\n # Save some memory by halfing the image sizes\n return image.rescale(0.5)\n\nimages = mio.import_images(IMAGES_PATH, \n landmark_resolver=medical_landmark_resolver)\nimages = list(images.map(preprocess))", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "visualize_images(images)", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "clm = CLM(images, \n verbose=True,\n scales=(0.25, 0.5, 1.0),\n diagonal=250,\n patch_normalisation=normalize_std,\n patch_shape=(17, 17))", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": true | |
}, | |
"cell_type": "code", | |
"source": "fitter = GradientDescentCLMFitter(clm, n_shape=[3, 5, 15])", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "im = images[0]\ngt_shape = im.landmarks['txt'].lms\nresult = fitter.fit_from_bb(im, gt_shape.bounding_box(), gt_shape=gt_shape, \n max_iters=30)", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "result.view_widget()", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "python2", | |
"display_name": "Python 2", | |
"language": "python" | |
}, | |
"language_info": { | |
"mimetype": "text/x-python", | |
"nbconvert_exporter": "python", | |
"name": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.11", | |
"file_extension": ".py", | |
"codemirror_mode": { | |
"version": 2, | |
"name": "ipython" | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment