コード例 #1
0
def login():
    """Login route"""
    # http://docs.aws.amazon.com/cognito/latest/developerguide/login-endpoint.html
    session['csrf_state'] = util.random_hex_bytes(8)
    cognito_login = ("https://%s/"
                     "login?response_type=code&client_id=%s"
                     "&state=%s"
                     "&redirect_uri=%s/callback" %
                     (config.COGNITO_DOMAIN, config.COGNITO_CLIENT_ID,
                      session['csrf_state'], config.BASE_URL))
    return redirect(cognito_login)
コード例 #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"]
    faces = ["No faces yet"]

    #####
    # s3 getting a list of photos in the bucket
    #####
    s3_client = boto3.client('s3')
    prefix = "photos/"
    response = s3_client.list_objects(Bucket=config.PHOTOS_BUCKET,
                                      Prefix=prefix)
    photos = []
    if 'Contents' in response and response['Contents']:
        photos = [
            s3_client.generate_presigned_url('get_object',
                                             Params={
                                                 'Bucket':
                                                 config.PHOTOS_BUCKET,
                                                 'Key': content['Key']
                                             })
            for content in response['Contents']
        ]

    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
            #######
            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_confidences = [
                label['Confidence'] for label in response['Labels']
            ]
            all_labels = [label['Name'] for label in response['Labels']]
            for i in range(0, len(all_labels)):
                all_labels[i] = all_labels[i] + ": " + str(
                    all_confidences[i]) + "%"

    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 %}
                <img width="150" src="{{photo}}" />
            {% endfor %}
            {% endif %}

            {% endblock %}
                """,
                                  form=form,
                                  url=url,
                                  photos=photos,
                                  all_labels=all_labels)
コード例 #4
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)
コード例 #5
0
def home():
    """Homepage route"""

    #####
    # s3 getting a list of templates in the bucket
    #####
    s3_client = boto3.client('s3',
                             aws_access_key_id=config.AWS_ID,
                             aws_secret_access_key=config.AWS_SECRET,
                             region_name='us-west-2')

    templates = getTemplates(s3_client)

    comprehend = boto3.client('comprehend',
                              aws_access_key_id=config.AWS_ID,
                              aws_secret_access_key=config.AWS_SECRET,
                              region_name='us-west-2')
    form = UploadForm()
    document = None
    if form.validate_on_submit():
        document = Document(form.file.data)
        if document:
            for para in document.paragraphs:
                #######
                # s3 excercise - save the file to a bucket
                #######
                name = util.random_hex_bytes(8)

                if len(para.text) > 15:
                    s3_client.put_object(
                        Bucket=config.TEMPLATES_BUCKET,
                        Key="paragraphs/" + name + '.txt',
                        Body=para.text,
                        ContentType=
                        'text/plain'  #'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
                    )

                    text = para.text

                    result = json.dumps(comprehend.detect_key_phrases(
                        Text=text, LanguageCode='en'),
                                        sort_keys=True,
                                        indent=4)

                    s3_client.put_object(Bucket=config.TEMPLATES_BUCKET,
                                         Key="labels/" + name + ".json",
                                         Body=result,
                                         ContentType='application/json')

                    sentiment = json.dumps(comprehend.detect_sentiment(
                        Text=text, LanguageCode='en'),
                                           sort_keys=True,
                                           indent=4)

                    s3_client.put_object(Bucket=config.TEMPLATES_BUCKET,
                                         Key="sentiment/" + name +
                                         ".sentiment.json",
                                         Body=sentiment,
                                         ContentType='application/json')
            templates = getTemplates(s3_client)

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

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

            <hr/>
            {% if document %}
            <h3>Uploaded!</h3>
            {% endif %}
            
            {% if templates %}
            <hr/>
            <h4>Templates</h4><hr/>
            {% for template in templates %}
                <span class="label label-default">Neutral: {{"%.2f" % template['sentiments']['Neutral']}}</span>
                <span class="label label-success">Positive: {{"%.2f" % template['sentiments']['Positive']}}</span>
                <span class="label label-warning">Mixed: {{"%.2f" % template['sentiments']['Mixed']}}</span>
                <span class="label label-danger">Negative: {{"%.2f" % template['sentiments']['Negative']}}</span>
                <br/>
                {% for label in template['labels'] %}
                    <span class="label label-info">{{label}}</span>
                {% endfor %}
                <br/>
                <span>{{template['text']}}</span><br/><hr/>
            {% endfor %}
            {% endif %}

            {% endblock %}
                """,
                                  form=form,
                                  document=document,
                                  templates=templates)
コード例 #6
0
ファイル: app.py プロジェクト: will7007/ccna-2021-face-swap
import os

# k8s addresses
NATS_ADDRESS = "nats-service:4222"  # "127.0.0.1:4222" or "nats-service:4222"
MINIO_ADDRESS = "minio-service:9000"  # "minio-service:9000" or "localhost:9000"
# local addresses
# NATS_ADDRESS = "127.0.0.1:4222"
# MINIO_ADDRESS = "localhost:9000"

# NEO4J_ADDRESS = "bolt://192.168.1.71:31620"
# MINIO_ADDRESS = "192.168.1.71:31199"
# NATS_ADDRESS = "nats://192.168.1.71:31348"

# Flask setup
application = Flask(__name__)
application.secret_key = util.random_hex_bytes(5)
application.config["UPLOAD_FOLDER"] = "."
bootstrap = Bootstrap(application)
application.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0


# Flask forms
class PhotoForm(FlaskForm):
    """flask_wtf form class the file upload"""
    photo = FileField('image', validators=[FileRequired()])


class NameForm(FlaskForm):
    name = StringField("Face name", validators=[DataRequired()])

コード例 #7
0
ファイル: application.py プロジェクト: Hohol/aws-test
def home():
    """Homepage route"""
    all_labels = ["No labels yet"]

    #####
    # s3 getting a list of photos in the bucket
    #####
    s3_client = boto3.client('s3',
                             config=boto3.session.Config(
                                 s3={'addressing_style': 'path'},
                                 signature_version='s3v4'))
    prefix = "photos/"
    response = s3_client.list_objects(Bucket=config.PHOTOS_BUCKET,
                                      Prefix=prefix)
    print(response)
    photos = []
    if 'Contents' in response and response['Contents']:
        photos = [
            s3_client.generate_presigned_url('get_object',
                                             Params={
                                                 'Bucket':
                                                 config.PHOTOS_BUCKET,
                                                 'Key': content['Key']
                                             })
            for content in sorted(response['Contents'],
                                  key=lambda d: d['LastModified'])
        ]

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

    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 %}
                <img width="150" src="{{photo}}" />
            {% endfor %}
            {% endif %}

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