Skip to content

Commit 12619d0

Browse files
author
Jon Wayne Parrott
authored
Fix flex and vision codelab, add basic sanity test (#1277)
Change-Id: I4485572163c5a9643ddd199cfbede266fe5eb7f5
1 parent f59dffd commit 12619d0

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

codelabs/flex_and_vision/main.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,24 @@ def upload_photo():
6363
blob.make_public()
6464

6565
# Create a Cloud Vision client.
66-
vision_client = vision.Client()
66+
vision_client = vision.ImageAnnotatorClient()
6767

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

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

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

codelabs/flex_and_vision/main_test.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2017 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pytest
16+
import requests
17+
import six
18+
19+
import main
20+
21+
TEST_PHOTO_URL = (
22+
'https://upload.wikimedia.org/wikipedia/commons/5/5e/'
23+
'John_F._Kennedy%2C_White_House_photo_portrait%2C_looking_up.jpg')
24+
25+
26+
@pytest.fixture
27+
def app():
28+
main.app.testing = True
29+
client = main.app.test_client()
30+
return client
31+
32+
33+
def test_index(app):
34+
r = app.get('/')
35+
assert r.status_code == 200
36+
37+
38+
def test_upload_photo(app):
39+
test_photo_data = requests.get(TEST_PHOTO_URL).content
40+
41+
r = app.post(
42+
'/upload_photo',
43+
data={
44+
'file': (six.BytesIO(test_photo_data), 'flex_and_vision.jpg')
45+
}
46+
)
47+
48+
assert r.status_code == 302

0 commit comments

Comments
 (0)