Skip to content

Commit

Permalink
Record game camera information automatically
Browse files Browse the repository at this point in the history
Starts #55
  • Loading branch information
Deon Moolman committed Nov 9, 2015
1 parent c80cd67 commit 4ba7dad
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ pom.xml.asc
*.class
/.lein-*
/.nrepl-port
/.creds
/.creds
capture
4 changes: 3 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
[org.openpnp/opencv "2.4.11-1"]
[quil "2.2.6"]
[clj-http "2.0.0"]
[cheshire "5.5.0"]]
[cheshire "5.5.0"]
[de.schlichtherle.truezip/truezip-file "7.7.9"]
[de.schlichtherle.truezip/truezip-driver-zip "7.7.9"]]
:main igoki.core)
66 changes: 42 additions & 24 deletions src/igoki/game.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
[igoki.util :as util]
[igoki.sgf :as sgf]
[igoki.inferrence :as inferrence]
[quil.core :as q])
[quil.core :as q] )
(:import (java.awt Toolkit)
(java.io File)
(java.io File ByteArrayInputStream)
(processing.core PImage)
(java.util Date)
(java.text SimpleDateFormat)))
(java.util Date UUID)
(java.text SimpleDateFormat)
(org.opencv.highgui Highgui)
(org.opencv.core MatOfByte)))

(defn board-diff [b1 b2]
(remove
Expand Down Expand Up @@ -72,8 +74,10 @@

(defn camera-updated [wk ctx old new]
(view/camera-updated wk ctx old new)
(let [{{{:keys [latch board] :as submit} :submit :as game} :kifu
cboard :board} @ctx
(let [{{{:keys [latch board] :as submit} :submit
:keys [filename camidx] :as game} :kifu
{:keys [raw]} :camera
cboard :board} @ctx
updatelist @captured-boardlist]
(cond
(nil? submit) nil
Expand All @@ -92,12 +96,19 @@
(do
(println "Debounce success - move submitted")
(.beep (Toolkit/getDefaultToolkit))
(when filename
(let [out (MatOfByte.)]
(println "Writing jpg: " filename "/" (str camidx ".jpg"))
(Highgui/imencode ".jpg" raw out)
(util/zip-add-file filename (str camidx ".jpg") (ByteArrayInputStream. (.toArray out)))
(println "Done writing jpg: " filename "/" (str camidx ".jpg"))
))
(let [new (inferrence/infer-moves game (butlast updatelist) (last updatelist))]
(if new
(do
(reset! captured-boardlist [])
(swap! ctx assoc :kifu (dissoc new :submit)))
(swap! ctx update :kifu dissoc :submit)))
(swap! ctx assoc :kifu (assoc (dissoc new :submit) :camidx (inc camidx))))
(swap! ctx update :kifu #(assoc (dissoc % :submit) :camidx (inc camidx)))))
(swap! ctx update :camera dissoc :read-delay)))))

(defn add-initial-points [node board]
Expand All @@ -115,22 +126,29 @@
(> (count black) (count white)) (assoc :player-start ["W"]))))

(defn reset-kifu [ctx]
(let [board (-> @ctx :board)]
(swap! ctx assoc :kifu
(->
{:moves (add-initial-points
{:branches []
:player-start ["B"]
:application ["Igoki"]
:file-format ["4"]
:gametype ["1"]
:size [(-> @ctx :goban :size)]
:date [(.format (SimpleDateFormat. "YYYY-MM-dd") (Date.))]
:komi ["5.5"]}
board)
:movenumber 0
:current-branch-path [[]]}
inferrence/reconstruct))))
(let [board (-> @ctx :board)
new-game
(->
{:filename (str "capture/" (.toString (UUID/randomUUID)) ".zip")
:camidx 0
:moves (add-initial-points
{:branches []
:player-start ["B"]
:application ["Igoki"]
:file-format ["4"]
:gametype ["1"]
:size [(-> @ctx :goban :size)]
:date [(.format (SimpleDateFormat. "YYYY-MM-dd") (Date.))]
:komi ["5.5"]}
board)
:movenumber 0
:current-branch-path [[]]}
inferrence/reconstruct)]
(util/zip-add-file-string
(:filename new-game)
"config.edn"
(pr-str new-game))
(swap! ctx assoc :kifu new-game)))

(defmethod ui/construct :kifu [ctx]
(if-not (-> @ctx :kifu)
Expand Down
18 changes: 16 additions & 2 deletions src/igoki/util.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
(ns igoki.util
(:require [clojure.java.io :as io])
(:import (org.opencv.core Mat Size CvType Point)
(java.awt.image BufferedImage DataBufferByte)
(processing.core PImage PConstants)))
(processing.core PImage PConstants)
(de.schlichtherle.truezip.file TFile TFileWriter TArchiveDetector)
(java.io InputStream ByteArrayInputStream)))

(defn mat-to-buffered-image [^Mat frame]
(let [type (case (.channels frame)
Expand Down Expand Up @@ -130,4 +133,15 @@
(if oks
(let [r (butlast oks)]
(recur r (get-in m oks) (assoc (get-in m r) (last oks) s)))
s)))
s)))


(defn zip-add-file [zipname destname ^InputStream input]
(let [f (TFile. ^String zipname ^String destname (TArchiveDetector.".zip"))]
(TFile/cp input f)))

(defn zip-add-file-string [zipname destname ^String input]
(zip-add-file zipname destname (ByteArrayInputStream. (.getBytes input))))



0 comments on commit 4ba7dad

Please sign in to comment.