You are on page 1of 7

0.

Download FIles

In [1]:

!wget https://gitlab.com/andreass.bayu/imageai-repo/-/raw/master/yolo-tiny.h5
!wget https://gitlab.com/andreass.bayu/imageai-repo/-/raw/master/resnet50_coco_best_v2.1
.0.h5
!wget https://gitlab.com/andreass.bayu/imageai-repo/-/raw/master/yolo.h5

--2021-04-19 10:10:13-- https://gitlab.com/andreass.bayu/imageai-repo/-/raw/master/yolo-


tiny.h5
Resolving gitlab.com (gitlab.com)... 172.65.251.78, 2606:4700:90:0:f22e:fbec:5bed:a9b9
Connecting to gitlab.com (gitlab.com)|172.65.251.78|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 35555784 (34M) [application/octet-stream]
Saving to: ‘yolo-tiny.h5’

yolo-tiny.h5 100%[===================>] 33.91M 9.75MB/s in 4.2s

2021-04-19 10:10:18 (8.07 MB/s) - ‘yolo-tiny.h5’ saved [35555784/35555784]

--2021-04-19 10:10:18-- https://gitlab.com/andreass.bayu/imageai-repo/-/raw/master/resne


t50_coco_best_v2.1.0.h5
Resolving gitlab.com (gitlab.com)... 172.65.251.78, 2606:4700:90:0:f22e:fbec:5bed:a9b9
Connecting to gitlab.com (gitlab.com)|172.65.251.78|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 152662144 (146M) [application/octet-stream]
Saving to: ‘resnet50_coco_best_v2.1.0.h5’

resnet50_coco_best_ 100%[===================>] 145.59M 14.2MB/s in 11s

2021-04-19 10:10:29 (13.5 MB/s) - ‘resnet50_coco_best_v2.1.0.h5’ saved [152662144/1526621


44]

--2021-04-19 10:10:29-- https://gitlab.com/andreass.bayu/imageai-repo/-/raw/master/yolo.


h5
Resolving gitlab.com (gitlab.com)... 172.65.251.78, 2606:4700:90:0:f22e:fbec:5bed:a9b9
Connecting to gitlab.com (gitlab.com)|172.65.251.78|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 248686624 (237M) [application/octet-stream]
Saving to: ‘yolo.h5’

yolo.h5 100%[===================>] 237.17M 245MB/s in 1.0s

2021-04-19 10:10:30 (245 MB/s) - ‘yolo.h5’ saved [248686624/248686624]

In [2]:
!wget https://gitlab.com/andreass.bayu/imageai-repo/-/raw/master/1_v0Bm-HQxWtpbQ0Yq463uq
w.jpeg
!mv 1_v0Bm-HQxWtpbQ0Yq463uqw.jpeg image-1.jpeg

!wget https://gitlab.com/andreass.bayu/imageai-repo/-/raw/master/object-detection-with-i
mageai-python-1.jpg
!mv object-detection-with-imageai-python-1.jpg image-2.jpg

--2021-04-19 10:10:30-- https://gitlab.com/andreass.bayu/imageai-repo/-/raw/master/1_v0B


m-HQxWtpbQ0Yq463uqw.jpeg
Resolving gitlab.com (gitlab.com)... 172.65.251.78, 2606:4700:90:0:f22e:fbec:5bed:a9b9
Connecting to gitlab.com (gitlab.com)|172.65.251.78|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 106900 (104K) [image/jpeg]
Saving to: ‘1_v0Bm-HQxWtpbQ0Yq463uqw.jpeg’

1_v0Bm-HQxWtpbQ0Yq4 100%[===================>] 104.39K --.-KB/s in 0.02s

2021-04-19 10:10:31 (6.61 MB/s) - ‘1_v0Bm-HQxWtpbQ0Yq463uqw.jpeg’ saved [106900/106900]


2021-04-19 10:10:31 (6.61 MB/s) - ‘1_v0Bm-HQxWtpbQ0Yq463uqw.jpeg’ saved [106900/106900]

