Skip to content

Instantly share code, notes, and snippets.

@marce1994
Last active July 21, 2018 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marce1994/3a7f3ffb401433cdc8aaf64e6298110d to your computer and use it in GitHub Desktop.
Save marce1994/3a7f3ffb401433cdc8aaf64e6298110d to your computer and use it in GitHub Desktop.
something went wrong
const { StreamCamera, Codec } = require("pi-camera-connect");
const fs = require("fs");
const cv = require('opencv4nodejs');
var locks = require('locks');
var mutex = locks.createMutex();
const streamCamera = new StreamCamera({
codec: Codec.H264,
fps: 10,
width: 1920,
height: 1080
});
const videoStream = streamCamera.createStream();
videoStream.on("start", function(data){
console.log("Video stream has started");
});
videoStream.on("error", function(error){
console.log(error);
});
videoStream.on("data",function(data){
console.log(mutex.isLocked);
try{
if(!mutex.isLocked){
mutex.lock(async function(){
const classifier = new cv.CascadeClassifier(cv.HAAR_FRONTALFACE_ALT2);
const matFromArray = new cv.Mat(Buffer.from(data), 1920, 1080, cv.CV_8UC3);
const grayImg = matFromArray.bgrToGray();
console.log("procesando imagen");
const { objects, numDetections } = await classifier.detectMultiScaleAsync(grayImg);
mutex.unlock();
});
}
}catch(err){
console.log(err);
if(mutex.isLocked){
mutex.unlock();
}
}
});
videoStream.on("end", function(data){
console.log("Video stream has ended");
});
const runApp = async () => {
await streamCamera.startCapture();
while(true){
try{
name = (Date.now() / 1000 | 0) + ".h264";
console.log("Nuevo archivo ("+name+")");
const writeStream = fs.createWriteStream(name);
// Pipe the video stream to our video file
videoStream.pipe(writeStream);
writeStream.on("finish",function(){
console.log("Archivo grabado");
});
writeStream.on("error",function(err){
console.log(err);
});
// Wait for 5 seconds
await new Promise(resolve => setTimeout(() => resolve(), 100000));
writeStream.close();
}catch(err){
console.log(err);
}
}
};
runApp();
FROM resin/raspberrypi3-buildpack-deps:jessie
ENV NODE_VERSION 10.0.0
RUN curl -SLO "http://resin-packages.s3.amazonaws.com/node/v$NODE_VERSION/node-v$NODE_VERSION-linux-armv7hf.tar.gz" \
&& tar -xzf "node-v$NODE_VERSION-linux-armv7hf.tar.gz" -C /usr/local --strip-components=1 \
&& rm "node-v$NODE_VERSION-linux-armv7hf.tar.gz" \
&& npm config set unsafe-perm true -g --unsafe-perm \
&& rm -rf /tmp/*
RUN apt-get update && apt-get upgrade -y
RUN apt-get install -y build-essential \
cmake \
pkg-config \
python3-dev \
python3-numpy \
python3-pip \
libopenblas-dev
RUN apt-get install libraspberrypi-dev raspberrypi-kernel-headers -y
RUN usermod -a -G video root
RUN npm i face-recognition
RUN npm i raspberry-pi-camera-native --unsafe-perm
RUN npm i jpeg-js node-raspistill
RUN npm i opencv4nodejs
RUN npm i locks
RUN npm i pi-camera-connect
COPY main.js main.js
ENTRYPOINT [ "node", "main.js" ]
@marce1994
Copy link
Author

Sergio Daniel Xalambrí [18:17]
no es buena idea eso, podés estar causando que la app corra muchas veces y se llene la memoria
mejor hace esto:

setInterval(main)```
(editado)

Pablo Bianco [18:18]
ah, no dabia que eso aplicaba tambien para node
:cara_neutra:
ah

Sergio Daniel Xalambrí [18:18]
y cada que termina main hacés otro setInterval o setImmediate mejor

Pablo Bianco [18:18]
el setInterval

Sergio Daniel Xalambrí [18:18]
llamando otra vez a main
donde main es tu código dentro del while(true)
de esa forma cada que termine main se vuelve a ejecutar
similar al while(true) pero te aseguras que espera a que termine

Pablo Bianco [18:19]
gracias
tengo que re armar el entorno
y tarda horas en buildear, asi que cuando lo corriga me meto otra vez a jodes xD
joder

Sergio  [18:20]
https://gist.github.com/marce1994/3a7f3ffb401433cdc8aaf64e6298110d#file-bug-js-L71
eso también sacalo, si haces los del setImmediate podés volverlo a ejecutar en el on finish

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment