Object Detection with 10 lines of code - Image AI

To perform object detection using ImageAI, all you need to do is

  1. Install Python on your computer system
  2. Install ImageAI and its dependencies

3. Download the Object Detection model file

4. Run the sample codes (which is as few as 10 lines)

Now let’s get started.

1) Download and install Python 3 from official Python Language website

https://python.org

2) Install the following dependencies via pip:

i. Tensorflow

pip install tensorflow

ii. Numpy

pip install numpy

iii. SciPy

pip install scipy

iv. OpenCV

pip install opencv-python

v. Pillow

pip install pillow

vi. Matplotlib

pip install matplotlib

vii. H5py

pip install h5py

viii. Keras

pip install keras

ix. ImageAI

pip installhttps://github.com/OlafenwaMoses/ImageAI/releases/download/2.0.1/imageai-2.0.1-py3-none-any.whl

3) Download the RetinaNet model file that will be used for object detection via this link.

Great. Now that you have installed the dependencies, you are ready to write your first object detection code. Create a Python file and give it a name (For example, FirstDetection.py), and then write the code below into it. Copy the RetinaNet model file and the image you want to detect to the folder that contains the python file.

FirstDetection.py

from imageai.Detection import ObjectDetection
import os

execution_path = os.getcwd()

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "image.jpg"), output_image_path=os.path.join(execution_path , "imagenew.jpg"))

for eachObject in detections:
    print(eachObject["name"] + " : " + eachObject["percentage_probability"] )

  

Then run the code and wait while the results prints in the console. Once the result is printed to the console, go to the folder in which your FirstDetection.py is and you will find a new image saved. Take a look at a 2 image samples below and the new images saved after detection.

Before Detection:

Image Credit: alzheimers.co.uk

Image Credit: Wikicommons

After Detection:

Console result for above image:

person : 55.8402955532074

person : 53.21805477142334

person : 69.25139427185059

person : 76.41745209693909

bicycle : 80.30363917350769

person : 83.58567953109741

person : 89.06581997871399

truck : 63.10953497886658

person : 69.82483863830566

person : 77.11606621742249

bus : 98.00949096679688

truck : 84.02870297431946

car : 71.98476791381836

Console result for above image:

person : 71.10445499420166

person : 59.28672552108765

person : 59.61582064628601

person : 75.86382627487183

motorcycle : 60.1050078868866

bus : 99.39600229263306

car : 74.05484318733215

person : 67.31776595115662

person : 63.53200078010559

person : 78.2265305519104

person : 62.880998849868774

person : 72.93365597724915

person : 60.01397967338562

person : 81.05944991111755

motorcycle : 50.591760873794556

motorcycle : 58.719027042388916

person : 71.69321775436401

bicycle : 91.86570048332214

motorcycle : 85.38855314254761

Now let us explain how the 10-line code works.

from imageai.Detection import ObjectDetection
import os

execution_path = os.getcwd()

  

In the above 3 lines, we imported the ImageAI object detection class in the first line, imported the python os class in the second line and defined a variable to hold the path to the folder where our python file, RetinaNet model file and images are in the third line.

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "image.jpg"), output_image_path=os.path.join(execution_path , "imagenew.jpg"))

  

In the 5 lines of code above, we defined our object detection class in the first line, set the model type to RetinaNet in the second line, set the model path to the path of our RetinaNet model in the third line, load the model into the object detection class in the fourth line, then we called the detection function and parsed in the input image path and the output image path in the fifth line.

for eachObject in detections:
    print(eachObject["name"] + " : " + eachObject["percentage_probability"] )

  

In the above 2 lines of code, we iterate over all the results returned by the detector.detectObjectsFromImage function in the first line, then print out the name and percentage probability of the model on each object detected in the image in the second line.

ImageAI supports many powerful customization of the object detection process. One of it is the ability to extract the image of each object detected in the image. By simply parsing the extra parameter extract_detected_objects=True into the detectObjectsFromImagefunction as seen below, the object detection class will create a folder for the image objects, extract each image, save each to the new folder created and return an extra array that contains the path to each of the images.

detections, extracted_images = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "image.jpg"), output_image_path=os.path.join(execution_path , "imagenew.jpg"), extract_detected_objects=True)

  

原文地址:https://www.cnblogs.com/Javi/p/9293404.html

时间: 2024-08-30 11:26:09

