Analyze Images with the Cloud Vision API

๋ชฉ๋ก
- APIs Explorer: Qwik Start
- Extract, Analyze, and Translate Text from Images with the Cloud ML APIs
- Detect Labels, Faces, and Landmarks in images with the Cloud Vision API โฌ ๏ธ ์ค๋ ํ์ตํ Lab!
- Analyze Images with the Cloud Vision API: Challenge Lab
- Claim credential (๋ชจ๋ ๊ฐ์๋ฅผ ์๋ฃํด์ผ Skill badge ์ทจ๋ ๊ฐ๋ฅ!โจ)
Cloud Vision API๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง์์ ๊ฐ์ฒด, ์ผ๊ตด, ๋๋๋งํฌ๋ฅผ ๊ฐ์งํด๋ณด๊ธฐ
- ๋ผ๋ฒจ ์ธ์ ์ด๋ฏธ์ง:
- ์ผ๊ตด ์ธ์ ์ด๋ฏธ์ง:
- ๋๋๋งํฌ ์ด๋ฏธ์ง:



โก๏ธ ๋ค๋น๊ฒ์ด์
๋ฉ๋ด > APIs & Services > Credentials > API Key

Select API restrictions ์ค ์ ํํ API ํญ๋ชฉ๋ค
- Cloud Vision API
- Cloud Translation API
- Cloud Natural Language API
โก๏ธ ์์ฑ ํ API Key ๊ฐ ๋ณต์ฌํด๋๊ธฐ

#๋ฑ๋ก
export API_KEY={API_KEY}
#ํ์ธ
echo $API_KEY
์ด๋ฏธ์ง ๋ฑ๋ก ๋ฐ ์ด๋ฏธ์ง ์ ๊ทผ ๊ถํ์ ์ด์ ํฌ์คํธ ์ฐธ๊ณ !

โก๏ธ Cloud Shell > Open Editor ๋ฒํผ ํด๋ฆญ

โก๏ธ request.json ํ์ผ ์์ฑ ํ ์์ ๋ด์ฉ์ ๋ค์๊ณผ ๊ฐ์ด ์์ฑ
โก๏ธ Editor ๋ด์์ ๋ณธ์ธ์ ํด๋์ ๋ง์ฐ์ค๋ฅผ ์ฌ๋ฆฌ๋ฉด ํ์ผ ์ถ๊ฐ ์์ด์ฝ์ด ๋ํ๋๋ค.

{
"requests": [
{
"image": {
"source": {
"gcsImageUri": "gs://PROJECT_ID-bucket/donuts.png" #๋ณธ์ธ์ Bucket์ ์
๋ก๋๋ ํ์ผ ์ฃผ์
}
},
"features": [
{
"type": "LABEL_DETECTION",
"maxResults": 10
}
]
}
]
}

# Request
gcloud services enable cloudaicompanion.googleapis.com
# Response
Operation "operations/acat.p2-xxxx-xxx" finished successfully.

โก๏ธ ํ๋ฉด ์ข์ธก ํ๋จ์ Cloud Code - ํ๋ก์ ํธ ์์ > Select a Google Cloud project > Project ID ์ ํํ์ฌ ํ๋ฌ๊ทธ์ธ์ ์น์ธ
โก๏ธ ์ํ ํ์์ค์ Cloud Code ์ํ ๋ฉ์์ง์ Google Cloud ํ๋ก์ ํธ(Project ID)๊ฐ ํ์๋๋ฉด ์๋ฃ!

