Advertisement
Guest User

Untitled

a guest
May 21st, 2021
1,122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. path_to_frozen_inference_graph = 'frozen_inference_graph_coco.pb'
  4. path_coco_model= 'mask_rcnn_inception_v2_coco_2018_01_28.pbtxt'
  5. image_path='road.jpg'
  6. # # Generate random colors
  7. colors = np.random.randint(0, 255, (80, 3))
  8.  
  9. # #image_path='C:/Users/maind/Downloads/dog1.jpg'
  10. # # Loading Mask RCNN(# model weight file # model config file)
  11. net = cv2.dnn.readNetFromTensorflow(path_to_frozen_inference_graph,path_coco_model)
  12. #
  13. #
  14. # print(colors)
  15. #
  16. # # Load image
  17. img = cv2.imread(image_path)
  18. img=cv2.resize(img,(650,550))
  19. height, width, _ = img.shape
  20. #
  21. # # Create black image
  22. black_image = np.zeros((height, width, 3), np.uint8)
  23. # # set background color of blank image
  24. black_image[:] = (150, 150, 0)
  25. #
  26. # # preprocess & Detect objects
  27. blob = cv2.dnn.blobFromImage(img, swapRB=True)
  28. net.setInput(blob)
  29. #
  30. # # propagate through the network
  31. boxes, masks = net.forward(["detection_out_final", "detection_masks"])
  32. # # # Boxes is a 3 dimensional list of information about the detected objects
  33. # # # can access the i'th object like boxes = [0,0,i-1]
  34. # # #Returns an array like this:
  35. # # # [0.         0.         0.99492836 0.92008555 0.27316627 0.98529506 0.53607523]
  36. # # #Last 4 is the position of the bounding box
  37. #
  38. #
  39. # # no of objects detected
  40. detection_count = boxes.shape[2]
  41. #
  42. # # iterate through the no of objects
  43. for i in range(detection_count):
  44.      box = boxes[0, 0, i]
  45.      class_id = box[1]
  46.      score = box[2]
  47.      if score < 0.5:
  48.          continue
  49. #     # Get box Coordinates
  50. #     # box return x,y,w,h according to the aspect ratio thus it needs to be multiplied by
  51. #     # the width and height of the image
  52.      x = int(box[3] * width)
  53.      y = int(box[4] * height)
  54.      x2 = int(box[5] * width)
  55.      y2 = int(box[6] * height)
  56. #
  57. #     # define the roi which is actually each separate object in every loop
  58. #     # iteration
  59.      roi = black_image[y: y2, x: x2]
  60.      roi_height, roi_width, _ = roi.shape
  61. #
  62. #     # Get the mask (ith object in the list, class id)
  63.      mask = masks[i, int(class_id)]
  64. #     # the model returns a 15x15 array which we resize to the shape oof the
  65. #     # roi to create the mask
  66.      mask = cv2.resize(mask, (roi_width, roi_height))
  67. #     # use the threshold function to transform the ROI into as mask where
  68. #     #     # all the pixels with values under 0.5 in the image are zero and above
  69. #     #     # are 1
  70.      _, mask = cv2.threshold(mask, 0.5, 255, cv2.THRESH_BINARY)
  71. #
  72. #     # put bounding boxes around objects
  73.      cv2.rectangle(img, (x, y), (x2, y2), (255, 0, 0), 3)
  74. #     # Get mask coordinates
  75.      contours, _ = cv2.findContours(np.array(mask, np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  76.      color = colors[int(class_id)]
  77.      for cnt in contours:
  78.          cv2.fillPoly(roi, [cnt], (int(color[0]), int(color[1]), int(color[2])))
  79. #
  80. #     # cv2.imshow("roi", roi)
  81. #     # cv2.waitKey(0)
  82. #
  83. #
  84. # # cv2.imshow("Image", img)
  85. # # cv2.imshow("Black image", black_image)
  86. cv2.imshow("Final",np.hstack([img,black_image]))
  87. cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement