-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluation.py
38 lines (34 loc) · 1.25 KB
/
evaluation.py
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
import sys, yaml
from google.cloud import automl
'''
get_prediction - passes image to be analyzed by pre-trained
Google Cloud Vision API AutoML. Credentials for google cloud app auth are privately locked
in creds.yaml file.
- returns predicted subject, and relative coordinates for corners of boxed subject
'''
def get_prediction(file_path):
prediction_client = automl.PredictionServiceClient()
creds = yaml.safe_load(open("creds.yaml", "r"))
project_id = creds['PROJ_ID']
model_id = creds['MODEL_ID']
with open(file_path, 'rb') as ff:
content = ff.read()
name = 'projects/{}/locations/us-central1/models/{}'.format(project_id, model_id)
payload = {'image': {'image_bytes': content }}
params = {"score_threshold": "0.501"}
request = automl.PredictRequest(
name=name,
payload=payload,
params=params
)
response = prediction_client.predict(request=request)
coords = []
pred = ''
for item in response.payload:
for vert in item.image_object_detection.bounding_box.normalized_vertices:
coords.append(vert)
pred = item.display_name
if pred!='':
return pred,coords[0].x,coords[0].y,coords[1].x,coords[1].y
else:
return None,None,None,None,None