class StageRecognizer(): def __init__(self, trained_net_path): self.net = NeuralNet() self.net.load_from_file(trained_net_path) def recognize_image(self, img): net_return = self.net.apply_over_data(extract_counter_feat(img)) stage_number = int(round(net_return)) stage = '' precision = 'strong' if stage_number == 1: stage = 'red' if abs(stage_number - 1) > .15: precision = 'weak' elif stage_number == 2: stage = 'yellow' if abs(stage_number - 1) > .15: precision = 'weak' elif stage_number == 3: stage = 'green' if abs(stage_number - 1) > .15: precision = 'weak' return stage, precision
def load_network(): if os.path.isfile("networks/XOR_Operator/XOR_Operator.obj"): global network network = NeuralNet.load_from_file( "networks/XOR_Operator/XOR_Operator.obj") else: raise ValueError("networks/XOR_Operator/XOR_Operator.obj")
def findBin(frame): image=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB) pil_im = Image.fromarray(image) #~ img1 = pil_im.resize((basewidth, height), Image.ANTIALIAS) pil_im.thumbnail((256, 256), Image.ANTIALIAS) img2 = pil_im.convert('1') #~ pixels = img2.load() pixels1 = np.asarray(img2.getdata(),dtype=np.bool) outstr = "outimg" +".bmp" img2.save(outstr) array01 = [] count = 0 Tot = 0 for item in pixels1: Tot += 1 if not item: array01.append(1) count += 1 else: array01.append(0) testitem = [] testitem.append(Instance(array01, [0])) # load a stored network configuration network = NeuralNet.load_from_file( "plastic122.pkl" ) arr = network.print_test(testitem) print('Value returned by neural network plastic: ' + str(arr[0])) network2 = NeuralNet.load_from_file( "metal122.pkl" ) arr2 = network2.print_test(testitem) print('Value returned by neural network metal: ' + str(arr2[0])) network3 = NeuralNet.load_from_file( "paper122.pkl" ) arr3 = network3.print_test(testitem) print('Value returned by neural network paper: ' + str(arr3[0])) pl = arr[0] me = arr2[0] pa = arr3[0] if((pl > pa and pl > me) or pl > 0.5 or (pa < 0.42 and me < 0.09) ): return 1 #plastic elif((me > pa and me > pl) or me > 0.13): return 3 #metal else: return 2 #paper
def findBin(frame): image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) pil_im = Image.fromarray(image) #~ img1 = pil_im.resize((basewidth, height), Image.ANTIALIAS) pil_im.thumbnail((256, 256), Image.ANTIALIAS) img2 = pil_im.convert('1') #~ pixels = img2.load() pixels1 = np.asarray(img2.getdata(), dtype=np.bool) outstr = "outimg" + ".bmp" img2.save(outstr) array01 = [] count = 0 Tot = 0 for item in pixels1: Tot += 1 if not item: array01.append(1) count += 1 else: array01.append(0) testitem = [] testitem.append(Instance(array01, [0])) # load a stored network configuration network = NeuralNet.load_from_file("plastic122.pkl") arr = network.print_test(testitem) print('Value returned by neural network plastic: ' + str(arr[0])) network2 = NeuralNet.load_from_file("metal122.pkl") arr2 = network2.print_test(testitem) print('Value returned by neural network metal: ' + str(arr2[0])) network3 = NeuralNet.load_from_file("paper122.pkl") arr3 = network3.print_test(testitem) print('Value returned by neural network paper: ' + str(arr3[0])) pl = arr[0] me = arr2[0] pa = arr3[0] if ((pl > pa and pl > me) or pl > 0.5 or (pa < 0.42 and me < 0.09)): return 1 #plastic elif ((me > pa and me > pl) or me > 0.13): return 3 #metal else: return 2 #paper
#~ # The last pair in you list describes the number of output signals #~ #~ # Optional settings #~ "weights_low" : -0.1, # Lower bound on initial weight range #~ "weights_high" : 0.1, # Upper bound on initial weight range #~ "save_trained_network" : True, # Whether to write the trained weights to disk #~ #~ "input_layer_dropout" : 0.0, # dropout fraction of the input layer #~ "hidden_layer_dropout" : 0.0, # dropout fraction in all hidden layers #~ } # initialize the neural network #~ network = NeuralNet( settings ) # load a stored network configuration network = NeuralNet.load_from_file( "network5.pkl" ) # Train the network using backpropagation #~ backpropagation( #~ network, #~ training_one, # specify the training set #~ ERROR_LIMIT = 0.001, # define an acceptable error limit #~ #max_iterations = 100, # continues until the error limit is reach if this argument is skipped #~ #~ # optional parameters #~ learning_rate = 0.03, # learning rate #~ momentum_factor = 0.4, # momentum #~ ) #~ #~ # Train the network using SciPy #~ scipyoptimize(
#~ # The last pair in you list describes the number of output signals #~ #~ # Optional settings #~ "weights_low" : -0.1, # Lower bound on initial weight range #~ "weights_high" : 0.1, # Upper bound on initial weight range #~ "save_trained_network" : True, # Whether to write the trained weights to disk #~ #~ "input_layer_dropout" : 0.0, # dropout fraction of the input layer #~ "hidden_layer_dropout" : 0.0, # dropout fraction in all hidden layers #~ } # initialize the neural network #~ network = NeuralNet( settings ) # load a stored network configuration network = NeuralNet.load_from_file("network5.pkl") # Train the network using backpropagation #~ backpropagation( #~ network, #~ training_one, # specify the training set #~ ERROR_LIMIT = 0.001, # define an acceptable error limit #~ #max_iterations = 100, # continues until the error limit is reach if this argument is skipped #~ #~ # optional parameters #~ learning_rate = 0.03, # learning rate #~ momentum_factor = 0.4, # momentum #~ ) #~ #~ # Train the network using SciPy #~ scipyoptimize(
def load_network(): if os.path.isfile("networks/XOR_Operator/XOR_Operator.obj"): global network network = NeuralNet.load_from_file("networks/XOR_Operator/XOR_Operator.obj") else: raise ValueError("networks/XOR_Operator/XOR_Operator.obj")