Skip to content

Commit 6a5fc80

Browse files
authored
chore(deps): update dependency google-cloud-vision to v2 (GoogleCloudPlatform#4775)
This PR contains the following updates: | Package | Update | Change | |---|---|---| | [google-cloud-vision](https://togithub.com/googleapis/python-vision) | major | `==1.0.0` -> `==2.0.0` | --- ### Release Notes
googleapis/python-vision ### [`v2.0.0`](https://togithub.com/googleapis/python-vision/blob/master/CHANGELOG.md#​200-httpswwwgithubcomgoogleapispython-visioncomparev100v200-2020-09-29) [Compare Source](https://togithub.com/googleapis/python-vision/compare/v1.0.0...v2.0.0) ##### ⚠ BREAKING CHANGES - migrate to use microgen ([#&GoogleCloudPlatform#8203;52](https://togithub.com/googleapis/python-vision/issues/52)) ##### Features - migrate to use microgen ([#&GoogleCloudPlatform#8203;52](https://www.github.com/googleapis/python-vision/issues/52)) ([cf3d353](https://www.github.com/googleapis/python-vision/commit/cf3d35306c3a8f6d32cc7ce1eb436c965acc30fe)) ##### Bug Fixes - update retry configs ([#&GoogleCloudPlatform#8203;29](https://www.github.com/googleapis/python-vision/issues/29)) ([39c1652](https://www.github.com/googleapis/python-vision/commit/39c16522f7bc97544c361f8e14dbc9a2a5d4c0e4)) ##### Documentation - added note about not supported device ([#&GoogleCloudPlatform#8203;24](https://www.github.com/googleapis/python-vision/issues/24)) ([b33fa88](https://www.github.com/googleapis/python-vision/commit/b33fa88e4f1e9cb2f6e029e6a34364fb6cdc1a96))
--- ### Renovate configuration :date: **Schedule**: At any time (no schedule defined). :vertical_traffic_light: **Automerge**: Disabled by config. Please merge this manually once you are satisfied. :recycle: **Rebasing**: Renovate will not automatically rebase this PR, because other commits have been found. :no_bell: **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] If you want to rebase/retry this PR, check this box --- This PR has been generated by [WhiteSource Renovate](https://renovate.whitesourcesoftware.com). View repository job log [here](https://app.renovatebot.com/dashboard#github/GoogleCloudPlatform/python-docs-samples).
1 parent 107bf7e commit 6a5fc80

File tree

8 files changed

+143
-113
lines changed

8 files changed

+143
-113
lines changed

codelabs/flex_and_vision/main.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,29 @@
2323
from google.cloud import vision
2424

2525

26-
CLOUD_STORAGE_BUCKET = os.environ.get('CLOUD_STORAGE_BUCKET')
26+
CLOUD_STORAGE_BUCKET = os.environ.get("CLOUD_STORAGE_BUCKET")
2727

2828

2929
app = Flask(__name__)
3030

3131

32-
@app.route('/')
32+
@app.route("/")
3333
def homepage():
3434
# Create a Cloud Datastore client.
3535
datastore_client = datastore.Client()
3636

3737
# Use the Cloud Datastore client to fetch information from Datastore about
3838
# each photo.
39-
query = datastore_client.query(kind='Faces')
39+
query = datastore_client.query(kind="Faces")
4040
image_entities = list(query.fetch())
4141

4242
# Return a Jinja2 HTML template and pass in image_entities as a parameter.
43-
return render_template('homepage.html', image_entities=image_entities)
43+
return render_template("homepage.html", image_entities=image_entities)
4444

4545

46-
@app.route('/upload_photo', methods=['GET', 'POST'])
46+
@app.route("/upload_photo", methods=["GET", "POST"])
4747
def upload_photo():
48-
photo = request.files['file']
48+
photo = request.files["file"]
4949

5050
# Create a Cloud Storage client.
5151
storage_client = storage.Client()
@@ -55,8 +55,7 @@ def upload_photo():
5555

5656
# Create a new blob and upload the file's content.
5757
blob = bucket.blob(photo.filename)
58-
blob.upload_from_string(
59-
photo.read(), content_type=photo.content_type)
58+
blob.upload_from_string(photo.read(), content_type=photo.content_type)
6059

6160
# Make the blob publicly viewable.
6261
blob.make_public()
@@ -65,10 +64,9 @@ def upload_photo():
6564
vision_client = vision.ImageAnnotatorClient()
6665

6766
# Use the Cloud Vision client to detect a face for our image.
68-
source_uri = 'gs://{}/{}'.format(CLOUD_STORAGE_BUCKET, blob.name)
69-
image = vision.types.Image(
70-
source=vision.types.ImageSource(gcs_image_uri=source_uri))
71-
faces = vision_client.face_detection(image).face_annotations
67+
source_uri = "gs://{}/{}".format(CLOUD_STORAGE_BUCKET, blob.name)
68+
image = vision.Image(source=vision.ImageSource(gcs_image_uri=source_uri))
69+
faces = vision_client.face_detection(image=image).face_annotations
7270

7371
# If a face is detected, save to Datastore the likelihood that the face
7472
# displays 'joy,' as determined by Google's Machine Learning algorithm.
@@ -77,11 +75,16 @@ def upload_photo():
7775

7876
# Convert the likelihood string.
7977
likelihoods = [
80-
'Unknown', 'Very Unlikely', 'Unlikely', 'Possible', 'Likely',
81-
'Very Likely']
78+
"Unknown",
79+
"Very Unlikely",
80+
"Unlikely",
81+
"Possible",
82+
"Likely",
83+
"Very Likely",
84+
]
8285
face_joy = likelihoods[face.joy_likelihood]
8386
else:
84-
face_joy = 'Unknown'
87+
face_joy = "Unknown"
8588

8689
# Create a Cloud Datastore client.
8790
datastore_client = datastore.Client()
@@ -90,7 +93,7 @@ def upload_photo():
9093
current_datetime = datetime.now()
9194

9295
# The kind for the new entity.
93-
kind = 'Faces'
96+
kind = "Faces"
9497

9598
# The name/ID for the new entity.
9699
name = blob.name
@@ -101,28 +104,33 @@ def upload_photo():
101104
# Construct the new entity using the key. Set dictionary values for entity
102105
# keys blob_name, storage_public_url, timestamp, and joy.
103106
entity = datastore.Entity(key)
104-
entity['blob_name'] = blob.name
105-
entity['image_public_url'] = blob.public_url
106-
entity['timestamp'] = current_datetime
107-
entity['joy'] = face_joy
107+
entity["blob_name"] = blob.name
108+
entity["image_public_url"] = blob.public_url
109+
entity["timestamp"] = current_datetime
110+
entity["joy"] = face_joy
108111

109112
# Save the new entity to Datastore.
110113
datastore_client.put(entity)
111114

112115
# Redirect to the home page.
113-
return redirect('/')
116+
return redirect("/")
114117

115118

116119
@app.errorhandler(500)
117120
def server_error(e):
118-
logging.exception('An error occurred during a request.')
119-
return """
121+
logging.exception("An error occurred during a request.")
122+
return (
123+
"""
120124
An internal error occurred:
{}
121125
See logs for full stacktrace.
122-
""".format(e), 500
126+
""".format(
127+
e
128+
),
129+
500,
130+
)
123131

