Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. #I smoke weedz every day for da memez
  2. import numpy as np
  3. import cv2
  4. import sys
  5. import serial
  6.  
  7. #Determines the port for serial communication (Arduino)
  8. ser=serial.Serial('/dev/ttyACM0',9600)
  9. #Using Python 2.7
  10. sys.path.append('/usr/local/lib/python2.7/site-packages')
  11.  
  12.  
  13. #x max = 650
  14. #y max = 480
  15.  
  16. cam = cv2.VideoCapture(0)
  17. cam.set(3,320)
  18. cam.set(4,240)
  19.  
  20. cv2.namedWindow('frame')
  21.  
  22. cv2.namedWindow('frame2')
  23.  
  24. cv2.moveWindow('frame', 0,0)
  25. cv2.moveWindow('frame2', 645,35)
  26.  
  27.  
  28. while True:
  29. ballcollected = False
  30. firstcone = False
  31. secondcone = False
  32. lastcone = False
  33. greenL = (30, 22, 80)
  34. greenH = (50, 200, 255)
  35. while True:
  36. ballfound = False
  37. ret, image =cam.read()
  38. hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
  39. greencolor = cv2.dilate(cv2.erode(cv2.inRange(hsv, greenL, greenH), None, iterations=2), None, iterations=2)
  40.  
  41.  
  42.  
  43. contour = cv2.findContours(greencolor.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
  44.  
  45.  
  46. #initialize center
  47. c = None
  48.  
  49. #if there is at least one green object
  50. if len(contour) > 0:
  51. #biggest green object
  52. largest = max(contour, key=cv2.contourArea)
  53. #radius and center
  54. ((x,y), r) = cv2.minEnclosingCircle(largest)
  55. #finds center of mass
  56. #moments- help finds things such as area, center, etc.
  57. M = cv2.moments(largest)
  58. xx = (int(M["m10"] / M["m00"]))
  59. yy = (int(M["m01"] / M["m00"]))
  60. c = (int(M["m10"] / M["m00"]),int(M["m01"] / M["m00"]))
  61.  
  62. #draw circle
  63. #make sure object is sufficiently large
  64. if r > 15:
  65. cv2.circle(image, c, 5, (0,255,255), -1)
  66. cv2.circle(image, (int(x), int(y)), int(r), (255,0,255), 2)
  67. ballfound = True
  68. cv2.imshow('frame',image)
  69. cv2.imshow('frame2',greencolor)
  70. if ballfound:
  71. #when ball stops moving
  72.  
  73. #if cbefore == None:
  74.  
  75.  
  76. #deltad = xx
  77. #if delta < 15:
  78. # ballstopped = True
  79.  
  80.  
  81. #print(int(x))
  82. #print(int(r))
  83.  
  84.  
  85. #replace with moments center
  86.  
  87. if lastcone: #oriented towards last cone
  88. break
  89. elif int(yy) > 180 and firstcone: #reach first cone and look for second
  90. secondcone = True
  91. ser.write('5')
  92. elif int(yy) > 180:
  93. ser.write('4')
  94. ballcollected = True
  95. elif int(xx) > 180:
  96. ser.write('2')
  97. elif int(xx) < 140:
  98. ser.write('3')
  99. else:
  100. ser.write('7')
  101.  
  102. ballfound = False
  103.  
  104. else:
  105. ser.write('1')
  106. if ballcollected:
  107. #change to color of first cone
  108. firstcone = True
  109. greenL = (30, 22, 80)
  110. greenH = (50, 200, 255)
  111. ballcollected = False
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. #cv2.waitKey(500)
  119. key = cv2.waitKey(5) & 0xFF
  120. if key == ord("q"):
  121. break
  122.  
  123. if secondcone:
  124. #change to color of second cone
  125. greenL = (30, 22, 80)
  126. greenH = (50, 200, 255)
  127. lastcone = True
  128. secondcone = False
  129.  
  130. #ser.write('6')
  131. cam.release()
  132. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement