google-research
109 строк · 3.0 Кб
1# coding=utf-8
2# Copyright 2024 The Google Research Authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""From a directory of images and keypoint protobufs, shows images, with kps.
17
18Invocation:
19$ python -m code/show_keypoints <image_dir> [<mesh_file>].
20"""
21
22import glob23import os24import sys25
26import cv227import matplotlib.pyplot as plt28import numpy as np29
30from keypose import utils31
32colors = 255 * plt.cm.get_cmap('rainbow')(np.linspace(0, 1.0, 10))[:, :3]33
34
35def draw_circle(image, uvd, color, size=3):36# Filled color circle.37cv2.circle(image, (int(uvd[0]), int(uvd[1])), size, color, -1)38# White outline.39cv2.circle(image, (int(uvd[0]), int(uvd[1])), size + 1, (255, 255, 255))40
41
42def show_keypoints(image_dir, mesh_file):43"""Display keypoints in a keypose data file."""44print('Looking for images in %s' % image_dir)45filenames = glob.glob(os.path.join(image_dir, '*_L.png'))46if not filenames:47print("Couldn't find any PNG files in %s" % image_dir)48exit(-1)49filenames.sort()50print('Found %d files in %s' % (len(filenames), image_dir))51
52obj = None53if mesh_file:54obj = utils.read_mesh(mesh_file)55
56for fname in filenames:57im_l = utils.read_image(fname)58im_r = utils.read_image(fname.replace('_L.png', '_R.png'))59im_mask = utils.read_image(fname.replace('_L.png', '_mask.png'))60im_border = utils.read_image(fname.replace('_L.png', '_border.png'))61cam, _, _, uvds, _, _, transform = utils.read_contents_pb(62fname.replace('_L.png', '_L.pbtxt')63)64print(fname)65ret = show_kps(im_l, im_r, im_mask, im_border, (cam, uvds, transform), obj)66if ret:67break68
69
70def show_kps(im_l, im_r, im_border, im_mask, kps, obj=None, size=3):71"""Draw left/right images and keypoints using OpenCV."""72cam, uvds, _ = kps73
74im_l = cv2.cvtColor(im_l, cv2.COLOR_BGR2RGB)75im_r = cv2.cvtColor(im_r, cv2.COLOR_BGR2RGB)76
77uvds = np.array(uvds)78for i, uvd in enumerate(uvds):79draw_circle(im_l, uvd, colors[i * 3], size)80
81if obj:82p_matrix = utils.p_matrix_from_camera(cam)83q_matrix = utils.q_matrix_from_camera(cam)84xyzs = utils.project_np(q_matrix, uvds.T)85obj.project_to_uvd(xyzs, p_matrix)86im_l = obj.draw_points(im_l)87
88cv2.imshow('Image Left', im_l)89cv2.imshow('Border', im_border)90cv2.imshow('Mask', im_mask)91
92key = cv2.waitKey()93if key == ord('q'):94return True95return False96
97
98def main():99image_dir = sys.argv[1]100if len(sys.argv) < 3:101mesh_file = None102else:103mesh_file = sys.argv[2]104
105show_keypoints(image_dir, mesh_file)106
107
108if __name__ == '__main__':109main()110