Upgrade to OPENRNDR 0.4 snapshot

This commit is contained in:
Edwin Jakobs
2021-06-22 11:08:07 +02:00
parent 579ddf9bb5
commit 9435907ef9
339 changed files with 460 additions and 497 deletions

View File

@@ -0,0 +1,98 @@
package org.openrndr.extra.runway
import com.google.gson.annotations.SerializedName
// -- AttnGAN
class AttnGANRequest(val caption: String)
class AttnGANResult(val result: String)
// -- BASNET
class BASNETRequest(val image: String)
class BASNETResult(val image: String)
// -- BDCN
class BdcnRequest(val input_image: String)
class BdcnResult(val output_image: String)
// -- BigBiGAN
class BigBiGANQuery(@SerializedName("input_image") val inputImage: String)
class BigBiGANResult(@SerializedName("output_image") val outputImage: String)
// -- DensePose
class DensePoseQuery(@SerializedName("input") val input: String)
class DensePoseResult(@SerializedName("output") val output: String)
// -- SPADE-COCO
class SpadeCocoRequest(val semantic_map: String)
class SpadeCocoResult(val output: String)
// -- GPT-2
class Gpt2Request(val prompt: String, val seed: Int = 0, @SerializedName("sequence_length") val sequenceLength: Int = 128)
class Gpt2Result(val text: String)
// -- im2txt
class Im2txtRequest(val image: String)
class Im2txtResult(val caption: String)
// -- PSENet
class PsenetRequest(@SerializedName("input_image") val inputImage: String)
class PsenetResult(val bboxes: Array<Array<Double>>)
// -- Face landmarks
class FaceLandmarksRequest(val photo: String)
class FaceLandmarksResponse(val points: List<List<Double>>, val labels: List<String>)
// -- StyleGAN
/**
* StyleGAN request
* @param z a list of 512 doubles
*/
class StyleGANRequest(val z: List<Double>, val truncation: Double = 1.0)
class StyleGANResponse(val image: String)
// -- DeOldify
class DeOldifyRequest(val image: String, val renderFactor: Int = 20)
class DeOldifyResponse(val image: String)
// -- DenseCap
class DenseCapRequest(val image: String, @SerializedName("max_detections") val maxDetections: Int = 10)
class DenseCapResponse(val bboxes: List<List<Double>>, val classes: List<String>, val scores: List<Double>)
// -- Person-Segmentation
/**
* Automatically detects people and extracts them from photos
*
* @property image
* @property threshold [0.0, 1.0]
*/
class PersonSegmentationRequest(val image: String, val threshold: Double)
class PersonSegmentationResult(val image: String)
// -- PoseNet
class PoseNetRequest(
val image: String,
@SerializedName("estimationType") val estimationType: String = "Multi Pose",
@SerializedName("maxPoseDetections") val maxPoseDetections: Int = 5,
@SerializedName("scoreThreshold") val scoreThreshold: Double = 0.25
)
class PoseNetResponse(val poses: List<List<List<Double>>>, val scores: List<Double>)
// -- U-2-Net
class U2NetRequest(val image: String)
class U2NetResult(val image: String)

View File

@@ -0,0 +1,74 @@
package org.openrndr.extra.runway
import com.google.gson.Gson
import org.openrndr.draw.ColorBuffer
import org.openrndr.draw.ImageFileFormat
import java.io.ByteArrayInputStream
import java.io.File
import java.net.*
import java.util.*
/**
* Construct a base64 representation of an encoded image
*/
fun ColorBuffer.toData(format: ImageFileFormat = ImageFileFormat.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 {
try {
val queryJson = Gson().toJson(query)
val connection = URL(target).openConnection() as HttpURLConnection
connection.doOutput = true
connection.connectTimeout = 1_000
connection.readTimeout = 200_000
connection.requestMethod = "POST"
connection.setRequestProperty("Content-Type", "application/json")
connection.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)
} catch (e: SocketTimeoutException) {
error("RunwayML connection timed out '$target'. Check if Runway and model are running.")
} catch (e: ConnectException) {
error("RunwayML connection refused '$target'. Check if Runway and model are running.")
} catch (e: UnknownHostException) {
error("Runway host not found '$target'. Check if Runway and model are running.")
}
}