Skip to content

Commit

Permalink
Add behavior text display and active flag handling in GUI
Browse files Browse the repository at this point in the history
- Introduced  and  methods to process active flags and generate a behavior text string.
- Updated  to set the behavior text on the canvas.
- Enhanced  to support displaying current behavior text, with customizable position, font, and color.
- Modified  in  to render the behavior text dynamically on the pixmap.
  • Loading branch information
healthonrails committed Dec 20, 2024
1 parent c6e3e6a commit 64278a0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
15 changes: 14 additions & 1 deletion annolid/gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,17 @@ def handle_flags_saved(self, flags={}):
def handle_row_selected(self, flag_name: str):
self.event_type = flag_name

def get_active_flags(self, flags):
active_flags = []
for _flag in flags:
if flags[_flag]:
active_flags.append(_flag)
return active_flags

def get_current_behavior_text(self, flags):
active_flags = self.get_active_flags(flags)
return ','.join(active_flags)

def _grounding_sam(self):
"""
Handles the text prompt inputs for grouding DINO SAM.
Expand Down Expand Up @@ -1279,6 +1290,8 @@ def reset_predict_button(self):

def loadFlags(self, flags):
""" Loads flags using FlagTableWidget's loadFlags method """
behave_text = self.get_current_behavior_text(flags)
self.canvas.setBehaviorText(behave_text)
self.flag_widget.loadFlags(flags)

def saveLabels(self, filename):
Expand Down Expand Up @@ -1310,7 +1323,7 @@ def format_shape(s):
shapes = [format_shape(item.shape()) for item in self.labelList]
flags = {}
if self.flag_widget:
# # Retrieve the flags as a dictionary
# Retrieve the flags as a dictionary
flags = {_flag: True for _flag in self.flag_widget._get_existing_flag_names(
) if self.is_behavior_active(self.frame_number, _flag)}
try:
Expand Down
36 changes: 28 additions & 8 deletions annolid/gui/widgets/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ def __init__(self, *args, **kwargs):
self.sam_predictor = None
self.sam_hq_model = None
self.sam_mask = MaskShape()
self.behavior_text_position = "top-left" # Default position
self.behavior_text_color = QtGui.QColor(255, 255, 255)
self.behavior_text_background = None # Optional background color
self.current_behavior_text = None

def fillDrawing(self):
return self._fill_drawing
Expand All @@ -142,6 +146,9 @@ def setFillDrawing(self, value):
def setCaption(self, text):
self.caption_label.setText(text)

def setBehaviorText(self, text):
self.current_behavior_text = text

def getCaption(self):
return self.caption_label.toPlainText()

Expand Down Expand Up @@ -1005,9 +1012,6 @@ def boundedShiftShapes(self, shapes):
self.boundedMoveShapes(shapes, point + offset)

def paintEvent(self, event):
if not self.pixmap:
return super(Canvas, self).paintEvent(event)

p = self._painter
p.begin(self)
p.setRenderHint(QtGui.QPainter.Antialiasing)
Expand All @@ -1020,14 +1024,30 @@ def paintEvent(self, event):
p.drawPixmap(0, 0, self.pixmap)
self.sam_mask.paint(p)

if self.current_behavior_text and len(self.current_behavior_text) > 0:
# Calculate font size based on pixmap size
pixmap_size = min(self.pixmap.width(), self.pixmap.height())
font_size = max(20, pixmap_size // 40) # Adjust divisor as needed

font = QtGui.QFont()
font.setPointSize(font_size)
font.setBold(True)
p.setFont(font)
p.setPen(QtGui.QColor(255, 255, 255)) # White text

# Adjust position based on font size
text_x = 10
text_y = font_size + 10 # Add some padding

# Position and text
p.drawText(text_x, text_y, self.current_behavior_text)

# draw crosshair
if ((not self.createMode == 'grounding_sam')
and (self._crosshair[self._createMode]
and self.drawing()
and self.prevMovePoint
and not self.outOfPixmap(self.prevMovePoint)
)):
p.setPen(QtGui.QColor(0, 0, 0))
and self.drawing())):
p.setPen(QtGui.QPen(QtGui.QColor(
255, 255, 255), 1, QtCore.Qt.DotLine))
p.drawLine(
0,
int(self.prevMovePoint.y()),
Expand Down

0 comments on commit 64278a0

Please sign in to comment.