forked from tridge/SonyCamera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_capture.py
189 lines (163 loc) · 8.32 KB
/
test_capture.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python
'''
Simple script to test Sony camera WiFi interface. Tested on a QX10.
Started by Andrew Tridgell October 2014
Released under GNU GPLv3 or later
'''
import requests, json, time, sys, socket
import xml.etree.ElementTree as ET
from optparse import OptionParser
# parse command line options
parser = OptionParser()
parser.add_option("--camera", help="camera IP", default="http://")
(opts, args) = parser.parse_args()
# SSDP
def find_camera():
'''Send an SSDP request to get the camera URL'''
SSDP_ADDR = "239.255.255.250";
SSDP_PORT = 1900;
SSDP_MX = 1;
SSDP_ST = "urn:schemas-sony-com:service:ScalarWebAPI:1";
ssdpRequest = "M-SEARCH * HTTP/1.1\r\n" + \
"HOST: %s:%d\r\n" % (SSDP_ADDR, SSDP_PORT) + \
"MAN: \"ssdp:discover\"\r\n" + \
"MX: %d\r\n" % (SSDP_MX, ) + \
"ST: %s\r\n" % (SSDP_ST, ) + "\r\n";
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(ssdpRequest, (SSDP_ADDR, SSDP_PORT))
result = sock.recv(1000)
print (result)
start = result.find("LOCATION: ") + 10
end = result.find("xml", start) + 3
xmlUrl = result[start:end]
deviceDescription = requests.request('GET', xmlUrl)
open("cameraDescription.xml", 'w').write(deviceDescription.content)
tree = ET.ElementTree(file='cameraDescription.xml')
for elem in tree.iter():
if elem.tag == '{urn:schemas-sony-com:av}X_ScalarWebAPI_ActionList_URL':
opts.camera = elem.text
print opts.camera
def make_call(service, payload):
'''make a call to camera'''
url = "%s/%s" % (opts.camera, service)
headers = {"content-type": "application/json"}
data = json.dumps(payload)
response = requests.post(url,
data=data,
headers = headers).json()
return response
def simple_call(method, target="camera", params=[], id=1, version="1.0"):
'''make a simple call'''
print("Calling %s" % method)
return make_call(target,
{ "method" : method,
"params" : params,
"id" : id,
"version" : version })
def show_version():
'''show API version'''
return simple_call("getApplicationInfo")
def get_available_shutter_speeds():
'''show shutter speeds'''
return simple_call("getAvailableShutterSpeed")
def set_shutter_speed(speed):
'''set shutter speed'''
return simple_call("setShutterSpeed", params=[speed])
def enable_methods():
'''enable some more API methods. Thanks to https://github.com/erik-smit/sony-camera-api for the approach!'''
import hashlib, base64
response = simple_call("actEnableMethods",
target="accessControl",
params=[{"developerID" : "",
"developerName" : "",
"methods" : "",
"sg" : ""}])
dg = response['result'][0]['dg']
key="90adc8515a40558968fe8318b5b023fdd48d3828a2dda8905f3b93a3cd8e58dc" + dg
h = hashlib.sha256()
h.update(key)
digest = h.digest()
sg = base64.b64encode(digest)
response = simple_call("actEnableMethods",
target="accessControl",
params=[{"developerID" : "7DED695E-75AC-4ea9-8A85-E5F8CA0AF2F3",
"developerName" : "Sony Corporation",
"methods" : "camera/setFlashMode:camera/getFlashMode:camera/getSupportedFlashMode:camera/getAvailableFlashMode:camera/setExposureCompensation:camera/getExposureCompensation:camera/getSupportedExposureCompensation:camera/getAvailableExposureCompensation:camera/setSteadyMode:camera/getSteadyMode:camera/getSupportedSteadyMode:camera/getAvailableSteadyMode:camera/setViewAngle:camera/getViewAngle:camera/getSupportedViewAngle:camera/getAvailableViewAngle:camera/setMovieQuality:camera/getMovieQuality:camera/getSupportedMovieQuality:camera/getAvailableMovieQuality:camera/setFocusMode:camera/getFocusMode:camera/getSupportedFocusMode:camera/getAvailableFocusMode:camera/setStillSize:camera/getStillSize:camera/getSupportedStillSize:camera/getAvailableStillSize:camera/setBeepMode:camera/getBeepMode:camera/getSupportedBeepMode:camera/getAvailableBeepMode:camera/setCameraFunction:camera/getCameraFunction:camera/getSupportedCameraFunction:camera/getAvailableCameraFunction:camera/setLiveviewSize:camera/getLiveviewSize:camera/getSupportedLiveviewSize:camera/getAvailableLiveviewSize:camera/setTouchAFPosition:camera/getTouchAFPosition:camera/cancelTouchAFPosition:camera/setFNumber:camera/getFNumber:camera/getSupportedFNumber:camera/getAvailableFNumber:camera/setShutterSpeed:camera/getShutterSpeed:camera/getSupportedShutterSpeed:camera/getAvailableShutterSpeed:camera/setIsoSpeedRate:camera/getIsoSpeedRate:camera/getSupportedIsoSpeedRate:camera/getAvailableIsoSpeedRate:camera/setExposureMode:camera/getExposureMode:camera/getSupportedExposureMode:camera/getAvailableExposureMode:camera/setWhiteBalance:camera/getWhiteBalance:camera/getSupportedWhiteBalance:camera/getAvailableWhiteBalance:camera/setProgramShift:camera/getSupportedProgramShift:camera/getStorageInformation:camera/startLiveviewWithSize:camera/startIntervalStillRec:camera/stopIntervalStillRec:camera/actFormatStorage:system/setCurrentTime",
"sg" : sg}])
print(response)
def take_photo(filename):
'''take a photo and put result in filename'''
response = simple_call("actTakePicture")
if 'result' in response:
url = response['result'][0][0]
req = requests.request('GET', url)
open(filename, 'w').write(req.content)
return True
return False
def show_information():
'''show various bits of camera information'''
print(show_version())
print(get_available_shutter_speeds())
print(simple_call("getShutterSpeed"))
print(simple_call("getAvailableShutterSpeed"))
print(simple_call("getAvailableApiList"))
print(simple_call("getSupportedExposureMode"))
print(simple_call("getAvailableExposureMode"))
print(simple_call("getAvailableCameraFunction"))
print(simple_call("getAvailableShootMode"))
print(simple_call("getStorageInformation"))
print(simple_call("getAvailableExposureMode"))
print(simple_call("getExposureMode"))
print(simple_call("setExposureMode", params=['Program Auto']))
print(simple_call("getExposureMode"))
print(simple_call("getAvailableShutterSpeed"))
print(simple_call("getSupportedShutterSpeed"))
print(simple_call("getSupportedStillSize"))
print(simple_call("getAvailableStillSize"))
print(simple_call("getSupportedExposureCompensation"))
print(simple_call("getSupportedWhiteBalance"))
print(simple_call("getWhiteBalance"))
print(simple_call("getApplicationInfo"))
print(simple_call("getVersions"))
print(simple_call("getSupportedSelfTimer"))
print(simple_call("getSupportedPostviewImageSize"))
print(simple_call("getSupportedBeepMode"))
def set_highest_still_size():
'''setup highest possible still size'''
available = simple_call("getSupportedStillSize")
best = None
best_size = 0
for s in available['result'][0]:
size = s['size']
if size[-1] != 'M':
continue
size = float(size[:-1])
if size > best_size:
best = s
best_size = size
if best is not None:
print("Chose still size ", best)
print(simple_call("setStillSize", params=[best['aspect'], best['size']]))
def frame_time(t):
'''return a time string for a filename with 0.01 sec resolution'''
# round to the nearest 100th of a second
t += 0.005
hundredths = int(t * 100.0) % 100
return "%s%02uZ" % (time.strftime("%Y%m%d%H%M%S", time.gmtime(t)), hundredths)
def continuous_capture():
'''capture photos continuously'''
while True:
filename = 'photo%s.jpg' % frame_time(time.time())
if take_photo(filename):
print(filename)
find_camera()
simple_call("echo", params=["Hello Camera API"])
'''enable_methods()'''
print(simple_call("setExposureMode", params=['Program Auto']))
set_highest_still_size()
print(simple_call("setPostviewImageSize", params=['Original']))
show_information()
try:
continuous_capture()
except KeyboardInterrupt:
pass