예제 #1
0
def upload():
    username = session.get('username')
    if username:
        user_id = model_session.query(model.User).filter_by(username=username).first().id
    else:
        user_id = 0

    f_keys = request.form.keys()
    pattern = re.compile(r'^data:image/(png|jpeg|jpg);base64,(.*)$')
    raw_data = []
    for key in f_keys:
        if key != 'cloud_name' and key != 'choice':
            match = pattern.match(request.form[key])
            if match is None:
                raise ValueError('Invalid image data.')

            content_type = 'image/{}'.format(match.group(1))
            file_data = base64.b64decode(match.group(2))
            raw_data.append(file_data)

    user_path = 'static/uploads/%d' % user_id
    if not os.path.exists(user_path):
        # user doesn't have his/her own directory yet, so create one
        os.mkdir(user_path)

    cloud_name = request.form['cloud_name']
    algorithm = request.form['choice']

    # save cloud to database
    new_cloud = model.Cloud(user_id=user_id, name=cloud_name)
    model_session.add(new_cloud)
    model_session.commit()
    model_session.refresh(new_cloud)

    cloud_id = model_session.query(model.Cloud.id).order_by(desc(model.Cloud.id)).first()[0]

    # create a new directory inside the user's directory for uploaded photos
    path = user_path + '/' + str(cloud_id)
    if not os.path.exists(path):
        os.mkdir(path)

    for idx, d in enumerate(raw_data):
        filename = '{}.{}'.format(len(raw_data)-idx, match.group(1))
        with open(path + '/' + filename, 'w') as f:
            f.write(d)

    path = 'static/uploads/%d/%s' % (user_id, cloud_id)

    images = sorted(path + '/' + img for img in os.listdir(path) if img.rpartition('.')[2].lower() in ('jpg', 'jpeg', 'png'))
    points_path = os.path.abspath(os.path.join(path, "points"))
    reconstruct.start(algorithm, images, file_path=points_path)

    if algorithm == 'features':
        points = str(reconstruct.extract_points(points_path + "_inliers.txt"))
    elif algorithm == 'flow':
        points = str(reconstruct.extract_points(points_path + ".txt"))

    # set the path to the text file storing the 3D points of the cloud
    cloud = model_session.query(model.Cloud).filter_by(id=cloud_id).first()
    cloud.path = path
    model_session.commit()

    # save photos to database
    photos = [ img for img in os.listdir(path) if img.rpartition('.')[2].lower() in ('jpg', 'jpeg', 'png') ]

    for photo in photos:
        new_photo = model.Photo(filename=photo, path=path, cloud_id=cloud_id)
        model_session.add(new_photo)
        model_session.commit()
        model_session.refresh(new_photo)

    cloud_data = {'cloud_id': cloud_id, 'points': points}

    return jsonify(cloud_data)
예제 #2
0
def get_cloud(id):
    '''Gets the 3D points for a past point cloud.'''
    path = model_session.query(model.Cloud).filter_by(id=id).first().path
    points = str(reconstruct.extract_points(path + '/points.txt'))
    return points