Example #1
0
def make_srcnn_rgb_dataset_based_on_cifar10():
    print('making SRCNN-RGB dataset using CIFAR-10...')
    (Y_train, _), (Y_test, _) = get_dataset_part(cifar10.load_data(),
                                                 train_part=0.2)

    from image_handler import get_image, get_image_data, zoom_out_image, zoom_up_image

    # X_train
    print('making X_train list...')
    X_train = []
    for item in Y_train:
        image_data = item.tolist()
        image = get_image(image_data, mode='RGB')
        zoomed_out_image = zoom_out_image(image, times=2)
        zoomed_up_image = zoom_up_image(zoomed_out_image, times=2)
        zoomed_up_image_data = get_image_data(zoomed_up_image)
        X_train.append(zoomed_up_image_data)

    # X_test
    print('making X_test list...')
    X_test = []
    for item in Y_test:
        image_data = item.tolist()
        image = get_image(image_data, mode='RGB')
        zoomed_out_image = zoom_out_image(image, times=2)
        zoomed_up_image = zoom_up_image(zoomed_out_image, times=2)
        zoomed_up_image_data = get_image_data(zoomed_up_image)
        X_test.append(zoomed_up_image_data)

    dtype = 'uint8'
    dataset = (np.array(X_train, dtype=dtype), np.array(Y_train, dtype=dtype)), \
              (np.array(X_test, dtype=dtype), np.array(Y_test, dtype=dtype))
    print('saving dataset to srcnn-rgb-cifar10-dataset.npz...')
    np.savez('datasets/srcnn-rgb-cifar10-dataset.npz', dataset)
Example #2
0
def show_mnist_example(count=1):
    from keras.datasets import mnist
    (X_train, y_train), (X_test, y_test) = mnist.load_data()

    from image_handler import get_image
    for i in range(count):
        get_image(X_train[i].tolist())
Example #3
0
    def create_surface(self):
        """Creates a Surface depicting the Tweet."""
        # TODO: Needs refactoring (including the helper methods).
        header = []

        profile_pic = image_handler.get_image(self.profile_image_url)
        size = config2.config['profile_size']
        profile_pic = pygame.transform.scale(profile_pic, (size, size))
        header.append(profile_pic)

        Tweet.font.set_underline(True)
        name = Tweet.font.render('@' + self.screen_name, 1, BLACK)
        Tweet.font.set_underline(False)
        header.append(name)


        text = Tweet.create_body(self.lines)
        content = []
        content.append(make_header(header, WHITE, text.get_rect().width))
        content.append(text)

        content.extend([image_handler.get_image(img) for img in self.imgs])
        
        popularity_bar = self.create_popularity_surface()
        content.append(popularity_bar)
        
        content_surf = make_column(content, WHITE)
        content_surf = Tweet.create_bubble(content_surf)#surrounds the content in the bubble
        surfs = []
        surfs.append(content_surf) 

        tweet = make_column(surfs, BLACK)
        return content_surf
Example #4
0
def get_srcnn_rgb_cifar10_20000_dataset_interpolation_metrics():
    from image_handler import make_interpolated_image_from, get_images_difference_metrics, get_image

    (X_train, Y_train), (X_test,
                         Y_test) = get_srcnn_rgb_cifar10_20000_dataset()

    for image_data in Y_train:
        get_image()
Example #5
0
def show_dataset_example(dataset, count=1):
    (X_train, Y_train), (X_test, Y_test) = dataset
    from image_handler import get_image
    for i in range(count):
        # get_image(X_train[i], mode='RGB').show()
        # get_image(Y_train[i], mode='RGB').show()
        get_image(X_test[i], mode='RGB').show()
        get_image(Y_test[i], mode='RGB').show()
Example #6
0
def show_cifar100_example(count=1):
    (X_train, y_train), (X_test, y_test) = cifar100.load_data()

    # num_train, height, width, depth = X_train.shape
    print(X_train.shape)
    # print(X_train[0].tolist())

    from image_handler import get_image
    for i in range(count):
        get_image(X_train[i].tolist(), mode='RGB').show()
