def make_npy(self): base = self.knn_num samples, labels = [], [] paths = hp.get_paths(base) for path in paths: img = hp.get_image(base, path) sample = self.__buddy_hog(img) label = hp.get_name(path) labels.append(label) samples.append(sample) samples = np.float32(samples) labels = np.array(labels) np.save(self.samples_num_file, samples) np.save(self.labels_num_file, labels) base = self.knn_sym samples, labels = [], [] paths = hp.get_paths(base) for path in paths: img = hp.get_image(base, path) sample = self.__buddy_hog(img) label = hp.get_name(path) - 10 labels.append(label) samples.append(sample) samples = np.float32(samples) labels = np.array(labels) np.save(self.samples_sym_file, samples) np.save(self.labels_sym_file, labels)
def make_npy(self): base = self.svm_num samples, labels = [], [] paths = hp.get_paths(base) for path in paths: img = hp.get_image(base, path) sample = self.__buddy_hog(img) label = hp.get_name(path) labels.append(label) samples.append(sample) samples = np.float32(samples) labels = np.array(labels) np.save(self.samples_num_file, samples) np.save(self.labels_num_file, labels) base = self.svm_sym samples, labels = [], [] paths = hp.get_paths(base) for path in paths: img = hp.get_image(base, path) sample = self.__buddy_hog(img) label = hp.get_name(path) - 10 labels.append(label) samples.append(sample) samples = np.float32(samples) labels = np.array(labels) np.save(self.samples_sym_file, samples) np.save(self.labels_sym_file, labels)
def __make_svm_from_images(self, dir_train, mode): samples, labels = [], [] paths = hp.get_paths(dir_train) modifier = 0 if mode == "num": modifier = 0 elif mode == "sym": modifier = 10 for path in paths: img = hp.get_image(dir_train, path) sample = self.__buddy_hog(img) label = hp.get_name(path) - modifier labels.append(label) samples.append(sample) samples = np.array(samples) labels = np.array(labels) rand = np.random.RandomState(321) shuffle = rand.permutation(len(samples)) samples, labels = samples[shuffle], labels[shuffle] samples = np.float32(samples) model = SVM(C=2.67, gamma=5.383) model.train(samples, labels) return model
def make_hog_num_file(): base = hp.small_test_num_images_15x20 samples, labels = [], [] paths = hp.get_paths(base) for path in paths: img = hp.get_image(base, path) sample = buddy_hog(img) label = hp.get_name(path) labels.append(label) samples.append(sample) samples = np.float32(samples) labels = np.array(labels) labels = list(labels) # head document = "" head1 = str(len(samples)) + " " head2 = str(len(samples[0])) + " " head3 = str("10") + "\n" head = head1 + head2 + head3 document += head for sample, label in zip(samples, labels): input = ' '.join(map(str, list(sample))) output = make_output_text_vector(int(label), 10) document += input + "\n" + output print document with open(os.path.join("ann/test/", "test-hog-nums.dat"), 'wb') as temp_file: temp_file.write(document)
def __make_knn_from_images(self, mode): base = "" modifier = 0 samples, labels = [], [] if mode == "num": base = self.knn_num modifier = 0 elif mode == "sym": base = self.knn_sym modifier = 10 paths = hp.get_paths(base) for path in paths: img = hp.get_image(base, path) sample = self.__buddy_hog(img) label = hp.get_name(path) - modifier labels.append(label) samples.append(sample) samples = np.float32(samples) labels = np.array(labels) model = KNearest(k=5) model.train(samples, labels) return model
def run(self): while True: if not self.__queue.empty(): print 'process dir' dir_in = self.__queue.get() paths = hp.get_paths(dir_in) for num, path in enumerate(paths, start=1): meta = {'id': random.randint(1000, 1000000000), 'plate': hp.get_image(dir_in, path)} self.plate_captured.emit(meta) # print path self.__queue.task_done()
def global_testing(input_dir, set_name, mode, ann, knn, msp, svm): print "Start recognition:" print "run test", (set_name, mode) abc = {3: 0, 2: 0, 1: 0} problem = 0 paths = hp.get_paths(input_dir) total, err = len(paths), len(paths) for path in paths: # recognition img = hp.get_image(input_dir, path) at, ann_i = ann.rec(img, mode) kt, knn_i = knn.rec(img, mode) mt, msp_i = msp.rec(img, mode) st, svm_i = svm.rec(img, mode) # test tlit, ti = hp.get_test(path, mode) up_count_dict(tlit) up_dict(ann_dict, at, tlit) up_dict(knn_dict, kt, tlit) up_dict(msp_dict, mt, tlit) up_dict(svm_dict, st, tlit) rec = None xyz = {at, kt, st} if len(xyz) < 3: t = [at, kt, st] c = Counter(t) rec = max(c, key=c.get) if tlit == rec: up_dict(common_dict, rec, tlit) err -= 1 else: rec = None if rec is None: abc[len(xyz)] += 1 if len(xyz) == 1: cv2.imwrite("try/awesome/" + str([at, kt, st]) + "__" + str(tlit) + "__" + path, img) hp.print_result(total, err, set_name, mode) print "awesome:", abc[1] print "great:", abc[2] print "f**k this shit: ", abc[3] print " "
def make_prediction(file_path): print(' * Starting prediction.....') ref_image = helper.get_image_from_filename(file_path) # find the category results = [] cat = 0 for cur_path in PATH_LIST: filelist = os.listdir(os.path.join(FACE_DTA_PATH, cur_path)) idx = np.random.randint(len(filelist)) cur_image = helper.get_image(FACE_DTA_PATH, cat, idx) dist = model.predict([ref_image, cur_image])[0][0] results.append(dist) cat += 1 idx = np.argmin(results) return CAT_LIST[idx]
def testing_svm_from_image_base(input_dir, set_name, mode, svm): success, error = "", "" counter, err = 1, 0 paths = hp.get_paths(input_dir) for path in paths: # recognition img = hp.get_image(input_dir, path) lit, i = svm.rec(img, mode) # test tlit, ti = hp.get_test(path, mode) if tlit != lit: err += 1 error += "{0}\n".format(path) hp.write_image("try/error", path, img) else: success += "{0}\n".format(path) counter += 1 hp.print_result(counter, err, set_name, mode)
def testing_from_image_base(input_dir, set_name, mode, ann): #init block success, error = "", "" counter, err = 1, 0 paths = hp.get_paths(input_dir) for path in paths: # recognition img = hp.get_image(input_dir, path) lit, i = ann.rec(img, mode) # test tlit, ti = hp.get_test(path, mode) if tlit != lit: err += 1 error += "{0}\n".format(path) hp.write_image("try/error", path, img) else: success += "{0}\n".format(path) counter += 1 hp.print_result(counter, err, set_name, mode)
model = load_model(os.path.join(MODEL_PATH, MODEL_NAME), custom_objects={'contrastive_loss': SN.contrastive_loss}) ## test with many randomly selected images matching = 0 for tiral in range(NUM_TRIALS): print("Now trial #%d of %d" % (tiral, NUM_TRIALS)) pathlist = os.listdir(DATA_PATH) category = np.random.randint(len(pathlist)) cur_path = os.path.join(DATA_PATH, pathlist[category]) filelist = os.listdir(cur_path) index = np.random.randint(len(filelist)) ref_image = helper.get_image(DATA_PATH, category, index) results = [] for cat in range(len(pathlist)): filelist = os.listdir(os.path.join(DATA_PATH, pathlist[cat])) idx = np.random.randint(len(filelist)) cur_image = helper.get_image(DATA_PATH, cat, idx) dist = model.predict([ref_image, cur_image])[0][0] results.append(dist) if category == np.argmin(results): matching += 1 print("Accuracy: %5.2f %%\n" % (100.0 * matching / NUM_TRIALS))
def image(): name = request.args.get("name") return helper.get_image(name)
import numpy as np from skimage import color, data, restoration import numpy as np import numpy.random as npr from scipy.signal import convolve2d import helper camera = color.rgb2gray(data.camera()) camera = helper.get_image('cameraman') from scipy.signal import convolve2d psf = np.ones((5, 5)) / 25 camera = convolve2d(camera, psf, 'same') camera += 0.1 * camera.std() * np.random.standard_normal(camera.shape) deconvolved = restoration.richardson_lucy(camera, psf, 5) helper.show_images({'deconv':deconvolved})
import numpy as np from skimage import color, data, restoration import numpy as np import numpy.random as npr from scipy.signal import convolve2d import helper camera = color.rgb2gray(data.camera()) camera = helper.get_image('cameraman') from scipy.signal import convolve2d psf = np.ones((5, 5)) / 25 camera = convolve2d(camera, psf, 'same') camera += 0.1 * camera.std() * np.random.standard_normal(camera.shape) deconvolved = restoration.richardson_lucy(camera, psf, 5) helper.show_images({'deconv': deconvolved})
psf_mirror = psf[::-1, ::-1] for _ in range(iterations): RB = C / (G * F + eps) # relative_blur = image / fftconvolve(im_deconv, psf, 'same') im_deconv *= ifft2(RB * G_mirror) F = fft2(im_deconv) # im_deconv *= fftconvolve(relative_blur, psf_mirror, 'same') if clip: im_deconv[im_deconv > 1] = 1 im_deconv[im_deconv < -1] = -1 return fft2(im_deconv), im_deconv f = helper.get_image("cameraman64") N, _ = f.shape f /= f.sum() F = fft2(f) g = helper.gaussian(sigma=1, N=N) g /= g.sum() G = fft2(g) C = F * G c = ifft2(C) max_its = 80 for k in range(int(max_its)): F, f_k = update_f(F, G, C, eps=0, iterations=1) # G, g_k = update_g(F, G, C, eps=0, iterations=2)
#im_deconv = 0.5*np.ones_like(f) psf_mirror = psf[::-1, ::-1] for _ in range(iterations): RB = C / (G*F + eps) #relative_blur = image / fftconvolve(im_deconv, psf, 'same') im_deconv *= ifft2(RB * G_mirror) F = fft2(im_deconv) #im_deconv *= fftconvolve(relative_blur, psf_mirror, 'same') if clip: im_deconv[im_deconv > 1] = 1 im_deconv[im_deconv < -1] = -1 return fft2(im_deconv), im_deconv f = helper.get_image('cameraman64') N, _ = f.shape f /= f.sum() F = fft2(f) g = helper.gaussian(sigma=1, N=N) g /= g.sum() G = fft2(g) C = F*G c = ifft2(C) max_its = 80 for k in range(int(max_its)): F, f_k = update_f(F, G, C, eps=0, iterations=1) #G, g_k = update_g(F, G, C, eps=0, iterations=2)