SHARE
TWEET

Untitled

a guest Feb 17th, 2016 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import cv2
  2. import numpy
  3. from networktables import NetworkTable
  4. import sys
  5. import time
  6.  
  7. class OpenCV:
  8.    
  9.  
  10. #   constants for the color rbg values
  11.     e=0
  12.    
  13.     RED = [0, 0, 255]
  14.     BLUE = [255, 0, 0]
  15.     GREEN = [0,0,0]
  16.     BLACK = [0,0,0]
  17.     YELLOW = [0, 255, 255]
  18.     LOWER_BOUNDS = [58,0,109]
  19.     UPPER_BOUNDS = [93,255,240]
  20.     resize = [320,240]
  21.  
  22.     TOP_TARGET_HEIGHT = 97
  23.     TOP_CAMERA_HEIGHT = 15
  24.     VERTICAL_FOV  = 47;
  25.     HORIZONTAL_FOV  = 67;
  26.     #CHANGE
  27.     CAMERA_ANGLE = 90;
  28.    
  29.     videoCapture = cv2.VideoCapture()
  30.  
  31.     shouldRun = True;
  32.  
  33.    
  34.  
  35.    
  36.        
  37.        
  38.        # matHSV = []
  39.      #   matThresh = []
  40.       #  clusters = []
  41.      #   matHeirarchy = []
  42.     NetworkTable.setIPAddress("roboRIO-1403-FRC.local")
  43.     NetworkTable.setClientMode()
  44.     NetworkTable.initialize()
  45.  
  46.     sd = NetworkTable.getTable("SmartDashboard")
  47.     sd.putNumber('Initial', 1)
  48.     def processImage():
  49.        
  50.         RED = numpy.array((0, 0, 255), numpy.uint8)
  51.         BLUE = numpy.array((255, 0, 0), numpy.uint8)
  52.         GREEN = numpy.array((0,0,0), numpy.uint8)
  53.         BLACK = numpy.array((0,0,0), numpy.uint8)
  54.         YELLOW = numpy.array((0, 255, 255), numpy.uint8)
  55.         LOWER_BOUNDS = numpy.array((58,0,109), numpy.uint8)
  56.         UPPER_BOUNDS = numpy.array((93,255,240), numpy.uint8)
  57.         resize = numpy.array((320,240), numpy.uint8)
  58.  
  59.         TOP_TARGET_HEIGHT = 97
  60.         TOP_CAMERA_HEIGHT = 15
  61.         VERTICAL_FOV  = 47;
  62.         HORIZONTAL_FOV  = 67;
  63.         #CHANGE
  64.         CAMERA_ANGLE = 90;
  65.         matThresh = numpy.array((0,0,0), numpy.uint8)
  66.         matHeirarchy = numpy.array((0,0,0),numpy.uint8)
  67.         videoCapture = cv2.VideoCapture()
  68.  
  69.         shouldRun = True;
  70.  
  71.    
  72.  
  73.        
  74.         videoCapture = cv2.VideoCapture()
  75.         sd = NetworkTable.getTable("SmartDashboard")
  76.         sd.putNumber("check",4)
  77.         #matOriginal = numpy.array((0,0,0), numpy.uint8)
  78.         matHSV = numpy.array((0,0,0), numpy.uint8)
  79.        
  80.        
  81.         matOfPoint = numpy.array((0), numpy.uint8)
  82.        
  83.         frameCount=0
  84.         before = int(round(time.time()*1000))
  85.         emp = numpy.array((0), numpy.uint8)
  86.         contours = numpy.array((0), numpy.uint8)
  87.         while(frameCount<100):
  88.             contours = emp
  89.           #stopped here
  90.             matOriginal = cv2.imread("simple.png")
  91.            
  92.             videoCapture.read(matOriginal)
  93.            
  94.             cv2.imwrite("~/Desktop/simple.png", matOriginal)
  95.            
  96.            
  97.             matHSV = numpy.array((0,0,0), numpy.uint8)
  98.             matHSV=cv2.cvtColor(matOriginal,cv2.COLOR_BGR2HSV)
  99.             matThresh = cv2.inRange(matHSV, LOWER_BOUNDS, UPPER_BOUNDS)
  100.             sd.putNumber("it got herelasjd", 5)
  101.             im2, contours, matHierarchy = cv2.findContours(matThresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  102.            # print contours
  103.            # cv2.findContours(matThresh, contours, matHeirarchy, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  104.             sd.putNumber("it got herelasjd", 5)
  105.            
  106.            
  107.             counter =0
  108.             for x in contours:
  109.                
  110.                 numpy.append(contours, x)
  111.                 rec = cv2.boundingRect(matOfPoint)
  112.                 ho,y,w,h = cv2.boundingRect(matOfPoint)
  113.                 #print ho
  114.                 #rec = cv2.rectangle(x,y,w,h)
  115.                 if(h<25 or w<25):
  116.                     chris = numpy.array((x), numpy.uint8)
  117.                     numpy.delete(matOfPoint,chris)
  118.                    # matOfPoint.remove(x)
  119.                     continue
  120.                 for mop in contours:
  121.                    
  122.                     rec = cv2.boundingRect(mop)
  123.                    
  124.                     #rec = cv2.boundingRect(mop)
  125.                     cv2.rectangle(matOriginal, ((x+w),(y+h)), (x,y), BLACK)
  126.                     #print x
  127.            
  128.  
  129.             if len(contours)==1:
  130.                 print x
  131.                 #print y
  132.                 xval = x+w
  133.                 yval = y+h
  134.                 rec = cv2.boundingRect(contours[0])
  135.                # y=yval+h / 2
  136.                # y = -((2*(y/matOriginal.height()))-1)
  137.                # distance = (TOP_TARGET_HEIGHT - TOP_HEIGHT)/pumpy.tan((y*VERTICAL_FOV/2.0+CAMERA_ANGLE)*numpy.pi/180)
  138.                # targetX = rec.tl() + rec.width /2
  139.               # targetX =(2*(targetX / matOriginal.width()))-1
  140.                # azimuth = normalize360(targetX*HORIZONTAL_FOV/2.0+0)
  141.                 #comment out later
  142.                # center = xval-w/2 - 15, yval-h/2
  143.                # centerw = -w/2 - 15, yval - h/2
  144.                 #cv2.putText(matOriginal, ""+int(distance), center, cv2.FONT_HERSHEY_PLAIN, 1, BLACK)
  145.                # cv2.putTect(matOriginal, ""+int(azimuth), centerw, cv2.FONT_HERSHEY_PLAIN, 1, BLAC
  146.            
  147.                # print "nada"
  148.                # print x+0.5*w
  149.               #  print y+0.5*h
  150.                # sd.putNumber('centerX', xval)
  151.                 #sd.putNumber('centerY', yval)
  152.            
  153.            
  154.            
  155.            
  156.            # cv2.imwrite("output.png", matOriginal)
  157.             frameCount+=1
  158.         shouldRun =False
  159.  
  160.  
  161.  
  162.  
  163.     def normalize360():
  164.         while(angle>=360.0):
  165.              angle-=360.0
  166.  
  167.         while(angle<0.0):
  168.              angle+=360.0
  169.         return angle
  170.    
  171.     while(shouldRun):
  172.            
  173.            
  174.         try:
  175.             sd.putNumber("check",1)
  176.             videoCapture = cv2.VideoCapture()
  177.             videoCapture.open("10.14.3.24/mjpg/video.mjpg")
  178.             sd.putNumber("check",2)
  179.             #while(not videoCapture.isOpened()):
  180.                #pass
  181.            
  182.             processImage()
  183.         except(e):
  184.             break
  185.         sd.putNumber("check",3)
  186.         videoCapture.release()
  187.         sys.exit()
RAW Paste Data
Top