Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. #############################################
  2. # Object detection - YOLO - OpenCV
  3. # Author : Arun Ponnusamy (July 16, 2018)
  4. # Website : http://www.arunponnusamy.com
  5. ############################################
  6.  
  7.  
  8. import cv2
  9. import argparse
  10. import numpy as np
  11.  
  12. '''ap = argparse.ArgumentParser()
  13. ap.add_argument('-i', '--image', required=True,
  14. help = 'path to input image')
  15. ap.add_argument('-c', '--config', required=True,
  16. help = 'path to yolo config file')
  17. ap.add_argument('-w', '--weights', required=True,
  18. help = 'path to yolo pre-trained weights')
  19. ap.add_argument('-cl', '--classes', required=True,
  20. help = 'path to text file containing class names')
  21. args = ap.parse_args()
  22. '''
  23. ap = argparse.ArgumentParser()
  24. ap.add_argument('--image', default='6.jpg',
  25. help='path to input image')
  26. ap.add_argument('--config', default='yolov3.cfg',
  27. help='path to yolo config file')
  28. ap.add_argument('--weights', default='yolov3.weights',
  29. help='path to yolo pre-trained weights')
  30. ap.add_argument('--classes', default='yolov3.txt',
  31. help='path to text file containing class names')
  32. args = ap.parse_args()
  33.  
  34.  
  35. def get_output_layers(net):
  36. layer_names = net.getLayerNames()
  37.  
  38. output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
  39.  
  40. return output_layers
  41.  
  42.  
  43. def draw_prediction(img, class_id, confidence, x, y, x_plus_w, y_plus_h):
  44. label = str(classes[class_id])
  45.  
  46. color = COLORS[class_id]
  47.  
  48. cv2.rectangle(img, (x, y), (x_plus_w, y_plus_h), color, 2)
  49.  
  50. cv2.putText(img, label, (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
  51.  
  52.  
  53. image = cv2.imread(args.image)
  54.  
  55. Width = image.shape[1]
  56. Height = image.shape[0]
  57. scale = 0.00392
  58.  
  59. classes = None
  60.  
  61. with open(args.classes, 'r') as f:
  62. classes = [line.strip() for line in f.readlines()]
  63.  
  64. COLORS = np.random.uniform(0, 255, size=(len(classes), 3))
  65.  
  66. net = cv2.dnn.readNetFromDarknet(args.config, args.weights)
  67.  
  68. blob = cv2.dnn.blobFromImage(image, scale, (416, 416), (0, 0, 0), True, crop=False)
  69.  
  70. net.setInput(blob)
  71.  
  72. outs = net.forward(get_output_layers(net))
  73.  
  74. class_ids = []
  75. confidences = []
  76. boxes = []
  77. conf_threshold = 0.5
  78. nms_threshold = 0.4
  79.  
  80. for out in outs:
  81. for detection in out:
  82. scores = detection[5:]
  83. class_id = np.argmax(scores)
  84. confidence = scores[class_id]
  85. if confidence > 0.5:
  86. center_x = int(detection[0] * Width)
  87. center_y = int(detection[1] * Height)
  88. w = int(detection[2] * Width)
  89. h = int(detection[3] * Height)
  90. x = center_x - w / 2
  91. y = center_y - h / 2
  92. class_ids.append(class_id)
  93. confidences.append(float(confidence))
  94. boxes.append([x, y, w, h])
  95.  
  96. indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold)
  97.  
  98. for i in indices:
  99. i = i[0]
  100. box = boxes[i]
  101. x = box[0]
  102. y = box[1]
  103. w = box[2]
  104. h = box[3]
  105. draw_prediction(image, class_ids[i], confidences[i], round(x), round(y), round(x + w), round(y + h))
  106.  
  107. cv2.imshow("object detection", image)
  108. cv2.waitKey()
  109.  
  110. cv2.imwrite("object-detection.jpg", image)
  111. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement