-
Notifications
You must be signed in to change notification settings - Fork 29
/
WebCam3.jl
261 lines (218 loc) · 7.62 KB
/
WebCam3.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# WebCam: Camera Widget
# - uses VideoUI only for determination of camera names and format
# - camera can be deactivated to allow for access from other video software
# - images are converted to png by ffmpg.exe
# - supports two update modes: "webchannel" (default) and "url"
# - supports multiple cameras, hardware and models are separated
# - browser refresh rate (model.fps) is reflected after reactivating the camera
# - model.fps = 0 together with model.updatemode = "webchannel" will update as fast as possible
# - this is the new default, so please provide model.fps > 0 in "url" updatemode.
# Note: currently I have defined a global model in order to easily play with the values from the REPL
# in a productive application that might be removed
using Stipple, StippleUI
using HTTP
using FileIO, VideoIO
import VideoIO.FFMPEG.ffmpeg
import Base.cconvert
Base.cconvert(::Type{Ptr{Ptr{VideoIO.AVDictionary}}}, d::VideoIO.AVDict) = d.ref_ptr_dict
using Base64: base64encode
base64png(png) = "data:image/png;base64,$(base64encode(png))"
Base.@kwdef mutable struct Camera
camera::String = get(VideoIO.CAMERA_DEVICES, 1, "0")
process::Base.Process = run(Sys.iswindows() ? `cmd /C` : `echo`)
fmt::String = unsafe_load(VideoIO.DEFAULT_CAMERA_FORMAT[]).name |> unsafe_string
img::Vector{UInt8} = UInt8[]
sx::Int = 640
sy::Int = 360
fps::Int = 30 # auto in "webchannel" updatemode
end
const PORT = 8000
const CAMERAS = Dict{String, Camera}()
# CAMERAS = Dict(camera => Camera(;camera) for camera in VideoIO.CAMERA_DEVICES)
empty!(CAMERAS)
for camera in VideoIO.CAMERA_DEVICES
push!(CAMERAS, camera => Camera(;camera))
end
# Stipple imports `view` via Html, therefore it needs to be explicitly imported
import Base.view
# `readpngdata` taken from Per Rutquist (@Per) https://github.com/perrutquist/FFmpegPipe.jl/blob/cc2d73acfa8ce55e3e4e53b8264c94477fa0bce3/src/FFmpegPipe.jl#L74
function readpngdata(io)
blk = 65536;
a = Array{UInt8}(undef, blk)
readbytes!(io, a, 8)
if view(a, 1:8) != magic(format"PNG")
error("Bad magic.")
end
n = 8
while !eof(io)
if length(a)<n+12
resize!(a, length(a)+blk)
end
readbytes!(io, view(a, n+1:n+12), 12)
m = 0
for i=1:4
m = m<<8 + a[n+i]
end
chunktype = view(a, n+5:n+8)
n=n+12
if chunktype == codeunits("IEND")
break
end
if length(a)<n+m
resize!(a, max(length(a)+blk, n+m+12))
end
readbytes!(io, view(a, n+1:n+m), m)
n = n+m
end
resize!(a,n)
return a
end
start_camera(camera::Camera) = ffmpeg() do exe
stop_camera(camera)
device = string("video=", camera.camera)
@info "starting camera with '$device'"
camera.process = open(`$exe -hide_banner -loglevel error -f $(camera.fmt) -r $(camera.fps) -s $(camera.sx)x$(camera.sy) -i $device -c:v png -f image2pipe -`)
@async while process_running(camera.process)
camera.img = readpngdata(camera.process)
end
return camera.process
end
function stop_camera(camera::Camera)
while !process_exited(camera.process)
kill(camera.process)
sleep(0.1)
end
end
@reactive! mutable struct WebCam <: ReactiveModel
camera::R{String} = CAMERAS[first(VideoIO.CAMERA_DEVICES)].camera
cameraon::R{Bool} = false
cameratimer::Int = 0
updatemode::R{String} = "webchannel"
request_image::R{Bool} = false
cameras::R{Vector{String}} = copy(VideoIO.CAMERA_DEVICES), READONLY
# refresh rate of the browser (not necessarily identical with the hardware refreshrate `camera.fps`)
# this can be chosen a higher number than the hardware resfresh rate, e.g. 100, as the browser will skip frames
# as long as the previous frame has not been transferred. Very high rates will decrease browser performance, though.
fps::R{Int} = 0
img::R{Vector{UInt8}} = Vector{UInt8}(), PRIVATE
image::R{String} = ""
end
Stipple.js_methods(model::WebCam) = """
updateimage: function () {
if (this.updatemode == "webchannel") {
if (! this.request_image) { this.request_image = true }
} else {
this.image = "frame/" + new Date().getTime()
}
},
startcamera: function () {
if (this.fps) {
this.cameratimer = setInterval(this.updateimage, 1000/this.fps);
} else {
this.updateimage()
}
},
stopcamera: function () {
clearInterval(this.cameratimer);
}
"""
Stipple.js_watch(model::WebCam) = """
cameraon: function (newval, oldval) {
this.stopcamera()
if (newval) { this.startcamera() }
},
request_image: function (newval, oldval) {
if (this.cameraon & this.updatemode == "webchannel" & this.fps == 0 & ! this.request_image) {
this.request_image = true
}
}
"""
function handlers(model)
on(model.isready) do isready
isready || return
model.cameraon[] = true
end
on(model.cameraon) do ison
haskey(CAMERAS, model.camera[]) || return
camera = CAMERAS[model.camera[]]
ison ? start_camera(camera) : stop_camera(camera)
end
onbutton(model.request_image) do
global t0
model.image[] = base64png(CAMERAS[model.camera[]].img)
# println("fps: ", 1000 / (now() - t0).value)
# t0 = now()
end
model
end
# kill(model.cam_process__[])
function ui(model)
page(model, [
p(quasar(:img, "", src=:image, :basic, style="
-webkit-app-region: drag;
border-radius: 50%;
width: 95vw;
height: 95vw"),
style = "margin: 2.5vw"),
p(toggle("", fieldname = :cameraon)),
], title = "WebCam") *
script("""document.documentElement.style.setProperty("--st-dashboard-bg", "#fff0")""") *
style("""
::-webkit-scrollbar { width: 0px; }
body:hover { background: #ffcccc00 }
""")
end
# for debugging
# ElectronAPI.reload(win)
route("/") do
global model
model = init(WebCam, debounce = 0)
model |> handlers |> ui |> html
end
t0 = now()
route("frame/:timestamp") do
global model, t0
# println(" fps: ", 1000 / (now() - t0).value)
# t0 = now()
HTTP.Messages.Response(200, CAMERAS[model.camera[]].img)
end
Genie.config.server_host = "127.0.0.1"
up(PORT)
using Electron, JSON
function camerawidget()
win = Window(URI("http://localhost:$PORT"), options = Dict(
"transparent" => true,
"frame" => false,
"width" => 145,
"height" => 200,
))
ElectronAPI.setAlwaysOnTop(win, true)
# initialize `oldSize`, `width` and `height`
wsize = ElectronAPI.getSize(win)
run(win.app, """
oldSize = $(JSON.json(wsize))
width = oldSize[0]
height = oldSize[1]
""")
# implement auto resize on dragging of the side handles
# resizing by the edge handles works only poorly
ElectronAPI.on(win, "resize", JSON.JSONText("""function() {
win = electron.BrowserWindow.fromId($(win.id))
newSize = win.getSize()
if (Math.abs(oldSize[0] - newSize[0]) < 5) {
height = newSize[1]
width = height - 45
} else if (Math.abs(oldSize[1] - newSize[1]) < 5) {
width = newSize[0]
height = width + 45
}
if (Math.abs(oldSize[0] - newSize[0]) < 3 & Math.abs(oldSize[1] - newSize[1]) < 3) {
oldSize = newSize
return
}
oldSize = [width, height]
win.setSize(width, height)
}"""))
win
end
win = camerawidget()