def process_sprites(sprite_paths_list, sprite_width, output_dir): logger.info("Processing sprites") processed_sprite_paths_list = [] for sprite_paths in sprite_paths_list: processed_paths = [] for sprite_path in sprite_paths: sprite_name = os.path.basename(sprite_path) output_path = os.path.join(output_dir, sprite_name) process_image(sprite_path, sprite_width, output_path) processed_paths.append(output_path) logger.debug("Processed sprite '" + sprite_name + "'") processed_sprite_paths_list.append(processed_paths) return processed_sprite_paths_list
def photo_ml(array): image_age = image_sentiment = 0 image_emotions = { "anger": 0, "contempt": 0, "disgust": 0, "fear": 0, "happiness": 0, "neutral": 0, "sadness": 0, "surprise": 0 } response = {} for i in array: url = i[ "image_url"] #'https://instagram.fsst1-2.fna.fbcdn.net/t51.2885-15/e35/17818239_241984309542803_360515474607308800_n.jpg' text = i["content_text"] if image.process_image(url) != False: emotions = photo_processing.total_score( image.process_image(url)[0]) info = image.process_image(url)[1] age = sum([j["age"] for j in info["faces"]]) / len(info["faces"]) #sentiment_score = sentiment.process_text(text) image_age += age #if sentiment_score: # image_sentiment += sentiment_score image_emotions["anger"] += emotions[0]["anger"] image_emotions["contempt"] += emotions[0]["contempt"] image_emotions["disgust"] += emotions[0]["disgust"] image_emotions["fear"] += emotions[0]["fear"] image_emotions["happiness"] += emotions[0]["happiness"] image_emotions["neutral"] += 0 #emotions[0]["neutral"] image_emotions["sadness"] += emotions[0]["sadness"] image_emotions["surprise"] += emotions[0]["surprise"] n = len(array) response['emotions'] = { "anger": image_emotions["anger"], "contempt": image_emotions["contempt"], "disgust": image_emotions["disgust"], "fear": image_emotions["fear"], "happiness": image_emotions["happiness"], "neutral": image_emotions["neutral"], "sadness": image_emotions["sadness"], "surprise": image_emotions["surprise"] } response['age'] = image_age / n #response['sentiment']= image_sentiment/n return response
def main(): #path = "flowers\\valid\\9\\image_06414.jpg" #print(path) #Image.open(path) in_arg = get_input_args() arch = in_arg.arch checkpoint_path = in_arg.checkpoint top_k = in_arg.top_k #cat_to_name = in_arg.category_names image_path = in_arg.image_path #processing_unit = in_arg.gpu if torch.cuda.is_available() and in_arg.gpu == 'gpu': print('GPU will be used') processing_unit = 'gpu' elif torch.cuda.is_available() == False: print('CPU will be used') processing_unit = 'cpu' with open(in_arg.category_names, 'r') as f: cat_to_name = json.load(f) #print(in_arg,arch,checkpoint_path,top_k,cat_to_name,processing_unit,image_path) class_to_idx = torch.load(checkpoint_path)['class_to_idx'] model_new = pretrained_model(arch) model = load_checkpoint(checkpoint_path, model_new) image = process_image(image_path) pros, indexes = predict(image, model, top_k, processing_unit) check_predict(class_to_idx, cat_to_name, pros, indexes, top_k)
def predict(image_path, model, topk, gpu): ''' Predict the class (or classes) of an image using a trained deep learning model. ''' image = torch.Tensor(process_image(image=image_path)).unsqueeze_(0) device = torch.device( "cuda" if gpu and torch.cuda.is_available() else "cpu") model.to(device) image = image.to(device) model.eval() with torch.no_grad(): logps = model.forward(image) ps = np.exp(logps) probs, classes = ps.topk(topk, dim=1) return np.asarray(probs[0]) * 100, np.asarray(classes[0])
def predict(image_path, model, use_gpu, idx_to_class, topk): if use_gpu: model.to('cuda') model.eval() softmax = nn.Softmax(dim=1) inputs = np.array([image.process_image(image_path)]) inputs = torch.from_numpy(inputs).float() if use_gpu: inputs.to('cuda') outputs = model(inputs) outputs = softmax(outputs) topk = outputs.topk(topk) probs = topk[0].to('cpu').detach().numpy()[0] indices = topk[1].to('cpu').detach().numpy()[0] del inputs, outputs torch.cuda.empty_cache() classes = [idx_to_class[str(x)] for x in indices] return probs, classes
dest='y', help='The new y images size', required=True) parser.add_argument( '-s', '--save_box_images', dest='save_box_images', help= 'If True, it will save the resized image and a drawed image with the boxes in the images', default=0) IMAGE_FORMATS = ('.jpeg', '.png', '.jpg') args = parser.parse_args() create_path(args.output_path) create_path(''.join([args.output_path, '/boxes_images'])) args.dataset_path = add_end_slash(args.dataset_path) args.output_path = add_end_slash(args.output_path) for root, _, files in os.walk(args.dataset_path): output_path = os.path.join(args.output_path, root[len(args.dataset_path):]) create_path(output_path) for file in files: if file.endswith(IMAGE_FORMATS): file_path = os.path.join(root, file) process_image(file_path, output_path, int(args.x), int(args.y), args.save_box_images)
required=False) parser.add_argument( '-s', '--save_box_images', dest='save_box_images', help= 'If True, it will save the resized image and a drawed image with the boxes in the images', default=0) IMAGE_FORMATS = ('.jpeg', '.JPEG', '.png', '.PNG', '.jpg', '.JPG') args = parser.parse_args() create_path(args.output_path) if int(args.save_box_images): create_path(''.join([args.output_path, '/boxes_images'])) args.dataset_path = add_end_slash(args.dataset_path) args.output_path = add_end_slash(args.output_path) for root, _, files in os.walk(args.dataset_path): output_path = os.path.join(args.output_path, root[len(args.dataset_path):]) create_path(output_path) for file in files: if file.endswith(IMAGE_FORMATS): file_path = os.path.join(root, file) process_image(file_path, output_path, args.x, args.y, args.save_box_images, args.mode)
def hello(): if request.method == 'GET': bucket_name = request.args.get('bucket') key = request.args.get('key') res = process_image(bucket_name, key) return jsonify(res)
# '-m', # '--margins', # dest='margins', # help='...', # default=None # ) IMAGE_FORMATS = ('.jpeg', '.JPEG', '.png', '.PNG', '.jpg', '.JPG') args = parser.parse_args() args.dataset_path += '/data_train' args.output_path += '/data_train' create_path(args.output_path) create_path(''.join([args.output_path, '/boxes_images'])) args.dataset_path = add_end_slash(args.dataset_path) args.output_path = add_end_slash(args.output_path) assert os.path.exists( args.dataset_path ), 'All data (images and annotations) should be within a folder named \'data_train\'' for root, _, files in os.walk(args.dataset_path): output_path = os.path.join(args.output_path, root[len(args.dataset_path):]) create_path(output_path) for file in files: if file.endswith(IMAGE_FORMATS): file_path = os.path.join(root, file) process_image(file_path, output_path, int(args.w), int(args.h), args.save_box_images, args.do_crop, args.do_sub_crop)