Пример #1
0
def upload_file():
    if "logged_in" in session and session['logged_in']:
        if request.method == 'POST':
            failed = None
            no_file = None
            list1 = []
            file = request.files['file']

            if file and allowed_file(file.filename):
                a = database.get_last_id()
                b = file.filename
                if a == None:
                    f = str(1) + "." + b.rsplit('.', 1)[1].lower()
                else:
                    f = str(
                        (int(a.id) + 1)) + "." + b.rsplit('.', 1)[1].lower()
                target = os.path.join(app.config['UPLOAD_FOLDER'], f)
                file.save(target)
                response = model.predict_by_filename(
                    '/Users/DakMac/Desktop/jennyz/MEET/Whats on the meet laptop/labs/y2l-individual-project/static/user_images/'
                    + f)
                frames = response["outputs"]
                if a == None:
                    for frame in frames:
                        for concept in frame["data"]["concepts"]:
                            list1.append(concept["name"])
                    for i in range(10):
                        database.add_keyword(list1[i], 1)
                    keywords1 = request.form["keywords1"]
                    if keywords1 == '':
                        d = None
                    else:
                        database.add_keyword(keywords1, 1)
                    keywords2 = request.form["keywords2"]
                    if keywords2 == "":
                        b = None
                    else:
                        database.add_keyword(keywords2, 1)
                    keywords3 = request.form["keywords3"]
                    if keywords3 == '':
                        c = None
                    else:
                        database.add_keyword(keywords3, 1)
                else:
                    for frame in frames:
                        for concept in frame["data"]["concepts"]:
                            list1.append(concept["name"])
                    for i in range(10):
                        database.add_keyword(list1[i], a.id + 1)

                    keywords1 = request.form["keywords1"]
                    if keywords1 == '':
                        d = None
                    else:
                        database.add_keyword(keywords1, a.id + 1)
                    keywords2 = request.form["keywords2"]
                    if keywords2 == "":
                        b = None
                    else:
                        database.add_keyword(keywords2, a.id + 1)
                    keywords3 = request.form["keywords3"]
                    if keywords3 == '':
                        c = None
                    else:
                        database.add_keyword(keywords3, a.id + 1)
                c = database.get_user_by_id(session['id'])
                y = c.username
                price = request.form["price"]
                currency = request.form["currency"]
                percentage = request.form["percentage"]
                database.add_photo(f, session['id'], y, price, currency,
                                   percentage)
                return redirect(url_for('home_page'))
            if file and not allowed_file(file.filename):
                failed = True
                return render_template("upload.html", failed=failed)

        return render_template("upload.html")
    else:
        return redirect(url_for("home_page"))
Пример #2
0
def myphotos():
    "login required my photos route"
    all_labels = ["No labels yet"]

    #####
    # rds exercise get list of images from database
    # now we have a user id from cognito
    #####
    s3_client = boto3.client('s3')
    photos = database.list_photos(flask_login.current_user.id)
    for photo in photos:
        photo["signed_url"] = s3_client.generate_presigned_url(
            'get_object',
            Params={
                'Bucket': config.PHOTOS_BUCKET,
                'Key': photo["object_key"]
            })

    form = PhotoForm()
    url = None
    if form.validate_on_submit():
        image_bytes = util.resize_image(form.photo.data, (300, 300))
        if image_bytes:
            #######
            # s3 excercise - save the file to a bucket
            #######
            prefix = "photos/"
            key = prefix + util.random_hex_bytes(8) + '.png'
            s3_client.put_object(Bucket=config.PHOTOS_BUCKET,
                                 Key=key,
                                 Body=image_bytes,
                                 ContentType='image/png')
            # http://boto3.readthedocs.io/en/latest/guide/s3.html#generating-presigned-urls
            url = s3_client.generate_presigned_url('get_object',
                                                   Params={
                                                       'Bucket':
                                                       config.PHOTOS_BUCKET,
                                                       'Key': key
                                                   })

            #######
            # rekcognition exercise
            #######
            rek = boto3.client('rekognition')
            response = rek.detect_labels(Image={
                'S3Object': {
                    'Bucket': config.PHOTOS_BUCKET,
                    'Name': key
                }
            })
            all_labels = [label['Name'] for label in response['Labels']]

            #######
            # rds excercise
            # added user id and description to the database
            #######
            labels_comma_separated = ", ".join(all_labels)
            database.add_photo(key, labels_comma_separated,
                               form.description.data,
                               flask_login.current_user.id)
            form.description.data = ''

    return render_template_string("""
            {% extends "main.html" %}
            {% block content %}
            <h4>Upload Photo</h4>
            <form method="POST" enctype="multipart/form-data" action="{{ url_for('myphotos') }}">
                {{ form.csrf_token }}
                  <div class="control-group">
                   <label class="control-label">Photo</label>
                    {{ form.photo() }}
                  </div>
                  <div class="control-group">
                    <label class="control-label">Description</label>
                    <div class="controls">
                    {{ form.description(class="form-control") }}
                    </div>
                  </div>
                    &nbsp;
                   <div class="control-group">
                    <div class="controls">
                        <input class="btn btn-primary" type="submit" value="Upload">
                    </div>
                  </div>
            </form>

            {% if url %}
            <hr/>
            <h3>Uploaded!</h3>
            <img src="{{url}}" /><br/>
            {% for label in all_labels %}
            <span class="label label-info">{{label}}</span>
            {% endfor %}
            {% endif %}
            
            {% if photos %}
            <hr/>
            <h4>Photos</h4>
            {% for photo in photos %}
                <table class="table table-bordered">
                <tr> <td rowspan="4" class="col-md-2 text-center"><img width="150" src="{{photo.signed_url}}" />
                    <a href="{{ url_for('myphotos_delete', object_key=photo.object_key) }}"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span> delete</a>
                </td></tr>
                <tr> <th scope="row" class="col-md-2">Description</th> <td>{{photo.description}}</td> </tr>
                <tr> <th scope="row" class="col-md-2">Labels</th> <td>{{photo.labels}}</td> </tr>
                <tr> <th scope="row" class="col-md-2">Created</th> <td>{{photo.created_datetime}} UTC</td> </tr>
                </table>

            {% endfor %}
            {% endif %}


            {% endblock %}
                """,
                                  form=form,
                                  url=url,
                                  photos=photos,
                                  all_labels=all_labels)
