<commit_before>#IMPORTANT!!!
#Use Table "datatable" for testing &
#table "vision" for final robot code.
#DO NOT implement "vision" in simulated robot code!

#Imports
import cv2
import urllib
import numpy as np
from time import time
from pynetworktables import *

#Debug Flag
debug = False

#Source variables
ret = False
useInternal = True
camClosed = True
internalCam = 0
robotCamIP = 'http://10.25.76.11/mjpg/video.mjpg'
bytes=''

#Robot Variables
useLocalRobot = True
robotIP = '10.25.76.2'
localIP = '127.0.0.1'
allianceIsRed = True
#isConnected = False

#Thresholding HSV Values
redMin = np.array([160, 50, 50], np.uint8)
redMax = np.array([180, 200, 200], np.uint8)
blueMin = np.array([70, 50, 50], np.uint8)
blueMax = np.array([135, 200, 200], np.uint8)

#Function Parameters
seSize = (2, 2)        #Element for morphology
se = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, seSize)  #Create structuring element before loop
minDist = 100          #Minimum distance between circle center points
radMin = 50            #Minimum detected circle radius
radMax = 250           #Maximum detected circle radius
frameInterval = 33     #Time in ms in between frames
gaussian_power = 5     #Gaussian blur parameter
param1ForCircles = 50  #First Parameter for HoughCircles -
param2ForCircles = 25  #Second Parameter for HoughCircles -
dpForCircles = 2       #DP parameter for HoughCircles -

#Circle Parameters
centerPoint = 0        #Circle center point
radius = 0             #Circle radius
##largestRadius = 0
##largestCircle = (0, 0, 0)
prev_x = 0
prev_y = 0
prev_r = 0

#FPS Calculation
startAtFrame = 10
totalTime = 0
numberOfFrames = 0
time1 = time()
time2 = 0
fps = 0
fpsTextPos = (5, 25)
fpsTextColor = (255, 255, 255)

#Define setting variables
CV_CAP_PROP_FRAME_WIDTH = 3
CV_CAP_PROP_FRAME_HEIGHT = 4

#Window
cv2.namedWindow("Vision DS", 1)

#Extra Images
errorScreen = cv2.imread("system_down.jpg")
blank = np.zeros((400, 400, 3), np.uint8)
canvas = np.zeros((645, 645, 3), np.uint8)
canvas[245:645, :] = (145, 145, 145)

#NetworkTables Client Init
if useLocalRobot:
    NetworkTable.SetIPAddress(localIP)
else:
    NetworkTable.SetIPAddress(robotIP)
NetworkTable.SetClientMode()
NetworkTable.Initialize()

#Table Object
if useLocalRobot:
    table = NetworkTable.GetTable("datatable")
else:
    table = NetworkTable.GetTable("vision")

#Check robot color alliance
def checkAlliance():
    global allianceIsRed
    global isConnected
    try:
        allianceIsRed = table.GetBoolean("allianceRed")
    except TableKeyNotDefinedException:
        isConnected = False

#Initial Robot Connection
def connect():
    global isConnected
    isConnected = False
    while not isConnected:
        cv2.imshow("Vision DS", errorScreen)
        try:
            isConnected = table.GetBoolean("connection")
        except TableKeyNotDefinedException:
            isConnected = False
        checkAlliance()
        if cv2.waitKey(frameInterval) == 27:
            cv2.destroyAllWindows()
            exit(0)

#Callback Function
#def callBack(event, x, y, flags, param):
#    if event == cv2.cv.EVENT_LBUTTONDOWN:
#        print(img[x, y])
#        if debug:
#            print("Click")

#cv2.setMouseCallback("canvas", callBack)

#Choose source for cam
if useInternal:
    cam = cv2.VideoCapture(internalCam)
    cam.set(CV_CAP_PROP_FRAME_WIDTH, 320)
    cam.set(CV_CAP_PROP_FRAME_HEIGHT, 240)
else:
    cam = cv2.VideoCapture(robotCamIP)

#Verify cam
while camClosed:
    if cam.isOpened():
    #Read Cam
        if useLocalRobot:
            ret, img = cam.read()
        else:
            bytes+=stream.read(16384)
            a = bytes.find('\xff\xd8')
            b = bytes.find('\xff\xd9')
            if a!=-1 and b!=-1:
                jpg = bytes[a:b+2]
                bytes= bytes[b+2:]
                canvas = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR)
                if canvas != Null:
                    ret = true
        if ret:
            camClosed = False
        else:
            camClosed = True
    else:
        camClosed = True

#Call connect function
connect()

#Main Loop
while ret:
    if allianceIsRed:
        #Ball Detection for Red
        #Convert & Threshold
        redHSVImage = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        redDetectImage = cv2.inRange(redHSVImage, redMin, redMax)
        #Morphology
        redOpenMorphImage = cv2.morphologyEx(redDetectImage, cv2.MORPH_OPEN, se)
        redCloseMorphImage = cv2.morphologyEx(redOpenMorphImage, cv2.MORPH_CLOSE, se)
        #Gaussian Blur
        redBlurredImage = cv2.GaussianBlur(redCloseMorphImage, (0, 0), gaussian_power)
        #Detect Circles
        circles = cv2.HoughCircles(redBlurredImage, cv2.cv.CV_HOUGH_GRADIENT, dpForCircles,
                                   minDist, param1=param1ForCircles, param2=param2ForCircles,
                                   minRadius=radMin, maxRadius=radMax)
    else:
        #Ball Detection for Blue<commit_msg>Modified cam. Testing IDE<commit_after>#IMPORTANT!!!
#Use Table "datatable" for testing &
#table "vision" for final robot code.
#DO NOT implement "vision" in simulated robot code!

