Skip to content

Commit

Permalink
Add functionality to display captions for video frames from JSON file
Browse files Browse the repository at this point in the history
- Parse captions from the provided JSON file
- Display the corresponding caption on each video frame
  • Loading branch information
healthonrails committed Oct 2, 2024
1 parent 91f0406 commit acfcc16
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
8 changes: 3 additions & 5 deletions annolid/annotation/keypoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ def save_labels(filename, imagePath,
return
lf = LabelFile()
shapes = [format_shape(shape) for shape in label_list]



# Load existing shapes from the JSON file and merge with new shapes
existing_shapes = load_existing_shapes(filename)

Expand All @@ -115,8 +114,6 @@ def save_labels(filename, imagePath,
# Set default value for otherData
if otherData is None:
otherData = {}
if caption is not None:
otherData['caption'] = caption

if flags is not None:
flags = flags
Expand All @@ -132,7 +129,8 @@ def save_labels(filename, imagePath,
imageHeight=height,
imageWidth=width,
otherData=otherData,
flags=flags
flags=flags,
caption=caption,
)


Expand Down
3 changes: 3 additions & 0 deletions annolid/gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,7 @@ def format_shape(s):
imageWidth=self.image.width(),
otherData=self.otherData,
flags=flags,
caption=self.canvas.getCaption(),
)
logger.info(f"Saved image and label json file: {filename}")
if has_zone_shapes:
Expand Down Expand Up @@ -2321,6 +2322,8 @@ def loadPredictShapes(self, frame_number, filename):
is_video_frame=True)
if self.labelFile:
self.loadLabels(self.labelFile.shapes)
caption = self.labelFile.get_caption()
self.canvas.setCaption(caption)
except Exception as e:
print(e)

Expand Down
12 changes: 12 additions & 0 deletions annolid/gui/label_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(self,
self.imagePath = None
self.imageData = None
self.is_video_frame = is_video_frame
self.caption = None
if filename is not None:
self.load(filename)
self.filename = filename
Expand Down Expand Up @@ -77,6 +78,7 @@ def load(self, filename):
"flags", # image level flags
"imageHeight",
"imageWidth",
"caption",
]
shape_keys = [
"label",
Expand All @@ -93,6 +95,10 @@ def load(self, filename):
data = json.load(f)
imageData = None
imagePath = None
caption = None

if data['caption'] is not None:
caption = data['caption']

if data["imageData"] is not None:
imageData = base64.b64decode(data["imageData"])
Expand Down Expand Up @@ -142,6 +148,10 @@ def load(self, filename):
self.imageData = imageData
self.filename = filename
self.otherData = otherData
self.caption = caption

def get_caption(self):
return self.caption

@staticmethod
def _check_image_height_and_width(imageData, imageHeight, imageWidth):
Expand Down Expand Up @@ -170,6 +180,7 @@ def save(
imageData=None,
otherData=None,
flags=None,
caption=None,
):
if imageData is not None:
imageData = base64.b64encode(imageData).decode("utf-8")
Expand All @@ -188,6 +199,7 @@ def save(
imageData=imageData,
imageHeight=imageHeight,
imageWidth=imageWidth,
caption=caption,
)
for key, value in otherData.items():
assert key not in data
Expand Down
35 changes: 35 additions & 0 deletions annolid/gui/widgets/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def __init__(self, *args, **kwargs):
self.double_click
)
)
self.caption_label = QtWidgets.QTextEdit()
self.num_backups = kwargs.pop("num_backups", 10)
self._crosshair = kwargs.pop(
"crosshair",
Expand Down Expand Up @@ -138,6 +139,12 @@ def fillDrawing(self):
def setFillDrawing(self, value):
self._fill_drawing = value

def setCaption(self, text):
self.caption_label.setText(text)

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

@property
def createMode(self):
return self._createMode
Expand Down Expand Up @@ -1013,6 +1020,34 @@ def paintEvent(self, event):
p.drawPixmap(0, 0, self.pixmap)
self.sam_mask.paint(p)

# Draw text from the label at the desired position
if self.caption_label.toPlainText():
text = self.caption_label.toPlainText()

# Define a bounding rectangle for the text (adjust the position and size as needed)
rect = QtCore.QRect(5, 3, 500, 55) # x, y, width, height

# Set the background color (optional)
background_color = QtGui.QColor(
255, 255, 255, 128) # White background
# Fill the rectangle with the background color
p.fillRect(rect, background_color)

# Set the font for the text
# Change to your preferred font and size
font = QtGui.QFont("Arial", 8)
p.setFont(font)

# Set text color
text_color = QtGui.QColor(0, 0, 0) # Black text color
p.setPen(text_color)

# Set text alignment and enable word wrapping
alignment = QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop # Adjust alignment as needed

# Draw the text within the rectangle with word wrapping
p.drawText(rect, alignment | QtCore.Qt.TextWordWrap, text)

# draw crosshair
if ((not self.createMode == 'grounding_sam')
and (self._crosshair[self._createMode]
Expand Down

0 comments on commit acfcc16

Please sign in to comment.