Anda di halaman 1dari 164

POLITEKNIK HARAPAN BERSAMA Edisi September 2023

MODUL PRAKTIKUM MATA KULIAH


COMPUTER VISION

Oleh :
Wildani Eko Nugroho, M.Kom
MODUL PRAKTIKUM MATA KULIAH
COMPUTER VISION

HALAMAN JUDUL

DISUSUN OLEH :
WILDANI EKO NUGROHO, M.Kom
NIPY. 12.013.169

PROGRAM STUDI DIII TEKNIK KOMPUTER


POLITEKNIK HARAPAN BERSAMA
TAHUN 2023
DESKRIPSI SINGKAT
Ruang Lingkup yang menjadi kajian mata kuliah ini meliputi hal-hal sebagai berikut: Dasar
Linux CLI, Pemrograman Python, implementasi PCD, Image Drawing, Feature Extraction,
Face Detection, Parallel Processing, Dataset Preparation, Train Machine Learning Model,
Deploy Machine Learning As API, Face Recognition SVM, OCR Naive Bayes

MANFAAT
Setelah menyelesaikan kuliah Computer Vision, mahasiswa dapat mengimplementasikan
bahasa pemrograman python dengan mengolah citra gambar dan video real time yang dapat
diolah menghasilkan nformasi baru.

CAPAIAN PEMBELAJARAN
1. Capaian Pembelajaran Lulusan Program Studi (CPL-Prodi)
1) Mahasiswa dapat mengaplikasikan image processing dengan melibatkan perangkat
keras
2) Mengaplikasikan skill pemrograman python,
3) Menerapkan kemampuan library pada python yang diterapkan dalam computer
vision

2. Capaian Pembelajaran Mata Kuliah/ Standar Kompetensi (TIU)


1) Mahasiswa dapat mengenal mengenai materi pemrograman python
2) Mahasiswa dapat menguasai kemampuan mengolah citra
3) Menghasilkan sebuah sistem sederhana dari computer vision

3. Bahan Kajian/Materi Pembelajaran/ Pokok Bahasan


1) Kontrak pembelajaran: tujuan dan ruang lingkup materi, aturan, dan tata cara
perkuliahan, evaluasi.
2) Introduction to computer vision
3) Dasar Pemrograman Python
4) Dasar Image Processing dengan python
5) Image color conversion
6) Image preprocessing
7) Object detection and Image segmentation
8) Contur and edge
9) Realtime Video capture
10) Face Recognition

4. Daftar Pustaka
Utama :
1) Machine Learning In Action – Peter Harrington
2) Introduction to Machine Learning Second Edition -Ethem Alpaydın
3) Soft Computing and Intellegent System Design – Fakhreddine O, Karray and
Clarance De Silva
Pendukung :
1) Programming Computer Vision with Python – Jan Erik Solem
2) Computer Vision : Algorithms and Applications - Richard Szeliski

5. Media Pembelajaran
1) PPT
2) Video Tutorial
3) Google classroom
4) Google Scholar
Training
Raspberry Pi
OpenCV & Python

IoT Training Bogor


Muhammad Yunus(083870713580)
Raspbian
Debian based Linux distribution OS
Hardware Setup
Network Setup

SSID : PPU4GROUTER
PSK : admin321
IP NETWORK : 192.168.43.0/24
Additional Software

Jupyter Notebook VNC Client


jupyter notebook --ip=< RASPI-IP>
Putty : Remote SSH Client
VNC Server

SSH Server
Main Application

OpenCV 3.4.1

Python 2.7
Terhubung Ke Raspberry
SSH via Putty
Via SSH
VNC Server
Terhubung Ke Raspberry
VNC
Jupyter Notebook

Di terminal melalui SSH atau VNC, jalankan,


Terhubung Ke Raspberry
jupyter notebook --ip=< RASPI-IP>
Jupyter Notebook

Main Page Jupyter Notebook

Notebook Python2

Web Terminal
Install Library
• sudo apt-get update
• sudo apt-get upgrade
• sudo apt-get install -y python-dev
• sudo apt-get -y install python3-pip
• sudo pip3 install jupyter
• sudo ipython3 kernelspec install-self

• jupyter notebook --no-browser --ip 192.168.43.254 --port 8888


