def change_button_img(self): # run in Thread brk = 0 count = 0 while 'playlistOfTheDay' not in self.yandex.play_lists_info: # wite loging to yandex time.sleep(.1) if brk > 100: break brk += 1 else: count = self.yandex.play_lists_info[ 'playlistOfTheDay'].play_counter.value if not count: return imgPPM = utils.Image(file=self.path + 'img/PlaylistOftheDay100Num.ppm') imgPPM.addCount(count, 10, 83, [(77, 200, 73), (82, 201, 79)]) self.bPhotos['playlistOfTheDay'] = imgPPM() self.lBttns['playlistOfTheDay']['image'] = self.bPhotos[ 'playlistOfTheDay'] imgPPM.darker(44, 57, 33) self.bPhotos['playlistOfTheDayCancel'] = imgPPM()
def parents_parts(num): offspring = [] for i in range(num): parent1_img = random.choice(self.current_population)["img"] parent2_img = random.choice(self.current_population)["img"] child = { "img": utils.Image(imgs=self.imgs, imgs_shape=self.small_img_shape), "score": -1 } # threshold = np.random.uniform(0,1) child_indexes = parent1_img.get_index().copy() # old_child_indexes = child_indexes.copy() total_num_rc = parent1_img.get_index().shape num_rows_selected = int(total_num_rc[0] * self.crossover_percentage[0]) num_cols_selected = int(total_num_rc[1] * self.crossover_percentage[1]) parent1_parts = tuple([ parent1_img.get_index() [:num_rows_selected, :num_cols_selected], parent1_img.get_index()[num_rows_selected:, num_cols_selected:] ]) parent2_parts = tuple([ parent2_img.get_index() [:num_rows_selected, :num_cols_selected], parent2_img.get_index()[num_rows_selected:, num_cols_selected:] ]) parts_1p = tuple([parent1_parts[0], parent2_parts[0]]) parts_2p = tuple([parent1_parts[1], parent2_parts[1]]) # all parts can swapped together if (num_rows_selected == total_num_rc[0] - num_rows_selected and num_cols_selected == total_num_rc[1] - num_cols_selected): parts = tuple( [parts_1p[0], parts_1p[1], parts_2p[0], parts_2p[1]]) child_indexes[:num_rows_selected, : num_cols_selected] = random.choice(parts) child_indexes[num_rows_selected:, num_cols_selected:] = random.choice(parts) else: child_indexes[:num_rows_selected, : num_cols_selected] = random.choice(parts_1p) child_indexes[num_rows_selected:, num_cols_selected:] = random.choice(parts_2p) child["img"].set_index(child_indexes) child["score"] = self._fitness_single( child["img"].construct_img()) offspring.append(child) return offspring
def greedy_parts_v2(gen_num, parent_num=None): if (parent_num is None): parent_num = len(self.current_population) initial_indexes = self.current_population[0]["img"].get_index() child = { "img": utils.Image(imgs=self.imgs, imgs_shape=self.small_img_shape), "score": -1 } child_indexes = initial_indexes.copy() # child["img"].set_index(child_indexes) # child["score"] = self._fitness_single(child["img"].construct_img()) # print(child["score"]) # input("~~~INPUT DEBUG") parent_num = min(parent_num, len(self.current_population)) parents = random.choices(self.current_population, k=parent_num) for index_row, row in tqdm(enumerate(child_indexes)): for index_col, _ in tqdm(enumerate(row)): mn_error = 10000000000000000 mn_index = 0 for i in tqdm(range(parent_num)): parent_indexes = parents[i]["img"].get_index() for parent_index_row, parent_row in enumerate( parent_indexes): for parent_index_col, _ in enumerate(parent_row): parent_gene_index = parent_indexes[ parent_index_row][parent_index_col] parent_img_gene = self.imgs[parent_gene_index] rlim, clim = self.small_img_shape[:2] input_img_gene = self.input_img[ rlim * index_row:rlim * index_row + rlim, clim * index_col:clim * index_col + clim] error = self.calc_error(input_img_gene, parent_img_gene, mn_error=mn_error) if (error < mn_error): mn_error = error mn_index = parent_gene_index child_indexes[index_row][index_col] = mn_index child["img"].set_index(child_indexes) child["score"] = self._fitness_single(child["img"].construct_img()) # print(child["score"]) # input("~~~INPUT DEBUG") offspring = [child for _ in range(gen_num)] return offspring
def init(images, ri): print "inside init" global ReprojectedImage global TargetImageSize aEnergy = GetApperanceEnergy(images[ReferenceLabel], images) for i in range(len(images)): image = images[i] klDivergenceEnergy = aEnergy[i] Images.append( utils.Image(image=image, cameraPose=None, kLDivergenceEnergy=klDivergenceEnergy)) TargetImageSize = len(ri), len(ri[1]) ReprojectedImage = [[{} for _ in range(TargetImageSize[1])] for _ in range(TargetImageSize[0])] for i, pixelList in enumerate(ri): for j, pixelBucket in enumerate(pixelList): ReprojectedImage[i][j] = CompressPixelList(pixelBucket)
def _generate_population(self, parent=None): # Totally random population population = [] if (parent is None): for _ in range(self.population_size): chromosome_indexes = np.random.randint( self.max_index_imgs, size=self.small_imgs_num_rc) chromosome_img = utils.Image(imgs=self.imgs, index=chromosome_indexes, imgs_shape=self.small_img_shape) # img = chromosome_img.construct_img() img = chromosome_img population.append({"img": img, "score": -1}) # According to the parent else: population = None return population
def uniform(gen_num, parent_num=None): if (gen_num == 0): return 0 if (parent_num is None): parent_num = len(self.current_population) offspring = [] for _ in range(gen_num): initial_indexes = self.current_population[0]["img"].get_index() child = { "img": utils.Image(imgs=self.imgs, imgs_shape=self.small_img_shape), "score": -1 } child_indexes = initial_indexes.copy() parent_num = min(parent_num, len(self.current_population)) parents = random.choices(self.current_population, k=parent_num) for index_row, row in enumerate(child_indexes): for index_col, _ in enumerate(row): random_parent_index = np.random.randint(parent_num) random_parent_index_row = np.random.randint( self.small_imgs_num_rc[0]) random_parent_index_col = np.random.randint( self.small_imgs_num_rc[1]) child_indexes[index_row][index_col] = parents[ random_parent_index]["img"].get_index( )[random_parent_index_row][random_parent_index_col] child["img"].set_index(child_indexes) child["score"] = self._fitness_single( child["img"].construct_img()) offspring.append(child) return offspring
def aug_function(inpath, outputpath, incsvfilepath, outcsvfilepath, augtype, t_factor, r_factor, s_factor): #print("\n") #print(augtype) #print("\n") file1 = open(incsvfilepath, 'r') # 2. open file containing bounding box coordinates lines = file1.readlines() randlist = lines # selecting k random images to augment for i in tqdm_gui(randlist): x = i.strip().split(",") #print("\nx: ") #print(x) #print("\n") mylist = [ int(float(x[1])), int(float(x[2])), int(float(x[3])), int(float(x[4])) ] #extract coordinates from file cord = [] for i in mylist: a = int(i) cord.append(a) a = x[0] # image filename imageFolderPath = inpath # 3. input image folder path loc = os.path.join(imageFolderPath, a) #location of image # # Brightness and Contrast if (augtype == 'Brightness and Contrast'): image = cv.imread(loc) alpha = random.triangular(1, 2) beta = random.randint(20, 50) result = cv.addWeighted(image, alpha, np.zeros(image.shape, image.dtype), 0, beta) oname = "brightness_" + a csvr = [oname] + mylist with open(outcsvfilepath, 'a', newline='') as file: # 4. write to csv file writer = csv.writer(file) writer.writerow(csvr) #print(mylist) #print(oname) drawRectangle(result, outputpath, cord, output_name=oname) else: img_class = utils.Image(path=loc) # Create image class img = img_class.getImage() coord = cord if (augtype == 'Scale'): # # Test Scaling output = img_class.transform('scale', coord, s_factor) oname = "scaled_" + a # name of output image ocord = output[1] # augmented coordinates csvr = [oname ] + output[1] # line to be written to output csv file with open(outcsvfilepath, 'a', newline='') as file: # 4. write to csv file writer = csv.writer(file) writer.writerow(csvr) drawRectangle(output[0], outputpath, output[1], output_name=oname) # for saving output image elif (augtype == 'Rotate'): #print("\nrotate") ##Test Rotation output = img_class.transform('rotate', coord, r_factor) oname = "rotated_" + a # ##print("ONAME",oname) # ##print("New Bounding Boxes rotation: ", output[1]) ocord = output[1] csvr = [oname] + output[1] # ##print("ocord",ocord) with open(outcsvfilepath, 'a', newline='') as file: writer = csv.writer(file) writer.writerow(csvr) drawRectangle(output[0], outputpath, output[1], output_name=oname) #drawRectangle(img,outputpath, coord, output_name = "output_original.jpeg") elif (augtype == 'Translate'): #print("\ntranslate") # # Test Translation - Horizontal output = img_class.transform('translate', coord, t_factor) oname = "translated_" + a # name of output image ocord = output[1] # augmented coordinates csvr = [oname ] + output[1] # line to be written to output csv file with open(outcsvfilepath, 'a', newline='') as file: #write to csv file writer = csv.writer(file) writer.writerow(csvr) drawRectangle(output[0], outputpath, output[1], output_name=oname) # for saving output image elif (augtype == 'Flip'): #print("\nflip") # # Test Flipping output = img_class.transform('flip', coord) oname = "flipped_" + a # name of output image ocord = ' '.join(str(e) for e in output[1]) # augmented coordinates csvr = oname + ' ' + ocord # line to be written to output csv file listcsvr = csvr.split(" ") #print("\noutcsvfilepath: ") #print(outcsvfilepath) #print("\ncsvr: ") #print(csvr) with open(outcsvfilepath, 'a', newline='') as file: #write to csv file writer = csv.writer(file) writer.writerow(listcsvr) drawRectangle(output[0], outputpath, output[1], output_name=oname) # for saving output image elif (augtype == 'Shear'): # # Test Shear #print("\nshear") output = img_class.transform('shear', coord) oname = "shear_" + a # name of output image ocord = output[1] # augmented coordinates csvr = [oname ] + output[1] # line to be written to output csv file with open(outcsvfilepath, 'a', newline='') as file: #write to csv file writer = csv.writer(file) writer.writerow(csvr) drawRectangle(output[0], outputpath, output[1], output_name=oname) # for saving output image elif (augtype == 'Saturation'): #print("\nhsv") HSV_output_name = "Saturated_" + a img_HSV, bboxes_HSV = utils.RandomHSV(hue=None, saturation=100, brightness=None)( img.copy(), cord.copy()) drawRectangle( img_HSV, outputpath, bboxes_HSV, output_name=HSV_output_name) # for saving output image csvr = [HSV_output_name ] + bboxes_HSV # line to be written to output csv file with open(outcsvfilepath, 'a', newline='') as file: #write to csv file writer = csv.writer(file) writer.writerow(csvr)