def predict(self): if self.model is None: self.build_model() if not os.path.isdir(ROOT_PATH): os.makedirs(ROOT_PATH) path = TEST_PATH files = list_files(path) pix = [] for f in files: pix_file = os.path.join(path, f) pix_data = read_gray(pix_file) pix.append(pix_data) # print(pix_file) pix = np.array(pix) print("Test image max: ", np.amax(pix)) pix = pix / 255.0 print("Normalized image max: ", np.amax(pix)) ipix = np.expand_dims(pix, axis=3) for i in range(ipix.shape[0]): img = ipix[i] img = np.expand_dims(img, axis=0) out_pix = self.model.predict([img, img, img, img]) out_pix[out_pix >= self.thresh] = 1.0 out_pix[out_pix < self.thresh] = 0.0 out_pix = np.squeeze(out_pix) * 255.0 out_pix = out_pix.astype(np.uint8) path = os.path.join(ROOT_PATH, files[i]) print("Saving ... ", path) imsave(path, out_pix, cmap='gray')
def get_in_pix(filename="in_pix.npy", ispix=True, isskel=False, istest=False): path = PX_PATH if istest: path = PT_PATH if isskel: path = SK_PATH if not ispix: path = path.replace("pixel", "point") files = list_files(path) pix = [] pmax = 0 pmin = 255 maxpts = 0 for f in files: pix_file = os.path.join(path, f) print(pix_file) if ispix: pix_data = read_gray(pix_file) else: image = np.zeros((256, 256), dtype=np.uint8) pix_data = read_points(pix_file) if len(pix_data) > maxpts: maxpts = len(pix_data) for p in pix_data: if p[0] > pmax: pmax = p[0] if p[0] < pmin: pmin = p[0] if p[1] > pmax: pmax = p[1] if p[1] < pmin: pmin = p[1] x = min(round(p[0]), 255) y = min(round(p[1]), 255) image[x][y] = 255 impath = os.path.join("tmp", f + ".png") print("Saving ... ", impath) imsave(impath, image, cmap='gray') pix_data = image pix.append(pix_data) # Max pts: 12270 print("Max pts: ", maxpts) pix = np.array(pix) print("Shape: ", pix.shape) print("PMin: ", pmin) print("PMax: ", pmax) if not istest: pix = np.expand_dims(pix, axis=3) print("Final shape: ", pix.shape) print("Min: ", np.amin(pix)) print("Max: ", np.amax(pix)) if not istest: print("Saving to ", filename) np.save(filename, pix) return pix
def img2pt(): path = ROOT_PATH if not os.path.isdir(PRED_PATH): os.makedirs(PRED_PATH) files = list_files(path) pix = [] for f in files: pix_file = os.path.join(path, f) print(pix_file) filename = pix_file filename = filename.replace(".png", "") filename = filename.replace("full", "skel") filename = filename.replace("root", "pred") print(filename) pix_data = read_gray(pix_file) with open(filename, "w+") as fh: for x in range(pix_data.shape[0]): for y in range(pix_data.shape[0]): if pix_data[x][y]>0: fh.write("%d %d\n" % (x, y))
def get_out_pix(filename="out_pix.npy"): files = list_files(SK_PATH) pix = [] for f in files: pix_file = os.path.join(SK_PATH, f) pix_data = read_gray(pix_file) print(pix_file) pix.append(pix_data) pix = np.array(pix) pix = np.mean(pix, axis=3) pix = pix.astype(np.uint8) print("Shape: ", pix.shape) print("Uniques: ", np.unique(pix)) pix = np.expand_dims(pix, axis=3) print("Final shape: ", pix.shape) print("Min: ", np.amin(pix)) print("Max: ", np.amax(pix)) print("Saving to ", filename) np.save(filename, pix) return pix
def img2pt(): shutil.rmtree(PRED_PATH, ignore_errors=True) path = ROOT_PATH if not os.path.isdir(PRED_PATH): os.makedirs(PRED_PATH) files = list_files(path) pix = [] for f in files: pix_file = os.path.join(path, f) print(pix_file) filename = pix_file filename = filename.replace(".png", "") filename = filename.replace("full", "skel") #filename = filename.replace(os.path.basename(os.path.normpath(ROOT_PATH)), "pred") filename = os.path.join(PRED_PATH, os.path.basename(os.path.normpath(filename))) print(filename) pix_data = read_gray(pix_file) with open(filename, "w+") as fh: for x in range(pix_data.shape[0]): for y in range(pix_data.shape[0]): if pix_data[x][y] > 0: fh.write("%d %d\n" % (x, y))