Object Detection with 10 lines of code - Image AI的相关文章

Install Tensorflow object detection API in Anaconda (Windows)

This blog is to explain how to install Tensorflow object detection API in Anaconda in Windows 10 as well as how to train train a convolution neural network to do object detection on your own data set. Steps: 1. Installation and Configuration Install

中文版 R-FCN: Object Detection via Region-based Fully Convolutional Networks

R-FCN: Object Detection via Region-based Fully Convolutional Networks 摘要 我们提出了基于区域的全卷积网络,以实现准确和高效的目标检测.与先前的基于区域的检测器(如Fast/Faster R-CNN [6,18])相比,这些检测器应用昂贵的每个区域子网络数百次,我们的基于区域的检测器是全卷积的,几乎所有计算都在整张图像上共享.为了实现这一目标,我们提出了位置敏感分数图,以解决图像分类中的平移不变性与目标检测中的平移变化之间的困

TensorFlow object detection API应用二

前一篇讲述了TensorFlow object detection API的安装与配置,现在我们尝试用这个API搭建自己的目标检测模型. 一.准备数据集 本篇旨在人脸识别,在百度图片上下载了120张张钧甯的图片,存放在/models/research/object_detection下新建的images文件夹内,images文件夹下新建train和test两个文件夹,然后将120分为100和20张分别存放在train和test中. 接下来使用 LabelImg 这款小软件,对train和test

Tensorflow object detection API 搭建属于自己的物体识别模型

一.下载Tensorflow object detection API工程源码 网址:https://github.com/tensorflow/models,可通过Git下载,打开Git Bash,输入git clone https://github.com/tensorflow/models.git进行下载. 二.标记需要训练的图片 ①.在第一步下载的工程文件models\research\object_detection目录下,建立一个my_test_images用来放测试test和训练t

CV:object detection(Haar)

一. Haar分类器的前世今生 人脸检测属于计算机视觉的范畴,早期人们的主要研究方向是人脸识别,即根据人脸来识别人物的身份,后来在复杂背景下的人脸检测需求越来越大,人脸检测也逐渐作为一个单独的研究方向发展起来. 目前的人脸检测方法主要有两大类:基于知识和基于统计. "基于知识的方法主要利用先验知识将人脸看作器官特征的组合,根据眼睛.眉毛.嘴巴.鼻子等器官的特征以及相互之间的几何位置关系来检测人脸.基于统计的方法则将人脸看作一个整体的模式--二维像素矩阵,从统计的观点通过大量人脸图像样本构造人脸模

论文阅读(Chenyi Chen——【ACCV2016】R-CNN for Small Object Detection)

Chenyi Chen--[ACCV2016]R-CNN for Small Object Detection 目录 作者和相关链接 方法概括 创新点和贡献 方法细节 实验结果 总结与收获点 参考文献 作者和相关链接 论文下载 Chenyi Chen , Ming-Yu Liu, Jianxiong Xiao 所有作者的简单信息 方法概括 这篇文章主要讨论针对小目标的目标检测 文章为了证明:对传统的R-CNN style的方法进行改进,可以用于小目标检测,并且性能比DPM方法好 整个检测流程:

(转)Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks(更快的RCNN:通过区域提议网络实现实时)

原文出处 感谢作者~ Faster R-CNN: Towards Real-Time Object Detection with Region ProposalNetworks Shaoqing Ren, Kaiming He, Ross Girshick, Jian Sun 摘要 目前最先进的目标检测网络需要先用区域建议算法推测目标位置,像SPPnet[7]和Fast R-CNN[5]这些网络已经减少了检测网络的运行时间,这时计算区域建议就成了瓶颈问题.本文中,我们介绍一种区域建议网络(Reg

论文笔记:Faster R-CNN:Towards Real-Time Object Detection with Region Proposal Networks

文章: http://arxiv.org/abs/1506.01497 源码:坐等.... 目录: Region Proposal Networks Translation-Invariant Anchors A Loss Function for Learning Region Proposals Optimization Sharing Convolutional Features for Region Proposal and Object Detection Implementation

论文阅读--Scalable Object Detection using Deep Neural Networks

Scalable Object Detection using Deep Neural Networks 作者: Dumitru Erhan, Christian Szegedy, Alexander Toshev, and Dragomir Anguelov 引用: Erhan, Dumitru, et al. "Scalable object detection using deep neural networks." Proceedings of the IEEE Confere