Skip to content

Instantly share code, notes, and snippets.

@hohonuuli
Created June 28, 2023 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hohonuuli/ba3c47593dec48060c89c6d877aef194 to your computer and use it in GitHub Desktop.
Save hohonuuli/ba3c47593dec48060c89c6d877aef194 to your computer and use it in GitHub Desktop.
Proof of concept script to analyze video using ML via beholder and pythia services
#!/usr/bin/env -S scala-cli shebang
//> using scala "3.3.0"
//> using dep "com.lihaoyi::requests:0.8.0"
//> using dep "com.lihaoyi::mainargs:0.5.0"
//> using dep "com.lihaoyi::upickle:3.1.0"
import requests.*
import mainargs.{main, arg, ParserForMethods, Flag}
import upickle.default.*
import java.nio.file.{Files, Paths}
object Main:
@main def run(
@arg(
short = 'v',
name = "videoUrl",
doc = "URL of the video to extract a frame from"
)
videoUrl: String,
beholderUrl: String = "http://singularity.shore.mbari.org:8088/capture",
xApiKey: String = "foo"): Unit =
var ok = true
var elapsedTimeMillis = 0
while (ok) {
try
val imageBytes = requestJpegImage(videoUrl, elapsedTimeMillis, beholderUrl, xApiKey)
predict(imageBytes)
elapsedTimeMillis += 1000
catch
case e: Exception =>
println("Stopping")
ok = false
}
def requestJpegImage(videoUrl: String,
elapsedTimeMillis: Int,
beholderUrl: String = "http://singularity.shore.mbari.org:8088/capture",
xApiKey: String = "foo"): Array[Byte] =
val bodyData = Map(
"videoUrl" -> videoUrl,
"elapsedTimeMillis" -> elapsedTimeMillis.toString
)
val body = write(bodyData)
val res = requests.post(beholderUrl, data = body, headers = Map(
"x-api-key" -> xApiKey
))
// Files.write(Paths.get("test.jpg"), res.contents)
println("-- Millis " + elapsedTimeMillis)
res.contents
def predict(jpeg: Array[Byte],
pythiaUrl: String = "http://perceptron.shore.mbari.org:8080/predict"): String =
val res = requests.post(pythiaUrl,
data = requests.MultiPart(requests.MultiItem("file", jpeg)))
println(res.text())
res.text()
def main(args: Array[String]): Unit = ParserForMethods(this).runOrExit(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment