Skip to content

Commit

Permalink
Clean up video handler and remove debug keyboard shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
jswilson committed Aug 22, 2019
1 parent 57765e7 commit 1968074
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 51 deletions.
7 changes: 2 additions & 5 deletions src/MenuBar.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ def _on_back1frame(self):
def _on_fwd1frame(self):
self.store.set_video_frame(self.store.video_current_frame + 1)

def _disble_playback_actions():
def _disble_playback_actions(self):
for action in self.playback_actions:
action.setEnabled(False)

def _enable_playback_actions():
def _enable_playback_actions(self):
for action in self.playback_actions:
action.setEnabled(True)

Expand Down Expand Up @@ -248,9 +248,6 @@ def _create_playback_menu(self):
if not self.store.video_filename:
for a in self.playback_actions:
a.setEnabled(False)
# map(lambda x: x.setEnabled(False), self.playback_actions)

print(self.playback_actions[0].isEnabled())

def _create_tagging_shortcuts(self, menu):
self.add_tag1 = QAction(Registry.MENU_TAG_ONE, self)
Expand Down
18 changes: 1 addition & 17 deletions src/VRTWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ def __init__(self):
QMainWindow.__init__(self)

try:
file = open('range-tagger.seg', 'r')
file = open(Registry.CONFIG_FILE_LOCATION, 'r')
self.store = Serializer.json_to_store(file.read())
except FileNotFoundError:
self.store = Store()
self.invoker = CommandInvoker()

self._init_ui()
self._init_shortcuts()
self._init_menu()

def _init_ui(self):
Expand Down Expand Up @@ -52,18 +51,3 @@ def _init_ui(self):
def _init_menu(self):
self.menubar = MenuBar(self.store, self.invoker)
self.setMenuBar(self.menubar)

def _init_shortcuts(self):
self.saveimgs = QShortcut(QKeySequence(Qt.CTRL + Qt.SHIFT + Qt.Key_U), self)
self.saveimgs.activated.connect(self._on_save_images)

def _on_save_images(self):
starting_frame = 0
incr = 30
dest = './frames/'

curr = starting_frame
while curr < self.store.video_total_frames:
qimg = self.video_area.video_handler.get_frame_as_image(curr)
qimg.save(dest + str(curr) + '.jpg')
curr += incr
13 changes: 11 additions & 2 deletions src/components/video/VideoArea.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def __init__(self, store):
self.store = store
self.video_handler = None
self.playback_handler = PlaybackHandler(self.store, self.video_handler)
self._prev_frame = None

self.store.video_filename_changed.connect(self._video_filename_changed)
self.store.video_frame_number_changed.connect(self._video_frame_number_changed)
Expand All @@ -40,20 +41,28 @@ def _show_video_area_and_load_video(self, filename):
self.error_message.show()
return

self._set_video_area_visible()

self.store.set_video_loaded(True)
self.store.set_total_frames(self.video_handler.get_total_frames())
self.store.set_video_frame(0)

def _set_video_area_visible(self):
self.graphics_view.setVisible(True)
self.controls.setVisible(True)
self.button.setVisible(False)
self.store.set_total_frames(self.video_handler.get_total_frames())
self.store.set_video_frame(0)

def _video_frame_number_changed(self, frame_number):
if frame_number is None or self.video_handler is None:
return

frame = self.video_handler.get_frame(frame_number)
self.graphics_scene.addItem(frame)

if self._prev_frame:
self.graphics_scene.removeItem(self._prev_frame)
self._prev_frame = frame

self.graphics_view.fitInView(self.graphics_view.sceneRect(), Qt.KeepAspectRatio)

def resizeEvent(self, event):
Expand Down
31 changes: 7 additions & 24 deletions src/components/video/video_handler/VideoHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,23 @@ def get_current_frame_number(self):
return self.current_frame_number

def get_frame(self, frame_number):

