Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

125004102

JAYASAKTHI S

DEMO—EYE DETECTION USING WEBCAM

CODE:

eyeDetector = vision.CascadeObjectDetector('EyePairBig');
cam = webcam;
captureDuration = 10;
figure;
set(gcf, 'Name', 'Real-time Eye Detection and Capture', 'NumberTitle', 'off');
captureTimer = tic;
while toc(captureTimer) <= captureDuration
img = snapshot(cam);

bbox = step(eyeDetector, img);

if ~isempty(bbox)
img = insertObjectAnnotation(img, 'rectangle', bbox, 'Eyes');

eyeRegion = imcrop(img, bbox(1, :));

imwrite(eyeRegion, 'captured_eyes.jpg');

figure;
imshow(eyeRegion);
title('Captured Eyes');

pause(1);

close(gcf);
end

imshow(img);

drawnow;
end
clear cam;
close all;
OUTPUT:

1. Cascade Object Detector Initialization:


- A Cascade Object Detector for detecting eyes (`EyePairBig` preset) is created using
the `vision.CascadeObjectDetector` function.

2. Webcam Initialization:
- A webcam object is created using the `webcam` function to capture live video frames.

3. Figure Initialization:
- A figure is created to display the live video feed and the captured eye region.

4. Loop for Real-time Processing:


- A loop is started, which runs until the specified `captureDuration` is reached.
- A frame is captured from the webcam using `snapshot`.
- The `eyeDetector` is used to detect eyes in the captured frame, resulting in bounding
box coordinates (`bbox`).
- If eyes are detected (the `bbox` is not empty):
- Rectangles are drawn around the detected eyes in the live video feed using
`insertObjectAnnotation`.
- The eye region is extracted from the frame using `imcrop`.
- The extracted eye region is saved to an image file named 'captured_eyes.jpg' using
`imwrite`.
- The extracted eye region is displayed in a separate figure briefly.
- The figure displaying the captured eyes is closed after a brief pause.
- The live video feed is continuously displayed in the main figure using `imshow`.

5. Webcam Stop and Figure Closure:


- After the specified `captureDuration`, the webcam is stopped (`clear cam`), and all
figures are closed (`close all`).

6. Adjustment:
- You can adjust the `captureDuration` variable according to the desired duration for
capturing eyes.

The code demonstrates a simple real-time eye detection and capture system using a
webcam and MATLAB's Computer Vision System Toolbox. It leverages the Viola-Jones
object detection framework for eye detection and provides visual feedback with
rectangles drawn around the detected eyes in the live video feed. The captured eye
regions are saved to an image file during the specified duration.

You might also like