forked from lavishsheth/code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extract, Analyze, and Translate Text from Images with the Cloud ML APIs
62 lines (53 loc) · 2.1 KB
/
Extract, Analyze, and Translate Text from Images with the Cloud ML APIs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
gcloud alpha services api-keys create --display-name="testnamee"
KEY_NAME=$(gcloud alpha services api-keys list --format="value(name)" --filter "displayName=testnamee")
API_KEY=$(gcloud alpha services api-keys get-key-string $KEY_NAME --format="value(keyString)")
export PROJECT_ID=$(gcloud config list --format 'value(core.project)')
gsutil mb gs://$PROJECT_ID
git clone https://github.com/karankk45/karankk45.git
gsutil cp karankk45/sign.jpg gs://$PROJECT_ID
gsutil acl ch -u AllUsers:R gs://$PROJECT_ID/sign.jpg
touch ocr-request.json
tee ocr-request.json <<EOF
{
"requests": [
{
"image": {
"source": {
"gcsImageUri": "gs://$PROJECT_ID/sign.jpg"
}
},
"features": [
{
"type": "TEXT_DETECTION",
"maxResults": 10
}
]
}
]
}
EOF
curl -s -X POST -H "Content-Type: application/json" --data-binary @ocr-request.json https://vision.googleapis.com/v1/images:annotate?key=${API_KEY}
curl -s -X POST -H "Content-Type: application/json" --data-binary @ocr-request.json https://vision.googleapis.com/v1/images:annotate?key=${API_KEY} -o ocr-response.json
touch translation-request.json
tee translation-request.json <<EOF
{
"q": "My Name is Karan",
"target": "en"
}
EOF
STR=$(jq .responses[0].textAnnotations[0].description ocr-response.json) && STR="${STR//\"}" && sed -i "s|your_text_here|$STR|g" translation-request.json
curl -s -X POST -H "Content-Type: application/json" --data-binary @translation-request.json https://translation.googleapis.com/language/translate/v2?key=${API_KEY} -o translation-response.json
cat translation-response.json
touch nl-request.json.json
tee nl-request.json <<EOF
{
"document":{
"type":"PLAIN_TEXT",
"content":"your_text_here"
},
"encodingType":"UTF8"
}
EOF
STR=$(jq .data.translations[0].translatedText translation-response.json) && STR="${STR//\"}" && sed -i "s|your_text_here|$STR|g" nl-request.json
curl "https://language.googleapis.com/v1/documents:analyzeEntities?key=${API_KEY}" \
-s -X POST -H "Content-Type: application/json" --data-binary @nl-request.json