Skip to content

Commit e3001da

Browse files
committed
Added interval to apply OpenCV effects
1 parent 2b71b61 commit e3001da

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

src/classes/effect_init.py

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
"y": 0.05,
9696
"width": 0.25,
9797
"height": 0.25,
98+
"first-frame": 1,
9899
"type": "rect"
99100
},
100101

src/windows/process_effect.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ def __init__(self, clip_id, effect_name, effect_params):
5959
self.effect_name = effect_name
6060
self.context = {}
6161

62+
timeline_instance = get_app().window.timeline_sync.timeline
63+
for clip_instance in timeline_instance.Clips():
64+
if clip_instance.Id() == self.clip_id:
65+
self.clip_instance = clip_instance
66+
break
67+
6268
# Load UI from designer & init
6369
ui_util.load_ui(self, self.ui_path)
6470
ui_util.init_ui(self)
@@ -233,10 +239,11 @@ def rect_select_clicked(self, widget, param):
233239
reader_path = c.data.get('reader', {}).get('path','')
234240
f = File.get(path=reader_path)
235241
if f:
236-
win = SelectRegion(f, c)
242+
win = SelectRegion(f, self.clip_instance)
237243
# Run the dialog event loop - blocking interaction on this window during that time
238244
result = win.exec_()
239245
if result == QDialog.Accepted:
246+
# self.first_frame = win.current_frame
240247
# Region selected (get coordinates if any)
241248
topLeft = win.videoPreview.regionTopLeftHandle
242249
bottomRight = win.videoPreview.regionBottomRightHandle
@@ -266,6 +273,7 @@ def rect_select_clicked(self, widget, param):
266273
"scaled_x": topLeft.x() / viewPortSize.width(), "scaled_y": topLeft.y() / viewPortSize.height(),
267274
"scaled_width": (bottomRight.x() - topLeft.x()) / viewPortSize.width(),
268275
"scaled_height": (bottomRight.y() - topLeft.y()) / viewPortSize.height(),
276+
"first-frame": win.current_frame
269277
})
270278
log.info(self.context)
271279

@@ -286,11 +294,6 @@ def accept(self):
286294

287295
# DO WORK HERE, and periodically set progressBar value
288296
# Access C++ timeline and find the Clip instance which this effect should be applied to
289-
timeline_instance = get_app().window.timeline_sync.timeline
290-
for clip_instance in timeline_instance.Clips():
291-
if clip_instance.Id() == self.clip_id:
292-
self.clip_instance = clip_instance
293-
break
294297

295298
# Create effect Id and protobuf data path
296299
ID = get_app().project.generate_id()
@@ -365,6 +368,10 @@ def generateJson(self, protobufPath):
365368
tracker_dict = self.context["region"]
366369
bbox = (tracker_dict["x"],tracker_dict["y"],tracker_dict["width"],tracker_dict["height"])
367370
jsonString += ',"bbox": {"x": %d, "y": %d, "w": %d, "h": %d}' % (bbox)
371+
372+
# Get processing start frame
373+
print(self.context["region"])
374+
jsonString +=',"first_frame": %d' % (self.context["region"]["first-frame"])
368375

369376
# Finish JSON string
370377
jsonString+='}'

src/windows/region.py

+37-17
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,30 @@ def __init__(self, file=None, clip=None):
7878
self.start_image = None
7979
self.end_frame = 1
8080
self.end_image = None
81+
self.current_frame = 1
82+
83+
self.clip = clip
84+
self.clip.Open()
85+
self.clip_position = self.clip.Position()
86+
self.clip.Position(0)
8187

