Guest User

Untitled

a guest
Nov 14th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. import cv2
  2. from collections import deque
  3. from scipy.spatial import distance as dist
  4. from imutils import perspective
  5. import numpy as np
  6. import argparse
  7. import imutils
  8. import math
  9. import time
  10.  
  11. font = cv2.FONT_HERSHEY_SIMPLEX
  12.  
  13. def midpoint(ptA, ptB):
  14. return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)
  15.  
  16. ap = argparse.ArgumentParser()
  17. ap.add_argument("-v", "--video", help="path to the (optional) video file")
  18. args = vars(ap.parse_args())
  19.  
  20. sensitivity = 43
  21. greyLower = np.array([96,0,176-sensitivity])
  22. greyUpper = np.array([180,sensitivity,255])
  23. pixelsPerMetric = None
  24. KNOWN_WIDTH = 19
  25.  
  26. if not args.get("video", False):
  27. camera = cv2.VideoCapture(0)
  28. else:
  29. camera = cv2.VideoCapture(args["video"])
  30.  
  31. while True:
  32. (grabbed, frame) = camera.read()
  33.  
  34. if args.get("video") and not grabbed:
  35. break
  36.  
  37. frame = imutils.resize(frame, width=600)
  38. hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
  39.  
  40. mask = cv2.inRange(hsv,greyLower, greyUpper)
  41. mask = cv2.erode(mask, None, iterations=2)
  42. mask = cv2.dilate(mask, None, iterations=2)
  43.  
  44. (_, contours, hierarchy) = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  45. center = None
  46. for pic, contour in enumerate(contours):
  47.  
  48. if len(contours) > 0:
  49. contour = max(contours, key =cv2.contourArea)
  50. x,y,w,h = cv2.boundingRect(contour)
  51. rect = (x+w, y+h)
  52. rect = cv2.minAreaRect(contour)
  53. box = cv2.boxPoints(rect)
  54. box = np.int0(box)
  55. box = perspective.order_points(box)
  56. M = cv2.moments(contour)
  57. for (x, y) in box:
  58. M = cv2.rectangle(frame,(int(x),int(y)),(int(x+w), int(y+h)),(0,0,139),2)
  59. # unpack the ordered bounding box, then compute the midpoint
  60. # between the top-left and top-right coordinates, followed by
  61. # the midpoint between bottom-left and bottom-right coordinates
  62. (tl, tr, br, bl) = box
  63. (tltrX, tltrY) = midpoint(tl, tr)
  64. (blbrX, blbrY) = midpoint(bl, br)
  65.  
  66. # compute the midpoint between the top-left and top-right points,
  67. # followed by the midpoint between the top-righ and bottom-right
  68. (tlblX, tlblY) = midpoint(tl, bl)
  69. (trbrX, trbrY) = midpoint(tr, br)
  70.  
  71. # draw the midpoints on the image
  72. cv2.circle(M, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1)
  73. cv2.circle(M, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1)
  74. cv2.circle(M, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1)
  75. cv2.circle(M, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)
  76.  
  77. # draw lines between the midpoints
  78. cv2.line(M, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)),
  79. (255, 0, 255), 2)
  80. cv2.line(M, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)),
  81. (255, 0, 255), 2)
  82. # compute the Euclidean distance between the midpoints
  83. dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))
  84. dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))
  85.  
  86. # if the pixels per metric has not been initialized, then
  87. # compute it as the ratio of pixels to supplied metric
  88. # (in this case, inches)
  89.  
  90. # compute the size of the object
  91. dimA = dA / KNOWN_WIDTH
  92. dimB = dB / KNOWN_WIDTH
  93.  
  94. if rect >300:
  95. frame = cv2.rectangle(frame,(int(x),int(y)),(int(x+w), int(y+h)),(0,0,139),2)
  96. cv2.putText(frame,"MCB",(x,y),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0,0,139))
  97.  
  98. # draw the object sizes on the image
  99. cv2.putText(frame, "{:.1f}cm".format(dimA),
  100. (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX,
  101. 0.65, (255, 255, 255), 2)
  102. cv2.putText(frame, "{:.1f}cm".format(dimB),
  103. (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,
  104. 0.65, (255, 255, 255), 2)
  105. cv2.imshow("Frame", frame)
  106.  
  107. key = cv2.waitKey(10) & 0xFF
  108.  
  109. if key == ord("q"):
  110. break
  111.  
  112. camera.release()
  113. cv2.destroyAllWindows()
Add Comment
Please, Sign In to add comment