Skip to content

Instantly share code, notes, and snippets.

@farmaker47
farmaker47 / content_style_layers.py
Last active August 8, 2020 07:48
Middle layers feature extractors
# Content layer where will pull our feature maps
content_layers = ['block5_conv2']
# Style layer we are interested in
style_layers = ['block1_conv1',
'block2_conv1',
'block3_conv1',
'block4_conv1',
'block5_conv1'
]
@farmaker47
farmaker47 / init_dynamic_range_models.py
Last active August 10, 2020 05:33
Initialilzation of dynamic range models and selection of input size
private fun initializeIntrpreterForVideoQuality(value: Int) {
CONTENT_IMAGE_SIZE = value
interpreterPredict = getInterpreter(mContext, STYLE_PREDICT_INT_MODEL, false)
interpreterTransform = getInterpreter(mContext, STYLE_TRANSFER_INT_MODEL, false)
val index = interpreterTransform.getInputIndex("content_image")
interpreterTransform.resizeInput(
index,
intArrayOf(1, CONTENT_IMAGE_SIZE, CONTENT_IMAGE_SIZE, 3)
)
}
@farmaker47
farmaker47 / drop_bitmaps_until_inference_is_done.py
Created August 8, 2020 07:56
How to drop generated bitmaps from camera until inference is done
/** Process of image */
private fun processImage(bitmap: Bitmap) {
// Crop bitmap.
val croppedBitmap = cropBitmap(bitmap)
// Created scaled version of bitmap for model input.
scaledBitmap = Bitmap.createScaledBitmap(
croppedBitmap,
MODEL_WIDTH,
MODEL_HEIGHT, true
@farmaker47
farmaker47 / blend_style_with_image.py
Created August 8, 2020 07:58
How to calculate blending ration in Kotlin
// Apply style inheritance by changing values with seekbar integers
for (i in 0 until contentBottleneck[0][0][0].size) {
contentBottleneck[0][0][0][i] =
contentBottleneck[0][0][0][i] * contentBlendingRatio
}
for (i in styleBottleneck[0][0][0].indices) {
styleBottleneck[0][0][0][i] =
styleBottleneck[0][0][0][i] * (1 - contentBlendingRatio)
}
@farmaker47
farmaker47 / blending_stlyle_image_with_content_image.py
Created August 8, 2020 07:59
How to blend style image with content image
val contentArray =
ImageUtils.bitmapToByteBuffer(
contentImageBitmap,
CONTENT_IMAGE_SIZE,
CONTENT_IMAGE_SIZE
)
val inputsForStyleTransfer = if (useGPU) {
arrayOf(contentArray, styleBottleneckBlended)
} else {
private val AUDIO_SOURCE = MediaRecorder.AudioSource.VOICE_RECOGNITION
private val SAMPLE_RATE = 16000
private val CHANNEL_MASK = AudioFormat.CHANNEL_IN_MONO
private val ENCODING = AudioFormat.ENCODING_PCM_16BIT
private val BUFFER_SIZE = AudioRecord.getMinBufferSize(SAMPLE_RATE, CHANNEL_MASK, ENCODING)
private val AUDIO_FORMAT =
AudioFormat.Builder().setEncoding(ENCODING)
.setSampleRate(SAMPLE_RATE)
.setChannelMask(CHANNEL_MASK)
.build()
/**
* Start the recording process.
*/
mRecorder = AudioRecord.Builder().setAudioSource(AUDIO_SOURCE)
.setAudioFormat(AUDIO_FORMAT)
.setBufferSizeInBytes(BUFFER_SIZE)
.build()
mRecorder?.startRecording()
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) {
// load tflite file from assets folder
@Throws(IOException::class)
private fun loadModelFile(context: Context, modelFile: String): MappedByteBuffer {
val fileDescriptor = context.assets.openFd(modelFile)
val inputStream = FileInputStream(fileDescriptor.fileDescriptor)
val fileChannel = inputStream.channel
val startOffset = fileDescriptor.startOffset
val declaredLength = fileDescriptor.declaredLength
val retFile = fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength)
fileDescriptor.close()
fun execute(floatsInput: FloatArray): ArrayList<String> {
predictTime = System.currentTimeMillis()
val inputSize = floatsInput.size // ~2 seconds of sound
var outputSize = 0
when (inputSize) {
// 16.000 * 2 seconds recording
32000 -> outputSize = ceil(inputSize / 512.0).toInt()
else -> outputSize = (ceil(inputSize / 512.0) + 1).toInt()
}