Skip to content

Commit 0320094

Browse files
committed
ObjectDetection: updated object selection and transform handler
Removed the necessity to append the detected object index (related to the frame) to the effect JSON - which makes the JSON smaller and the performance better.
1 parent f94ccb5 commit 0320094

File tree

4 files changed

+130
-166
lines changed

4 files changed

+130
-166
lines changed

src/windows/main_window.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,12 +2391,7 @@ def addSelection(self, item_id, item_type, clear_existing=False):
23912391

23922392
effect = Effect.get(id=item_id)
23932393
if effect:
2394-
has_tracked_object = False
2395-
for effect_key in effect.data.keys():
2396-
if effect_key.startswith("box_id"):
2397-
has_tracked_object = True
2398-
break
2399-
if has_tracked_object:
2394+
if effect.data["has_tracked_object"]:
24002395
# Show bounding boxes transform on preview
24012396
clip_id = effect.parent['id']
24022397
self.KeyFrameTransformSignal.emit(item_id, clip_id)

src/windows/video_widget.py

Lines changed: 97 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -307,23 +307,18 @@ def paintEvent(self, event, *args):
307307
painter.setTransform(self.transform)
308308

309309
if self.transforming_effect:
310-
# Get properties of clip at current frame
311-
raw_properties_effect = json.loads(self.transforming_effect_object.PropertiesJSON(clip_frame_number))
312-
for effect_key in raw_properties_effect.keys():
313-
if effect_key.startswith("x1-"):
314-
# Get current tracked object index
315-
tracked_obj_idx = effect_key.split("-", 2)[1]
316-
# Check if the tracked object is visible in this frame
317-
visible = raw_properties_effect['visible-'+tracked_obj_idx]['value']
318-
if visible:
319-
# Get current bounding box values
320-
rotation = raw_properties_effect['rotation-'+tracked_obj_idx]['value']
321-
x1 = raw_properties_effect['x1-'+tracked_obj_idx]['value']
322-
y1 = raw_properties_effect['y1-'+tracked_obj_idx]['value']
323-
x2 = raw_properties_effect['x2-'+tracked_obj_idx]['value']
324-
y2 = raw_properties_effect['y2-'+tracked_obj_idx]['value']
325-
self.drawTransformHandler(painter, sx, sy, source_width, source_height, origin_x, origin_y,
326-
x1, y1, x2, y2, rotation)
310+
# Check if the effect has a tracked object
311+
if self.transforming_effect_object.info.has_tracked_object:
312+
# Get properties of clip at current frame
313+
raw_properties_effect = json.loads(self.transforming_effect_object.PropertiesJSON(clip_frame_number))
314+
# Get the selected bounding box values
315+
rotation = raw_properties_effect['rotation']['value']
316+
x1 = raw_properties_effect['x1']['value']
317+
y1 = raw_properties_effect['y1']['value']
318+
x2 = raw_properties_effect['x2']['value']
319+
y2 = raw_properties_effect['y2']['value']
320+
self.drawTransformHandler(painter, sx, sy, source_width, source_height, origin_x, origin_y,
321+
x1, y1, x2, y2, rotation)
327322
else:
328323
self.drawTransformHandler(painter, sx, sy, source_width, source_height, origin_x, origin_y)
329324

@@ -823,104 +818,91 @@ def mouseMoveEvent(self, event):
823818
self.original_clip_data = self.transforming_clip.data
824819