--2021-04-19 10:10:31-- https://gitlab.com/andreass.bayu/imageai-repo/-/raw/master/objec


t-detection-with-imageai-python-1.jpg
Resolving gitlab.com (gitlab.com)... 172.65.251.78, 2606:4700:90:0:f22e:fbec:5bed:a9b9
Connecting to gitlab.com (gitlab.com)|172.65.251.78|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 84512 (83K) [image/jpeg]
Saving to: ‘object-detection-with-imageai-python-1.jpg’

object-detection-wi 100%[===================>] 82.53K --.-KB/s in 0.005s

2021-04-19 10:10:31 (15.9 MB/s) - ‘object-detection-with-imageai-python-1.jpg’ saved [845


12/84512]

In [4]:
!pip install tensorflow==2.4.0
!pip install opencv-python
!pip install keras
!pip install imageAI

Collecting tensorflow==2.4.0
Downloading https://files.pythonhosted.org/packages/94/0a/012cc33c643d844433d13001dd1db
179e7020b05ddbbd0a9dc86c38a8efa/tensorflow-2.4.0-cp37-cp37m-manylinux2010_x86_64.whl (394
.7MB)
|████████████████████████████████| 394.7MB 38kB/s
Requirement already satisfied: termcolor~=1.1.0 in /usr/local/lib/python3.7/dist-packages
(from tensorflow==2.4.0) (1.1.0)
Requirement already satisfied: absl-py~=0.10 in /usr/local/lib/python3.7/dist-packages (f
rom tensorflow==2.4.0) (0.12.0)
Requirement already satisfied: flatbuffers~=1.12.0 in /usr/local/lib/python3.7/dist-packa
ges (from tensorflow==2.4.0) (1.12)
Requirement already satisfied: tensorboard~=2.4 in /usr/local/lib/python3.7/dist-packages
(from tensorflow==2.4.0) (2.4.1)
Requirement already satisfied: tensorflow-estimator<2.5.0,>=2.4.0rc0 in /usr/local/lib/py
thon3.7/dist-packages (from tensorflow==2.4.0) (2.4.0)
Requirement already satisfied: h5py~=2.10.0 in /usr/local/lib/python3.7/dist-packages (fr
om tensorflow==2.4.0) (2.10.0)
Requirement already satisfied: google-pasta~=0.2 in /usr/local/lib/python3.7/dist-package
s (from tensorflow==2.4.0) (0.2.0)
Requirement already satisfied: gast==0.3.3 in /usr/local/lib/python3.7/dist-packages (fro
m tensorflow==2.4.0) (0.3.3)
Requirement already satisfied: keras-preprocessing~=1.1.2 in /usr/local/lib/python3.7/dis
t-packages (from tensorflow==2.4.0) (1.1.2)
Requirement already satisfied: wrapt~=1.12.1 in /usr/local/lib/python3.7/dist-packages (f
rom tensorflow==2.4.0) (1.12.1)
Requirement already satisfied: protobuf>=3.9.2 in /usr/local/lib/python3.7/dist-packages
(from tensorflow==2.4.0) (3.12.4)
Requirement already satisfied: typing-extensions~=3.7.4 in /usr/local/lib/python3.7/dist-
packages (from tensorflow==2.4.0) (3.7.4.3)
Requirement already satisfied: numpy~=1.19.2 in /usr/local/lib/python3.7/dist-packages (f
rom tensorflow==2.4.0) (1.19.5)
Requirement already satisfied: opt-einsum~=3.3.0 in /usr/local/lib/python3.7/dist-package
s (from tensorflow==2.4.0) (3.3.0)
Requirement already satisfied: grpcio~=1.32.0 in /usr/local/lib/python3.7/dist-packages (
from tensorflow==2.4.0) (1.32.0)
Requirement already satisfied: astunparse~=1.6.3 in /usr/local/lib/python3.7/dist-package
s (from tensorflow==2.4.0) (1.6.3)
Requirement already satisfied: six~=1.15.0 in /usr/local/lib/python3.7/dist-packages (fro
m tensorflow==2.4.0) (1.15.0)
Requirement already satisfied: wheel~=0.35 in /usr/local/lib/python3.7/dist-packages (fro
m tensorflow==2.4.0) (0.36.2)
Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /usr/local/lib/python3
.7/dist-packages (from tensorboard~=2.4->tensorflow==2.4.0) (0.4.4)
Requirement already satisfied: setuptools>=41.0.0 in /usr/local/lib/python3.7/dist-packag
es (from tensorboard~=2.4->tensorflow==2.4.0) (54.2.0)
Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /usr/local/lib/python3.7/
dist-packages (from tensorboard~=2.4->tensorflow==2.4.0) (1.8.0)
Requirement already satisfied: google-auth<2,>=1.6.3 in /usr/local/lib/python3.7/dist-pac
kages (from tensorboard~=2.4->tensorflow==2.4.0) (1.28.1)
Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.7/dist-package
Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.7/dist-package
s (from tensorboard~=2.4->tensorflow==2.4.0) (1.0.1)
Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.7/dist-packa
ges (from tensorboard~=2.4->tensorflow==2.4.0) (2.23.0)
Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.7/dist-packages
(from tensorboard~=2.4->tensorflow==2.4.0) (3.3.4)
Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.7/dist-
packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard~=2.4->tensorflow==2.4.0) (1.
3.0)
Requirement already satisfied: rsa<5,>=3.1.4; python_version >= "3.6" in /usr/local/lib/p
ython3.7/dist-packages (from google-auth<2,>=1.6.3->tensorboard~=2.4->tensorflow==2.4.0)
(4.7.2)
Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.7/dist-pac
kages (from google-auth<2,>=1.6.3->tensorboard~=2.4->tensorflow==2.4.0) (0.2.8)
Requirement already satisfied: cachetools<5.0,>=2.0.0 in /usr/local/lib/python3.7/dist-pa
ckages (from google-auth<2,>=1.6.3->tensorboard~=2.4->tensorflow==2.4.0) (4.2.1)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/
python3.7/dist-packages (from requests<3,>=2.21.0->tensorboard~=2.4->tensorflow==2.4.0) (
1.24.3)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-package
s (from requests<3,>=2.21.0->tensorboard~=2.4->tensorflow==2.4.0) (3.0.4)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (fr
om requests<3,>=2.21.0->tensorboard~=2.4->tensorflow==2.4.0) (2.10)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packag
es (from requests<3,>=2.21.0->tensorboard~=2.4->tensorflow==2.4.0) (2020.12.5)
Requirement already satisfied: importlib-metadata; python_version < "3.8" in /usr/local/l
ib/python3.7/dist-packages (from markdown>=2.6.8->tensorboard~=2.4->tensorflow==2.4.0) (3
.10.1)
Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.7/dist-packages
(from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard~=2.4->tenso
rflow==2.4.0) (3.1.0)
Requirement already satisfied: pyasn1>=0.1.3 in /usr/local/lib/python3.7/dist-packages (f
rom rsa<5,>=3.1.4; python_version >= "3.6"->google-auth<2,>=1.6.3->tensorboard~=2.4->tens
orflow==2.4.0) (0.4.8)
Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.7/dist-packages (from
importlib-metadata; python_version < "3.8"->markdown>=2.6.8->tensorboard~=2.4->tensorflow
==2.4.0) (3.4.1)
Installing collected packages: tensorflow
Found existing installation: tensorflow 2.4.1
Uninstalling tensorflow-2.4.1:
Successfully uninstalled tensorflow-2.4.1
Successfully installed tensorflow-2.4.0
Requirement already satisfied: opencv-python in /usr/local/lib/python3.7/dist-packages (4
.1.2.30)
Requirement already satisfied: numpy>=1.14.5 in /usr/local/lib/python3.7/dist-packages (f
rom opencv-python) (1.19.5)
Requirement already satisfied: keras in /usr/local/lib/python3.7/dist-packages (2.4.3)
Requirement already satisfied: pyyaml in /usr/local/lib/python3.7/dist-packages (from ker
as) (3.13)
Requirement already satisfied: scipy>=0.14 in /usr/local/lib/python3.7/dist-packages (fro
m keras) (1.4.1)
Requirement already satisfied: h5py in /usr/local/lib/python3.7/dist-packages (from keras
) (2.10.0)
Requirement already satisfied: numpy>=1.9.1 in /usr/local/lib/python3.7/dist-packages (fr
om keras) (1.19.5)
Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from h5py->
keras) (1.15.0)
Collecting imageAI
Downloading https://files.pythonhosted.org/packages/73/44/3d5d8ef572888025666eec284e85f
9243faf06ca8c12085dcff1ca9754ed/imageai-2.1.6-py3-none-any.whl (160kB)
|████████████████████████████████| 163kB 3.9MB/s
Collecting keras-resnet==0.2.0
Downloading https://files.pythonhosted.org/packages/76/d4/a35cbd07381139dda4db42c81b88c
59254faac026109022727b45b31bcad/keras-resnet-0.2.0.tar.gz
Collecting numpy==1.19.3
Downloading https://files.pythonhosted.org/packages/65/b3/07864c89acb2a86df6f2e8c9bf091
ec5916da58dd3ce3a633a51a02c115e/numpy-1.19.3-cp37-cp37m-manylinux2010_x86_64.whl (14.9MB)
|████████████████████████████████| 14.9MB 331kB/s
Requirement already satisfied: keras==2.4.3 in /usr/local/lib/python3.7/dist-packages (fr
om imageAI) (2.4.3)
Requirement already satisfied: scipy==1.4.1 in /usr/local/lib/python3.7/dist-packages (fr
om imageAI) (1.4.1)
Requirement already satisfied: h5py==2.10.0 in /usr/local/lib/python3.7/dist-packages (fr
Requirement already satisfied: h5py==2.10.0 in /usr/local/lib/python3.7/dist-packages (fr
om imageAI) (2.10.0)
Requirement already satisfied: opencv-python in /usr/local/lib/python3.7/dist-packages (f
rom imageAI) (4.1.2.30)
Collecting matplotlib==3.3.2
Downloading https://files.pythonhosted.org/packages/87/a6/8d7d06f6b69236a3c1818157875ce
b1259ba0d9df4194f4fe138ffdc0f41/matplotlib-3.3.2-cp37-cp37m-manylinux1_x86_64.whl (11.6MB
)
|████████████████████████████████| 11.6MB 46.8MB/s
Collecting pillow==7.0.0
Downloading https://files.pythonhosted.org/packages/f5/79/b2d5695d1a931474fa68b68ec93bd
f08ba9acbc4d6b3b628eb6aac81d11c/Pillow-7.0.0-cp37-cp37m-manylinux1_x86_64.whl (2.1MB)
|████████████████████████████████| 2.1MB 42.5MB/s
Requirement already satisfied: pyyaml in /usr/local/lib/python3.7/dist-packages (from ker
as==2.4.3->imageAI) (3.13)
Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from h5py==
2.10.0->imageAI) (1.15.0)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (fr
om matplotlib==3.3.2->imageAI) (0.10.0)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.3 in /usr/local/lib
/python3.7/dist-packages (from matplotlib==3.3.2->imageAI) (2.4.7)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-package
s (from matplotlib==3.3.2->imageAI) (1.3.1)
Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-pack
ages (from matplotlib==3.3.2->imageAI) (2.8.1)
Requirement already satisfied: certifi>=2020.06.20 in /usr/local/lib/python3.7/dist-packa
ges (from matplotlib==3.3.2->imageAI) (2020.12.5)
Building wheels for collected packages: keras-resnet
Building wheel for keras-resnet (setup.py) ... done
Created wheel for keras-resnet: filename=keras_resnet-0.2.0-py2.py3-none-any.whl size=2
0486 sha256=1b227c59639348dda9e6da42d067ea21f91ee3573b9edd7f23d6c2beba6b2fe7
Stored in directory: /root/.cache/pip/wheels/5f/09/a5/497a30fd9ad9964e98a1254d1e164bcd1
b8a5eda36197ecb3c
Successfully built keras-resnet
ERROR: datascience 0.10.6 has requirement folium==0.2.1, but you'll have folium 0.8.3 whi
ch is incompatible.
ERROR: bokeh 2.3.1 has requirement pillow>=7.1.0, but you'll have pillow 7.0.0 which is i
ncompatible.
ERROR: albumentations 0.1.12 has requirement imgaug<0.2.7,>=0.2.5, but you'll have imgaug
0.2.9 which is incompatible.
Installing collected packages: keras-resnet, numpy, pillow, matplotlib, imageAI
Found existing installation: numpy 1.19.5
Uninstalling numpy-1.19.5:
Successfully uninstalled numpy-1.19.5
Found existing installation: Pillow 7.1.2
Uninstalling Pillow-7.1.2:
Successfully uninstalled Pillow-7.1.2
Found existing installation: matplotlib 3.2.2
Uninstalling matplotlib-3.2.2:
Successfully uninstalled matplotlib-3.2.2
Successfully installed imageAI-2.1.6 keras-resnet-0.2.0 matplotlib-3.3.2 numpy-1.19.3 pil
low-7.0.0

