Working CLI utility
This commit is contained in:
parent
ddca2198c5
commit
4893b8243c
|
@ -0,0 +1,9 @@
|
||||||
|
.DS_Store
|
||||||
|
.idea
|
||||||
|
*.log
|
||||||
|
tmp/
|
||||||
|
|
||||||
|
.cpcache/
|
||||||
|
.nrepl-port
|
||||||
|
target/
|
||||||
|
result
|
|
@ -267,6 +267,16 @@
|
||||||
"mvn-repo": "https://repo1.maven.org/maven2/",
|
"mvn-repo": "https://repo1.maven.org/maven2/",
|
||||||
"hash": "sha256-bY3hTDrIdXYMX/kJVi/5hzB3AxxquTnxyxOeFp/pB1g="
|
"hash": "sha256-bY3hTDrIdXYMX/kJVi/5hzB3AxxquTnxyxOeFp/pB1g="
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"mvn-path": "org/clojure/tools.cli/1.0.214/tools.cli-1.0.214.jar",
|
||||||
|
"mvn-repo": "https://repo1.maven.org/maven2/",
|
||||||
|
"hash": "sha256-7h4iTowAv6wht2tSC3D5WF0nj4al16wMelXpGt8PTqM="
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"mvn-path": "org/clojure/tools.cli/1.0.214/tools.cli-1.0.214.pom",
|
||||||
|
"mvn-repo": "https://repo1.maven.org/maven2/",
|
||||||
|
"hash": "sha256-ulk+rq6K6NhkVBgYjY1707XNndYKY5w04fBuGyqDpIQ="
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"mvn-path": "potemkin/potemkin/0.4.5/potemkin-0.4.5.jar",
|
"mvn-path": "potemkin/potemkin/0.4.5/potemkin-0.4.5.jar",
|
||||||
"mvn-repo": "https://repo.clojars.org/",
|
"mvn-repo": "https://repo.clojars.org/",
|
||||||
|
|
1
deps.edn
1
deps.edn
|
@ -3,6 +3,7 @@
|
||||||
:deps {
|
:deps {
|
||||||
org.clojure/clojure { :mvn/version "1.11.1" }
|
org.clojure/clojure { :mvn/version "1.11.1" }
|
||||||
org.clojure/data.json { :mvn/version "2.4.0" }
|
org.clojure/data.json { :mvn/version "2.4.0" }
|
||||||
|
org.clojure/tools.cli { :mvn/version "1.0.214" }
|
||||||
clj-http/clj-http { :mvn/version "3.12.3" }
|
clj-http/clj-http { :mvn/version "3.12.3" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,14 +16,14 @@
|
||||||
default = objectifier-client;
|
default = objectifier-client;
|
||||||
objectifier-client = helpers.packages."${system}".mkClojureBin {
|
objectifier-client = helpers.packages."${system}".mkClojureBin {
|
||||||
name = "org.fudo/objectifier-client";
|
name = "org.fudo/objectifier-client";
|
||||||
primary-namespace = "objectifier-client.cli";
|
primaryNamespace = "objectifier-client.cli";
|
||||||
src = ./.;
|
src = ./.;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
devShells = let pkgs = import nixpkgs { inherit system; };
|
devShells = let pkgs = import nixpkgs { inherit system; };
|
||||||
in rec {
|
in rec {
|
||||||
default = update-deps;
|
default = objectifier-client;
|
||||||
objectifier-client = pkgs.mkShell {
|
objectifier-client = pkgs.mkShell {
|
||||||
buildInputs = [ self.packages."${system}".objectifier-client ];
|
buildInputs = [ self.packages."${system}".objectifier-client ];
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,7 +2,8 @@
|
||||||
(:require [objectifier-client.core :as obj]
|
(:require [objectifier-client.core :as obj]
|
||||||
[clojure.java.io :as io]
|
[clojure.java.io :as io]
|
||||||
[clojure.tools.cli :as cli]
|
[clojure.tools.cli :as cli]
|
||||||
[clojure.string :as str]))
|
[clojure.string :as str])
|
||||||
|
(:gen-class))
|
||||||
|
|
||||||
(defn- read-file-bytes [filename]
|
(defn- read-file-bytes [filename]
|
||||||
(with-open [in (java.io.FileInputStream. filename)
|
(with-open [in (java.io.FileInputStream. filename)
|
||||||
|
@ -41,20 +42,20 @@
|
||||||
prob)))))
|
prob)))))
|
||||||
|
|
||||||
(defn- display-labels [client filenames]
|
(defn- display-labels [client filenames]
|
||||||
(distinct
|
(let [labels (distinct
|
||||||
(mapcat (fn [filename]
|
(mapcat (fn [filename] (obj/get-labels! client (read-file-bytes filename)))
|
||||||
(obj/get-labels! client (read-file-bytes filename)))
|
filenames))]
|
||||||
filenames)))
|
(doseq [label labels] (println label))))
|
||||||
|
|
||||||
(defn -main [& args]
|
(defn -main [& args]
|
||||||
(let [{:keys [options arguments summary errors]}]
|
(let [{:keys [options arguments summary errors]} (cli/parse-opts args cli-opts)]
|
||||||
(when (seq errors)
|
(when (seq errors)
|
||||||
(msg-quit 1 (usage summary errors)))
|
(msg-quit 1 (usage summary errors)))
|
||||||
(when (:help options)
|
(when (:help options)
|
||||||
(msg-quit 0 (usage summary)))
|
(msg-quit 0 (usage summary)))
|
||||||
(when (empty? (arguments))
|
(when (empty? arguments)
|
||||||
(msg-quit 0 (usage summary ["No files provided to scan."])))
|
(msg-quit 0 (usage summary ["No files provided to scan."])))
|
||||||
(let [client (obj/define-connection "http" (:server options) (:port options))]
|
(let [client (obj/define-connection :host (:server options) :port (:port options))]
|
||||||
(if (:labels options)
|
(if (:labels options)
|
||||||
(display-labels client arguments)
|
(display-labels client arguments)
|
||||||
(doseq [file arguments]
|
(doseq [file arguments]
|
||||||
|
|
|
@ -1,16 +1,17 @@
|
||||||
(ns objectifier-client.core
|
(ns objectifier-client.core
|
||||||
(:require [clj-http.client :as client]
|
(:require [clj-http.client :as client]
|
||||||
[clojure.data.json :as json]
|
[clojure.data.json :as json]
|
||||||
|
[clojure.string :as str]
|
||||||
[slingshot.slingshot :refer [throw+]])
|
[slingshot.slingshot :refer [throw+]])
|
||||||
(:import java.net.URL
|
(:import java.net.URL
|
||||||
java.io.ByteArrayInputStream))
|
java.io.ByteArrayInputStream))
|
||||||
|
|
||||||
(defprotocol IObjectifierClient
|
(defprotocol IObjectifierClient
|
||||||
(get! [_ image-data])
|
(get! [_ image-data])
|
||||||
(get-labels! [_ image-data])
|
(get-labels! [_ image-data])
|
||||||
(get-detections! [_ image-data])
|
(get-detections! [_ image-data])
|
||||||
(get-highlights! [_ image-data])
|
(get-highlights! [_ image-data])
|
||||||
(get-probabilites! [_ image-data]))
|
(get-probabilities! [_ image-data]))
|
||||||
|
|
||||||
(defn- url->string [url] (.toExternalForm url))
|
(defn- url->string [url] (.toExternalForm url))
|
||||||
|
|
||||||
|
@ -23,9 +24,15 @@
|
||||||
{:multipart [{:name "image"
|
{:multipart [{:name "image"
|
||||||
:content input-stream}]})))
|
:content input-stream}]})))
|
||||||
|
|
||||||
|
(defn- to-keyword [str]
|
||||||
|
(-> str
|
||||||
|
(str/lower-case)
|
||||||
|
(str/replace #" " "-")
|
||||||
|
(keyword)))
|
||||||
|
|
||||||
(defn- process-response [resp]
|
(defn- process-response [resp]
|
||||||
(if (<= 200 (:status resp) 299)
|
(if (<= 200 (:status resp) 299)
|
||||||
(-> resp :body (json/read-str :key-fn keyword))
|
(-> resp :body (json/read-str :key-fn to-keyword))
|
||||||
(throw+ {:type ::http-error
|
(throw+ {:type ::http-error
|
||||||
:status (:status resp)
|
:status (:status resp)
|
||||||
:reason (:reason-phrase resp)
|
:reason (:reason-phrase resp)
|
||||||
|
@ -48,13 +55,13 @@
|
||||||
(-> (get! self image-data)
|
(-> (get! self image-data)
|
||||||
:output))
|
:output))
|
||||||
|
|
||||||
(get-probabilites! [self image-data]
|
(get-probabilities! [self image-data]
|
||||||
(into {}
|
(into {}
|
||||||
(map (juxt (comp keyword :label) :confidence))
|
(map (juxt (comp keyword :label) :confidence))
|
||||||
(get-detections! self image-data))))
|
(get-detections! self image-data))))
|
||||||
|
|
||||||
(defn define-connection
|
(defn define-connection
|
||||||
[{:keys [scheme host port]
|
[& {:keys [scheme host port]
|
||||||
:or {scheme "http"
|
:or {scheme "http"
|
||||||
port 80}}]
|
port 80}}]
|
||||||
(->ObjectifierClient scheme host port))
|
(->ObjectifierClient scheme host port))
|
||||||
|
|
Loading…
Reference in New Issue