def reset_level(file_id): get_file(file_id) db = get_db() db.execute('DELETE FROM face WHERE file_id = ?', (file_id, )) db.execute('UPDATE file SET check_level = 0 WHERE id = ?', (file_id, )) db.commit() return redirect(url_for('file.file_detail', file_id=file_id))
def load_logged_in_user(): user_id = session.get('user_id') if user_id is None: g.user = None else: g.user = get_db().execute('SELECT * FROM user WHERE id = ?', (user_id, )).fetchone()
def get_page_files(page_index=0): db = get_db() files = db.execute( 'SELECT f.id, f.name, f.path, f.created, f.uploaded, username, user_id, f.check_level FROM file f JOIN user u' ' ON f.user_id = u.id and u.id = ' + str(g.user['id']) + ' ORDER BY f.uploaded DESC LIMIT 50 OFFSET ' + str(page_index * 50)).fetchall() return files
def add_file(path, is_commit=True, db=None): if not db: db = get_db() db.execute('INSERT INTO file (user_id, path, name)' ' VALUES (?, ?, ?)', (g.user['id'], path, os.path.basename(path))) if is_commit: db.commit()
def file_detail(file_id): db = get_db() file = db.execute( 'SELECT f.id, f.name, f.path, f.created, f.uploaded, username, user_id, f.check_level FROM file f JOIN user u' ' ON f.user_id = u.id WHERE f.id = ' + str(file_id) + ' ORDER BY created, uploaded DESC').fetchone() thumbs = db.execute( 'SELECT thumb_path, file.id as file_id FROM file INNER JOIN face ON file.id = face.file_id and file.id = ' + str(file_id)).fetchall() return render_template('file/file_detail.html', file=file, thumbs=thumbs)
def get_file(id, check_author=True): file = get_db().execute( 'SELECT f.id, path, created, user_id, username' ' FROM file f JOIN user u ON f.user_id = u.id' ' WHERE f.id = ?', (id, )).fetchone() if file is None: abort(404, "Post id {0} doesn't exist.".format(id)) if check_author and file['user_id'] != g.user['id']: abort(403) return file
def login(): error = None if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_db() user = db.execute('SELECT * FROM user WHERE username = ?', (username, )).fetchone() if user is None: error = 'Incorrect username.' elif not check_password_hash(user['password'], password): error = 'Incorrect password.' if error is None: session.clear() session['user_id'] = user['id'] return redirect(url_for('file.index')) return render_template('auth/login.html', error=error)
def make_thumbnail(user_id, file_id, file_path): tpg = thumbnail_path_gen(user_id, file_id, file_path) npa = load_numpy_array(file_path) boxes = face_recognition.face_locations(npa) for i in range(len(boxes)): cv2.imwrite(tpg.get_index_path(i), get_face_array(npa[:, :, ::-1], boxes[i])) db = get_db() for i in range(len(boxes)): (top, right, bottom, left) = boxes[i] db.execute( 'INSERT INTO' ' face (user_id, file_id, face_index, top, right, bottom, left, origin_path, thumb_path)' ' VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', (user_id, file_id, i, top, right, bottom, left, file_path, tpg.get_index_path(i)) ) db.commit()
def bulk_thumbnail_query(db=None): files = file.get_files_in_level(0) if not db: db = get_db() for f in files: tpg = thumbnail_path_gen(g.user['id'], f['id'], f['path']) npa = load_numpy_array(f['path']) boxes = face_recognition.face_locations(npa) for i in range(len(boxes)): (top, right, bottom, left) = boxes[i] db.execute( 'INSERT INTO' ' face (user_id, file_id, face_index, top, right, bottom, left, origin_path, thumb_path)' ' VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', (g.user['id'], f['id'], i, top, right, bottom, left, f['path'], tpg.get_index_path(i)) ) db.commit()
def update(file_id): file = get_file(file_id) if request.method == 'POST': path = request.form['path'] error = None if not path: error = 'Title is required.' if error is not None: flash(error) else: db = get_db() db.execute('UPDATE file SET path = ?' ' WHERE id = ?', (path, file_id)) db.commit() return redirect(url_for('file.index')) return render_template('file/update.html', file=file)
def register(): error = None if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_db() if not username: error = 'Username is required.' elif not password: error = 'Password is required.' elif db.execute('SELECT id from user WHERE username = ?', (username, )).fetchone() is not None: error = 'user {} is already registered.'.format(username) if error is None: db.execute('INSERT INTO user (username, password) VALUES (?, ?)', (username, generate_password_hash(password))) db.commit() return redirect(url_for('file.index')) return render_template('auth/register.html', error=error)
def bulk_thumbnail(db=None): files = file.get_files_in_level(0) if not db: db = get_db() fcount = 0 last_commit_time = datetime.datetime.now() for f in files: tpg = thumbnail_path_gen(g.user['id'], f['id'], f['path']) npa = load_numpy_array(f['path']) boxes = face_recognition.face_locations(npa) for i in range(len(boxes)): cv2.imwrite(tpg.get_index_path(i), get_face_array(npa[:, :, ::-1], boxes[i])) (top, right, bottom, left) = boxes[i] db.execute( 'INSERT INTO' ' face (user_id, file_id, face_index, top, right, bottom, left, origin_path, thumb_path)' ' VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', (g.user['id'], f['id'], i, top, right, bottom, left, f['path'], tpg.get_index_path(i)) ) check_level = 1 if len(boxes) > 0: check_level = 2 db.execute( 'UPDATE file SET check_level = ?' ' WHERE id = ?', (check_level, f['id']) ) if (datetime.datetime.now() - last_commit_time).total_seconds() > 10: last_commit_time = datetime.datetime.now() db.commit() fcount += 1 db.commit()
def get_total_count(): db = get_db() count = db.execute('SELECT count(*) FROM file f JOIN user u' ' ON f.user_id = u.id and u.id = ' + str(g.user['id'])).fetchone() return int(count[0] / 50) # page count
def delete(file_id): get_file(file_id) db = get_db() db.execute('DELETE FROM file WHERE id = ?', (file_id, )) db.commit() return redirect(url_for('file.index'))
def get_files_in_level(level): db = get_db() return db.execute( 'SELECT f.id, f.path FROM file f JOIN user u ON f.user_id = u.id and u.id = ' + str(g.user['id']) + ' WHERE check_level = ' + str(level)).fetchall()