-
Notifications
You must be signed in to change notification settings - Fork 429
Person ReID and keypoint retrieval #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
theairbend3r
wants to merge
8
commits into
facebookresearch:main
Choose a base branch
from
theairbend3r:person-reid-keypoint-retrieval
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f470358
Add new file.
theairbend3r d4a5728
Initial commit.
theairbend3r 5917713
Refactored functions to hooks.
theairbend3r c386a85
Center keypoint coordinates per bbox.
theairbend3r a0b1dcc
L2 norm before dot product.
theairbend3r 958fc27
Run linter.
theairbend3r e126c8c
Replace dot product with cosine similarity.
theairbend3r 8848387
Populate frame tracker after decoding video.
theairbend3r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import cv2 | ||
| import torch | ||
|
|
||
|
|
||
| def process_video(video_path, model, max_frames): | ||
| video = cv2.VideoCapture(video_path) | ||
|
|
||
| frame_count = 0 | ||
|
|
||
| frame_tracker = [] | ||
|
|
||
| while True: | ||
| is_frame, frame = video.read() | ||
|
|
||
| if not is_frame: | ||
| break | ||
|
|
||
| # generate model output | ||
| model_outputs = model.predict(frame) | ||
|
|
||
| # get bounding boxes (x1, y1, x2, y2) | ||
| bboxes_per_frame = model_outputs["instances"][ | ||
| model_outputs["instances"].pred_classes == 0 | ||
| ].pred_boxes | ||
| bboxes_per_frame = bboxes_per_frame.tensor.to("cpu").squeeze() | ||
|
|
||
| # calculate bbox center (x1, y1, x2, y2) | ||
| bboxes_per_frame_center_x = ( | ||
| bboxes_per_frame[:, 0] + bboxes_per_frame[:, 2] | ||
| ) / 2 # (x1+x2)/2 | ||
| bboxes_per_frame_center_y = ( | ||
| bboxes_per_frame[:, 1] + bboxes_per_frame[:, 3] | ||
| ) / 2 # (y1+y2)/2 | ||
|
|
||
| # get keypoints | ||
| keypoints_per_frame = model_outputs["instances"][ | ||
| model_outputs["instances"].pred_classes == 0 | ||
| ].pred_keypoints | ||
| keypoints_per_frame = keypoints_per_frame[:, :, :2].to("cpu") | ||
|
|
||
| # change origin of the keypoints to center of each bbox | ||
| keypoints_per_frame[:, :, 0] = keypoints_per_frame[ | ||
| :, :, 0 | ||
| ] - bboxes_per_frame_center_x.unsqueeze(1) | ||
| keypoints_per_frame[:, :, 1] = keypoints_per_frame[ | ||
| :, :, 1 | ||
| ] - bboxes_per_frame_center_y.unsqueeze(1) | ||
|
|
||
| for i in range(bboxes_per_frame.shape[0]): | ||
| bbox_coord = bboxes_per_frame[i, :] | ||
| keypoint_per_bbox = keypoints_per_frame[i, :, :] | ||
|
|
||
| bbox_info = { | ||
| "bbox_id": i, | ||
| "person_id": None, | ||
| "frame_id": frame_count, | ||
| "bbox_coord": bbox_coord, | ||
| "keypoint_coord": keypoint_per_bbox, | ||
| } | ||
|
|
||
| frame_tracker.append(bbox_info) | ||
|
|
||
| frame_count += 1 | ||
|
|
||
| if frame_count == max_frames: | ||
| break | ||
|
|
||
| video.release() | ||
| cv2.destroyAllWindows() | ||
|
|
||
| return frame_tracker | ||
|
|
||
|
|
||
| def create_keypoint_features_db(frame_tracker): | ||
|
theairbend3r marked this conversation as resolved.
Outdated
|
||
| return torch.stack([bbox["keypoint_coord"].flatten() for bbox in frame_tracker]) | ||
|
|
||
|
|
||
| def calculate_distance(action_query, keypoint_db): | ||
| scores = action_query @ keypoint_db.T | ||
| return scores | ||
|
|
||
|
|
||
| def get_closest_matches(scores, method, n): | ||
| if method == "topk": | ||
| return torch.topk(scores, n).indices.squeeze() | ||
| elif method == "softmax": | ||
| score_probs = torch.nn.functional.softmax(scores, dim=1) | ||
| return (score_probs > n).squeeze().nonzero() | ||
|
|
||
|
|
||
| ## workflow | ||
|
|
||
| # frame_tracker = process_video( | ||
| # video_path="./sample_video.mp4", model=keypoint_detection_model, max_frames=5 | ||
| # ) | ||
| # | ||
| # keypoint_db = create_keypoint_features_db(frame_tracker) | ||
| # | ||
| # scores = calculate_distance(action_query, keypoint_db) | ||
| # | ||
| # best_bbox_matches = get_closest_matches(scores, method="topk", n=10) | ||
| # | ||
| # for bbox_id in best_bbox_matches.tolist(): | ||
| # print(frame_tracker[bbox_id]["frame_id"]) | ||
Empty file.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.