def user_image_create(): username_of_jwt = get_jwt_identity() image_id = 1 images = Image.query.order_by(Image.id.desc()).first() if images is None: pass else: image_id = images.id + 1 if "image" not in request.files: return abort(400, description="No image") image = request.files["image"] if Path(image.filename).suffix not in [".jpg", ".png"]: return abort(400, description="Invalid file type") filename = str(image_id) + Path(image.filename).suffix bucket = boto3.resource("s3").Bucket(current_app.config["AWS_S3_BUCKET"]) key = f"images/user-{username_of_jwt}-image-{filename}" bucket.upload_fileobj(image, key) new_image = Image() new_image.username = username_of_jwt new_image.path = key db.session.add(new_image) db.session.commit() return jsonify(image_schema.dump(new_image))
def save_image(source, file_name, from_type='string'): """ Saves an image to disk. # Arguments source: image to be saved. from_type: format of image to be saved. # Raises NotImplementedError: if from_type is not supported. """ image = Image() db.session.add(image) db.session.flush() image.name = file_name image.path = f'uploads/images/{image.id}_{file_name}' image.thumbnail = f'uploads/thumbnails/{image.id}_{file_name}' if from_type == 'np_array': cv2.imwrite(image.path, source) elif from_type == 'string': with open(image.path, 'wb+') as f: f.write(source.read()) else: raise NotImplementedError(f'Support for {from_type} is currenlty not supported!') prediction, label, class_index = app.config['MODEL'].predict_from_path(image.path) image.label = label image.prediction = prediction image.class_index = int(class_index) thumbnail = cv2.imread(image.path) thumbnail = cv2.resize(thumbnail, (100, 100)) cv2.imwrite(image.thumbnail, thumbnail) db.session.add(image) db.session.commit()