def match_turn(): """ Play a match turn for a given user """ print "JSON response: %s" %request.data data = json.loads(request.data) game_id = data['game_id'] player_id = data['player_id'] match_image = data['match_image'] # base 64 encoded data for the image move_result = 0 # default it to false label = utils.create_unique_label() match_image_url = get_image_url_from_imgur(match_image) print "Imgur upload link: %s" %match_image_url player_number = which_player(game_id, player_id) # detect player 1 or 2 move_type = 'M' result = match_image_to_turn(match_image, game_id) remove_image_from_training_set(game_id) if not result: count = increment_player_missed_count(game_id, player_number) if count == settings.MAX_MISSES: update_results(game_id, player_number) move_result = 1 if result else 0 create_move(game_id, player_id, move_type, match_image_url, label, move_result) # Save next turn type as upload save_turn(game_id, 'U') status = SUCCESS if result else FAIL return jsonify({"status": status})
def upload_turn(): """ Play a upload turn for the user """ print "Json response: %s" %request.data data = json.loads(request.data) game_id = data['game_id'] player_id = data['player_id'] upload_image = data['upload_image'] move_result = 0 # default it to false label = utils.create_unique_label() upload_image_url = get_image_url_from_imgur(upload_image) print "Image from imgur upload: %s" %upload_image_url player_number = which_player(game_id, player_id) # detect player 1 or 2 move_type = 'U' iq_image_id = add_image_to_training_set(upload_image, label) update_game_with_image_upload(upload_image_url, game_id, player_id, label, iq_image_id) create_move(game_id, player_id, move_type, upload_image_url, label, move_result) # Swap the turn for the given player swap_turn(game_id, player_number) # Save match as the next turn type save_turn(game_id, 'M') return jsonify({"status": SUCCESS})
def match_image_to_turn(image, game_id): """ Match the image to the given image in the games last played image section returns True or False """ api = Api(settings.IQE_KEY, settings.IQE_SECRET) label = utils.create_unique_label() filename = "/tmp/%s" %label file = open(filename, "w") file.write(image.decode('base64')) file.close() response, qid = api.query(filename, device_id=label) print "Object created with response: %s and qid: %s" %(response, qid) game = query_db('select * from game where id=?', [game_id], one=True) expected_label = game['label'] try: # update method result = api.update(device_id=label) data = result['data'] if "results" in data: print data['results'] if isinstance(data['results'], list): actual_labels = [result['qid_data']['labels'] for result in data['results']] result = expected_label in actual_labels else: actual_labels = data['results']['qid_data']['labels'] result = expected_label == actual_labels print "Actual labels: %s" %actual_labels print "Expected labels: %s" %expected_label print "Result for the image match is: %s" %result if result: return result except Exception as ex: print "Match raised ane exception" import traceback traceback.print_exc() # result method response = api.result(qid) data = response['data'] print "Data response: %s" %response if "results" in data: if isinstance(data['results'], list): actual_labels = [result['labels'] for result in data['results']] result = expected_label in actual_labels else: actual_labels = data['results']['labels'] result = expected_label == actual_labels print "Actual labels: %s" %actual_labels print "Expected labels: %s" %expected_label print "Result for the image match is: %s" %result return result else: return False