").append(m.parseHTML(a)).find(d):a)}).complete(c&&function(a,b){g.each(c,e||[a.responseText,b,a])}),this},m.each(["ajaxStart","ajaxStop","ajaxComplete","ajaxError","ajaxSuccess","ajaxSend"],function(a,b){m.fn[b]=function(a){return this.on(b,a)}}),m.expr.filters.animated=function(a){return m.grep(m.timers,function(b){return a===b.elem}).length};var cc=a.document.documentElement;function dc(a){return m.isWindow(a)?a:9===a.nodeType?a.defaultView||a.parentWindow:!1}m.offset={setOffset:function(a,b,c){var d,e,f,g,h,i,j,k=m.css(a,"position"),l=m(a),n={};"static"===k&&(a.style.position="relative"),h=l.offset(),f=m.css(a,"top"),i=m.css(a,"left"),j=("absolute"===k||"fixed"===k)&&m.inArray("auto",[f,i])>-1,j?(d=l.position(),g=d.top,e=d.left):(g=parseFloat(f)||0,e=parseFloat(i)||0),m.isFunction(b)&&(b=b.call(a,c,h)),null!=b.top&&(n.top=b.top-h.top+g),null!=b.left&&(n.left=b.left-h.left+e),"using"in b?b.using.call(a,n):l.css(n)}},m.fn.extend({offset:function(a){if(arguments.length)return void 0===a?this:this.each(function(b){m.offset.setOffset(this,a,b)});var b,c,d={top:0,left:0},e=this[0],f=e&&e.ownerDocument;if(f)return b=f.documentElement,m.contains(b,e)?(typeof e.getBoundingClientRect!==K&&(d=e.getBoundingClientRect()),c=dc(f),{top:d.top+(c.pageYOffset||b.scrollTop)-(b.clientTop||0),left:d.left+(c.pageXOffset||b.scrollLeft)-(b.clientLeft||0)}):d},position:function(){if(this[0]){var a,b,c={top:0,left:0},d=this[0];return"fixed"===m.css(d,"position")?b=d.getBoundingClientRect():(a=this.offsetParent(),b=this.offset(),m.nodeName(a[0],"html")||(c=a.offset()),c.top+=m.css(a[0],"borderTopWidth",!0),c.left+=m.css(a[0],"borderLeftWidth",!0)),{top:b.top-c.top-m.css(d,"marginTop",!0),left:b.left-c.left-m.css(d,"marginLeft",!0)}}},offsetParent:function(){return this.map(function(){var a=this.offsetParent||cc;while(a&&!m.nodeName(a,"html")&&"static"===m.css(a,"position"))a=a.offsetParent;return a||cc})}}),m.each({scrollLeft:"pageXOffset",scrollTop:"pageYOffset"},function(a,b){var c=/Y/.test(b);m.fn[a]=function(d){return V(this,function(a,d,e){var f=dc(a);return void 0===e?f?b in f?f[b]:f.document.documentElement[d]:a[d]:void(f?f.scrollTo(c?m(f).scrollLeft():e,c?e:m(f).scrollTop()):a[d]=e)},a,d,arguments.length,null)}}),m.each(["top","left"],function(a,b){m.cssHooks[b]=La(k.pixelPosition,function(a,c){return c?(c=Ja(a,b),Ha.test(c)?m(a).position()[b]+"px":c):void 0})}),m.each({Height:"height",Width:"width"},function(a,b){m.each({padding:"inner"+a,content:b,"":"outer"+a},function(c,d){m.fn[d]=function(d,e){var f=arguments.length&&(c||"boolean"!=typeof d),g=c||(d===!0||e===!0?"margin":"border");return V(this,function(b,c,d){var e;return m.isWindow(b)?b.document.documentElement["client"+a]:9===b.nodeType?(e=b.documentElement,Math.max(b.body["scroll"+a],e["scroll"+a],b.body["offset"+a],e["offset"+a],e["client"+a])):void 0===d?m.css(b,c,g):m.style(b,c,d,g)},b,f?d:void 0,f,null)}})}),m.fn.size=function(){return this.length},m.fn.andSelf=m.fn.addBack,"function"==typeof define&&define.amd&&define("jquery",[],function(){return m});var ec=a.jQuery,fc=a.$;return m.noConflict=function(b){return a.$===m&&(a.$=fc),b&&a.jQuery===m&&(a.jQuery=ec),m},typeof b===K&&(a.jQuery=a.$=m),m});
6 |
--------------------------------------------------------------------------------
/examples/javascript/thetaJSTest_will_not_work.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
26 |
27 |
--------------------------------------------------------------------------------
/examples/kivy/ping/fnt/Lato-Thin.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codetricity/theta-s-api-tests/c60a5fd8862a00579e5dd93cc57c0264a60abced/examples/kivy/ping/fnt/Lato-Thin.ttf
--------------------------------------------------------------------------------
/examples/kivy/ping/img/ricoh-theta-s.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codetricity/theta-s-api-tests/c60a5fd8862a00579e5dd93cc57c0264a60abced/examples/kivy/ping/img/ricoh-theta-s.png
--------------------------------------------------------------------------------
/examples/kivy/ping/img/theta_developers.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codetricity/theta-s-api-tests/c60a5fd8862a00579e5dd93cc57c0264a60abced/examples/kivy/ping/img/theta_developers.png
--------------------------------------------------------------------------------
/examples/kivy/ping/main.py:
--------------------------------------------------------------------------------
1 | from kivy.app import App
2 | from kivy.uix.widget import Widget
3 | from kivy.uix.button import Button
4 | from thetaPythonTest import startSession, takePicture
5 |
6 | class takePictureBtn(Button):
7 | def on_press(self):
8 | print("Take Picture button pressed")
9 | sid = startSession()
10 | takePicture(sid)
11 |
12 | class Ping(Widget):
13 | pass
14 |
15 | class PingApp(App):
16 | def build(self):
17 | return Ping()
18 |
19 | PingApp().run()
20 |
--------------------------------------------------------------------------------
/examples/kivy/ping/ping.kv:
--------------------------------------------------------------------------------
1 | #:kivy 1.9
2 |
3 |
:
4 | Label:
5 | font_size: 30
6 | text: "RICOH THETA S API v2 \nPython Example with Kivy"
7 | pos: 200, root.top - 100
8 |
9 | takePictureBtn:
10 | text: "Take Picture"
11 | pos: 150, root.top - 250
12 |
13 |
--------------------------------------------------------------------------------
/examples/kivy/ping/thetaPythonTest.py:
--------------------------------------------------------------------------------
1 | import requests, json, pprint
2 |
3 |
4 |
5 | def request(url_request):
6 | url_base = "http://192.168.1.1/osc/"
7 | url = url_base + url_request
8 | return url
9 |
10 | def basic_info():
11 | protocols = [
12 | "state",
13 | "info"
14 | ]
15 | for protocol in protocols:
16 | print(60 * "=")
17 | print(protocol + " - Testing RICOH THETA API v2\n")
18 | url = request(protocol)
19 | if protocol == "info":
20 | req = requests.get(url)
21 | else:
22 | req = requests.post(url)
23 | pprint.pprint(req.json())
24 |
25 | def startSession():
26 | url = request("commands/execute")
27 | body = json.dumps({"name": "camera.startSession",
28 | "parameters": {}
29 | })
30 | req = requests.post(url, data=body)
31 | response = req.json()
32 | print(60 * "=")
33 | print("startSession - Testing RICOH THETA API v2\n")
34 | pprint.pprint(response)
35 | sid = (response["results"]["sessionId"])
36 | print sid
37 | return sid
38 |
39 | def takePicture(sid):
40 | url = request("commands/execute")
41 | body = json.dumps({"name": "camera.takePicture",
42 | "parameters": {
43 | "sessionId": sid
44 | }
45 | })
46 | req = requests.post(url, data=body)
47 | response = req.json()
48 | print(60 * "=")
49 | print("takePicture - Testing RICOH THETA API v2\n")
50 | pprint.pprint(response)
51 |
52 | def main():
53 | basic_info()
54 | sid = startSession() #grab session ID
55 | takePicture(sid) #still image
56 |
57 | if __name__ == '__main__':
58 | main()
59 |
60 | # url = base_url + "commands/execute"
61 |
62 | #req = requests.post(url, data=body)
63 |
--------------------------------------------------------------------------------
/examples/kivy/ping/thetaPythonTest.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codetricity/theta-s-api-tests/c60a5fd8862a00579e5dd93cc57c0264a60abced/examples/kivy/ping/thetaPythonTest.pyc
--------------------------------------------------------------------------------
/examples/kivy/ping/thetapylib.py:
--------------------------------------------------------------------------------
1 | """
2 | Example library for RICOH THETA S hacking with Python. The new
3 | API is compliant with the Open Spherical Camera specification.
4 | This is intended to show how the THETA S API works. It is
5 | not intended for use in your program. There is no error
6 | checking and this example library only handles a handful
7 | of commands.
8 |
9 |
10 | There are three example programs that use this library.
11 | At the top of your Python script, use
12 |
13 | from thetapylib import *
14 |
15 | After you import the library, you can use the commands like this:
16 |
17 | state()
18 |
19 | That will return the state of the camera, which is great to
20 | get the sessionId.
21 |
22 | You can also get the sessionId when you start a new session:
23 |
24 | startSession()
25 |
26 | In fact, the startSession() function will return the
27 | sessionId.
28 |
29 | Example use of the library with Pygame to detect the
30 | button press.
31 |
32 | if event.type == pygame.MOUSEBUTTONDOWN:
33 | mouse_pos = pygame.mouse.get_pos()
34 | if pictureButton.collidepoint(mouse_pos):
35 | sid = startSession()
36 | takePicture(sid)
37 | if captureStartButton.collidepoint(mouse_pos):
38 | sid = startSession()
39 | startCapture(sid)
40 | if captureStopButton.collidepoint(mouse_pos):
41 | stopCapture(sid)
42 |
43 | Example use of library from the command line:
44 |
45 | if sys.argv[1] == "startCapture":
46 | if len(sys.argv) < 3:
47 | print("Usage: pyTHETA.py startCapture SID_000X")
48 | print("Use 'state' to get sessionId")
49 |
50 | else:
51 | sid = sys.argv[2]
52 | response = startCapture(sid)
53 | pprint.pprint(response)
54 | elif sys.argv[1] == "stopCapture":
55 | if len(sys.argv) < 3:
56 | print("Usage: pyTHETA.py stopCapture SID_000X")
57 | print("Use 'state' to get sessionId")
58 | else:
59 | sid = sys.argv[2]
60 | response = stopCapture(sid)
61 | pprint.pprint(response)
62 |
63 | """
64 | import requests
65 | import json
66 | # import pprint # for printing out test data
67 | # from PIL import Image
68 | # from StringIO import StringIO
69 |
70 | def request(url_request):
71 | """
72 | Generate the URI to send to the THETA S. The THETA IP address is
73 | 192.168.1.1
74 | All calls start with /osc/
75 | """
76 | url_base = "http://192.168.1.1/osc/"
77 | url = url_base + url_request
78 | return url
79 |
80 | def startSession():
81 | """
82 | Start a new session. Grab the sessionId number and
83 | return it.
84 | You'll need the sessionId to take a video or image.
85 | """
86 | url = request("commands/execute")
87 | body = json.dumps({"name": "camera.startSession",
88 | "parameters": {}
89 | })
90 | try:
91 | req = requests.post(url, data=body)
92 | except requests.exceptions.ConnectionError as e:
93 | sid = None
94 | return sid
95 |
96 | if req.status_code == 200:
97 | response = req.json()
98 | sid = (response["results"]["sessionId"])
99 | else:
100 | sid = None
101 | return sid
102 |
103 | def takePicture(sid):
104 | """
105 | Take a still image. The sessionId is either taken from
106 | startSession or from state. You can change the mode
107 | from video to image with captureMode in the options.
108 | """
109 | if sid == None:
110 | response = None
111 | return response
112 | url = request("commands/execute")
113 | body = json.dumps({"name": "camera.takePicture",
114 | "parameters": {
115 | "sessionId": sid
116 | }
117 | })
118 | try:
119 | req = requests.post(url, data=body)
120 | except requests.exceptions.ConnectionError as e:
121 | return e
122 | if req.status_code == 200:
123 | response = req.json()
124 | else:
125 | response = "HTTP error"
126 | return response
127 |
128 | def setMode(sid, mode):
129 | """
130 | Change mode between image and _video.
131 | The sessionId is either taken from
132 | startSession or from state.
133 | See
134 | https://developers.theta360.com/en/docs/v2/api_reference/options/capture_mode.html
135 | """
136 | if sid == None:
137 | response = None
138 | return response
139 | url = request("commands/execute")
140 | body = json.dumps({"name": "camera.setOptions",
141 | "parameters": {
142 | "sessionId": sid,
143 | "options": {
144 | "captureMode": mode,
145 | }
146 | }
147 | })
148 | try:
149 | req = requests.post(url, data=body)
150 | except requests.exceptions.ConnectionError as e:
151 | return e
152 | if req.status_code == 200:
153 | response = req.json()
154 | else:
155 | response = "HTTP error"
156 | return response
157 |
158 |
159 | def info():
160 | """
161 | Get basic information ont he camera. Note that this is a GET call
162 | and not a POST. Most of the calls are POST.
163 | """
164 | url = request("info")
165 | try:
166 | req = requests.get(url)
167 | except requests.exceptions.ConnectionError as e:
168 | return e
169 | print(req.status_code)
170 | if req.status_code == 200:
171 | response = req.json()
172 | else:
173 | response = "HTTP error"
174 | return response
175 |
176 |
177 |
178 |
179 | def state():
180 | """
181 | Get the state of the camera, which will include the sessionsId and also the
182 | latestFileUri if you've just taken a picture.
183 | """
184 | url = request("state")
185 | try:
186 | req = requests.post(url)
187 | except requests.exceptions.ConnectionError as e:
188 | return e
189 | if req.status_code == 200:
190 | response = req.json()
191 | else:
192 | response = "HTTP error"
193 | return response
194 |
195 | def getSid():
196 | """
197 | Get the state of the camera, and return the sessionsId.
198 | """
199 | url = request("state")
200 | try:
201 | req = requests.post(url)
202 | except requests.exceptions.ConnectionError as e:
203 | return e
204 | if req.status_code == 200:
205 | response = req.json()
206 | sid = response["state"]["sessionId"]
207 | else:
208 | sid = "HTTP error"
209 | return sid
210 |
211 |
212 | def startCapture(sid):
213 | """
214 | Begin video capture if the captureMode is _video. If the
215 | captureMode is set to image, the camera will take multiple
216 | still images. The captureMode can be set in the options.
217 | Note that this will not work with streaming video using the
218 | HDMI or USB cable.
219 | """
220 | url = request("commands/execute")
221 | body = json.dumps({"name": "camera._startCapture",
222 | "parameters": {
223 | "sessionId": sid
224 | }
225 | })
226 | try:
227 | req = requests.post(url, data=body)
228 | except requests.exceptions.ConnectionError as e:
229 | return e
230 | if req.status_code == 200:
231 | response = req.json()
232 | else:
233 | response = "HTTP error"
234 | return response
235 |
236 |
237 | def stopCapture(sid):
238 | """
239 | Stop video capture. If in image mode, will stop
240 | automatic image taking.
241 | """
242 | url = request("commands/execute")
243 | body = json.dumps({"name": "camera._stopCapture",
244 | "parameters": {
245 | "sessionId": sid
246 | }
247 | })
248 | try:
249 | req = requests.post(url, data=body)
250 | except requests.exceptions.ConnectionError as e:
251 | return e
252 | if req.status_code == 200:
253 | response = req.json()
254 | else:
255 | response = "HTTP error"
256 | return response
257 |
258 | def latestFileUri():
259 | """
260 | This will only work if you've just taken a picture. The state
261 | will include the attribute latestFileUri. You need this to
262 | transfer the file from the camera to your computer or phone.
263 | """
264 | try:
265 | state_data = state()["state"]
266 | except:
267 | return False
268 | latestFileUri = state_data["_latestFileUri"]
269 | return latestFileUri
270 |
271 |
272 |
273 | def getImage(fileUri, imageType="image"):
274 | """
275 | Transfer the file from the camera to computer and save the
276 | binary data to local storage. This works, but is clunky.
277 | There are easier ways to do this. The __type parameter
278 | can be set to "thumb" for a thumbnail or "image" for the
279 | full-size image. The default is "image".
280 | """
281 | if fileUri:
282 | url = request("commands/execute")
283 | body = json.dumps({"name": "camera.getImage",
284 | "parameters": {
285 | "fileUri": fileUri,
286 | "_type": imageType
287 | }
288 | })
289 | fileName = fileUri.split("/")[1]
290 | print(fileName)
291 | with open(fileName, 'wb') as handle:
292 | response = requests.post(url, data=body, stream=True)
293 | for block in response.iter_content(1024):
294 | handle.write(block)
295 |
296 | def getMode(sid):
297 | url = request("commands/execute")
298 | body = json.dumps({"name": "camera.getOptions",
299 | "parameters": {
300 | "sessionId": sid,
301 | "optionNames": [
302 | "captureMode"]
303 | }
304 | })
305 | try:
306 | req = requests.post(url, data=body)
307 | except requests.exceptions.ConnectionError as e:
308 | return e
309 | if req.status_code == 200:
310 | response = req.json()
311 | mode = response["results"]["options"]["captureMode"]
312 | else:
313 | mode = "HTTP error"
314 | return mode
315 |
316 |
317 |
318 | def listAll(entryCount = 3, detail = False, sortType = "newest", ):
319 | """
320 | entryCount:
321 | Integer No. of still images and video files to be acquired
322 | detail:
323 | Boolean (Optional) Whether or not file details are acquired
324 | true is acquired by default. Only values that can be acquired
325 | when false is specified are "name", "uri", "size" and "dateTime"
326 | sort:
327 | String (Optional) Specify the sort order
328 | newest (dateTime descending order)/ oldest (dateTime ascending order)
329 | Default is newest
330 | """
331 | url = request("commands/execute")
332 | body = json.dumps({"name": "camera._listAll",
333 | "parameters": {
334 | "entryCount": entryCount,
335 | "detail": detail,
336 | "sort": sortType
337 | }
338 | })
339 | try:
340 | req = requests.post(url, data=body)
341 | except requests.exceptions.ConnectionError as e:
342 | return e
343 | if req.status_code == 200:
344 | response = req.json()
345 | else:
346 | response = "HTTP error"
347 | return response
348 |
--------------------------------------------------------------------------------
/examples/pyTHETA/README.md:
--------------------------------------------------------------------------------
1 | Usage: python pyTHETA command
2 |
3 | Example:
4 |
5 | Show avaiable commands
6 |
7 | python pyTHETA help
8 |
9 | Show camera info
10 |
11 | python pyTHETA info
12 |
13 | Start session
14 |
15 | python pyTHETA startSession
16 |
17 | Take picture
18 |
19 | python pyTHETA takePicture SID_0001
20 |
--------------------------------------------------------------------------------
/examples/pyTHETA/pyTHETA.py:
--------------------------------------------------------------------------------
1 | #! /usr/bin/env python
2 |
3 | import requests, json, pprint
4 | import sys, io
5 | from thetapylib import *
6 |
7 | def main():
8 | if len(sys.argv) < 2:
9 | print("\nUsage: $ python pyTHETA.py command")
10 | print("'$ python pyTHETA.py help' for options\n")
11 | # elif len(sys.argv) > 2:
12 | # print("\nUse only one argument.")
13 | else:
14 | help = ["help", "--help", "-h", "-help", "h"]
15 | if sys.argv[1] in help:
16 | print("""
17 | Usage: $ python pyTHETA.py COMMAND
18 | Available commands:
19 | startSession
20 | takePicture SID
21 | info
22 | state
23 | startCapture SID
24 | stopCapture SID
25 | latestFileUri
26 | getLatestImage
27 | listAll NUMBER
28 | getMode SID
29 | getSid
30 | setMode SID_000X mode
31 | """)
32 |
33 | elif sys.argv[1] == "startSession":
34 | sid = startSession()
35 | print ("sessionId: {}".format(sid))
36 | elif sys.argv[1] == "takePicture":
37 | if len(sys.argv) < 3:
38 | print("Usage: pyTHETA.py takePicture SID_000X")
39 | print("Use 'state' to get sessionId")
40 | else:
41 | sid = startSession()
42 | response = takePicture(sid)
43 | pprint.pprint(response)
44 | elif sys.argv[1] == "getMode":
45 | sid = sys.argv[2]
46 | response = getMode(sid)
47 | pprint.pprint(response)
48 | elif sys.argv[1] == "setMode":
49 | if len(sys.argv) < 4:
50 | print("Usage: pyTHETA.py setMode SID_000X mode")
51 | print("Use pyTHETA.py getSid to get sessionID")
52 | print("mode is either image or video")
53 | else:
54 | sid = sys.argv[2]
55 | mode = sys.argv[3]
56 | response = setMode(sid, mode)
57 | pprint.pprint(response)
58 | elif sys.argv[1] == "getSid":
59 | response = getSid()
60 | pprint.pprint(response)
61 |
62 | elif sys.argv[1] == "info":
63 | response = info()
64 | pprint.pprint(response)
65 | elif sys.argv[1] == "state":
66 | response = state()
67 | pprint.pprint(response)
68 | elif sys.argv[1] == "startCapture":
69 | if len(sys.argv) < 3:
70 | print("Usage: pyTHETA.py startCapture SID_000X")
71 | print("Use 'state' to get sessionId")
72 |
73 | else:
74 | sid = sys.argv[2]
75 | response = startCapture(sid)
76 | pprint.pprint(response)
77 | elif sys.argv[1] == "stopCapture":
78 | if len(sys.argv) < 3:
79 | print("Usage: pyTHETA.py stopCapture SID_000X")
80 | print("Use 'state' to get sessionId")
81 | else:
82 | sid = sys.argv[2]
83 | response = stopCapture(sid)
84 | pprint.pprint(response)
85 | elif sys.argv[1] == "latestFileUri":
86 | data = latestFileUri()
87 | print(data)
88 | elif sys.argv[1] == "getLatestImage":
89 | fileUri = latestFileUri()
90 | getImage(fileUri)
91 | elif sys.argv[1] == "listAll":
92 | if len(sys.argv) > 2:
93 | listing_all = listAll(int(sys.argv[2]))
94 | pprint.pprint(listing_all)
95 | else:
96 | listing_all = listAll()
97 | pprint.pprint(listing_all)
98 |
99 |
100 | if __name__ == '__main__':
101 | main()
102 |
--------------------------------------------------------------------------------
/examples/pyTHETA/thetapylib.py:
--------------------------------------------------------------------------------
1 | """
2 | Example library for RICOH THETA S hacking with Python. The new
3 | API is compliant with the Open Spherical Camera specification.
4 | This is intended to show how the THETA S API works. It is
5 | not intended for use in your program. There is no error
6 | checking and this example library only handles a handful
7 | of commands.
8 |
9 |
10 | There are three example programs that use this library.
11 | At the top of your Python script, use
12 |
13 | from thetapylib import *
14 |
15 | After you import the library, you can use the commands like this:
16 |
17 | state()
18 |
19 | That will return the state of the camera, which is great to
20 | get the sessionId.
21 |
22 | You can also get the sessionId when you start a new session:
23 |
24 | startSession()
25 |
26 | In fact, the startSession() function will return the
27 | sessionId.
28 |
29 | Example use of the library with Pygame to detect the
30 | button press.
31 |
32 | if event.type == pygame.MOUSEBUTTONDOWN:
33 | mouse_pos = pygame.mouse.get_pos()
34 | if pictureButton.collidepoint(mouse_pos):
35 | sid = startSession()
36 | takePicture(sid)
37 | if captureStartButton.collidepoint(mouse_pos):
38 | sid = startSession()
39 | startCapture(sid)
40 | if captureStopButton.collidepoint(mouse_pos):
41 | stopCapture(sid)
42 |
43 | Example use of library from the command line:
44 |
45 | if sys.argv[1] == "startCapture":
46 | if len(sys.argv) < 3:
47 | print("Usage: pyTHETA.py startCapture SID_000X")
48 | print("Use 'state' to get sessionId")
49 |
50 | else:
51 | sid = sys.argv[2]
52 | response = startCapture(sid)
53 | pprint.pprint(response)
54 | elif sys.argv[1] == "stopCapture":
55 | if len(sys.argv) < 3:
56 | print("Usage: pyTHETA.py stopCapture SID_000X")
57 | print("Use 'state' to get sessionId")
58 | else:
59 | sid = sys.argv[2]
60 | response = stopCapture(sid)
61 | pprint.pprint(response)
62 |
63 | """
64 | import requests
65 | import json
66 | # import pprint # for printing out test data
67 | # from PIL import Image
68 | # from StringIO import StringIO
69 |
70 | def request(url_request):
71 | """
72 | Generate the URI to send to the THETA S. The THETA IP address is
73 | 192.168.1.1
74 | All calls start with /osc/
75 | """
76 | url_base = "http://192.168.1.1/osc/"
77 | url = url_base + url_request
78 | return url
79 |
80 | def startSession():
81 | """
82 | Start a new session. Grab the sessionId number and
83 | return it.
84 | You'll need the sessionId to take a video or image.
85 | """
86 | url = request("commands/execute")
87 | body = json.dumps({"name": "camera.startSession",
88 | "parameters": {}
89 | })
90 | try:
91 | req = requests.post(url, data=body)
92 | except requests.exceptions.ConnectionError as e:
93 | sid = None
94 | return sid
95 |
96 | if req.status_code == 200:
97 | response = req.json()
98 | sid = (response["results"]["sessionId"])
99 | else:
100 | sid = None
101 | return sid
102 |
103 | def takePicture(sid):
104 | """
105 | Take a still image. The sessionId is either taken from
106 | startSession or from state. You can change the mode
107 | from video to image with captureMode in the options.
108 | """
109 | if sid == None:
110 | response = None
111 | return response
112 | url = request("commands/execute")
113 | body = json.dumps({"name": "camera.takePicture",
114 | "parameters": {
115 | "sessionId": sid
116 | }
117 | })
118 | try:
119 | req = requests.post(url, data=body)
120 | except requests.exceptions.ConnectionError as e:
121 | return e
122 | if req.status_code == 200:
123 | response = req.json()
124 | else:
125 | response = "HTTP error"
126 | return response
127 |
128 | def setMode(sid, mode):
129 | """
130 | Change mode between image and _video.
131 | The sessionId is either taken from
132 | startSession or from state.
133 | See
134 | https://developers.theta360.com/en/docs/v2/api_reference/options/capture_mode.html
135 | """
136 | if sid == None:
137 | response = None
138 | return response
139 | url = request("commands/execute")
140 | body = json.dumps({"name": "camera.setOptions",
141 | "parameters": {
142 | "sessionId": sid,
143 | "options": {
144 | "captureMode": mode,
145 | }
146 | }
147 | })
148 | try:
149 | req = requests.post(url, data=body)
150 | except requests.exceptions.ConnectionError as e:
151 | return e
152 | if req.status_code == 200:
153 | response = req.json()
154 | else:
155 | response = "HTTP error"
156 | return response
157 |
158 |
159 | def info():
160 | """
161 | Get basic information ont he camera. Note that this is a GET call
162 | and not a POST. Most of the calls are POST.
163 | """
164 | url = request("info")
165 | try:
166 | req = requests.get(url)
167 | except requests.exceptions.ConnectionError as e:
168 | return e
169 | print(req.status_code)
170 | if req.status_code == 200:
171 | response = req.json()
172 | else:
173 | response = "HTTP error"
174 | return response
175 |
176 |
177 |
178 |
179 | def state():
180 | """
181 | Get the state of the camera, which will include the sessionsId and also the
182 | latestFileUri if you've just taken a picture.
183 | """
184 | url = request("state")
185 | try:
186 | req = requests.post(url)
187 | except requests.exceptions.ConnectionError as e:
188 | return e
189 | if req.status_code == 200:
190 | response = req.json()
191 | else:
192 | response = "HTTP error"
193 | return response
194 |
195 | def getSid():
196 | """
197 | Get the state of the camera, and return the sessionsId.
198 | """
199 | url = request("state")
200 | try:
201 | req = requests.post(url)
202 | except requests.exceptions.ConnectionError as e:
203 | return e
204 | if req.status_code == 200:
205 | response = req.json()
206 | sid = response["state"]["sessionId"]
207 | else:
208 | sid = "HTTP error"
209 | return sid
210 |
211 |
212 | def startCapture(sid):
213 | """
214 | Begin video capture if the captureMode is _video. If the
215 | captureMode is set to image, the camera will take multiple
216 | still images. The captureMode can be set in the options.
217 | Note that this will not work with streaming video using the
218 | HDMI or USB cable.
219 | """
220 | url = request("commands/execute")
221 | body = json.dumps({"name": "camera._startCapture",
222 | "parameters": {
223 | "sessionId": sid
224 | }
225 | })
226 | try:
227 | req = requests.post(url, data=body)
228 | except requests.exceptions.ConnectionError as e:
229 | return e
230 | if req.status_code == 200:
231 | response = req.json()
232 | else:
233 | response = "HTTP error"
234 | return response
235 |
236 |
237 | def stopCapture(sid):
238 | """
239 | Stop video capture. If in image mode, will stop
240 | automatic image taking.
241 | """
242 | url = request("commands/execute")
243 | body = json.dumps({"name": "camera._stopCapture",
244 | "parameters": {
245 | "sessionId": sid
246 | }
247 | })
248 | try:
249 | req = requests.post(url, data=body)
250 | except requests.exceptions.ConnectionError as e:
251 | return e
252 | if req.status_code == 200:
253 | response = req.json()
254 | else:
255 | response = "HTTP error"
256 | return response
257 |
258 | def latestFileUri():
259 | """
260 | This will only work if you've just taken a picture. The state
261 | will include the attribute latestFileUri. You need this to
262 | transfer the file from the camera to your computer or phone.
263 | """
264 | try:
265 | state_data = state()["state"]
266 | except:
267 | return False
268 | latestFileUri = state_data["_latestFileUri"]
269 | return latestFileUri
270 |
271 |
272 |
273 | def getImage(fileUri, imageType="image"):
274 | """
275 | Transfer the file from the camera to computer and save the
276 | binary data to local storage. This works, but is clunky.
277 | There are easier ways to do this. The __type parameter
278 | can be set to "thumb" for a thumbnail or "image" for the
279 | full-size image. The default is "image".
280 | """
281 | if fileUri:
282 | url = request("commands/execute")
283 | body = json.dumps({"name": "camera.getImage",
284 | "parameters": {
285 | "fileUri": fileUri,
286 | "_type": imageType
287 | }
288 | })
289 | fileName = fileUri.split("/")[1]
290 | print(fileName)
291 | with open(fileName, 'wb') as handle:
292 | response = requests.post(url, data=body, stream=True)
293 | for block in response.iter_content(1024):
294 | handle.write(block)
295 |
296 | def getMode(sid):
297 | url = request("commands/execute")
298 | body = json.dumps({"name": "camera.getOptions",
299 | "parameters": {
300 | "sessionId": sid,
301 | "optionNames": [
302 | "captureMode"]
303 | }
304 | })
305 | try:
306 | req = requests.post(url, data=body)
307 | except requests.exceptions.ConnectionError as e:
308 | return e
309 | if req.status_code == 200:
310 | response = req.json()
311 | mode = response["results"]["options"]["captureMode"]
312 | else:
313 | mode = "HTTP error"
314 | return mode
315 |
316 |
317 |
318 | def listAll(entryCount = 3, detail = False, sortType = "newest", ):
319 | """
320 | entryCount:
321 | Integer No. of still images and video files to be acquired
322 | detail:
323 | Boolean (Optional) Whether or not file details are acquired
324 | true is acquired by default. Only values that can be acquired
325 | when false is specified are "name", "uri", "size" and "dateTime"
326 | sort:
327 | String (Optional) Specify the sort order
328 | newest (dateTime descending order)/ oldest (dateTime ascending order)
329 | Default is newest
330 | """
331 | url = request("commands/execute")
332 | body = json.dumps({"name": "camera._listAll",
333 | "parameters": {
334 | "entryCount": entryCount,
335 | "detail": detail,
336 | "sort": sortType
337 | }
338 | })
339 | try:
340 | req = requests.post(url, data=body)
341 | except requests.exceptions.ConnectionError as e:
342 | return e
343 | if req.status_code == 200:
344 | response = req.json()
345 | else:
346 | response = "HTTP error"
347 | return response
348 |
--------------------------------------------------------------------------------
/examples/raspberryTHETA/.bash_profile:
--------------------------------------------------------------------------------
1 | alias l='ls -F'
2 | alias la='ls -Fa'
3 | alias ll='ls -lF'
4 |
--------------------------------------------------------------------------------
/examples/raspberryTHETA/README.md:
--------------------------------------------------------------------------------
1 | You must have Pygame installed. The example is built with Python 2.7.
2 | Usage: deskTHETA.py
3 |
--------------------------------------------------------------------------------
/examples/raspberryTHETA/fnt/Lato-Bold.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codetricity/theta-s-api-tests/c60a5fd8862a00579e5dd93cc57c0264a60abced/examples/raspberryTHETA/fnt/Lato-Bold.ttf
--------------------------------------------------------------------------------
/examples/raspberryTHETA/fnt/Lato-Thin.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codetricity/theta-s-api-tests/c60a5fd8862a00579e5dd93cc57c0264a60abced/examples/raspberryTHETA/fnt/Lato-Thin.ttf
--------------------------------------------------------------------------------
/examples/raspberryTHETA/icon/camera_icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codetricity/theta-s-api-tests/c60a5fd8862a00579e5dd93cc57c0264a60abced/examples/raspberryTHETA/icon/camera_icon.png
--------------------------------------------------------------------------------
/examples/raspberryTHETA/icon/camera_icon2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codetricity/theta-s-api-tests/c60a5fd8862a00579e5dd93cc57c0264a60abced/examples/raspberryTHETA/icon/camera_icon2.png
--------------------------------------------------------------------------------
/examples/raspberryTHETA/icon/download.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codetricity/theta-s-api-tests/c60a5fd8862a00579e5dd93cc57c0264a60abced/examples/raspberryTHETA/icon/download.png
--------------------------------------------------------------------------------
/examples/raspberryTHETA/icon/stop_icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codetricity/theta-s-api-tests/c60a5fd8862a00579e5dd93cc57c0264a60abced/examples/raspberryTHETA/icon/stop_icon.png
--------------------------------------------------------------------------------
/examples/raspberryTHETA/icon/timer_icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codetricity/theta-s-api-tests/c60a5fd8862a00579e5dd93cc57c0264a60abced/examples/raspberryTHETA/icon/timer_icon.png
--------------------------------------------------------------------------------
/examples/raspberryTHETA/icon/video.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codetricity/theta-s-api-tests/c60a5fd8862a00579e5dd93cc57c0264a60abced/examples/raspberryTHETA/icon/video.png
--------------------------------------------------------------------------------
/examples/raspberryTHETA/img/ricoh-theta-s.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codetricity/theta-s-api-tests/c60a5fd8862a00579e5dd93cc57c0264a60abced/examples/raspberryTHETA/img/ricoh-theta-s.png
--------------------------------------------------------------------------------
/examples/raspberryTHETA/img/theta_developers.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codetricity/theta-s-api-tests/c60a5fd8862a00579e5dd93cc57c0264a60abced/examples/raspberryTHETA/img/theta_developers.png
--------------------------------------------------------------------------------
/examples/raspberryTHETA/led.py:
--------------------------------------------------------------------------------
1 | import RPi.GPIO as GPIO
2 | import time
3 | from thetapylib import *
4 |
5 | led_pin = 25
6 | switch_pin = 19
7 |
8 | GPIO.setmode(GPIO.BCM)
9 | GPIO.setup(led_pin, GPIO.OUT)
10 | GPIO.setup(switch_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
11 |
12 | led_state = 0
13 |
14 | try:
15 | while True:
16 | #GPIO.output(led_pin, True)
17 | #time.sleep(0.5)
18 | GPIO.output(led_pin, True)
19 | #time.sleep(0.5)
20 | # if not GPIO.input(switch_pin):
21 | # print("Button pressed")
22 | # if led_state == 0:
23 | # led_state = 1
24 | # else:
25 | # led_state = 0
26 | # sid = startSession()
27 | # takePicture(sid)
28 | # time.sleep(0.8)
29 |
30 | # print(led_state)
31 |
32 |
33 | finally:
34 | print("Cleaning up")
35 | GPIO.cleanup()
36 |
--------------------------------------------------------------------------------
/examples/raspberryTHETA/pyTHETA.py:
--------------------------------------------------------------------------------
1 | #! /usr/bin/env python
2 |
3 | import requests, json, pprint
4 | import sys, io
5 | from thetapylib import *
6 | from PIL import Image
7 | import binascii
8 | from StringIO import StringIO
9 | import urllib
10 |
11 | def main():
12 | if len(sys.argv) < 2:
13 | print("\nUsage: $ python pyTHETA.py command")
14 | print("'$ python pyTHETA.py help' for options\n")
15 | # elif len(sys.argv) > 2:
16 | # print("\nUse only one argument.")
17 | else:
18 | help = ["help", "--help", "-h", "-help", "h"]
19 | if sys.argv[1] in help:
20 | print("""
21 | Usage: $ python pyTHETA.py COMMAND
22 | Available commands:
23 | startSession
24 | takePicture SID
25 | info
26 | state
27 | startCapture SID
28 | stopCapture SID
29 | latestFileUri
30 | getLatestImage
31 | listAll NUMBER
32 | getMode SID
33 | getSid
34 | setMode SID_000X mode
35 | """)
36 |
37 | elif sys.argv[1] == "startSession":
38 | sid = startSession()
39 | print ("sessionId: {}".format(sid))
40 | elif sys.argv[1] == "takePicture":
41 | if len(sys.argv) < 3:
42 | print("Usage: pyTHETA.py takePicture SID_000X")
43 | print("Use 'state' to get sessionId")
44 | else:
45 | sid = startSession()
46 | response = takePicture(sid)
47 | pprint.pprint(response)
48 | elif sys.argv[1] == "getMode":
49 | sid = sys.argv[2]
50 | response = getMode(sid)
51 | pprint.pprint(response)
52 | elif sys.argv[1] == "setMode":
53 | if len(sys.argv) < 4:
54 | print("Usage: pyTHETA.py setMode SID_000X mode")
55 | print("Use pyTHETA.py getSid to get sessionID")
56 | print("mode is either image or video")
57 | else:
58 | sid = sys.argv[2]
59 | mode = sys.argv[3]
60 | response = setMode(sid, mode)
61 | pprint.pprint(response)
62 | elif sys.argv[1] == "getSid":
63 | response = getSid()
64 | pprint.pprint(response)
65 |
66 | elif sys.argv[1] == "info":
67 | response = info()
68 | pprint.pprint(response)
69 | elif sys.argv[1] == "state":
70 | response = state()
71 | pprint.pprint(response)
72 | elif sys.argv[1] == "startCapture":
73 | if len(sys.argv) < 3:
74 | print("Usage: pyTHETA.py startCapture SID_000X")
75 | print("Use 'state' to get sessionId")
76 |
77 | else:
78 | sid = sys.argv[2]
79 | response = startCapture(sid)
80 | pprint.pprint(response)
81 | elif sys.argv[1] == "stopCapture":
82 | if len(sys.argv) < 3:
83 | print("Usage: pyTHETA.py stopCapture SID_000X")
84 | print("Use 'state' to get sessionId")
85 | else:
86 | sid = sys.argv[2]
87 | response = stopCapture(sid)
88 | pprint.pprint(response)
89 | elif sys.argv[1] == "latestFileUri":
90 | data = latestFileUri()
91 | print(data)
92 | elif sys.argv[1] == "getLatestImage":
93 | fileUri = latestFileUri()
94 | getImage(fileUri)
95 | elif sys.argv[1] == "listAll":
96 | if len(sys.argv) > 2:
97 | listing_all = listAll(int(sys.argv[2]))
98 | pprint.pprint(listing_all)
99 | else:
100 | listing_all = listAll()
101 | pprint.pprint(listing_all)
102 |
103 |
104 | if __name__ == '__main__':
105 | main()
106 |
--------------------------------------------------------------------------------
/examples/raspberryTHETA/raspberryTHETA.py:
--------------------------------------------------------------------------------
1 | import pygame, sys
2 | import requests
3 | import json
4 | from thetapylib import *
5 | import time
6 |
7 |
8 | WHITE = (255,255,255)
9 | GRAY = (230, 230, 230)
10 | GREEN = (100, 200, 50)
11 | DARK = (64, 64, 64)
12 |
13 |
14 | pygame.init()
15 | SCREENSIZE = (800, 600)
16 | SCREEN = pygame.display.set_mode(SCREENSIZE, pygame.FULLSCREEN)
17 | pygame.display.set_caption("THETA S Unofficial Hacking Guide Example")
18 |
19 | font = pygame.font.Font("fnt/Lato-Bold.ttf", 20)
20 | timer_font = pygame.font.Font("fnt/Lato-Bold.ttf", 48)
21 | clock = pygame.time.Clock()
22 | FPS = 20
23 |
24 | pictureIcon = pygame.image.load("icon/camera_icon2.png")
25 | pictureButton = pictureIcon.get_rect(topleft=(50,120))
26 | pictureButton_text = font.render("Take Picture", True, DARK)
27 | text_box = (pictureButton.left -24, pictureButton.bottom)
28 |
29 | captureStartIcon = pygame.image.load("icon/video.png")
30 | captureStartButton = captureStartIcon.get_rect(topleft=(200, 120))
31 | captureStartButton_text = font.render("Take Video", True, DARK)
32 | captureText_box = (captureStartButton.left - 15, captureStartButton.bottom)
33 |
34 | captureStopIcon = pygame.image.load("icon/stop_icon.png")
35 | captureStopButton = captureStopIcon.get_rect(topleft=(200, 230))
36 | captureStopButton_text = font.render("Stop Video", True, DARK)
37 | captureStopText_box = (captureStopButton.left - 15, captureStopButton.bottom)
38 |
39 | timerIcon = pygame.image.load("icon/timer_icon.png")
40 | timerButton = timerIcon.get_rect(topleft=(50, 230))
41 | timerButton_text = font.render("Timer 15s", True, DARK)
42 | timerText_box = (timerButton.left - 15, timerButton.bottom)
43 |
44 | downloadIcon = pygame.image.load("icon/download.png")
45 | downloadButton = downloadIcon.get_rect(topleft=(50,340))
46 | downloadButton_text = font.render("Download", True, DARK)
47 | downloadText_box = (downloadButton.left -15 , downloadButton.bottom)
48 |
49 | # text placement pos for timer countdown
50 | countdown_pos = (320, 230)
51 |
52 | theta = pygame.image.load("img/ricoh-theta-s.png")
53 | theta_rect = theta.get_rect()
54 | theta_rect.right = SCREENSIZE[0] - 50
55 | theta_rect.top = 60
56 |
57 | developers_logo = pygame.image.load("img/theta_developers.png")
58 |
59 | sid = "SID_0001"
60 | delay_on = False
61 |
62 |
63 | while True:
64 | for event in pygame.event.get():
65 | if event.type == pygame.QUIT:
66 | pygame.quit()
67 | sys.exit()
68 | if event.type == pygame.KEYDOWN:
69 | if event.key == pygame.K_ESCAPE:
70 | pygame.quit()
71 | sys.exit()
72 |
73 | if event.type == pygame.MOUSEBUTTONDOWN:
74 | mouse_pos = pygame.mouse.get_pos()
75 | if pictureButton.collidepoint(mouse_pos):
76 | sid = startSession()
77 | takePicture(sid)
78 | if captureStartButton.collidepoint(mouse_pos):
79 | sid = startSession()
80 | startCapture(sid)
81 | if captureStopButton.collidepoint(mouse_pos):
82 | stopCapture(sid)
83 | if downloadButton.collidepoint(mouse_pos):
84 | fileUri = latestFileUri()
85 | getImage(fileUri)
86 | if timerButton.collidepoint(mouse_pos):
87 | start = time.clock()
88 | delay_on = True
89 |
90 |
91 | ## set up keyboard presses for FLIRC USB controller that
92 | ## maps an remote controller like an Apple controller
93 | ## or any TV remote for easy picture taking.
94 | ## Map the FLICR as follows:
95 | ## p = take picture
96 | ## v = start video capture (or automatic picture if in image mode)
97 | ## s = stop video capture or auto-picture
98 | ## d = delay
99 |
100 | if not delay_on:
101 | if event.type == pygame.KEYDOWN:
102 | if event.key == pygame.K_p:
103 | sid = startSession()
104 | takePicture(sid)
105 | if event.key == pygame.K_v:
106 | print("start video capture")
107 | sid = startSession()
108 | startCapture(sid)
109 | if event.key == pygame.K_s:
110 | print("stop video capture")
111 | stopCapture(sid)
112 | if event.key == pygame.K_d:
113 | start = time.clock()
114 | delay_on = True
115 |
116 | if delay_on:
117 | elapsed = time.clock() - start
118 | if elapsed > 15:
119 | delay_on = False
120 | sid = startSession()
121 | takePicture(sid)
122 |
123 |
124 |
125 |
126 | SCREEN.fill(WHITE) # blank out screen
127 |
128 | # draw take picture button
129 | SCREEN.blit(pictureIcon, pictureButton)
130 | SCREEN.blit(pictureButton_text, text_box)
131 |
132 | # draw take video button
133 | SCREEN.blit(captureStartButton_text, captureText_box)
134 | SCREEN.blit(captureStartIcon, captureStartButton)
135 |
136 | # draw stop video button
137 | SCREEN.blit(captureStopButton_text, captureStopText_box)
138 | SCREEN.blit(captureStopIcon, captureStopButton)
139 |
140 | # draw timer button
141 | SCREEN.blit(timerButton_text, timerText_box)
142 | SCREEN.blit(timerIcon, timerButton)
143 |
144 |
145 | # draw download button
146 | SCREEN.blit(downloadButton_text, downloadText_box)
147 | SCREEN.blit(downloadIcon, downloadButton)
148 |
149 | # draw countdown shutter timer
150 | if delay_on: # only display if timer button pressed
151 | countdown_text = timer_font.render("Timer: " + str(int(15 - elapsed)), True, DARK)
152 | SCREEN.blit(countdown_text, countdown_pos)
153 |
154 | # decorations
155 | SCREEN.blit(theta, theta_rect)
156 | SCREEN.blit(developers_logo, (10, 10))
157 |
158 | clock.tick(FPS)
159 | pygame.display.update()
160 |
--------------------------------------------------------------------------------
/examples/raspberryTHETA/raspberry_led.py:
--------------------------------------------------------------------------------
1 | import pygame, sys
2 | import requests #better alternative to urllib2
3 | import json
4 | # customized python library for THETA S
5 | from thetapylib import *
6 | import time
7 | # for Raspberry Pi input output pins
8 | import RPi.GPIO as GPIO
9 |
10 | # test LEDs to show camera state such as timer.
11 | # Set to False if you are not using LEDs
12 | LED = True
13 |
14 | if LED:
15 | GPIO.setmode(GPIO.BCM)
16 | led_01 = 12
17 | led_02 = 18
18 | led_03 = 23
19 | led_04 = 25
20 | leds = [led_01, led_02, led_03, led_04]
21 | for led in leds:
22 | GPIO.setup(led, GPIO.OUT)
23 | GPIO.output(led, False)
24 |
25 | WHITE = (255,255,255)
26 | GRAY = (230, 230, 230)
27 | GREEN = (100, 200, 50)
28 | DARK = (64, 64, 64)
29 |
30 |
31 | pygame.init()
32 | SCREENSIZE = (800, 600)
33 | SCREEN = pygame.display.set_mode(SCREENSIZE, pygame.FULLSCREEN)
34 | pygame.display.set_caption("THETA S Unofficial Hacking Guide Example")
35 |
36 | font = pygame.font.Font("fnt/Lato-Bold.ttf", 20)
37 | timer_font = pygame.font.Font("fnt/Lato-Bold.ttf", 48)
38 | clock = pygame.time.Clock()
39 | FPS = 20
40 |
41 | pictureIcon = pygame.image.load("icon/camera_icon2.png")
42 | pictureButton = pictureIcon.get_rect(topleft=(50,120))
43 | pictureButton_text = font.render("Take Picture", True, DARK)
44 | text_box = (pictureButton.left -24, pictureButton.bottom)
45 |
46 | captureStartIcon = pygame.image.load("icon/video.png")
47 | captureStartButton = captureStartIcon.get_rect(topleft=(200, 120))
48 | captureStartButton_text = font.render("Take Video", True, DARK)
49 | captureText_box = (captureStartButton.left - 15, captureStartButton.bottom)
50 |
51 | captureStopIcon = pygame.image.load("icon/stop_icon.png")
52 | captureStopButton = captureStopIcon.get_rect(topleft=(200, 230))
53 | captureStopButton_text = font.render("Stop Video", True, DARK)
54 | captureStopText_box = (captureStopButton.left - 15, captureStopButton.bottom)
55 |
56 | timerIcon = pygame.image.load("icon/timer_icon.png")
57 | timerButton = timerIcon.get_rect(topleft=(50, 230))
58 | timerButton_text = font.render("Timer 15s", True, DARK)
59 | timerText_box = (timerButton.left - 15, timerButton.bottom)
60 |
61 | downloadIcon = pygame.image.load("icon/download.png")
62 | downloadButton = downloadIcon.get_rect(topleft=(50,340))
63 | downloadButton_text = font.render("Download", True, DARK)
64 | downloadText_box = (downloadButton.left -15 , downloadButton.bottom)
65 |
66 | # text placement pos for timer countdown
67 | countdown_pos = (320, 230)
68 |
69 | theta = pygame.image.load("img/ricoh-theta-s.png")
70 | theta_rect = theta.get_rect()
71 | theta_rect.right = SCREENSIZE[0] - 50
72 | theta_rect.top = 60
73 |
74 | developers_logo = pygame.image.load("img/theta_developers.png")
75 |
76 | sid = "SID_0001"
77 | delay_on = False
78 |
79 | max_step = 20
80 | wait_step = max_step
81 | # captureMode will either be image or _video
82 | mode = "image"
83 | startSession()
84 | sid = getSid()
85 | mode = getMode(sid)
86 | print(mode)
87 |
88 |
89 | while True:
90 | if LED:
91 | # power is on
92 | GPIO.output(led_01, True)
93 | # get camera mode, video or image
94 | if mode == "image":
95 | GPIO.output(led_02, True)
96 | GPIO.output(led_03, False)
97 | if mode == "_video":
98 | GPIO.output(led_03, True)
99 | GPIO.output(led_02, False)
100 | if wait_step > 0:
101 | wait_step -= 1
102 | else:
103 | sid = getSid()
104 | mode = getMode(sid)
105 | wait_step = max_step
106 |
107 | for event in pygame.event.get():
108 | if event.type == pygame.QUIT:
109 | pygame.quit()
110 | sys.exit()
111 | if event.type == pygame.KEYDOWN:
112 | if event.key == pygame.K_ESCAPE:
113 | GPIO.cleanup()
114 | pygame.quit()
115 | sys.exit()
116 |
117 |
118 | if event.type == pygame.MOUSEBUTTONDOWN:
119 | mouse_pos = pygame.mouse.get_pos()
120 | if pictureButton.collidepoint(mouse_pos):
121 | sid = startSession()
122 | takePicture(sid)
123 | if captureStartButton.collidepoint(mouse_pos):
124 | sid = startSession()
125 | startCapture(sid)
126 | if captureStopButton.collidepoint(mouse_pos):
127 | stopCapture(sid)
128 | if downloadButton.collidepoint(mouse_pos):
129 | fileUri = latestFileUri()
130 | getImage(fileUri)
131 | if timerButton.collidepoint(mouse_pos):
132 | start = time.clock()
133 | delay_on = True
134 |
135 |
136 | ## set up keyboard presses for FLIRC USB controller that
137 | ## maps an remote controller like an Apple controller
138 | ## or any TV remote for easy picture taking.
139 | ## Map the FLICR as follows:
140 | ## p = take picture
141 | ## v = start video capture (or automatic picture if in image mode)
142 | ## s = stop video capture or auto-picture
143 | ## d = delay
144 | ## m = change capture mode
145 |
146 | if not delay_on:
147 | if event.type == pygame.KEYDOWN:
148 | if event.key == pygame.K_p:
149 | sid = startSession()
150 | takePicture(sid)
151 | if event.key == pygame.K_v:
152 | print("start video capture")
153 | sid = startSession()
154 | startCapture(sid)
155 | if event.key == pygame.K_s:
156 | print("stop video capture")
157 | stopCapture(sid)
158 | if event.key == pygame.K_d:
159 | start = time.clock()
160 | delay_on = True
161 | if event.key == pygame.K_m:
162 | # change capture mode
163 | pass
164 |
165 |
166 | if delay_on:
167 | elapsed = time.clock() - start
168 | if LED:
169 | GPIO.output(led_04, True)
170 | if elapsed > 15:
171 | if LED:
172 | GPIO.output(led_04, False)
173 | delay_on = False
174 | sid = startSession()
175 | takePicture(sid)
176 |
177 |
178 |
179 |
180 | SCREEN.fill(WHITE) # blank out screen
181 |
182 | # draw take picture button
183 | SCREEN.blit(pictureIcon, pictureButton)
184 | SCREEN.blit(pictureButton_text, text_box)
185 |
186 | # draw take video button
187 | SCREEN.blit(captureStartButton_text, captureText_box)
188 | SCREEN.blit(captureStartIcon, captureStartButton)
189 |
190 | # draw stop video button
191 | SCREEN.blit(captureStopButton_text, captureStopText_box)
192 | SCREEN.blit(captureStopIcon, captureStopButton)
193 |
194 | # draw timer button
195 | SCREEN.blit(timerButton_text, timerText_box)
196 | SCREEN.blit(timerIcon, timerButton)
197 |
198 |
199 | # draw download button
200 | SCREEN.blit(downloadButton_text, downloadText_box)
201 | SCREEN.blit(downloadIcon, downloadButton)
202 |
203 | # draw countdown shutter timer
204 | if delay_on: # only display if timer button pressed
205 | countdown_text = timer_font.render("Timer: " + str(int(15 - elapsed)), True, DARK)
206 | SCREEN.blit(countdown_text, countdown_pos)
207 |
208 | # decorations
209 | SCREEN.blit(theta, theta_rect)
210 | SCREEN.blit(developers_logo, (10, 10))
211 |
212 | clock.tick(FPS)
213 | pygame.display.update()
214 |
--------------------------------------------------------------------------------
/examples/raspberryTHETA/thetapylib.py:
--------------------------------------------------------------------------------
1 | """
2 | Example library for RICOH THETA S hacking with Python. The new
3 | API is compliant with the Open Spherical Camera specification.
4 | This is intended to show how the THETA S API works. It is
5 | not intended for use in your program. There is no error
6 | checking and this example library only handles a handful
7 | of commands.
8 |
9 |
10 | There are three example programs that use this library.
11 | At the top of your Python script, use
12 |
13 | from thetapylib import *
14 |
15 | After you import the library, you can use the commands like this:
16 |
17 | state()
18 |
19 | That will return the state of the camera, which is great to
20 | get the sessionId.
21 |
22 | You can also get the sessionId when you start a new session:
23 |
24 | startSession()
25 |
26 | In fact, the startSession() function will return the
27 | sessionId.
28 |
29 | Example use of the library with Pygame to detect the
30 | button press.
31 |
32 | if event.type == pygame.MOUSEBUTTONDOWN:
33 | mouse_pos = pygame.mouse.get_pos()
34 | if pictureButton.collidepoint(mouse_pos):
35 | sid = startSession()
36 | takePicture(sid)
37 | if captureStartButton.collidepoint(mouse_pos):
38 | sid = startSession()
39 | startCapture(sid)
40 | if captureStopButton.collidepoint(mouse_pos):
41 | stopCapture(sid)
42 |
43 | Example use of library from the command line:
44 |
45 | if sys.argv[1] == "startCapture":
46 | if len(sys.argv) < 3:
47 | print("Usage: pyTHETA.py startCapture SID_000X")
48 | print("Use 'state' to get sessionId")
49 |
50 | else:
51 | sid = sys.argv[2]
52 | response = startCapture(sid)
53 | pprint.pprint(response)
54 | elif sys.argv[1] == "stopCapture":
55 | if len(sys.argv) < 3:
56 | print("Usage: pyTHETA.py stopCapture SID_000X")
57 | print("Use 'state' to get sessionId")
58 | else:
59 | sid = sys.argv[2]
60 | response = stopCapture(sid)
61 | pprint.pprint(response)
62 |
63 | """
64 | import requests
65 | import json
66 | # import pprint # for printing out test data
67 | # from PIL import Image
68 | # from StringIO import StringIO
69 |
70 | def request(url_request):
71 | """
72 | Generate the URI to send to the THETA S. The THETA IP address is
73 | 192.168.1.1
74 | All calls start with /osc/
75 | """
76 | url_base = "http://192.168.1.1/osc/"
77 | url = url_base + url_request
78 | return url
79 |
80 | def startSession():
81 | """
82 | Start a new session. Grab the sessionId number and
83 | return it.
84 | You'll need the sessionId to take a video or image.
85 | """
86 | url = request("commands/execute")
87 | body = json.dumps({"name": "camera.startSession",
88 | "parameters": {}
89 | })
90 | try:
91 | req = requests.post(url, data=body)
92 | except requests.exceptions.ConnectionError as e:
93 | sid = None
94 | return sid
95 |
96 | if req.status_code == 200:
97 | response = req.json()
98 | sid = (response["results"]["sessionId"])
99 | else:
100 | sid = None
101 | return sid
102 |
103 | def takePicture(sid):
104 | """
105 | Take a still image. The sessionId is either taken from
106 | startSession or from state. You can change the mode
107 | from video to image with captureMode in the options.
108 | """
109 | if sid == None:
110 | response = None
111 | return response
112 | url = request("commands/execute")
113 | body = json.dumps({"name": "camera.takePicture",
114 | "parameters": {
115 | "sessionId": sid
116 | }
117 | })
118 | try:
119 | req = requests.post(url, data=body)
120 | except requests.exceptions.ConnectionError as e:
121 | return e
122 | if req.status_code == 200:
123 | response = req.json()
124 | else:
125 | response = "HTTP error"
126 | return response
127 |
128 | def setMode(sid, mode):
129 | """
130 | Change mode between image and _video.
131 | The sessionId is either taken from
132 | startSession or from state.
133 | See
134 | https://developers.theta360.com/en/docs/v2/api_reference/options/capture_mode.html
135 | """
136 | if sid == None:
137 | response = None
138 | return response
139 | url = request("commands/execute")
140 | body = json.dumps({"name": "camera.setOptions",
141 | "parameters": {
142 | "sessionId": sid,
143 | "options": {
144 | "captureMode": mode,
145 | }
146 | }
147 | })
148 | try:
149 | req = requests.post(url, data=body)
150 | except requests.exceptions.ConnectionError as e:
151 | return e
152 | if req.status_code == 200:
153 | response = req.json()
154 | else:
155 | response = "HTTP error"
156 | return response
157 |
158 |
159 | def info():
160 | """
161 | Get basic information ont he camera. Note that this is a GET call
162 | and not a POST. Most of the calls are POST.
163 | """
164 | url = request("info")
165 | try:
166 | req = requests.get(url)
167 | except requests.exceptions.ConnectionError as e:
168 | return e
169 | print(req.status_code)
170 | if req.status_code == 200:
171 | response = req.json()
172 | else:
173 | response = "HTTP error"
174 | return response
175 |
176 |
177 |
178 |
179 | def state():
180 | """
181 | Get the state of the camera, which will include the sessionsId and also the
182 | latestFileUri if you've just taken a picture.
183 | """
184 | url = request("state")
185 | try:
186 | req = requests.post(url)
187 | except requests.exceptions.ConnectionError as e:
188 | return e
189 | if req.status_code == 200:
190 | response = req.json()
191 | else:
192 | response = "HTTP error"
193 | return response
194 |
195 | def getSid():
196 | """
197 | Get the state of the camera, and return the sessionsId.
198 | """
199 | url = request("state")
200 | try:
201 | req = requests.post(url)
202 | except requests.exceptions.ConnectionError as e:
203 | return e
204 | if req.status_code == 200:
205 | response = req.json()
206 | sid = response["state"]["sessionId"]
207 | else:
208 | sid = "HTTP error"
209 | return sid
210 |
211 |
212 | def startCapture(sid):
213 | """
214 | Begin video capture if the captureMode is _video. If the
215 | captureMode is set to image, the camera will take multiple
216 | still images. The captureMode can be set in the options.
217 | Note that this will not work with streaming video using the
218 | HDMI or USB cable.
219 | """
220 | url = request("commands/execute")
221 | body = json.dumps({"name": "camera._startCapture",
222 | "parameters": {
223 | "sessionId": sid
224 | }
225 | })
226 | try:
227 | req = requests.post(url, data=body)
228 | except requests.exceptions.ConnectionError as e:
229 | return e
230 | if req.status_code == 200:
231 | response = req.json()
232 | else:
233 | response = "HTTP error"
234 | return response
235 |
236 |
237 | def stopCapture(sid):
238 | """
239 | Stop video capture. If in image mode, will stop
240 | automatic image taking.
241 | """
242 | url = request("commands/execute")
243 | body = json.dumps({"name": "camera._stopCapture",
244 | "parameters": {
245 | "sessionId": sid
246 | }
247 | })
248 | try:
249 | req = requests.post(url, data=body)
250 | except requests.exceptions.ConnectionError as e:
251 | return e
252 | if req.status_code == 200:
253 | response = req.json()
254 | else:
255 | response = "HTTP error"
256 | return response
257 |
258 | def latestFileUri():
259 | """
260 | This will only work if you've just taken a picture. The state
261 | will include the attribute latestFileUri. You need this to
262 | transfer the file from the camera to your computer or phone.
263 | """
264 | try:
265 | state_data = state()["state"]
266 | except:
267 | return False
268 | latestFileUri = state_data["_latestFileUri"]
269 | return latestFileUri
270 |
271 |
272 |
273 | def getImage(fileUri, imageType="image"):
274 | """
275 | Transfer the file from the camera to computer and save the
276 | binary data to local storage. This works, but is clunky.
277 | There are easier ways to do this. The __type parameter
278 | can be set to "thumb" for a thumbnail or "image" for the
279 | full-size image. The default is "image".
280 | """
281 | if fileUri:
282 | url = request("commands/execute")
283 | body = json.dumps({"name": "camera.getImage",
284 | "parameters": {
285 | "fileUri": fileUri,
286 | "_type": imageType
287 | }
288 | })
289 | fileName = fileUri.split("/")[1]
290 | print(fileName)
291 | with open(fileName, 'wb') as handle:
292 | response = requests.post(url, data=body, stream=True)
293 | for block in response.iter_content(1024):
294 | handle.write(block)
295 |
296 | def getMode(sid):
297 | url = request("commands/execute")
298 | body = json.dumps({"name": "camera.getOptions",
299 | "parameters": {
300 | "sessionId": sid,
301 | "optionNames": [
302 | "captureMode"]
303 | }
304 | })
305 | try:
306 | req = requests.post(url, data=body)
307 | except requests.exceptions.ConnectionError as e:
308 | return e
309 | if req.status_code == 200:
310 | response = req.json()
311 | mode = response["results"]["options"]["captureMode"]
312 | else:
313 | mode = "HTTP error"
314 | return mode
315 |
316 |
317 |
318 | def listAll(entryCount = 3, detail = False, sortType = "newest", ):
319 | """
320 | entryCount:
321 | Integer No. of still images and video files to be acquired
322 | detail:
323 | Boolean (Optional) Whether or not file details are acquired
324 | true is acquired by default. Only values that can be acquired
325 | when false is specified are "name", "uri", "size" and "dateTime"
326 | sort:
327 | String (Optional) Specify the sort order
328 | newest (dateTime descending order)/ oldest (dateTime ascending order)
329 | Default is newest
330 | """
331 | url = request("commands/execute")
332 | body = json.dumps({"name": "camera._listAll",
333 | "parameters": {
334 | "entryCount": entryCount,
335 | "detail": detail,
336 | "sort": sortType
337 | }
338 | })
339 | try:
340 | req = requests.post(url, data=body)
341 | except requests.exceptions.ConnectionError as e:
342 | return e
343 | if req.status_code == 200:
344 | response = req.json()
345 | else:
346 | response = "HTTP error"
347 | return response
348 |
--------------------------------------------------------------------------------
/examples/thetaPythonDesktop.py:
--------------------------------------------------------------------------------
1 | import pygame, sys
2 | import requests
3 | import json
4 | from thetaPythonTest import startSession, takePicture
5 |
6 | WHITE = (255,255,255)
7 | GRAY = (230, 230, 230)
8 | GREEN = (100, 200, 50)
9 | DARK = (64, 64, 64)
10 |
11 |
12 | pygame.init()
13 | SCREENSIZE = (800, 600)
14 | SCREEN = pygame.display.set_mode(SCREENSIZE)
15 | pygame.display.set_caption("THETA S Unofficial Hacking Guide Example")
16 |
17 | button = pygame.Rect(100, 120, 140, 80)
18 | font = pygame.font.Font("fnt/Lato-Thin.ttf", 20)
19 | button_text = font.render("Take Picture", True, DARK)
20 | text_box = (button.left + 15, button.top + 23)
21 |
22 | theta = pygame.image.load("img/ricoh-theta-s.png")
23 | theta_rect = theta.get_rect()
24 | theta_rect.right = SCREENSIZE[0] - 50
25 | theta_rect.top = 60
26 |
27 | developers_logo = pygame.image.load("img/theta_developers.png")
28 |
29 | while True:
30 | for event in pygame.event.get():
31 | if event.type == pygame.QUIT:
32 | pygame.quit()
33 | sys.exit()
34 | if event.type == pygame.MOUSEBUTTONDOWN:
35 | mouse_pos = pygame.mouse.get_pos()
36 | if button.collidepoint(mouse_pos):
37 | sid = startSession()
38 | takePicture(sid)
39 |
40 | SCREEN.fill(WHITE)
41 | pygame.draw.rect(SCREEN,GRAY, button)
42 | pygame.draw.rect(SCREEN, DARK, button, 1)
43 | SCREEN.blit(button_text, text_box)
44 | SCREEN.blit(theta, theta_rect)
45 | SCREEN.blit(developers_logo, (10, 10))
46 |
47 | pygame.display.update()
48 |
--------------------------------------------------------------------------------
/examples/thetaPythonTest.py:
--------------------------------------------------------------------------------
1 | import requests, json, pprint
2 |
3 |
4 |
5 | def request(url_request):
6 | url_base = "http://192.168.1.1/osc/"
7 | url = url_base + url_request
8 | return url
9 |
10 | def basic_info():
11 | protocols = [
12 | "state",
13 | "info"
14 | ]
15 | for protocol in protocols:
16 | print(60 * "=")
17 | print(protocol + " - Testing RICOH THETA API v2\n")
18 | url = request(protocol)
19 | if protocol == "info":
20 | req = requests.get(url)
21 | else:
22 | req = requests.post(url)
23 | pprint.pprint(req.json())
24 |
25 | def startSession():
26 | url = request("commands/execute")
27 | body = json.dumps({"name": "camera.startSession",
28 | "parameters": {}
29 | })
30 | req = requests.post(url, data=body)
31 | response = req.json()
32 | print(60 * "=")
33 | print("startSession - Testing RICOH THETA API v2\n")
34 | pprint.pprint(response)
35 | sid = (response["results"]["sessionId"])
36 | print sid
37 | return sid
38 |
39 | def takePicture(sid):
40 | url = request("commands/execute")
41 | body = json.dumps({"name": "camera.takePicture",
42 | "parameters": {
43 | "sessionId": sid
44 | }
45 | })
46 | req = requests.post(url, data=body)
47 | response = req.json()
48 | print(60 * "=")
49 | print("takePicture - Testing RICOH THETA API v2\n")
50 | pprint.pprint(response)
51 |
52 | def main():
53 | basic_info()
54 | sid = startSession() #grab session ID
55 | takePicture(sid) #still image
56 |
57 | if __name__ == '__main__':
58 | main()
59 |
60 | # url = base_url + "commands/execute"
61 |
62 | #req = requests.post(url, data=body)
63 |
--------------------------------------------------------------------------------
/pyTHETA.py:
--------------------------------------------------------------------------------
1 | #! /usr/bin/env python
2 |
3 | import requests, json, pprint
4 | import sys, io
5 | from thetapylib import *
6 |
7 | def main():
8 | if len(sys.argv) < 2:
9 | print("\nUsage: $ python pyTHETA.py command")
10 | print("'$ python pyTHETA.py help' for options\n")
11 | # elif len(sys.argv) > 2:
12 | # print("\nUse only one argument.")
13 | else:
14 | help = ["help", "--help", "-h", "-help", "h"]
15 | if sys.argv[1] in help:
16 | print("""
17 | Usage: $ python pyTHETA.py COMMAND
18 | Available commands:
19 | startSession
20 | takePicture SID
21 | info
22 | state
23 | startCapture SID
24 | stopCapture SID
25 | latestFileUri
26 | getLatestImage
27 | listAll NUMBER
28 | getMode SID
29 | getSid
30 | setMode SID_000X mode
31 | """)
32 |
33 | elif sys.argv[1] == "startSession":
34 | sid = startSession()
35 | print ("sessionId: {}".format(sid))
36 | elif sys.argv[1] == "takePicture":
37 | if len(sys.argv) < 3:
38 | print("Usage: pyTHETA.py takePicture SID_000X")
39 | print("Use 'state' to get sessionId")
40 | else:
41 | sid = startSession()
42 | response = takePicture(sid)
43 | pprint.pprint(response)
44 | elif sys.argv[1] == "getMode":
45 | sid = sys.argv[2]
46 | response = getMode(sid)
47 | pprint.pprint(response)
48 | elif sys.argv[1] == "setMode":
49 | if len(sys.argv) < 4:
50 | print("Usage: pyTHETA.py setMode SID_000X mode")
51 | print("Use pyTHETA.py getSid to get sessionID")
52 | print("mode is either image or video")
53 | else:
54 | sid = sys.argv[2]
55 | mode = sys.argv[3]
56 | response = setMode(sid, mode)
57 | pprint.pprint(response)
58 | elif sys.argv[1] == "getSid":
59 | response = getSid()
60 | pprint.pprint(response)
61 |
62 | elif sys.argv[1] == "info":
63 | response = info()
64 | pprint.pprint(response)
65 | elif sys.argv[1] == "state":
66 | response = state()
67 | pprint.pprint(response)
68 | elif sys.argv[1] == "startCapture":
69 | if len(sys.argv) < 3:
70 | print("Usage: pyTHETA.py startCapture SID_000X")
71 | print("Use 'state' to get sessionId")
72 |
73 | else:
74 | sid = sys.argv[2]
75 | response = startCapture(sid)
76 | pprint.pprint(response)
77 | elif sys.argv[1] == "stopCapture":
78 | if len(sys.argv) < 3:
79 | print("Usage: pyTHETA.py stopCapture SID_000X")
80 | print("Use 'state' to get sessionId")
81 | else:
82 | sid = sys.argv[2]
83 | response = stopCapture(sid)
84 | pprint.pprint(response)
85 | elif sys.argv[1] == "latestFileUri":
86 | data = latestFileUri()
87 | print(data)
88 | elif sys.argv[1] == "getLatestImage":
89 | fileUri = latestFileUri()
90 | getImage(fileUri)
91 | elif sys.argv[1] == "listAll":
92 | if len(sys.argv) > 2:
93 | listing_all = listAll(int(sys.argv[2]))
94 | pprint.pprint(listing_all)
95 | else:
96 | listing_all = listAll()
97 | pprint.pprint(listing_all)
98 |
99 |
100 | if __name__ == '__main__':
101 | main()
102 |
--------------------------------------------------------------------------------
/thetapylib.py:
--------------------------------------------------------------------------------
1 | """
2 | Example library for RICOH THETA S hacking with Python. The new
3 | API is compliant with the Open Spherical Camera specification.
4 | This is intended to show how the THETA S API works. It is
5 | not intended for use in your program. There is no error
6 | checking and this example library only handles a handful
7 | of commands.
8 |
9 |
10 | There are three example programs that use this library.
11 | At the top of your Python script, use
12 |
13 | from thetapylib import *
14 |
15 | After you import the library, you can use the commands like this:
16 |
17 | state()
18 |
19 | That will return the state of the camera, which is great to
20 | get the sessionId.
21 |
22 | You can also get the sessionId when you start a new session:
23 |
24 | startSession()
25 |
26 | In fact, the startSession() function will return the
27 | sessionId.
28 |
29 | Example use of the library with Pygame to detect the
30 | button press.
31 |
32 | if event.type == pygame.MOUSEBUTTONDOWN:
33 | mouse_pos = pygame.mouse.get_pos()
34 | if pictureButton.collidepoint(mouse_pos):
35 | sid = startSession()
36 | takePicture(sid)
37 | if captureStartButton.collidepoint(mouse_pos):
38 | sid = startSession()
39 | startCapture(sid)
40 | if captureStopButton.collidepoint(mouse_pos):
41 | stopCapture(sid)
42 |
43 | Example use of library from the command line:
44 |
45 | if sys.argv[1] == "startCapture":
46 | if len(sys.argv) < 3:
47 | print("Usage: pyTHETA.py startCapture SID_000X")
48 | print("Use 'state' to get sessionId")
49 |
50 | else:
51 | sid = sys.argv[2]
52 | response = startCapture(sid)
53 | pprint.pprint(response)
54 | elif sys.argv[1] == "stopCapture":
55 | if len(sys.argv) < 3:
56 | print("Usage: pyTHETA.py stopCapture SID_000X")
57 | print("Use 'state' to get sessionId")
58 | else:
59 | sid = sys.argv[2]
60 | response = stopCapture(sid)
61 | pprint.pprint(response)
62 |
63 | """
64 | import requests
65 | import json
66 | # import pprint # for printing out test data
67 | # from PIL import Image
68 | # from StringIO import StringIO
69 |
70 | def request(url_request):
71 | """
72 | Generate the URI to send to the THETA S. The THETA IP address is
73 | 192.168.1.1
74 | All calls start with /osc/
75 | """
76 | url_base = "http://192.168.1.1/osc/"
77 | url = url_base + url_request
78 | return url
79 |
80 | def startSession():
81 | """
82 | Start a new session. Grab the sessionId number and
83 | return it.
84 | You'll need the sessionId to take a video or image.
85 | """
86 | url = request("commands/execute")
87 | body = json.dumps({"name": "camera.startSession",
88 | "parameters": {}
89 | })
90 | try:
91 | req = requests.post(url, data=body)
92 | except requests.exceptions.ConnectionError as e:
93 | sid = None
94 | return sid
95 |
96 | if req.status_code == 200:
97 | response = req.json()
98 | sid = (response["results"]["sessionId"])
99 | else:
100 | sid = None
101 | return sid
102 |
103 | def takePicture(sid):
104 | """
105 | Take a still image. The sessionId is either taken from
106 | startSession or from state. You can change the mode
107 | from video to image with captureMode in the options.
108 | """
109 | if sid == None:
110 | response = None
111 | return response
112 | url = request("commands/execute")
113 | body = json.dumps({"name": "camera.takePicture",
114 | "parameters": {
115 | "sessionId": sid
116 | }
117 | })
118 | try:
119 | req = requests.post(url, data=body)
120 | except requests.exceptions.ConnectionError as e:
121 | return e
122 | if req.status_code == 200:
123 | response = req.json()
124 | else:
125 | response = "HTTP error"
126 | return response
127 |
128 | def setMode(sid, mode):
129 | """
130 | Change mode between image and _video.
131 | The sessionId is either taken from
132 | startSession or from state.
133 | See
134 | https://developers.theta360.com/en/docs/v2/api_reference/options/capture_mode.html
135 | """
136 | if sid == None:
137 | response = None
138 | return response
139 | url = request("commands/execute")
140 | body = json.dumps({"name": "camera.setOptions",
141 | "parameters": {
142 | "sessionId": sid,
143 | "options": {
144 | "captureMode": mode,
145 | }
146 | }
147 | })
148 | try:
149 | req = requests.post(url, data=body)
150 | except requests.exceptions.ConnectionError as e:
151 | return e
152 | if req.status_code == 200:
153 | response = req.json()
154 | else:
155 | response = "HTTP error"
156 | return response
157 |
158 |
159 | def info():
160 | """
161 | Get basic information ont he camera. Note that this is a GET call
162 | and not a POST. Most of the calls are POST.
163 | """
164 | url = request("info")
165 | try:
166 | req = requests.get(url)
167 | except requests.exceptions.ConnectionError as e:
168 | return e
169 | print(req.status_code)
170 | if req.status_code == 200:
171 | response = req.json()
172 | else:
173 | response = "HTTP error"
174 | return response
175 |
176 |
177 |
178 |
179 | def state():
180 | """
181 | Get the state of the camera, which will include the sessionsId and also the
182 | latestFileUri if you've just taken a picture.
183 | """
184 | url = request("state")
185 | try:
186 | req = requests.post(url)
187 | except requests.exceptions.ConnectionError as e:
188 | return e
189 | if req.status_code == 200:
190 | response = req.json()
191 | else:
192 | response = "HTTP error"
193 | return response
194 |
195 | def getSid():
196 | """
197 | Get the state of the camera, and return the sessionsId.
198 | """
199 | url = request("state")
200 | try:
201 | req = requests.post(url)
202 | except requests.exceptions.ConnectionError as e:
203 | return e
204 | if req.status_code == 200:
205 | response = req.json()
206 | sid = response["state"]["sessionId"]
207 | else:
208 | sid = "HTTP error"
209 | return sid
210 |
211 |
212 | def startCapture(sid):
213 | """
214 | Begin video capture if the captureMode is _video. If the
215 | captureMode is set to image, the camera will take multiple
216 | still images. The captureMode can be set in the options.
217 | Note that this will not work with streaming video using the
218 | HDMI or USB cable.
219 | """
220 | url = request("commands/execute")
221 | body = json.dumps({"name": "camera._startCapture",
222 | "parameters": {
223 | "sessionId": sid
224 | }
225 | })
226 | try:
227 | req = requests.post(url, data=body)
228 | except requests.exceptions.ConnectionError as e:
229 | return e
230 | if req.status_code == 200:
231 | response = req.json()
232 | else:
233 | response = "HTTP error"
234 | return response
235 |
236 |
237 | def stopCapture(sid):
238 | """
239 | Stop video capture. If in image mode, will stop
240 | automatic image taking.
241 | """
242 | url = request("commands/execute")
243 | body = json.dumps({"name": "camera._stopCapture",
244 | "parameters": {
245 | "sessionId": sid
246 | }
247 | })
248 | try:
249 | req = requests.post(url, data=body)
250 | except requests.exceptions.ConnectionError as e:
251 | return e
252 | if req.status_code == 200:
253 | response = req.json()
254 | else:
255 | response = "HTTP error"
256 | return response
257 |
258 | def latestFileUri():
259 | """
260 | This will only work if you've just taken a picture. The state
261 | will include the attribute latestFileUri. You need this to
262 | transfer the file from the camera to your computer or phone.
263 | """
264 | try:
265 | state_data = state()["state"]
266 | except:
267 | return False
268 | latestFileUri = state_data["_latestFileUri"]
269 | return latestFileUri
270 |
271 |
272 |
273 | def getImage(fileUri, imageType="image"):
274 | """
275 | Transfer the file from the camera to computer and save the
276 | binary data to local storage. This works, but is clunky.
277 | There are easier ways to do this. The __type parameter
278 | can be set to "thumb" for a thumbnail or "image" for the
279 | full-size image. The default is "image".
280 | """
281 | if fileUri:
282 | url = request("commands/execute")
283 | body = json.dumps({"name": "camera.getImage",
284 | "parameters": {
285 | "fileUri": fileUri,
286 | "_type": imageType
287 | }
288 | })
289 | fileName = fileUri.split("/")[1]
290 | print(fileName)
291 | with open(fileName, 'wb') as handle:
292 | response = requests.post(url, data=body, stream=True)
293 | for block in response.iter_content(1024):
294 | handle.write(block)
295 |
296 | def getMode(sid):
297 | url = request("commands/execute")
298 | body = json.dumps({"name": "camera.getOptions",
299 | "parameters": {
300 | "sessionId": sid,
301 | "optionNames": [
302 | "captureMode"]
303 | }
304 | })
305 | try:
306 | req = requests.post(url, data=body)
307 | except requests.exceptions.ConnectionError as e:
308 | return e
309 | if req.status_code == 200:
310 | response = req.json()
311 | mode = response["results"]["options"]["captureMode"]
312 | else:
313 | mode = "HTTP error"
314 | return mode
315 |
316 |
317 |
318 | def listAll(entryCount = 3, detail = False, sortType = "newest", ):
319 | """
320 | entryCount:
321 | Integer No. of still images and video files to be acquired
322 | detail:
323 | Boolean (Optional) Whether or not file details are acquired
324 | true is acquired by default. Only values that can be acquired
325 | when false is specified are "name", "uri", "size" and "dateTime"
326 | sort:
327 | String (Optional) Specify the sort order
328 | newest (dateTime descending order)/ oldest (dateTime ascending order)
329 | Default is newest
330 | """
331 | url = request("commands/execute")
332 | body = json.dumps({"name": "camera._listAll",
333 | "parameters": {
334 | "entryCount": entryCount,
335 | "detail": detail,
336 | "sort": sortType
337 | }
338 | })
339 | try:
340 | req = requests.post(url, data=body)
341 | except requests.exceptions.ConnectionError as e:
342 | return e
343 | if req.status_code == 200:
344 | response = req.json()
345 | else:
346 | response = "HTTP error"
347 | return response
348 |
--------------------------------------------------------------------------------