Advertisement
kaisarkhatak

lane-detect-main-vid.py

Jan 18th, 2019
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. from keras.preprocessing import image
  4.  
  5. import visualize_car_detection
  6. import process_image as ip
  7. import model
  8.  
  9. class InferenceConfig():
  10. # Set batch size to 1 since we'll be running inference on
  11. # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
  12. GPU_COUNT = 1
  13. IMAGES_PER_GPU = 1
  14. BACKBONE_SHAPES=np.array([[256, 256], [128, 128], [ 64, 64], [ 32, 32], [ 16, 16]])
  15. BACKBONE_STRIDES=[4, 8, 16, 32, 64]
  16. BATCH_SIZE=1
  17. BBOX_STD_DEV=[ 0.1, 0.1, 0.2, 0.2]
  18. DETECTION_MAX_INSTANCES=100
  19. DETECTION_MIN_CONFIDENCE=0.6 #0.5
  20. DETECTION_NMS_THRESHOLD=0.3
  21. IMAGE_MAX_DIM=1024
  22. IMAGE_MIN_DIM=800
  23. IMAGE_PADDING=True
  24. IMAGE_SHAPE=np.array([1024, 1024, 3])
  25. LEARNING_MOMENTUM=0.9
  26. LEARNING_RATE =0.002
  27. MASK_POOL_SIZE=14
  28. MASK_SHAPE =[28, 28]
  29. MAX_GT_INSTANCES=100
  30. MEAN_PIXEL =[ 123.7, 116.8, 103.9]
  31. MINI_MASK_SHAPE =(56, 56)
  32. NAME ="coco"
  33. NUM_CLASSES =81
  34. POOL_SIZE =7
  35. POST_NMS_ROIS_INFERENCE =1000
  36. POST_NMS_ROIS_TRAINING =2000
  37. ROI_POSITIVE_RATIO=0.33
  38. RPN_ANCHOR_RATIOS =[0.5, 1, 2]
  39. RPN_ANCHOR_SCALES =(32, 64, 128, 256, 512)
  40. RPN_ANCHOR_STRIDE =2
  41. RPN_BBOX_STD_DEV =np.array([ 0.1, 0.1, 0.2 , 0.2])
  42. RPN_TRAIN_ANCHORS_PER_IMAGE=256
  43. RPN_NMS_THRESHOLD = 0.3
  44. STEPS_PER_EPOCH =1000
  45. TRAIN_ROIS_PER_IMAGE =128
  46. USE_MINI_MASK =True
  47. USE_RPN_ROIS =True
  48. VALIDATION_STPES =50
  49. WEIGHT_DECAY =0.0001
  50.  
  51. class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
  52. 'bus', 'train', 'truck', 'boat', 'traffic light',
  53. 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
  54. 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',
  55. 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
  56. 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
  57. 'kite', 'baseball bat', 'baseball glove', 'skateboard',
  58. 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
  59. 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
  60. 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
  61. 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
  62. 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
  63. 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
  64. 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
  65. 'teddy bear', 'hair drier', 'toothbrush']
  66.  
  67. def findLane(img):
  68. cropped_img = ip.area_of_interest(img, [ip.crop_points.astype(np.int32)])
  69. trans_img = ip.applyTransformation(cropped_img)
  70. masked_image = ip.applyMasks(trans_img)
  71. left_fit, right_fit, _ = ip.slidingWindow(masked_image)
  72. lane_mask = ip.applyBackTrans(img, left_fit, right_fit)
  73. img_result = cv2.addWeighted(img, 1, lane_mask, 1, 0)
  74. return img_result
  75.  
  76. def process_video(neural_net, input_img):
  77.  
  78. img = cv2.resize(input_img, (1024, 1024))
  79. img = image.img_to_array(img)
  80. results = neural_net.detect([img], verbose=0)
  81. r = results[0]
  82. final_img = visualize_car_detection.display_instances2(img, r['rois'], r['masks'], r['class_ids'],
  83. class_names, r['scores'])
  84. inp_shape = image.img_to_array(input_img).shape
  85. final_img = cv2.resize(final_img, (inp_shape[1], inp_shape[0]))
  86.  
  87. return final_img
  88.  
  89. if __name__ == "__main__":
  90. #img_arr = cv2.imread('input/test2.jpg')
  91. # Create a VideoCapture object
  92. cap = cv2.VideoCapture('/home/ubuntu/cviz/RCNN-Vehicle-Tracking-Lane-Detection/input/massachusetts.mp4')
  93. frame_width = int(cap.get(3))
  94. frame_height = int(cap.get(4))
  95. print("vid frame_width {}".format(int(frame_width)))
  96. print("vid frame_height {}".format(int(frame_height)))
  97.  
  98. fourcc = cv2.VideoWriter_fourcc(*'XVID')
  99. output_movie = cv2.VideoWriter('output/output.avi', fourcc, 30, (frame_width, frame_height))
  100.  
  101. config = InferenceConfig()
  102. NN = model.MaskRCNN(mode="inference", model_dir='logs', config=config)
  103. NN.load_weights('mask_rcnn_coco.h5', by_name=True)
  104.  
  105. # Check if camera opened successfully
  106. if (cap.isOpened() == False):
  107. print("Unable to read camera feed")
  108.  
  109. while(True):
  110.  
  111. # grab the frame from the threaded video stream
  112. (grabbed, frame) = cap.read()
  113.  
  114. # Stop the program if reached end of video
  115. if not grabbed:
  116. print("Done processing !!!")
  117. #print("Output file is stored as ", outputFile)
  118. cv2.waitKey(3000)
  119. break
  120.  
  121. #cv2.imshow("Input", frame)
  122. key = cv2.waitKey(1) & 0xFF
  123. #img_result = process_video(NN,frame)
  124. img_result_ld = findLane(frame)
  125. #img_result = cv2.addWeighted(img_result, 1, img_result_ld, 1, 0)
  126. output_movie.write(img_result_ld)
  127.  
  128. # otheriwse, if the `q` key was pressed, break from the loop
  129. if key == ord("q"):
  130. break
  131.  
  132. # do a bit of cleanup
  133. cv2.destroyAllWindows()
  134.  
  135.  
  136. #img_result = process_video(NN,img_arr)
  137. #cv2.imwrite('output/output1.png', img_result)
  138.  
  139. #img_result = findLane(img_arr)
  140. #cv2.imwrite('output/output_lane.png', img_result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement