-
I have keypoint of each node for each frame of a video, I need a code like below to create an SLEAP project. import sleap
from sleap import Labels, Video, LabeledFrame, Instance, Skeleton
from sleap.skeleton import Node
# Create a new SLEAP project
labels = Labels()
# Create a video object and add it to the project
video = Video.from_filename("video.mp4")
labels.add_video(video)
# Create a labeled frame and add it to the project
frame = LabeledFrame(video=video, frame_idx=0)
labels.frames.append(frame)
# Create a skeleton and add it to the project
skeleton = Skeleton()
skeleton.add_node(Node("head"))
skeleton.add_node(Node("tail"))
labels.add_skeleton(skeleton)
# Create an instance and add it to the labeled frame
instance = Instance(skeleton=skeleton)
instance.add_node("head", (100, 100))
instance.add_node("tail", (200, 200))
frame.add_instance(instance)
# Save the project to a SLEAP file
sleap.save_file(labels, "labels.slp") |
Beta Was this translation helpful? Give feedback.
Answered by
roomrys
Oct 5, 2023
Replies: 2 comments 3 replies
-
Hi @mmmahdiii, When creating a import sleap
from sleap import Labels, Video, LabeledFrame, Instance, Skeleton
from sleap.instance import Point
from sleap.skeleton import Node
# Create a video object
video_path = r"path/to/video.mp4"
video = Video.from_filename(video_path)
# Create a skeleton
nodes = ["head", "tail"]
skeleton = Skeleton(name="my_skeleton")
skeleton.add_nodes(nodes)
skeleton.add_edge("head", "tail")
# Initialize a list of labeled frames
lfs = []
# 0. Create a list of instances for this labeled frame
instances = []
# 1. Create an instance and append to list of instances
points = dict(head=Point(100, 100), tail=Point(200, 200))
inst = Instance(skeleton=skeleton, points=points)
instances.append(inst)
# 2. Create a labeled frame and add it to the list
frame = LabeledFrame(video=video, frame_idx=0, instances=instances)
lfs.append(frame)
# Repeat steps 0-2 for all frames you would like to add (preferably in a for-loop)
...
# Create the Labels object and save to an slp
ds = r"path/to/save/labels.slp"
labels = Labels(lfs)
labels.save(ds) Thanks, |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
deemok7
-
Hi @roomrys, Thanks a million! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @mmmahdiii,
When creating a
Labels
object, create it from the inside out by creating object that are referenced by other objects first: