def select_image(): # grab a reference to the image panels global panelA, panelB # open a file chooser dialog and allow the user to select an input # image path = tkFileDialog.askopenfilename() # ensure a file path was selected if len(path) > 0: # load the image from disk, convert it to grayscale, and detect # edges in it image = t.readImage(path) shortestPath = t.solveMaze(image, (0,0), (19,19), 20, 20) img = image_enhancer.highlightPath(image, (0,0), (19,19), shortestPath) # convert the images to PIL format... image = Image.fromarray(image) edged = Image.fromarray(img) # ...and then to ImageTk format image = ImageTk.PhotoImage(image) edged = ImageTk.PhotoImage(edged) # if the panels are None, initialize them if panelA is None or panelB is None: # the first panel will store our original image panelA = Label(image=image) panelA.image = image panelA.pack(side="left", padx=10, pady=10) # while the second panel will store the edge map panelB = Label(image=edged) panelB.image = edged panelB.pack(side="right", padx=10, pady=10) # otherwise, update the image panels else: # update the pannels panelA.configure(image=image) panelB.configure(image=edged) panelA.image = image panelB.image = edged
print( '\n[ERROR] task_1a.py or image_enhancer.pyc file is missing from Task 1A folder !\n') exit() # modify the task_1a.CELL_SIZE to 40 since maze images # in task_1c_images folder have cell size of 40 pixels task_1a.CELL_SIZE = 40 print('\n============================================') print('\nFor maze0' + str(file_num) + '.jpg') try: original_binary_img = task_1a.readImage(img_file_path) height, width = original_binary_img.shape except AttributeError as attr_error: print('\n[ERROR] readImage function is not returning binary form of original image in expected format !\n') exit() # number of cells in height of maze image no_cells_height = int(height/task_1a.CELL_SIZE) # number of cells in width of maze image no_cells_width = int(width/task_1a.CELL_SIZE) initial_point = (0, 0) # start point coordinates of maze # end point coordinates of maze final_point = ((no_cells_height-1), (no_cells_width-1))
def isInPath(i, j, shortestPath, cell_img): ######################################################################### # NOTE: YOU ARE NOT ALLOWED TO MAKE ANY CHANGE TO THIS FUNCTION # # Function Name: main # Inputs: None # Outputs: None # Purpose: the function first takes 'maze00.jpg' as input and solves the maze by calling computeSum # function, it then asks the user whether to repeat the same on all maze images # present in 'task_1c_images' folder or not if __name__ != '__main__': curr_dir_path = os.getcwd() # Importing task_1a and image_enhancer script try: task_1a_dir_path = curr_dir_path + '/../../Task 1A/codes' sys.path.append(task_1a_dir_path) import task_1a import image_enhancer except Exception as e: print('\ntask_1a.py or image_enhancer.pyc file is missing from Task 1A folder !\n') exit() if __name__ == '__main__': curr_dir_path = os.getcwd() img_dir_path = curr_dir_path + '/../task_1c_images/' # path to directory of 'task_1c_images' file_num = 0 img_file_path = img_dir_path + 'maze0' + str(file_num) + '.jpg' # path to 'maze00.jpg' image file # Importing task_1a and image_enhancer script try: task_1a_dir_path = curr_dir_path + '/../../Task 1A/codes' sys.path.append(task_1a_dir_path) import task_1a import image_enhancer except Exception as e: print('\n[ERROR] task_1a.py or image_enhancer.pyc file is missing from Task 1A folder !\n') exit() # modify the task_1a.CELL_SIZE to 40 since maze images # in task_1c_images folder have cell size of 40 pixels task_1a.CELL_SIZE = 40 print('\n============================================') print('\nFor maze0' + str(file_num) + '.jpg') try: original_binary_img = task_1a.readImage(img_file_path) height, width = original_binary_img.shape except AttributeError as attr_error: print('\n[ERROR] readImage function is not returning binary form of original image in expected format !\n') exit() no_cells_height = int(height/task_1a.CELL_SIZE) # number of cells in height of maze image no_cells_width = int(width/task_1a.CELL_SIZE) # number of cells in width of maze image initial_point = (0, 0) # start point coordinates of maze final_point = ((no_cells_height-1),(no_cells_width-1)) # end point coordinates of maze try: shortestPath = task_1a.solveMaze(original_binary_img, initial_point, final_point, no_cells_height, no_cells_width) if len(shortestPath) > 2: img = image_enhancer.highlightPath(original_binary_img, initial_point, final_point, shortestPath) else: print('\n[ERROR] shortestPath returned by solveMaze function is not complete !\n') exit() except TypeError as type_err: print('\n[ERROR] solveMaze function is not returning shortest path in maze image in expected format !\n') exit() print('\nShortest Path = %s \n\nLength of Path = %d' % (shortestPath, len(shortestPath))) digits_list, digits_on_path, sum_of_digits_on_path = computeSum(img_file_path, shortestPath) print('\nDigits in the image = ', digits_list) print('\nDigits on shortest path in the image = ', digits_on_path) print('\nSum of digits on shortest path in the image = ', sum_of_digits_on_path) print('\n============================================') cv2.imshow('canvas0' + str(file_num), img) cv2.waitKey(0) cv2.destroyAllWindows() choice = input('\nWant to run your script on all maze images ? ==>> "y" or "n": ') if choice == 'y': file_count = len(os.listdir(img_dir_path)) for file_num in range(file_count): img_file_path = img_dir_path + 'maze0' + str(file_num) + '.jpg' # path to 'maze00.jpg' image file print('\n============================================') print('\nFor maze0' + str(file_num) + '.jpg') try: original_binary_img = task_1a.readImage(img_file_path) height, width = original_binary_img.shape except AttributeError as attr_error: print('\n[ERROR] readImage function is not returning binary form of original image in expected format !\n') exit() no_cells_height = int(height/task_1a.CELL_SIZE) # number of cells in height of maze image no_cells_width = int(width/task_1a.CELL_SIZE) # number of cells in width of maze image initial_point = (0, 0) # start point coordinates of maze final_point = ((no_cells_height-1),(no_cells_width-1)) # end point coordinates of maze try: shortestPath = task_1a.solveMaze(original_binary_img, initial_point, final_point, no_cells_height, no_cells_width) if len(shortestPath) > 2: img = image_enhancer.highlightPath(original_binary_img, initial_point, final_point, shortestPath) else: print('\n[ERROR] shortestPath returned by solveMaze function is not complete !\n') exit() except TypeError as type_err: print('\n[ERROR] solveMaze function is not returning shortest path in maze image in expected format !\n') exit() print('\nShortest Path = %s \n\nLength of Path = %d' % (shortestPath, len(shortestPath))) digits_list, digits_on_path, sum_of_digits_on_path = computeSum(img_file_path, shortestPath) print('\nDigits in the image = ', digits_list) print('\nDigits on shortest path in the image = ', digits_on_path) print('\nSum of digits on shortest path in the image = ', sum_of_digits_on_path) print('\n============================================') cv2.imshow('canvas0' + str(file_num), img) cv2.waitKey(0) cv2.destroyAllWindows() else: print('')
def python_client(): try: curr_dir_path = os.getcwd() img_dir_path = curr_dir_path + '/../Maze_Image/' # path to directory of 'Maze_Image' img_file_path = img_dir_path + '/Task4_maze.jpg' # path to 'Task4_maze.jpg' image file # Importing task_1a and image_enhancer script try: task_1a_dir_path = curr_dir_path sys.path.append(task_1a_dir_path) import task_1a import image_enhancer # changing the 'CELL_SIZE' variable to 40 x 40 pixels in accordance with the size in image task_1a.CELL_SIZE = 40 except Exception as e: print('\n[ERROR] task_1a.py or image_enhancer.pyc file is missing from Task 1A folder !\n') exit() # Read the image and find the shortest path try: original_binary_img = task_1a.readImage(img_file_path) height, width = original_binary_img.shape except AttributeError as attr_err: print('\n[ERROR] readImage function is not returning binary form of original image in expected format !\n') exit() no_cells_height = int(height/task_1a.CELL_SIZE) # number of cells in height of maze image no_cells_width = int(width/task_1a.CELL_SIZE) # number of cells in width of maze image try: print('\n============================================') # Create socket connection with server try: sock = connect_to_server(SERVER_ADDRESS) if sock == None: print('\n[ERROR] connect_to_server function is not returning socket object in expected format !\n') exit() else: print('\nConnecting to %s Port %s' %(SERVER_ADDRESS)) except ConnectionRefusedError as connect_err: print('\n[ERROR] the robot-server.c file is not executing, start the server first !\n') exit() # Send the 'digits_list' and 'combination_of_digits' to robot digits_list = [8, 0, 2, 2] combination_digits = [2, 2] combination_locations = [(6,9), (8,6)] combination = create_combination_dict(combination_digits, combination_locations) print('\nGiven Digits in image = %s \n\nGiven Combination of Digits with Locations = %s' % (digits_list, combination)) except Exception: pass ############# NOTE: Edit this part to complete the Task 4 implementation ############### # below data should be of type string string recv_data = '' new_shortpath = '' hosp_vac = 4 # hospital vacancie # Sending digit_list and combination send_to_server(sock, digits_list) send_to_server(sock, combination) # for esp to send @started@ recv_data = receive_from_server(sock) # starting (4,4) end ist fireszone (6,9). runs only the first time if(recv_data[0:9] == "@started@"): cord_start = (4,4) cord_stop, combination = next_fz(hosp_vac,combination) # Tells the next fire zone or exit cordinates new_shortpath = task_1a.solveMaze(original_binary_img,cord_start,cord_stop,no_cells_height,no_cells_width) send_to_server(sock,new_shortpath) while (1): recv_data = receive_from_server(sock) if(recv_data[:-1]!= "@HA reached, Task accomplished!@"): n = int(recv_data[3]) bot_vac = hosp_vac - n cord_start = recv_data[5:10] cord_start=(new_shortpath[-2][0],new_shortpath[-2][1]) cord_stop,combination = next_fz(bot_vac,combination) new_shortpath = task_1a.solveMaze(original_binary_img,cord_start,cord_stop,no_cells_height,no_cells_width) send_to_server(sock, new_shortpath) else: sock.close() sys.exit() break ########################################################################################## except KeyboardInterrupt: sys.exit()
def python_client(): try: curr_dir_path = os.getcwd() img_dir_path = curr_dir_path + '/../Maze_Image/' # path to directory of 'Maze_Image' img_file_path = img_dir_path + '/Task4_maze.jpg' # path to 'Task4_maze.jpg' image file # Importing task_1a and image_enhancer script try: task_1a_dir_path = curr_dir_path sys.path.append(task_1a_dir_path) import task_1a import image_enhancer # changing the 'CELL_SIZE' variable to 40 x 40 pixels in accordance with the size in image task_1a.CELL_SIZE = 40 except Exception as e: print( '\n[ERROR] task_1a.py or image_enhancer.pyc file is missing from Task 1A folder !\n' ) exit() # Read the image and find the shortest path try: original_binary_img = task_1a.readImage(img_file_path) height, width = original_binary_img.shape except AttributeError as attr_err: print( '\n[ERROR] readImage function is not returning binary form of original image in expected format !\n' ) exit() no_cells_height = int( height / task_1a.CELL_SIZE) # number of cells in height of maze image no_cells_width = int( width / task_1a.CELL_SIZE) # number of cells in width of maze image try: print('\n============================================') # Create socket connection with server try: sock = connect_to_server(SERVER_ADDRESS) if sock == None: print( '\n[ERROR] connect_to_server function is not returning socket object in expected format !\n' ) exit() else: print('\nConnecting to %s Port %s' % (SERVER_ADDRESS)) except ConnectionRefusedError as connect_err: print( '\n[ERROR] the robot-server.c file is not executing, start the server first !\n' ) exit() # Send the 'digits_list' and 'combination_of_digits' to robot digits_list = [8, 0, 2, 2] combination_digits = [2, 2] combination_locations = [(6, 9), (8, 6)] combination = create_combination_dict(combination_digits, combination_locations) print( '\nGiven Digits in image = %s \n\nGiven Combination of Digits with Locations = %s' % (digits_list, combination)) except Exception: pass ############# NOTE: Edit this part to complete the Task 4 implementation ############### sent = '' received = '' length = 0 for x in combination: temp1 = len(combination[x]) length = length + temp1 # sent_data_i = "hello" # sent, received = send_to_receive_from_server(sock, sent_data_i) # while received[1] != 's': # sent, received = send_to_receive_from_server(sock,sent_data_i) sent_data_f = str(digits_list) + '|' + str(combination) # send_data(sock,sent_data_f) # initial_point1 = (4, 4) # start point coordinates of maze # final_point1 = ((no_cells_height - 1), (no_cells_width - 1)) # shortestPath1 = solveMaze(original_binary_img, initial_point1, final_point1, no_cells_height, no_cells_width) initial_point1 = (4, 4) new_path1 = [[]] v = 0 for x in combination_locations: final_point1 = x np = (task_1a.solveMaze(original_binary_img, initial_point1, final_point1, no_cells_height, no_cells_width)) new_path1.append(np) v = v + 1 # send_data(sock,new_path) initial_point1 = final_point1 new_path = "|" for x in new_path1: new_path = new_path + str(x) + "|" # send_data(sock,new_path) for i in range(0, 4): send_data(sock, "okay") received = recv_data(sock) print("%s" % received) # while received[1] != '$': # received = recv_data(sock) # received=recv_data(sock) # while received[1]!='H': # received=recv_data(sock) ########################################################################################## except KeyboardInterrupt: sys.exit()