Add orx-runway (somewhat WIP and undocumented)

This commit is contained in:
Edwin Jakobs
2019-12-02 18:52:43 +01:00
parent 678ac4e72b
commit 6bfc4e274d
5 changed files with 78 additions and 0 deletions

View File

@@ -41,6 +41,7 @@ project.ext {
kotlinVersion = "1.3.50"
spekVersion = "2.0.6"
libfreenectVersion = "0.5.7-1.5.2"
gsonVersion = "2.8.6"
}
allprojects {

3
orx-runway/build.gradle Normal file
View File

@@ -0,0 +1,3 @@
dependencies {
compile "com.google.code.gson:gson:$gsonVersion"
}

View File

@@ -0,0 +1,9 @@
package org.openrndr.extra.runway
import com.google.gson.annotations.SerializedName
class CaptionRequest(val caption: String)
class CaptionResult(val result: String)
class BigBiGANQuery(@SerializedName("input_image") val inputImage: String)
class BigBiGANResult(@SerializedName("output_image") val outputImage: String)

View File

@@ -0,0 +1,64 @@
package org.openrndr.extra.runway
import com.google.gson.Gson
import org.openrndr.draw.ColorBuffer
import org.openrndr.draw.FileFormat
import java.io.ByteArrayInputStream
import java.io.File
import java.net.HttpURLConnection
import java.net.URL
import java.util.*
/**
* Construct a base64 representation of an encoded image
*/
fun ColorBuffer.toData(format: FileFormat = FileFormat.JPG): String {
val tempFile = File.createTempFile("orx-runway", null)
saveToFile(tempFile, format, async = false)
val ref = File(tempFile.absolutePath)
val imageBytes = ref.readBytes()
val encoder = Base64.getEncoder()
val base64Data = encoder.encodeToString(imageBytes)
tempFile.delete()
return "data:image/jpeg;base64,$base64Data"
}
/**
* Construct a color buffer from a base64 data string
*/
fun ColorBuffer.Companion.fromData(data: String): ColorBuffer {
val decoder = Base64.getDecoder()
val commaIndex = data.indexOf(",")
val imageData = decoder.decode(data.drop(commaIndex+1))
ByteArrayInputStream(imageData).use {
return ColorBuffer.fromStream(it)
}
}
/**
* Perform a Runway query
* @param target url string e.g. http://localhost:8000/query
*/
inline fun <Q, reified R> runwayQuery(target: String, query: Q): R {
val queryJson = Gson().toJson(query)
val connection = URL(target).openConnection() as HttpURLConnection
with(connection) {
doOutput = true
connectTimeout = 1_000
readTimeout = 200_000
requestMethod = "POST"
setRequestProperty("Content-Type", "application/json")
setRequestProperty("Accept", "application/json")
}
val outputStream = connection.outputStream
outputStream.write(queryJson.toByteArray())
outputStream.flush()
val inputStream = connection.inputStream
val responseJson = String(inputStream.readBytes())
inputStream.close()
connection.disconnect()
return Gson().fromJson(responseJson, R::class.java)
}

View File

@@ -19,6 +19,7 @@ include 'orx-camera',
'orx-osc',
'orx-palette',
'orx-poisson-fill',
'orx-runway',
'orx-shader-phrases',
'orx-temporal-blur',
'orx-kinect-common',