def draw_face_rectangle(image_name): ''' save face detected image to faces/image_name params: image_name (str): file name of the image in ./images return: bool: True if at least one face was detected ''' face_cascade = cv.CascadeClassifier(os.path.join(app.root_path,'data/haarcascade_frontalface_default.xml')) img = cv.imread(os.path.join(app.root_path,'images/', image_name)) gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) if len(faces) == 0: return False for (x, y, w, h) in faces: cv.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) key = 'faces/' + image_name cv.imwrite(os.path.join(app.root_path, key), img) move_to_s3(key) return True
def upload(): ''' handle requests to api/upload params: username, password and the file content return: ok if success, otherwise a error message will be returned with request content type ''' error = None username = request.form['username'] password = request.form['password'] cursor = get_db().cursor(dictionary=True) cursor.execute('SELECT * FROM users WHERE username = %s', (username, )) user = cursor.fetchone() if user is None: error = 'User is not valid' elif not check_password_hash(user["password"], password): error = 'Incorrect password.' if error is not None: return abort(404, error) if 'file' not in request.files: error = 'You cannot upload empty file.' elif request.files['file'].filename == '': error = "Your file name is not valid." elif not allowed_file(request.files['file'].filename): error = "Your File format is not correct: {}".format( request.files['file'].filename) elif '\'' in request.files['file'].filename or '\"' in request.files[ 'file'].filename: error = "Invalid file name." else: file = request.files['file'] filename = file.filename cursor = get_db().cursor() cursor.execute('INSERT INTO images ( name, user_id) VALUES (%s, %s)', (filename, user['id'])) id = cursor.lastrowid filename = str(id) + '.' + filename.rsplit('.', 1)[1].lower() file.save(os.path.join(app.root_path, 'images/', filename)) try: save_thumbnail(filename, 200, 200) draw_face_rectangle(filename) move_to_s3('images/' + filename) get_db().commit() return 'ok\n' except: get_db().rollback() error = "Error creating image." os.remove(os.path.join(app.root_path, 'images/', filename)) return abort(404, error)
def upload(): ''' handle requests to api/upload params: username, password and the file content return: ok if success, otherwise a error message will be returned with request content type ''' error = None username = request.form['username'] password = request.form['password'] table = get_db().Table('Users') response = table.get_item(Key={'username': username}) user = response['Item'] if 'Item' in response else None if user is None: error = 'User is not valid' elif not check_password_hash(user["password"], password): error = 'Incorrect password.' if error is not None: return abort(404, error) if 'file' not in request.files: error = 'You cannot upload empty file.' elif request.files['file'].filename == '': error = "Your file name is not valid." elif not allowed_file(request.files['file'].filename): error = "Your File format is not correct: {}".format( request.files['file'].filename) elif '\'' in request.files['file'].filename or '\"' in request.files[ 'file'].filename: error = "Invalid file name." else: file = request.files['file'] filename = file.filename id = datetime.utcnow().strftime( "%Y-%m-%d-%H-%M-%S-%f") + user['username'].replace( '.', '').replace('/', '') filename = str(id) + '.' + filename.rsplit('.', 1)[1].lower() table = get_db().Table('Images') table.put_item(Item={'imageid': filename, 'user': user['username']}) move_to_s3(file, filename) return 'ok\n' return abort(404, error)
def save_thumbnail(imagename, frame_width, frame_height): ''' save thumbnail image (aspect ratio preserved) to thumbnails/image_name params: image_name (str): file name of the image in ./images frame_width, frame_height (int): width and height of specified frame return: ''' with wand.image.Image(filename=os.path.join(app.root_path, 'images/', imagename)) as img: img.strip() img.sample(int(img.width / 5), int(img.height / 5)) img.transform(resize='{}x{}>'.format(frame_width, frame_height)) key = 'thumbnails/' + imagename img.save(filename=os.path.join(app.root_path, key)) move_to_s3(key)
def create(): """Create a new image for the current user.""" if request.method == 'POST': error = None if 'file' not in request.files: error = 'You cannot upload empty file.' elif request.files['file'].filename == '': error = "Your file name is not valid." elif not allowed_file(request.files['file'].filename): error = "Your File format is not correct." elif '\'' in request.files['file'].filename or '\"' in request.files[ 'file'].filename: error = "Invalid file name." else: file = request.files['file'] filename = file.filename id = datetime.utcnow().strftime( "%Y-%m-%d-%H-%M-%S-%f") + g.user['username'].replace( '.', '').replace('/', '') filename = str(id) + '.' + filename.rsplit('.', 1)[1].lower() table = get_db().Table('Images') table.put_item(Item={ 'imageid': filename, 'user': g.user['username'] }) move_to_s3(file, filename) if error is not None: flash(error) return 'ok\n' return render_template('image/create.html')