Dasar Linux CLI
Perintah Dasar Filesystem

pwd Menampilkan direktori kerja saat ini.


ls Mendaftar isi dari direktori kerja saat ini.
cd Mengganti ke direktori lain.
cp Menyalin file.
mv Memindahkan file atau mengganti nama file.
rm Menghapus file atau direktori.
mkdir Membuat sebuah direktori baru.
rmdir Menghapus sebuah direktori.
Perintah Dasar Filesystem

pwd Menampilkan direktori kerja saat ini.


Perintah Dasar Filesystem
ls Mendaftar isi dari direktori kerja saat ini.
Perintah Dasar Filesystem
cd Mengganti ke direktori lain.
Perintah Dasar Filesystem

rm Menghapus file atau direktori.


mv Memindahkan file atau mengganti nama file.
Perintah Dasar Filesystem
mkdir Membuat sebuah direktori baru.
rmdir Menghapus sebuah direktori.
Perintah Dasar Network & Software

ifconfig Menampilkan informasi tentang alat jaringan Anda. Anda juga bisa
mengkonfigurasi network adapter Anda.
ping Ping mengirim paket ip ke komputer yang dituju dan mencari respon.
apt-get update Update software list
apt-get upgrade Upgrade software list
apt-get install <package_name> Install software
raspi-config Konfigurasi system raspberry
sudo Super user do
nano Text editor on cli (close and save ctrl +x, ctrl +y)
raspistill –o <image Name> Menggambil gambar melalui Camera raspberry Pi
raspivid –o <image Name> -t <time> Menggambil gambar melalui Camera raspberry Pi
Python
Basic Python
Numpy
OpenCV 3.4.1

pip install numpy


Python version

$ python --version
Print
Help & Documentation
Ada dua fungsi yang digunakan untuk mencari bantuan:

fungsi dir() untuk melihat fungsi apa saja yang tersedia pada sebuah modul;
fungsi help() untuk membuka dokumentasi suatu fungsi.

