Skip to content

Fix flex and vision codelab, add basic sanity test #1277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions codelabs/flex_and_vision/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,24 @@ def upload_photo():
blob.make_public()

# Create a Cloud Vision client.
vision_client = vision.Client()
vision_client = vision.ImageAnnotatorClient()

# Use the Cloud Vision client to detect a face for our image.
source_uri = 'gs://{}/{}'.format(CLOUD_STORAGE_BUCKET, blob.name)
image = vision_client.image(source_uri=source_uri)
faces = image.detect_faces(limit=1)
image = vision.types.Image(
source=vision.types.ImageSource(gcs_image_uri=source_uri))
faces = vision_client.face_detection(image).face_annotations

# If a face is detected, save to Datastore the likelihood that the face
# displays 'joy,' as determined by Google's Machine Learning algorithm.
if len(faces) > 0:
face = faces[0]

# Convert the face.emotions.joy enum type to a string, which will be
# something like 'Likelihood.VERY_LIKELY'. Parse that string by the
# period to extract only the 'VERY_LIKELY' portion.
face_joy = str(face.emotions.joy).split('.')[1]
# Convert the likelihood string.
likelihoods = [
'Unknown', 'Very Unlikely', 'Unlikely', 'Possible', 'Likely',
'Very Likely']
face_joy = likelihoods[face.joy_likelihood]
else:
face_joy = 'Unknown'

Expand Down
48 changes: 48 additions & 0 deletions codelabs/flex_and_vision/main_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright 2017 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest
import requests
import six

import main

TEST_PHOTO_URL = (
'https://upload.wikimedia.org/wikipedia/commons/5/5e/'
'John_F._Kennedy%2C_White_House_photo_portrait%2C_looking_up.jpg')


@pytest.fixture
def app():
main.app.testing = True
client = main.app.test_client()
return client


def test_index(app):
r = app.get('/')
assert r.status_code == 200


def test_upload_photo(app):
test_photo_data = requests.get(TEST_PHOTO_URL).content

r = app.post(
'/upload_photo',
data={
'file': (six.BytesIO(test_photo_data), 'flex_and_vision.jpg')
}
)

assert r.status_code == 302