pylint warnings fixed

This commit is contained in:
Fabio Manganiello 2019-07-13 18:25:42 +02:00
parent 07dbe57641
commit 0b05d7d8de

View file

@ -12,7 +12,7 @@ from platypush.config import Config
from platypush.message.response import Response from platypush.message.response import Response
from platypush.message.event.camera import CameraRecordingStartedEvent, \ from platypush.message.event.camera import CameraRecordingStartedEvent, \
CameraRecordingStoppedEvent, CameraVideoRenderedEvent, \ CameraRecordingStoppedEvent, CameraVideoRenderedEvent, \
CameraPictureTakenEvent, CameraFrameCapturedEvent, CameraEvent CameraPictureTakenEvent, CameraFrameCapturedEvent
from platypush.plugins import Plugin, action from platypush.plugins import Plugin, action
@ -52,8 +52,7 @@ class CameraPlugin(Plugin):
sleep_between_frames=_default_sleep_between_frames, sleep_between_frames=_default_sleep_between_frames,
max_stored_frames=_max_stored_frames, max_stored_frames=_max_stored_frames,
color_transform=_default_color_transform, color_transform=_default_color_transform,
scale_x=None, scale_y=None, rotate=None, flip=None, scale_x=None, scale_y=None, rotate=None, flip=None, **kwargs):
*args, **kwargs):
""" """
:param device_id: Index of the default video device to be used for :param device_id: Index of the default video device to be used for
capturing (default: 0) capturing (default: 0)
@ -115,7 +114,7 @@ class CameraPlugin(Plugin):
:type flip: int :type flip: int
""" """
super().__init__(*args, **kwargs) super().__init__(**kwargs)
self.default_device_id = device_id self.default_device_id = device_id
self.frames_dir = os.path.abspath(os.path.expanduser(frames_dir)) self.frames_dir = os.path.abspath(os.path.expanduser(frames_dir))
@ -131,10 +130,10 @@ class CameraPlugin(Plugin):
self.rotate = rotate self.rotate = rotate
self.flip = flip self.flip = flip
self._is_recording = {} # device_id => Event map self._is_recording = {} # device_id => Event map
self._devices = {} # device_id => VideoCapture map self._devices = {} # device_id => VideoCapture map
self._recording_threads = {} # device_id => Thread map self._recording_threads = {} # device_id => Thread map
self._recording_info = {} # device_id => recording info map self._recording_info = {} # device_id => recording info map
def _init_device(self, device_id, frames_dir=None, **info): def _init_device(self, device_id, frames_dir=None, **info):
self._release_device(device_id) self._release_device(device_id)
@ -153,7 +152,6 @@ class CameraPlugin(Plugin):
return self._devices[device_id] return self._devices[device_id]
def _release_device(self, device_id, wait_thread_termination=True): def _release_device(self, device_id, wait_thread_termination=True):
if device_id in self._is_recording: if device_id in self._is_recording:
self._is_recording[device_id].clear() self._is_recording[device_id].clear()
@ -174,8 +172,8 @@ class CameraPlugin(Plugin):
if device_id in self._recording_info: if device_id in self._recording_info:
del self._recording_info[device_id] del self._recording_info[device_id]
@staticmethod
def _store_frame_to_file(self, frame, frames_dir, image_file): def _store_frame_to_file(frame, frames_dir, image_file):
if image_file: if image_file:
filepath = image_file filepath = image_file
else: else:
@ -185,23 +183,21 @@ class CameraPlugin(Plugin):
cv2.imwrite(filepath, frame) cv2.imwrite(filepath, frame)
return filepath return filepath
def _get_stored_frames_files(self, frames_dir): def _get_stored_frames_files(self, frames_dir):
ret = sorted([ ret = sorted([
os.path.join(frames_dir, f) for f in os.listdir(frames_dir) os.path.join(frames_dir, f) for f in os.listdir(frames_dir)
if os.path.isfile(os.path.join(frames_dir, f)) and if os.path.isfile(os.path.join(frames_dir, f)) and
re.search(self._frame_filename_regex, f) re.search(self._frame_filename_regex, f)
]) ])
return ret return ret
def _get_avg_fps(self, frames_dir): def _get_avg_fps(self, frames_dir):
files = self._get_stored_frames_files(frames_dir) files = self._get_stored_frames_files(frames_dir)
frame_time_diff = 0.0 frame_time_diff = 0.0
n_frames = 0 n_frames = 0
for i in range(1, len(files)): for i in range(1, len(files)):
m1 = re.search(self._frame_filename_regex, files[i-1]) m1 = re.search(self._frame_filename_regex, files[i - 1])
m2 = re.search(self._frame_filename_regex, files[i]) m2 = re.search(self._frame_filename_regex, files[i])
if not m1 or not m2: if not m1 or not m2:
@ -209,18 +205,16 @@ class CameraPlugin(Plugin):
t1 = datetime.timestamp(datetime(*map(int, m1.groups()))) t1 = datetime.timestamp(datetime(*map(int, m1.groups())))
t2 = datetime.timestamp(datetime(*map(int, m2.groups()))) t2 = datetime.timestamp(datetime(*map(int, m2.groups())))
frame_time_diff += (t2-t1) frame_time_diff += (t2 - t1)
n_frames += 1 n_frames += 1
return n_frames/frame_time_diff if n_frames and frame_time_diff else 0 return n_frames / frame_time_diff if n_frames and frame_time_diff else 0
def _remove_expired_frames(self, frames_dir, max_stored_frames): def _remove_expired_frames(self, frames_dir, max_stored_frames):
files = self._get_stored_frames_files(frames_dir) files = self._get_stored_frames_files(frames_dir)
for f in files[:len(files)-max_stored_frames]: for f in files[:len(files) - max_stored_frames]:
os.unlink(f) os.unlink(f)
def _make_video_file(self, frames_dir, video_file, video_type): def _make_video_file(self, frames_dir, video_file, video_type):
files = self._get_stored_frames_files(frames_dir) files = self._get_stored_frames_files(frames_dir)
if not files: if not files:
@ -239,7 +233,6 @@ class CameraPlugin(Plugin):
self.fire_event(CameraVideoRenderedEvent(filename=video_file)) self.fire_event(CameraVideoRenderedEvent(filename=video_file))
shutil.rmtree(frames_dir, ignore_errors=True) shutil.rmtree(frames_dir, ignore_errors=True)
def _recording_thread(self): def _recording_thread(self):
def thread(duration, video_file, image_file, device_id, def thread(duration, video_file, image_file, device_id,
frames_dir, n_frames, sleep_between_frames, frames_dir, n_frames, sleep_between_frames,
@ -282,7 +275,7 @@ class CameraPlugin(Plugin):
rows, cols = frame.shape rows, cols = frame.shape
if not rotation_matrix: if not rotation_matrix:
rotation_matrix = cv2.getRotationMatrix2D( rotation_matrix = cv2.getRotationMatrix2D(
(cols/2, rows/2), rotate, 1) (cols / 2, rows / 2), rotate, 1)
frame = cv2.warpAffine(frame, rotation_matrix, (cols, rows)) frame = cv2.warpAffine(frame, rotation_matrix, (cols, rows))
@ -293,7 +286,7 @@ class CameraPlugin(Plugin):
scale_x = scale_x or 1 scale_x = scale_x or 1
scale_y = scale_y or 1 scale_y = scale_y or 1
frame = cv2.resize(frame, None, fx=scale_x, fy=scale_y, frame = cv2.resize(frame, None, fx=scale_x, fy=scale_y,
interpolation = cv2.INTER_CUBIC) interpolation=cv2.INTER_CUBIC)
self._store_frame_to_file(frame=frame, frames_dir=frames_dir, self._store_frame_to_file(frame=frame, frames_dir=frames_dir,
image_file=image_file) image_file=image_file)
@ -326,7 +319,6 @@ class CameraPlugin(Plugin):
return thread return thread
@action @action
def start_recording(self, duration=None, video_file=None, video_type=None, def start_recording(self, duration=None, video_file=None, video_type=None,
device_id=None, frames_dir=None, device_id=None, frames_dir=None,
@ -360,17 +352,18 @@ class CameraPlugin(Plugin):
return self.status(device_id=device_id) return self.status(device_id=device_id)
recording_started = threading.Event() recording_started = threading.Event()
def on_recording_started(event): def on_recording_started(event):
recording_started.set() recording_started.set()
frames_dir = os.path.abspath(os.path.expanduser(frames_dir)) \ frames_dir = os.path.abspath(os.path.expanduser(frames_dir)) \
if frames_dir is not None else self.frames_dir if frames_dir is not None else self.frames_dir
sleep_between_frames = sleep_between_frames if sleep_between_frames \ sleep_between_frames = sleep_between_frames if sleep_between_frames \
is not None else self.sleep_between_frames is not None else self.sleep_between_frames
max_stored_frames = max_stored_frames if max_stored_frames \ max_stored_frames = max_stored_frames if max_stored_frames \
is not None else self.max_stored_frames is not None else self.max_stored_frames
color_transform = color_transform if color_transform \ color_transform = color_transform if color_transform \
is not None else self.color_transform is not None else self.color_transform
scale_x = scale_x if scale_x is not None else self.scale_x scale_x = scale_x if scale_x is not None else self.scale_x
scale_y = scale_y if scale_y is not None else self.scale_y scale_y = scale_y if scale_y is not None else self.scale_y
rotate = rotate if rotate is not None else self.rotate rotate = rotate if rotate is not None else self.rotate
@ -399,17 +392,17 @@ class CameraPlugin(Plugin):
self.register_handler(CameraRecordingStartedEvent, on_recording_started) self.register_handler(CameraRecordingStartedEvent, on_recording_started)
self._recording_threads[device_id] = threading.Thread( self._recording_threads[device_id] = threading.Thread(
target=self._recording_thread(), kwargs = { target=self._recording_thread(), kwargs={
'duration':duration, 'duration': duration,
'video_file':video_file, 'video_file': video_file,
'video_type':video_type, 'video_type': video_type,
'image_file':None, 'device_id':device_id, 'image_file': None, 'device_id': device_id,
'frames_dir':frames_dir, 'n_frames':None, 'frames_dir': frames_dir, 'n_frames': None,
'sleep_between_frames':sleep_between_frames, 'sleep_between_frames': sleep_between_frames,
'max_stored_frames':max_stored_frames, 'max_stored_frames': max_stored_frames,
'color_transform':color_transform, 'color_transform': color_transform,
'scale_x':scale_x, 'scale_y':scale_y, 'scale_x': scale_x, 'scale_y': scale_y,
'rotate':rotate, 'flip':flip 'rotate': rotate, 'flip': flip
}) })
self._recording_threads[device_id].start() self._recording_threads[device_id].start()
@ -459,7 +452,7 @@ class CameraPlugin(Plugin):
status = self.status(device_id=device_id).output.get(device_id) status = self.status(device_id=device_id).output.get(device_id)
if 'image_file' in status: if 'image_file' in status:
shutil.copyfile(status['image_file'], image_file) shutil.copyfile(status['image_file'], image_file)
return { 'path': image_file } return {'path': image_file}
raise RuntimeError('Recording already in progress and no images ' + raise RuntimeError('Recording already in progress and no images ' +
'have been captured yet') 'have been captured yet')
@ -467,7 +460,7 @@ class CameraPlugin(Plugin):
warmup_frames = warmup_frames if warmup_frames is not None else \ warmup_frames = warmup_frames if warmup_frames is not None else \
self.warmup_frames self.warmup_frames
color_transform = color_transform if color_transform \ color_transform = color_transform if color_transform \
is not None else self.color_transform is not None else self.color_transform
scale_x = scale_x if scale_x is not None else self.scale_x scale_x = scale_x if scale_x is not None else self.scale_x
scale_y = scale_y if scale_y is not None else self.scale_y scale_y = scale_y if scale_y is not None else self.scale_y
rotate = rotate if rotate is not None else self.rotate rotate = rotate if rotate is not None else self.rotate
@ -481,16 +474,16 @@ class CameraPlugin(Plugin):
self.register_handler(CameraPictureTakenEvent, on_picture_taken) self.register_handler(CameraPictureTakenEvent, on_picture_taken)
self._recording_threads[device_id] = threading.Thread( self._recording_threads[device_id] = threading.Thread(
target=self._recording_thread(), kwargs = { target=self._recording_thread(), kwargs={
'duration':None, 'video_file':None, 'duration': None, 'video_file': None,
'image_file':image_file, 'video_type':None, 'image_file': image_file, 'video_type': None,
'device_id':device_id, 'frames_dir':None, 'device_id': device_id, 'frames_dir': None,
'n_frames':warmup_frames, 'n_frames': warmup_frames,
'sleep_between_frames':None, 'sleep_between_frames': None,
'max_stored_frames':None, 'max_stored_frames': None,
'color_transform':color_transform, 'color_transform': color_transform,
'scale_x':scale_x, 'scale_y':scale_y, 'scale_x': scale_x, 'scale_y': scale_y,
'rotate':rotate, 'flip':flip 'rotate': rotate, 'flip': flip
}) })
self._recording_threads[device_id].start() self._recording_threads[device_id].start()
@ -498,7 +491,7 @@ class CameraPlugin(Plugin):
picture_taken.wait() picture_taken.wait()
self.unregister_handler(CameraPictureTakenEvent, on_picture_taken) self.unregister_handler(CameraPictureTakenEvent, on_picture_taken)
return { 'path': image_file } return {'path': image_file}
@action @action
def status(self, device_id=None): def status(self, device_id=None):
@ -512,15 +505,14 @@ class CameraPlugin(Plugin):
id: { id: {
'image_file': self._get_stored_frames_files(info['frames_dir'])[-2] 'image_file': self._get_stored_frames_files(info['frames_dir'])[-2]
if 'frames_dir' in info if 'frames_dir' in info
and len(self._get_stored_frames_files(info['frames_dir'])) > 1 and len(self._get_stored_frames_files(info['frames_dir'])) > 1
and 'image_file' not in info else info.get('image_file'), **info and 'image_file' not in info else info.get('image_file'), **info
} }
for id, info in self._recording_info.items() for id, info in self._recording_info.items()
if device_id is None or id == device_id if device_id is None or id == device_id
}, disable_logging=True) }, disable_logging=True)
return resp return resp
@action @action
def get_default_device_id(self): def get_default_device_id(self):
return self.default_device_id return self.default_device_id