8288
# Keep track of file object
83-
self.file = file
84-
self.file_path = file.absolute_path()
85-
self.video_length = int(file.data['video_length'])
86-
self.fps_num = int(file.data['fps']['num'])
87-
self.fps_den = int(file.data['fps']['den'])
88-
self.fps = float(self.fps_num) / float(self.fps_den)
89-
self.width = int(file.data['width'])
90-
self.height = int(file.data['height'])
91-
self.sample_rate = int(file.data['sample_rate'])
92-
self.channels = int(file.data['channels'])
93-
self.channel_layout = int(file.data['channel_layout'])
89+
# self.file = file
90+
# self.file_path = file.absolute_path()
91+
92+
c_info = clip.Reader().info
93+
self.fps = c_info.fps.ToInt() #float(self.fps_num) / float(self.fps_den)
94+
self.fps_num = self.fps #int(file.data['fps']['num'])
95+
self.fps_den = 1 #int(file.data['fps']['den'])
96+
self.width = c_info.width #int(file.data['width'])
97+
self.height = c_info.height #int(file.data['height'])
98+
self.sample_rate = c_info.sample_rate #int(file.data['sample_rate'])
99+
self.channels = c_info.channels #int(file.data['channels'])
100+
self.channel_layout = c_info.channel_layout #int(file.data['channel_layout'])
101+
self.video_length = int(self.clip.Duration() * self.fps) + 1 #int(file.data['video_length'])
94102

95103
# Open video file with Reader
96-
log.info(self.file_path)
104+
log.info(self.clip.Reader())
97105

98106
# Add Video Widget
99107
self.videoPreview = VideoWidget()
@@ -110,7 +118,7 @@ def __init__(self, file=None, clip=None):
110118

111119
try:
112120
# Add clip for current preview file
113-
self.clip = openshot.Clip(self.file_path)
121+
# self.clip = openshot.Clip(self.file_path)
114122

115123
# Show waveform for audio files
116124
if not self.clip.Reader().info.has_video and self.clip.Reader().info.has_audio:
@@ -123,6 +131,7 @@ def __init__(self, file=None, clip=None):
123131
self.r.info.video_length = self.video_length
124132

125133
self.r.AddClip(self.clip)
134+
126135
except:
127136
log.error('Failed to load media file into region select player: %s' % self.file_path)
128137
return
@@ -147,8 +156,8 @@ def __init__(self, file=None, clip=None):
147156

148157
# Determine if a start or end attribute is in this file
149158
start_frame = 1
150-
if 'start' in self.file.data.keys():
151-
start_frame = (float(self.file.data['start']) * self.fps) + 1
159+
# if 'start' in self.file.data.keys():
160+
# start_frame = (float(self.file.data['start']) * self.fps) + 1
152161

153162
# Display start frame (and then the previous frame)
154163
QTimer.singleShot(500, functools.partial(self.sliderVideo.setValue, start_frame + 1))
@@ -166,7 +175,7 @@ def __init__(self, file=None, clip=None):
166175
self.sliderVideo.valueChanged.connect(self.sliderVideo_valueChanged)
167176
self.initialized = True
168177

169-
get_app().window.SelectRegionSignal.emit(clip.id)
178+
get_app().window.SelectRegionSignal.emit(clip.Id())
170179

171180
def actionPlay_Triggered(self):
172181
# Trigger play button (This action is invoked from the preview thread, so it must exist here)
@@ -175,6 +184,7 @@ def actionPlay_Triggered(self):
175184
def movePlayhead(self, frame_number):
176185
"""Update the playhead position"""
177186

187+
self.current_frame = frame_number
178188
# Move slider to correct frame position
179189
self.sliderIgnoreSignal = True
180190
self.sliderVideo.setValue(frame_number)
@@ -222,10 +232,16 @@ def sliderVideo_valueChanged(self, new_frame):
222232

223233
def accept(self):
224234
""" Ok button clicked """
235+
236+
self.clip.Position(self.clip_position)
237+
225238
self.shutdownPlayer()
226239
super(SelectRegion, self).accept()
227240

228241
def shutdownPlayer(self):
242+
243+
self.clip.Position(self.clip_position)
244+
229245
log.info('shutdownPlayer')
230246

231247
# Stop playback
@@ -237,11 +253,15 @@ def shutdownPlayer(self):
237253
self.preview_parent.background.wait(5000)
238254

239255
# Close readers
256+
self.r.RemoveClip(self.clip)
240257
self.r.Close()
241-
self.clip.Close()
258+
# self.clip.Close()
242259
self.r.ClearAllCache()
243260

244261
def reject(self):
262+
263+
self.clip.Position(self.clip_position)
264+
245265
# Cancel dialog
246266
self.shutdownPlayer()
247267
super(SelectRegion, self).reject()

0 commit comments

Comments
 (0)