Skip to content

Commit

Permalink
Analyze folder script is ready
Browse files Browse the repository at this point in the history
  • Loading branch information
lext committed Dec 14, 2019
1 parent 08059a7 commit 0a5aa1c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ The software is currently composed of four separate loosely-coupled services. Sp
The platform is designed so that it is possible to use `KNEEL` and `DeepKnee` separately. Both microservices expect
a `JSON` with `{dicom: <I64>}`, where `<I64>` is the dicom file encoded in `base64`. If you make a request to either of the services,
it needs to be done to `/kneel/predict/bilateral` or `/deepknee/predict/bilateral` for `KNEEL` and `DeepKnee`, respectively.
A Jupyter Notebook `request_example.ipynb` demonstrates an example of such request.

An example script that uses the platform can be found in the file `analyze_folder.py`.

## License
This code is freely available only for research purposes. Commercial use is not allowed by any means.
Expand Down
25 changes: 18 additions & 7 deletions analyze_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,25 @@ def base64tonumpy(buffer):

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--deepknee_host', default='http://127.0.0.1')
parser.add_argument('--deepknee_port', type=int, default=5001)
parser.add_argument('--img_dir', default='')
parser.add_argument('--save_results', default='/tmp/deepknee')
parser.add_argument('--deepknee_host', default='http://127.0.0.1', help='Host on which deepknee is running.')
parser.add_argument('--deepknee_port', type=int, default=5001, help='Port of deepknee.')
parser.add_argument('--img_dir', default='', help='Directory with images.')
parser.add_argument('--patient_level', type=bool, default=False,
help='Whether there image lies in a patient directory. '
'In this case, the script will know that the img_dir '
'contains folders that contain images.')
parser.add_argument('--save_results', default='/tmp/deepknee', help='Folder where to save the results.')
args = parser.parse_args()

url = f'{args.deepknee_host}:{args.deepknee_port}/deepknee/predict/bilateral'
os.makedirs(args.save_results, exist_ok=True)
output_csv = []
flist = glob.glob(os.path.join(args.img_dir, '*'))
for img_path in tqdm(flist, total=len(flist)):
if not args.patient_level:
flist = glob.glob(os.path.join(args.img_dir, '*'))
else:
flist = glob.glob(os.path.join(args.img_dir, '*', '*'))

for idx, img_path in tqdm(enumerate(flist), total=len(flist)):
# Encoding the DICOM as base64 and sending the request to the server
with open(img_path, 'rb') as f:
data_base64 = base64.b64encode(f.read()).decode('ascii')
Expand All @@ -39,9 +47,12 @@ def base64tonumpy(buffer):
# You can also access the localized image, heatmaps and the probability maps
result[knee] = {'img': base64tonumpy(res[knee]['img']),
'hm': base64tonumpy(res[knee]['hm']),
'preds_bar': base64tonumpy(res[knee]['preds_bar']),
'probs_bar': base64tonumpy(res[knee]['preds_bar']),
'kl': res[knee]['kl']}
output_csv.append({'File': img_path, 'Side': knee, 'KL': result[knee]['kl']})
cv2.imwrite(os.path.join(args.save_results, f'{idx}_{knee}_img.png'), result[knee]['img'])
cv2.imwrite(os.path.join(args.save_results, f'{idx}_{knee}_hm.png'), result[knee]['hm'])
cv2.imwrite(os.path.join(args.save_results, f'{idx}_{knee}_probs.png'), result[knee]['probs_bar'])

df = pd.DataFrame(data=output_csv)
df.to_csv(os.path.join(args.save_results, 'deepknee.csv'), index=None)

0 comments on commit 0a5aa1c

Please sign in to comment.