curl -s -X POST -H "Content-Type: application/json" --data-binary @request.json https://vision.googleapis.com/v1/images:annotate?key=${API_KEY} -o label_detection.json && cat label_detection.json
โก๏ธ ํฐ๋ฏธ๋์์ curl์ ์ฌ์ฉํ์ฌ Cloud Vision API๋ฅผ ํธ์ถํ๊ณ label_detection.json ํ์ผ์ ์๋ต์ ์ ์ฅ
{
"responses": [
{
"labelAnnotations": [
{
"mid": "/m/01dk8s",
"description": "Powdered sugar",
"score": 0.9861496,
"topicality": 0.9861496
},
{
"mid": "/m/01wydv",
"description": "Beignet",
"score": 0.9565117,
"topicality": 0.9565117
},
{
"mid": "/m/02wbm",
"description": "Food",
"score": 0.9424965,
"topicality": 0.9424965
},
{
"mid": "/m/0hnyx",
"description": "Pastry",
"score": 0.8173416,
"topicality": 0.8173416
},
{
"mid": "/m/02q08p0",
"description": "Dish",
"score": 0.8076026,
"topicality": 0.8076026
},
{
"mid": "/m/01ykh",
"description": "Cuisine",
"score": 0.79036003,
"topicality": 0.79036003
},
{
"mid": "/m/03nsjgy",
"description": "Kourabiedes",
"score": 0.77726763,
"topicality": 0.77726763
},
{
"mid": "/m/06gd3r",
"description": "Angel wings",
"score": 0.73792106,
"topicality": 0.73792106
},
{
"mid": "/m/06x4c",
"description": "Sugar",
"score": 0.71921736,
"topicality": 0.71921736
},
{
"mid": "/m/01zl9v",
"description": "Zeppole",
"score": 0.7111677,
"topicality": 0.7111677
}
]
}
]
}
์๋ต ๊ฒฐ๊ณผ ๋ถ์
description: ํญ๋ชฉ์ ์ด๋ฆscore: ์ค๋ช ๊ณผ ์ด๋ฏธ์ง ๋ด์ฉ์ ์ผ์น์ ๋ํ ์ ๋ขฐ ์์ค์ 0~1 ์ฌ์ด์ ์ซ์๋ก ๋ํ๋mid: Google ์ง์ ๊ทธ๋ํ์์ ํด๋น ํญ๋ชฉ์ mid ๊ฐ์ ๋งคํ๋ ๊ฐ๐ก ์ข ํฉ์ ์ผ๋ก ์๋ต ๊ฒฐ๊ณผ๋ฅผ ํด์ํ์ ๋, API๊ฐ ์ด ๋๋์ด ์๊ฐ ํ์ฐ๋ ๋๋์ด๋ผ๋ ๊ตฌ์ฒด์ ์ธ ์ ํ์ ์๋ณ!


โก๏ธ Gemini Code Assist: ์ค๋งํธ ์์ ์์ด์ฝ ํด๋ฆญ > ํ๋กฌํํธ ๋ด์ฉ ์์ฑ
You are a Machine Learning Engineer at Cymbal AI. A new team member needs help understanding this Cloud Vision API response. Explain the label_detection.json file in detail. Break down its key components and their function within the JSON code.
For the suggested improvements, don't make any changes to the file's content.
โก๏ธ Gemini Code Assist: ์ค๋งํธ ์์
์์ด์ฝ ํด๋ฆญ > ํ๋กฌํํธ ๋ด์ฉ ์์ฑ

โก๏ธ label_detection.json ์ฝ๋์ Cloud Vision API ์๋ต์ ๋ํ ์์ธํ ์ค๋ช
์ด Gemini Code Assist ์ฑํ
์ ํ์๋๋ค!

โก๏ธ request.json ํ์ผ ์ ํ > Gemini Code Assist:์ค๋งํธ ์์
> ํ๋กฌํํธ ์
๋ ฅ ํ Enter > Gemini Diff ๋ทฐ์ ๋ฉ์์ง๊ฐ ํ์๋๋ฉด ๋ชจ๋ ๋ณ๊ฒฝ์ฌํญ ์ ์ฉ์ ํด๋ฆญ
In the request.json file, update the features list, change type from LABEL_DETECTION to WEB_DETECTION.

{
"requests": [
{
"image": {
"source": {
"gcsImageUri": "gs://PROJECT_ID-bucket/donuts.png"
}
},
"features": [
{
"type": "WEB_DETECTION",
"maxResults": 10
}
]
}
]
}
โก๏ธ request.json ํ์ผ ๋ด์ฉ์ด ํ๋กฌํํธ์ ์
๋ ฅํ ๋ด์ฉ ๋ฐํ์ผ๋ก ์์ ๋๋ค.

