diff --git a/README.md b/README.md index d5a0f73..b1418a7 100644 --- a/README.md +++ b/README.md @@ -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: }`, where `` 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. diff --git a/analyze_folder.py b/analyze_folder.py index 8a08579..c9be03d 100644 --- a/analyze_folder.py +++ b/analyze_folder.py @@ -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') @@ -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)