SHARE
TWEET

Untitled

a guest Jan 29th, 2020 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2. from PIL import Image
  3. import numpy as np
  4. import cv2
  5. import cv2.aruco as aruco
  6. import matplotlib.pyplot as plt
  7.  
  8. import pyglet
  9.  
  10.  
  11.  
  12.  
  13. def drawpoint(x, y, color, pic):
  14.     pic.putpixel((int(x), int(y)), color)
  15.     pic.putpixel((int(x) + 1, int(y) - 1), color)
  16.     pic.putpixel((int(x) - 1, int(y) + 1), color)
  17.     pic.putpixel((int(x) + 1, int(y) + 1), color)
  18.     pic.putpixel((int(x) - 1, int(y) - 1), color)
  19.     pic.putpixel((int(x) + 1, int(y)), color)
  20.     pic.putpixel((int(x) - 1, int(y)), color)
  21.     pic.putpixel((int(x), int(y) + 1), color)
  22.     pic.putpixel((int(x), int(y) - 1), color)
  23.  
  24.  
  25. aruco_dict = aruco.custom_dictionary(0, 4, 1)
  26. aruco_dict.bytesList = np.empty(shape=(4, 2, 4), dtype=np.uint8)
  27. mybits = np.array([[0, 1, 1, 0], [0, 0, 1, 0], [1, 0, 0, 1], [1, 0, 0, 1]], dtype=np.uint8)
  28. mybits2 = np.array([[0, 0, 1, 1], [0, 0, 1, 1], [0, 0, 1, 0], [1, 1, 0, 1]], dtype=np.uint8)
  29. mybits3 = np.array([[1, 0, 1, 1], [0, 1, 0, 1], [0, 0, 1, 1], [0, 0, 1, 0]], dtype=np.uint8)
  30. mybits4 = np.array([[0, 1, 1, 0], [0, 1, 0, 1], [0, 1, 0, 0], [0, 1, 1, 1]], dtype=np.uint8)
  31.  
  32. aruco_dict.bytesList[0] = aruco.Dictionary_getByteListFromBits(mybits)
  33. # print(len(aruco_dict.bytesList))
  34. aruco_dict.bytesList[1] = aruco.Dictionary_getByteListFromBits(mybits2)
  35. aruco_dict.bytesList[2] = aruco.Dictionary_getByteListFromBits(mybits3)
  36. aruco_dict.bytesList[3] = aruco.Dictionary_getByteListFromBits(mybits4)
  37.  
  38. threshold = 100
  39. # img = cv2.imread('img.png', 0)
  40. img = None
  41.  
  42.  
  43.  
  44.  
  45. capture = cv2.VideoCapture(1)
  46.  
  47. capture.set(3,1920)
  48. capture.set(4,1080)
  49.  
  50. for i in range(30):
  51.     capture.read()
  52.  
  53. ret, img = capture.read()
  54. capture.release()
  55. cv2.destroyAllWindows()
  56. #img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  57. img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  58.  
  59. #plt.imshow(img)
  60.  
  61.  
  62. th2 = cv2.adaptiveThreshold(img, 1, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 3)
  63.  
  64. ret, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
  65.  
  66. kernel = np.ones((2, 3), np.uint8)
  67. opening = cv2.morphologyEx(th1, cv2.MORPH_OPEN, kernel)
  68. #opening = img
  69. plt.imshow(opening)
  70. plt.show()
  71.  
  72. parameters = aruco.DetectorParameters_create()
  73. corners, ids, rejectedImgPoints = aruco.detectMarkers(
  74.     opening, aruco_dict, parameters=parameters)
  75.  
  76. #print(corners, ids, rejectedImgPoints)
  77. print(corners)
  78.  
  79. pic = Image.new("RGB", (1500, 1500), (255, 255, 255))
  80.  
  81.  
  82.  
  83. minimal = [99999999, 99999999, 9999999, 9999999]
  84. minp = [0, 0, 0, 0]
  85. sax = [0, 0, 0, 0, 0]
  86. say = [0, 0, 0, 0, 0]
  87. for i in range(0, 4):
  88.     sx = 0
  89.     sy = 0
  90.     for j in range(0, 4):
  91.         sx += corners[i][0][j][0]
  92.         sy += corners[i][0][j][1]
  93.     sx /= 4
  94.     sy /= 4
  95.     sax[i] = sx
  96.     say[i] = sy
  97.  
  98. srx = 0
  99. sry = 0
  100.  
  101. for i in range(0, 4):
  102.     drawpoint(sax[i], say[i], (0, 255, 0), pic)
  103.     srx += sax[i]
  104.     sry += say[i]
  105.  
  106. srx /= 4
  107. sry /= 4
  108. drawpoint(srx, sry, (0, 255, 0), pic)
  109.  
  110. for i in range(0, 4):
  111.     for j in range(0, 4):
  112.         drawpoint(corners[i][0][j][0], corners[i][0][j][1], (255, 0, 0), pic)
  113.  
  114. for i in range(0, 4):
  115.     for j in range(0, 4):
  116.         dist = ((corners[i][0][j][0] - srx) ** 2 + (corners[i][0][j][1] - sry) ** 2)
  117.         if dist < minimal[i]:
  118.             minimal[i] = dist
  119.             minp[i] = j
  120.  
  121. for i in range(0, 4):
  122.     drawpoint(corners[i][0][minp[i]][0], corners[i][0][minp[i]][1], (0, 0, 255), pic)
  123.  
  124. plt.imshow(pic)
  125. plt.show()
  126.  
  127. # Enable blending
  128. pyglet.gl.glEnable(pyglet.gl.GL_BLEND)
  129. pyglet.gl.glBlendFunc(pyglet.gl.GL_SRC_ALPHA, pyglet.gl.GL_ONE_MINUS_SRC_ALPHA)
  130.  
  131. # Enable transparency
  132. pyglet.gl.glEnable(pyglet.gl.GL_ALPHA_TEST)
  133. pyglet.gl.glAlphaFunc(pyglet.gl.GL_GREATER, .1)
  134.  
  135. x1 = srx
  136. x2 = srx
  137. x3 = srx
  138. x0 = srx
  139. y1 = sry
  140. y2 = sry
  141. y3 = sry
  142. y0 = sry
  143.  
  144. window = pyglet.window.Window(1920, 1080)
  145.  
  146. @window.event
  147. def on_draw():
  148.     global x1, x2, x3, x0, y1, y2, y3, y0
  149.     window.clear()
  150.  
  151.     pyglet.gl.glColor4f(0.5, 1.0, 0.2, 0.5)
  152.  
  153.     pyglet.gl.glBegin(pyglet.gl.GL_QUADS)
  154.     pyglet.gl.glVertex3f(x0, y0, 0)
  155.     pyglet.gl.glVertex3f(x1, y1, 0)
  156.     pyglet.gl.glVertex3f(x3, y3, 0)
  157.     pyglet.gl.glVertex3f(x2, y2, 0)
  158.  
  159.     pyglet.gl.glEnd()
  160.  
  161.  
  162. def update(dt):
  163.     global x1, x2, x3, x0, y1, y2, y3, y0
  164.     x0 -= 1
  165.     y0 -= 1
  166.     x1 -= 1
  167.     y1 += 1
  168.     x3 += 1
  169.     y3 += 1
  170.     x2 += 1
  171.     y2 -= 1
  172.     on_draw()
  173.  
  174.  
  175. pyglet.clock.schedule_interval(update, 1 / 50.0)
  176. pyglet.app.run()
  177. pyglet.clock.unschedule(update)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top