Example #7
0
def make_pasadena_dataset():
    print('making PASADENA dataset...')

    from image_handler import get_image_data, get_image, zoom_out_image, zoom_up_image

    path = 'saved_images/Pasadena Dataset/'
    filename_prefix = path + 'dcp_24'
    size = 10
    result = []

    for i in range(size):
        filename = filename_prefix + str(12 + i) + '.jpg'
        image = Image.open(filename)
        print('Image', filename.rpartition('/')[2], 'opened')
        image_data = get_image_data(image)
        result.append(image_data)

    print('making Y_train and Y_test...')
    train_size = 7
    Y_train, Y_test = result[:train_size], result[train_size:]

    # X_train
    print('making X_train list...')
    X_train = []
    for item in Y_train:
        image_data = item  # .tolist()
        image = get_image(image_data, mode='RGB')
        zoomed_out_image = zoom_out_image(image, times=2)
        zoomed_up_image = zoom_up_image(zoomed_out_image, times=2)
        zoomed_up_image_data = get_image_data(zoomed_up_image)
        X_train.append(zoomed_up_image_data)

    # X_test
    print('making X_test list...')
    X_test = []
    for item in Y_test:
        image_data = item  # .tolist()
        image = get_image(image_data, mode='RGB')
        zoomed_out_image = zoom_out_image(image, times=2)
        zoomed_up_image = zoom_up_image(zoomed_out_image, times=2)
        zoomed_up_image_data = get_image_data(zoomed_up_image)
        X_test.append(zoomed_up_image_data)

    dtype = 'uint8'
    dataset = (np.array(X_train, dtype=dtype), np.array(Y_train, dtype=dtype)), \
              (np.array(X_test, dtype=dtype), np.array(Y_test, dtype=dtype))
    print('saving dataset to pasadena-dataset.npz...')
    np.savez_compressed('datasets/pasadena-dataset.npz', dataset)
    print('pasadena-dataset.npz saved')
Example #8
0
def make_hundred_dataset():
    print('making HUNDRED dataset...')

    from image_handler import get_image_data, get_image, zoom_out_image, zoom_up_image

    path = 'images/Hundred Dataset/images/'
    size = 53
    result = []

    for i in range(size):
        filename = path + str(1 + i) + '.png'
        image = Image.open(filename)
        print('Image', filename.rpartition('/')[2], 'opened')
        image_data = get_image_data(image)
        result.append(image_data)

    print('making Y_train and Y_test...')
    train_size = 40
    Y_train, Y_test = result[:train_size], result[train_size:]

    # X_train
    print('making X_train list...')
    X_train = []
    for item in Y_train:
        image_data = item  # .tolist()
        image = get_image(image_data, mode='RGB')
        zoomed_out_image = zoom_out_image(image, times=2)
        zoomed_up_image = zoom_up_image(zoomed_out_image, times=2)
        zoomed_up_image_data = get_image_data(zoomed_up_image)
        X_train.append(zoomed_up_image_data)

    # X_test
    print('making X_test list...')
    X_test = []
    for item in Y_test:
        image_data = item  # .tolist()
        image = get_image(image_data, mode='RGB')
        zoomed_out_image = zoom_out_image(image, times=2)
        zoomed_up_image = zoom_up_image(zoomed_out_image, times=2)
        zoomed_up_image_data = get_image_data(zoomed_up_image)
        X_test.append(zoomed_up_image_data)

    dtype = 'uint8'
    dataset = (np.array(X_train, dtype=dtype), np.array(Y_train, dtype=dtype)), \
              (np.array(X_test, dtype=dtype), np.array(Y_test, dtype=dtype))
    print('saving dataset to hundred-dataset.npz...')
    np.savez('datasets/hundred-dataset.npz', dataset)  # _compressed
    print('hundred-dataset.npz saved')
