Skip to content

Commit efafff7

Browse files
authored
Add files via upload
0 parents  commit efafff7

9 files changed

+202
-0
lines changed

README.MD

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Integrate Python Code into Simulink for Simulation
2+
3+
Using Python Human Detection Algorithm in Simulink Example
4+
Copyright 2012-2021 The MathWorks, Inc.
5+
6+
This example shows users how to integrate Python Human detection code into Simulink for simulation. This example reads a pre-recorded video, applies the human detection algorithm defined in Python, and generates the output video with human marked. This example includes the following files:
7+
8+
runme.m: This file provides the prep steps when using this example. Since the Python algorithm is based on OpenCV, you will need to install the needed packages if they are not available on your computer. This file also helps set the "out of Process" execution mode to avoid possible MATLAB crashes.
9+
10+
11+
detectHuman.py: this file contains the Python algorithm using OpenCV Histogram of Oriented Gradients (HOG) for human detection.
12+
13+
livedata.mp4: this file is a pre-recorded video showing several people walking.
14+
15+
python_HumanDetector.m: this file contains the MATLAB System Object integrating the Python human detection algorithm.
16+
17+
videoReader.m: this file contains the MATLAB System Object reading the pre-recorded video of livedata.mp4. This can be replaced if you have DSP System Toolbox available (see below).
18+
19+
base_python_example_21a.slx: this file shows how Simulink can integrate Python code for simulation by using either MATLAB Function block or MATLAB System block. You could use the manual switch to select either method to bring in Python code. When you run this file, the output video will show blue boxes drawn around detected people.
20+
21+
If you have DSP System Toolbox available, you could use the "From Multimedia File" block to replace the videoReader System block for simpler video reading; If you have Computer Vision Toolbox available, you can use the "To video Display" block to replace the Video_with_human_detection MATLAB function block for simpler video play.
22+
23+
To use this example, open and run the runme.m first in MATLAB; Then start the base_python_example_21a.slx and run the simulation.
24+
25+
Products needed for using this example :
26+
27+
MATLAB;
28+
Simulink;
29+
DSP System Toolbox (optional);
30+
Computer Vision Toolbox (optional);

SECURITY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Reporting Security Vulnerabilities
2+
3+
If you believe you have discovered a security vulnerability, please report it to
4+
[security@mathworks.com](mailto:security@mathworks.com). Please see
5+
[MathWorks Vulnerability Disclosure Policy for Security Researchers](https://www.mathworks.com/company/aboutus/policies_statements/vulnerability-disclosure-policy.html)
6+
for additional information.

base_python_example_21a.slx

30 KB
Binary file not shown.

detectHuman.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import time
2+
import imutils
3+
import numpy as np
4+
from cv2 import cv2
5+
6+
7+
class hogObject:
8+
detector = cv2.HOGDescriptor()
9+
def __init__(self):
10+
self.detector.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
11+
12+
13+
def getHogObject():
14+
return hogObject()
15+
16+
17+
def detectHumanFromFrame(image, hog):
18+
image = np.asarray(image)
19+
image = imutils.resize(image, width=min(400, image.shape[1]))
20+
21+
# Detecting all the regions in the Image that has a pedestrians inside it
22+
(regions, _) = hog.detector.detectMultiScale(
23+
image, winStride=(4, 4), padding=(4, 4), scale=1.05)
24+
25+
# Drawing the regions in the image
26+
for (x, y, w, h) in regions:
27+
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
28+
29+
return image
30+
31+
32+
if __name__ == "__main__":
33+
34+
hog = hogObject()
35+
36+
cap = cv2.VideoCapture('livedata.mp4')
37+
ret, image = cap.read()
38+
39+
t_end = time.time() + 10
40+
while time.time() < t_end:
41+
# Reading the video stream
42+
ret, image = cap.read()
43+
if ret:
44+
image = detectHumanFromFrame(image, hog)
45+
cv2.imshow("Image", image)
46+
cv2.waitKey(5)
47+
if cv2.waitKey(5) & 0xFF == ord('q'):
48+
exit()
49+
else:
50+
break
51+
52+
cap.release()
53+
cv2.destroyAllWindows()

license.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Copyright (c) 2020, The MathWorks, Inc.
2+
All rights reserved.
3+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
5+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
6+
3. In all cases, the software is, and all modifications and derivatives of the software shall be, licensed to you solely for use in conjunction with MathWorks products and service offerings.
7+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8+
9+
10+
11+

livedata.mp4

1.57 MB
Binary file not shown.

python_HumanDetector.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
classdef python_HumanDetector < matlab.System
2+
% Pre-computed constants
3+
properties(Access = private, Nontunable)
4+
hog
5+
end
6+
7+
methods(Access = protected)
8+
function setupImpl(obj)
9+
% Perform one-time calculations, such as computing constants
10+
obj.hog = py.detectHuman.getHogObject();
11+
end
12+
13+
function y = stepImpl(obj,u)
14+
% Calculate y as a function of input u and discrete states.
15+
out = py.detectHuman.detectHumanFromFrame(u, obj.hog);
16+
y = uint8(out);
17+
end
18+
19+
function out = getOutputSizeImpl(obj)
20+
out = [300 400 3];
21+
end
22+
23+
function y1 = getOutputDataTypeImpl(obj)
24+
y1 = 'uint8';
25+
end
26+
27+
function y1 = isOutputComplexImpl(~)
28+
y1 = false;
29+
end
30+
31+
function out = isOutputFixedSizeImpl(obj)
32+
out = true;
33+
end
34+
35+
end
36+
end

runme.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
%%%%%%%%%%%%%%%%%
2+
% Setting up python installer and getting packages
3+
% 1. install pip
4+
% 2. install opencv
5+
% 3. install imutils
6+
%
7+
% 1. intall pip (on Linux console)
8+
% sudo apt install python-pip
9+
%
10+
% 2. install opencv
11+
% python -m pip install opencv-python
12+
%
13+
% 3. install imutils
14+
% python -m pip install imutils
15+
16+
% MATLAB may crash with python process - use "OutOfProcess" execution mode
17+
% could avoid that
18+
pyenv("ExecutionMode","OutOfProcess");
19+
20+
py.importlib.import_module('detectHuman');
21+
22+
pathToPyfun = fileparts(which('detectHuman.py'));
23+
if count(py.sys.path,pathToPyfun) == 0
24+
insert(py.sys.path,int32(0),pathToPyfun);
25+
end

videoReader.m

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
classdef videoReader < matlab.System
2+
% Pre-computed constants
3+
properties(Access = private, Nontunable)
4+
v
5+
end
6+
7+
methods(Access = protected)
8+
function setupImpl(obj)
9+
% Perform one-time calculations, such as computing constants
10+
obj.v = VideoReader('livedata.mp4');
11+
end
12+
13+
function y = stepImpl(obj)
14+
% Calculate y as a function of input u and discrete states.
15+
if hasFrame(obj.v)
16+
out = readFrame(obj.v);
17+
else
18+
out = zeros(480, 640, 3);
19+
end
20+
21+
y = uint8(out);
22+
end
23+
24+
function out = getOutputSizeImpl(obj)
25+
out = [480 640 3];
26+
end
27+
28+
function y1 = getOutputDataTypeImpl(obj)
29+
y1 = 'uint8';
30+
end
31+
32+
function y1 = isOutputComplexImpl(~)
33+
y1 = false;
34+
end
35+
36+
function out = isOutputFixedSizeImpl(obj)
37+
out = true;
38+
end
39+
40+
end
41+
end

0 commit comments

Comments
 (0)