Advertisement
Guest User

Untitled

a guest
Feb 18th, 2016
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. import cv2
  2. import time
  3. import numpy
  4. import sys
  5. from networktables import NetworkTable
  6. from wx import Rect
  7. #initalization occurs here
  8. e=0
  9. BLUE = numpy.array((255,0,0),numpy.uint8)
  10. BLUE = numpy.array((255, 0, 0), numpy.uint8)
  11. GREEN = numpy.array((0,0,0), numpy.uint8)
  12. BLACK = numpy.array((0,0,0), numpy.uint8)
  13. YELLOW = numpy.array((0, 255, 255), numpy.uint8)
  14. LOWER_BOUNDS = numpy.array((58,0,109), numpy.uint8)
  15. UPPER_BOUNDS = numpy.array((93,255,240), numpy.uint8)
  16. resize = numpy.array((320,240), numpy.uint8)
  17. TOP_TARGET_HEIGHT = 97
  18. TOP_CAMERA_HEIGHT = 15
  19. VERTICAL_FOV = 47;
  20. HORIZONTAL_FOV = 67;
  21. #CHANGE
  22. CAMERA_ANGLE = 90;
  23. matThresh = numpy.array((0,0,0), numpy.uint8)
  24. matOriginal = numpy.array((0,0,0), numpy.uint8)
  25. clusters = numpy.array((0,0,0), numpy.uint8)
  26. matHSV = numpy.array((0,0,0), numpy.uint8)
  27.  
  28. matHeirarchy = numpy.array((0,0,0),numpy.uint8)
  29. videoCapture = cv2.VideoCapture()
  30. NetworkTable.setIPAddress("roboRIO-1403-FRC.local")
  31. NetworkTable.setClientMode()
  32. NetworkTable.initialize()
  33.  
  34. sd = NetworkTable.getTable("SmartDashboard")
  35. sd.putNumber('Initial', 1)
  36. shouldRun = True
  37. biggestArea =0
  38. biggestAreaIndex =0
  39. buttonPressed = False
  40. contours = []
  41. selected = []
  42. videoCapture = cv2.VideoCapture()
  43.  
  44. def normalize360(angle):
  45. while angle<=360:
  46. angle-=360
  47. while angle<0:
  48. angle+=360
  49. return angle
  50.  
  51.  
  52.  
  53. def processImage():
  54. matThresh = numpy.array((0,0,0), numpy.uint8)
  55. matOriginal = numpy.array((0,0,0), numpy.uint8)
  56. clusters = numpy.array((0,0,0), numpy.uint8)
  57. matHSV = numpy.array((0,0,0), numpy.uint8)
  58.  
  59. matHeirarchy = numpy.array((0,0,0),numpy.uint8)
  60. emp = []
  61. print "We be processing Image"
  62. FrameCount =0
  63. x=0
  64.  
  65. while(FrameCount<100):
  66. print FrameCount
  67. contours = emp
  68. selected = emp
  69.  
  70. print "Before Read"
  71. print "Something is going on here"
  72. ret, matOriginal =videoCapture.read(0)
  73.  
  74. #matOriginal = cv2.imread("~/Desktop/original.png")
  75. # videoCapture.read(matOriginal)
  76. print "After Read"
  77. print "1"
  78. cv2.imwrite(" ~/Desktop/testing.png", matOriginal, )
  79. print "11"
  80. matHSV = cv2.cvtColor(matOriginal, cv2.COLOR_BGR2HSV)
  81. print "111"
  82. matThresh = cv2.inRange(matHSV, LOWER_BOUNDS, UPPER_BOUNDS)
  83. print "1111"
  84. filler, contours, heirarchy = cv2.findContours(matThresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  85.  
  86. #for n in xrange(len(tmp)):
  87. # print "tmp[" + str(n) + "]: "
  88. # print tmp[n]
  89. #im2, contours, matHeirarchy = tmp
  90.  
  91. #print "Size :" + contours[0]
  92. #print contours
  93. for i in contours:
  94. print contours
  95. for x in i:
  96. rec = Rect()
  97. print i[x]
  98. rec = cv2.boundingRect(i[x])
  99. aspect = float(rec.width)/float(rec.height)
  100. if(aspect>1.0):
  101. if(rec.area()>biggestArea):
  102. biggestArea = rec.area()
  103. selected.append(contours[i])
  104. print "Size of selected :" + len(selected)
  105. print selected
  106. for i in selected:
  107. rec = Rect()
  108.  
  109. rec = cv2.boundingRect(selected[i])
  110. print "looping on selected"
  111. cv2.rectangle(matOriginal, rec.GetBottomRight(), rec.GetTopLeft(), BLACK)
  112. if len(selected ==1):
  113. print "selected is one"
  114. rec = Rect()
  115. rec = cv2.boundingRect(selected[0])
  116. y = rec.getBottomRight().y + rec.GetHeight()/2
  117. y = -((2*(y/matOriginal.cols()))-1)
  118. distance = (TOP_TARGET_HEIGHT - TOP_CAMERA_HEIGHT) / numpy.tan((y*VERTICAL_FOV/2.0 +CAMERA_ANGLE) * numpy.pi/180)
  119. targetX = rec.GetTopLeft().x + rec.GetWidth()/2
  120. targetX = (2*(targetX/matOriginal.rows)) -1
  121. azimuth = normalize360(targetX*HORIZONTAL_FOV/2.0+0)
  122. center = rec.GetBottomRight().x - rec.GetWidth()/2 - 15, rec.GetBottomRight().y - rec.height / 2
  123. centerw = rec.GetBottomRight().x - rec.width / 2 - 15, rec.GetBottomRight().y - rec.height / 2 - 20
  124. cv2.putText(matOriginal, ""+ int(distance), center, cv2.FONT_HERSHEY_PLAIN, 1,BLACK)
  125. cv2.putText(matOriginal, ""+ int(azimuth), center, cv2.FONT_HERSHEY_PLAIN, 1, BLACK)
  126. cv.imwrite("`/Desktop/output.jpg", matOriginal)
  127. FrameCount+=1
  128.  
  129.  
  130.  
  131. while(shouldRun):
  132.  
  133.  
  134. #try:
  135. sd.putNumber("check",1)
  136. videoCapture = cv2.VideoCapture(0)
  137.  
  138. videoCapture.open('http://10.14.3.24/mjpg/video.mjpg')
  139. if not videoCapture.isOpened():
  140. print "Camera is connected "
  141. if videoCapture.isOpened():
  142. print "Camera not connected"
  143. # while(not videoCapture.isOpened()):
  144. # pass
  145. print "About to process image"
  146. processImage()
  147. print
  148. # except(Exception):
  149. #print "aight, no camera detected. Jays make sure the IP is right before next match"
  150. # break
  151. sd.putNumber("check",3)
  152. videoCapture.release()
  153. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement