Skip to content

Commit 81de18a

Browse files
author
flip phillips
committed
Sped up + universalized ffmpeg stuff
1 parent f5ba5c4 commit 81de18a

File tree

7 files changed

+97
-25
lines changed

7 files changed

+97
-25
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@
1717

1818
## [Unreleased changes]
1919

20+
## [0.1.14] - 2018-04-24
21+
2022
### Added
2123
- `UnitizedGaussianPDF`
24+
- lens distortion model
25+
26+
### Changed
27+
- `RunProcess` instead of `Run` in the ffmpeg stuff
2228

2329
## [0.1.13] - 2018-04-14
2430

Development/Image.nb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:9394b5a9f63e8db4e74cdb57c8a94035a2d259fcf43bf076f038bce701eb2364
3-
size 58280147
2+
oid sha256:033ee2daef52eeed5f601d4f10f500950991ddd072568caeb88a479cd7be7644
3+
size 62417822

FPTools/Image.wl

Lines changed: 78 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
(* ::Package:: *)
22

3+
(* ::Subsection:: *)
4+
(*ImageID sugar*)
5+
6+
37
ImageIdentifyFP[image_Image,n_:5]:=ImageIdentify[image, All, n, "Probability"]
48

59

10+
(* ::Subsection:: *)
11+
(*face blurer*)
12+
13+
614
Options[BlurFaces] = {Method->Automatic,"FilterRadius"->Automatic};
715

816
(* Options are "Box" (default), "Disk", "Outline", "Eyes", "FaceParts" for now *)
@@ -35,44 +43,66 @@ BlurFaces[i_Image,OptionsPattern[]]:=Module[{how,fboxs,polys,ff},
3543
HighlightImage[i, {{"Blur",OptionValue["FilterRadius"]}, polys}]]]
3644

3745

38-
AddAlphaChannel[i_Image] := SetAlphaChannel[i,Image[ConstantArray[1, Reverse@ImageDimensions[i]]]]
46+
(* ::Subsection:: *)
47+
(*Alpha Channel*)
3948

4049

50+
AddAlphaChannel[i_Image] := SetAlphaChannel[i,Image[ConstantArray[1, Reverse@ImageDimensions[i]]]]
4151

4252

4353
(* ::Subsection:: *)
4454
(*ffmpeg things*)
4555

4656

47-
$FFMpegPath = "/usr/local/bin/ffmpeg";
57+
ImportMP4Frame::noex = "Can't find ffmpeg executable.";
58+
59+
60+
$FFMpegPath=None;
61+
62+
63+
FindFFMPEG[] := Module[{deflocs},
64+
Unprotect[$FFMpegPath];
65+
$FFMpegPath = Which[
66+
$FFMpegPath=!=None,$FFMpegPath,
67+
FileExistsQ["/usr/local/bin/ffmpeg"],"/usr/local/bin/ffmpeg",
68+
FileExistsQ["/usr/bin/ffmpeg"],"/usr/bin/ffmpeg",
69+
True, Message[ImportMP4Frame::noex];None
70+
]
71+
]
72+
4873

4974
(* from https://stackoverflow.com/questions/10957412/fastest-way-to-extract-frames-using-ffmpeg *)
5075

51-
ImportMP4Frame[f_, sec_] := Module[{command,fx,fn},
76+
77+
ImportMP4Frame[f_, sec_] := Module[{command,fx,fn,run},
78+
If[$FFMpegPath===None,If[FindFFMPEG[]==None,Return[{}]]];
79+
5280
fx = FileNameJoin[{$TemporaryDirectory,"ImportFrames"}];
53-
Quiet[DeleteDirectory[fx, DeleteContents -> True]];
54-
CreateDirectory[fx];
81+
(*Quiet[DeleteDirectory[fx, DeleteContents -> True]];*)
82+
Quiet[CreateDirectory[fx]];
5583

5684
fn = FileNameJoin[{fx,"aframe"<>ToString[sec]<>".bmp"}];
5785

58-
command =
59-
StringRiffle[{$FFMpegPath,"-y","-accurate_seek","-ss",ToString[sec],"-i",StringReplace[f," "->"\\ "],"-frames:v 1", fn}," "];
60-
61-
If[Run[command] == 0, Import[fn], None]]
86+
command={$FFMpegPath,"-y","-accurate_seek","-ss",ToString[sec],"-i",f,"-frames:v","1", fn};
87+
run=RunProcess[command,ProcessEnvironment-><||>];
88+
If[run["ExitCode"]==0, Import[fn], If[$FPToolsDebug,run,None]]
89+
]
6290

6391

6492
ImportMP4Frames[f_, startTime_, duration_, rate_:1] :=
6593
Table[ImportMP4Frame[f,t],{t,startTime,startTime+duration,rate}]
66-
67-
ImportWebFrame[url_] := Module[{command,fx},
68-
fx = FileNameJoin[{$TemporaryDirectory,"aframe.png"}];
69-
70-
command =
71-
$FFMpegPath<>" -y -i " <> url <> " -vframes 1 " <> fx;
7294

73-
If[Run[command] == 0, Import[fx], None]
95+
96+
ImportWebFrame[url_] := Module[{command,fx,run},
97+
If[$FFMpegPath===None,If[FindFFMPEG[]==None,Return[{}]]];
98+
fx = FileNameJoin[{$TemporaryDirectory,"aframe.png"}];
99+
100+
command={$FFMpegPath,"-y","-i",url,"-vframes","1",fx};
101+
run=RunProcess[command,ProcessEnvironment-><||>];
102+
If[run["ExitCode"] == 0, Import[fx], If[$FPToolsDebug,run,None]]
74103
]
75104

105+
76106
(*
77107
outdir = FileNameJoin[{$TemporaryDirectory, "cappyPorts2"}];
78108
Quiet[DeleteDirectory[outdir, DeleteContents -> True]];
@@ -90,4 +120,35 @@ cmd = "/usr/local/bin/ffmpeg -i " <> url <>
90120

91121
(*
92122
time for i in {0..39} ; do ffmpeg -accurate_seek -ss `echo $i*60.0 | bc` -i input.mp4 -frames:v 1 period_down_$i.bmp ; done
93-
*)
123+
*)
124+
125+
126+
127+
128+
(* ::Subsection:: *)
129+
(*Lens model*)
130+
131+
132+
(* ::Input::Initialization:: *)
133+
LensDistortPoint[{x0_,y0_},{p1_,p2_},kVec_:{}]:=Module[{itx,k,r2,idist,dx,dy},
134+
k=PadRight[kVec,6,0];
135+
136+
r2=x0^2+y0^2;
137+
idist=(1.0+((k[[6]] r2+k[[5]])r2+k[[4]])r2)/(1.0+((k[[3]] r2+k[[2]])r2+k[[1]])r2);
138+
139+
dx=2.0 p1 x0 y0+p2 (r2+2.0x0^2);
140+
dy=p1(r2+2.0y0^2)+2.0p2 x0 y0;
141+
{(x0-dx),(y0-dy)}idist
142+
]
143+
144+
145+
LensDistortionCorrection[i_,{tx_,ty_},\[Theta]_,{p1_,p2_},kVec_,opt:OptionsPattern[ImageTransformation]]:=Module[{id,ar,t,tt,ut,rt},
146+
id=ImageDimensions[i];
147+
ar=1/Divide@@id;
148+
149+
t=RescalingTransform[{{0,1},{0,ar}},{{-1,1},{-ar,ar}}];
150+
tt=TranslationTransform[{tx,ty}]@*t;
151+
ut=RescalingTransform[{{-1,1},{-ar,ar}},{{0,1},{0,ar}}];
152+
rt=ut@*RotationTransform[\[Theta]];
153+
154+
ImageTransformation[i,rt[LensDistortPoint[tt[#],{p1,p2},kVec]]&,PlotRange->{{0.1,0.9},{0.1,0.5}},opt]]

FPTools/PacletInfo.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Copyright -> "©2018- Flip Phillips & Misc Contributors",
77
License -> "MIT",
88
Version -> "0.1.14",
9-
BuildNumber -> "110",
9+
BuildNumber -> "124",
1010
MathematicaVersion -> "11.2+",
1111
URL -> "https://github.com/flipphillips/FPTools",
1212
Thumbnail -> "Documentation/icon.png",
@@ -27,7 +27,7 @@
2727
"FPTools`RectangleToDisk",
2828

2929
"FPTools`ImageIdentifyFP","FPTools`BlurFaces","FPTools`AddAlphaChannel",
30-
30+
"FPTools`LensDistortPoint", "FPTools`LensDistortionCorrection",
3131
"FPTools`$MachineAddressesExternal",
3232

3333
"FPTools`ApplyIf",

FPTools/Usage.wl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ ImportMP4Frame::usage = "ImportMP4Frame[file,frame] extracts frame `frame` from
4141
ImportMP4Frames::usage = "ImportMP4Frames[file,start,dur] extracts frames from start of dur from MP4 file `file`.";
4242
ImportWebFrame::usage = "ImportWebFrame[url] grabs a frame from the stream at url.";
4343

44+
LensDistortPoint::usage = "LensDistortPoint[{x,y},{p1,p2},{k1,...}] projects point x,y using the distortion specified in the p and k vectors.";
45+
46+
LensDistortionCorrection::usage = "LensDistortionCorrection[image,{tx,ty},theta,{p1,p1},{k1,k2,k3...}] performs a lens distortion correction along with a generalized translation (t) and rotation (theta). p1 and p2 specify 1st order astygmatism and k1-6 the different orders of distortion beyond that. Note you only need to supply k's up to the desired order of correction.";
47+
4448

4549
(* ::Section:: *)
4650
(*Internet*)

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
![](icon.png)
44

5-
v 0.1.13
5+
v 0.1.14
66

7-
A set Mathematica tools I frequently use for debugging, introspection &c.
7+
A set Mathematica tools I frequently use for debugging, introspection &c.
8+
**Note that there is a bunch of seemingly unrelated stuff in recent versions. This is because I am using this to easily distribute some tools for my classes this semester. At some point I'll refactor all this out, but for now it remains!**
89

910
For the time being, a small amount of documentation can be had by looking in the Mathematica Help Browser under "Add-ons and Packages".
1011

version.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"major_version": 0,
44
"minor_version": 1,
55
"revision_number": 14,
6-
"build_number": 111,
7-
"date": "Mon Apr 16 21:53:19 EDT 2018"
6+
"build_number": 125,
7+
"date": "Tue Apr 24 11:31:10 EDT 2018"
88
}

0 commit comments

Comments
 (0)