Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 28 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
* text=auto

*.fig binary
*.mat binary
*.mdl binary diff merge=mlAutoMerge
*.mdlp binary
*.mex* binary
*.mlapp binary
*.mldatx binary
*.mlproj binary
*.mlx binary
*.p binary
*.sfx binary
*.sldd binary
*.slreqx binary merge=mlAutoMerge
*.slmx binary merge=mlAutoMerge
*.sltx binary
*.slxc binary
*.slx binary merge=mlAutoMerge
*.slxp binary

## Other common binary file types
*.docx binary
*.exe binary
*.jpg binary
*.pdf binary
*.png binary
*.xlsx binary
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__/
slprj/
Binary file added HumanDetection.slx
Binary file not shown.
33 changes: 33 additions & 0 deletions HumanDetection_import.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"SimulinkCodeImporterVersion": 1,
"LibraryFileName": "HumanDetection",
"CustomCode": {
"SourceFiles": ".\\detectHuman.py",
"GlobalVariableInterface": false
},
"FunctionsToImport": "detectHuman.detectHumanFromFrame",
"FunctionSettings": {
"Name": "detectHuman.detectHumanFromFrame",
"PortSpecArray": [
{
"ArgName": "image",
"PortName": "image",
"Scope": "Output",
"Index": "1",
"Type": "uint8",
"Size": "[300 400 3]",
"IsGlobal": false
},
{
"ArgName": "image",
"PortName": "image",
"Scope": "Input",
"Index": "1",
"Type": "uint8",
"Size": "[480 640 3]",
"IsGlobal": false
}
],
"IsDeterministic": false
}
}
79 changes: 49 additions & 30 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
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

[![Open in MATLAB Online](https://www.mathworks.com/images/responsive/global/open-in-matlab-online.svg)](https://matlab.mathworks.com/open/github/v1?repo=yanndebray/Integrate_Python_code_with_Simulink&file=README.mlx)

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

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

```matlab
open_system("base_python_example_23a.slx")
```

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


## Product dependencies
Products needed for using this example :

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

&reg; 2021-2024 The MathWorks, Inc.
Binary file added README.mlx
Binary file not shown.
Binary file added base_python_example_23a.slx
Binary file not shown.
Binary file added base_python_example_23a_with_toolboxes.slx
Binary file not shown.
361 changes: 361 additions & 0 deletions debug.ipynb

Large diffs are not rendered by default.

28 changes: 11 additions & 17 deletions detectHuman.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import time
import imutils
# import imutils
import numpy as np
from cv2 import cv2
import cv2
import hogClass

def detectHumanFromFrame(image):

class hogObject:
detector = cv2.HOGDescriptor()
def __init__(self):
self.detector.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())


def getHogObject():
return hogObject()


def detectHumanFromFrame(image, hog):
hog = hogClass.hogObject()

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 All @@ -30,8 +26,6 @@ def detectHumanFromFrame(image, hog):


if __name__ == "__main__":

hog = hogObject()

cap = cv2.VideoCapture('livedata.mp4')
ret, image = cap.read()
Expand All @@ -41,7 +35,7 @@ def detectHumanFromFrame(image, hog):
# Reading the video stream
ret, image = cap.read()
if ret:
image = detectHumanFromFrame(image, hog)
image = detectHumanFromFrame(image)
cv2.imshow("Image", image)
cv2.waitKey(5)
if cv2.waitKey(5) & 0xFF == ord('q'):
Expand Down
33 changes: 33 additions & 0 deletions detectHuman_detectHumanFromFrame.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
classdef detectHuman_detectHumanFromFrame < matlab.System
% Python def detectHumanFromFrame imported from python module detectHuman.py
methods(Access = protected)
function validateInputsImpl(obj, varargin)
if ~isempty(varargin{1})
validateattributes(varargin{1}, {'uint8'}, {'size',[480 640 3]});
end
end
function setupImpl(obj)
py.importlib.import_module('detectHuman');
end
function [image] = stepImpl(obj,image)
image = uint8(py.detectHuman.detectHumanFromFrame(image));
end
function varargout = getOutputDataTypeImpl(obj)
varargout{1} = 'uint8';
end
function varargout = getOutputSizeImpl(obj)
varargout{1} = [300 400 3];
end
function varargout = isOutputComplexImpl(obj)
varargout{1} = false;
end
function varargout = isOutputFixedSizeImpl(obj)
varargout{1} = true;
end
end
methods(Static, Access = protected)
function simMode = getSimulateUsingImpl
simMode = 'Interpreted execution';
end
end
end
10 changes: 10 additions & 0 deletions hogClass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import cv2

class hogObject:
detector = cv2.HOGDescriptor()
def __init__(self):
self.detector.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())


def getHogObject():
return hogObject()
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.




2 changes: 2 additions & 0 deletions pySimCV.prj
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<MATLABProject xmlns="http://www.mathworks.com/MATLABProjectFile"/>
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
opencv-python
numpy
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info Ref="" Type="Relative"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="556b23f8-b46f-40bc-ba39-5688323b44fd" type="Reference"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info File="runme.m" GroupUUID="default" Icon="" Name="runme" Type="StartUp" Visible="0">
<Extension Name="StartUpPrev" Value="HEAD"/>
</Info>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="89a2a5cc-1383-4cf9-8298-ba7852a76583" type="EntryPoint"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info Name="Test" ReadOnly="READ_ONLY"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="test" type="Label"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info Name="Other" ReadOnly="READ_ONLY"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="other" type="Label"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info Name="Convenience" ReadOnly="READ_ONLY"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="convenience" type="Label"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info Name="None" ReadOnly="READ_ONLY"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="none" type="Label"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info Name="Derived" ReadOnly="READ_ONLY"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="derived" type="Label"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info Name="Design" ReadOnly="READ_ONLY"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="design" type="Label"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info Name="Artifact" ReadOnly="READ_ONLY"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="artifact" type="Label"/>
2 changes: 2 additions & 0 deletions resources/project/Project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info MetadataType="fixedPathV2"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info DataType="None" Name="Classification" ReadOnly="1" SingleValued="1"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="FileClassCategory" type="Category"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info>
<Category UUID="FileClassCategory">
<Label UUID="design"/>
</Category>
</Info>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="setup_python.m" type="File"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="license.txt" type="File"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="README.MD" type="File"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info>
<Category UUID="FileClassCategory">
<Label UUID="design"/>
</Category>
</Info>
Loading