LMS行政E化暨班級社群教職員工數位歷程檔線上手冊Login
Position: 王文良 > 專題製作
two files for opencv4nodejs
by 王文良 2019-02-25 16:47:01, Reply(0), Views(169)
利用nodejs, javascript, socketio, 建立視訊串流

index.js :
 const cv=require('opencv4nodejs');
const path=require('path');
const express=require('express')
const app=express();
const server=require('http').Server(app);
const io=require('socket.io')(server);

const FPS=10;
const wCap=new cv.VideoCapture(0);
wCap.set(cv.CAP_PROP_FRAME_WIDTH,600);
wCap.set(cv.CAP_PROP_FRAME_HEIGHT,600);
app.get('/',(req,res)=>{
res.sendFile(path.join(__dirname,'index.html'));
});

io.on('connect',function(){console.log('abc');});

setInterval(()=>{
const frame=wCap.read();
const image=cv.imencode('.jpg',frame).toString('base64');
io.emit('image',image);
},1000/FPS);

server.listen(3000);


index.html:
<html>
<body>
<h1> Wwl</h1>
<img id="image" />
<script src="/socket.io/socket.io.js"></script>
<script>
const socket=io.connect('http://192.168.151.95:3000');
socket.on('image', (image)=>{
const imageElm=document.getElementById('image');
imageElm.src=`data:image/jpeg;base64,${image}`;

});
</script>
</body>
</html>
Attachments:
2. index.js (1 KB)
Reply