def __init__(self, *args, **kwargs): super(TestHistClassifier, self).__init__(*args, **kwargs) self.classifier = HistogramColorClassifier( channels=[0, 1, 2], hist_size=[128, 128, 128], hist_range=[0, 256, 0, 256, 0, 256], hist_type='BGR') BASE_PATH = "Data/Color_Classification/model_" models = [] self.names = [ "Flash", "Batman", "Hulk", "Superman", "Captain America", "Wonder Woman", "Iron Man", "Wolverine" ] for i in range(1, 9): models.append(cv2.imread(BASE_PATH + str(i) + '.png')) for model, name in zip(models, self.names): self.classifier.addModelHistogram(model, name=name) self.test_image = cv2.imread('Data/Color_Classification/image_2.jpg')
def determineFanType(): classifier = HistogramColorClassifier(channels=[0, 1, 2], hist_size=[128, 128, 128], hist_range=[0, 256, 0, 256, 0, 256], hist_type='BGR') bif_model = cv2.imread('./data/bif/REFERENCE.jpg') classifier.addModelHistogram(bif_model) fck_model = cv2.imread('./data/fck/REFERENCE.jpg') classifier.addModelHistogram(fck_model) path = input("Please enter the path of the image you want to check: ") filecheck = Path(path) if filecheck.is_file(): resize(path) image = cv2.imread(path) result = classifier.returnHistogramComparisonArray( image, method="intersection") if result[0] > result[1]: print("Picture is most likely of a BIF fan.") elif result[0] < result[1]: print("Picture is most likely of an FCK fan.") else: print("Cant determine fan type. Try a different picture") showResultGraph(2, 'BIF', 'FCK', result) else: print( "File dosent exist. Run the file again and try a different path.")
def learn_object_from_folder(images_folder, image_extension='jpg'): '''Given a folder with images of objects on uniform background and different colours fingerprints, it associate the colours to the object name. The folder must contain images named: 1_firstobjectname.jpg, 2_secondobjectname.jpg, ... When username=None the object is associated with an empty object and not used. ''' my_classifier = HistogramColorClassifier(channels=[0, 1, 2], hist_size=[128, 128, 128], hist_range=[0, 256, 0, 256, 0, 256], hist_type='BGR') for filename in os.listdir(images_folder): if filename.endswith(image_extension): model = cv2.imread(filename) my_classifier.addModelHistogram(model) return my_classifier
def __init__(self, icub_root='/icubSim'): # Global variables self.thread_movement_detection = threading.Thread(target=None) self.acapela_account_login = '' self.acapela_application_login = '' self.acapela_application_password = '' self.acapela_service_url = '' # Deepgaze variables self.object_list = list() self.histogram_classifier = HistogramColorClassifier( channels=[0, 1, 2], hist_size=[128, 128, 128], hist_range=[0, 256, 0, 256, 0, 256], hist_type='BGR') # Init YARP yarp.Network.init() # Camera connection try: cam_w = 320 # 640 cam_h = 240 # 480 # Left camera print("[ICUB] Init: Waiting for " + icub_root + "/cam/left' ...") self.port_left_camera = yarp.Port() self.port_left_camera.open("/pyera-left-image-port") yarp.Network.connect(icub_root + "/cam/left", "/pyera-left-image-port") # right camera print("[ICUB] Init: Waiting for " + icub_root + "/cam/right' ...") self.port_right_camera = yarp.Port() self.port_right_camera.open("/pyera-right-image-port") yarp.Network.connect(icub_root + "/cam/right", "/pyera-right-image-port") # Set the numpy array to fill with the image self.img_array = np.zeros((cam_h, cam_w, 3), dtype=np.uint8) self.yarp_image = yarp.ImageRgb() self.yarp_image.resize(cam_w, cam_h) self.yarp_image.setExternal(self.img_array, self.img_array.shape[1], self.img_array.shape[0]) except BaseException, err: print("[ICUB][ERROR] connect To Camera catching error " + str(err)) return
#MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY #CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE #SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #In this example the histogram intersection algorithm is used in order #to classify eight different superheroes. The histogram intersection is #one of the simplest classifier and it uses histograms as #comparison to identify the best match between an input image and a model. import cv2 import numpy as np from matplotlib import pyplot as plt from deepgaze.color_classification import HistogramColorClassifier #Defining the classifier my_classifier = HistogramColorClassifier(channels=[0, 1, 2], hist_size=[128, 128, 128], hist_range=[0, 256, 0, 256, 0, 256], hist_type='BGR') model_1 = cv2.imread('model_1.png') #Flash model_2 = cv2.imread('model_2.png') #Batman model_3 = cv2.imread('model_3.png') #Hulk model_4 = cv2.imread('model_4.png') #Superman model_5 = cv2.imread('model_5.png') #Capt. America model_6 = cv2.imread('model_6.png') #Wonder Woman model_7 = cv2.imread('model_7.png') #Iron Man model_8 = cv2.imread('model_8.png') #Wolverine #model_9 = cv2.imread('model_9_c.png') #Thor #model_10 = cv2.imread('model_10_c.png') #Magneto my_classifier.addModelHistogram(model_1) my_classifier.addModelHistogram(model_2) my_classifier.addModelHistogram(model_3)
class TestHistClassifier(unittest.TestCase): """Test cases to ensure color classification with Histogram Color Classifier works as expected""" def __init__(self, *args, **kwargs): super(TestHistClassifier, self).__init__(*args, **kwargs) self.classifier = HistogramColorClassifier( channels=[0, 1, 2], hist_size=[128, 128, 128], hist_range=[0, 256, 0, 256, 0, 256], hist_type='BGR') BASE_PATH = "Data/Color_Classification/model_" models = [] self.names = [ "Flash", "Batman", "Hulk", "Superman", "Captain America", "Wonder Woman", "Iron Man", "Wolverine" ] for i in range(1, 9): models.append(cv2.imread(BASE_PATH + str(i) + '.png')) for model, name in zip(models, self.names): self.classifier.addModelHistogram(model, name=name) self.test_image = cv2.imread('Data/Color_Classification/image_2.jpg') def test_ComparisonArray(self): expected_comparison = [ 0.00818883, 0.55411926, 0.12405966, 0.07735263, 0.34388389, 0.12672027, 0.09870308, 0.2225694 ] #expected_name=['Flash', 'Batman', 'Hulk', 'Superman', 'Captain America', 'Wonder Woman', 'Iron Man', 'Wolverine'] comparison_array = self.classifier.returnHistogramComparisonArray( self.test_image, method="intersection") assert np.allclose(comparison_array, expected_comparison) def test_Names(self): expected_name = [ 'Flash', 'Batman', 'Hulk', 'Superman', 'Captain America', 'Wonder Woman', 'Iron Man', 'Wolverine' ] assert self.classifier.returnNameList() == expected_name def test_BestMatchIndex(self): assert self.classifier.returnBestMatchIndex(self.test_image) == 1 def test_BestMatchName(self): assert self.classifier.returnBestMatchName(self.test_image) == 'Batman' def test_Cache(self): expected_name = [ 'Flash', 'Batman', 'Hulk', 'Superman', 'Captain America', 'Wonder Woman', 'Iron Man', 'Wolverine' ] self.classifier.returnHistogramComparisonArray(self.test_image, method="intersection") assert self.classifier.returnNameList() == expected_name assert self.classifier.returnBestMatchIndex() == 1 assert self.classifier.returnBestMatchName() == 'Batman'