저는 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
어떤 아이디어?
견적 "표준 출력 및 표준 오류로부터 데이터를 읽기 (참고 당신에게 줄 수도 Python2 또는 3,
p.stdout.read()
을 사용하는 경우에 따라서는 등 대신O = b''
해야O = ''
의미, 다시 데이터를 바이트) 파일에 도달했습니다. " - 프로세스가 종료되지 않은 경우 입력 대기 중입니다. 프로세스가 완료 되었습니까? – TorxedIm 확실합니다. 카메라의 셔터 소리가 들리고 파일이 생성됩니다. 일단 내가 적어도 5 분 동안 앉아 있지만 여전히 동결하자 ... 터미널에서 프로세스를 테스트하고 추가 입력없이 작동합니다. – Dennis
대신 다음과 비슷한 것을 시도해보십시오 : https://gist.github.com/anonymous/0811f71ca0b290f2625c9ba768bd45fd – Torxed