video acquisition through webcam and processing

6 ビュー (過去 30 日間)
vardhit
vardhit 2014 年 4 月 1 日
回答済み: prabhat kumar sharma 2025 年 1 月 30 日 9:07
Basically i want to use live video(webcam) in matlab and detect human body and preview it simultaneously . I have seen demo matlab vision codes for detecting and tracking human body but they work on video file not on live webcam feed. I would like to know how to do that and preview it the same time.When human is detected the video should be recorded and saved in the hard disk.plz guide me how to do this.

回答 (1 件)

prabhat kumar sharma
prabhat kumar sharma 2025 年 1 月 30 日 9:07
Hello Vardhit,
To achieve live human detection using a webcam in MATLAB, you can use the webcam function to capture the video feed and the Computer Vision Toolbox for detecting humans. You can then process each frame to detect humans and save the video when a human is detected. Below is a step-by-step guide and a sample code to get you started:Steps to Implement Live Human Detection
  1. Set Up Webcam:
  • Use MATLAB's webcam function to access your webcam.
2. Load Pre-trained Detector:
  • Use a pre-trained object detector, such as peopleDetectorACF from the Computer Vision Toolbox, to detect humans.
3. Process Video Frames:
  • Capture frames from the webcam, detect humans, and annotate the frames.
4. Record Video:
  • Start recording when a human is detected and save the video to the hard disk.
5 . Display Video:
  • Use imshow to display the video feed with annotations in real-time.
Example Code:
% Initialize webcam
cam = webcam;
% Create a video player
videoPlayer = vision.VideoPlayer('Position', [100, 100, 680, 520]);
% Create a video writer to save the recorded video
videoWriter = VideoWriter('DetectedVideo.avi');
open(videoWriter);
% Load a pre-trained people detector
peopleDetector = peopleDetectorACF();
% Flag to indicate if we are recording
isRecording = false;
% Process frames in a loop
while true
% Capture a frame from the webcam
frame = snapshot(cam);
% Detect people in the frame
bboxes = peopleDetector(frame);
% Annotate detected people
annotatedFrame = insertObjectAnnotation(frame, 'rectangle', bboxes, 'Human');
% Display the annotated frame
step(videoPlayer, annotatedFrame);
% Check if any humans are detected
if ~isempty(bboxes)
% Start recording if not already recording
if ~isRecording
isRecording = true;
disp('Recording started...');
end
% Write the frame to the video file
writeVideo(videoWriter, annotatedFrame);
else
% Stop recording if no humans are detected
if isRecording
isRecording = false;
disp('Recording stopped.');
end
end
% Exit loop if video player window is closed
if ~isOpen(videoPlayer)
break;
end
end
% Clean up
clear cam;
release(videoPlayer);
close(videoWriter);
I hope it helps!

カテゴリ

Help Center および File ExchangeComputer Vision Toolbox についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by