-
Notifications
You must be signed in to change notification settings - Fork 8
/
takeAndGet.py
101 lines (62 loc) · 2.37 KB
/
takeAndGet.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python
import requests
import shutil
import time
import sys
BASEURL = 'http://192.168.107.1/osc/'
resp = requests.get(BASEURL + 'info')
if resp.status_code != 200:
# This means something went wrong.
raise ApiError('GET /osc/info/ {}'.format(resp.status_code))
print('Manufacturer: {}'.format(resp.json()["manufacturer"]))
print('Model: {}'.format(resp.json()["model"]))
print('Firmware: {}'.format(resp.json()["firmwareVersion"]))
print('Serial: {}'.format(resp.json()["serialNumber"]))
resp = requests.post(BASEURL + 'state')
if resp.status_code != 200:
# This means something went wrong.
raise ApiError('GET /osc/state/ {}'.format(resp.status_code))
print('batteryLevel: {}'.format(resp.json()["state"]["batteryLevel"]*100))
data = {"name": "camera.startSession", "parameters": {} }
resp = requests.post(BASEURL + 'commands/execute', json=data)
if resp.status_code != 200:
# This means something went wrong.
raise ApiError('camera.startSession: {}'.format(resp.status_code))
sessionId = (resp.json()["results"]["sessionId"])
print ('SessionId: {}'.format(sessionId))
# TAKE A NEW IMAGE
print ('Say cheese!')
data = {"name": "camera.takePicture", "parameters": { "sessionId": sessionId} }
resp = requests.post(BASEURL + 'commands/execute', json=data)
if resp.status_code != 200:
# This means something went wrong.
raise ApiError('camera.takePicture: {}'.format(resp.status_code))
pictureId = (resp.json()["id"])
print ('Click!')
# WAIT FOR LAST IMAGE TO CHANGE
sys.stdout.write('Waiting for image processing')
sys.stdout.flush()
for x in xrange(1, 30):
sys.stdout.write('.')
sys.stdout.flush()
data = {"id": pictureId }
resp = requests.post(BASEURL + 'commands/status', json=data)
if format(resp.json()["state"])=='done':
break
time.sleep(0.5)
print ('')
uri = resp.json()["results"]["fileUri"]
#print ('uri: {}'.format(uri))
# GET NEW IMAGE
name="OSC_" + pictureId+".JPG"
data = {"name": "camera.getImage", "parameters": { "fileUri": uri} }
resp = requests.post(BASEURL + 'commands/execute', json=data)
if resp.status_code != 200:
# This means something went wrong.
raise ApiError('camera.getImage: {}'.format(resp))
# SAVE NEW IMAGE
resp.raw.decode_content = True
with open(name,'w') as ofh:
for chunk in resp:
ofh.write(chunk)
print ('Image stored as: {}'.format(name))