Mercurial > lasercutter
view src/clojureDemo/Xuggle.clj @ 4:6533b4ef83ad
added hash-map filters :)
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 20 Aug 2010 01:30:12 -0400 |
parents | 6d9bdaf919f7 |
children |
line wrap: on
line source
1 (ns clojureDemo.Xuggle)3 (import '(ij ImagePlus IJ))4 (import '(java.awt Rectangle Robot Toolkit) )5 (import '(java.awt.image BufferedImage) )6 (import '(java.awt Graphics2D Panel))7 (import '(java.io File) )8 (import '(javax.imageio ImageIO) )9 (import '(com.xuggle.mediatool ToolFactory))10 (import '(com.xuggle.mediatool IMediaDebugListener IMediaDebugListener$Event))11 (import '(com.xuggle.mediatool MediaToolAdapter MediaListenerAdapter))12 (import '(com.xuggle.xuggler IContainer IContainer$Type IPacket))13 (import '(javax.swing JFrame))14 (import '(com.xuggle.mediatool IMediaWriter))15 (import '(org.apache.commons.io FileUtils))16 (import '(javax.imageio.stream FileImageOutputStream))17 (import '(javax.imageio ImageWriteParam IIOImage))18 (import '(com.xuggle.xuggler IRational))19 (import '(java.util.concurrent TimeUnit))20 (import '(com.xuggle.xuggler ICodec))22 (use 'clojureDemo.Defines)23 ;(use '[clojureDemo.OpenCv :only (video-data)])27 (import '(java.io File))28 (import '(org.apache.commons.io FileUtils))29 (import '(javax.imageio ImageIO) )31 (import '(ij.plugin PlugIn))32 (import '(ij ImagePlus IJ))35 (use 'clojure.contrib.repl-utils)36 (use ['clojureDemo.Defines37 :only '(38 lian look getto human0 blow base app0 app1 app2 app3 app4 app539 bounce0 bounce1 bounce2 bounce3 bounce4 bounce5 bounce640 collide0 collide1 collide2 collide3 collide441 give0 give1 give2 give3 give4 target default)])44 ;(def hamster (ImagePlus. "lklk" (first (video-seq look))))50 (defn- makePanel [image] (proxy [Panel] [] (paint [g] (.drawImage g image 0 0 nil))))53 (defmulti display "Creates a JFrame and displays a buffered image" class)55 (defmethod display56 BufferedImage [image]57 (let [panel (makePanel image)58 frame (JFrame. "Oh Yeah!")]59 (.add frame panel)60 (.pack frame)61 (.setVisible frame true )62 (.setSize frame(.getWidth image) (.getHeight image))))64 (defmethod display65 ImagePlus [image]66 (display (.getBufferedImage image)))71 (defn flash72 [image]74 (.start (Thread. (fn []75 (do76 (.show image)77 (.updateAndRepaintWindow image)78 (Thread/sleep 4000)79 (.hide image))))))82 (defn readerRecurse83 "calls .readPacket until there's nothing left to do"84 [reader]85 (if (not (nil? (.readPacket reader))) ; here .readPacket actually does the processing as a side-effect.86 nil ; it returns null when it has MORE to process, and signals an error when done...87 (recur reader)))90 (def *cache-directory* (File. "/home/r/Desktop/vision-cache"))91 (def *ext* "jpg")96 (defn writeJpg97 "WTF is this shit?!"98 [image target quality]99 (let [jpgWriter (.next (ImageIO/getImageWritersByFormatName *ext*))]100 (doto (.getDefaultWriteParam jpgWriter)101 (.setCompressionMode ImageWriteParam/MODE_EXPLICIT)102 (.setCompressionQuality quality))103 (doto jpgWriter104 (.setOutput (FileImageOutputStream. target))105 (.write (IIOImage. image nil nil))106 (.dispose))))110 (defn cache-path111 [video]112 (File. *cache-directory* (.getName video)))114 (defn video-frame-path115 [video n]116 (File. (cache-path video) (str n "." *ext*)))119 (defn already-cached120 "this is the simplest and most retarded way to do it"121 [video]122 (.exists (cache-path video)))129 (defn make-incrementer [start increment] (let [a (ref (- start increment))] (fn [] (dosync (ref-set a (+ @a increment))))))131 (defn make-frame-writer132 [video]133 (let [incrementer (make-incrementer 0 1)]134 (proxy [MediaListenerAdapter] []136 (onVideoPicture137 [event]138 ;(println (.getImage event))139 ;(println (File. (cache-path video) (str (incrementer) "." *ext* ) ))141 (let [target (File. (cache-path video) (str (incrementer) "." *ext* ))]142 (if (= *ext* "jpg")143 (writeJpg (.getImage event) target 1)144 (ImageIO/write (.getImage event) *ext* target )))))))148 (defn cache149 "caching of frames without opencv"150 [video]152 (if (already-cached video)153 nil154 (time155 (let [reader (ToolFactory/makeReader (str video))]156 (println "slow cache!")157 (FileUtils/forceMkdir (cache-path video))158 (doto reader159 (.setBufferedImageTypeToGenerate BufferedImage/TYPE_3BYTE_BGR)160 (.addListener (make-frame-writer video))161 (.addListener (ToolFactory/makeDebugListener (into-array [IMediaDebugListener$Event/META_DATA]))))162 (readerRecurse reader)))))165 (defn video-data166 "get video data without opencv"167 [video] (cache video)170 {:length (- (count (file-seq (cache-path video))) 1)171 :width (.getWidth (ImagePlus. (str (video-frame-path video 0))))172 :height (.getHeight (ImagePlus. (str (video-frame-path video 0))))173 :fps 30}) ; yeah --- I'll figure this out later.174 (def video-data (memoize video-data))178 (defn convert179 "takes video and converts it to a new type of video"180 [videoInput videoOutput]181 (let [reader (ToolFactory/makeReader (str videoInput))]182 (doto reader183 (.addListener (ToolFactory/makeWriter (str videoOutput) reader))184 (.addListener (ToolFactory/makeDebugListener (into-array [IMediaDebugListener$Event/META_DATA]))))185 (readerRecurse reader)))188 (import '(com.xuggle.xuggler.video ConverterFactory))191 (defn trans-save192 "this is a transitive way to save a stream to an avi file.193 It uses meta data to determine what fps to use to encode."194 [destination video-seq]195 (let [data (meta video-seq)196 writer (ToolFactory/makeWriter (str destination))197 incrementer (make-incrementer 0 (/ 1 30))]198 (.addVideoStream writer 0 0 (ICodec/findEncodingCodecByName "mpeg4")199 (IRational/make (double (:fps data)))200 (int (:width data)) (int (:height data)))201 (dorun (map #(.encodeVideo writer 0202 (ConverterFactory/convertToType (.getBufferedImage %) BufferedImage/TYPE_3BYTE_BGR)203 (long (* 1000000000 (incrementer))) TimeUnit/NANOSECONDS) video-seq))204 (.close writer))205 video-seq)208 (defn video-seq209 "let's use ImagePlus stuff!"210 ([video] (cache video)211 (with-meta (map #(ImagePlus. (str (video-frame-path video %))) (range (:length (video-data video))) ) (video-data video))))218 (comment219 (do (use :reload-all 'clojureDemo.Xuggle) (in-ns 'clojureDemo.Xuggle))220 )