Пример #1
0
 def test_minmax_array_numpy(self):
     test_sound_1 = SoundPreprocessor(TestSoundPreprocessor.voicefile_1)
     result = test_sound_1.minmax_array_numpy()
     self.assertIsInstance(result, ndarray)
     self.assertIsInstance(result.shape, tuple)
     self.assertEqual(len(result.shape), 1)
     self.assertAlmostEqual(result.min(), 0.0)
     self.assertAlmostEqual(result.max(), 10.0)
Пример #2
0
 def test_create_voice_image_mean_array(self):
     test_sound_1 = TestSoundPreprocessor.create_test_sound(
         TestSoundPreprocessor.voicefile_1)
     test_sound_2 = TestSoundPreprocessor.create_test_sound(
         TestSoundPreprocessor.voicefile_2)
     result = SoundPreprocessor.create_voice_image_mean_array(
         [test_sound_1.scipy_audio, test_sound_2.scipy_audio])
     self.assertIsInstance(result, ndarray)
     self.assertIsInstance(result.shape, tuple)
     self.assertEqual(len(result.shape), 1)
     self.assertAlmostEqual(result.min(), 0.0)
     self.assertAlmostEqual(result.max(), 10.0)
    def post(self, merchant_id: int, user_id: int, text_id: int, local_filename: str, remote_filename: str):
        """
        create an ndarray out of .wav file sample and upload it to database
        :return: json message
        """
        # initialize voice array model from input data
        new_voice_array = VoiceArrayModel(merchant_id, user_id, text_id, local_filename)
        new_voice_array_exist = os.path.isfile(os.path.join(WorkingFolders.upload_folder, local_filename))
        if not new_voice_array_exist:
            return {'message': f'Filename: {local_filename} does not exists on back-end server!',
                    'status': 'error'}, 404

        try:
            # transform input wavefile
            input_sound = SoundPreprocessor(os.path.join(WorkingFolders.upload_folder, local_filename))
            # input_sound.convert_stereo_to_mono()
            input_sound.fourier_transform_audio()
            input_sound.minmax_array_numpy()

        except Exception as e:
            return e

        # save .npy file
        save(os.path.join(WorkingFolders.arrays_folder, remote_filename), input_sound.scipy_audio)

        # upload to database as binary
        result = UploadFileToDatabase.post(os.path.join(WorkingFolders.arrays_folder, remote_filename))

        # delete local array
        os.remove(os.path.join(WorkingFolders.arrays_folder, remote_filename))
        os.remove(os.path.join(WorkingFolders.upload_folder, new_voice_array.local_filename))

        return {'message': result}
Пример #4
0
    def generate_binary_voice_image(arrays_list: list, local_filename: str):
        """
        generates binary image from average values of voice arrays (per specific text) and upload it up to database
        :return: bool
        """

        # create ndarray from selected arrays
        image_ndarray = SoundPreprocessor.create_voice_image_mean_array(
            arrays_list)

        # generate image out of compiled ndarray file
        _, stored_image_buffer = ImagePreprocessor.generate_audio_image(
            image_ndarray)

        with open(local_filename, "wb") as f:
            f.write(stored_image_buffer.getbuffer())

        return local_filename
Пример #5
0
    def verify_voice(user_email: str, local_wavefile: str,
                     local_voice_image: str):

        # process input sound
        input_sound = SoundPreprocessor(local_wavefile)
        # input_sound.convert_stereo_to_mono()
        input_sound.fourier_transform_audio()
        input_sound.minmax_array_numpy()

        # generate image from processed audio and put it into buffer
        input_image_buffer: BytesIO
        _, input_image_buffer = ImagePreprocessor.generate_audio_image(
            input_sound.scipy_audio)

        # compare images
        image_preprocessor = ImagePreprocessor(input_image_buffer,
                                               local_voice_image)

        result_dhash = image_preprocessor.compare_dhash()
        result_whash = image_preprocessor.compare_whash()

        print(f"DHASH Difference: {result_dhash}")
        print(f"WHASH Difference: {result_whash}")

        # close BytesIO buffers
        input_image_buffer.close()

        if result_dhash > 600 or result_whash > 600:
            return {
                'message': 'Values over 600!',
                'dhash': str(result_dhash),
                'whash': str(result_whash),
                'status': 'error'
            }, 403
        elif result_dhash > result_whash:
            return {
                'message': 'DHASH bigger then WHASH!!',
                'dhash': str(result_dhash),
                'whash': str(result_whash),
                'status': 'error'
            }, 403
        elif result_whash / result_dhash < 1.5:
            return {
                'message': 'Proportion lower than 1.5!!',
                'dhash': str(result_dhash),
                'whash': str(result_whash),
                'status': 'error'
            }, 403
        else:
            if result_dhash < 250:
                if result_dhash <= 220:
                    return {
                        'message': f'Minimal DHASH OK: {result_dhash}',
                        'dhash': str(result_dhash),
                        'whash': str(result_whash),
                        'status': 'success'
                    }, 200
                elif result_dhash + result_whash < 650:
                    return {
                        'message': f'Sum OK: {result_dhash + result_whash}',
                        'dhash': str(result_dhash),
                        'whash': str(result_whash),
                        'status': 'success'
                    }, 200
                else:
                    return {
                        'message':
                        f'Sum too big: {result_dhash + result_whash}',
                        'dhash': str(result_dhash),
                        'whash': str(result_whash),
                        'status': 'error'
                    }, 403
            else:
                return {
                    'message': f'DHASH too big: {result_dhash}',
                    'dhash': str(result_dhash),
                    'whash': str(result_whash),
                    'status': 'error'
                }, 403
Пример #6
0
    def create_test_sound(imagefile):
        test_sound = SoundPreprocessor(imagefile)
        test_sound.fourier_transform_audio()
        test_sound.minmax_array_numpy()

        return test_sound
Пример #7
0
 def test_fourier_transform_audio(self):
     test_sound_2 = SoundPreprocessor(TestSoundPreprocessor.voicefile_2)
     result = test_sound_2.fourier_transform_audio()
     self.assertIsInstance(result, ndarray)
     self.assertIsInstance(result.shape, tuple)
     self.assertEqual(len(result.shape), 1)
Пример #8
0
 def test_return_bit_depth(self):
     test_sound_1 = SoundPreprocessor(TestSoundPreprocessor.voicefile_1)
     result = test_sound_1.return_bit_depth()
     self.assertIsInstance(result, int)
Пример #9
0
 def test_voice_2(self):
     test_sound_2 = SoundPreprocessor(TestSoundPreprocessor.voicefile_2)
     self.assertIsInstance(test_sound_2, object)