Skip to content

Commit 2bab65e

Browse files
committed
Remove imutils dependency
1 parent efafff7 commit 2bab65e

9 files changed

+424
-48
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__pycache__/
2+
slprj/

README.MD

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,43 @@
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);
1+
# Integrate Python Code into Simulink for Simulation
2+
3+
📹 Integrate OpenCV into Simulink models through Python wrapper:
4+
Simulation of a Human Detection Algorithm
5+
6+
<img src="simCV.gif" width= 500 />
7+
8+
This example shows users how to integrate Python Human detection code into Simulink for simulation. This example reads a pre-recorded video, apply the human detection algorithm defined in Python and generate the outpur video with human marked. This example includes the following files:
9+
10+
* [runme.m](runme.m): This file provides the prep steps when using this example. Since the Python algorithm is based on OpenCV, you will need 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. And it will also set the Python path to the current folder.
11+
12+
13+
* [detectHuman.py](detectHuman.py): this file contains the Python algorithm using OpenCV Histogram of Oriented Gradients (HOG) for human detection.
14+
15+
* [livedata.mp4](livedata.mp4): this file is a pre-recorded video showing several people walking.
16+
17+
* [python_HumanDetector.m](python_HumanDetector.m): this file contains the MATLAB System Object integrating the Python human detection algorithm.
18+
19+
* [videoReader.m](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.
20+
(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.)
21+
22+
* [base_python_example_21a.slx](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.
23+
24+
25+
![simCV-model.png](simCV-model.png)
26+
27+
## How to
28+
29+
To use this example,
30+
1. open and run the runme.m first in MATLAB;
31+
2. Then start the base_python_example_21a.slx
32+
3. Run simulaiton.
33+
34+
35+
## Product dependencies
36+
Products needed for using this example :
37+
38+
* MATLAB;
39+
* Simulink;
40+
* DSP System Toolbox (optional);
41+
* Computer Vision Toolbox (optional);
42+
43+
&reg; 2021-2024 The MathWorks, Inc.

debug.ipynb

Lines changed: 361 additions & 0 deletions
Large diffs are not rendered by default.

detectHuman.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import time
2-
import imutils
2+
# import imutils
33
import numpy as np
4-
from cv2 import cv2
4+
import cv2
55

66

77
class hogObject:
@@ -16,7 +16,10 @@ def getHogObject():
1616

1717
def detectHumanFromFrame(image, hog):
1818
image = np.asarray(image)
19-
image = imutils.resize(image, width=min(400, image.shape[1]))
19+
# image = imutils.resize(image, width=min(400, image.shape[1]))
20+
ratio = image.shape[0] / image.shape[1]
21+
new_height = int(400 * ratio)
22+
image = cv2.resize(image, (400,new_height))
2023

2124
# Detecting all the regions in the Image that has a pedestrians inside it
2225
(regions, _) = hog.detector.detectMultiScale(

license.txt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +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-
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+

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
opencv-python

runme.m

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22
% Setting up python installer and getting packages
33
% 1. install pip
44
% 2. install opencv
5-
% 3. install imutils
65
%
76
% 1. intall pip (on Linux console)
87
% sudo apt install python-pip
98
%
109
% 2. install opencv
1110
% python -m pip install opencv-python
12-
%
13-
% 3. install imutils
14-
% python -m pip install imutils
1511

1612
% MATLAB may crash with python process - use "OutOfProcess" execution mode
1713
% could avoid that

simCV-model.png

52.7 KB
Loading

simCV.gif

6.65 MB
Loading

0 commit comments

Comments
 (0)