This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if os.environ['COLAB_TPU_ADDR']: | |
cluster_resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='') | |
tf.config.experimental_connect_to_cluster(cluster_resolver) | |
tf.tpu.experimental.initialize_tpu_system(cluster_resolver) | |
strategy = tf.distribute.TPUStrategy(cluster_resolver) | |
print('Using TPU') | |
elif tf.test.is_gpu_available(): | |
strategy = tf.distribute.MirroredStrategy() | |
print('Using GPU') | |
else: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun convertArrayToBitmap( | |
imageArray: Array<Array<Array<FloatArray>>>, | |
imageWidth: Int, | |
imageHeight: Int | |
): Pair<Bitmap, Bitmap> { | |
// Convert multidimensional array to 1D | |
val oneDFloatArray = ArrayList<Float>() | |
for (m in imageArray[0].indices) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun bitmapToByteBuffer( | |
bitmapIn: Bitmap, | |
width: Int, | |
height: Int, | |
mean: Float = 0.0f, | |
std: Float = 255.0f | |
): ByteBuffer { | |
//var bitmap = cropBitmap(bitmapIn) | |
//bitmap = scaleBitmapAndKeepRatio(bitmapIn, width, height) | |
val bitmap = Bitmap.createScaledBitmap(bitmapIn, width, height, true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun bitmapToFloatArray(bitmap: Bitmap): | |
Array<Array<Array<FloatArray>>> { | |
val width: Int = bitmap.width | |
val height: Int = bitmap.height | |
val intValues = IntArray(width * height) | |
bitmap.getPixels(intValues, 0, width, 0, 0, width, height) | |
val floatArray = Array(1) { | |
Array(3) { | |
Array(width) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interpreter = tf.lite.Interpreter('/path/to/yamnet.tflite') | |
input_details = interpreter.get_input_details() | |
waveform_input_index = input_details[0]['index'] | |
output_details = interpreter.get_output_details() | |
scores_output_index = output_details[0]['index'] | |
embeddings_output_index = output_details[1]['index'] | |
spectrogram_output_index = output_details[2]['index'] | |
# Input: 3 seconds of silence as mono 16 kHz waveform samples. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private val readAudio = Runnable { | |
var readBytes: Int | |
buffer = ShortArray(BUFFER_SIZE) | |
while (mRecording) { | |
readBytes = mRecorder!!.read(buffer, 0, BUFFER_SIZE) | |
//Higher volume of microphone | |
//https://stackoverflow.com/questions/25441166/how-to-adjust-microphone-sensitivity-while-recording-audio-in-android | |
if (readBytes > 0) { | |
for (i in 0 until readBytes) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun startRecording() { | |
mRecorder = AudioRecord.Builder().setAudioSource(AUDIO_SOURCE) | |
.setAudioFormat(AUDIO_FORMAT) | |
.setBufferSizeInBytes(BUFFER_SIZE) | |
.build() | |
done = false | |
mRecording = true | |
mRecorder?.startRecording() | |
mThread = Thread(readAudio) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun execute(floatsInput: FloatArray): Pair<ArrayList<String>, ArrayList<Float>> { | |
................................. | |
................................. | |
val listOfArrayMeanScores = arrayMeanScores.toCollection(ArrayList()) | |
val listOfMaximumValues = arrayListOf<Float>() | |
for (i in 0 until 10) { | |
val number = listOfArrayMeanScores.max() ?: 0f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun execute(floatsInput: FloatArray): Pair<ArrayList<String>, ArrayList<Float>> { | |
predictTime = System.currentTimeMillis() | |
val inputSize = floatsInput.size // ~2 seconds of sound | |
//Log.i("YAMNET_INPUT_SIZE", inputSize.toString()) | |
val inputValues = floatsInput//FloatArray(inputSize) | |
val inputs = arrayOf<Any>(inputValues) | |
val outputs = HashMap<Int, Any>() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Function for ML Binding | |
fun executeWithMLBinding( | |
contentImagePath: Bitmap, | |
styleImageName: String, | |
context: Context | |
): ModelExecutionResult { | |
try { | |
Log.i(TAG, "running models") | |
fullExecutionTime = SystemClock.uptimeMillis() |