Computer/ObjectDetection/TrafficLightDetection/TF_TrafficLightClassifier.py
def _identifyColor(self, rgbImg, threshold_percentAbundance):
        """
        gets color of traffic light. if does not seem to be a traffic light color, return none
        :param rgbImg:
        :param threshold_percentAbundance: threshold percent of minimum abundance of color in img
        :return: 'red' or 'green' or 'none' -- String values returned
        """
        hsvImg = cv2.cvtColor(rgbImg, cv2.COLOR_RGB2HSV)  # FULL indicates hue in range 0 - 255, else it is 0 - 180

        # take count
        redCount = 0
        greenCount = 0
        noneCount = 0;  # holds count for when not green or red

        for row in hsvImg:
            for pixel in row:
                hue = pixel.item(0)
                value = pixel.item(2)
                if (hue >= 150 and hue <= 180):  # red (magenta in HSV scale)
                    redCount += 1;
                elif (hue >= 0 and hue <= 60 and value >= 200):  # check if range of [0,60] and value is above 200. Explanation in OneNote
                    redCount += 1;
                elif (hue >= 60 and hue <= 120):  # green (green and cyan in HSV scale)
                    greenCount += 1;
                else:
                    noneCount += 1

        maxCount = max(max(redCount, greenCount), noneCount)
        percentAbundance = maxCount / (rgbImg.shape[0] * rgbImg.shape[1])
        # print("percent abundance: ", maxCount/(rgbImg.shape[0]*rgbImg.shape[1]))
        # print("redCount: {0}\ngreenCount: {1}\nnoneCount: {2}".format(redCount,greenCount,noneCount))
        if(percentAbundance >= threshold_percentAbundance):
            if (redCount == maxCount):
                return 'red'
            elif (greenCount == maxCount):
                return 'green'
            else:
                return 'none'
        else:
            return 'none'

        gets color of traffic light. if does not seem to be a traffic light color, return none
        :param rgbImg:
        :param threshold_percentAbundance: threshold percent of minimum abundance of color in img
        :return: 'red' or 'green' or 'none' -- String values returned
        
gets color of traffic light. if does not seem to be a traffic light color, return none
gets color of traffic light . if does not seem to be a traffic light color return none
def _identifyColor(self, rgbImg, threshold_percentAbundance):
        hsvImg = cv2.cvtColor(rgbImg, cv2.COLOR_RGB2HSV)  
        redCount = 0
        greenCount = 0
        noneCount = 0;  
        for row in hsvImg:
            for pixel in row:
                hue = pixel.item(0)
                value = pixel.item(2)
                if (hue >= 150 and hue <= 180):  
                    redCount += 1;
                elif (hue >= 0 and hue <= 60 and value >= 200):  
                    redCount += 1;
                elif (hue >= 60 and hue <= 120):  
                    greenCount += 1;
                else:
                    noneCount += 1
        maxCount = max(max(redCount, greenCount), noneCount)
        percentAbundance = maxCount / (rgbImg.shape[0] * rgbImg.shape[1])
        if(percentAbundance >= threshold_percentAbundance):
            if (redCount == maxCount):
                return 'red'
            elif (greenCount == maxCount):
                return 'green'
            else:
                return 'none'
        else:
            return 'none'
def _identifyColor ( self , rgbImg , threshold_percentAbundance ) : hsvImg = cv2 . cvtColor ( rgbImg , cv2 . COLOR_RGB2HSV ) redCount = 0 greenCount = 0 noneCount = 0 ; for row in hsvImg : for pixel in row : hue = pixel . item ( 0 ) value = pixel . item ( 2 ) if ( hue >= 150 and hue <= 180 ) : redCount += 1 ; elif ( hue >= 0 and hue <= 60 and value >= 200 ) : redCount += 1 ; elif ( hue >= 60 and hue <= 120 ) : greenCount += 1 ; else : noneCount += 1 maxCount = max ( max ( redCount , greenCount ) , noneCount ) percentAbundance = maxCount / ( rgbImg . shape [ 0 ] * rgbImg . shape [ 1 ] ) if ( percentAbundance >= threshold_percentAbundance ) : if ( redCount == maxCount ) : return 'red' elif ( greenCount == maxCount ) : return 'green' else : return 'none' else : return 'none'
gets color of traffic light.
gets color of traffic light .
"""
        gets color of traffic light. if does not seem to be a traffic light color, return none
        :param rgbImg:
        :param threshold_percentAbundance: threshold percent of minimum abundance of color in img
        :return: 'red' or 'green' or 'none' -- String values returned
        """ # FULL indicates hue in range 0 - 255, else it is 0 - 180 # take count # holds count for when not green or red # red (magenta in HSV scale) # check if range of [0,60] and value is above 200. Explanation in OneNote # green (green and cyan in HSV scale) # print("percent abundance: ", maxCount/(rgbImg.shape[0]*rgbImg.shape[1])) # print("redCount: {0}\ngreenCount: {1}\nnoneCount: {2}".format(redCount,greenCount,noneCount))
'red' or 'green' or 'none' -- String values returned
' red ' or ' green ' or ' none ' -- String values returned
self
rgbImg
None
threshold_percentAbundance
threshold percent of minimum abundance of color in img
threshold percent of minimum abundance of color in img