124132

125-
if __name__ == '__main__':
133+
if __name__ == "__main__":
126134
# This is used when running locally. Gunicorn is used to run the
127135
# application on Google App Engine. See entrypoint in app.yaml.
128-
app.run(host='127.0.0.1', port=8080, debug=True)
136+
app.run(host="127.0.0.1", port=8080, debug=True)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Flask==1.1.2
22
gunicorn==20.0.4; python_version > '3.0'
33
gunicorn==19.10.0; python_version < '3.0'
4-
google-cloud-vision==1.0.0
4+
google-cloud-vision==2.0.0
55
google-cloud-storage==1.31.2
66
google-cloud-datastore==1.15.1

functions/imagemagick/main.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,31 @@
3030
def blur_offensive_images(data, context):
3131
file_data = data
3232

33-
file_name = file_data['name']
34-
bucket_name = file_data['bucket']
33+
file_name = file_data["name"]
34+
bucket_name = file_data["bucket"]
3535

3636
blob = storage_client.bucket(bucket_name).get_blob(file_name)
37-
blob_uri = f'gs://{bucket_name}/{file_name}'
38-
blob_source = {'source': {'image_uri': blob_uri}}
37+
blob_uri = f"gs://{bucket_name}/{file_name}"
38+
blob_source = vision.Image(source=vision.ImageSource(gcs_image_uri=blob_uri))
3939

