mirror of
https://github.com/jehiah/TrafficSpeed.git
synced 2025-12-24 12:47:54 +08:00
fix seek
This commit is contained in:
@@ -13,10 +13,10 @@ function avg_background(f::VideoIO.VideoReader, rrc::Function)
|
||||
# println("rrc summary $(summary(bg))")
|
||||
# bg = convert(Image{ColorTypes.RGB{Float32}}, frame)
|
||||
|
||||
step = duration(f)/20
|
||||
step = duration(f)/25
|
||||
total_pos = 0
|
||||
count = 1
|
||||
while count < 20
|
||||
while count < 25
|
||||
total_pos += step
|
||||
count+=1
|
||||
println("background frame $count @ $total_pos seconds")
|
||||
|
||||
@@ -2,11 +2,16 @@ using Images
|
||||
import ImageMagick
|
||||
|
||||
function base64img(m::MIME, i::Image)
|
||||
Images.save("/tmp/base64img.png", i)
|
||||
f = open("/tmp/base64img.png", "r")
|
||||
body = readall(f)
|
||||
close(f)
|
||||
return "data:$(m);base64,$(base64encode(body))"
|
||||
try
|
||||
Images.save("/tmp/base64img.png", i)
|
||||
f = open("/tmp/base64img.png", "r")
|
||||
body = readall(f)
|
||||
close(f)
|
||||
return "data:$(m);base64,$(base64encode(body))"
|
||||
catch
|
||||
println("$(summary(i))")
|
||||
rethrow()
|
||||
end
|
||||
# this has issues w/ some Images so we won't use it
|
||||
#
|
||||
# buff = IOBuffer()
|
||||
|
||||
@@ -45,6 +45,11 @@ function labelimg_example(img::Image, background::Image, masks::Array, blur=[3,3
|
||||
colorant"orange", colorant"purple", colorant"gray", colorant"brown",
|
||||
colorant"aliceblue", colorant"burlywood", colorant"cadetblue", colorant"cyan",
|
||||
colorant"deeppink", colorant"gold", colorant"fuchsia", colorant"greenyellow",
|
||||
colorant"yellow3", colorant"red4", colorant"green4",
|
||||
colorant"sandybrown", colorant"salmon", colorant"yellow4", colorant"violetred4"]
|
||||
if maximum(labels) > length(colors)
|
||||
println("error $(maximum(labels)) labels above to $(length(colors))")
|
||||
# labels = labels[:length(colors)]
|
||||
end
|
||||
Image(map(x->colors[x+1], labels'))
|
||||
end
|
||||
10
src/main.jl
10
src/main.jl
@@ -89,11 +89,15 @@ http = HttpHandler() do req::Request, res::Response
|
||||
# inline read, rotate, crop w/ access to job
|
||||
function rrc(f::VideoIO.VideoReader)
|
||||
# _buffer is a 3-dimensional array (color x width x height), but by reinterpreting
|
||||
VideoIO.retrieve!(f, _rrc_buffer)
|
||||
VideoIO.read!(f, _rrc_buffer)
|
||||
# read!(f, img)
|
||||
# buffer = read(f, Image)
|
||||
if haskey(job, "rotate") && job["rotate"] != 0.00001
|
||||
# Image(rotate_and_crop(buffer, job["rotate"], job["bbox_region"]), Dict("spatialorder"=>["x","y"]))
|
||||
Image(rotate_and_crop(reinterpret(ColorTypes.RGB{FixedPointNumbers.UFixed{UInt8, 8}}, _rrc_buffer), job["rotate"], job["bbox_region"]), Dict("spatialorder"=>["x","y"]))
|
||||
else
|
||||
# just crop it (even if it's not really being cropped)
|
||||
# Image(Base.unsafe_getindex(buffer, job["bbox_region"][1], job["bbox_region"][2]), Dict("spatialorder"=>["x","y"]))
|
||||
Image(Base.unsafe_getindex(reinterpret(ColorTypes.RGB{FixedPointNumbers.UFixed{UInt8, 8}}, _rrc_buffer), job["bbox_region"][1], job["bbox_region"][2]), Dict("spatialorder"=>["x","y"]))
|
||||
end
|
||||
end
|
||||
@@ -137,9 +141,9 @@ http = HttpHandler() do req::Request, res::Response
|
||||
|
||||
if job["step"] == 6
|
||||
# we just need to eco back a frame
|
||||
if haskey("seek")
|
||||
if haskey(job, "seek")
|
||||
seek(f, job["seek"])
|
||||
e["step_6_img"] = base64img("image/png", rrc(f))
|
||||
resp["step_6_img"] = base64img("image/png", rrc(f))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -225,7 +225,7 @@ const tpl = `
|
||||
<h2>Step 6: Speed Detection</h2>
|
||||
|
||||
<div class="form-group">
|
||||
<label>Seek (seconds): <input name="seek" id="seek" type="text" /></label>
|
||||
<label>Seek (seconds): <input name="seek" id="seek" type="text" value="{{.Seek}}" /></label>
|
||||
</div>
|
||||
|
||||
{{ if .Seek }}
|
||||
@@ -242,7 +242,7 @@ const tpl = `
|
||||
{{ end }}
|
||||
<button type="submit" class="btn btn-primary" name="next" value="6">Continue</button>
|
||||
|
||||
<img src="{{.Response.Step4Img}}" id="mousemove">
|
||||
<img src="{{.Response.Step6Img}}" id="mousemove">
|
||||
{{ end }}
|
||||
|
||||
</form>
|
||||
|
||||
24
src/seek.jl
24
src/seek.jl
@@ -9,22 +9,28 @@ function Base.seek(s::VideoIO.VideoReader, time, video_stream=1)
|
||||
s
|
||||
end
|
||||
|
||||
function av_rescale_q(a, bq::VideoIO.AVRational, cq::VideoIO.AVRational)
|
||||
b = bq.num * cq.den
|
||||
c = cq.num * bq.den
|
||||
return a * b / c
|
||||
end
|
||||
|
||||
function Base.seek(avin::VideoIO.AVInput, time, video_stream = 1)
|
||||
# AVFormatContext
|
||||
fc = avin.apFormatContext[1]
|
||||
|
||||
stream_info = avin.video_info[video_stream]
|
||||
|
||||
s = stream_info.stream
|
||||
c = stream_info.codec_ctx
|
||||
base_per_frame = (c.time_base.num * c.ticks_per_frame * s.time_base.den / c.time_base.den * s.time_base.num)
|
||||
avg_frame_rate = s.avg_frame_rate.num / s.avg_frame_rate.den
|
||||
pos = floor(Int, time * base_per_frame * avg_frame_rate)
|
||||
println("seeking to $(time) sec @ position $pos (frame rate $avg_frame_rate/sec)")
|
||||
# https://www.ffmpeg.org/doxygen/2.3/group__lavu__time.html
|
||||
seek_target = time * VideoIO.AV_TIME_BASE
|
||||
|
||||
# Seek
|
||||
# http://dranger.com/ffmpeg/functions.html#av_rescale_q
|
||||
# seek_target= av_rescale_q(seek_target, AV_TIME_BASE_Q, pFormatCtx->streams[stream_index]->time_base);
|
||||
seek_target = floor(Int, av_rescale_q(seek_target, VideoIO.AVUtil.AV_TIME_BASE_Q, stream_info.stream.time_base))
|
||||
|
||||
# pos (aka Timestamp) is in AVStream.time_base units or, if no stream is specified, in AV_TIME_BASE units.
|
||||
ret = VideoIO.av_seek_frame(fc, stream_info.stream_index0, pos, VideoIO.AVSEEK_FLAG_ANY)
|
||||
# https://www.ffmpeg.org/doxygen/2.5/group__lavf__decoding.html#gaa23f7619d8d4ea0857065d9979c75ac8
|
||||
# http://dranger.com/ffmpeg/functions.html#av_seek_frame
|
||||
ret = VideoIO.av_seek_frame(fc, stream_info.stream_index0, seek_target, VideoIO.AVSEEK_FLAG_ANY)
|
||||
|
||||
ret < 0 && throw(ErrorException("Could not seek to position of stream"))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user