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
#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) my_classifier.addModelHistogram(model_4) my_classifier.addModelHistogram(model_5) my_classifier.addModelHistogram(model_6) my_classifier.addModelHistogram(model_7) my_classifier.addModelHistogram(model_8) #my_classifier.addModelHistogram(model_9) #my_classifier.addModelHistogram(model_10) image = cv2.imread('image_2.jpg') #Load the image #Get a numpy array which contains the comparison values #between the model and the input image comparison_array = my_classifier.returnHistogramComparisonArray(image, method="intersection") #Normalisation of the array
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'