curl -s -X POST -H "Content-Type: application/json" --data-binary @request.json https://vision.googleapis.com/v1/images:annotate?key=${API_KEY}
โก๏ธ ํฐ๋ฏธ๋์์ ๋ช ๋ น์ด๋ฅผ ์ ๋ ฅํ์ฌ Cloud Vision API ํธ์ถํ๊ณ , ์ด ์ด๋ฏธ์ง๊ฐ ๋ฐํํ ํญ๋ชฉ ์ดํด๋ณด๊ธฐ
{
"responses": [
{
"webDetection": {
"webEntities": [
{
"entityId": "/m/06_dn",
"score": 1.1010823,
"description": "Snow"
},
{
"entityId": "/m/0csby",
"score": 0.4279,
"description": "Cloud"
},
...
"fullMatchingImages": [
{
"url": "https://cdn.qwiklabs.com/V4PmEUI7yXdKpytLNRqwV%2ByGHqym%2BfhdktVi8nj4pPs%3D"
},
{
"url": "https://miro.medium.com/v2/resize:fit:1400/1*JSX2yNORAAOxdGoU17c4_A.png"
}
],
"pagesWithMatchingImages": [
{
"url": "https://www.youtube.com/watch?v=KzEFdedw-9o",
"pageTitle": "Snow, and stuff about it you might not know - YouTube"
},
{
"url": "https://www.youtube.com/watch?v=ubUlXd0VcTk",
"pageTitle": "Backyard Science: What Is So Cool About Snow? - YouTube"
},
...
"visuallySimilarImages": [
{
"url": "https://images.nationalgeographic.org/image/upload/v1638889943/EducationHub/photos/beignets.jpg"
},
{
"url": "https://i0.wp.com/www.middleeastmonitor.com/wp-content/uploads/2023/09/thumbnail_website-31.jpg?fit=1200%2C800&ssl=1"
},
...
์๋ต ๊ฒฐ๊ณผ ๋ถ์
webEntities: ์ด ์ด๋ฏธ์ง๋ Cloud ML API์ ๋ํ ์ฌ๋ฌ ํ๋ ์ ํ ์ด์ ์์ ์ฌ์ฉ๋์๊ธฐ ๋๋ฌธ์ API์์ "๋จธ์ ๋ฌ๋"๊ณผ "Google Cloud Platform" ์ํฐํฐ๋ฅผ ์ฐพ์๋ค.fullMatchingImages,partialMatchingImages,pagesWithMatchingImages๋ถ๋ถ์ URL์ ์ดํด๋ณด๋ฉด ๋ง์ URL์ด ์ด ๋ฉ ์ฌ์ดํธ๋ฅผ ๊ฐ๋ฆฌํค๋ ๊ฒ์ ์ ์ ์๋ค.visuallySimilarImages: ์์ ํ ๋๊ฐ์ ์ด๋ฏธ์ง๊ฐ ์๋ ๋ค๋ฅธ ์ด๋ฏธ์ง๋ฅผ ์ฐพ๊ณ ์ถ์ ๋ ์ฐธ๊ณ ํ๋ฉด ๋๋ค.

Update the JSON file request.json to achieve three specific changes:
* Update the gcsImageUri value from donuts.png to selfie.png.
* Replace the existing features array with two new feature types: FACE_DETECTION and LANDMARK_DETECTION.
* Don't want maxResults result.
โก๏ธ request.json > ํ๋กฌํํธ ๋ฉ์์ง ์
๋ ฅ > Gemini Diff ๋ทฐ์ ๋ฉ์์ง๊ฐ ํ์๋๋ฉด ๋ชจ๋ ๋ณ๊ฒฝ์ฌํญ ์ ์ฉ์ ํด๋ฆญ

curl -s -X POST -H "Content-Type: application/json" --data-binary @request.json https://vision.googleapis.com/v1/images:annotate?key=${API_KEY}
โก๏ธ ํฐ๋ฏธ๋์์ ๋ช ๋ น์ด๋ฅผ ์ ๋ ฅํ์ฌ Cloud Vision API ํธ์ถํ๊ณ , ์ด ์ด๋ฏธ์ง๊ฐ ๋ฐํํ ํญ๋ชฉ ์ดํด๋ณด๊ธฐ
{
"faceAnnotations": [
{
"boundingPoly": {
"vertices": [
{
"x": 669,
"y": 324
},
...
]
},
"fdBoundingPoly": {
...
},
"landmarks": [
{
"type": "LEFT_EYE",
"position": {
"x": 692.05646,
"y": 372.95868,
"z": -0.00025268539
}
},
...
],
"rollAngle": 0.21619819,
"panAngle": -23.027969,
"tiltAngle": -1.5531756,
"detectionConfidence": 0.72354823,
"landmarkingConfidence": 0.20047489,
"joyLikelihood": "LIKELY",
"sorrowLikelihood": "VERY_UNLIKELY",
"angerLikelihood": "VERY_UNLIKELY",
"surpriseLikelihood": "VERY_UNLIKELY",
"underExposedLikelihood": "VERY_UNLIKELY",
"blurredLikelihood": "VERY_UNLIKELY",
"headwearLikelihood": "VERY_LIKELY"
}
...
}
}
์๋ต ๊ฒฐ๊ณผ ๋ถ์
boundingPoly:์ผ๊ตด ์ด๋ฏธ์ง์ ์ฃผ๋ณ x, y ์ขํ ์ ๊ณตfdBoundingPoly:boundingPoly๋ณด๋ค ์์ ์์๋ก, ์ผ๊ตด์ ํผ๋ถ ๋ถ๋ถ์ ์ง์คlandmarks: ์ผ๊ตด์ ๊ฐ ํน์ง์ ๋ํ ๊ฐ์ฒด์ ๋ฐฐ์ด
request.json > ์์ฒญ ํ์ผ ๋ด์ฉ ์
๋ฐ์ดํธ{
"requests": [
{
"image": {
"source": {
"gcsImageUri": "gs://PROJECT_ID-bucket/city.png"
}
},
"features": [
{
"type": "LANDMARK_DETECTION",
"maxResults": 10
}
]
}
]
}

curl -s -X POST -H "Content-Type: application/json" --data-binary @request.json https://vision.googleapis.com/v1/images:annotate?key=${API_KEY}
โก๏ธ ํฐ๋ฏธ๋์์ ๋ช ๋ น์ด๋ฅผ ์ ๋ ฅํ์ฌ Cloud Vision API ํธ์ถํ๊ณ , ์ด ์ด๋ฏธ์ง๊ฐ ๋ฐํํ ํญ๋ชฉ ์ดํด๋ณด๊ธฐ
์๋ต ๊ฒฐ๊ณผ ๋ถ์
mid: ๋๋๋งํฌ์ mid ๊ฐdescription: ๋๋๋งํฌ ์ด๋ฆscore: ์ ๋ขฐ๋ ์ ์boundingPoly: ์ด๋ฏธ์ง์์ ๋๋๋งํฌ๊ฐ ์๋ณ๋ ์์ญlocations: ์ฌ์ง์ ์๋ ๋ฐ ๊ฒฝ๋ ์ขํ๋ฅผ ์๋ ค์ฃผ๋ ํค๐กAPI ์๋ต ๊ฒฐ๊ณผ๋ฅผ ๋ถ์ํ ๊ฒฐ๊ณผ, ์ฌ์ง์ด ์ดฌ์๋ ์ฅ์๋ฅผ ์๋ณํ๊ณ ํด๋น ์์น์ ์ง๋ ์ขํ(๋ฌ์์ ๋ชจ์คํฌ๋ฐ ๋ถ์ ๊ด์ฅ์ ์ฑ ๋ฐ์ค๋ฆฌ ์ฑ๋น)๋ฅผ ์ ๊ณตํจ์ ์ ์ ์๋ค.

โก๏ธ request.json > ์์ฒญ ํ์ผ ๋ด์ฉ ์
๋ฐ์ดํธ
{
"requests": [
{
"image": {
"source": {
"imageUri": "https://cloud.google.com/vision/docs/images/bicycle_example.png"
}
},
"features": [
{
"maxResults": 10,
"type": "OBJECT_LOCALIZATION"
}
]
}
]
}

curl -s -X POST -H "Content-Type: application/json" --data-binary @request.json https://vision.googleapis.com/v1/images:annotate?key=${API_KEY}
โก๏ธ ํฐ๋ฏธ๋์์ ๋ช ๋ น์ด๋ฅผ ์ ๋ ฅํ์ฌ Cloud Vision API ํธ์ถํ๊ณ , ์ด ์ด๋ฏธ์ง๊ฐ ๋ฐํํ ํญ๋ชฉ ์ดํด๋ณด๊ธฐ
{
"responses": [
{
"localizedObjectAnnotations": [
{
"mid": "/m/01bqk0",
"name": "Bicycle wheel",
"score": 0.89648587,
"boundingPoly": {
"normalizedVertices": [
{
"x": 0.32076266,
"y": 0.78941387
},
{
"x": 0.43812272,
"y": 0.78941387
},
{
"x": 0.43812272,
"y": 0.97331065
},
{
"x": 0.32076266,
"y": 0.97331065
}
]
}
},
{
"mid": "/m/0199g",
"name": "Bicycle",
"score": 0.886761,
"boundingPoly": {
"normalizedVertices": [
{
"x": 0.312,
"y": 0.6616471
},
{
"x": 0.638353,
"y": 0.6616471
},
{
"x": 0.638353,
"y": 0.9705882
},
{
"x": 0.312,
"y": 0.9705882
}
]
}
},
...
์๋ต ๊ฒฐ๊ณผ ๋ถ์๐ก
Vision API๊ฐ ์ด ์ฌ์ง์์ ์์ ๊ฑฐ์ ์์ ๊ฑฐ ๋ฐํด๋ฅผ ์๋ณํ๋ค๋ ๊ฒ์ ์ ์ ์๋ค!