SHARE
TWEET
Untitled
a guest
Feb 17th, 2016
86
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- import cv2
- import numpy
- from networktables import NetworkTable
- import sys
- import time
- class OpenCV:
- # constants for the color rbg values
- e=0
- RED = [0, 0, 255]
- BLUE = [255, 0, 0]
- GREEN = [0,0,0]
- BLACK = [0,0,0]
- YELLOW = [0, 255, 255]
- LOWER_BOUNDS = [58,0,109]
- UPPER_BOUNDS = [93,255,240]
- resize = [320,240]
- TOP_TARGET_HEIGHT = 97
- TOP_CAMERA_HEIGHT = 15
- VERTICAL_FOV = 47;
- HORIZONTAL_FOV = 67;
- #CHANGE
- CAMERA_ANGLE = 90;
- videoCapture = cv2.VideoCapture()
- shouldRun = True;
- # matHSV = []
- # matThresh = []
- # clusters = []
- # matHeirarchy = []
- NetworkTable.setIPAddress("roboRIO-1403-FRC.local")
- NetworkTable.setClientMode()
- NetworkTable.initialize()
- sd = NetworkTable.getTable("SmartDashboard")
- sd.putNumber('Initial', 1)
- def processImage():
- RED = numpy.array((0, 0, 255), numpy.uint8)
- BLUE = numpy.array((255, 0, 0), numpy.uint8)
- GREEN = numpy.array((0,0,0), numpy.uint8)
- BLACK = numpy.array((0,0,0), numpy.uint8)
- YELLOW = numpy.array((0, 255, 255), numpy.uint8)
- LOWER_BOUNDS = numpy.array((58,0,109), numpy.uint8)
- UPPER_BOUNDS = numpy.array((93,255,240), numpy.uint8)
- resize = numpy.array((320,240), numpy.uint8)
- TOP_TARGET_HEIGHT = 97
- TOP_CAMERA_HEIGHT = 15
- VERTICAL_FOV = 47;
- HORIZONTAL_FOV = 67;
- #CHANGE
- CAMERA_ANGLE = 90;
- matThresh = numpy.array((0,0,0), numpy.uint8)
- matHeirarchy = numpy.array((0,0,0),numpy.uint8)
- videoCapture = cv2.VideoCapture()
- shouldRun = True;
- videoCapture = cv2.VideoCapture()
- sd = NetworkTable.getTable("SmartDashboard")
- sd.putNumber("check",4)
- #matOriginal = numpy.array((0,0,0), numpy.uint8)
- matHSV = numpy.array((0,0,0), numpy.uint8)
- matOfPoint = numpy.array((0), numpy.uint8)
- frameCount=0
- before = int(round(time.time()*1000))
- emp = numpy.array((0), numpy.uint8)
- contours = numpy.array((0), numpy.uint8)
- while(frameCount<100):
- contours = emp
- #stopped here
- matOriginal = cv2.imread("simple.png")
- videoCapture.read(matOriginal)
- cv2.imwrite("~/Desktop/simple.png", matOriginal)
- matHSV = numpy.array((0,0,0), numpy.uint8)
- matHSV=cv2.cvtColor(matOriginal,cv2.COLOR_BGR2HSV)
- matThresh = cv2.inRange(matHSV, LOWER_BOUNDS, UPPER_BOUNDS)
- sd.putNumber("it got herelasjd", 5)
- im2, contours, matHierarchy = cv2.findContours(matThresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
- # print contours
- # cv2.findContours(matThresh, contours, matHeirarchy, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
- sd.putNumber("it got herelasjd", 5)
- counter =0
- for x in contours:
- numpy.append(contours, x)
- rec = cv2.boundingRect(matOfPoint)
- ho,y,w,h = cv2.boundingRect(matOfPoint)
- #print ho
- #rec = cv2.rectangle(x,y,w,h)
- if(h<25 or w<25):
- chris = numpy.array((x), numpy.uint8)
- numpy.delete(matOfPoint,chris)
- # matOfPoint.remove(x)
- continue
- for mop in contours:
- rec = cv2.boundingRect(mop)
- #rec = cv2.boundingRect(mop)
- cv2.rectangle(matOriginal, ((x+w),(y+h)), (x,y), BLACK)
- #print x
- if len(contours)==1:
- print x
- #print y
- xval = x+w
- yval = y+h
- rec = cv2.boundingRect(contours[0])
- # y=yval+h / 2
- # y = -((2*(y/matOriginal.height()))-1)
- # distance = (TOP_TARGET_HEIGHT - TOP_HEIGHT)/pumpy.tan((y*VERTICAL_FOV/2.0+CAMERA_ANGLE)*numpy.pi/180)
- # targetX = rec.tl() + rec.width /2
- # targetX =(2*(targetX / matOriginal.width()))-1
- # azimuth = normalize360(targetX*HORIZONTAL_FOV/2.0+0)
- #comment out later
- # center = xval-w/2 - 15, yval-h/2
- # centerw = -w/2 - 15, yval - h/2
- #cv2.putText(matOriginal, ""+int(distance), center, cv2.FONT_HERSHEY_PLAIN, 1, BLACK)
- # cv2.putTect(matOriginal, ""+int(azimuth), centerw, cv2.FONT_HERSHEY_PLAIN, 1, BLAC
- # print "nada"
- # print x+0.5*w
- # print y+0.5*h
- # sd.putNumber('centerX', xval)
- #sd.putNumber('centerY', yval)
- # cv2.imwrite("output.png", matOriginal)
- frameCount+=1
- shouldRun =False
- def normalize360():
- while(angle>=360.0):
- angle-=360.0
- while(angle<0.0):
- angle+=360.0
- return angle
- while(shouldRun):
- try:
- sd.putNumber("check",1)
- videoCapture = cv2.VideoCapture()
- videoCapture.open("10.14.3.24/mjpg/video.mjpg")
- sd.putNumber("check",2)
- #while(not videoCapture.isOpened()):
- #pass
- processImage()
- except(e):
- break
- sd.putNumber("check",3)
- videoCapture.release()
- sys.exit()
RAW Paste Data
