This library is designed to detect the lighting conditions of a camera in real-time, providing insights into whether the camera is in a well-lit or poorly lit environment.
npm install detect-camera-lighting
This library is designed to detect the lighting conditions from video stream or an image.
``sh`
npm install detect-camera-lighting
`javascript
import DetectLighting from 'detect-camera-lighting';
const detectLighting = new DetectLighting();
const lightImageUrl = './images/light.jpg';
const eventEmitter = detectLighting.image(lightImageUrl);
eventEmitter.addEventListener('processed', (data) => {
console.log('Processed image:', data.detail); // this will give value 'light' or 'dark'
});
`
Note: Ensure video is loaded before passing to the library
`javascript
import DetectLighting from 'detect-camera-lighting';
navigator.mediaDevices.getUserMedia({ video: true })
.then((stream) => {
const video = document.getElementById('video');
video.srcObject = stream;
video.addEventListener('loadedmetadata', () => {
video.play();
});
video.addEventListener('play', () => {
detectLighting.video(video).addEventListener('processed', (data) => {
console.log('Processed video frame:', data.detail); // this will give value 'light' or 'dark'
});
});
})
.catch((error) => {
console.error('Error accessing webcam:', error);
});
`
You can pass options to the image/video methods of DetectLighting to customize the detection process.
Threshold: The default threshold is set to 0.5, which means if the average brightness of the image is greater than 0.5, it will be considered as 'light', otherwise 'dark'.
`javascript
const eventEmitter = detectLighting.image(lightImageUrl, {
threshold: 0.5, // Threshold for light detection (default is 0.5)
});
const eventEmitter = detectLighting.video(video, {
threshold: 0.5, // Threshold for light detection (default is 0.5)
});
``
If you like my work, please consider giving it a star on GitHub or Sponsor my work by following sponsoring me link.