Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Jun 7, 2024. It is now read-only.
forked from open-runtimes/examples
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
Latest commit
57 lines (45 loc) · 1.71 KB
/
Copy pathmain.py
File metadata and controls
57 lines (45 loc) · 1.71 KB
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
importjson
importrequests
fromcloudmersive_image_api_clientimportConfiguration, RecognizeApi, ApiClient
fromcloudmersive_image_api_client.restimportApiException
defmain(req, res):
# Input validation
file_url=None
try:
payload=json.loads(req.payload)
file_url=payload['url']
exceptExceptionaserr:
print(err)
raiseException('Payload is invalid.')
ifnotfile_url:
raiseException('Invalid search.')
# Make sure we have envirnment variables required to execute
ifnotreq.variables.get('CLOUDMERSIVE_API_KEY', None):
raiseException('Please provide all required environment variables.')
api_key=req.variables.get('CLOUDMERSIVE_API_KEY', None)
# Download the file
response=requests.get(file_url, stream=True)
withopen("temp.jpg", 'wb') asnewFile:
forchunkinresponse.iter_content(chunk_size=1024):
ifchunk:
newFile.write(chunk)
newFile.flush()
# Configure the detection client
configuration=Configuration()
configuration.api_key['Apikey'] =api_key
# Create an instance of the API class
api_client=RecognizeApi(ApiClient(configuration))
# Detect objects including types and locations in an image
api_response=api_client.recognize_detect_objects("temp.jpg")
# Return object detection results
results=list(map(lambdax: {
"name": x.object_class_name,
"confidence": x.score,
"x": x.x,
"y": x.y,
"width": x.width,
"height": x.height
}, api_response.objects))
iflen(results) <1:
raiseException('No objects found in the image.')
returnres.json(results[0])