def main(): if len(sys.argv) < 2: raise Exception( "Inputs:\n\tparameter 1: Test image to use for processing.") input_image = sys.argv[1] print("Image: %s" % input_image) image = cv2.imread(input_image) img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) img_bw = cv2.imread(input_image, cv2.IMREAD_GRAYSCALE) x_coords, y_coords = util.get_cell_boundaries(img_bw) row = 0 for y_coord in y_coords: column = 0 for x_coord in x_coords: raw_image = img_rgb[y_coord[0]:y_coord[1], x_coord[0]:x_coord[1]] image_height, image_width = img_bw.shape image_sum = raw_image.sum() image_density = image_sum / (image_width * image_height * 255) if image_density < image_density_threshold or image_density >= 1 - image_density_threshold: number = 0 else: custom_oem_psm_config = r'--oem 3 --psm 10' number = pytesseract.image_to_string( image, config=custom_oem_psm_config) util.show_image(raw_image, title="Row %s, col %s: %s" % (row, column, number)) print("Row %s\tColumn %s\t%s" % (row, column, number)) column += 1 row += 1
def reconstruct(): while True: x = raw_input('emoji name > ') if len(x) == 0: break orig, recon = n.reconstruct(x + '.png') show_image(orig) show_image(recon)
def tracking(args, vid, detector, writer, msg, file_path, lock): if args.display: cv2.startWindowThread() for frame in vid: if cv2.waitKey(5) & 0xFF == ord('q'): vid.stop() break for box, score in detector.detect(frame, display_bg=args.display): label = f"person" if args.show_score: label += f" - {score:{.4}}" # Draw bounding rect human frame = draw_prediction(frame, label, box) with lock: msg.value = f"Have human!!! Alert!" print(msg.value) if args.output_folder and msg.value: latest_file = writer.put(frame) with lock: file_path.value = latest_file if args.display: show_image('Capture', frame, wait=False) vid.stop() writer.release() cv2.destroyAllWindows()
def detect(self, frame, display_bg=False): """ Detect and respond box, score ones :param frame: :return: """ boxes, scores = [], [] fgMask = self.background_sub.apply(frame) # find motion objects and remove small objects contours, _ = cv2.findContours(fgMask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) blobs = [ c for c in contours if cv2.contourArea(c) > self.min_object_size ] # Detect human if have motion object if blobs: boxes, scores = self.detector.detect_person(frame) if display_bg: show_image('Background', fgMask, wait=False) for box, score in zip(boxes, scores): yield box, score
def login_qq(self): """ 使用帐号密码进行登录 """ qq = JDQQ(config.qq['account'], config.qq['password'], self.session) while True: try: qq.login() break except NeedVerifyCode as e: verifier = e.verifier util.show_image(verifier.fetch_image()) verify_code = input('请输入验证码: ') try: verifier.verify(verify_code) except NeedVerifyCode: # 需刷新验证码, 使用新的 verifier; 或 qq.login(force=True) qq.verifier = None print('验证码错误.') except LogInError as e: raise LogInError('登录 QQ 失败: {}'.format(e)) return qq.g_tk()
def evaluate(self, inp, outp): noise = np.random.uniform(size=[len(inp), self.noise_size]) use_real_image = np.random.randint(0, 2, len(inp)) loss = self.session.run(self.disc_loss, feed_dict={ self.real_image_input: inp, self.use_real_image: use_real_image, self.disc_dropout_keep_prob: 1, self.noise_input: noise }) self.last_loss = loss img = self.generate_images(count=1)[0] show_image(img, path="/Users/nateparrott/Desktop/gan.png") return loss
def generate_random(self): mean = 0.838776 stddev = math.sqrt(1.4425) # a = np.random.normal(mean, stddev, bottleneck_size) a = np.zeros(bottleneck_size) a.fill(mean) a[random.randint(0, bottleneck_size - 1)] = mean * 2 blank_img = np.zeros((1, 64, 64, 3)) img = self.session.run(self.output, feed_dict={ self.input: blank_img, self.artificial_hidden_logits: a, self.use_artificial_hidden_logits: 1 })[0] show_image(img)
def main(): args = ArgumentParser() args.add_argument('-c', '--camera_url', default=0, type=str, help='0 - local camera') args.add_argument('-dt', '--detect_threshold', default=0.975, type=float, help="Threshold of face detection") args.add_argument('-rf', '--recognized_threshold', default=0.8, type=float, help="Threshold of face recognition") args.add_argument('--device', default='cuda:0', type=str, help="Device run model. `cuda:<id>` or `cpu`") args.add_argument('--detect_face_model', default='data/pretrained/mobilenet_header.pth', type=str, help="Face detector model path") args.add_argument('--detect_face_backbone', default='data/pretrained/mobile_backbone.tar', type=str, help="Face detector backbone path") args.add_argument('--recognized_model', default='data/pretrained/embedder_resnet50_asia.pth' , type=str, help="Face embedding model path") args.add_argument('--model_registered', default='model_faces.npy', type=str, help="Model contain face's vectors") args.add_argument('--model_ids', default='model_face_ids.npy', type=str, help="Model contain face's ids") args = args.parse_args() try: args.camera_url = int(args.camera_url) except: pass if not (os.path.isfile(args.model_registered) and os.path.isfile(args.model_ids)): face_model = numpy.zeros((0, 512), dtype=numpy.float32) ids_model = [] else: face_model = numpy.load(args.model_registered, allow_pickle=True) ids_model = numpy.load(args.model_ids, allow_pickle=True).tolist() detector = FaceDetection(args.detect_face_model, args.detect_face_backbone, scale_size=480, device=args.device) embedder = FaceEmbedding(args.recognized_model, device=args.device) # recognize video = VideoCapture(args.camera_url) for frame in video: faces = detector(frame) faces = embedder(faces) for face in faces: txt = "None" color = RED scores = cosine_similarity(face.embedding.reshape(1, 512), face_model, skip_normalize=True).ravel() args_idx = numpy.argmax(scores) if scores[args_idx] >= args.recognized_threshold: txt = ids_model[args_idx] color = GREEN frame = draw_square(frame, face.box.astype(numpy.int), color=color) frame = cv2.putText(frame, f"EID: {txt}", (int(face.box[0]), int(face.box[1] - 20)), cv2.FONT_HERSHEY_PLAIN, 1, GREEN) if not show_image(frame, 'Face Recognition', windows_size=(1920, 1080)): break video.stop()
def main(): img = cv2.imread(sys.argv[1]) if sys.argv[-1] == "-d": print "The license plate is : ", sys.argv[1].split("_")[0], "\n Predicting the characters of plate :" img = util.conv2gray(img) img_thresh = util.threshold(img) if sys.argv[-1] == "-d": util.show_image(img_thresh) height, width = util.get_2dimensions(img_thresh) # if sys.argv[2] == "0": # img_resize = img_resize[util.BORDER : height-util.BORDER, # util.NL_LOGO : width - util.BORDER] string = segment_characters(img_thresh) string = [each.split(".")[0] for each in string] if sys.argv[-1] == "-d": print "\nIdentified characters of license plate \n", string lpr_value = character_integer(string) if sys.argv[-1] == "-d": print "Concatenated integer" print lpr_value
def main(): img = cv2.imread(sys.argv[1]) if sys.argv[-1] == "-d": print "The license plate is : ", sys.argv[1].split( "_")[0], "\n Predicting the characters of plate :" img = util.conv2gray(img) img_thresh = util.threshold(img) if sys.argv[-1] == "-d": util.show_image(img_thresh) height, width = util.get_2dimensions(img_thresh) # if sys.argv[2] == "0": # img_resize = img_resize[util.BORDER : height-util.BORDER, # util.NL_LOGO : width - util.BORDER] string = segment_characters(img_thresh) string = [each.split(".")[0] for each in string] if sys.argv[-1] == "-d": print "\nIdentified characters of license plate \n", string lpr_value = character_integer(string) if sys.argv[-1] == "-d": print "Concatenated integer" print lpr_value
def main(): if len(sys.argv) < 3: raise Exception("Input parameter 1: folder of input images.Input parameter 2: folder for output") input_folder = sys.argv[1] output_folder = sys.argv[2] for image_file in listdir(input_folder): print("Processing: %s..." % image_file) image_file_name, image_file_extension = os.path.splitext(image_file) if image_file_extension == '' or image_file_name[0] == '.': print("Skipping, not an image file.") else: image = cv2.imread("%s/%s" % (input_folder, image_file), cv2.IMREAD_GRAYSCALE) x_coords, y_coords = util.get_cell_boundaries(image) bw_threshold = 160 row = 1 for y_coord in y_coords: column = 1 for x_coord in x_coords: cell_image = image[y_coord[0]:y_coord[1], x_coord[0]:x_coord[1]] # util.show_image(cell_image) # Make the image monochrome. If the original pixel value > threshold, 1, otherwise 0. # We use 1 and 0 since this will make it easy to sum the pixels in each direction (thresh, monochrome_image) = cv2.threshold(cell_image, bw_threshold, 1, cv2.THRESH_BINARY_INV) util.show_image(monochrome_image) # Save the image of this cell new_file_name = "%s/%s/%s%s%s%s" % (output_folder, column, image_file_name, row, column, image_file_extension) write_succeeded = cv2.imwrite(new_file_name, monochrome_image) column += 1 row += 1 print("Finished: %s" % image_file)
def detect_orange_btn(image1, image2, org_pos_x, org_pos_y, org_size, show_type=0): log.print_debug(TAG, "-----------detect_orange_btn-----------") global dynamic_org_size global last_detected_org_size global current_image global filter_image #print org_pos_x, org_pos_y, org_size log.print_debug( TAG, "passed in Posx " + str(org_pos_x) + " Poxy " + str(org_pos_y) + " size " + str(org_size)) if last_detected_org_size != 0: dynamic_org_size = org_size - last_detected_org_size last_detected_org_size = org_size res1, res2 = color_filter.filter_orange(image1, image2) # tmp1 = cv2.cvtColor(res1, cv2.COLOR_HSV2BGR) tmp2 = cv2.cvtColor(res2, cv2.COLOR_HSV2BGR) # black1 = cv2.cvtColor(tmp1, cv2.COLOR_BGR2GRAY) black2 = cv2.cvtColor(tmp2, cv2.COLOR_BGR2GRAY) # ret1, thresh1 = cv2.threshold(black1, 0, 255, cv2.THRESH_BINARY) ret2, thresh2 = cv2.threshold(black2, 0, 255, cv2.THRESH_BINARY) # cnts1, hierarchy = cv2.findContours(thresh1.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) current_image = image2 filter_image = black2 tmp = cv2.findContours(thresh2.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) if len(tmp) == 3: cnts2 = tmp[1] hierarchy = tmp[2] else: cnts2 = tmp[0] hierarchy = tmp[1] # cnts1 and cnts2 contain the contour result # org_btn_candidate_1 = [] org_btn_candidate_2 = [] # util.debug_print(TAG,"detect_orange_btn img1") # for cnt in cnts1: # area0 = cv2.contourArea(cnt) # cnt_len = cv2.arcLength(cnt, True) # cnt = cv2.approxPolyDP(cnt, 0.02 * cnt_len, True) # if estimate_orange_area(area0, org_size): # x1, y1, w1, h1 = cv2.boundingRect(cnt) # util.debug_print(TAG, "size1 " + str(area0) + " x " + str(x1) +" y "+ str(y1)) # org_btn_candidate_1.append(cnt) util.debug_print(TAG, "img2:") for cnt in cnts2: cnt_len = cv2.arcLength(cnt, True) # area0 = cv2.contourArea(cnt) cnt = cv2.approxPolyDP(cnt, 0.02 * cnt_len, False) area0 = cv2.contourArea(cnt) if area0 > 100: x1, y1, w1, h1 = cv2.boundingRect(cnt) #log.print_debug(TAG, "area0 > 100 " + str(area0) + " Posx " + str(x1) + " Posy " + str(y1)) if estimate_orange_area(area0, org_size): x1, y1, w1, h1 = cv2.boundingRect(cnt) log.print_debug( TAG, "candidate area size " + str(area0) + " Posx " + str(x1) + " Posy " + str(y1)) org_btn_candidate_2.append(cnt) if len(org_btn_candidate_2) == 0: # thinking if is because size variant too much log.print_debug(TAG, "not orange button found size variant too much ?") for cnt in cnts2: cnt_len = cv2.arcLength(cnt, True) # area0 = cv2.contourArea(cnt) cnt = cv2.approxPolyDP(cnt, 0.02 * cnt_len, False) area0 = cv2.contourArea(cnt) x1, y1, w1, h1 = cv2.boundingRect(cnt) if estimate_org_area_coor(area0, org_size, x1, y1, org_pos_x, org_pos_y): log.print_debug( TAG, "variant area size " + str(area0) + " Posx " + str(x1) + " Posy " + str(y1)) org_btn_candidate_2.append(cnt) # cnt1 = determine_orange_btn(org_btn_candidate_1, org_pos_x, org_pos_y) cnt2 = determine_orange_btn(org_btn_candidate_2, org_pos_x, org_pos_y) if show_type == 1: util.show_two_image(image2, res2) if show_type == 2: util.show_image("orange_btn_1", image2) util.show_image("orange_btn_2", res2) # if len(cnt1) == 0 or len(cnt2) == 0: if len(cnt2) == 0: return org_pos_x, org_pos_y, org_size, False x2, y2, w2, h2 = cv2.boundingRect(cnt2) area0 = cv2.contourArea(cnt2) log.print_debug( TAG, "detected orange button size " + str(area0) + " " + " x2 " + str(x2) + " y2 " + str(y2)) return x2, y2, area0, True
from pylsci import Lsci from util import stack_images, show_image, read_image lsci = Lsci() print('temporal LSCI ...') speckle_imgs = read_image('img/temporal.png') speckle_img_stack = stack_images(speckle_imgs) print(speckle_img_stack.shape) t_lsci = lsci.temporal_contrast(speckle_img_stack) show_image(t_lsci) print(t_lsci.shape) print('spatio-temporal LSCI ...') speckle_imgs = read_image('img/temporal.png') speckle_img_stack = stack_images(speckle_imgs) print(speckle_img_stack.shape) st_lsci = lsci.spatio_temporal_contrast(speckle_img_stack) show_image(st_lsci) print(st_lsci.shape) print('spatial LSCI ...') speckle_img = read_image('img/spatial.tif') print(speckle_img.shape) s_lsci = lsci.spatial_contrast(speckle_img) show_image(s_lsci) print(s_lsci.shape) print('success')
def main(): ### ========== TODO : START ========== ### # part 1: explore LFW data set X, y = util.get_lfw_data() #util.show_image(X[0]) #util.show_image(X[1]) #util.show_image(X[2]) scores = np.zeros((19, 19)) for i in range(19): for j in range(19): if i != j: X1, y1 = util.limit_pics(X, y, [i, j], 40) face_points = build_face_image_points(X1, y1) cluster_set = kMeans(face_points, 2, "cheat", plot=False) scores[i, j] = cluster_set.score() np.fill_diagonal(scores, np.iinfo(np.int16).max) similar_tuple = np.unravel_index(np.argmin(scores), scores.shape) print "it did worst with: ", similar_tuple np.fill_diagonal(scores, np.iinfo(np.int16).min) distinct_tuple = np.unravel_index(np.argmax(scores), scores.shape) print "it did best with: ", distinct_tuple X1, y1 = util.limit_pics(X, y, [similar_tuple[0]], 40) util.show_image(X1[0]) X1, y1 = util.limit_pics(X, y, [similar_tuple[1]], 40) util.show_image(X1[0]) X1, y1 = util.limit_pics(X, y, [distinct_tuple[0]], 40) util.show_image(X1[0]) X1, y1 = util.limit_pics(X, y, [distinct_tuple[1]], 40) util.show_image(X1[0]) #util.show_image(np.mean(X, axis=1)) U, mu = util.PCA(X) #util.plot_gallery([util.vec_to_image(U[:,i]) for i in xrange(12)]) # for i in [1,10,50, 100, 500, 1288]: # Z, ul = util.apply_PCA_from_Eig(X, U, i, mu) # new_X = util.reconstruct_from_PCA(Z, ul, mu) # util.plot_gallery([new_X[j] for j in xrange(12)]) ### ========== TODO : END ========== ### #======================================== # part 2 # part b: test Cluster implementation # centroid: [ 1.04022358 0.62914619] np.random.seed(1234) sim_points = generate_points_2d(20) cluster = Cluster(sim_points) print 'centroid:', cluster.centroid().attrs # parts c-d: test kMeans implementation using toy dataset np.random.seed(1234) sim_points = generate_points_2d(20) k = 3 # test cluster using random initialization #kmeans_clusters = kMeans(sim_points, k, init='random', plot=True) # test cluster using cheat initialization kmeans_clusters = kMeans(sim_points, k, init='cheat', plot=True) ### ========== TODO : START ========== ### # part 3 # part a: explore effect of lower-dimensional representations on clustering performance np.random.seed(1234) # part b: determine ``most discriminative'' and ``least discriminative'' pairs of images np.random.seed(1234) ### ========== TODO : END ========== ### #======================================== # part 4a # test Cluster implementation # medoid: [ 1.05674064 0.71183522] np.random.seed(1234) sim_points = generate_points_2d(20) cluster = Cluster(sim_points) print 'medoid:', cluster.medoid().attrs # test kMedoids implementation using toy dataset np.random.seed(1234) sim_points = generate_points_2d(20) k = 3 # test cluster using random initialization kmedoids_clusters = kMedoids(sim_points, k, init='random', plot=True) ### ========== TODO : START ========== ### # part 4 # part b: compare k-means and k-medoids np.random.seed(1234) # part c: explore effect of lower-dimensional representations on clustering performance np.random.seed(1234)
def generate(): while True: show_image(n.generate_images(count=1)[0]) raw_input('press enter for more')
def flash_detection(img1, img2, orange_x, orange_y, size, show_type=0): log.print_debug(TAG, "----------flash detection start----------") global org_pos_x global org_pos_y global confidence_counter global current_image global filter_image org_pos_x = orange_x org_pos_y = orange_y log.print_debug( TAG, "passed in org_pos_x " + str(org_pos_x) + " org_pos_y " + str(org_pos_y) + " size " + str(size)) hsv1, hsv2 = color_filter.filter_flash(img1, img2) return1 = cv2.findContours(hsv1.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) return2 = cv2.findContours(hsv2.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # cnts1 = cv2.findContours(hsv1.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # cnts2 = cv2.findContours(hsv2.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) current_image = img2 filter_image = hsv2 if len(return1) == 3: cnts1 = return1[1] hierarchy1 = return1[2] else: cnts1 = return1[0] hierarchy1 = return1[1] if len(return2) == 3: cnts2 = return2[1] hierarchy2 = return2[2] else: cnts2 = return2[0] hierarchy2 = return2[1] if show_type == 1: util.show_two_image(hsv1, hsv2) elif show_type == 2: util.show_image("w1", hsv1, 640, 320) util.show_image("w2", img2, 640, 320) # org_btn_candidate_1 = [] org_btn_candidate_2 = [] # print "for area of cnts 1" # for cnt in cnts1: # area0 = cv2.contourArea(cnt) # #if area0 > 10: # # print area0 # cnt_len = cv2.arcLength(cnt, True) # cnt = cv2.approxPolyDP(cnt, 0.02 * cnt_len, False) # #if area0 > 100: # #print "area " + str(area0) # if estimate_org_flash_size(area0, size):#feature_detetor.estimate_orange_area(area0,size): # x, y, w, h = cv2.boundingRect(cnt) # print org_pos_x,org_pos_y,x,y,area0 # org_btn_candidate_1.append(cnt) log.print_debug(TAG, "for area of cnts 2") for cnt in cnts2: # if area0 > 10: # print area0 cnt_len = cv2.arcLength(cnt, True) area0 = cv2.contourArea(cnt) cnt = cv2.approxPolyDP(cnt, 0.02 * cnt_len, False) if area0 > 100: x, y, w, h = cv2.boundingRect(cnt) print "flash button area size " + str(area0) + " Posx " + str( x) + " Posy " + str(y) if estimate_org_flash_size( area0, size): # feature_detetor.estimate_orange_area(area0,size): x, y, w, h = cv2.boundingRect(cnt) log.print_debug( TAG, "flash button area size " + str(area0) + " Posx " + str(x) + " Posy " + str(y)) org_btn_candidate_2.append(cnt) # counter1 = orange_flash_num(org_btn_candidate_1) counter2 = orange_flash_num(org_btn_candidate_2) if counter2 >= 1: # print org_btn_candidate_2 confidence_counter += 1 if confidence_counter >= 4: log.print_debug(TAG, "detected!!") reset() return True print '------------end' return False
lamda = np.random.randint(N_ACT, size=N_S) #initialize policy of dog lamda_re = lamda.copy() + 1 record = np.zeros(STEP) p = 1 / N_ACT * np.ones([N_S, N_ACT]) # policy for each observation t = 0 done = 0 s = 0 step_t = 0 while done != 1: if random.random() < 0: a = np.random.randint(0, N_ACT) else: a = lamda[s] dog_action = a rat_action = get_action(p_act[s], N_ACT) f, result = show_image(s, rat_action, dog_action, dog_size, rat_size, length, N_ACT, PI) step_t += 1 if f == 3 or step_t >= 16: # limit the number of feedback s += 1 step_t = 0 if s >= N_S: print('finish experiment') break else: continue record_n[s, a] += 1 if f == 0: r = 1 elif f == 1: r = -1
def build_model(): optimizer = Adadelta() # build Completion Network model org_img = Input(shape=input_shape, dtype='float32') mask = Input(shape=(input_shape[0], input_shape[1], 1)) generator, completion_out = model_generator(org_img, mask) completion_model = generator.compile(loss='mse', optimizer=optimizer) # build Discriminator model in_pts = Input(shape=(4, ), dtype='int32') # [y1,x1,y2,x2] discriminator = model_discriminator(input_shape, local_shape) d_container = Network(inputs=[org_img, in_pts], outputs=discriminator([org_img, in_pts])) d_out = d_container([org_img, in_pts]) d_model = Model([org_img, in_pts], d_out) d_model.compile(loss='binary_crossentropy', optimizer=optimizer) d_container.trainable = False # build Discriminator & Completion Network models all_model = Model([org_img, mask, in_pts], [completion_out, d_out]) all_model.compile(loss=['mse', 'binary_crossentropy'], loss_weights=[1.0, alpha], optimizer=optimizer) X_train = filenames[:5000] valid = np.ones((batch_size, 1)) ## label fake = np.zeros((batch_size, 1)) ## label for n in range(n_epoch): progbar = generic_utils.Progbar(len(X_train)) for i in range(int(len(X_train) // batch_size)): X_batch = X_train[i * batch_size:(i + 1) * batch_size] inputs = np.array([ process_image(filename, input_shape[:2]) for filename in X_batch ]) points, masks = get_points(batch_size) completion_image = generator.predict([inputs, masks]) g_loss = 0.0 d_loss = 0.0 if n < tc: g_loss = generator.train_on_batch([inputs, masks], inputs) else: d_loss_real = d_model.train_on_batch([inputs, points], valid) d_loss_fake = d_model.train_on_batch( [completion_image, points], fake) d_loss = 0.5 * np.add(d_loss_real, d_loss_fake) if n >= tc + td: g_loss = all_model.train_on_batch([inputs, masks, points], [inputs, valid]) g_loss = g_loss[0] + alpha * g_loss[1] progbar.add(inputs.shape[0], values=[("Epoch", int(n + 1)), ("D loss", d_loss), ("G mse", g_loss)]) # show_image show_image(batch_size, n, inputs, masks, completion_image) # save model generator.save("model/generator.h5") discriminator.save("model/discriminator.h5")
def main() : ### ========== TODO : START ========== ### # part 1: explore LFW data set X, y = util.get_lfw_data() n,d = X.shape avg_face = [] for column_index in range(d): col = X[:,column_index] avg_face_attr = np.mean(col, axis=0) avg_face.append(avg_face_attr) util.show_image(np.array(avg_face)) ### ========== TODO : END ========== ### # 1b U, mu = util.PCA(X) n,d = U.shape plot_gallery([vec_to_image(U[:,i]) for i in xrange(12)]) for column_index in range(d): col = U[:,column_index] util.show_image(util.vec_to_image(col)) # 1c ls = [1, 10, 50, 100, 500, 1288] for l in ls: Z, Ul = util.apply_PCA_from_Eig(X, U, l, mu) X_rec = util.reconstruct_from_PCA(Z, Ul, mu) plot_gallery(X_rec[:12]) # test centroid # p1 = Point('1', 1, np.array([5, 4])) # p2 = Point('2', 2, np.array([9, 10])) # p3 = Point('3', 3, np.array([3, 9])) # c = Cluster([p1, p2, p3]) # print(str(c)) # print(str(c.centroid())) # end test centroid ### ========== TODO : START ========== ### # part 2d-2f: cluster toy dataset np.random.seed(1234) k = 3 pts_per_cluster = 20 for i in range(1): points = generate_points_2d(pts_per_cluster) k_clusters = kMeans(points, k, init="cheat", plot=True) k_clusters = kMedoids(points, k, init="cheat", plot=True) ### ========== TODO : END ========== ### ### ========== TODO : START ========== ### # part 3a: cluster faces np.random.seed(1234) k = 4 X1, y1 = util.limit_pics(X, y, [4, 6, 13, 16], 40) points = build_face_image_points(X1, y1) plot = {} for pt in points: if pt.label not in plot: plot[pt.label] = [] plot[pt.label].append(pt) clusters = ClusterSet() for l in plot: clusters.add(Cluster(plot[l])) plot_clusters(clusters, 'orig', ClusterSet.centroids) Part 3a centroid_score = [] medoid_score = [] for i in range(10): k_clusters = kMeans(points, k, init="random", plot=False) centroid_score.append(k_clusters.score()) centroid_mean = sum(centroid_score) / float(len(centroid_score)) centroid_min = min(centroid_score) centroid_max = max(centroid_score) print('Centroid avg:', centroid_mean) print('Centroid min:', centroid_min) print('Centroid max:', centroid_max) medoid_score = [] for i in range(10): k_clusters = kMedoids(points, k, init="random", plot=False) medoid_score.append(k_clusters.score()) centroid_mean = sum(medoid_score) / float(len(medoid_score)) centroid_min = min(medoid_score) centroid_max = max(medoid_score) print('Medoid avg:', centroid_mean) print('Medoid min:', centroid_min) print('Medoid max:', centroid_max) # part 3b: explore effect of lower-dimensional representations on clustering performance np.random.seed(1234) U, mu = util.PCA(X) X1, y1 = util.limit_pics(X, y, [4, 13], 40) k = 2 ls = range(42)[1::2] centroid_score = [] medoid_score = [] for l in ls: Z, Ul = util.apply_PCA_from_Eig(X1, U, l, mu) # X_rec = util.reconstruct_from_PCA(Z, Ul, mu) points = build_face_image_points(Z, y1) # plot_gallery(X_rec[:12]) c = kMeans(points, k, init="cheat", plot=False) centroid_score.append(c.score()) k_clusters = kMedoids(points, k, init="cheat") medoid_score.append(k_clusters.score()) scatter = plt.scatter(ls, centroid_score, c='c', s=20) scatter2 = plt.scatter(ls, medoid_score, c='r', s=20) plt.suptitle('kMeans and kMedoids', fontsize=20) plt.xlabel('L', fontsize=16) plt.ylabel('Score', fontsize=16) plt.legend((scatter, scatter2), ('kMeans', 'kMedoids'), scatterpoints=1, loc='lower right', ncol=3, fontsize=14) plt.show() # part 3c: determine ``most discriminative'' and ``least discriminative'' pairs of images np.random.seed(1234) totalPeople = 19 best_score = 0 worst_score = float("inf") best_pair = None worst_pair = None for p1 in xrange(totalPeople): for p2 in xrange(p1+1, totalPeople): X3, y3 = util.limit_pics(X, y, [p1, p2], 40) points = build_face_image_points(X3, y3) clusters = kAverages(points, 2, ClusterSet.medoids, init='cheat', plot=False) score = clusters.score() if score > best_score: best_score = score best_pair = (p1,p2) if score < worst_score: worst_score = score worst_pair = (p1,p2) print(best_pair) print(best_score) plot_representative_images(X,y, best_pair, title="Most Similar Face") print(worst_pair) print(worst_score) plot_representative_images(X,y, worst_pair, title="Least Similar Face")
def reconstruct(): while True: x = raw_input('emoji name > ') if len(x) == 0: break show_image(n.reconstruct(x + '.png'))
from util import show_image from flowermodel import FlowerModel a = FlowerModel() a.pre_processing() a.train(5) a.predict("./TestImages/rainbowRose.jpg") show_image('./TestImages/rainbowRose.jpg') a.saveModel("flowerData.pth")