我正在使用 Gphoto2 在 DSLR 上拍照。由于它基于我尝试使用的 bash 命令,subprocess.communicate
但它在相机拍照后冻结。
如果我在终端中尝试gphoto2 --capture-image-and-download
,只需不到 2 秒。我正在研究树莓派。
代码:
import subprocess
class Wrapper(object):
def __init__(self, subprocess):
self._subprocess = subprocess
def call(self,cmd):
p = self._subprocess.Popen(cmd, shell=True, stdout=self._subprocess.PIPE, stderr=self._subprocess.PIPE)
out, err = p.communicate()
return p.returncode, out.rstrip(), err.rstrip()
class Gphoto(Wrapper):
def __init__(self, subprocess):
Wrapper.__init__(self,subprocess)
self._CMD = 'gphoto2'
def captureImageAndDownload(self):
code, out, err = self.call(self._CMD + " --capture-image-and-download")
if code != 0:
raise Exception(err)
filename = None
for line in out.split('\n'):
if line.startswith('Saving file as '):
filename = line.split('Saving file as ')[1]
return filename
def main():
camera = Gphoto(subprocess)
filename = camera.captureImageAndDownload()
print(filname)
if __name__ == "__main__":
main()
如果我退出,我会得到这个:
Traceback (most recent call last):
File "test.py", line 39, in <module>
main()
File "test.py", line 35, in main
filename = camera.captureImageAndDownload()
File "test.py", line 22, in captureImageAndDownload
code, out, err = self.call(self._CMD + " --capture-image-and-download")
File "test.py", line 11, in call
out, err = p.communicate()
File "/usr/lib/python2.7/subprocess.py", line 799, in communicate
return self._communicate(input)
File "/usr/lib/python2.7/subprocess.py", line 1409, in _communicate
stdout, stderr = self._communicate_with_poll(input)
File "/usr/lib/python2.7/subprocess.py", line 1463, in _communicate_with_poll
ready = poller.poll()
KeyboardInterrupt
有任何想法吗?