# libopenshot's cache isn't particularly useful for us, so we clear it
# out every 100 calls (otherwise, it just uses up way too mucm memory)
# out every 100 calls (otherwise, it just uses up way too much memory)
self._frame_call_count += 1
if self._frame_call_count % 100 == 0:
self.capture.GetCache().Clear()

openshot_frame_number = frame_number + 1 # openshot is 1-indexed...yay.
framee = self.capture.GetFrame(openshot_frame_number)

newqimg = framee.GetImage()
frame = self.capture.GetFrame(frame_number + 1) # openshot is 1-indexed...yay.

# f = openshot.UCharVector()
# f = framee.GetImageAsVec()
# width = framee.GetWidth()
# height = framee.GetHeight()
# newqimg = QImage(f, width, height, width*4, QImage.Format_RGBA8888)
# in order to retrieve the image from libopen shot, we actually have
# to base64 decode it
dat = base64.b64decode(frame.GetImageAsString())
frame.DeleteImageAsStringMemory()
newqimg = QImage(dat, frame.GetWidth(), frame.GetHeight(), frame.GetWidth()*4, QImage.Format_RGBA8888)

dat = base64.b64decode(framee.GetImageAsString())
width = framee.GetWidth()
height = framee.GetHeight()
newqimg = QImage(dat, width, height, width*4, QImage.Format_RGBA8888)

self.current_frame_number = frame_number
return self._image_to_pixmap(newqimg)

def get_frame_as_image(self, frame_number):
framee = self.capture.GetFrame(frame_number+1) # openshot is 1-indexed
dat = base64.b64decode(framee.GetImageAsString())
width = framee.GetWidth()
height = framee.GetHeight()
newqimg = QImage(dat, width, height, width*4, QImage.Format_RGBA8888)
return newqimg

def _image_to_pixmap(self, qimg):
pixmap = QGraphicsPixmapItem()
pixmap.setPixmap( QPixmap.fromImage(qimg) );
Expand Down
5 changes: 4 additions & 1 deletion src/registry/Registry.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os.path
import ntpath
import sys
from appdirs import AppDirs

def path_leaf(path):
head, tail = ntpath.split(path)
Expand All @@ -20,12 +21,14 @@ class Registry:
SEGMENT_LIST_ITEM_QSS = resource_location("./src/components/segments/segment-list-item.qss")
OPEN_IMAGE = resource_location("./src/registry/open.png")

CONFIG_FILE_LOCATION = os.path.join(AppDirs("range-tagger", "").user_data_dir, 'range-tagger.seg')

OPEN_VIDEO_DIALOG_TITLE = "Open Video"
OPEN_VIDEO_DIALOG_FILE_TYPES = "Video Files (*.mp4 *.mkv *.avi *.ts)"
SEGMENT_MENU_HEADER_STR = "Segments"
TAG_MENU_HEADER_STR = "Tags"
NEW_TAG_PLACEHOLDER_STR = "Add Tag..."
OPEN_VIDEO_STR = "Open Video for Segmenting"
OPEN_VIDEO_STR = "Open Video for Tagging"
CREATE_SEGMENT_BUTTON = "Create Segment"
EXPORT_SEGMENTS_TITLE = "Export Segments as CSV"
EXPORT_SEGMENTS_TYPES = "CSV (*.csv)"
Expand Down
12 changes: 10 additions & 2 deletions src/store/persist.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
from functools import wraps
import os

from ..components.serialize import Serializer
from ..registry import Registry

def persist_state(f):
@wraps(f)
def wrapped(*args, **kwargs):
r = f(*args, **kwargs)

json = Serializer.store_to_json(args[0])
with open('range-tagger.seg', 'w') as file:
file.write(json)

try:
with open(Registry.CONFIG_FILE_LOCATION, 'w') as file:
file.write(json)
except FileNotFoundError:
os.makedirs(os.path.dirname(Registry.CONFIG_FILE_LOCATION))
with open(Registry.CONFIG_FILE_LOCATION, 'w') as file:
file.write(json)

return r
return wrapped

0 comments on commit 1968074

Please sign in to comment.