825820
_ = self.getTransformMode(0, 0, 0, event)
826-
827-
# Get properties of effect at current frame
828-
raw_properties = json.loads(self.transforming_effect_object.PropertiesJSON(clip_frame_number))
829-
830-
for raw_property_key in raw_properties.keys():
831-
if raw_property_key.startswith("x1-"):
832-
# Get current tracked object index
833-
tracked_obj_idx = raw_property_key.split("-", 2)[1]
834-
# Check if the tracked object is visible in this frame
835-
visible = raw_properties.get('visible-'+tracked_obj_idx).get('value')
836-
if visible:
837-
# Get current bounding box values
838-
rotation = raw_properties.get('rotation-'+tracked_obj_idx).get('value')
839-
x1 = raw_properties.get('x1-'+tracked_obj_idx).get('value')
840-
y1 = raw_properties.get('y1-'+tracked_obj_idx).get('value')
841-
x2 = raw_properties.get('x2-'+tracked_obj_idx).get('value')
842-
y2 = raw_properties.get('y2-'+tracked_obj_idx).get('value')
843-
844-
# Transform effect object
845-
if self.transform_mode:
846-
847-
if self.transform_mode == 'location':
848-
# Get current keyframe value
849-
location_x = raw_properties.get('delta_x-'+tracked_obj_idx).get('value')
850-
location_y = raw_properties.get('delta_y-'+tracked_obj_idx).get('value')
851-
852-
# Calculate new location coordinates
853-
location_x += (event.pos().x() - self.mouse_position.x()) / viewport_rect.width()
854-
location_y += (event.pos().y() - self.mouse_position.y()) / viewport_rect.height()
855-
856-
# Update keyframe value (or create new one)
857-
self.updateEffectProperty(self.transforming_effect.id, clip_frame_number, 'delta_x-'+tracked_obj_idx, location_x, refresh=False)
858-
self.updateEffectProperty(self.transforming_effect.id, clip_frame_number, 'delta_y-'+tracked_obj_idx, location_y)
859-
860-
elif self.transform_mode == 'rotation':
861-
# Get current rotation keyframe value
862-
rotation = raw_properties.get('rotation-'+tracked_obj_idx).get('value')
863-
scale_x = max(float(raw_properties.get('scale_x-'+tracked_obj_idx).get('value')), 0.001)
864-
scale_y = max(float(raw_properties.get('scale_y-'+tracked_obj_idx).get('value')), 0.001)
865-
866-
# Calculate new location coordinates
867-
is_on_left = event.pos().x() < self.originHandle.x()
868-
is_on_top = event.pos().y() < self.originHandle.y()
869-
870-
if is_on_top:
871-
rotation += (event.pos().x() - self.mouse_position.x()) / ((self.clipBounds.width() * scale_x) / 90)
872-
else:
873-
rotation -= (event.pos().x() - self.mouse_position.x()) / ((self.clipBounds.width() * scale_x) / 90)
874-
875-
if is_on_left:
876-
rotation -= (event.pos().y() - self.mouse_position.y()) / ((self.clipBounds.height() * scale_y) / 90)
877-
else:
878-
rotation += (event.pos().y() - self.mouse_position.y()) / ((self.clipBounds.height() * scale_y) / 90)
879-
880-
# Update keyframe value (or create new one)
881-
self.updateEffectProperty(self.transforming_effect.id, clip_frame_number, 'rotation-'+tracked_obj_idx, rotation)
882-
883-
elif self.transform_mode.startswith('scale_'):
884-
# Get current scale keyframe value
885-
scale_x = max(float(raw_properties.get('scale_x-'+tracked_obj_idx).get('value')), 0.001)
886-
scale_y = max(float(raw_properties.get('scale_y-'+tracked_obj_idx).get('value')), 0.001)
887-
888-
if self.transform_mode == 'scale_top_right':
889-
scale_x += (event.pos().x() - self.mouse_position.x()) / (self.clipBounds.width() / 2.0)
890-
scale_y -= (event.pos().y() - self.mouse_position.y()) / (self.clipBounds.height() / 2.0)
891-
elif self.transform_mode == 'scale_bottom_right':
892-
scale_x += (event.pos().x() - self.mouse_position.x()) / (self.clipBounds.width() / 2.0)
893-
scale_y += (event.pos().y() - self.mouse_position.y()) / (self.clipBounds.height() / 2.0)
894-
elif self.transform_mode == 'scale_top_left':
895-
scale_x -= (event.pos().x() - self.mouse_position.x()) / (self.clipBounds.width() / 2.0)
896-
scale_y -= (event.pos().y() - self.mouse_position.y()) / (self.clipBounds.height() / 2.0)
897-
elif self.transform_mode == 'scale_bottom_left':
898-
scale_x -= (event.pos().x() - self.mouse_position.x()) / (self.clipBounds.width() / 2.0)
899-
scale_y += (event.pos().y() - self.mouse_position.y()) / (self.clipBounds.height() / 2.0)
900-
elif self.transform_mode == 'scale_top':
901-
scale_y -= (event.pos().y() - self.mouse_position.y()) / (self.clipBounds.height() / 2.0)
902-
elif self.transform_mode == 'scale_bottom':
903-
scale_y += (event.pos().y() - self.mouse_position.y()) / (self.clipBounds.height() / 2.0)
904-
elif self.transform_mode == 'scale_left':
905-
scale_x -= (event.pos().x() - self.mouse_position.x()) / (self.clipBounds.width() / 2.0)
906-
elif self.transform_mode == 'scale_right':
907-
scale_x += (event.pos().x() - self.mouse_position.x()) / (self.clipBounds.width() / 2.0)
908-
909-
if int(QCoreApplication.instance().keyboardModifiers() & Qt.ControlModifier) > 0:
910-
# If CTRL key is pressed, fix the scale_y to the correct aspect ration
911-
if scale_x and scale_y:
912-
scale_y = scale_x
913-
elif scale_y:
914-
scale_x = scale_y
915-
elif scale_x:
916-
scale_y = scale_x
917-
918-
# Update keyframe value (or create new one)
919-
both_scaled = scale_x != 0.001 and scale_y != 0.001
920-
if scale_x != 0.001:
921-
self.updateEffectProperty(self.transforming_effect.id, clip_frame_number, 'scale_x-'+tracked_obj_idx, scale_x, refresh=(not both_scaled))
922-
if scale_y != 0.001:
923-
self.updateEffectProperty(self.transforming_effect.id, clip_frame_number, 'scale_y-'+tracked_obj_idx, scale_y)
821+
822+
if self.transforming_effect_object.info.has_tracked_object:
823+
# Get properties of effect at current frame
824+
raw_properties = json.loads(self.transforming_effect_object.PropertiesJSON(clip_frame_number))
825+
826+
# Transform effect object
827+
if self.transform_mode:
828+
829+
if self.transform_mode == 'location':
830+
# Get current keyframe value
831+
location_x = raw_properties.get('delta_x').get('value')
832+
location_y = raw_properties.get('delta_y').get('value')
833+
834+
# Calculate new location coordinates
835+
location_x += (event.pos().x() - self.mouse_position.x()) / viewport_rect.width()
836+
location_y += (event.pos().y() - self.mouse_position.y()) / viewport_rect.height()
837+
838+
# Update keyframe value (or create new one)
839+
self.updateEffectProperty(self.transforming_effect.id, clip_frame_number, 'delta_x', location_x, refresh=False)
840+
self.updateEffectProperty(self.transforming_effect.id, clip_frame_number, 'delta_y', location_y)
841+
842+
elif self.transform_mode == 'rotation':
843+
# Get current rotation keyframe value
844+
rotation = raw_properties.get('rotation').get('value')
845+
scale_x = max(float(raw_properties.get('scale_x').get('value')), 0.001)
846+
scale_y = max(float(raw_properties.get('scale_y').get('value')), 0.001)
847+
848+
# Calculate new location coordinates
849+
is_on_left = event.pos().x() < self.originHandle.x()
850+
is_on_top = event.pos().y() < self.originHandle.y()
851+
852+
if is_on_top:
853+
rotation += (event.pos().x() - self.mouse_position.x()) / ((self.clipBounds.width() * scale_x) / 90)
854+
else:
855+
rotation -= (event.pos().x() - self.mouse_position.x()) / ((self.clipBounds.width() * scale_x) / 90)
856+
857+
if is_on_left:
858+
rotation -= (event.pos().y() - self.mouse_position.y()) / ((self.clipBounds.height() * scale_y) / 90)
859+
else:
860+
rotation += (event.pos().y() - self.mouse_position.y()) / ((self.clipBounds.height() * scale_y) / 90)
861+
862+
# Update keyframe value (or create new one)
863+
self.updateEffectProperty(self.transforming_effect.id, clip_frame_number, 'rotation', rotation)
864+
865+
elif self.transform_mode.startswith('scale_'):
866+
# Get current scale keyframe value
867+
scale_x = max(float(raw_properties.get('scale_x').get('value')), 0.001)
868+
scale_y = max(float(raw_properties.get('scale_y').get('value')), 0.001)
869+
870+
if self.transform_mode == 'scale_top_right':
871+
scale_x += (event.pos().x() - self.mouse_position.x()) / (self.clipBounds.width() / 2.0)
872+
scale_y -= (event.pos().y() - self.mouse_position.y()) / (self.clipBounds.height() / 2.0)
873+
elif self.transform_mode == 'scale_bottom_right':
874+
scale_x += (event.pos().x() - self.mouse_position.x()) / (self.clipBounds.width() / 2.0)
875+
scale_y += (event.pos().y() - self.mouse_position.y()) / (self.clipBounds.height() / 2.0)
876+
elif self.transform_mode == 'scale_top_left':
877+
scale_x -= (event.pos().x() - self.mouse_position.x()) / (self.clipBounds.width() / 2.0)
878+
scale_y -= (event.pos().y() - self.mouse_position.y()) / (self.clipBounds.height() / 2.0)
879+
elif self.transform_mode == 'scale_bottom_left':
880+
scale_x -= (event.pos().x() - self.mouse_position.x()) / (self.clipBounds.width() / 2.0)
881+
scale_y += (event.pos().y() - self.mouse_position.y()) / (self.clipBounds.height() / 2.0)
882+
elif self.transform_mode == 'scale_top':
883+
scale_y -= (event.pos().y() - self.mouse_position.y()) / (self.clipBounds.height() / 2.0)
884+
elif self.transform_mode == 'scale_bottom':
885+
scale_y += (event.pos().y() - self.mouse_position.y()) / (self.clipBounds.height() / 2.0)
886+
elif self.transform_mode == 'scale_left':
887+
scale_x -= (event.pos().x() - self.mouse_position.x()) / (self.clipBounds.width() / 2.0)
888+
elif self.transform_mode == 'scale_right':
889+
scale_x += (event.pos().x() - self.mouse_position.x()) / (self.clipBounds.width() / 2.0)
890+
891+
if int(QCoreApplication.instance().keyboardModifiers() & Qt.ControlModifier) > 0:
892+
# If CTRL key is pressed, fix the scale_y to the correct aspect ration
893+
if scale_x and scale_y:
894+
scale_y = scale_x
895+
elif scale_y:
896+
scale_x = scale_y
897+
elif scale_x:
898+
scale_y = scale_x
899+
900+
# Update keyframe value (or create new one)
901+
both_scaled = scale_x != 0.001 and scale_y != 0.001
902+
if scale_x != 0.001:
903+
self.updateEffectProperty(self.transforming_effect.id, clip_frame_number, 'scale_x', scale_x, refresh=(not both_scaled))
904+
if scale_y != 0.001:
905+
self.updateEffectProperty(self.transforming_effect.id, clip_frame_number, 'scale_y', scale_y)
924906

925907
# Force re-paint
926908
self.update()

0 commit comments

Comments
 (0)