Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Remove imutils dependency
  • Loading branch information
yanndebray committed Jan 1, 2024
commit 2bab65eda89bcf5d2a515a487617e83da728d79f
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__/
slprj/
73 changes: 43 additions & 30 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
Integrate Python Code into Simulink for Simulation

Using Python Human Detection Algorithm in Simulink Example
Copyright 2012-2021 The MathWorks, Inc.

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:

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.


detectHuman.py: this file contains the Python algorithm using OpenCV Histogram of Oriented Gradients (HOG) for human detection.

livedata.mp4: this file is a pre-recorded video showing several people walking.

python_HumanDetector.m: this file contains the MATLAB System Object integrating the Python human detection algorithm.

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).

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.

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.

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.

Products needed for using this example :

MATLAB;
Simulink;
DSP System Toolbox (optional);
Computer Vision Toolbox (optional);
# Integrate Python Code into Simulink for Simulation

📹 Integrate OpenCV into Simulink models through Python wrapper:
Simulation of a Human Detection Algorithm

<img src="simCV.gif" width= 500 />

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:

* [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.


* [detectHuman.py](detectHuman.py): this file contains the Python algorithm using OpenCV Histogram of Oriented Gradients (HOG) for human detection.

* [livedata.mp4](livedata.mp4): this file is a pre-recorded video showing several people walking.

* [python_HumanDetector.m](python_HumanDetector.m): this file contains the MATLAB System Object integrating the Python human detection algorithm.

* [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.
(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.)

* [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.


![simCV-model.png](simCV-model.png)

## How to

To use this example,
1. open and run the runme.m first in MATLAB;
2. Then start the base_python_example_21a.slx
3. Run simulaiton.


## Product dependencies
Products needed for using this example :

* MATLAB;
* Simulink;
* DSP System Toolbox (optional);
* Computer Vision Toolbox (optional);

&reg; 2021-2024 The MathWorks, Inc.
361 changes: 361 additions & 0 deletions debug.ipynb

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions detectHuman.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time
import imutils
# import imutils
import numpy as np
from cv2 import cv2
import cv2


class hogObject:
Expand All @@ -16,7 +16,10 @@ def getHogObject():

def detectHumanFromFrame(image, hog):
image = np.asarray(image)
image = imutils.resize(image, width=min(400, image.shape[1]))
# image = imutils.resize(image, width=min(400, image.shape[1]))
ratio = image.shape[0] / image.shape[1]
new_height = int(400 * ratio)
image = cv2.resize(image, (400,new_height))

# Detecting all the regions in the Image that has a pedestrians inside it
(regions, _) = hog.detector.detectMultiScale(
Expand Down
22 changes: 11 additions & 11 deletions license.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Copyright (c) 2020, The MathWorks, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
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.
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.
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.
Copyright (c) 2020, The MathWorks, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
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.
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.
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.




1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
opencv-python
4 changes: 0 additions & 4 deletions runme.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@
% Setting up python installer and getting packages
% 1. install pip
% 2. install opencv
% 3. install imutils
%
% 1. intall pip (on Linux console)
% sudo apt install python-pip
%
% 2. install opencv
% python -m pip install opencv-python
%
% 3. install imutils
% python -m pip install imutils

% MATLAB may crash with python process - use "OutOfProcess" execution mode
% could avoid that
Expand Down
Binary file added simCV-model.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added simCV.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.