Example #9
0
def handle_mnist():
    from image_handler import get_image, get_image_data, zoom_out_image
    from keras.datasets import mnist
    (X_train, y_train), (X_test, y_test) = mnist.load_data()

    dataset_size = 60000

    dataset_X, dataset_Y = [], []
    for i, X_train_item in enumerate(X_train[:dataset_size]):
        image_data = X_train_item.tolist()
        image = get_image(image_data, mode='L')
        zoomed_out_image = zoom_out_image(image, times=2)
        zoomed_out_image_data = get_image_data(zoomed_out_image)

        dataset_X.append(zoomed_out_image_data)
        dataset_Y.append(image_data)

        print('image ' + str(i) + ', height : ' + str(len(image_data)),
              'width : ' + str(len(image_data[0])),
              sep=', ')
        print('zoomed_out_image ' + str(i) + ', height : ' +
              str(len(zoomed_out_image_data)),
              'width : ' + str(len(zoomed_out_image_data[0])),
              sep=', ')
        # print('image_data :', image_data)
        # print('zoomed_out_image_data :', zoomed_out_image_data)
        # image.show()
        # zoomed_out_image.show()

    dataset = (dataset_X, dataset_Y)

    import pickle
    mnist_dataset_file = open('datasets/mnist-dataset.pkl', 'wb')
    pickle.dump(dataset, mnist_dataset_file)
    mnist_dataset_file.close()
Example #10
0
 def create_surface(self):
     """Creates a Surface depicting the Tweet."""
     # TODO: Needs refactoring (including the helper methods).
     surfs = []
     image = image_handler.get_image(self.profile_image_url)
     profile_pic = pygame.transform.scale(image, (70, 70))
     surfs.append(profile_pic)
     name = Tweet.font.render('@' + self.screen_name, 1, BLACK)
     surfs.append(name)
     text = Tweet.create_body(self.lines)
     content = [text]
     content.extend([image_handler.get_image(img) for img in self.imgs])
     popularity_bar = self.create_popularity_surface()
     content.append(popularity_bar)
     content_surf = make_column(content, WHITE)
     surfs.append(Tweet.create_bubble(content_surf))
     tweet = make_column(surfs, TWITTER_BLUE)
     return tweet
Example #11
0
def show_srcnn_mnist_dataset_example(count=1):
    from image_handler import get_image
    (X_train, Y_train), (X_test, Y_test) = get_srcnn_mnist_dataset()

    for i in range(count):
        get_image(X_train[i], mode='L').show()
        get_image(Y_train[i], mode='L').show()

    for i in range(count):
        get_image(X_test[i], mode='L').show()
        get_image(Y_test[i], mode='L').show()
Example #12
0
def make_srcnn_dataset_based_on_mnist():
    print('making SRCNN dataset using MNIST...')

    from image_handler import get_image, get_image_data, zoom_out_image, zoom_up_image

    (Y_train, _), (Y_test, _) = mnist.load_data()  # 60000, 10000

    # making X_train list
    print('making X_train list...')
    X_train = []
    for item in Y_train:
        image_data = item.tolist()
        image = get_image(image_data, mode='L')
        zoomed_out_image = zoom_out_image(image, times=2)
        zoomed_up_image = zoom_up_image(zoomed_out_image, times=2)
        zoomed_up_image_data = get_image_data(zoomed_up_image)
        X_train.append(zoomed_up_image_data)

    # making X_test list
    print('making X_test list...')
    X_test = []
    for item in Y_test:
        image_data = item.tolist()
        image = get_image(image_data, mode='L')
        zoomed_out_image = zoom_out_image(image, times=2)
        zoomed_up_image = zoom_up_image(zoomed_out_image, times=2)
        zoomed_up_image_data = get_image_data(zoomed_up_image)
        X_test.append(zoomed_up_image_data)

    # X_train, X_test = [], []
    # for X, Y in [(X_train, Y_train), (X_test, Y_test)]:
    #     for item in Y:
    #         image_data = item.tolist()
    #         image = get_image(image_data, mode='L')
    #         zoomed_out_image = zoom_out_image(image, times=2)
    #         zoomed_up_image = zoom_up_image(zoomed_out_image, times=2)
    #         zoomed_up_image_data = get_image_data(zoomed_up_image)
    #         X.append(zoomed_up_image_data)

    dtype = 'uint8'
    dataset = (np.array(X_train, dtype=dtype), np.array(Y_train, dtype=dtype)), \
              (np.array(X_test, dtype=dtype), np.array(Y_test, dtype=dtype))
    print('saving dataset to srcnn-mnist-dataset.npz...')
    np.savez('datasets/srcnn-mnist-dataset.npz', dataset)
