Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. from cv2 import cv2
  2. import numpy as npy
  3. import serial
  4. from time import sleep
  5.  
  6. port = "COM23"
  7. baud = 9800
  8. ser = serial.Serial(port, baud, timeout=1)
  9.  
  10. a = input()
  11.  
  12. while (a != 's'):
  13. buf = b'\x08\x00\x80\x09\x00\x04' + bytes([0]) + bytes([0]) + bytes([1]) + bytes([0])
  14. ser.write(buf)
  15. print(ser.readline())
  16. a = input()
  17.  
  18.  
  19. buf = b'\x08\x00\x80\x09\x00\x04' + bytes([0]) + bytes([0]) + bytes([2]) + bytes([0])
  20. ser.write(buf)
  21.  
  22. cap = cv2.VideoCapture(0)
  23. cv2.namedWindow('image')
  24.  
  25. low1 = npy.array([0, 130, 90])
  26. high1 = npy.array([15, 255, 255])
  27. low2 = npy.array([165, 130, 90])
  28. high2 = npy.array([180, 255, 255])
  29.  
  30. low3 = npy.array([69, 100, 120])
  31. high3 = npy.array([75, 200, 220])
  32.  
  33. encoder = 0
  34.  
  35. berries = [[],[]]
  36. allberries = []
  37.  
  38. switch = 0
  39. lastid = -1
  40. # 0 130 90
  41. # 165 130 90
  42.  
  43. while(True):
  44. # Capture frame-by-frame
  45. ret, frame = cap.read()
  46.  
  47. hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
  48.  
  49. mask = cv2.inRange(hsv, low1, high1)
  50. mask += cv2.inRange(hsv, low2, high2)
  51. #mask += cv2.inRange(hsv, low3, high3)
  52.  
  53. cv2.bitwise_and(hsv, hsv, mask = mask)
  54. connectivity = 7
  55. output = cv2.connectedComponentsWithStats(mask, connectivity, cv2.CV_32S)
  56.  
  57. num_labels = output[0]
  58. labels = output[1]
  59. stats = output[2]
  60. centroids = output[3]
  61.  
  62.  
  63. del berries[(switch + 1)%2][:]
  64. for i in range(num_labels):
  65. x, y, w, h, s = stats[i]
  66. if s > 2500 and s < 50000 and y > 50 and y < 100:
  67. sx = hex(x)[2:].zfill(4)
  68. sy = hex(y)[2:].zfill(4)
  69.  
  70. cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),3)
  71. cv2.circle(frame,(int(centroids[i][0]), int(centroids[i][1])), 3, (0,255,0), -1)
  72.  
  73. a = 2500
  74. b = -1
  75.  
  76.  
  77. for berry in berries[(switch)%2]:
  78.  
  79. r = (berry['x'] - centroids[i][0])**2 + (berry['y'] - centroids[i][1])
  80.  
  81. if r < a:
  82. a = r
  83. b = berry['id']
  84.  
  85.  
  86.  
  87. if b == -1:
  88. lastid+=1
  89. berries[(switch + 1)%2].append({'x' : centroids[i][0], 'y' : centroids[i][1], 'id' : lastid})
  90.  
  91. a = ser.readline()
  92. while (len(a) < 7) :
  93. timeout = 0
  94. buf = b'\x08\x00\x80\x09\x00\x04' + bytes([0]) + bytes([0]) + bytes([3]) + bytes([0])
  95. ser.write(buf)
  96. a = ser.readline()
  97. print(a)
  98.  
  99. allberries.append({'x' : centroids[i][0], 'y' : centroids[i][1], 'id' : lastid, 'enc' : a[5] + a[6]*256})
  100. else :
  101. berries[(switch + 1)%2].append({'x' : centroids[i][0], 'y' : centroids[i][1], 'id' : b})
  102.  
  103.  
  104. print(allberries)
  105. switch=(switch + 1)%2
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114. cv2.imshow('frame', frame)
  115. cv2.imshow('mask', mask)
  116. if cv2.waitKey(1) == ord('q'):
  117. cap.release()
  118. cv2.destroyAllWindows()
  119. exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement