def get_cube(debug_flag=False, directory='/home/jan/rubiks-cube-NxNxN-solver'): #PRODUCTION #def get_cube(debug_flag=False, directory='/home/jan/rubiks-cube-tracker/test-data/3x3x3-random-01'): #TEST data = {} if not os.path.isdir(directory): sys.stderr.write("ERROR: directory %s does not exist\n", directory) sys.exit(1) cube_size = None for (side_index, side_name) in enumerate(('U', 'L', 'F', 'R', 'B', 'D')): filename = os.path.join(directory, "rubiks-side-%s.png" % side_name) if os.path.exists(filename): #log.info("filename %s, side_index %s, side_name %s" % (filename, side_index, side_name)) #log.info("filename %s, side_index %s, side_name %s" % (filename, side_index, side_name)) rimg = RubiksImage(side_index, side_name, debug=debug_flag) rimg.analyze_file(filename, cube_size) if cube_size is None: side_square_count = len(rimg.data.keys()) cube_size = int(sqrt(side_square_count)) data = merge_two_dicts(data, rimg.data) # log.info("cube_size %d" % cube_size) else: sys.stderr.write("ERROR: %s does not exist\n" % filename) sys.exit(1) return json.dumps(data, sort_keys=True)
def api_root(): img = request.files['image'] img.save(img.filename) try: rimg = RubiksImage() rimg.analyze_file(img.filename) color_short = "" color_rgb = [] _color_rgb = [[0, 0, 255], [0, 0, 255], [255, 255, 255], [255, 255, 0], [255, 255, 0], [255, 255, 0], [0, 128, 0], [0, 128, 0], [255, 165, 0], [255, 165, 0], [255, 165, 0], [255, 0, 0], [255, 0, 0], [255, 0, 0], [255, 0, 0], [255, 255, 255], [255, 255, 255]] color_name = [ "Blue", "Blue", "White", "Yellow", "Yellow", "Yellow", "Green", "Green", "Orange", "Orange", "Orange", "Red", "Red", "Red", "Red", "White", "White" ] colors = np.array([[50, 50, 255], [26, 82, 147], [255, 255, 255], [200, 180, 100], [237, 218, 49], [191, 152, 5], [120, 170, 120], [13, 130, 114], [255, 150, 50], [238, 125, 87], [224, 57, 16], [255, 20, 20], [211, 117, 138], [169, 53, 58], [203, 83, 93], [100, 100, 100], [173, 179, 175]]) # _color_rgb = [ [0,0,255], [0,0,255], [255,255,255], [255,255,0], [255,255,0], [0,128,0], [0,128,0], [255,165,0], [255,165,0], [255,165,0], [255,0,0], [255,0,0], [255,0,0], [255,0,0], [255,255,255]] # color_name = ["Blue", "Blue", "White", "Yellow", "Yellow", "Green", "Green", "Orange", "Orange", "Orange", "Red", "Red", "Red", "Red", "White"] # colors = np.array([[50,50,255],[26, 82, 147],[255,255,255],[200,180,100],[237, 218, 49],[120,170,120],[13, 130, 114],[255,150,50],[238, 125, 87],[224, 57, 16],[255,20,20],[211, 117, 138],[169, 53, 58],[203, 83, 93],[100,100,100]]) for key, value in rimg.data.items(): color = np.array(value) distances = np.sqrt(np.sum((colors - color)**2, axis=1)) index_of_smallest = np.where(distances == np.amin(distances)) # smallest_distance = colors[index_of_smallest] color_short = color_short + color_name[index_of_smallest[0][0]][0] color_rgb.insert(key, _color_rgb[index_of_smallest[0][0]]) return jsonify({ "status": True, "color_name": color_short, "color_rgb": color_rgb }) except: return jsonify({ "status": False, "message": "unable to detect colors, try again..." })
def tracker(directory): def convert_keys_to_int(dict_to_convert): result = {} for (key, value) in dict_to_convert.items(): result[int(key)] = value return result data = {} cube_size = None cube_size = None for (side_index, side_name) in enumerate(('U', 'L', 'F', 'R', 'B', 'D')): filename = os.path.join(directory, "rubiks-side-%s.png" % side_name) if os.path.exists(filename): #log.info("filename %s, side_index %s, side_name %s" % (filename, side_index, side_name)) #log.info("filename %s, side_index %s, side_name %s" % (filename, side_index, side_name)) rimg = RubiksImage(side_index, side_name) rimg.analyze_file(filename, cube_size) if cube_size is None: side_square_count = len(rimg.data.keys()) cube_size = int(sqrt(side_square_count)) data = merge_two_dicts(data, rimg.data) # log.info("cube_size %d" % cube_size) else: print "ERROR: %s does not exist" % filename sys.exit(1) print(json.dumps(data, sort_keys=True)) with open('out.txt', 'w') as f: print >> f, json.dumps(data, sort_keys=True)
def recognition(): get_pictures_of_sides() data = {} cube_size = None if not os.path.isdir(args.directory): print "ERROR: directory %s does not exist" % args.directory sys.exit(1) for (side_index, side_name) in enumerate(('U', 'L', 'F', 'R', 'B', 'D')): filename = os.path.join(args.directory, "rubiks-side-%s.png" % side_name) rimg = RubiksImage(side_index, side_name, debug=False) rimg.analyze_file(filename, cube_size) print side_name if cube_size is None: side_square_count = len(rimg.data.keys()) cube_size = int(sqrt(side_square_count)) data = merge_two_dicts(data, rimg.data) #print "rubiks-color-resolver.py --json --rgb " + str(data) #print(json.dumps(data, sort_keys=True)) command = "rubiks-color-resolver.py --rgb '" + str( json.dumps(data, sort_keys=True)) + "'" #+"|grep kociemba" #proc=subprocess.check_call(str(command), shell=True, stdout=subprocess.PIPE) #kubic = "DRLUUBFBRBLURRLRUBLRDDFDLFUFUFFDBRDUBRUFLLFDDBFLUBLRBD" kubic = run_command(command) print "Cubic recognized!" print kubic kubic_fix = raw_input( "Please insert the right kubic configuration if it did not recognized it correctly!" ) if len(kubic_fix) < len( "DRLUUBFBRBLURRLRUBLRDDFDLFUFUFFDBRDUBRUFLLFDDBFLUBLRBD"): print("There was no fix...") return kubic else: print("The cube is fixed a bit: ") print(kubic_fix) return kubic_fix
def CubeScan(): #return 'DRLUUBFBRBLURRLRUBLRDDFDLFUFUFFDBRDUBRUFLLFDDBFLUBLRBD' #return 'UUUUUUUUURRRRRRRRRFFFFFFFFFDDDDDDDDDLLLLLLLLLBBBBBBBBB' #return 'UUUUUUUUULLLLLLLLLFFFFFFFFFRRRRRRRRRBBBBBBBBBDDDDDDDDD' #data = json.loads(str) data = {} # F,R,B,L,U,D # (letters stand for Up, Left, Front, Right, Back, and Down) #for (side_index, side_name) in enumerate(('U', 'R', 'F', 'D', 'L', 'B')): for (side_index, side_name) in enumerate(('U', 'R', 'F', 'D', 'L', 'B')): index = 0 if side_name == 'U': index = 4 elif side_name == 'L': index = 1 elif side_name == 'F': index = 0 elif side_name == 'R': index = 2 elif side_name == 'B': index = 3 elif side_name == 'D': index = 5 filename = os.path.join(os.getcwd(), "images", "%s.png" % index) print('file = ', filename) rimg = RubiksImage(index, side_name) rimg.analyze_file(filename) data = merge_two_dicts(data, rimg.data) cube = RubiksColorSolverGeneric(3) cube.enter_scan_data(data) cube.crunch_colors() return ''.join(cube.cube_for_kociemba_strict())
args = parser.parse_args() if args.webcam is None and args.directory is None and args.filename is None: log.error("args.directory and args.filename are None") sys.exit(1) if args.debug: log.setLevel(logging.DEBUG) if args.webcam is not None: rvid = RubiksVideo(args.webcam) rvid.analyze_webcam() elif args.filename: log.setLevel(logging.DEBUG) rimg = RubiksImage(args.index, args.name, args.debug) rimg.analyze_file(args.filename) print(json.dumps(rimg.data, sort_keys=True)) else: data = {} if not os.path.isdir(args.directory): sys.stderr.write("ERROR: directory %s does not exist\n" % args.directory) sys.exit(1) cube_size = None cube_size = None for (side_index, side_name) in enumerate(("U", "L", "F", "R", "B", "D")): filename = os.path.join(args.directory,
type=str, help='Directory of images to examine') parser.add_argument('-f', '--filename', type=str, help='Image to examine') parser.add_argument('-w', '--webcam', type=int, help='webcam to use...0, 1, etc') args = parser.parse_args() if args.webcam is not None: rvid = RubiksVideo(args.webcam) rvid.analyze_webcam() elif args.filename: log.setLevel(logging.DEBUG) rimg = RubiksImage(debug=True) rimg.analyze_file(args.filename) print(json.dumps(rimg.data, sort_keys=True)) else: data = {} if not os.path.isdir(args.directory): print "ERROR: directory %s does not exist" % args.directory sys.exit(1) for (side_index, side_name) in enumerate(('U', 'L', 'F', 'R', 'B', 'D')): filename = os.path.join(args.directory, "rubiks-side-%s.png" % side_name) if os.path.exists(filename):