Example #13
0
def show_srcnn_rgb_cifar10_20000_dataset_example(count=1):
    (X_train, Y_train), (X_test,
                         Y_test) = get_srcnn_rgb_cifar10_20000_dataset()
    from image_handler import get_image
    for i in range(count):
        # get_image(X_train[i], mode='RGB').show()
        # get_image(Y_train[i], mode='RGB').show()
        # get_image(X_test[i], mode='RGB').show()
        get_image(Y_test[i], mode='RGB').show()

        get_image(X_train[i],
                  mode='RGB').save('saved_images/cifar10_20000_X_train[0].png')
        get_image(Y_train[i],
                  mode='RGB').save('saved_images/cifar10_20000_Y_train[0].png')
Example #14
0
def run(image_data):
    print('neural network running...')

    # preparing image_data
    test = np.array([image_data])
    test = test.astype('float32')
    test /= 255  # Normalise data to [0, 1] range
    test = test.reshape(1, 3, 32, 32)

    # loading model
    # path_to_model = 'models/srcnn-cifar10-20000-9_3_1_5-64_32_32-20epochs-he_uniform-custom_relu.h5'
    path_to_model = ''
    model = keras.models.load_model(path_to_model,
                                    custom_objects={
                                        'psnr_3_channels': psnr_3_channels,
                                        'custom_relu': custom_relu,
                                    })  # , custom_objects={'psnr_L': psnr_L})
    print(model.summary())

    batch_size = 64
    prediction = model.predict(test, batch_size=batch_size, verbose=1)

    from keras.backend import clear_session
    clear_session()

    prediction.resize((1, 32, 32, 3))
    prediction = np.rint(prediction * 255).astype('uint8')
    print_ndarray_info(
        prediction,
        verbose=True)  # reshape((3, 4)) => a ; resize((2,6)) => on place
    prediction_image_data = prediction[0]
    print_ndarray_info(prediction_image_data, verbose=True)

    from image_handler import get_image
    prediction_image = get_image(prediction_image_data, mode='RGB')
    # prediction_image.show()

    return prediction_image_data
Example #15
0
def show_pasadena_dataset_example(count=1):
    (X_train, Y_train), (X_test, Y_test) = get_pasadena_dataset()
    from image_handler import get_image
    for i in range(count):
        get_image(X_train[i], mode='RGB').show()
        get_image(Y_train[i], mode='RGB').show()
