1. # -*- coding: utf-8 -*-
    2. from __future__ import division
    3. import numpy as np
    4. import sys,os
    5. import cv2
    6. caffe_root = '/home/jiasj/workspaces/Caffe/Calmcar_file/caffe_deephi/'
    7. sys.path.insert(0, caffe_root + 'python')
    8. import caffe
    9. import time
    10. net_file= 'train_val_reach15.prototxt'
    11. caffe_model='SSD_480x360_GENIII_15_iter_701100.caffemodel'
    12. test_dir = "/home/jiasj/Desktop/ces_demo1"
    13. image_outdir = "/home/jiasj/Desktop/demo/"
    14. if not os.path.exists(image_outdir):
    15. os.makedirs(image_outdir)
    16. if not os.path.exists(caffe_model):
    17. print("MobileNetSSD_deploy.caffemodel does not exist,")
    18. print("use merge_bn.py to generate it.")
    19. exit()
    20. net = caffe.Net(net_file,caffe_model,caffe.TEST)
    21. caffe.set_mode_gpu()
    22. caffe.set_device(0)
    23. CLASSES = ('background',
    24. 'car_whole', 'car_rear', 'car_front', 'bus_whole',
    25. 'bus_rear', 'bus_front', 'truck_whole', 'truck_rear', 'truck_front','person','bicycle','motorbike','tricycle',"speed_limit_sign","traffic_cone")
    26. def preprocess(src):
    27. img = cv2.resize(src, (480,360))
    28. img = img - 127.5
    29. #img = img * 0.007843img - 127.5
    30. return img
    31. def postprocess(img, out):
    32. h = img.shape[0]
    33. w = img.shape[1]
    34. box = out['detection_out'][0,0,:,3:7] * np.array([w, h, w, h])
    35. cls = out['detection_out'][0,0,:,1]
    36. conf = out['detection_out'][0,0,:,2]
    37. return (box.astype(np.int32), conf, cls)
    38. def compute_iou(rec1, rec2):
    39. left_column_max = max(rec1[0],rec2[0])
    40. right_column_min = min(rec1[2],rec2[2])
    41. up_row_max = max(rec1[1],rec2[1])
    42. down_row_min = min(rec1[3],rec2[3])
    43. S1 = (rec1[2]-rec1[0])*(rec1[3]-rec1[1])
    44. S2 = (rec2[2]-rec2[0])*(rec2[3]-rec2[1])
    45. if left_column_max>=right_column_min or up_row_max >=down_row_min:
    46. return 0
    47. else:
    48. S_cross = (down_row_min-up_row_max)*(right_column_min-left_column_max)
    49. return S_cross/(S1+S2-S_cross)
    50. def class_loop(origimg, file_name, box, conf, cls, class_f, class_s1, class_s2):
    51. for i in range(len(box)):
    52. if ((conf[i] > 0.3)&((str(CLASSES[int(cls[i])]) == class_f))):
    53. car=(box[i][0], box[i][1],box[i][2], box[i][3])
    54. for j in range(len(box)):
    55. if ((conf[j] > 0.3)&((str(CLASSES[int(cls[j])]) == class_s1) | (str(CLASSES[int(cls[j])]) == class_s2))):
    56. car_front = (box[j][0], box[j][1],box[j][2], box[j][3])
    57. iou=compute_iou(car, car_front)
    58. if (iou>0.1)&(iou <1):
    59. xmin_ = abs(car[0] - car_front[0])
    60. xmax_ = abs(car[2] - car_front[2])
    61. if xmin_ < xmax_:
    62. car_outside, car_inside = draw_3d_road_left(car, car_front)
    63. cv2.rectangle(origimg, (car_outside[0], car_outside[1]), (car_outside[2], car_outside[3]), (255, 128, 128), 2)
    64. cv2.line(origimg, (car_outside[0], car_outside[1]), (car_inside[0], car_inside[1]), (255, 128, 128), 2)
    65. cv2.line(origimg, (car_outside[2], car_outside[1]), (car_inside[2], car_inside[1]), (255, 128, 128), 2)
    66. cv2.line(origimg, (car_outside[2], car_outside[3]), (car_inside[2], car_inside[3]), (255, 128, 128), 2)
    67. cv2.line(origimg, (car_inside[0], car_inside[1]), (car_inside[2], car_inside[1]), (255, 128, 128), 2)
    68. cv2.line(origimg, (car_inside[2], car_inside[1]), (car_inside[2], car_inside[3]), (255, 128, 128), 2)
    69. cv2.imwrite(image_outdir + '/'+ file_name, origimg)
    70. elif xmin_ > xmax_:
    71. car_outside, car_inside = draw_3d_road_right(car, car_front)
    72. cv2.rectangle(origimg, (car_outside[0], car_outside[1]), (car_outside[2], car_outside[3]), (255, 128, 128), 2)
    73. cv2.line(origimg, (car_outside[0], car_outside[1]), (car_inside[0], car_inside[1]), (255, 128, 128), 2)
    74. cv2.line(origimg, (car_outside[2], car_outside[1]), (car_inside[2], car_inside[1]), (255, 128, 128), 2)
    75. cv2.line(origimg, (car_outside[0], car_outside[3]), (car_inside[0], car_inside[3]), (255, 128, 128), 2)
    76. cv2.line(origimg, (car_inside[0], car_inside[1]), (car_inside[2], car_inside[1]), (255, 128, 128), 2)
    77. cv2.line(origimg, (car_inside[0], car_inside[1]), (car_inside[0], car_inside[3]), (255, 128, 128), 2)
    78. cv2.imwrite(image_outdir + '/'+ file_name, origimg)
    79. conf[j] = 0
    80. conf[i] = 0
    81. break
    82. def detect(imgfile):
    83. file_path,file_name = os.path.split(imgfile)
    84. print file_name
    85. start = time.time()
    86. origimg = cv2.imread(imgfile)
    87. img = preprocess(origimg)
    88. img = img.astype(np.float32)
    89. img = img.transpose((2, 0, 1))
    90. net.blobs['data'].data[...] = img
    91. out = net.forward()
    92. box, conf, cls = postprocess(origimg, out)
    93. class_loop(origimg, file_name, box, conf, cls, 'car_whole', 'car_rear', 'car_front')
    94. class_loop(origimg, file_name, box, conf, cls, 'bus_whole', 'bus_rear', 'bus_front')
    95. class_loop(origimg, file_name, box, conf, cls, 'truck_whole', 'truck_rear', 'truck_front')
    96. for i in range(len(box)):
    97. if (conf[i] > 0.3):
    98. p3 = (max(box[i][0], 15), max(box[i][1], 15))
    99. cv2.rectangle(origimg, (box[i][0], box[i][1]), (box[i][2], box[i][3]), (255, 128, 128),2)
    100. cv2.imwrite(image_outdir + '/'+ file_name, origimg)
    101. cv2.imshow("SSD", origimg)
    102. k = cv2.waitKey(1) & 0xff
    103. return True
    104. def draw_3d_road_left(car, car_rear):
    105. w = car[2] - car[0]
    106. h = car[3] - car[1]
    107. top_back_y = car[1]
    108. wheel_front = (car[2], int(car[3] - (car[3] - car[1]) * 0.15))
    109. wheel_back = (car_rear[2], car_rear[3])
    110. light_middle = car_rear[2]
    111. v1 = car[3]
    112. l1 = h
    113. v2 = wheel_front[1]
    114. l2 = int(v2 / v1 * l1)
    115. retf_ul_x = car[2] - (car_rear[2] - car_rear[0])
    116. retf_ul_y = wheel_front[1] - int(l2 * (1 - 0.125))
    117. retf_dr_x = wheel_front[0]
    118. retf_dr_y = int(car[1] + h - (light_middle - wheel_front[0]) *
    119. (wheel_back[1] - wheel_front[1]) / (wheel_back[0] - wheel_front[0]))
    120. return car_rear, (retf_ul_x, retf_ul_y, retf_dr_x, retf_dr_y)
    121. def draw_3d_road_right(car, car_rear):
    122. w = car[2] - car[0]
    123. h = car[3] - car[1]
    124. top_back_y = car[1]
    125. wheel_front = (car[0], int(car[3] - (car[3] - car[1]) * 0.15))
    126. wheel_back = (car_rear[0], car_rear[3])
    127. light_middle = car_rear[0]
    128. v1 = car[3]
    129. l1 = h
    130. v2 = wheel_front[1]
    131. l2 = int(v2 / v1 * l1)
    132. retf_ul_x = wheel_front[0]
    133. retf_ul_y = wheel_front[1] - int(l2 * (1 - 0.125))
    134. retf_dr_x = wheel_front[0] + car[2] - light_middle
    135. retf_dr_y = int(car[1] + h - (light_middle - wheel_front[0]) *
    136. (wheel_back[1] - wheel_front[1]) / (wheel_back[0] - wheel_front[0]))
    137. return (light_middle, top_back_y, car[2], car[3]), (retf_ul_x, retf_ul_y, retf_dr_x, retf_dr_y)
    138. for f in sorted(os.listdir(test_dir)):
    139. if detect(test_dir + "/" + f) == False:
    140. break

    3dbbox.pdf