flake8 compliance (except for PyQt5Gui)
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import io, logging
|
||||
import io
|
||||
import logging
|
||||
|
||||
from PIL import Image
|
||||
|
||||
@@ -10,7 +11,6 @@ import gphoto2 as gp
|
||||
from . import Camera
|
||||
|
||||
|
||||
|
||||
class CameraGphoto2(Camera):
|
||||
|
||||
def __init__(self):
|
||||
@@ -25,26 +25,24 @@ class CameraGphoto2(Camera):
|
||||
self._setupLogging()
|
||||
self._setupCamera()
|
||||
|
||||
|
||||
def cleanup(self):
|
||||
|
||||
self._cap.set_config(self._oldconfig)
|
||||
self._cap.exit(self._ctxt)
|
||||
|
||||
|
||||
def _setupLogging(self):
|
||||
|
||||
gp.error_severity[gp.GP_ERROR] = logging.WARNING
|
||||
gp.check_result(gp.use_python_logging())
|
||||
|
||||
|
||||
def _setupCamera(self):
|
||||
|
||||
self._ctxt = gp.Context()
|
||||
self._cap = gp.Camera()
|
||||
self._cap.init(self._ctxt)
|
||||
|
||||
logging.info('Camera summary: %s', str(self._cap.get_summary(self._ctxt)))
|
||||
logging.info('Camera summary: %s',
|
||||
str(self._cap.get_summary(self._ctxt)))
|
||||
|
||||
# get configuration tree
|
||||
# self._config = self._cap.get_config()
|
||||
@@ -52,30 +50,30 @@ class CameraGphoto2(Camera):
|
||||
config = self._cap.get_config()
|
||||
|
||||
# make sure camera format is not set to raw
|
||||
if 'raw' in config.get_child_by_name('imageformat').get_value().lower():
|
||||
imageformat = config.get_child_by_name('imageformat').get_value()
|
||||
if 'raw' in imageformat.lower():
|
||||
raise RuntimeError('Camera file format is set to RAW')
|
||||
|
||||
self._printConfig(config)
|
||||
|
||||
|
||||
@staticmethod
|
||||
def _configTreeToText(tree, indent=0):
|
||||
|
||||
config_txt = ''
|
||||
|
||||
for child in tree.get_children():
|
||||
for chld in tree.get_children():
|
||||
config_txt += indent * ' '
|
||||
config_txt += child.get_label() + ' [' + child.get_name() + ']: '
|
||||
config_txt += chld.get_label() + ' [' + chld.get_name() + ']: '
|
||||
|
||||
if child.count_children() > 0:
|
||||
config_txt += '\n'
|
||||
config_txt += CameraGphoto2._configTreeToText(child, indent + 4)
|
||||
if chld.count_children() > 0:
|
||||
config_txt += '\n'
|
||||
config_txt += CameraGphoto2._configTreeToText(chld, indent + 4)
|
||||
else:
|
||||
config_txt += str(child.get_value())
|
||||
config_txt += str(chld.get_value())
|
||||
try:
|
||||
choice_txt = ' ('
|
||||
|
||||
for c in child.get_choices():
|
||||
for c in chld.get_choices():
|
||||
choice_txt += c + ', '
|
||||
|
||||
choice_txt += ')'
|
||||
@@ -86,43 +84,34 @@ class CameraGphoto2(Camera):
|
||||
|
||||
return config_txt
|
||||
|
||||
|
||||
@staticmethod
|
||||
def _printConfig(config):
|
||||
config_txt = 'Camera configuration:\n'
|
||||
config_txt += CameraGphoto2._configTreeToText(config)
|
||||
logging.info(config_txt)
|
||||
|
||||
|
||||
def setActive(self):
|
||||
|
||||
config = self._cap.get_config()
|
||||
# self._config.get_child_by_name('viewfinder').set_value(True)
|
||||
config.get_child_by_name('output').set_value('PC')
|
||||
self._cap.set_config(config)
|
||||
|
||||
|
||||
def setIdle(self):
|
||||
|
||||
config = self._cap.get_config()
|
||||
# self._config.get_child_by_name('viewfinder').set_value(False)
|
||||
config.get_child_by_name('output').set_value('Off')
|
||||
self._cap.set_config(config)
|
||||
|
||||
|
||||
def getPreview(self):
|
||||
|
||||
# self._config.get_child_by_name('autofocusdrive').set_value(1)
|
||||
# self._cap.set_config(self._config)
|
||||
camera_file = self._cap.capture_preview()
|
||||
file_data = camera_file.get_data_and_size()
|
||||
return Image.open(io.BytesIO(file_data))
|
||||
|
||||
|
||||
def getPicture(self):
|
||||
|
||||
|
||||
file_path = self._cap.capture(gp.GP_CAPTURE_IMAGE)
|
||||
camera_file = self._cap.file_get(file_path.folder, file_path.name, gp.GP_FILE_TYPE_NORMAL)
|
||||
camera_file = self._cap.file_get(file_path.folder, file_path.name,
|
||||
gp.GP_FILE_TYPE_NORMAL)
|
||||
file_data = camera_file.get_data_and_size()
|
||||
return Image.open(io.BytesIO(file_data))
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import io, logging
|
||||
import io
|
||||
import logging
|
||||
|
||||
from PIL import Image
|
||||
|
||||
@@ -23,18 +24,18 @@ class CameraGphoto2Cffi(Camera):
|
||||
|
||||
self._setupCamera()
|
||||
|
||||
|
||||
def _setupCamera(self):
|
||||
|
||||
self._cap = gp.Camera()
|
||||
logging.info('Supported operations: %s', self._cap.supported_operations)
|
||||
logging.info('Supported operations: %s',
|
||||
self._cap.supported_operations)
|
||||
|
||||
if 'raw' in self._cap.config['imgsettings']['imageformat'].value.lower():
|
||||
imageformat = self._cap.config['imgsettings']['imageformat'].value
|
||||
if 'raw' in imageformat.lower():
|
||||
raise RuntimeError('Camera file format is set to RAW')
|
||||
|
||||
self._printConfig(self._cap.config)
|
||||
|
||||
|
||||
@staticmethod
|
||||
def _configTreeToText(config, indent=0):
|
||||
|
||||
@@ -46,38 +47,33 @@ class CameraGphoto2Cffi(Camera):
|
||||
|
||||
if hasattr(v, '__len__') and len(v) > 1:
|
||||
config_txt += '\n'
|
||||
config_txt += CameraGphoto2Cffi._configTreeToText(v, indent + 4)
|
||||
config_txt += CameraGphoto2Cffi._configTreeToText(v,
|
||||
indent + 4)
|
||||
else:
|
||||
config_txt += str(v) + '\n'
|
||||
|
||||
return config_txt
|
||||
|
||||
|
||||
@staticmethod
|
||||
def _printConfig(config):
|
||||
config_txt = 'Camera configuration:\n'
|
||||
config_txt += CameraGphoto2Cffi._configTreeToText(config)
|
||||
logging.info(config_txt)
|
||||
|
||||
|
||||
def setActive(self):
|
||||
|
||||
self._cap._get_config()['actions']['viewfinder'].set(True)
|
||||
self._cap._get_config()['settings']['output'].set('PC')
|
||||
|
||||
|
||||
def setIdle(self):
|
||||
|
||||
self._cap._get_config()['actions']['viewfinder'].set(False)
|
||||
self._cap._get_config()['settings']['output'].set('Off')
|
||||
|
||||
|
||||
def getPreview(self):
|
||||
|
||||
return Image.open(io.BytesIO(self._cap.get_preview()))
|
||||
|
||||
|
||||
def getPicture(self):
|
||||
|
||||
return Image.open(io.BytesIO(self._cap.capture()))
|
||||
|
||||
return Image.open(io.BytesIO(self._cap.capture()))
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import logging
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
from PIL import Image
|
||||
import os, subprocess, logging
|
||||
|
||||
from . import Camera
|
||||
|
||||
|
||||
class CameraGphoto2CommandLine(Camera):
|
||||
|
||||
def __init__(self):
|
||||
@@ -26,19 +30,17 @@ class CameraGphoto2CommandLine(Camera):
|
||||
|
||||
self.setActive()
|
||||
|
||||
|
||||
def setActive(self):
|
||||
|
||||
print(self._callGphoto('-a', '/dev/null'))
|
||||
|
||||
self._callGphoto('-a', '/dev/null')
|
||||
|
||||
def getPicture(self):
|
||||
|
||||
|
||||
self._callGphoto('--capture-image-and-download', self._tmp_filename)
|
||||
return Image.open(self._tmp_filename)
|
||||
|
||||
|
||||
def _callGphoto(self, action, filename):
|
||||
|
||||
cmd = 'gphoto2 --force-overwrite --quiet ' + action + ' --filename ' + filename
|
||||
return subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
|
||||
cmd = 'gphoto2 --force-overwrite --quiet {} --filename {}'
|
||||
return subprocess.check_output(cmd.format(action, filename),
|
||||
shell=True, stderr=subprocess.STDOUT)
|
||||
|
||||
@@ -9,6 +9,7 @@ import cv2
|
||||
|
||||
from . import Camera
|
||||
|
||||
|
||||
class CameraOpenCV(Camera):
|
||||
|
||||
def __init__(self):
|
||||
@@ -22,7 +23,6 @@ class CameraOpenCV(Camera):
|
||||
|
||||
self._cap = cv2.VideoCapture()
|
||||
|
||||
|
||||
def setActive(self):
|
||||
|
||||
if not self._cap.isOpened():
|
||||
@@ -30,20 +30,17 @@ class CameraOpenCV(Camera):
|
||||
if not self._cap.isOpened():
|
||||
raise RuntimeError('Camera could not be opened')
|
||||
|
||||
|
||||
def setIdle(self):
|
||||
|
||||
if self._cap.isOpened():
|
||||
self._cap.release()
|
||||
|
||||
|
||||
def getPreview(self):
|
||||
|
||||
return self.getPicture()
|
||||
|
||||
|
||||
def getPicture(self):
|
||||
|
||||
|
||||
self.setActive()
|
||||
status, frame = self._cap.read()
|
||||
if not status:
|
||||
@@ -52,4 +49,3 @@ class CameraOpenCV(Camera):
|
||||
# OpenCV yields frames in BGR format, conversion to RGB necessary.
|
||||
# (See https://stackoverflow.com/a/32270308)
|
||||
return Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
||||
|
||||
|
||||
@@ -6,8 +6,9 @@
|
||||
modules = (
|
||||
('python-gphoto2', 'CameraGphoto2', 'CameraGphoto2'),
|
||||
('gphoto2-cffi', 'CameraGphoto2Cffi', 'CameraGphoto2Cffi'),
|
||||
('gphoto2-commandline', 'CameraGphoto2CommandLine', 'CameraGphoto2CommandLine'),
|
||||
('opencv', 'CameraOpenCV', 'CameraOpenCV') )
|
||||
('gphoto2-commandline', 'CameraGphoto2CommandLine',
|
||||
'CameraGphoto2CommandLine'),
|
||||
('opencv', 'CameraOpenCV', 'CameraOpenCV'))
|
||||
|
||||
|
||||
class Camera:
|
||||
@@ -17,28 +18,23 @@ class Camera:
|
||||
self.hasPreview = False
|
||||
self.hasIdle = False
|
||||
|
||||
|
||||
def __enter__(self):
|
||||
|
||||
return self
|
||||
|
||||
|
||||
def __exit__(self, exc_type, exc_value, traceback):
|
||||
|
||||
self.cleanup()
|
||||
|
||||
|
||||
def cleanup(self):
|
||||
|
||||
pass
|
||||
|
||||
|
||||
@property
|
||||
def hasPreview(self):
|
||||
|
||||
return self._has_preview
|
||||
|
||||
|
||||
@hasPreview.setter
|
||||
def hasPreview(self, value):
|
||||
|
||||
@@ -51,7 +47,6 @@ class Camera:
|
||||
def hasIdle(self):
|
||||
|
||||
return self._has_idle
|
||||
|
||||
|
||||
@hasIdle.setter
|
||||
def hasIdle(self, value):
|
||||
@@ -61,7 +56,6 @@ class Camera:
|
||||
|
||||
self._has_idle = value
|
||||
|
||||
|
||||
def setActive(self):
|
||||
|
||||
if not self.hasIdle:
|
||||
@@ -69,7 +63,6 @@ class Camera:
|
||||
else:
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
def setIdle(self):
|
||||
|
||||
if not self.hasIdle:
|
||||
@@ -77,7 +70,6 @@ class Camera:
|
||||
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
def getPreview(self):
|
||||
|
||||
if not self.hasPreview:
|
||||
@@ -85,15 +77,6 @@ class Camera:
|
||||
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
def getPicture(self):
|
||||
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
def setIdle(self):
|
||||
|
||||
if not self.hasIdle:
|
||||
raise RuntimeError('Camera does not support idle state')
|
||||
|
||||
raise NotImplementedError()
|
||||
|
||||
Reference in New Issue
Block a user