#Imports
import cv2
import urllib
import numpy as np
from time import time
from pynetworktables import *

#Debug Flag
debug = False

#Source variables
ret = False
useInternal = True
camClosed = True
internalCam = 1
robotCamIP = 'http://10.25.76.11/mjpg/video.mjpg'
bytes=''

#Robot Variables
useLocalRobot = True
robotIP = '10.25.76.2'
localIP = '127.0.0.1'
allianceIsRed = True
#isConnected = False

#Thresholding HSV Values
redMin = np.array([160, 50, 50], np.uint8)
redMax = np.array([180, 200, 200], np.uint8)
blueMin = np.array([70, 50, 50], np.uint8)
blueMax = np.array([135, 200, 200], np.uint8)

#Function Parameters
seSize = (2, 2)        #Element for morphology
se = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, seSize)  #Create structuring element before loop
minDist = 100          #Minimum distance between circle center points
radMin = 50            #Minimum detected circle radius
radMax = 250           #Maximum detected circle radius
frameInterval = 33     #Time in ms in between frames
gaussian_power = 5     #Gaussian blur parameter
param1ForCircles = 50  #First Parameter for HoughCircles -
param2ForCircles = 25  #Second Parameter for HoughCircles -
dpForCircles = 2       #DP parameter for HoughCircles -

#Circle Parameters
centerPoint = 0        #Circle center point
radius = 0             #Circle radius
##largestRadius = 0
##largestCircle = (0, 0, 0)
prev_x = 0
prev_y = 0
prev_r = 0

#FPS Calculation
startAtFrame = 10
totalTime = 0
numberOfFrames = 0
time1 = time()
time2 = 0
fps = 0
fpsTextPos = (5, 25)
fpsTextColor = (255, 255, 255)

#Define setting variables
CV_CAP_PROP_FRAME_WIDTH = 3
CV_CAP_PROP_FRAME_HEIGHT = 4

#Window
cv2.namedWindow("Vision DS", 1)

#Extra Images
errorScreen = cv2.imread("system_down.jpg")
blank = np.zeros((400, 400, 3), np.uint8)
canvas = np.zeros((645, 645, 3), np.uint8)
canvas[245:645, :] = (145, 145, 145)

#NetworkTables Client Init
if useLocalRobot:
    NetworkTable.SetIPAddress(localIP)
else:
    NetworkTable.SetIPAddress(robotIP)
NetworkTable.SetClientMode()
NetworkTable.Initialize()

#Table Object
if useLocalRobot:
    table = NetworkTable.GetTable("datatable")
else:
    table = NetworkTable.GetTable("vision")

#Check robot color alliance
def checkAlliance():
    global allianceIsRed
    global isConnected
    try:
        allianceIsRed = table.GetBoolean("allianceRed")
    except TableKeyNotDefinedException:
        isConnected = False

#Initial Robot Connection
def connect():
    global isConnected
    isConnected = False
    while not isConnected:
        cv2.imshow("Vision DS", errorScreen)
        try:
            isConnected = table.GetBoolean("connection")
        except TableKeyNotDefinedException:
            isConnected = False
        checkAlliance()
        if cv2.waitKey(frameInterval) == 27:
            cv2.destroyAllWindows()
            exit(0)

#Callback Function
#def callBack(event, x, y, flags, param):
#    if event == cv2.cv.EVENT_LBUTTONDOWN:
#        print(img[x, y])
#        if debug:
#            print("Click")

#cv2.setMouseCallback("canvas", callBack)

#Choose source for cam
if useInternal:
    cam = cv2.VideoCapture(internalCam)
    cam.set(CV_CAP_PROP_FRAME_WIDTH, 320)
    cam.set(CV_CAP_PROP_FRAME_HEIGHT, 240)
else:
    cam = cv2.VideoCapture(robotCamIP)

#Verify cam
while camClosed:
    if cam.isOpened():
    #Read Cam
        if useLocalRobot:
            ret, img = cam.read()
        else:
            bytes += cam.read(16384)
            a = bytes.find('\xff\xd8')
            b = bytes.find('\xff\xd9')
            if a != -1 and b != -1:
                jpg = bytes[a : b+2]
                bytes= bytes[b+2 :]
                canvas = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR)
                if canvas != None:
                    ret = True
        if ret:
            camClosed = False
        else:
            camClosed = True
    else:
        camClosed = True

#Call connect function
connect()

#Main Loop
while ret:
    if allianceIsRed:
        #Ball Detection for Red
        #Convert & Threshold
        redHSVImage = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        redDetectImage = cv2.inRange(redHSVImage, redMin, redMax)
        #Morphology
        redOpenMorphImage = cv2.morphologyEx(redDetectImage, cv2.MORPH_OPEN, se)
        redCloseMorphImage = cv2.morphologyEx(redOpenMorphImage, cv2.MORPH_CLOSE, se)
        #Gaussian Blur
        redBlurredImage = cv2.GaussianBlur(redCloseMorphImage, (0, 0), gaussian_power)
        #Detect Circles
        circles = cv2.HoughCircles(redBlurredImage, cv2.cv.CV_HOUGH_GRADIENT, dpForCircles,
                                   minDist, param1=param1ForCircles, param2=param2ForCircles,
                                   minRadius=radMin, maxRadius=radMax)
    else:
        #Ball Detection for Blue
{'fraction_non_alphanumeric': 0.09791541391060332, 'fraction_numerical': 0.03848466626578473, 'mean_word_length': 2.9164050235478807, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2674021', 'n_tokens_mistral': 3545, 'n_tokens_neox': 3245, 'n_words': 856}