Example #16
0
def notifyGameEnd(summoner, gameId, previouslyFailedCount=0):
    if gameId is None:
        return

    gameId = str(gameId)  # api v5 fix
    url = api_calls.BASE_API_URL_V5 + \
        api_calls.MATCH_API_URL.format(matchId=f"NA1_{gameId}")
    response = call_api(url)
    if response and response.status_code == 200:
        json_data = response.json()
        participants = json_data["info"]["participants"]
        participantID = None
        participant = None
        totalKills = 0
        totalDamage = 0
        damageRank = 1
        team = None

        for p in participants:
            if p["summonerId"] == summoner.SummonerDTO["id"]:
                summonerId = p["summonerId"]
                participantID = p["participantId"]
                participant = p
                team = p["teamId"]
                break
        if participantID is None:
            logging.info(
                f"Could not find {summoner.SummonerDTO['name']} in participant list"
            )

        # participant now has the summoner stats for the game

        # personal stats
        champion = data.getChampionName(participant["championId"])
        kills = int(participant["kills"])
        deaths = int(participant["deaths"])
        assists = int(participant["assists"])
        damage = int(participant["totalDamageDealtToChampions"])

        # cumulative team stats
        for p in participants:
            if p["teamId"] == team:
                totalKills += int(p["kills"])
                p_dmg = int(p["totalDamageDealtToChampions"])
                totalDamage += p_dmg
                if p["participantId"] != participantID and p_dmg > damage:
                    damageRank += 1

        # kp
        if totalKills > 0:
            kp = str((100 * (kills + assists)) // totalKills)
        else:
            kp = "0"

        # damage share
        if totalDamage > 0:
            damageShare = str((100 * damage) // totalDamage)
        else:
            damageShare = 0
            damageRank = 5

        # Game End Stats Message
        win = participant["win"]
        result = "won" if win else "lost"
        dmg_text = ""
        if damageRank == 1:
            dmg_text = "highest in the team"
        elif damageRank == 5:
            dmg_text = "lowest in the team"
        else:
            pos = "2nd" if damageRank == 2 else "3rd" if damageRank == 3 else "4th" if damageRank == 4 else "NaN"
            dmg_text = pos + " highest in the team"
        msg = "GAME END: {} {} a game as {}. He went {}/{}/{} with a kp of {}% and dealt {:,} damage ({}% damage share, {})"
        msg = msg.format(summoner.getName(), result, champion, kills, deaths,
                         assists, kp, damage, damageShare, dmg_text)

        # Rank Change
        if summoner.CurrentRank != None:
            promos = None
            url = api_calls.BASE_API_URL + \
                api_calls.LEAGUE_API_URL.format(
                    encryptedSummonerId=summoner.SummonerDTO["id"])
            response = call_api(url)
            if response and response.status_code == 200:
                leagueEntrySet = response.json()
                rank = {}
                for entry in leagueEntrySet:
                    if (entry["queueType"] != consts.RANKED_SOLO_QUEUE_TYPE):
                        continue
                    rank["tier"] = entry["tier"]
                    rank["division"] = entry["rank"]
                    rank["lp"] = entry["leaguePoints"]
                    if "miniSeries" in entry:
                        promos = entry["miniSeries"]
                    break
                if rank["tier"] == summoner.CurrentRank["tier"] and rank[
                        "division"] == summoner.CurrentRank["division"]:
                    prevLp = int(summoner.CurrentRank["lp"])
                    newLp = int(rank["lp"])
                    lpDiff = 0
                    if win:
                        lpDiff = newLp - prevLp
                    else:
                        lpDiff = prevLp - newLp
                    result = "gained" if win else "lost"
                    if promos is None or lpDiff != 0:
                        msg += "\n" + summoner.SummonerDTO[
                            "name"] + " " + result + " " + str(
                                lpDiff
                            ) + "lp for this game. He is currently " + str(
                                rank["tier"]) + " " + str(
                                    rank["division"]) + " " + str(
                                        rank["lp"]) + "lp."
                    if promos is not None:
                        wins = str(promos["wins"])
                        losses = str(promos["losses"])
                        nextRankIndex = consts.TIERS.index(
                            summoner.CurrentRank["tier"]) + 1
                        nextRank = consts.TIERS[nextRankIndex]
                        msg += "\n" + summoner.SummonerDTO["name"] + " is currently " + \
                            wins + "-" + losses + " in promos to " + nextRank + "."

        # Match History Uri:
        postMsg = None

        matchUrls = [
            ("Mobalytics",
             api_calls.MOBALYTICS_MATCH_URL.format(
                 summonerName=urllib.parse.quote(summoner.SummonerDTO["name"]),
                 gameId=gameId)),
            ("LeagueOfGraphs", api_calls.LOG_MATCH_URL.format(gameId=gameId))
        ]
        urlListText = " | ".join([
            "[{text}](<{url}>)".format(text=item[0], url=item[1])
            for item in matchUrls
        ])
        postMsg = "View Game Details Here: " + urlListText

        color = utils.ColorCodes.GREEN if win else utils.ColorCodes.RED

        image_handler.get_image(gameId)
        image_handler.crop_image_post_game(gameId)

        discord_bot.SendMessage(msg,
                                color,
                                postMsg,
                                file=discord.File(f"cropped_{gameId}.png"))

        image_handler.remove_images(gameId)

        # Special Naeem section xD
        if summoner.getName(
        ) == "NaeemP" and not win and champion != "Nocturne":
            discord_bot.SendMessage("SADGE: No Nocturne no LP. ",
                                    file=discord.File("naeem_lose.jpg"))

    else:
        msg = "Try #" + str(previouslyFailedCount + 1) + ": Error Obtaining Game Info for match# " + \
            str(gameId) + " (Game by " + summoner.SummonerDTO["name"] + ")"

        if previouslyFailedCount > 3:
            msg += " Stopping trying, match query failed. Look into it."
        else:
            failed_game_end_ids.add(
                (gameId, summoner, previouslyFailedCount + 1))

        logging.info(response)  # print response to see what is going on
        logging.info(msg)

        discord_bot.SendMessage(msg)