Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. from __future__ import print_function
  2. from pyimagesearch.shapedetector import ShapeDetector
  3. from imutils.video import WebcamVideoStream
  4. from imutils.video import FPS
  5. import cv2
  6. import numpy as np
  7. import os
  8. import subprocess as sp
  9. import serial
  10. import time
  11. import imutils
  12. import RPi.GPIO as GPIO
  13.  
  14. def sendLineInfo(newX,oldX,width):
  15. try:
  16. if (newX != oldX): # if tempX has changed from last instance, new line has been found / line has moved
  17. ser.write(chr(int(newX*(126/width))+1).encode())
  18. print("writing serial value: " + str(int(newX*(126/width))+1))
  19.  
  20. except:
  21. print("No new line info. No serial written.")
  22.  
  23. # Function to display camera window on GUI. Default input is True, run False to disable.
  24. def showImage(show = True):
  25. if show:
  26. try:
  27. cv2.imshow("Res1", image) # Displays image windows
  28. cv2.imshow("mask", mask) # Displays the masked window (black and white filter)
  29. except:
  30. print("can't show camera")
  31.  
  32.  
  33. # Stops thread loading images and destroys camera windows.
  34. def cleanUp():
  35. cv2.destroyAllWindows()
  36. vs.stop()
  37. GPIO.cleanup()
  38.  
  39.  
  40. GPIO.setmode(GPIO.BCM)
  41. GPIO.setup(17,GPIO.IN,pull_up_down=GPIO.PUD_UP)
  42.  
  43. def arduinoCallback1(channel):
  44. print("checkpoint1")
  45.  
  46.  
  47.  
  48. GPIO.add_event_detect(17, GPIO.FALLING, callback=arduinoCallback1, bouncetime=2000)
  49.  
  50. # Set-up
  51.  
  52. # Defines serial at baudrate 9600
  53. ser = serial.Serial('/dev/ttyACM0', 9600) #ttyACM0 is default
  54.  
  55. # camera resolution (depends on camera). This can be changed to a max of 1080p, but with the downside of longer processing time.
  56. w = 200
  57. vs = WebcamVideoStream(src=0).start()
  58.  
  59. # Defines lower color values for color filters
  60. lr_b = 0 #0
  61. lg_b = 40 #40
  62. lb_b = 190 #190
  63.  
  64. hr_b = 130 # 66
  65. hg_b = 176 # 126
  66. hb_b = 255 # 255
  67.  
  68. lr_r = 220 #235
  69. lg_r = 0 #25
  70. lb_r = 0 #50
  71.  
  72. hr_r = 255 #255
  73. hg_r = 75 #50
  74. hb_r = 125 #100
  75.  
  76. lr_g = 0 #235
  77. lg_g = 151 #25
  78. lb_g = 0 #50
  79.  
  80. hr_g = 130 #255
  81. hg_g = 255 #50
  82. hb_g = 165 #100
  83.  
  84. # Defines numpy array with color filter values
  85. lower_color_blue = np.array([lb_b, lg_b, lr_b], dtype=np.uint8)
  86. upper_color_blue = np.array([hb_b, hg_b, hr_b], dtype=np.uint8)
  87. lower_color_green = np.array([lb_g, lg_g, lr_g], dtype=np.uint8)
  88. upper_color_green = np.array([hb_g, hg_g, hr_g], dtype=np.uint8)
  89. lower_color_red = np.array([lb_r, lg_r, lr_r], dtype=np.uint8)
  90. upper_color_red = np.array([hb_r, hg_r, hr_r], dtype=np.uint8)
  91.  
  92. print("blyat")
  93. max_slider = 40
  94.  
  95. # current robot line coordinates is defined with null values.
  96. hx1 = 0
  97. hx2 = 0
  98. hy1 = 0
  99. hy2 = 0
  100.  
  101. packageSymbol = "rectangle"
  102. # highLineY is a temporary value which remembers max Y value of previous line. A line can not be selected unless it has a higher Y value than this line.
  103. # This variable is slowly decreased in the code if the robot does not detect any valid lines, until it eventually reaches 0 and the robot will detect and blue line it sees.
  104. highLineY = 0
  105. tempX = 320
  106. old_tempX = 320
  107. state = 0 #0: following line, 1: looking for sign, 2: picking up package, 3: delivering package
  108. # While loop for main logic
  109. while True:
  110. # Image parameters / set-up for selecting colors and finding lines
  111. image = vs.read()
  112. image = imutils.resize(image, width=w)
  113.  
  114. if state == 0:
  115.  
  116. mask = cv2.inRange(image, lower_color_blue, upper_color_blue) # find colors between the color limits defined earlier. This image is black and white.
  117. edges = cv2.Canny(mask,50,100) # Find edges from the previously defined mask.
  118. lines = cv2.HoughLinesP(edges, 1, np.pi/180, max_slider, minLineLength=60, maxLineGap=100) # This command finds lines from the edges found previously. Lines becomes an array of line start/end coordinates
  119.  
  120. try:
  121. for index, line in enumerate(lines): # This for-loop finds the line with the highest (lowest on screen) Y-coordinate. This will become the line the robot will follow as it's the line closest to the robot.
  122. #print("a")
  123. x1, y1, x2, y2 = line[0]
  124. if (y1 > y2):
  125. if (y1 > highLineY): # If new Y coordinate is higher than previously highest Y coordinate, dump previous line and replace its line coordinates with the newly found line.
  126. #print("")
  127. hx1 = x1
  128. tempX = x1
  129. hx2 = x2
  130. hy1 = y1
  131. hy2 = y2
  132. highLineY = y1
  133. elif (y2 > y1):
  134. if (y2 > highLineY):
  135. #print("")
  136. hx1 = x1
  137. hx2 = x2
  138. tempX = x2
  139. hy1 = y1
  140. hy2 = y2
  141. highLineY = y2
  142. else:
  143. #print("what")
  144. cv2.line(image, (hx1, hy1), (hx2, hy2), (255, 0, 255), 5)
  145. if (highLineY > 10): # This slowly reduces the previously highest Y coordinate. This mechanism is neccesary as the robot would otherwise quickly select a new totally different line, if it for a moment can't see it's previous line.
  146. highLineY = highLineY - 5
  147.  
  148. if (hy2-hy1)/(hx2-hx1) > 9999:
  149. slope = 9999
  150. elif (hy2-hy1)/(hx2-hx1) < -9999:
  151. slope = -9999
  152. else:
  153. slope = (hy2-hy1)/(hx2-hx1)
  154.  
  155.  
  156. cv2.line(image, (hx1, hy1), (hx2, hy2), (255, 0, 255), 5) # Draws the new line on the "img" windows.
  157. # chr(254).encode()
  158. #ser.write(chr(tempX*0.39).encode())
  159. #print(chr(tempX*0.39).encode())
  160.  
  161. except:
  162. #print("can't find line.")
  163. if (highLineY > 10):
  164. highLineY = highLineY - 5
  165. slope = 9000
  166.  
  167. if (tempX > 40 and tempX < 60):
  168. if (slope > -0.7 and slope < 0):
  169. sendLineInfo(newX = 100, oldX = old_tempX, width = w)
  170. elif (slope < 0.7 and slope > 0):
  171. sendLineInfo(newX = 27, oldX = old_tempX, width = w)
  172. else:
  173. sendLineInfo(newX = tempX, oldX = old_tempX, width = w)
  174. else:
  175. sendLineInfo(newX = tempX, oldX = old_tempX, width = w)
  176.  
  177. old_tempX = tempX
  178. elif state == 1:
  179. print("old position of state 1 code")
  180.  
  181. showImage() # run showImage(False) to disable imageview.
  182.  
  183. if cv2.waitKey(1) & 0xFF == ord('q'):
  184. ser.write(chr(int(0)).encode()) # sending 0 over serial to stop movement.
  185. break # Stops program if button "q" is pressed.
  186.  
  187. cleanUp()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement