Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. import argparse
  2.  
  3. import numpy as np
  4. import cv2
  5. import cv2.aruco as aruco
  6. import time
  7. import math
  8.  
  9.  
  10. aruco_dict = aruco.Dictionary_get(aruco.DICT_6X6_250)
  11. parameters = aruco.DetectorParameters_create()
  12.  
  13. ARUCO_TAG_HEIGHT = 25
  14. ARUCO_TAG_WIDTH = 25
  15.  
  16. # Focal length = (Pixels x Distance) / Size
  17. WEBCAM_FOCAL_LENGTH = 1
  18. RASPICAM_FOCAL_LENGTH = 1
  19. POTATO_FOCAL_LENGTH = 1
  20.  
  21.  
  22. #
  23. #
  24. def get_distance(height, fl):
  25. return (ARUCO_TAG_HEIGHT * fl) / height
  26.  
  27.  
  28. # find_middle(tag_corners)
  29. #
  30. def find_middle(tag_corners):
  31. maxx, maxy = max(tag_corners[0][0], tag_corners[1][0], tag_corners[2][0], tag_corners[3][0]), \
  32. max(tag_corners[0][1], tag_corners[1][1], tag_corners[2][1], tag_corners[3][1])
  33.  
  34. minx, miny = min(tag_corners[0][0], tag_corners[1][0], tag_corners[2][0], tag_corners[3][0]), \
  35. min(tag_corners[0][1], tag_corners[1][1], tag_corners[2][1], tag_corners[3][1])
  36. return (maxx + minx) / 2, (maxy + miny) / 2, maxx - minx, maxy - miny
  37.  
  38.  
  39. # detect_and_cal_tags(a_image, overlay)
  40. #
  41. def detect_and_cal_tags(a_image, focal_length=WEBCAM_FOCAL_LENGTH):
  42. corners, ids, rejected_points = aruco.detectMarkers(a_image, aruco_dict, parameters=parameters)
  43. tags = []
  44. if ids is not None:
  45. for x in range(0, len(ids)):
  46. xid = ids[x][0]
  47. # print(corners[x][0])
  48. xx, xy, w, h = find_middle(corners[x][0])
  49. d = get_distance(h, focal_length)
  50. a = 0 #math.acos(w / ARUCO_TAG_WIDTH)
  51. tags.append((xid, xx, xy, d, a))
  52. # tags.append({'id': xid, 'x': xx, 'y': xy, 'distance': d, 'angle': a})
  53. # cv2.circle(a_image, (int(corners[x][0][0][0]), int(corners[x][0][0][1])), 2, (1, 0, 0), 2)
  54. # cv2.circle(a_image, (int(point[0]), int(point[1])), 2, (1, 0, 0), 2)
  55. return tags
  56.  
  57.  
  58. if __name__ == '__main__':
  59. parser = argparse.ArgumentParser(description='Do stuff')
  60. parser.add_argument('-c', '--camera', choices=['webcam', 'raspicam', 'potato'], default='webcam',
  61. help='Select camera interface, defaults to webcam')
  62. parser.add_argument('-y', '--height', type=int, default=480, help="Capture height, defaults to 480px")
  63. parser.add_argument('-x', '--width', type=int, default=720, help="Capture width, defaults to 720px")
  64. # parser.add_argument('-g', '--gui', type=bool, default=False)
  65. parser.add_argument('-i', '--camid', type=int, default=0, help="Webcam id, defaults to 0 for auto")
  66.  
  67.  
  68. args = parser.parse_args()
  69. print("Capturing with %s at %dx%d" % (args.camera, args.width, args.height))
  70.  
  71. if args.camera == "potato":
  72. print("How am I supposed to use a potato you moron?")
  73. exit(-1)
  74. elif args.camera == "raspicam":
  75. import picamera
  76.  
  77. with picamera.PiCamera() as camera:
  78. camera.resolution = (args.width, args.height)
  79. camera.framerate = 24
  80. time.sleep(2)
  81. image = np.empty((args.height * args.width * 3,), dtype=np.uint8)
  82. while True:
  83. camera.capture(image, 'bgr')
  84. cvimage = image.reshape((args.height, args.width, 3))
  85. aruco_tags = detect_and_cal_tags(cvimage, RASPICAM_FOCAL_LENGTH)
  86. print(aruco_tags)
  87.  
  88. else:
  89. cap = cv2.VideoCapture(args.camid)
  90. cap.set(cv2.CAP_PROP_FPS, 60)
  91. print("Camera FPS: %d" % cap.get(cv2.CAP_PROP_FPS))
  92. try:
  93. while True:
  94. ret, frame = cap.read()
  95. aruco_tags = detect_and_cal_tags(frame, WEBCAM_FOCAL_LENGTH)
  96. print(aruco_tags)
  97. except KeyboardInterrupt:
  98. pass
  99. finally:
  100. cap.release()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement