diff --git a/build.gradle b/build.gradle index 8b9dbccc..9ce07e52 100644 --- a/build.gradle +++ b/build.gradle @@ -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 { diff --git a/orx-runway/build.gradle b/orx-runway/build.gradle new file mode 100644 index 00000000..58aee255 --- /dev/null +++ b/orx-runway/build.gradle @@ -0,0 +1,3 @@ +dependencies { + compile "com.google.code.gson:gson:$gsonVersion" +} \ No newline at end of file diff --git a/orx-runway/src/main/kotlin/Presets.kt b/orx-runway/src/main/kotlin/Presets.kt new file mode 100644 index 00000000..34681b56 --- /dev/null +++ b/orx-runway/src/main/kotlin/Presets.kt @@ -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) diff --git a/orx-runway/src/main/kotlin/RunwayHttp.kt b/orx-runway/src/main/kotlin/RunwayHttp.kt new file mode 100644 index 00000000..a5928e2a --- /dev/null +++ b/orx-runway/src/main/kotlin/RunwayHttp.kt @@ -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 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) +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 5b066e23..f8a14c65 100644 --- a/settings.gradle +++ b/settings.gradle @@ -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',