import pandas as pd
import numpy as np
import cv2
frame=120#有10帧
#数据单位
COLUMN_NAMES=['frame', 'track_id', 'type', 'truncated', 'occluded', 'alpha', 'bbox_left', 'bbox_top','bbox_right',
'bbox_bottom', 'height', 'width', 'length', 'pos_x', 'pos_y', 'pos_z', 'rot_y']
df = pd.read_csv('/home/sk/Desktop/data/training/label_02/0000.txt',header=None,sep=' ')
df.columns = COLUMN_NAMES
df.head()#head()方法,默认读取前五行,如果想显示更多,那么在括号内赋值
df.type.isin(['Truck','Van','Tram'])
df.loc[df.type.isin(['Truck','Van','Tram']),'type']='Car'
df = df[df.type.isin(['Car','Pedestrian','Cyclist'])]
df.loc[2,['bbox_left' ,'bbox_top','bbox_right','bbox_bottom']]
box=np.array(df.loc[2,['bbox_left' ,'bbox_top','bbox_right','bbox_bottom']])
image = cv2.imread('/home/sk/Desktop/data/kitti/2011_09_26/2011_09_26_drive_0005_sync/image_00/data/%010d.png'%frame)
boxes = np.array(df[df.frame==frame][['bbox_left' ,'bbox_top','bbox_right','bbox_bottom']])
types = np.array(df[df.frame==frame]['type'])
DETECTION_COLOR_DICT= {'Car':(255,255,0),'Pedestrian':(0,226,255),'Cyclist':(141,40,255)}
for typ,box in zip(types,boxes):
top_left = int(box[0]),int(box[1])
bottom_right = int(box[2]),int(box[3])
cv2.rectangle(image,top_left,bottom_right,DETECTION_COLOR_DICT[typ],2)
cv2.imshow("image",image)
cv2.waitKey(0)
cv2.destroyAllWindows()
效果如图所示: