Mercurial > cortex
view org/vision.org @ 171:15bde60217aa
updated docstring for vision-fn
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 04 Feb 2012 05:02:25 -0700 |
parents | 1a00b4918529 |
children | 04a39e6bb695 |
line wrap: on
line source
1 #+title: Simulated Sense of Sight2 #+author: Robert McIntyre3 #+email: rlm@mit.edu4 #+description: Simulated sight for AI research using JMonkeyEngine3 and clojure5 #+keywords: computer vision, jMonkeyEngine3, clojure6 #+SETUPFILE: ../../aurellem/org/setup.org7 #+INCLUDE: ../../aurellem/org/level-0.org8 #+babel: :mkdirp yes :noweb yes :exports both10 * COMMENT Vision12 I want to make creatures with eyes. Each eye can be independely moved13 and should see its own version of the world depending on where it is.15 Here's how vision will work.17 Make the continuation in scene-processor take FrameBuffer,18 byte-buffer, BufferedImage already sized to the correct19 dimensions. the continuation will decide wether to "mix" them20 into the BufferedImage, lazily ignore them, or mix them halfway21 and call c/graphics card routines.23 (vision creature) will take an optional :skip argument which will24 inform the continuations in scene processor to skip the given25 number of cycles 0 means that no cycles will be skipped.27 (vision creature) will return [init-functions sensor-functions].28 The init-functions are each single-arg functions that take the29 world and register the cameras and must each be called before the30 corresponding sensor-functions. Each init-function returns the31 viewport for that eye which can be manipulated, saved, etc. Each32 sensor-function is a thunk and will return data in the same33 format as the tactile-sensor functions the structure is34 [topology, sensor-data]. Internally, these sensor-functions35 maintain a reference to sensor-data which is periodically updated36 by the continuation function established by its init-function.37 They can be queried every cycle, but their information may not38 necessairly be different every cycle.40 Each eye in the creature in blender will work the same way as41 joints -- a zero dimensional object with no geometry whose local42 coordinate system determines the orientation of the resulting43 eye. All eyes will have a parent named "eyes" just as all joints44 have a parent named "joints". The resulting camera will be a45 ChaseCamera or a CameraNode bound to the geo that is closest to46 the eye marker. The eye marker will contain the metadata for the47 eye, and will be moved by it's bound geometry. The dimensions of48 the eye's camera are equal to the dimensions of the eye's "UV"49 map.51 #+name: eyes52 #+begin_src clojure53 (ns cortex.vision54 "Simulate the sense of vision in jMonkeyEngine3. Enables multiple55 eyes from different positions to observe the same world, and pass56 the observed data to any arbitray function."57 {:author "Robert McIntyre"}58 (:use (cortex world sense util))59 (:use clojure.contrib.def)60 (:import com.jme3.post.SceneProcessor)61 (:import (com.jme3.util BufferUtils Screenshots))62 (:import java.nio.ByteBuffer)63 (:import java.awt.image.BufferedImage)64 (:import com.jme3.renderer.ViewPort)65 (:import com.jme3.math.ColorRGBA)66 (:import com.jme3.renderer.Renderer)67 (:import com.jme3.scene.Node))69 (cortex.import/mega-import-jme3)72 (defn vision-pipeline73 "Create a SceneProcessor object which wraps a vision processing74 continuation function. The continuation is a function that takes75 [#^Renderer r #^FrameBuffer fb #^ByteBuffer b #^BufferedImage bi],76 each of which has already been appropiately sized."77 [continuation]78 (let [byte-buffer (atom nil)79 renderer (atom nil)80 image (atom nil)]81 (proxy [SceneProcessor] []82 (initialize83 [renderManager viewPort]84 (let [cam (.getCamera viewPort)85 width (.getWidth cam)86 height (.getHeight cam)]87 (reset! renderer (.getRenderer renderManager))88 (reset! byte-buffer89 (BufferUtils/createByteBuffer90 (* width height 4)))91 (reset! image (BufferedImage.92 width height93 BufferedImage/TYPE_4BYTE_ABGR))))94 (isInitialized [] (not (nil? @byte-buffer)))95 (reshape [_ _ _])96 (preFrame [_])97 (postQueue [_])98 (postFrame99 [#^FrameBuffer fb]100 (.clear @byte-buffer)101 (continuation @renderer fb @byte-buffer @image))102 (cleanup []))))104 (defn frameBuffer->byteBuffer!105 "Transfer the data in the graphics card (Renderer, FrameBuffer) to106 the CPU (ByteBuffer)."107 [#^Renderer r #^FrameBuffer fb #^ByteBuffer bb]108 (.readFrameBuffer r fb bb) bb)110 (defn byteBuffer->bufferedImage!111 "Convert the C-style BGRA image data in the ByteBuffer bb to the AWT112 style ABGR image data and place it in BufferedImage bi."113 [#^ByteBuffer bb #^BufferedImage bi]114 (Screenshots/convertScreenShot bb bi) bi)116 (defn BufferedImage!117 "Continuation which will grab the buffered image from the materials118 provided by (vision-pipeline)."119 [#^Renderer r #^FrameBuffer fb #^ByteBuffer bb #^BufferedImage bi]120 (byteBuffer->bufferedImage!121 (frameBuffer->byteBuffer! r fb bb) bi))123 (defn add-camera!124 "Add a camera to the world, calling continuation on every frame125 produced."126 [#^Application world camera continuation]127 (let [width (.getWidth camera)128 height (.getHeight camera)129 render-manager (.getRenderManager world)130 viewport (.createMainView render-manager "eye-view" camera)]131 (doto viewport132 (.setClearFlags true true true)133 (.setBackgroundColor ColorRGBA/Black)134 (.addProcessor (vision-pipeline continuation))135 (.attachScene (.getRootNode world)))))137 (defn retina-sensor-profile138 "Return a map of pixel selection functions to BufferedImages139 describing the distribution of light-sensitive components of this140 eye. Each function creates an integer from the rgb values found in141 the pixel. :red, :green, :blue, :gray are already defined as142 extracting the red, green, blue, and average components143 respectively."144 [#^Spatial eye]145 (if-let [eye-map (meta-data eye "eye")]146 (map-vals147 load-image148 (eval (read-string eye-map)))))150 (defn eye-dimensions151 "Returns [width, height] specified in the metadata of the eye"152 [#^Spatial eye]153 (let [dimensions154 (map #(vector (.getWidth %) (.getHeight %))155 (vals (retina-sensor-profile eye)))]156 [(apply max (map first dimensions))157 (apply max (map second dimensions))]))159 (defvar160 ^{:arglists '([creature])}161 eyes162 (sense-nodes "eyes")163 "Return the children of the creature's \"eyes\" node.")165 (defn add-eye!166 "Create a Camera centered on the current position of 'eye which167 follows the closest physical node in 'creature and sends visual168 data to 'continuation."169 [#^Node creature #^Spatial eye]170 (let [target (closest-node creature eye)171 [cam-width cam-height] (eye-dimensions eye)172 cam (Camera. cam-width cam-height)]173 (.setLocation cam (.getWorldTranslation eye))174 (.setRotation cam (.getWorldRotation eye))175 (.setFrustumPerspective176 cam 45 (/ (.getWidth cam) (.getHeight cam))177 1 1000)178 (bind-sense target cam)179 cam))181 (def presets182 {:all 0xFFFFFF183 :red 0xFF0000184 :blue 0x0000FF185 :green 0x00FF00})187 (defn vision-fn188 "Returns a list of functions, each of which will return a color189 channel's worth of visual information when called inside a running190 simulation."191 [#^Node creature #^Spatial eye & {skip :skip :or {skip 0}}]192 (let [retinal-map (retina-sensor-profile eye)193 camera (add-eye! creature eye)194 vision-image195 (atom196 (BufferedImage. (.getWidth camera)197 (.getHeight camera)198 BufferedImage/TYPE_BYTE_BINARY))199 register-eye!200 (runonce201 (fn [world]202 (add-camera!203 world camera204 (let [counter (atom 0)]205 (fn [r fb bb bi]206 (if (zero? (rem (swap! counter inc) (inc skip)))207 (reset! vision-image208 (BufferedImage! r fb bb bi))))))))]209 (vec210 (map211 (fn [[key image]]212 (let [whites (white-coordinates image)213 topology (vec (collapse whites))214 mask (presets key)]215 (fn [world]216 (register-eye! world)217 (vector218 topology219 (vec220 (for [[x y] whites]221 (bit-and222 mask (.getRGB @vision-image x y))))))))223 retinal-map))))226 ;; TODO maybe should add a viewport-manipulation function to227 ;; automatically change viewport settings, attach shadow filters, etc.229 (defn vision!230 "Returns a function which returns visual sensory data when called231 inside a running simulation"232 [#^Node creature & {skip :skip :or {skip 0}}]233 (reduce234 concat235 (for [eye (eyes creature)]236 (vision-fn creature eye))))238 #+end_src241 Note the use of continuation passing style for connecting the eye to a242 function to process the output. You can create any number of eyes, and243 each of them will see the world from their own =Camera=. Once every244 frame, the rendered image is copied to a =BufferedImage=, and that245 data is sent off to the continuation function. Moving the =Camera=246 which was used to create the eye will change what the eye sees.248 * Example250 #+name: test-vision251 #+begin_src clojure252 (ns cortex.test.vision253 (:use (cortex world util vision))254 (:import java.awt.image.BufferedImage)255 (:import javax.swing.JPanel)256 (:import javax.swing.SwingUtilities)257 (:import java.awt.Dimension)258 (:import javax.swing.JFrame)259 (:import com.jme3.math.ColorRGBA)260 (:import com.jme3.scene.Node)261 (:import com.jme3.math.Vector3f))263 (defn test-two-eyes264 "Testing vision:265 Tests the vision system by creating two views of the same rotating266 object from different angles and displaying both of those views in267 JFrames.269 You should see a rotating cube, and two windows,270 each displaying a different view of the cube."271 []272 (let [candy273 (box 1 1 1 :physical? false :color ColorRGBA/Blue)]274 (world275 (doto (Node.)276 (.attachChild candy))277 {}278 (fn [world]279 (let [cam (.clone (.getCamera world))280 width (.getWidth cam)281 height (.getHeight cam)]282 (add-camera! world cam283 ;;no-op284 (comp (view-image) BufferedImage!)285 )286 (add-camera! world287 (doto (.clone cam)288 (.setLocation (Vector3f. -10 0 0))289 (.lookAt Vector3f/ZERO Vector3f/UNIT_Y))290 ;;no-op291 (comp (view-image) BufferedImage!))292 ;; This is here to restore the main view293 ;; after the other views have completed processing294 (add-camera! world (.getCamera world) no-op)))295 (fn [world tpf]296 (.rotate candy (* tpf 0.2) 0 0)))))297 #+end_src299 #+results: test-vision300 : #'cortex.test.vision/test-two-eyes302 The example code will create two videos of the same rotating object303 from different angles. It can be used both for stereoscopic vision304 simulation or for simulating multiple creatures, each with their own305 sense of vision.307 - As a neat bonus, this idea behind simulated vision also enables one308 to [[../../cortex/html/capture-video.html][capture live video feeds from jMonkeyEngine]].311 * COMMENT code generation312 #+begin_src clojure :tangle ../src/cortex/vision.clj313 <<eyes>>314 #+end_src316 #+begin_src clojure :tangle ../src/cortex/test/vision.clj317 <<test-vision>>318 #+end_src