├── requirements.txt ├── carfusion_coco_setup.sh ├── visualize_carfusion_coco.py ├── README.md ├── LICENSE ├── carfusion2coco.py └── Visualize.py /requirements.txt: -------------------------------------------------------------------------------- 1 | numpy 2 | googledrivedownloader 3 | shapely 4 | glob3 5 | opencv-python 6 | shapely 7 | requests 8 | pycocotools 9 | matplotlib 10 | scikit-image 11 | -------------------------------------------------------------------------------- /carfusion_coco_setup.sh: -------------------------------------------------------------------------------- 1 | ## download the data from the carfusion website 2 | 3 | python download_carfusion.py 4 | 5 | ## scripts to convert hte data from carfusion to coco format 6 | python carfusion2coco.py --path_dir ./datasets/carfusion/train/ --label_dir gt --image_dir images_jpg --output_dir ./datasets/carfusion/annotations/ --output_filename car_keypoints_train.json 7 | python carfusion2coco.py --path_dir ./datasets/carfusion/test/ --label_dir gt --image_dir images_jpg --output_dir ./datasets/carfusion/annotations/ --output_filename car_keypoints_test.json 8 | -------------------------------------------------------------------------------- /visualize_carfusion_coco.py: -------------------------------------------------------------------------------- 1 | from pycocotools.coco import COCO 2 | import numpy as np 3 | import skimage.io as io 4 | import matplotlib.pyplot as plt 5 | import pylab 6 | pylab.rcParams['figure.figsize'] = (8.0, 10.0) 7 | 8 | dataDir='' 9 | dataType='keypoints_train' 10 | 11 | 12 | # initialize COCO api for person keypoints annotations 13 | #annFile = '{}/annotations/person_keypoints_{}.json'.format(dataDir,dataType) 14 | annFile = '{}/annotations/car_{}.json'.format(dataDir,dataType) 15 | coco_kps=COCO(annFile) 16 | 17 | catIds = coco_kps.getCatIds(catNms=['car']); 18 | imgIds = coco_kps.getImgIds(catIds=catIds ); 19 | img = coco_kps.loadImgs(imgIds[np.random.randint(0,len(imgIds))])[0] 20 | 21 | print(coco_kps.getCatIds(),img) 22 | I = io.imread(dataDir+'train/'+img['file_name']) 23 | 24 | 25 | # load and display keypoints annotations 26 | plt.imshow(I); plt.axis('off') 27 | ax = plt.gca() 28 | annIds = coco_kps.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None) 29 | anns = coco_kps.loadAnns(annIds) 30 | coco_kps.showAnns(anns) 31 | plt.show() 32 | 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CarFusion: Combining Point Tracking and Part Detection for Dynamic 3D Reconstruction of Vehicle 2 | 3 | [N Dinesh Reddy](http://cs.cmu.edu/~dnarapur), [Minh Vo](http://www.cs.cmu.edu/~mvo), [Srinivasa G. Narasimhan](http://www.cs.cmu.edu/~srinivas/) 4 | 5 | IEEE Conference on Computer Vision and Pattern Recognition (CVPR), 2018. 6 | 7 | [[Project](http://www.cs.cmu.edu/~ILIM/projects/IM/CarFusion/)] [[Paper](http://www.cs.cmu.edu/~ILIM/publications/PDFs/RVN-CVPR18.pdf)] [[Supp](http://www.cs.cmu.edu/~ILIM/projects/IM/CarFusion/pdf/occlusion_net_supp.pdf)] 8 | 9 | ## Requirements 10 | - Python 3.6 11 | - Virtualenv 12 | - OpenCV 13 | - Numpy 14 | - Glob 15 | - googledrivedownloader 16 | - shapely 17 | - Cython 18 | 19 | ## Setup 20 | To download the data you need to fill the form [Access Form](https://forms.gle/FCUcbt3jD1hB6ja57) and convert it to coco format using the following commands: 21 | ``` 22 | virtualenv carfusion2coco -p python3.6 23 | source carfusion2coco/bin/activate 24 | pip install cython numpy 25 | pip install -r requirements.txt 26 | python download_carfusion.py (This file need to be downloaded by requesting, please fill to get access to the data) 27 | sh carfusion_coco_setup.sh 28 | ``` 29 | ## Dataset((14 Keypoints annotations for 100,000 cars(53,000 Images))) 30 | 31 | We provide mannual annotations of 14 semantic keypoints for 100,000 car instances (sedan, suv, bus, and truck) from 53,000 images captured from 18 moving cameras at Multiple intersections in Pittsburgh, PA. To view the labels, please run the following command: 32 | 33 | ## To visualize the data 34 | Visualization of the carfusion original labels 35 | ``` 36 | python Visualize.py PathToData CamID_FrameID 37 | ``` 38 | 39 | For example: 40 | ``` 41 | python Visualize.py ./datasets/carfusion/train/car_butler1/ 16_06401 42 | ``` 43 | 44 | 45 | Visualization of the coco format labels 46 | 47 | ``` 48 | python visualize_carfusion_coco.py 49 | ``` 50 | 51 | ### Citation 52 | ``` 53 | 54 | @InProceedings{Reddy_2018_CVPR, 55 | author = {Dinesh Reddy, N. and Vo, Minh and Narasimhan, Srinivasa G.}, 56 | title = {CarFusion: Combining Point Tracking and Part Detection for Dynamic 3D Reconstruction of Vehicles}, 57 | booktitle = {The IEEE Conference on Computer Vision and Pattern Recognition (CVPR)}, 58 | month = {June}, 59 | year = {2018} 60 | } 61 | ``` 62 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | SOFTWARE LICENSE AGREEMENT 2 | ACADEMIC OR NON-PROFIT ORGANIZATION NONCOMMERCIAL RESEARCH USE ONLY 3 | 4 | BY USING OR DOWNLOADING THE SOFTWARE, YOU ARE AGREEING TO THE TERMS OF THIS LICENSE AGREEMENT. IF YOU DO NOT AGREE WITH THESE TERMS, YOU MAY NOT USE OR DOWNLOAD THE SOFTWARE. 5 | 6 | This is a license agreement ("Agreement") between your academic institution or non-profit organization or self (called "Licensee" or "You" in this Agreement) and Carnegie Mellon University (called "Licensor" in this Agreement). All rights not specifically granted to you in this Agreement are reserved for Licensor. 7 | 8 | RESERVATION OF OWNERSHIP AND GRANT OF LICENSE: 9 | Licensor retains exclusive ownership of any copy of the Software (as defined below) licensed under this Agreement and hereby grants to Licensee a personal, non-exclusive, 10 | non-transferable license to use the Software for noncommercial research purposes, without the right to sublicense, pursuant to the terms and conditions of this Agreement. As used in this Agreement, the term "Software" means (i) the actual copy of all or any portion of code for program routines made accessible to Licensee by Licensor pursuant to this Agreement, inclusive of backups, updates, and/or merged copies permitted hereunder or subsequently supplied by Licensor, including all or any file structures, programming instructions, user interfaces and screen formats and sequences as well as any and all documentation and instructions related to it, and (ii) all or any derivatives and/or modifications created or made by You to any of the items specified in (i). 11 | 12 | CONFIDENTIALITY: Licensee acknowledges that the Software is proprietary to Licensor, and as such, Licensee agrees to receive all such materials in confidence and use the Software only in accordance with the terms of this Agreement. Licensee agrees to use reasonable effort to protect the Software from unauthorized use, reproduction, distribution, or publication. 13 | 14 | COPYRIGHT: The Software is owned by Licensor and is protected by United 15 | States copyright laws and applicable international treaties and/or conventions. 16 | 17 | PERMITTED USES: The Software may be used for your own noncommercial internal research purposes. You understand and agree that Licensor is not obligated to implement any suggestions and/or feedback you might provide regarding the Software, but to the extent Licensor does so, you are not entitled to any compensation related thereto. 18 | 19 | DERIVATIVES: You may create derivatives of or make modifications to the Software, however, You agree that all and any such derivatives and modifications will be owned by Licensor and become a part of the Software licensed to You under this Agreement. You may only use such derivatives and modifications for your own noncommercial internal research purposes, and you may not otherwise use, distribute or copy such derivatives and modifications in violation of this Agreement. 20 | 21 | BACKUPS: If Licensee is an organization, it may make that number of copies of the Software necessary for internal noncommercial use at a single site within its organization provided that all information appearing in or on the original labels, including the copyright and trademark notices are copied onto the labels of the copies. 22 | 23 | USES NOT PERMITTED: You may not distribute, copy or use the Software except as explicitly permitted herein. Licensee has not been granted any trademark license as part of this Agreement and may not use the name or mark “OpenPose", "Carnegie Mellon" or any renditions thereof without the prior written permission of Licensor. 24 | 25 | You may not sell, rent, lease, sublicense, lend, time-share or transfer, in whole or in part, or provide third parties access to prior or present versions (or any parts thereof) of the Software. 26 | 27 | ASSIGNMENT: You may not assign this Agreement or your rights hereunder without the prior written consent of Licensor. Any attempted assignment without such consent shall be null and void. 28 | 29 | TERM: The term of the license granted by this Agreement is from Licensee's acceptance of this Agreement by downloading the Software or by using the Software until terminated as provided below. 30 | 31 | The Agreement automatically terminates without notice if you fail to comply with any provision of this Agreement. Licensee may terminate this Agreement by ceasing using the Software. Upon any termination of this Agreement, Licensee will delete any and all copies of the Software. You agree that all provisions which operate to protect the proprietary rights of Licensor shall remain in force should breach occur and that the obligation of confidentiality described in this Agreement is binding in perpetuity and, as such, survives the term of the Agreement. 32 | 33 | FEE: Provided Licensee abides completely by the terms and conditions of this Agreement, there is no fee due to Licensor for Licensee's use of the Software in accordance with this Agreement. 34 | 35 | DISCLAIMER OF WARRANTIES: THE SOFTWARE IS PROVIDED "AS-IS" WITHOUT WARRANTY OF ANY KIND INCLUDING ANY WARRANTIES OF PERFORMANCE OR MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE OR PURPOSE OR OF NON-INFRINGEMENT. LICENSEE BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF THE SOFTWARE AND RELATED MATERIALS. 36 | 37 | SUPPORT AND MAINTENANCE: No Software support or training by the Licensor is provided as part of this Agreement. 38 | 39 | EXCLUSIVE REMEDY AND LIMITATION OF LIABILITY: To the maximum extent permitted under applicable law, Licensor shall not be liable for direct, indirect, special, incidental, or consequential damages or lost profits related to Licensee's use of and/or inability to use the Software, even if Licensor is advised of the possibility of such damage. 40 | 41 | EXPORT REGULATION: Licensee agrees to comply with any and all applicable 42 | U.S. export control laws, regulations, and/or other laws related to embargoes and sanction programs administered by the Office of Foreign Assets Control. 43 | 44 | SEVERABILITY: If any provision(s) of this Agreement shall be held to be invalid, illegal, or unenforceable by a court or other tribunal of competent jurisdiction, the validity, legality and enforceability of the remaining provisions shall not in any way be affected or impaired thereby. 45 | 46 | NO IMPLIED WAIVERS: No failure or delay by Licensor in enforcing any right or remedy under this Agreement shall be construed as a waiver of any future or other exercise of such right or remedy by Licensor. 47 | 48 | GOVERNING LAW: This Agreement shall be construed and enforced in accordance with the laws of the Commonwealth of Pennsylvania without reference to conflict of laws principles. You consent to the personal jurisdiction of the courts of this County and waive their rights to venue outside of Allegheny County, Pennsylvania. 49 | 50 | ENTIRE AGREEMENT AND AMENDMENTS: This Agreement constitutes the sole and entire agreement between Licensee and Licensor as to the matter set forth herein and supersedes any previous agreements, understandings, and arrangements between the parties relating hereto. 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /carfusion2coco.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | #import pims 4 | #import scipy.io 5 | import argparse 6 | import os 7 | #from IPython import embed 8 | import json 9 | import time 10 | #from PIL import Image 11 | import numpy as np 12 | 13 | #from scipy.misc import imsave 14 | #import cv2 15 | from shapely.geometry import Polygon 16 | 17 | def num(s): 18 | try: 19 | return int(s) 20 | except: 21 | return int(float(s)) 22 | 23 | 24 | def getAnnotation(instance): 25 | 26 | width, height = 1920, 1080 27 | 28 | valid_2 = instance[:, 2] == 1 29 | valid = instance[:, 2] == 2 30 | #print(instance) 31 | 32 | visible = np.logical_or(valid, valid_2) 33 | num_keypoints = int(np.sum(visible)) 34 | 35 | keypoints = np.zeros((14,3), dtype=np.int32) 36 | try: 37 | hull = Polygon([(x[0], x[1]) for x in instance[visible, :2]]).convex_hull 38 | frame = Polygon([(0, 0), (width, 0), (width, height), (0, height)]) 39 | hull = hull.intersection(frame).convex_hull 40 | 41 | bbox = hull.bounds 42 | w, h = bbox[2]-bbox[0], bbox[3]-bbox[1] 43 | x_o = max(bbox[0]-(w/10),0) 44 | y_o = max(bbox[1]-(h/10),0) 45 | x_i = min(x_o+(w/4)+w,width) 46 | y_i = min(y_o+(h/4)+h,height) 47 | bbox = [int(x_o), int(y_o), int(x_i - x_o), int(y_i - y_o)] 48 | 49 | segmentation = list(hull.convex_hull.exterior.coords)[:-1] 50 | segmentation = [[int(x[0]), int(x[1])] for x in segmentation] 51 | 52 | keypoints[:, :] = instance[:, :] 53 | #print(instance, num_keypoints) 54 | 55 | except: 56 | #print("failed finding any keypoint") 57 | #print(instance, num_keypoints) 58 | #asasas#print(instance, num_keypoints) 59 | bbox = [0, 0, 0, 0] 60 | segmentation = [] 61 | 62 | keypoints = np.reshape(keypoints, (42,)) 63 | keypoints = keypoints.tolist() 64 | keypoints = [int(x) for x in keypoints] 65 | 66 | seg = [] 67 | for s in segmentation: 68 | seg.append(s[0]) 69 | seg.append(s[1]) 70 | 71 | 72 | return bbox, seg, keypoints, num_keypoints 73 | 74 | 75 | 76 | if __name__ == "__main__": 77 | 78 | parser = argparse.ArgumentParser() 79 | parser.add_argument("--path_dir", required=True) 80 | parser.add_argument("--label_dir", required=True) 81 | parser.add_argument("--image_dir", required=True) 82 | parser.add_argument("--output_dir", required=True) 83 | parser.add_argument("--output_filename", required=True) 84 | args = parser.parse_args() 85 | 86 | 87 | label_dir = args.label_dir 88 | path_dir = args.path_dir 89 | 90 | #if not os.path.exists(label_dir): 91 | # print("Can not locate label folder: " + label_dir) 92 | # quit() 93 | 94 | 95 | data = {} 96 | 97 | data["info"] = { 98 | 'url': "https://www.andrew.cmu.edu/user/dnarapur/", 99 | 'year': 2018, 100 | 'date_created': time.strftime("%a, %d %b %Y %H:%M:%S +0000", 101 | time.localtime()), 102 | 'description': "This is a keypoint dataset for object detection.", 103 | 'version': '1.0', 104 | 'contributor': 'CMU'} 105 | 106 | data["categories"] = [{'name': 'car', 107 | 'id': 1, 108 | 'skeleton': [[0,2], 109 | [1,3], 110 | [0,1], 111 | [2,3], 112 | [9,11], 113 | [10,12], 114 | [9,10], 115 | [11,12], 116 | [4,0], 117 | [4,9], 118 | [4,5], 119 | [5,1], 120 | [5,10], 121 | [6,2], 122 | [6,11], 123 | [7,3], 124 | [7,12], 125 | [6,7]],# TODO 126 | 'supercategory': 'car', 127 | 'keypoints': [str(x) for x in range(14)]}] 128 | 129 | data["licenses"] = [{'id': 1, 130 | 'name': "unknown", 131 | 'url': "unknown"}] 132 | 133 | 134 | obj_id = 0 135 | # expect sub-folder for subsets 136 | data["images"] = [] 137 | data["annotations"] = [] 138 | json_name = args.output_filename 139 | loop=0 140 | count_images=0 141 | for sub_dir in os.listdir(path_dir): 142 | loop= loop+1 143 | im_dir = os.path.join( 144 | sub_dir, 145 | args.image_dir) 146 | 147 | sub_dir = path_dir + sub_dir + '/gt/'#os.path.join(path_dir, sub_dir) 148 | print(sub_dir) 149 | #sub_dir = os.path.join(sub_dir, '/gt/') 150 | if not os.path.isdir(sub_dir): 151 | continue 152 | 153 | 154 | # loop through all annotation file inside sub_dir 155 | # loop through all annotation file inside sub_dir 156 | #print(sub_dir) 157 | #asas 158 | for file_name in os.listdir(sub_dir): 159 | count_images =count_images+1 160 | file_str = file_name.split('.')[0] 161 | vid_str, id_str = file_str.split('_') 162 | frame_id = int(id_str) 163 | video_id = int(vid_str) 164 | image_id = int(loop*1e8+video_id*1e5+frame_id) 165 | 166 | image_name = os.path.join( 167 | im_dir, 168 | "{}.jpg".format(file_str)) 169 | 170 | width, height = 1920, 1080 171 | 172 | data["images"].append({'flickr_url': "unknown", 173 | 'coco_url': "unknown", 174 | 'file_name': image_name, 175 | 'id': image_id, 176 | 'license':1, 177 | #'has_visible_keypoints':True, 178 | 'date_captured': "unknown", 179 | 'width': width, 180 | 'height': height}) 181 | 182 | with open(os.path.join(sub_dir, file_name.split('.')[0]+'.txt')) as f: 183 | keypoints = f.readlines() 184 | keypoints = [s.split(',') for s in keypoints] 185 | keypoints = [list(map(num, s)) for s in keypoints] 186 | 187 | instances = {} 188 | 189 | for keypoint in keypoints: 190 | if keypoint[3] not in instances: 191 | instances[keypoint[3]] = np.zeros((14, 3), dtype=np.int32) 192 | instances[keypoint[3]][keypoint[2]-1,0] = keypoint[0] 193 | instances[keypoint[3]][keypoint[2]-1,1] = keypoint[1] 194 | if keypoint[4] == 2: 195 | instances[keypoint[3]][keypoint[2]-1,2] = 1 196 | elif keypoint[4] == 1: 197 | instances[keypoint[3]][keypoint[2]-1,2] = 2#keypoint[4] 198 | elif keypoint[4] == 3: 199 | instances[keypoint[3]][keypoint[2]-1,2] = 2#keypoint[4] 200 | 201 | if keypoint[0] <= 0 or keypoint[1] > height or keypoint[1] <= 0 or keypoint[0] > width: 202 | #print(keypoint[1]) 203 | 204 | instances[keypoint[3]][keypoint[2]-1,2] = 0#keypoint[4]\ 205 | #print(instances[keypoint[3]]) 206 | 207 | #print(instances) 208 | #asas 209 | 210 | for instance in instances.values(): 211 | 212 | bbox, segmentation, keypoints, num_keypoints = getAnnotation(instance) 213 | #print(keypoints) 214 | 215 | if num_keypoints == 0: 216 | continue 217 | 218 | data["annotations"].append({ 219 | 'image_id': image_id, 220 | 'category_id': 1, 221 | 'iscrowd': 0, 222 | #'has_visible_keypoints': True, 223 | 'id': obj_id, 224 | 'area': bbox[2]*bbox[3], 225 | 'bbox': bbox, 226 | 'num_keypoints': num_keypoints, 227 | 'keypoints': keypoints, 228 | 'segmentation': [segmentation]}) 229 | 230 | obj_id += 1 231 | #json_name = 'carfusion.json' 232 | json_str = json.dumps(data) 233 | 234 | print(json_name,count_images) 235 | ann_file = os.path.join(args.output_dir, json_name) 236 | if not os.path.exists(args.output_dir): 237 | os.mkdir(args.output_dir) 238 | with open(ann_file, 'w') as f: 239 | f.write(json_str) 240 | 241 | 242 | 243 | 244 | -------------------------------------------------------------------------------- /Visualize.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import glob 3 | import cv2 4 | import sys,os 5 | 6 | correspondence = [0,1,2,3,4,5,6,7,14,10,11,12,13] 7 | gt_indices = [1,2,3,4,5,6,7,8,9,10,11,12,13] 8 | correspondence = [1,0,3,2,5,4,7,6,14,11,10,13,12] 9 | 10 | 11 | def drawCar(img,keypoints,bb=None): 12 | # if keypoints[0,2] >50 and keypoints[2,2] >50: 13 | # cv2.line(img,tuple(keypoints[0,0:2]),tuple(keypoints[2,0:2]),(255,0,0),5) 14 | # if keypoints[1,2] >50 and keypoints[3,2] >50: 15 | # cv2.line(img,tuple(keypoints[1,0:2]),tuple(keypoints[3,0:2]),(0,0,255),5) 16 | # if keypoints[0,2] >50 and keypoints[1,2] >50: 17 | # cv2.line(img,tuple(keypoints[0,0:2]),tuple(keypoints[1,0:2]),(0,255,0),5) 18 | # if keypoints[3,2] >50 and keypoints[2,2] >50: 19 | # cv2.line(img,tuple(keypoints[3,0:2]),tuple(keypoints[2,0:2]),(128,128,0),5) 20 | threshold = 20 21 | # wheels 22 | if keypoints[1,2] >threshold: 23 | cv2.circle(img,tuple(keypoints[1,0:2]),3,(0,255,0),3) 24 | if keypoints[2,2] >threshold: 25 | cv2.circle(img,tuple(keypoints[2,0:2]),3,(64,255,255),3) 26 | if keypoints[3,2] >threshold: 27 | cv2.circle(img,tuple(keypoints[3,0:2]),3,(128,255,128),3) 28 | if keypoints[0,2] >threshold: 29 | cv2.circle(img,tuple(keypoints[0,0:2]),3,(128,255,0),3) 30 | 31 | if keypoints[0,2] >threshold and keypoints[2,2] >threshold: 32 | cv2.line(img,tuple(keypoints[0,0:2]),tuple(keypoints[2,0:2]),(255,0,0),2) 33 | if keypoints[1,2] >threshold and keypoints[3,2] >threshold: 34 | cv2.line(img,tuple(keypoints[1,0:2]),tuple(keypoints[3,0:2]),(255,0,0),2) 35 | if keypoints[0,2] >threshold and keypoints[1,2] >threshold: 36 | cv2.line(img,tuple(keypoints[0,0:2]),tuple(keypoints[1,0:2]),(255,0,0),2) 37 | if keypoints[2,2] >threshold and keypoints[3,2] >threshold: 38 | cv2.line(img,tuple(keypoints[2,0:2]),tuple(keypoints[3,0:2]),(255,0,0),2) 39 | 40 | 41 | 42 | # top of car 43 | if keypoints[10,2] >threshold: 44 | cv2.circle(img,tuple(keypoints[10,0:2]),3,(255,128,128),3) 45 | if keypoints[11,2] >threshold: 46 | cv2.circle(img,tuple(keypoints[11,0:2]),3,(128,128,128),3) 47 | if keypoints[12,2] >threshold: 48 | cv2.circle(img,tuple(keypoints[12,0:2]),3,(0,128,255),3) 49 | if keypoints[13,2] >threshold: 50 | cv2.circle(img,tuple(keypoints[13,0:2]),3,(0,255,255),3) 51 | 52 | if keypoints[10,2] >threshold and keypoints[12,2] >threshold: 53 | cv2.line(img,tuple(keypoints[10,0:2]),tuple(keypoints[12,0:2]),(0,255,0),2) 54 | if keypoints[11,2] >threshold and keypoints[13,2] >threshold: 55 | cv2.line(img,tuple(keypoints[11,0:2]),tuple(keypoints[13,0:2]),(0,255,0),2) 56 | if keypoints[10,2] >threshold and keypoints[11,2] >threshold: 57 | cv2.line(img,tuple(keypoints[10,0:2]),tuple(keypoints[11,0:2]),(0,255,0),2) 58 | if keypoints[12,2] >threshold and keypoints[13,2] >threshold: 59 | cv2.line(img,tuple(keypoints[12,0:2]),tuple(keypoints[13,0:2]),(0,255,0),2) 60 | 61 | # front head lights 62 | if keypoints[4,2] >threshold: 63 | cv2.circle(img,tuple(keypoints[4,0:2]),3,(0,255,0),3) 64 | if keypoints[0,2] >threshold and keypoints[4,2] >threshold: 65 | cv2.line(img,tuple(keypoints[0,0:2]),tuple(keypoints[4,0:2]),(0,0,255),2) 66 | if keypoints[10,2] >threshold and keypoints[4,2] >threshold: 67 | cv2.line(img,tuple(keypoints[10,0:2]),tuple(keypoints[4,0:2]),(0,0,255),2) 68 | 69 | if keypoints[5,2] >threshold: 70 | cv2.circle(img,tuple(keypoints[5,0:2]),3,(128,0,0),3) 71 | if keypoints[1,2] >threshold and keypoints[5,2] >threshold: 72 | cv2.line(img,tuple(keypoints[1,0:2]),tuple(keypoints[5,0:2]),(0,0,255),2) 73 | if keypoints[11,2] >threshold and keypoints[5,2] >threshold: 74 | cv2.line(img,tuple(keypoints[11,0:2]),tuple(keypoints[5,0:2]),(0,0,255),2) 75 | if keypoints[4,2] >threshold and keypoints[5,2] >threshold: 76 | cv2.line(img,tuple(keypoints[4,0:2]),tuple(keypoints[5,0:2]),(0,0,255),2) 77 | 78 | # back head lights 79 | if keypoints[6,2] >threshold: 80 | cv2.circle(img,tuple(keypoints[6,0:2]),3,(255,0,0),3) 81 | if keypoints[2,2] >threshold and keypoints[6,2] >threshold: 82 | cv2.line(img,tuple(keypoints[2,0:2]),tuple(keypoints[6,0:2]),(255,0,255),2) 83 | if keypoints[12,2] >threshold and keypoints[6,2] >threshold: 84 | cv2.line(img,tuple(keypoints[12,0:2]),tuple(keypoints[6,0:2]),(255,0,255),2) 85 | 86 | 87 | if keypoints[7,2] >threshold: 88 | cv2.circle(img,tuple(keypoints[7,0:2]),3,(255,0,128),5) 89 | if keypoints[3,2] >threshold and keypoints[7,2] >threshold: 90 | cv2.line(img,tuple(keypoints[3,0:2]),tuple(keypoints[7,0:2]),(255,0,255),2) 91 | if keypoints[13,2] >threshold and keypoints[7,2] >threshold: 92 | cv2.line(img,tuple(keypoints[13,0:2]),tuple(keypoints[7,0:2]),(255,0,255),2) 93 | if keypoints[6,2] >threshold and keypoints[7,2] >threshold: 94 | cv2.line(img,tuple(keypoints[6,0:2]),tuple(keypoints[7,0:2]),(255,0,255),2) 95 | 96 | # mirrror 97 | if keypoints[8,2] >threshold: 98 | cv2.circle(img,tuple(keypoints[8,0:2]),3,(128,0,128),5) 99 | if keypoints[8,2] >threshold and keypoints[4,2] >threshold: 100 | cv2.line(img,tuple(keypoints[8,0:2]),tuple(keypoints[4,0:2]),(0,0,255),2) 101 | 102 | if keypoints[9,2] >threshold: 103 | cv2.circle(img,tuple(keypoints[9,0:2]),3,(0,128,128),5) 104 | if keypoints[9,2] >threshold and keypoints[5,2] >threshold: 105 | cv2.line(img,tuple(keypoints[9,0:2]),tuple(keypoints[5,0:2]),(0,0,255),2) 106 | 107 | #cv2.line(img,tuple(keypoints[0,0:2]),tuple(keypoints[2,0:2]),(255,0,0),1) 108 | #cv2.line(img,tuple(keypoints[4,0:2]),tuple(keypoints[6,0:2]),(0,255,0),1) 109 | #cv2.line(img,tuple(keypoints[5,0:2]),tuple(keypoints[7,0:2]),(0,255,0),1) 110 | #cv2.line(img,tuple(keypoints[4,0:2]),tuple(keypoints[5,0:2]),(0,255,0),1) 111 | #cv2.line(img,tuple(keypoints[6,0:2]),tuple(keypoints[7,0:2]),(0,255,0),1) 112 | 113 | #cv2.line(img,tuple(keypoints[1]),tuple(keypoints[2]),(0,255,0),5) 114 | #cv2.line(img,tuple(keypoints[2]),tuple(keypoints[3]),(0,255,0),5) 115 | #cv2.line(img,tuple(keypoints[0]),tuple(keypoints[3]),(0,255,0),5) 116 | #print(keypoints[0]- [20,20]) 117 | 118 | def drawPerson(img,keypoints,bb=None): 119 | threshold = 10 120 | # wheels 121 | if keypoints[0,2] >threshold: 122 | cv2.circle(img,tuple(keypoints[0,0:2]),3,(128,255,0),2) 123 | if keypoints[1,2] >threshold: 124 | cv2.circle(img,tuple(keypoints[1,0:2]),3,(0,255,0),2) 125 | if keypoints[2,2] >threshold: 126 | cv2.circle(img,tuple(keypoints[2,0:2]),3,(64,255,255),2) 127 | if keypoints[3,2] >threshold: 128 | cv2.circle(img,tuple(keypoints[3,0:2]),3,(128,255,128),2) 129 | if keypoints[4,2] >threshold: 130 | cv2.circle(img,tuple(keypoints[4,0:2]),3,(0,255,0),2) 131 | if keypoints[5,2] >threshold: 132 | cv2.circle(img,tuple(keypoints[5,0:2]),3,(128,0,0),2) 133 | if keypoints[6,2] >threshold: 134 | cv2.circle(img,tuple(keypoints[6,0:2]),3,(255,0,0),2) 135 | if keypoints[7,2] >threshold: 136 | cv2.circle(img,tuple(keypoints[7,0:2]),3,(255,0,128),2) 137 | if keypoints[8,2] >threshold: 138 | cv2.circle(img,tuple(keypoints[8,0:2]),3,(128,0,128),2) 139 | if keypoints[9,2] >threshold: 140 | cv2.circle(img,tuple(keypoints[9,0:2]),3,(0,128,128),2) 141 | if keypoints[10,2] >threshold: 142 | cv2.circle(img,tuple(keypoints[10,0:2]),3,(255,128,128),2) 143 | if keypoints[11,2] >threshold: 144 | cv2.circle(img,tuple(keypoints[11,0:2]),3,(128,128,128),2) 145 | if keypoints[12,2] >threshold: 146 | cv2.circle(img,tuple(keypoints[12,0:2]),3,(0,128,255),2) 147 | if keypoints[13,2] >threshold: 148 | cv2.circle(img,tuple(keypoints[13,0:2]),3,(0,255,255),2) 149 | if keypoints[14,2] >threshold: 150 | cv2.circle(img,tuple(keypoints[14,0:2]),3,(0,255,64),2) 151 | if keypoints[15,2] >threshold: 152 | cv2.circle(img,tuple(keypoints[15,0:2]),3,(0,64,255),2) 153 | 154 | if keypoints[0,2] >threshold and keypoints[1,2] >threshold: 155 | cv2.line(img,tuple(keypoints[0,0:2]),tuple(keypoints[1,0:2]),(0,255,0),2) 156 | if keypoints[1,2] >threshold and keypoints[2,2] >threshold: 157 | cv2.line(img,tuple(keypoints[1,0:2]),tuple(keypoints[2,0:2]),(0,255,0),2) 158 | if keypoints[2,2] >threshold and keypoints[6,2] >threshold: 159 | cv2.line(img,tuple(keypoints[2,0:2]),tuple(keypoints[6,0:2]),(0,255,0),2) 160 | if keypoints[3,2] >threshold and keypoints[4,2] >threshold: 161 | cv2.line(img,tuple(keypoints[3,0:2]),tuple(keypoints[4,0:2]),(0,255,0),2) 162 | if keypoints[3,2] >threshold and keypoints[6,2] >threshold: 163 | cv2.line(img,tuple(keypoints[3,0:2]),tuple(keypoints[6,0:2]),(0,255,0),2) 164 | if keypoints[4,2] >threshold and keypoints[5,2] >threshold: 165 | cv2.line(img,tuple(keypoints[4,0:2]),tuple(keypoints[5,0:2]),(0,255,0),2) 166 | if keypoints[6,2] >threshold and keypoints[8,2] >threshold: 167 | cv2.line(img,tuple(keypoints[6,0:2]),tuple(keypoints[8,0:2]),(0,255,0),2) 168 | if keypoints[8,2] >threshold and keypoints[9,2] >threshold: 169 | cv2.line(img,tuple(keypoints[8,0:2]),tuple(keypoints[9,0:2]),(0,255,0),2) 170 | if keypoints[13,2] >threshold and keypoints[8,2] >threshold: 171 | cv2.line(img,tuple(keypoints[13,0:2]),tuple(keypoints[8,0:2]),(0,255,0),2) 172 | if keypoints[10,2] >threshold and keypoints[11,2] >threshold: 173 | cv2.line(img,tuple(keypoints[10,0:2]),tuple(keypoints[11,0:2]),(0,255,0),2) 174 | if keypoints[11,2] >threshold and keypoints[12,2] >threshold: 175 | cv2.line(img,tuple(keypoints[11,0:2]),tuple(keypoints[12,0:2]),(0,255,0),2) 176 | if keypoints[12,2] >threshold and keypoints[8,2] >threshold: 177 | cv2.line(img,tuple(keypoints[12,0:2]),tuple(keypoints[8,0:2]),(0,255,0),2) 178 | if keypoints[13,2] >threshold and keypoints[14,2] >threshold: 179 | cv2.line(img,tuple(keypoints[13,0:2]),tuple(keypoints[14,0:2]),(0,255,0),2) 180 | if keypoints[14,2] >threshold and keypoints[15,2] >threshold: 181 | cv2.line(img,tuple(keypoints[14,0:2]),tuple(keypoints[15,0:2]),(0,255,0),2) 182 | 183 | 184 | 185 | class BoundingBox(object): 186 | """ 187 | A 2D bounding box 188 | """ 189 | def __init__(self, points): 190 | if len(points) == 0: 191 | raise ValueError("Can't compute bounding box of empty list") 192 | self.minx, self.miny = float("inf"), float("inf") 193 | self.maxx, self.maxy = float("-inf"), float("-inf") 194 | for x, y in points: 195 | # Set min coords 196 | if x < self.minx: 197 | self.minx = x 198 | if y < self.miny: 199 | self.miny = y 200 | # Set max coords 201 | if x > self.maxx: 202 | self.maxx = x 203 | elif y > self.maxy: 204 | self.maxy = y 205 | @property 206 | def width(self): 207 | return self.maxx - self.minx 208 | @property 209 | def height(self): 210 | return self.maxy - self.miny 211 | def __repr__(self): 212 | return "BoundingBox({}, {}, {}, {})".format( 213 | self.minx, self.maxx, self.miny, self.maxy) 214 | # Usage example: 215 | 216 | def _compute_iou(boxA, boxB): 217 | # determine the (x, y)-coordinates of the intersection rectangle 218 | xA = max(boxA[0], boxB[0]) 219 | yA = max(boxA[1], boxB[1]) 220 | xB = min(boxA[0] + boxA[2], boxB[0] + boxB[2]) 221 | yB = min(boxA[1] + boxA[3], boxB[1] + boxB[3]) 222 | 223 | if xA < xB and yA < yB: 224 | # compute the area of intersection rectangle 225 | interArea = (xB - xA) * (yB - yA) 226 | # compute the area of both the prediction and ground-truth 227 | # rectangles 228 | boxAArea = boxA[2] * boxA[3] 229 | boxBArea = boxB[2] * boxB[3] 230 | # compute the intersection over union by taking the intersection 231 | # area and dividing it by the sum of prediction + ground-truth 232 | # areas - the intersection area 233 | iou = interArea / float(boxAArea + boxBArea - interArea) 234 | else: 235 | iou = 0 236 | 237 | assert iou >= 0 238 | assert iou <= 1.01 239 | 240 | return iou 241 | 242 | def get_keypoints_gt(b): 243 | with open(b, 'r') as content_file: 244 | content = content_file.read() 245 | keypoints = content.split('\n') 246 | kp_float = [] 247 | indices = [] 248 | for loop,kp in enumerate(keypoints): 249 | if len(kp)>0: 250 | kp_float.append([float(kp.split(',')[0]),float(kp.split(',')[1]),int(kp.split(',')[2])]) 251 | indices.append(int(kp.split(',')[3])) 252 | kp_all = [] 253 | for loop_kp in range(max(indices)+1): 254 | kp_final = np.zeros((16,3)) 255 | for loop,kp in enumerate(kp_float): 256 | if indices[loop] == loop_kp: 257 | t = correspondence[gt_indices.index(kp[2])]#kp_final[t,0:2] = kp[0:2] 258 | kp_final[t,0:2] = kp[0:2] 259 | kp_final[t,2] = 100 260 | kp_all.append(np.round(kp_final.astype(np.float)).astype(np.int)) 261 | return kp_all 262 | 263 | def get_keypoints(name): 264 | kp_all_new = [] 265 | with open(name) as f: 266 | lines = f.readlines() 267 | bb = [] 268 | points = [] 269 | class_name = [] 270 | for line in lines: 271 | bb.append(np.array(line.split(',')[1:5]).astype(np.float)) 272 | points.append(np.array(line.split(',')[5:-1]).astype(np.float)) 273 | class_name.append(line.split(',')[-1].split('\n')[0]) 274 | for bb_loop,bb_num in enumerate(bb): 275 | points_array = np.array(points[bb_loop])#.splitlines()[0].split(',')) 276 | points_arranged = points_array.reshape(int(len(points_array)/3),3) 277 | kp = points_arranged[:,0:3] 278 | kp = np.round(kp.astype(np.float)).astype(np.int) 279 | kp[:,0] = bb[bb_loop][0] + kp[:,0]*(bb[bb_loop][2]/64) 280 | kp[:,1] = bb[bb_loop][1] + kp[:,1]*(bb[bb_loop][3]/64) 281 | kp[:,2] = np.round(points_arranged[:,2].astype(np.float)*100).astype(np.int) 282 | kp_all_new.append(kp) 283 | return kp_all_new 284 | 285 | def count_pck(bb_gt,bb_computed,alpha,B): 286 | #print(bb_gt,bb_computed) 287 | count = 0 288 | count_inliers = 0 289 | for loop,point in enumerate(bb_computed): 290 | if point[2]>10 and loop != 8 and loop != 9: 291 | dist = np.linalg.norm(point[0:2]-bb_gt[loop,0:2]) 292 | #dist = np.linalg.norm(point[0]-bb_gt[loop,0]) 293 | #print(point[0:2],bb_gt[loop,0:2],dist) 294 | #print(dist,alpha*B) 295 | count = count+1 296 | if dist 1: 303 | Folder = sys.argv[1] 304 | image_name = sys.argv[2] 305 | GroundTruth_labels = Folder+'/gt/'+ sys.argv[2] + '.txt' 306 | else: 307 | Folder = '.' 308 | GroundTruth_labels = Folder+'/gt/*.txt' 309 | 310 | save = 1 311 | display = 1 312 | allfiles = glob.glob(GroundTruth_labels) 313 | 314 | if save==1: 315 | try: 316 | os.mkdir(Folder+'/images_labeled') 317 | except: 318 | print('Folder exists') 319 | for a,b in enumerate(allfiles): 320 | #b = allfiles[17] 321 | filename = b.split('/')[-1] 322 | kp_all_GT = get_keypoints_gt(b) 323 | 324 | 325 | image_name = Folder + '/images_jpg/'+filename.split('/')[-1].split('.')[0] + '.jpg' 326 | img = cv2.imread(image_name) 327 | for a,kp_label in enumerate(kp_all_GT): 328 | drawCar(img,kp_label,[0,0,1,1]) 329 | if save==1: 330 | cv2.imwrite(image_name.replace('images_jpg','images_labeled'),img) 331 | 332 | if display==1: 333 | cv2.namedWindow('image', cv2.WINDOW_NORMAL) 334 | cv2.imshow('image',img) 335 | cv2.waitKey(0) 336 | cv2.destroyAllWindows() 337 | 338 | if __name__ == "__main__": 339 | main() 340 | #evaluate_pck() 341 | --------------------------------------------------------------------------------