def loclabel_gen(ano_path, loc_path, out_path): pids = list(map(lambda x: x.strip('.json'), os.listdir(ano_path))) annotations = {} for pid in pids: pid_json_path = os.path.join(ano_path, pid + '.json') anno = Annotation() anno.from_json(pid_json_path) annotations[pid] = anno coords = [] infile = open(loc_path) for i, line in enumerate(infile): pid, x_center, y_center = line.strip('\n').split(',') coords.append((pid, x_center, y_center)) infile.close() num_sample = len(coords) print(f"Total sample: {num_sample}") outfile = open(out_path, 'w') for index in range(num_sample): pid, x_center, y_center = coords[index] x_center = int(x_center) y_center = int(y_center) x_top_left = int(x_center - IMG_SIZE / 2) y_top_left = int(y_center - IMG_SIZE / 2) label = [] for x_idx in range(3): for y_idx in range(3): # (x, y) is the center of each patch x = x_top_left + int((x_idx + 0.5) * SUB_SIZE) y = y_top_left + int((y_idx + 0.5) * SUB_SIZE) # get label information according to annotation if annotations[pid].inside_polygons((x, y), True): label.append(1) else: label.append(0) # write output outfile.write( f"{pid.lower()}, {x_center}, {y_center}, {str(label)[1:-1]}\n") if index % 100 == 0: print(index) outfile.close()
def object_hook(d): """ Usage ----- >>> import simplejson as json >>> with open('file.json', 'r') as f: ... json.load(f, object_hook=object_hook) """ from segment import Segment from timeline import Timeline from annotation import Annotation from transcription import Transcription if PYANNOTE_JSON_SEGMENT in d: return Segment.from_json(d) if PYANNOTE_JSON_TIMELINE in d: return Timeline.from_json(d) if PYANNOTE_JSON_ANNOTATION in d: return Annotation.from_json(d) if PYANNOTE_JSON_TRANSCRIPTION in d: return Transcription.from_json(d) return d