4040
# Ignore already-blurred files
41-
if file_name.startswith('blurred-'):
42-
print(f'The image {file_name} is already blurred.')
41+
if file_name.startswith("blurred-"):
42+
print(f"The image {file_name} is already blurred.")
4343
return
4444

45-
print(f'Analyzing {file_name}.')
45+
print(f"Analyzing {file_name}.")
4646

47-
result = vision_client.safe_search_detection(blob_source)
47+
result = vision_client.safe_search_detection(image=blob_source)
4848
detected = result.safe_search_annotation
4949

5050
# Process image
5151
if detected.adult == 5 or detected.violence == 5:
52-
print(f'The image {file_name} was detected as inappropriate.')
52+
print(f"The image {file_name} was detected as inappropriate.")
5353
return __blur_image(blob)
5454
else:
55-
print(f'The image {file_name} was detected as OK.')
55+
print(f"The image {file_name} was detected as OK.")
56+
57+
5658
# [END functions_imagemagick_analyze]
5759

5860

@@ -64,24 +66,26 @@ def __blur_image(current_blob):
6466

6567
# Download file from bucket.
6668
current_blob.download_to_filename(temp_local_filename)
67-
print(f'Image {file_name} was downloaded to {temp_local_filename}.')
69+
print(f"Image {file_name} was downloaded to {temp_local_filename}.")
6870

6971
# Blur the image using ImageMagick.
7072
with Image(filename=temp_local_filename) as image:
71-
image.resize(*image.size, blur=16, filter='hamming')
73+
image.resize(*image.size, blur=16, filter="hamming")
7274
image.save(filename=temp_local_filename)
7375

74-
print(f'Image {file_name} was blurred.')
76+
print(f"Image {file_name} was blurred.")
7577

7678
# Upload result to a second bucket, to avoid re-triggering the function.
7779
# You could instead re-upload it to the same bucket + tell your function
7880
# to ignore files marked as blurred (e.g. those with a "blurred" prefix)
79-
blur_bucket_name = os.getenv('BLURRED_BUCKET_NAME')
81+
blur_bucket_name = os.getenv("BLURRED_BUCKET_NAME")
8082
blur_bucket = storage_client.bucket(blur_bucket_name)
8183
new_blob = blur_bucket.blob(file_name)
8284
new_blob.upload_from_filename(temp_local_filename)
83-
print(f'Blurred image uploaded to: gs://{blur_bucket_name}/{file_name}')
85+
print(f"Blurred image uploaded to: gs://{blur_bucket_name}/{file_name}")
8486

8587
# Delete the temporary file.
8688
os.remove(temp_local_filename)
89+
90+
8791
# [END functions_imagemagick_blur]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
google-cloud-vision==1.0.0
1+
google-cloud-vision==2.0.0
22
google-cloud-storage==1.31.2
33
Wand==0.6.3

0 commit comments

Comments
 (0)