Build your OBJECT DETECTION SOFTWARE – Crash course
After watching this crash course about Object Detection software you will be able to create object detection software that can be easily used by anyone. Thanks to a simple graphic interface and the possibility of connecting external video resources, it will be possible to make the most of the potential of computer vision even by those who are not expert programmers.
Even tedious and lengthy installations of complicated libraries will no longer be necessary because your python software will be converted into a convenient .exe that can run on any windows computer.
1. Installations necessary for the Object Detection software
OpenCV is required for the use of Object Detection Software. You can install from the terminal using pypi.org with the command:
pip install opencv-contrib-python
Open the terminal (as shown in the image) and start the command to complete the installation. Now, you can go to the next step.
2. Object Detection
Identifying an object in Computer vision consists in surrounding the Object detected with a bounding box.
There are different object detection methods commonly used in computer vision implemented: detecting objects by their color, by their movement, or with deep learning.
To build an object detection software you need object recognition with Deep Learning. For this project, I used Tiny-Yolo with the aim of creating a light and fast software and COCO Dataset to recognize objects from a dataset of 80 things. You can see all the details on the official YOLO documentation with Darknet.
We will not see the details on object custom detection because it is not the goal of this Crash Course. If you are interested in this topic or want to carry out your own project with Deep Learning I recommend Object Detection (Opencv & Deep learning).
Follow the basic steps to integrate the deep learning model with Opencv and the entire software:
Object detection software using DNN module with OpenCV
I have already prepared the DNN model so you can download it from this crash_course_ods.zip link and put it in the root folder
Now open the main project file and call the DNN model with this code
... # Opencv DNN net = cv2.dnn.readNet("dnn_model/yolov4-tiny.weights", "dnn_model/yolov4-tiny.cfg") model = cv2.dnn_DetectionModel(net) ...
Retrieve video stream from the camera with OpenCV using these lines of code:
# Initialize camera cap = cv2.VideoCapture(0) ... while True: # Get frames ret, frame = cap.read()
Remember that Opencv considers video as a sequence of frames so it uses a While Loop to process frame by frame.
By analyzing the frames captured by OpenCV with the DNN Model it is possible to identify the objects present in that specific frame. As output, we will have: the class, the recognition percentage, the points in the frame that allow us to draw a box around the object and precisely identify its position.
# Object Detection (class_ids, scores, bboxes) = model.detect(frame, confThreshold=0.3, nmsThreshold=.4) for class_id, score, bbox in zip(class_ids, scores, bboxes): (x, y, w, h) = bbox class_name = classes[class_id] color = colors[class_id]
3. Simple user interface (add buttons)
To use our object detection software more comfortably we can add command buttons. The best solution would be to use PyQt but for simplicity, I just used the OpenCV functions to generate the buttons.
Find the gui_buttons.py file with the functions among the files available for download. Just add it to the main file.
from gui_buttons import Buttons # Initialize Buttons button = Buttons() button.add_button("person", 20, 20) button.add_button("cell phone", 20, 100) button.add_button("keyboard", 20, 180) button.add_button("remote", 20, 260) button.add_button("scissors", 20, 340) colors = button.colors ...
Capture mouse click with this function
def click_button(event, x, y, flags, params): global button_person if event == cv2.EVENT_LBUTTONDOWN: button.button_click(x, y)
Finally, it shows the buttons on the screen
# Display buttons button.display_buttons(frame)
As you can see from the image down here, the buttons on the left are shown. If the function is active the button is red with white writing, otherwise, it is an empty rectangle with red writing.
4. Convert to Window Executable (.exe file)
Converting our object detection software into a .exe file is very simple using the cx_freeze library. Being a python library it can be installed with a command line
pip install cx_freeze
if everything goes well you will not get errors.
How to prepare files for cx_freeze
Create a new file called setup.py and enter your software information. In the parameter of “Executable ()” insert the path of your main file, here is a code example
import sys from cx_Freeze import setup, Executable setup(name="Simple Object Detection Software", version="0.1", description="This software detects objects in realtime", executables=[Executable("main.py")] )
Save and put it in the folder of your main file. I recommend that you see the documentation for more customizations.
Build command line
If you want to compile from the command line, just enter the path to the setup.py file and the build command on cmd.
python c:/program/setup.py build
I remind you that it will compile with the version of python you are using and all the libraries associated with your python will be used. If you don’t get any errors, you’re done and can skip the next step.
Or Build with Pycharm
Alternatively, you can compile with Pycharm. Open the configuration file in Pycharm and at the top right click on “Edit configuration” as in the image below.
In the configuration window that opens, write “build” in the parameters and click on “Apply” to confirm.
Final result and main.exe
If the previous steps didn’t give any errors you will get a folder called “build”. Now copy inside the “dnn_model” folder that contains the object recognition model. In the image, you see the result to be obtained
You will have the final folder called “exe.win-amd64-3.9” with the main.exe file inside. To run the program, the folder and its contents are sufficient, the end-user can use the program without installing python or special libraries.
Detect custom object with your Object detection software
Recognizing objects is the fundamental part of computer vision. For the sake of time, we have used a pre-set object recognition model in this course. If you want object recognition and custom object tracking, I recommend that you see my course Object Recognition with deep learning
Hi there, I’m the founder of Pysource.
I’m a Computer Vision Consultant, developer and Course instructor.
I help Companies and Freelancers to easily and efficiently build Computer Vision Software.
Learn to build Computer Vision Software easily and efficiently.
This is a FREE Workshop where I'm going to break down the 4 steps that are necessary to build software to detect and track any object.Sign UP for FREE
Detect and Track any Object (Full Videocourse)
You can Build Software to detect and track any Object even if you have a basic programming knowledge.