def __autogenerate(self, template): """Generates plate based on template provided""" # Open base image template self.base_file = utils.get_random_item(template["base-image"]) image_path = os.path.join( self.context.getConfig("General", "templates_path"), self.base_file) self.image_data = PIL.Image.open(image_path) # Generate & draw plate number plate_template = utils.get_random_item(template["plate-number"]) self.plate_number, self.bounding_boxes = self.draw_regex( plate_template) # Draw extra text, if any if "extra-text" in template.keys(): for text_template in template["extra-text"]: self.draw_regex(text_template) # Will use CV2 for the rest of image operations self.image_data = self.pil_to_cv2(self.image_data) # Generate plate bbox, add it to the list w = self.image_data.shape[1] h = self.image_data.shape[0] cx = (w / 2) cy = (h / 2) plate_bbox = copy.copy(BBOX_ANNOTATION) plate_bbox['class'] = self.type plate_bbox['cx'], plate_bbox['cy'], plate_bbox['w'], plate_bbox[ 'h'] = cx, cy, w, h self.bounding_boxes.append(plate_bbox)
def create_purchase(session): customer = utils.get_random_item(customer_pool) product = utils.get_random_item(product_pool) purchase = Purchase() purchase.purchase_created = utils.get_random_date_update() purchase.purchase_updated = utils.get_random_date_create() session.add(purchase) purchase_detail_status = create_purchase_detail_status(session) purchase_detail = create_purchase_detail(session, customer, product, purchase, purchase_detail_status) session.commit() session.flush() return purchase, purchase_detail, purchase_detail_status
def get_random_bg(context): """Returns a random background image from configured path""" bg_path = context.getConfig('General', 'backgrounds_path') bg_list = os.listdir(bg_path) selected_bg = bg_list[random.randrange(len(bg_list))] bg_image = cv2.imread(os.path.join(bg_path, selected_bg)) # Resize image according to the list of configured sizes sizes = ast.literal_eval(context.getConfig('Image', 'bg_sizes')) size = utils.get_random_item(sizes) bg_image = utils.resize_image(bg_image, size) # Add alpha channel to image if not present, this is to add foreground if bg_image.shape[2] == 3: bg_image = cv2.cvtColor(bg_image, cv2.COLOR_RGB2RGBA) return bg_image
output_path = appContext.getConfig('General', 'output_path') annotator_type = appContext.getConfig('General', 'annotation_type') annotator = annotations.AnnotatorFactory.get_annotator(annotator_type) # Create output directory or clean it clear_output = appContext.getBoolean('General', 'clear_output') if not os.path.exists(output_path): os.makedirs(output_path) elif clear_output: files = glob.glob(os.path.join(output_path, '*')) for f in files: os.remove(f) for i in range(dataset_size): # Generate from random template plate_type = utils.get_random_item(templates) new_plate = plate.Plate(appContext, plate_type, templates[plate_type]) # Change perspective, size and background new_plate.random_resize() new_plate.image_data, new_plate.bounding_boxes = perspective.warp_image_random( new_plate.image_data, new_plate.bounding_boxes, appContext) new_plate.image_data, new_plate.bounding_boxes = scene.add_backgroud( new_plate.image_data, new_plate.bounding_boxes, appContext) # Generate annotation and image file annotator.append_annotation(new_plate) new_plate.save_image(output_path) time.sleep( 0.5 ) # TODO: Find solution for random images that are not written to disk
def random_resize(self): plate_scales = ast.literal_eval( self.context.getConfig('Image', 'plate_scales')) scale_factor = utils.get_random_item(plate_scales) self.resize_image(scale_factor) self.resize_bboxes(scale_factor)
def main(force_target=None): credentials = get_credentials() senders = get_senders() recipients = [] badges = get_badges() replicas = read_file_by_lines("replicas.txt", True) messages = [] attempts_all = 0 attempts_success = 0 print("success\t\t|sender\t\t| recipient\t\t| badge\t\t| message") print( "----------------|-----------|---------------|-----------|-------------------------" ) if force_target: for badge in badges: for sender in senders: attempts_all += 1 if len(messages) == 0: messages = read_file_by_lines("messages-ru.txt", True) replica = get_random_item(replicas, False) message = get_random_item(messages, True) attempt = Attempt(sender, force_target, badge) attempt.message = replica + " " + message attempt.is_request_success = send_request(credentials, attempt) if attempt.is_request_success: attempts_success += 1 else: for sender in senders: sender.get_points(credentials, True) for _ in badges: for sender in senders: if sender.can_send_more(badges): attempts_all += 1 if len(recipients) <= 1: recipients = get_recipients_profile_ids(senders) if len(messages) == 0: messages = read_file_by_lines("messages-ru.txt", True) replica = get_random_item(replicas, False) message = get_random_item(messages, True) attempt = sender.new_attempt(badges, recipients) attempt.message = replica + " " + message attempt.is_request_success = send_request( credentials, attempt) if attempt.is_request_success: attempts_success += 1 print("\nRESULTS:") for sender in senders: sender.get_points(credentials, False) sender.print_points() print("\nATTEMPTS:\t" + str(attempts_all) + "\nSUCCESS:\t" + str(attempts_success))