[英]How to get time between intervals of an on/off sequence?
I'm a bit of an amateur coder, so I'll explain my issue to the best of my ability:我有点业余编码员,所以我会尽我所能解释我的问题:
I'm trying to create a Morse Code Light Decoder, with the input being a video of a flashing morse code sequence, and the output being the decoded phrase.我正在尝试创建一个莫尔斯电码光解码器,输入是闪烁的莫尔斯电码序列的视频,输出是解码后的短语。
So far, I have gotten a variable that is true when the light is on, and false when it is off.到目前为止,我得到了一个变量,当灯亮时为真,灯灭时为假。 Here is a video of what I have so far:
这是我到目前为止所拥有的视频:
Demo Video演示视频
And here is my program if you want to edit it:如果你想编辑它,这是我的程序:
Morse Decoder莫尔斯解码器
Here is the video I am using for testing:这是我用于测试的视频:
Test测试
What I'm looking for is to somehow convert the fluctuating variable in the top left to something that looks like this:我正在寻找的是以某种方式将左上角的波动变量转换为如下所示的内容:
[{"state":"on", "lengthtime":"300ms"}, {"state":"off", "lengthtime":"200ms"}, {"state":"on", "lengthtime":"400ms"}, ...]
This list is each pulse/pause in order, along with the length of each pulse/pause.该列表是按顺序排列的每个脉冲/暂停,以及每个脉冲/暂停的长度。 This would be easy to parse through and convert to morse.
这很容易解析并转换为莫尔斯。
Code that I check if its on or off:我检查它是否打开或关闭的代码:
function drawFrame(video) {
let onoff = 0;
context.drawImage(video, 0, 0);
var imageData = context.getImageData(0, 0, canvas.width, canvas.height);
grayscaleVid(imageData.data);
context.putImageData(imageData, 0, 0);
var pixel = context.getImageData(112, 64, 1, 1);
var data = pixel.data;
const brightness = (data[0] + data[1] + data[2]) / 3;
if (brightness > 130) {
onoff = true;
} else {
onoff = false;
}
document.getElementById('test').innerHTML = onoff;
setTimeout(function() {
drawFrame(video);
}, 10);
}
Set up some global storage with a history and a previous state:设置一些具有历史记录和先前状态的全局存储:
const history = [];
let wasOn = False;
Adjust the brightness detection to log to history
调整亮度检测以记录到
history
const isOn = brightness > 130;
if (wasOn != isOn) {
history.push({
state: wasOn,
timestamp: performance.now(),
});
wasOn = isOn;
}
Then, when done, process the results:然后,完成后,处理结果:
const results = history.map((v,i,a) => {
if (!i) return {state: v.state, duration: 0};
return {state: v.state, duration: v.timestamp - a[i-1].timestamp};
});
Ok, what you need to do is, create a timer, and every time the state changes from on to off, or vice-versa, create a new register.好的,你需要做的是,创建一个定时器,每次状态由开变为关,反之亦然,创建一个新的寄存器。
let registers = []
;
let currTime = Date.now();
let onoff = false;
function drawFrame(){
...
let newOnoff;
if (brightness > 130) {
newOnoff = true;
} else {
newOnoff = false;
}
if (newOnoff !== onoff) {
const state = onoff === true ? 'on' : 'off';
const newTime = Date.now();
registers.push({state, lengthtime : (newTime - currTime) + 'ms' });
currTime = newTime;
}
onoff = newOnoff;
document.getElementById('test').innerHTML = onoff;
...
}
This code is not really pretty, but I'm just keeping the standards you're using这段代码不是很漂亮,但我只是保持你使用的标准
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.