def main(): args = parse_args() gpu = torch.cuda.is_available() and not args.force_cpu content_image = load_image_from_url( args.content_url) if args.content_url else load_image( args.content_path) style_image = load_image_from_url( args.style_url) if args.style_url else load_image(args.style_path) content_var = load_variable(content_image, gpu=gpu) style_var = load_variable(style_image, gpu=gpu) net = load_features_extractor(args.net) model = NeuralStyleNet(net) if gpu: model.cuda() solver = Solver(model, content_var, style_var, num_iters=args.num_iters, gpu=gpu) styled_image = solver.train() styled_image.save(args.output_path)
def valid_image(img_url): """ Validates that the given URL is actually a valid image. Returns True if there's a valid image at the given URL, and False otherwise. img_url -- The URL of the possible image. """ return True return load_image_from_url( img_url )
def polygons(): url = request.args.get("url") img = load_image_from_url(url) predictions = ml.detector.get_polygonal_outputs(img) response = {"successful": True, "data": predictions} return jsonify(response)
def hash(self): image_hash = db.get_full_item(self.image_id).get('hash_md5', None) if image_hash is None: if self._image is None: image_hash = image_hash_md5(load_image_from_url(self.url)) else: image_hash = image_hash_md5(self._image) db.get_full_item(self.image_id)['hash_md5'] = image_hash return image_hash
def image(self): if self._image is None: # self._image = random_image(self.image_id) self._image = load_image_from_url(self.url) # self._image = resize_image(self._image, 400) basic_image_id = self.basic_image_id angle = db.get_full_item(basic_image_id).get('angle', None) borders = db.get_full_item(basic_image_id).get('borders', None) (self._image, angle, borders) = transform_image(self._image, angle=angle, borders=borders) db.get_full_item(self.image_id)['angle'] = angle db.get_full_item(self.image_id)['borders'] = borders return self._image
def preprocess_vk_images(images): result = [] i = 0 for photos in images: if i > 2: break try: for size in photos['sizes']: if size['type'] == 'x': preprocessed = preprocess(load_image_from_url( size['url']))[0] # plt.imshow(preprocessed) # plt.show() result.append(np.array(preprocessed)) except BaseException: i -= 1 i += 1 return result
def async_rendered(): tmp_img = f"/tmp/{uuid.uuid4()}.png" if request.method == 'POST': img = load_image_from_formdata(request.files['file'].stream) elif request.method == 'GET': url = request.args.get("url") img = load_image_from_url(url) cv2.imwrite(tmp_img, img) rendered_image_name = f"{uuid.uuid4()}.png" output_file_name = f"{ml.cwd}/static/{rendered_image_name}" job = async_predict_rendered.apply_async(args=[tmp_img, output_file_name]) response = {"successful": True, "data": {"jobid": job.id}} return jsonify(response), 200
def rendered(): if request.method == 'POST': img = load_image_from_formdata(request.files['file'].stream) elif request.method == 'GET': url = request.args.get("url") img = load_image_from_url(url) rendered_image_name = f"{uuid.uuid4()}.png" output_file_name = f"{ml.cwd}/static/{rendered_image_name}" rendered = ml.detector.get_rendered_outputs(img, savename=output_file_name) response = { "successful": True, "data": { "url": url_for('ml.static', filename=rendered_image_name, _external=True) } } return jsonify(response)
"convert db (format 2.0) to format 3.0 (adding hash for every picture") parser.add_argument('-f', '--fromfile', type=str, required=True, help='file to convert from') parser.add_argument('-t', '--tofile', type=str, required=True, help='file to save to') args = parser.parse_args() from_file = args.fromfile to_file = args.tofile with open(from_file, 'r') as f: db = json.loads(f.read()) for image_id in tqdm(db): image_obj = db[image_id] md = image_obj.get('markdown', {}) if md == {}: continue image = load_image_from_url(image_obj['url']) image_obj['hash_md5'] = image_hash_md5(image) with open(to_file, 'w') as f: print(json.dumps(db, indent=4, sort_keys=True), file=f)
np.random.seed(123) # read db file with open(DB_FILE_PATH, 'r') as f_to_read_json: db_json = json.loads(f_to_read_json.read()) model_corners = ModelCorners(threshold=MODELS_CORNERS_THRESHOLD ) model_edges = ModelEdges(threshold=MODELS_EDGES_THRESHOLD) model_corners.load(MODELS_CORNERS_MODEL) model_edges.load(MODELS_EDGES_MODEL) keys = sorted(list(db_json.keys()), key=int) for ic, image_id in enumerate(tqdm(keys)): # get markdown print('start ', image_id) section = db_json[image_id] image = load_image_from_url(section['url']) if 'angle' not in section: section['angle'] = 0 if 'borders' not in section: section['borders'] = { "bottom_border": image.size[1], "left_border": 0, "right_border": image.size[1], "up_border": 0 } image = transform_image(image, section['angle'], section['borders'])[0] image = clean_image(image) # predict markdown print('predict corners..')