def load_parser(obj: dict, count: int, ret: List[dict]) -> Optional[bool]: progress_load_filter(obj, count, ret) load_image(obj, False) st = '%03d' % ((count - 1) // 1000) stored[obj[ID]] = st store_png(OUTPUT_DIR, ['images', st], str(obj[ID]), obj[FRAME_CONTENT]) id_array.append(obj[ID]) bitmap_array.append([obj[GRAY]]) return False
def measure_angle(fn: str): hits, count, errors = load_json('../../data/%s' % fn, progress_and_process_image) for h in hits: nkg_mark_hit_area(h) nkg_make_track(h, scale=1, downscale=False, skeleton_method='zhang') angle = 0 if h.get(NKG_PATH): angles = analyse_path(h.get(NKG_PATH), cut_first=1, cut_latest=1) if len(angles): angle = angles[0] if math.isnan(angle): angle = 0 store_png('/tmp/credo', [fn], '%03d_%s' % (abs(angle), str(h.get(ID))), h.get(IMAGE))
def store_pngs(arr, subdir): for a in arr: store_png( OUTPUT_DIR, [subdir, 'by_solidity'], '%.3f_%.3f_%d' % (a[ASTROPY_SOLIDITY][0], a[ASTROPY_ELLIPTICITY][0], a[ID]), a[IMAGE]) store_png( OUTPUT_DIR, [subdir, 'by_ellipticity'], '%.3f_%.3f_%d' % (a[ASTROPY_ELLIPTICITY][0], a[ASTROPY_SOLIDITY][0], a[ID]), a[IMAGE]) s = int(a[ASTROPY_SOLIDITY][0] * 5) * 2 e = int(a[ASTROPY_ELLIPTICITY][0] * 5) * 2 store_png(OUTPUT_DIR, [ subdir, 'by_matrix_solidity_per_ellipticity', '%02d-%02d_%02d-%02d' % (s, s + 2, e, e + 2) ], '%d' % a[ID], a[IMAGE])
def start_analyze(all_detections, log_prefix): print('%s group by devices...' % log_prefix) by_devices = group_by_device_id(all_detections) print('%s ... done' % log_prefix) dev_no = 0 dev_count = len(by_devices.keys()) for device_id, device_detections in by_devices.items(): by_resolution = group_by_resolution(device_detections) for resolution, detections in by_resolution.items(): dev_no += 1 print( '%s start device %d of %d, device id: %s, resolution: %dx%d, detections count: %d' % (log_prefix, dev_no, dev_count, str(device_id), resolution[0], resolution[1], len(detections))) # try to merge hits on the same frame by_frame = group_by_timestamp_division(detections) reconstructed = 0 for timestmp, in_frame in by_frame.items(): if len(in_frame) <= 1: continue image = Image.new('RGBA', (resolution[0], resolution[1]), (0, 0, 0)) for d in reversed(in_frame): cx = d.get(X) - 30 cy = d.get(Y) - 30 w, h = (60, 60) if DEBUG: store_png(DEBUG_DIR, [ 'reconstruct', str(device_id), str(timestmp), 'before' ], str(d.get(ID)), d.get(FRAME_CONTENT)) image.paste(d.get(IMAGE), (cx, cy, cx + w, cy + h)) # fix bug in early CREDO Detector App: black filled boundary 1px too large image.paste(image.crop((cx + w - 1, cy, cx + w, cy + h)), (cx + w, cy, cx + w + 1, cy + h)) image.paste(image.crop((cx, cy + h - 1, cx + w, cy + h)), (cx, cy + h, cx + w, cy + h + 1)) image.paste( image.crop((cx + w - 1, cy + h - 1, cx + w, cy + h)), (cx + w, cy + h, cx + w + 1, cy + h + 1)) for d in in_frame: cx = d.get(X) - 30 cy = d.get(Y) - 30 w, h = (60, 60) hit_img = image.crop((cx, cy, cx + w, cy + h)) with BytesIO() as output: hit_img.save(output, format="png") d[FRAME_CONTENT] = encode_base64(output.getvalue()) if DEBUG: store_png(DEBUG_DIR, [ 'reconstruct', str(device_id), str(timestmp), 'after' ], str(d.get(ID)), d.get(FRAME_CONTENT)) reconstructed += 1 if DEBUG: store_png(DEBUG_DIR, ['reconstruct', str(device_id)], str(timestmp), image) print('%s ... reconstructed frames: %d' % (log_prefix, reconstructed))
def store_png_for_debug(detections: List[dict], subdirs: List[str]): if DEBUG: for d in detections: store_png(DEBUG_DIR, subdirs, str(d.get(ID)), d.get(FRAME_CONTENT)) store_png(DEBUG_DIR, [*subdirs, d.get(DEVICE_ID)], str(d.get(ID)), d.get(FRAME_CONTENT))
def save_html_and_pngs_and_labels(stage: int, kmeans: KMeans, data: dict): sn = get_labels_file(stage) if os.path.exists(sn): return # Save PNG files in file system labels = {} images = {} for i in range(0, len(kmeans.labels_)): label = kmeans.labels_[i] _id = data['id_array'][i] image = data['bitmap_array'][i][0] if STORE_GRAY_CLUSTER: store_png(OUTPUT_DIR, ['%02d' % stage, '%03d' % label], str(_id), image) in_label = labels.get(label, []) in_label.append(_id) labels[label] = in_label in_image = images.get(label, []) in_image.append([image]) images[label] = in_image # make normalized sum and corona sum corona_radius = [2, 4, 8, 16] for label, in_image in images.items(): stacked = np.vstack(in_image) summed = np.sum(stacked, 0) m = np.amax(summed) image = summed * 255 / m store_png(OUTPUT_DIR, ['%02d' % stage], 'sum_%03d' % label, image.astype(np.uint8)) # corona: for radius in corona_radius: rr, cc = draw.disk((30, 30), radius=radius, shape=summed.shape) summed[rr, cc] = 0 m = np.amax(summed) image = summed * 255 / m store_png(OUTPUT_DIR, ['%02d' % stage], 'sum_corona_%03d_%d' % (label, radius), image.astype(np.uint8)) # Save HTML for preview clusters max_files_per_cluster = 66 with open('%s/%02d.html' % (OUTPUT_DIR, stage), 'wt') as html: html.write(html_head) for label in sorted(labels.keys()): html.write(' <tr><th>%d</th><th>%d</th>\n' % (label, len(labels[label]))) html.write('<td><img src="%02d/sum_%03d.png"/></td><td>' % (stage, label)) for radius in corona_radius: html.write('<img src="%02d/sum_corona_%03d_%d.png"/>' % (stage, label, radius)) html.write('</td><td>') used = 0 for _id in labels[label]: html.write(' <img src="images/%s/%s.png"/>\n' % (data['stored'][_id], str(_id))) used += 1 if used >= max_files_per_cluster: break html.write(' </td></tr>\n') html.write(html_foot) serialize(sn, labels)