Skip to content

Commit

Permalink
Multiple video open for ControlPlayer, ControlList improve
Browse files Browse the repository at this point in the history
  • Loading branch information
UmSenhorQualquer committed Feb 12, 2019
1 parent 6f76b88 commit a53109e
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 5 deletions.
4 changes: 4 additions & 0 deletions pyforms_gui/basewidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def __init__(self, *args, **kwargs):
self.toolbar = []
self._mainmenu = []
self._splitters = []
self.vlayouts = []
self.hlayouts = []
self._tabs = []
self._formset = None
self._formLoaded = False
Expand Down Expand Up @@ -143,6 +145,7 @@ def generate_panel(self, formset):
layout = None
if isinstance(formset, (tuple, no_columns)):
layout = QHBoxLayout()
self.hlayouts.append(layout)
for row in formset:
if isinstance(row, (list, tuple, vsplitter, hsplitter, no_columns, segment) ):
panel = self.generate_panel(row)
Expand Down Expand Up @@ -216,6 +219,7 @@ def generate_panel(self, formset):
layout.addWidget(param.form)
elif isinstance(formset, (list, segment)):
layout = QVBoxLayout()
self.vlayouts.append(layout)
for row in formset:
if isinstance(row, (list, tuple, vsplitter, hsplitter, segment, no_columns) ):
panel = self.generate_panel(row)
Expand Down
18 changes: 18 additions & 0 deletions pyforms_gui/controls/control_boundingslider.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from AnyQt import QtCore, _api
from AnyQt.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QSpinBox
from AnyQt.QtGui import QFont, QColor, QCursor, QPainter
from AnyQt.QtWidgets import QLabel, QSizePolicy

from pyforms_gui.controls.control_base import ControlBase

Expand All @@ -19,6 +20,8 @@ def __init__(self, *args, **kwargs):
self.setMinimumWidth(30)
self.setMaximumWidth(50)

self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)

self._step = 0
self._lower = 0
self._higher = 100
Expand Down Expand Up @@ -299,6 +302,14 @@ def init_form(self):
elif _api.USED_API == _api.QT_API_PYQT4:
hlayout.setMargin(0)

if self._label is not None:
self._controllabel = QLabel(self.form)
hlayout.addWidget(self._controllabel)
self._controllabel.setAccessibleName('ControlBoundingSlider-label')
self.label = self._label
else:
self._controllabel = None


hwidget.setLayout(hlayout)
self._min_spinbox = QSpinBox()
Expand Down Expand Up @@ -389,6 +400,13 @@ def save_form(self, data, path=None):
############ Properties ##################################################
##########################################################################

@property
def label(self):
return self._controllabel.getText()
@label.setter
def label(self, value):
self._controllabel.setText(value)