2. Import Package
Impor kelas ObjectDetection dari library ImageAI.

In [5]:
from imageai.Detection import ObjectDetection

3. Buat instance dari class ObjectDetection


Sekarang Anda telah mengimpor pustaka imageAI dan kelas ObjectDetection , hal berikutnya adalah
membuat instance kelas ObjectDetection , seperti yang ditunjukkan di sini:

In [6]:
detector = ObjectDetection()

4. Membuat spesifikasi lokasi gambar dan model


Mari tentukan path atau lokasi dari gambar masukan, gambar keluaran, dan model kita.

In [7]:
tiny_path = "yolo-tiny.h5"
yolo_path = "yolo.h5"
rest_path = "resnet50_coco_best_v2.1.0.h5"
input_path = "image-2.jpg"
output_path = "newimage.jpg"

In [8]:
import cv2

input_img = cv2.imread(input_path)

5. Pilih fungsi deteksi objek


Setelah membuat instance kelas ObjectDetection, sekarang kita dapat memanggil berbagai fungsi dari kelas
ObjectDetection yang berisi fungsi berikut untuk memanggil model terlatih:
setModelTypeAsRetinaNet() , setModelTypeAsYOLOv3() , dan setModelTypeAsTinyYOLOv3() .

TUGAS SCREENSHOT

Tiny YOLO

In [9]:
detector.setModelTypeAsTinyYOLOv3()
detector.setModelPath(tiny_path)
detector.loadModel()

detection = detector.detectObjectsFromImage(input_image=input_img, input_type="array", o


utput_image_path=output_path)
from IPython.display import Image
Image(filename='newimage.jpg')
Out[9]:
YOLOv3

In [10]:
detector.setModelTypeAsYOLOv3()
detector.setModelPath(yolo_path)
detector.loadModel()

detection = detector.detectObjectsFromImage(input_image=input_img, input_type="array", o


utput_image_path=output_path)
from IPython.display import Image
Image(filename='newimage.jpg')
Out[10]:

RetinaNet

In [11]:
detector.setModelTypeAsTinyYOLOv3()
detector.setModelPath(rest_path)
detector.loadModel()

detection = detector.detectObjectsFromImage(input_image=input_img, input_type="array", o


utput_image_path=output_path)
from IPython.display import Image
Image(filename='newimage.jpg')
Out[11]:
In [11]:

You might also like