Sebgai contoh, kita akan coba mencari tahu tentang penggunaan modul math.
Aturan Penulisan Sintaks Python
1. Penulisan Statement Python
Penulisan satu statement tidak diakhiri dengan tanda titik-koma.
2. Penulisan String pada Python
Bisa menggunakan tanda petik tunggal maupun ganda.
3. Penuilsan Case pada Python
Sintak Python bersifat case sensitive, artinya teksini dengna TeksInidibedakan.
4. Penulisan Blok Program Python
Blok program adalah kumpulan dari beberpaa statement yang digabungkan dalam satu blok.
Penulisan blok program harus ditambahkan indentasi (tab atau spasi 2x/4x).
5. Cara Penulisan Komentar pada Python
Dapat menggunakan tanda pagar (#), tanda petik (‘/”), triple petik (‘’’ / “””)
Aturan Penulisan Sintaks Python
Penulisan Blok Program Python
Blok program adalah kumpulan dari beberpaa statement yang digabungkan dalam satu blok.
Penulisan blok program harus ditambahkan indentasi (tab atau spasi 2x/4x).
Aturan Penulisan Sintaks Python
Cara Penulisan Komentar pada Python
Dapat menggunakan tanda pagar (#), tanda petik (‘/”), triple petik ( “””)

# ini adalah komentar


# Ini juga komentar

"Ini adalah komentar dengan tanda petik ganda"


'Ini juga komentar, tapi dengan tanda petik
tunggal'

"""kelas pagar untuk membuat objek pagar.


Dibuat oleh Petani Kode sebagai contoh saja."""
Variable & Tipe Data

Aturan Penulisan Variabel

• Nama variabel boleh diawali menggunakan huruf atau garis bawah (_),
contoh: nama, _nama, namaKu, nama_variabel.

• Karakter selanjutnya dapat berupa huruf, garis bawah (_) atau angka,
contoh: __nama, n2, nilai1.

• Karakter pada nama variabel bersifat sensitif (case-sensitif). Artinya huruf besar dan kecil
dibedakan. Misalnya, variabel_Ku dan variabel_ku, keduanya adalah variabel yang
berbeda.

• Nama variabel tidak boleh menggunakan kata kunci yang sudah ada dalam python
seperti if, while, for, dsb.
Variable & Tipe Data
Python akan seara otomatis mengenali jenis data atau tipe
data yang tersimpan dalam sebuah variabel.
Variable & Tipe Data

Fungsi-fungsi untuk mengubah tipe data:

int() untuk mengubah menjadi integer;


long() untuk mengubah menjadi integer panjang;
float() untuk mengubah menjadi float;
bool() untuk mengubah menjadi boolean;
chr() untuk mengubah menjadi karakter;
str() untuk mengubah menjadi string.
bin() untuk mengubah menjadi bilangan Biner.
hex() untuk mengubah menjadi bilangan Heksadesimal.
oct() untuk mengubah menjadi bilangan okta.
Cara Mengambil Input dan
Menampilkan Output
Python sudah menyediakan fungsi input() untuk mengambil
inputan dari keyboard.

Asterik pada tiap cell


aktif menandakan cell
sedang menjalankan
program

Widget Input text in line pada


jupyter notebook
Operator Aritmatika
Operator Penugasan
Operator Pembanding
Operator Logika
Memahami Percabangan untuk
Membuat Logika Program
Memahami Percabangan untuk
Membuat Logika Program
Memahami Perulangan
1. Perulangan for 2. Perulangan While

2. Perulangan while
Mengenal Struktur Data List
List dapat kita buat seperti membuat variabel biasa, namun
nilai variabelnya diisi dengan tanda kurung siku ([]).
Struktur Data List Lanjutan

Tedapat Tiga metode (method) atau fungsi yang bisa digunakan untuk
menambahkan isi atau item ke List:

prepend(item) menambahkan item dari depan;


append(item) menambahkan item dari belakang.
insert(index, item) menambahkan item dari indeks tertentu
Struktur Data List Lanjutan
Struktur Data List Lanjutan
Index item List & Extend
Struktur Data List Lanjutan
sum(), len(), min() and max() functions of List
Dictionary
Define dictionary and print element on dictionary
Dictionary
Change element in dictionary and iterating dictionary
Dictionary
Check key, check length and add item to dictionary
Matrix Operation

Python NumPy
For numerical computation

https://medium.com/@yasirabd/pengenalan-numpy-pandas-matplotlib-b90bafd36c0
Install Library
• pip3 remove numpy
• pip3 install numpy
• sudo apt-get install libatlas3-base libatlas-base-dev libjasper1
libilmbase-dev libopenexr-dev libgstreamer1.0-dev libqtgui4 libqt4-
test
NumPy: Array
NumPy: Create an array of 10 zeros, 10 ones, 10 fives
NumPy: Create an array of the integers from 30 to 70
NumPy: Create a 3x3 identity matrix
NumPy: Create a 3x3 identity matrix
NumPy: How to add an extra column
Install Library
• pip3 install opencv-python
• sudo pip3 install opencv-contrib-python

• pip3 install picamera


• sudo raspi-config > interfacing option > camera > enable > yes
Image As A Digital Content

8 bit pixel value (0 – 255)


Starndard Image Size
#Create Foto
Create Foto and Video
sudo raspistill -o image.jpg

#Create Video
using built-in command
sudo raspivid -o video.mp4 -t 20000 Raspberry Pi
Accessing individual pixels
Basic Show Image

Basic Show Image


Python
Resize & Show Image
Crop & Show Image
Rotating
an image
Rotating an image

Rotation of an image for an angle θ is achieved by the


transformation matrix of the form

But OpenCV provides scaled rotation with adjustable center of


rotation so that you can rotate at any location you prefer.
Modified transformation matrix is given by

𝑥𝑡 𝑥
𝑦𝑡 = 𝑴 ∙ 𝑦
Convert Image to Grayscale
Convert Image to Grayscale

Formula conversion from RGB to Gray level Image

𝐺 = 0.299𝑅 + 0.587𝐺 + 0.114𝐵


Play Video
Python
Capture Photo
Python
+ Raspi Cam

pip3 install picamera


Capture Video
Python
+ Raspicam
OpenCV Drawing
(line, rectangle, circle, polygon)
OpenCV Drawing
(line, rectangle, circle, polygon, text)
Draw line in image
Blurs an Image using a
Gaussian Filter

Original Image Noise reduction using 5x5 Gaussian Filtering


Gaussian Blur
Canny
Edge Detection
Canny Edge Detection

Original Image Noise reduction using 5x5 Gaussian Filtering Finding Intensity Gradient
of the Image

Edged Image Hysteresis Thresholding Non-maximum Suppression


https://docs.opencv.org/trunk/da/d22/tutorial_py_canny.html
https://en.wikipedia.org/wiki/Canny_edge_detector
Canny Edge Detection
Image Threshold
Python
Image Threshold
Simple Thresholding

cv.THRESH_BINARY
cv.THRESH_BINARY_INV
cv.THRESH_TRUNC
cv.THRESH_TOZERO
cv.THRESH_TOZERO_INV
Simple Thresholding
Image Threshold Otsu's Binarization

Otsu's algorithm tries to find a


threshold value (t) which
minimizes the weighted
within-class variance

https://en.wikipedia.org/wiki/Otsu%27s_method
https://docs.opencv.org/master/d7/d4d/tutorial_py_thresholding.html
Otsu's Thresholding
Detecting and
Drawing Contours
Contours : curve joining all the continuous points
(along the boundary), having same color or
intensity. The contours are a useful tool for
shape analysis and object detection and
recognition.
Detecting and Drawing Contours
Contour approximation method

with cv2.CHAIN_APPROX_NONE (734 points) and second image


shows the one with cv2.CHAIN_APPROX_SIMPLE (only 4 points)

cv2.CHAIN_APPROX_NONE, all the boundary points are stored


cv2.CHAIN_APPROX_SIMPLE, only end points / corner of that shape are stored

https://docs.opencv.org/master/d4/d73/tutorial_py_contours_begin.html
Contour retrieval mode
Mode Description
cv2.RETR_EXTERNAL retrieves only the extreme outer contours.
It sets hierarchy[i][2] = hierarchy[i][3] = -1 for all the contours.

cv2.RETR_LIST retrieves all of the contours without establishing any hierarchical relationships.

cv2.RETR_CCOMP retrieves all of the contours and organizes them into a two-level hierarchy. At the top
level, there are external boundaries of the components. At the second level, there are
boundaries of the holes. If there is another contour inside a hole of a connected
component, it is still put at the top level.
cv2.RETR_TREE retrieves all of the contours and reconstructs a full hierarchy of nested contours.

cv2.RETR_FLOODFILL

https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#ga819779b9857cc2f8601e6526a3a5bc71
Detecting Circle
Hough transform

Detect Circle & Line


Hough Line Transform
A line in the image space can be expressed with two variables.
For example:

In the Cartesian coordinate system: Parameters: (m,b).


In the Polar coordinate system: Parameters: (r,θ)

cv2.HoughLines() :
Hough Line Transform keeps track of the intersection between
The Standard Hough Transform
curves of every point in the image. If the number of
intersections is above some threshold, then it declares it as a
cv2.HoughLinesP() :
line with the parameters (θ,rθ) of the intersection point
The Probabilistic Hough Line Transform
https://docs.opencv.org/3.4/d9/db0/tutorial_hough_lines.html
Hough Line Transform
cv2.HoughCircles Parameters
image 8-bit, single-channel, grayscale input image.
circles Output vector of found circles. Each vector is encoded as 3 or 4 element floating-point
vector (x,y,radius) or (x,y,radius,votes) .
method Detection method, see HoughModes. Currently, the only implemented method is HOUGH_GRADIENT
dp Inverse ratio of the accumulator resolution to the image resolution. For example, if dp=1 , the accumulator has the same
resolution as the input image. If dp=2 , the accumulator has half as big width and height.
minDist Minimum distance between the centers of the detected circles. If the parameter is too small, multiple neighbor circles may
be falsely detected in addition to a true one. If it is too large, some circles may be missed.
param1 First method-specific parameter. In case of HOUGH_GRADIENT , it is the higher threshold of the two passed to the Canny
edge detector (the lower one is twice smaller).
param2 Second method-specific parameter. In case of HOUGH_GRADIENT , it is the accumulator threshold for the circle centers at
the detection stage. The smaller it is, the more false circles may be detected. Circles, corresponding to the larger
accumulator values, will be returned first.
minRadius Minimum circle radius.
maxRadius Maximum circle radius. If <= 0, uses the maximum image dimension. If < 0, returns centers without finding the radius.

https://docs.opencv.org/master/dd/d1a/group__imgproc__feature.html#ga47849c3be0d0406ad3ca45db65a25d2d
Hough Circle Transform
Color Detection
Using HSV color scheme
Color
detection
HSV
Haar Cascade
Classifier
python
Haar Cascade Classifier
Object Detection using Haar Haar features just like normal convolutional kernel
feature-based cascade
classifiers is an effective
object detection method
proposed by Paul Viola and
Michael Jones in their paper,
"Rapid Object Detection using
a Boosted Cascade of Simple
Features" in 2001

Algorithm needs a lot of positive images (images of


faces) and negative images (images without faces) to
train the classifier.

Each feature is a single value obtained by subtracting


sum of pixels under the white rectangle from sum of
pixels under the black rectangle
Haar Cascade Classifier

24x24 window (face image)


Haar features for face
Haar Cascade Classifier
Face Detection
Haar Cascade Classifier
Face & Eye Detection
Haar Cascade
Classifier
Face & Eye Detect

Camera Stream
Haar Cascade
Classifier
Face Detect

Camera Stream
Real-time Video
Analytic
Multithreading

pip install imutils


Multi-thread
Video stream
https://stackoverflow.com/questions/63345956/python-opencv-mutithreading-
with-opecv-video-streaming
https://ilmukomputer.org/2008/09/01/multithreading-pada-python-bagian-1/

https://stackoverflow.com/questions/63345956/python-opencv-mutithreading-with-opecv-video-
streaming
Robot Vision Using Raspberry
Hardware

Pan - tilt Servo 16 Channel Servo Controller Raspberry Pi Raspberry Pi


PCA9685 Cammera
Raspberry Pi
Raspberry Pi GPIO
Raspberry Pi Programming
Raspberry Pi Project
Raspberry Pi Camera

Raspberry Pi
Cammera
Raspberry Pi Camera Programming
Motor Servo & Servo Controller

Pan - tilt Servo 16 Channel Servo Controller


PCA9685
Wiring Diagram
Servo Programming

Install library

sudo apt-get install python-smbus


sudo apt-get install i2c-tools
sudo pip install adafruit-pca9685

enable I2C : sudo raspi-config > interfacing option > I2C > enable > yes
Basic Servo Programming
Pan Tilt Servo / Gimbal

180

180
0
Face Tracking OpenCV

Center of image
FRAME_H/2
FRAME_W/2

FRAME_H

FRAME_W
Face Tracking OpenCV

Apply Haar Cascade


Classifier

Center detected face


x + w/2
FRAME_H y + h/2

FRAME_W
Face Tracking OpenCV

Center detected face


x + w/2
FRAME_H y + h/2

Center of image
FRAME_H/2
FRAME_W/2

FRAME_W
Face Tracking OpenCV

FRAME_H
Center of image
FRAME_H/2
FRAME_W/2

Distance between center of


Center detected face image and center detected
face
x + w/2
y + h/2

FRAME_W
Face Tracking OpenCV
0 pixel y Distance between center of
180 image and center detected
face

FRAME_H
0 180
turn_y

turn_x

turn_x = -(x + w/2 - (FRAME_W/2))

turn_y = y + h/2 - (FRAME_H/2)


n pixel y
0 pixel y FRAME_W n pixel y
Face Tracking OpenCV

Convert distance turn_x and turn_y into percentage

w FRAME _ W
− (x + −( )
turn _ x = 2 2
FRAME _ W
2
turn_y

turn_x h FRAME _ H
−(y + −( )
turn _ y = 2 2
FRAME _ H
2
Face Tracking OpenCV
Face Tracking OpenCV
Face Tracking OpenCV
Face Tracking OpenCV
Face Tracking OpenCV
Image Processing Using Machine Learning

IoT Training Bogor


Muhammad Yunus
083870713580
Machine Learning ?
Machine learning (ML) is the scientific
study of algorithms and statistical
models that computer systems use to perform a
specific task without using explicit instructions,
relying on patterns and inference instead.
Machine Learning Algorithm
Machine Learning & Deep Learning Framework
Scikit - Learn

https://scikit-learn.org/stable/

https://scikit-learn.org/stable/tutorial/index.html

https://scikit-learn.org/stable/user_guide.html

Install via pip3 :


pip3 install sklearn
pip3 install scikit-image
pip3 install pickle-mixin
pip3 install matplotlib
Face Recognition Using
Eigenfaces & SVM
Face Recognition Using
Eigenfaces & SVM

SPLIT DATA, APPLY PCA TO


GRAYSCALE
LOAD DATA 75% TRAINING CALCULATE
CONVERT TO 1D
25% TEST EIGENFACES

TEST MODEL TRAIN MODEL SVM


Eigenfaces
Eigenfaces is the name given to a
set of eigenvectors when they are
used in the computer vision
problem of human face recognition.

A set of eigenfaces can be generated by performing


a mathematical process called principal component
analysis (PCA) on a large set of images depicting
different human faces.
Principal Component Analysis (PCA)
Eigenface in Python
Directory Structure
dataset
|___ Tiger_Woods
|___ Tiger_Woods_0001.jpg
|___ Tiger_Woods_0002.jpg
|___ ..........
|___ Tiger_Woods_0010.jpg
|___ Yunus
|___ Yunus_0001.jpg
|___ Yunus_0002.jpg
|___ ..........
|___ Yunus_0010.jpg

test
|___ Yunus_Test.jpg
|___ Tiger_Woods.jpg
Load Image Array Data
Load Image Array Data (cont..)
Eigenface in Python
Display Eigenface in Python
Support Vector Machine
A Support Vector Machine (SVM) is a discriminative classifier
formally defined by a separating hyperplane. In other words,
given labeled training data (supervised learning), the
algorithm outputs an optimal hyperplane which categorizes
new examples. In two dimentional space this hyperplane is a
line dividing a plane in two parts where in each class lay in
either side.
https://medium.com/machine-learning-101/chapter-2-svm-support-vector-machine-theory-f0812effc72
Support Vector Machine Face Classification

Cluster face Tiger Wood

hyperplane
Input Image
Cluster face Yunus

SVM Model
Train SVM To Recognize Face
Test The SVM Face Recognition Model
Viedo Stream - SVM Face Recognition
OCR Using Naïve Bayes
Algorithm
OCR Using Naïve Bayes Algorithm
In machine learning, naïve Bayes
classifiers are a family of simple
"probabilistic classifiers" based on
applying Bayes' theorem with strong
(naïve) independence assumptions Using Naïve Bayes theorem, we can find the probability
between the features. of A happening, given that B has occurred. Here, B is the
https://en.wikipedia.org/wiki/Naive_Bayes_classifier
evidence and A is the hypothesis. [source]

Gaussian Naïve Bayes

When the predictors take up a continuous value and are not


discrete, we assume that these values are sampled from a
gaussian distribution.
Train OCR Model using Gaussian Naïve Bayes
Prepare dataset
Install Library

Install via pip3 :


pip3 install sklearn
pip3 install scikit-image
pip3 install pickle-mixin
pip3 install matplotlib
Flow Naive Bayes Training Model

Load Image Dataset Convert Image Convert Grayscale


Change 2D Binary
20x20 pixel Dataset to Grayscale Image to Binary
Image (20x20 pixel)
Image Image
into 1 D (1x400 pixel)
20x20 pixel 20x20 pixel
Set Label

Cross Validation for


Save Generated measure accuracy of
Training Model model (3/4 dataset Load Algorithm
model (.sav)
for training, ¼
dataset for test)
Load Image Data as binary image
& reshape into 1D array

10 adalah banyaknya
dataset tiap letter/character
Load Algorithm (GNB) Gaussian Naive Bayes & Cross Validation
Generate Model and Save into SAV (SAVed file ML
model)
Flow Diagram | Test Model Naïve Bayes

Image Preprocessing
Character Segmentation Load Image
Run Model Prediction

Calculate bouding Search Connected Convert Grayscale Image Convert Image to


box / label Region & Labeling to Binary Image Grayscale

Filter Character Load Gaussian Naïve Run Prediction Character Predicted Plate
Bounding box Bayes Model Image Number
Sample Text (Lucida Sans)

TEKS
PANJANG
UNTUK TEST
Test Model Naïve Bayes
Test Model Naïve Bayes
Test Model Naïve Bayes

Anda mungkin juga menyukai