Пример #3
0
def home():
    """Homepage route"""
    all_labels = ["No labels yet"]

    s3_client = boto3.client('s3')
    photos = database.list_photos()
    for photo in photos:
        photo["signed_url"] = s3_client.generate_presigned_url(
            'get_object',
            Params={
                'Bucket': config.PHOTOS_BUCKET,
                'Key': photo["object_key"]
            })

    form = PhotoForm()
    url = None
    if form.validate_on_submit():
        image_bytes = util.resize_image(form.photo.data, (300, 300))
        if image_bytes:
            #######
            # s3
            #######
            prefix = "photos/"
            key = prefix + util.random_hex_bytes(8) + '.png'
            s3_client.put_object(Bucket=config.PHOTOS_BUCKET,
                                 Key=key,
                                 Body=image_bytes,
                                 ContentType='image/png')

            url = s3_client.generate_presigned_url('get_object',
                                                   Params={
                                                       'Bucket':
                                                       config.PHOTOS_BUCKET,
                                                       'Key': key
                                                   })

            #######
            # rekcognition
            #######
            rek = boto3.client('rekognition')
            response = rek.detect_labels(Image={
                'S3Object': {
                    'Bucket': config.PHOTOS_BUCKET,
                    'Name': key
                }
            })
            all_labels = [label['Name'] for label in response['Labels']]

            labels_comma_separated = ", ".join(all_labels)
            database.add_photo(key, labels_comma_separated)

    return render_template_string("""
            {% extends "main.html" %}
            {% block content %}
            <h4>Upload Photo</h4>
            <form method="POST" enctype="multipart/form-data" action="{{ url_for('home') }}">
                {{ form.csrf_token }}
                  <div class="control-group">
                   <label class="control-label">Photo</label>
                    {{ form.photo() }}
                  </div>

                    &nbsp;
                   <div class="control-group">
                    <div class="controls">
                        <input class="btn btn-primary" type="submit" value="Upload">
                    </div>
                  </div>
            </form>

            {% if url %}
            <hr/>
            <h3>Uploaded!</h3>
            <img src="{{url}}" /><br/>
            {% for label in all_labels %}
            <span class="label label-info">{{label}}</span>
            {% endfor %}
            {% endif %}
            
            {% if photos %}
            <hr/>
            <h4>Photos</h4>
            {% for photo in photos %}
                <table class="table table-bordered">
                <tr> <td rowspan="4" class="col-md-2 text-center"><img width="150" src="{{photo.signed_url}}" /> </td></tr>
                <tr> <th scope="row" class="col-md-2">Labels</th> <td>{{photo.labels}}</td> </tr>
                <tr> <th scope="row" class="col-md-2">Created</th> <td>{{photo.created_datetime}} UTC</td> </tr>
                </table>

            {% endfor %}
            {% endif %}


            {% endblock %}
                """,
                                  form=form,
                                  url=url,
                                  photos=photos,
                                  all_labels=all_labels)