コード例 #1
0
def pad_img(img: np.array):
    img = torch.from_numpy(img)
    row = min(int(img.shape[0] * 0.2), 100)
    col = min(int(img.shape[1] * 0.2), 100)
    ret = cv2.copyMakeBorder(img.numpy(), row, row, col, col,
                             cv2.BORDER_CONSTANT)
    return ret
コード例 #2
0
    def backchannel_prediction(self, mfcc: np.array):
        """
        Service that receives the MFCC features from the user's speech.
        It preprocess and normalize them and makes the BC prediction.

        Args:
            mfcc_features (torch.tensor): MFCC features

        Returns:
            (dict): a dictionary with the key "predicted_BC" and the value of the BC type
        """
        # class_int_mapping = {0: b'no_bc', 1: b'assessment', 2: b'continuer'}
        mfcc_features = mfcc.numpy()
        scaler = preprocessing.StandardScaler()
        mfcc_features = scaler.fit_transform(mfcc_features)
        input_splits = self.split_input_data(mfcc_features)
        prediction = self.model(input_splits).detach().numpy().argmax(axis=1)

        # Returning the majority, unless a BC appears,
        if len(set(prediction)) == 1:
            return {'predicted_BC': prediction[0]}
        elif 1 in prediction and 2 in prediction:
            ones = len(prediction[prediction == 1])
            twos = len(prediction[prediction == 2])
            return {'predicted_BC': 1 if ones > twos else 2}
        else:
            return {'predicted_BC': 1 if 1 in prediction else 2}
コード例 #3
0
ファイル: utils.py プロジェクト: gperakis/facial-similarity
def show_image(image: np.array, text: str = None, should_save: bool = False):
    """

    :param image:
    :param text:
    :param should_save:
    :return:
    """
    numpy_image = image.numpy()

    plt.axis("off")

    if text:
        plt.text(75,
                 8,
                 text,
                 style='italic',
                 fontweight='bold',
                 bbox={
                     'facecolor': 'white',
                     'alpha': 0.8,
                     'pad': 10
                 })

    plt.imshow(np.transpose(numpy_image, (1, 2, 0)))
    plt.show()
コード例 #4
0
ファイル: utils.py プロジェクト: selflein/Pose-Estimation-TF2
def extract_keypoints_from_heatmap_numpy(heatmap: np.array):
    row_idxs = tf.argmax(tf.reduce_max(heatmap, axis=1), axis=0).numpy()
    col_idxs = tf.argmax(tf.reduce_max(heatmap, axis=0), axis=0).numpy()

    heatmap = heatmap.numpy()
    values = heatmap[row_idxs, col_idxs, np.arange(heatmap.shape[-1])]
    keypoints = np.stack([col_idxs, row_idxs, values], axis=1)

    return keypoints
コード例 #5
0
ファイル: utils.py プロジェクト: zejiang-unsw/ml_drought
def _to_xarray_dataset(latlons: np.array,
                       data: np.array,
                       var_name: str = "data") -> xr.Dataset:
    """ create a 2D (single timestep) xr.Dataset """
    # convert to numpy array
    if isinstance(data, Tensor):
        try:
            data = data.detach().numpy()
        except RuntimeError as E:
            print(E)
            data.numpy()
    if isinstance(latlons, Tensor):
        latlons = latlons.numpy()

    points = [i for i in range(len(latlons))]
    _vals = data.flatten()
    dims = ["point"]
    coords = {"point": points}
    return xr.Dataset({var_name: (dims, _vals)}, coords=coords)
コード例 #6
0
def encode_image(img_array: np.array) -> str:
    """
        Encodes image data for transfer to frontend.
    """
    img_array = (img_array.numpy() * 255).astype("uint8")
    image = Image.fromarray(img_array)
    data = BytesIO()
    image.save(data, "JPEG")
    data.seek(0)
    return base64.b64encode(data.read()).decode()
コード例 #7
0
    def __transfo_image(self, tenseurImage: np.array):
        """ Applique aléatoirement une transformation à l'image pour faire du data augmentation """
        probaTransfo = random()

        if probaTransfo > 0.5:  # 50% => ne rien faire
            tenseurImage = tenseurImage.reshape(self.dims)
            if probaTransfo < 0.7:  # 20% => symétrie horizontale
                tenseurImage = tf.image.flip_left_right(tenseurImage)
            elif probaTransfo < 0.85:  # 15% => luminosité
                tenseurImage = tf.image.random_brightness(tenseurImage, 0.25)
            else:  # 15% => saturation
                tenseurImage = tf.image.random_saturation(
                    tenseurImage, 0.8, 1.5)
            tenseurImage = tenseurImage.numpy().reshape([self.dim])
        return (
            tenseurImage - 128.
        ) / 256.  # Applique une transformation pour ramener la valeur des pixels dans [-0.5 ; 0496]
コード例 #8
0
def array_to_asciiart_lines(image: np.array, scale: int = None):
    img = image if isinstance(image,
                              (np.ndarray, np.generic)) else image.numpy()
    if scale is None or scale == 0:
        isint = np.max(img) > 1
        scale = 70.0 / 256 if isint else 70.0
    else:
        scale = 70.0 / scale
    grey70 = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. "

    def lookup_grey70(i: int):
        return grey70[69 - i]

    asGreyScale = (img * scale).astype(np.uint8)
    lines = [
        ''.join(np.array(list(map(lookup_grey70, pixel))))
        for pixel in asGreyScale
    ]
    return lines
コード例 #9
0
    def downscale_image(image: np.array):
        """
        Scales down images using bicubic downsampling.
        Args:
            image: 3D or 4D tensor of preprocessed image
        """
        image_size = []
        if len(image.shape) == 3:
            image_size = [image.shape[1], image.shape[0]]
        else:
            raise ValueError("Dimension mismatch. Can work only on single image.")

        image = tf.squeeze(tf.cast(tf.clip_by_value(image, 0, 255), tf.uint8))

        lr_image = np.asarray(
            Image.fromarray(image.numpy()).resize(
                [image_size[0] // 4, image_size[1] // 4], Image.BICUBIC
            )
        )

        lr_image = tf.expand_dims(lr_image, 0)
        lr_image = tf.cast(lr_image, tf.float32)
        return lr_image
コード例 #10
0
ファイル: models.py プロジェクト: Ryusuketa/titanic
 def predict_proba(self, X: np.array) -> torch.Tensor:
     if isinstance(X, torch.Tensor):
         X = X.numpy()
     return torch.Tensor(super().predict_proba(X))