@property
def value(self):
"""
Expand Down
9 changes: 6 additions & 3 deletions pyforms_gui/controls/control_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class ControlFile(ControlBase):

def __init__(self, *args, **kwargs):

self.__exec_changed_event = True
super(ControlFile, self).__init__(*args, **kwargs)
self.use_save_dialog = kwargs.get('use_save_dialog', False)

Expand All @@ -25,11 +25,12 @@ def init_form(self):
self._form.pushButton.clicked.connect(self.click)
self.form.lineEdit.editingFinished.connect(self.finishEditing)
self._form.pushButton.setIcon(conf.PYFORMS_ICON_FILE_OPEN)
super(ControlFile, self).init_form()
super().init_form()

def finishEditing(self):
"""Function called when the lineEdit widget is edited"""
self.changed_event()
if self.__exec_changed_event:
self.changed_event()

def click(self):

Expand All @@ -54,7 +55,9 @@ def value(self):

@value.setter
def value(self, value):
self.__exec_changed_event = False
self._form.lineEdit.setText(value)
self.__exec_changed_event = True
ControlBase.value.fset(self, value)

@property
Expand Down
13 changes: 13 additions & 0 deletions pyforms_gui/controls/control_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class ControlList(ControlBase, QWidget):
def __init__(self, *args, **kwargs):
QWidget.__init__(self)

self._height = kwargs.get('height', None)

self._plusFunction = kwargs.get('add_function', None)
self._minusFunction = kwargs.get('remove_function', None)
ControlBase.__init__(self, *args, **kwargs)
Expand All @@ -37,6 +39,7 @@ def __init__(self, *args, **kwargs):
self.select_entire_row = kwargs.get('select_entire_row', False)
self.horizontal_headers = kwargs.get('horizontal_headers', None)


self.item_selection_changed_event = kwargs.get('item_selection_changed_event', self.item_selection_changed_event)
self.data_changed_event = kwargs.get('data_changed_event', self.data_changed_event)
self.item_selection_changed_event = kwargs.get('item_selection_changed_event', self.item_selection_changed_event)
Expand All @@ -57,6 +60,9 @@ def init_form(self):
# Load the UI for the self instance
uic.loadUi(os.path.join(rootPath, "list.ui"), self)

if self._height:
self.height = self._height

self.label = self._label

self.tableWidget.currentCellChanged.connect(self.tableWidgetCellChanged)
Expand Down Expand Up @@ -233,6 +239,13 @@ def cell_double_clicked_event(self, row, column):
############ PROPERTIES ##################################################
##########################################################################

@property
def height(self):
return self._height
@height.setter
def height(self, value):
self._height = value
self.setMaximumHeight(value)

@property
def horizontal_headers(self):
Expand Down
33 changes: 31 additions & 2 deletions pyforms_gui/controls/control_player/control_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
__status__ = "Development"

import logging, platform, os, math
from .multiple_videocapture import MultipleVideoCapture

try:
import cv2
Expand All @@ -26,6 +27,7 @@
from AnyQt.QtWidgets import QFrame
from AnyQt.QtWidgets import QApplication
from AnyQt.QtWidgets import QMainWindow
from AnyQt.QtWidgets import QMessageBox

from pyforms_gui.controls.control_base import ControlBase

Expand All @@ -49,6 +51,8 @@ def __init__(self, *args, **kwargs):
QFrame.__init__(self)
ControlBase.__init__(self, *args, **kwargs)

self._multiple_files = kwargs.get('multiple_files', False)

self._current_frame = None # current frame image
self._current_frame_index = None # current frame index

Expand All @@ -71,7 +75,6 @@ def init_form(self):


# Define the icon for the Play button

self.videoPlay.setIcon(conf.PYFORMS_ICON_VIDEOPLAYER_PAUSE_PLAY)
self.detach_btn.setIcon(conf.PYFORMS_ICON_VIDEOPLAYER_DETACH)

Expand Down Expand Up @@ -244,7 +247,33 @@ def value(self, value):
if value == 0:
self._value = cv2.VideoCapture(0)
elif isinstance(value, str) and value:
self._value = cv2.VideoCapture(value)

open_multiplefiles = self._multiple_files

if open_multiplefiles:
open_multiplefiles = len(MultipleVideoCapture.search_files(value))>0

if open_multiplefiles:
msg = "Multiple files were found with the same name, do you wish to combine then in a single video?\n\n"
for filepath in MultipleVideoCapture.search_files(value):
msg += "- {filename}\n".format(filename=os.path.basename(filepath))

reply = QMessageBox(
QMessageBox.Question,
'Open multiple files',
msg,
QMessageBox.No | QMessageBox.Yes
).exec_()

if reply == QMessageBox.Yes:
open_multiplefiles = True
else:
open_multiplefiles = False

if open_multiplefiles:
self._value = MultipleVideoCapture(value)
else:
self._value = cv2.VideoCapture(value)
else:
self._value = value

Expand Down
72 changes: 72 additions & 0 deletions pyforms_gui/controls/control_player/multiple_videocapture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import glob, os, cv2

class MultipleVideoCapture(object):


def __init__(self, filepath):

# VideoCapture objects
self.captures = [cv2.VideoCapture(fn) for fn in self.search_files(filepath)]

# Number of frames for each VideoCapture
self.n_frames = [c.get(7) for c in self.captures]

# Total frames ranges starting in 0
self.frames_ranges = [0]

# Active VideoCapture
self.capture_index = 0

for cap in self.captures:
nframes = self.frames_ranges[-1]+int(cap.get(7))
self.frames_ranges.append(nframes)

@classmethod
def search_files(cls, filepath):
filedir = os.path.dirname(filepath)
filename = os.path.basename(filepath)
name, ext = os.path.splitext(filename)

names = name.rsplit('_', 1)
search_name = os.path.join(filedir, names[0] + '_*' + ext)
return sorted(glob.glob(search_name))

@property
def capture(self):
return self.captures[self.capture_index]

def read(self):
next_capture = self.capture.get(1)==self.n_frames[self.capture_index]
if next_capture:
self.capture_index += 1
self.capture.set(1,0)
res = self.capture.read()
return res


def get(self, flag):
if flag==1:
return self.frames_ranges[self.capture_index]+int(self.capture.get(1))
elif flag==7:
# Return the total of frames
return self.frames_ranges[-1]
else:
return self.capture.get(flag)

def set(self, flag, value):

if flag==1:
for index, nframes in enumerate(self.frames_ranges):
if nframes<value:
self.capture_index = index

self.capture.set(flag, value-self.frames_ranges[self.capture_index])
else:
self.capture.set(flag, value)




if __name__ == "__main__":

video = MultipleVideoCapture('/home/ricardo/bitbucket/idtracker-project/idtrackerai_video_example_01.avi')

0 comments on commit a53109e

Please sign in to comment.