| import glob |
| import json |
| import os |
| import trimesh |
| import numpy as np |
| import traceback |
|
|
| class NpEncoder(json.JSONEncoder): |
| def default(self, obj): |
| if isinstance(obj, np.integer): |
| return int(obj) |
| if isinstance(obj, np.floating): |
| return float(obj) |
| if isinstance(obj, np.ndarray): |
| return obj.tolist() |
| return super(NpEncoder, self).default(obj) |
|
|
|
|
| class ScanSegmentation(): |
| def __init__(self): |
| """ |
| Write your own input validators here |
| Initialize your model etc. |
| """ |
|
|
| |
| |
|
|
| pass |
|
|
| @staticmethod |
| def load_input(input_dir): |
| """ |
| Read from /input/ |
| Check https://grand-challenge.org/algorithms/interfaces/ |
| """ |
|
|
| |
| inputs = glob.glob(f'{input_dir}/*.obj') |
| print("scan to process:", inputs) |
| return inputs |
|
|
| @staticmethod |
| def write_output(labels, instances, jaw): |
| """ |
| Write to /output/dental-labels.json your predicted labels and instances |
| Check https://grand-challenge.org/components/interfaces/outputs/ |
| """ |
| pred_output = {'id_patient': "", |
| 'jaw': jaw, |
| 'labels': labels, |
| 'instances': instances |
| } |
|
|
| |
| with open('./test/test_local/expected_output.json', 'w') as fp: |
| |
| json.dump(pred_output, fp, cls=NpEncoder) |
|
|
| return |
|
|
| @staticmethod |
| def get_jaw(scan_path): |
| try: |
| |
| _, jaw = os.path.basename(scan_path).split('.')[0].split('_') |
| except: |
| |
| try: |
| with open(scan_path, 'r') as f: |
| jaw = f.readline()[2:-1] |
| if jaw not in ["upper", "lower"]: |
| return None |
| except Exception as e: |
| print(str(e)) |
| print(traceback.format_exc()) |
| return None |
|
|
| return jaw |
|
|
| def predict(self, inputs): |
| """ |
| Your algorithm goes here |
| """ |
|
|
| try: |
| assert len(inputs) == 1, f"Expected only one path in inputs, got {len(inputs)}" |
| except AssertionError as e: |
| raise Exception(e.args) |
| scan_path = inputs[0] |
| print(f"loading scan : {scan_path}") |
| |
| try: |
| |
| mesh = trimesh.load(scan_path, process=False) |
| jaw = self.get_jaw(scan_path) |
| print("jaw processed is:", jaw) |
| except Exception as e: |
| print(str(e)) |
| print(traceback.format_exc()) |
| raise |
| |
| |
| |
| |
|
|
| |
| nb_vertices = mesh.vertices.shape[0] |
|
|
| |
| instances = [2] * nb_vertices |
| labels = [43] * nb_vertices |
|
|
| try: |
| assert (len(labels) == len(instances) and len(labels) == mesh.vertices.shape[0]),\ |
| "length of output labels and output instances should be equal" |
| except AssertionError as e: |
| raise Exception(e.args) |
|
|
| return labels, instances, jaw |
|
|
| def process(self): |
| """ |
| Read input from /input, process with your algorithm and write to /output |
| assumption /input contains only 1 file |
| """ |
| input = self.load_input(input_dir='/input') |
| labels, instances, jaw = self.predict(input) |
| self.write_output(labels=labels, instances=instances, jaw=jaw) |
|
|
|
|
| if __name__ == "__main__": |
| ScanSegmentation().process() |
|
|