opticalSensor.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/bin/python3
  2. import time
  3. import threading
  4. import traceback
  5. import numpy as np
  6. import cv2
  7. import cv2.aruco as aruco
  8. aruco_dict = aruco.Dictionary_get(aruco.DICT_4X4_50)
  9. def saveMarkers():
  10. image = np.zeros((400,400), np.uint8)
  11. image[:,:] = (255)
  12. image[ 50: 50 + 100, 50: 50 + 100] = aruco_dict.drawMarker(0, 100)
  13. image[-50 - 100:-50, 50: 50 + 100] = aruco_dict.drawMarker(1, 100)
  14. image[ 50: 50 + 100,-50 - 100:-50] = aruco_dict.drawMarker(2, 100)
  15. image[-50 - 100:-50,-50 - 100:-50] = aruco_dict.drawMarker(3, 100)
  16. cv2.imwrite("markers.png", image)
  17. parameters = aruco.DetectorParameters_create()
  18. def find_marker(image):
  19. # Our operations on the frame come here
  20. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  21. corners, ids, rejectedImgPoints = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)
  22. markers = [None] * 4
  23. if not corners:
  24. return markers
  25. for corner, id in zip(corners, ids.flatten()):
  26. # draw the bounding box of the ArUCo detection
  27. c = corner[0]
  28. cv2.line(image, tuple(c[0]), tuple(c[1]), (0, 255, 0), 2)
  29. cv2.line(image, tuple(c[1]), tuple(c[2]), (0, 255, 0), 2)
  30. cv2.line(image, tuple(c[2]), tuple(c[3]), (0, 255, 0), 2)
  31. cv2.line(image, tuple(c[3]), tuple(c[0]), (0, 255, 0), 2)
  32. cv2.putText(image, str(id), (int(c[0][0]), int(c[0][1]) - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
  33. cX = sum(c[:,0]) / 4
  34. cY = sum(c[:,1]) / 4
  35. cv2.circle(image, (int(cX), int(cY)), 4, (0, 0, 255), -1)
  36. if id < 4:
  37. markers[id] = (cX, cY)
  38. return markers
  39. def measureDistances(image):
  40. markers = find_marker(image)
  41. if markers[0] and markers[1]:
  42. cv2.line(image, (int(markers[0][0]),int(markers[0][1])), (int(markers[1][0]),int(markers[1][1])), (255, 0, 0), 2)
  43. length = np.sqrt((markers[0][0]-markers[1][0])**2 + (markers[0][1]-markers[1][1])**2)
  44. middle = abs(markers[0][0]-markers[1][0])
  45. return middle, length
  46. else:
  47. return None
  48. class OpticalSensor():
  49. def __init__(self):
  50. self.cap = cv2.VideoCapture(0)
  51. self._t = None
  52. self.values = None
  53. def start(self):
  54. if not self._t:
  55. self._t = threading.Thread(target=self._getFrames, args=())
  56. self._t.daemon = True # thread dies when main thread (only non-daemon thread) exits.
  57. self._t.start()
  58. def _getFrames(self):
  59. while True:
  60. success, image = self.cap.read()
  61. if success:
  62. self.values = measureDistances(image)
  63. print("opt:", self.values)
  64. def calibrate(self, x, y):
  65. pass
  66. def read(self):
  67. return self.values
  68. if __name__ == "__main__":
  69. # opt = OpticalSensor()
  70. # opt.start()
  71. # while True:
  72. # time.sleep(1)
  73. cap = cv2.VideoCapture(0)
  74. while True:
  75. success, image = cap.read()
  76. markers = find_marker(image)
  77. if markers[0] and markers[1]:
  78. cv2.line(image, (int(markers[0][0]),int(markers[0][1])), (int(markers[1][0]),int(markers[1][1])), (255, 0, 0), 2)
  79. length = np.sqrt((markers[0][0]-markers[1][0])**2 + (markers[0][1]-markers[1][1])**2)
  80. cv2.putText(image, "%.2fpx" % (length), (int(markers[0][0]), int(markers[0][1])), cv2.FONT_HERSHEY_SIMPLEX,
  81. 1.0, (255, 255, 0), 3)
  82. cv2.imshow("image", image)
  83. if cv2.waitKey(1) & 0xFF == ord('q'):
  84. break