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> <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)
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> <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)