OpenCV findContours – What it is?

OpenCV findContours detects change in the image color and marked it as contour.

In this example using archery target face, findContours detects the outer circle, yellow circle, bullseye circle, logos on top left and bottom left.

In this case, findContours doesn’t detect the red circle contour.

The Original Image

archery target face
archery target face

The Image Gray

image gray of target face
image gray of target face

The Find Contours Image

Green color is the contours found on the image.

opencv findcontours on archery target face
opencv findcontours on archery target face

The OpenCV Python Code

 

Conclusion

findContours is an easy way to automatically detect an image shape or outline.

References:

  1. https://docs.opencv.org/4.x/d4/d73/tutorial_py_contours_begin.html
  2. https://medium.com/analytics-vidhya/opencv-findcontours-detailed-guide-692ee19eeb18

 

OpenCV Python Image Perspective Transform

I wanted to learn OpenCV Perspective Transform. Tutorials that I found on the internet bit complex for me to really understand what it is all about. I have to read few tutorials before really understand it.

In layman term, perspective transform is taking a rectangle area from original image and re-project the cut rectangle to our defined size canvas.

To do this, you just need 4 corner pixel points and perspective transform will do all other pixels calculation automatically based on those 4 pixels points.

The Required Pixel Points

The corner pixels points must follow the order as shown in below image.

source pixels positions order
source pixels positions order

This is considered the source pixel points.

The destination points are where you want to re-project those source points into new plane.

The size of destination plane is:

width: 1000 pixels
height: 200 pixels

I just take the source rectangle width and height.

Image Used for the Perspective Transform

I used below image to do the perspective transform.

restaurant original image
restaurant original image

Code on Perspective Transform

Filename: Main.py

To run the program, type at the terminal

 

Folder Structure:

source code folder structure
source code folder structure

Reference

OpenCV Perspective Transform Youtube

How To Install WordPress in Docker using Docker Compose

I follow  How to install WordPress in Docker Tutorial and found it has error in its docker-compose.yml file. Besides, it didn’t tell how to access phpMyAdmin and WordPress.

I am completely newbie on Docker so it took me time to really understand.

For that reason, I copy the tutorial and improve and simplify it. So it is easier for newcomer to Docker.

How To Install WordPress in Docker Using Docker Compose?

Create a folder for your project

Create a docker-compose.yml file inside your folder

 

Run The Docker WordPress Installation

Run Docker Compose Command Same Location as Your docker-compose.yml

docker - wordpress run compose up
docker – wordpress run compose up

Access WordPress

Run the usual WordPress Installation

wordpress installation page
wordpress installation page

Database Configuration:

Database name: wordpress
Username: wordpress
Password: wordpress

Testing Your WordPress Installation in Docker

Login using username and passoword, you have created. Then insert a post.

docker creating a post in wordpress
docker creating a post in wordpress

Check at phpMyAdmin

Server: <leave it empty>
Username: WordPress
Password: WordPress

docker phpmyadmin username and password wordpress
docker phpmyadmin username and password wordpress

Check Inserted Data

docker - new post successfully inserted in database
docker – new post successfully inserted in database

Last Note

With this docker-compose configuration you can’t access to your local file system. So this another thing I need to find the solution.

 

Python Unit Test Error: AttributeError has no attribute test

I got AttributeError: has no attribute ‘test’ when I run python unit test.

The cause of the error is due to file naming that has ‘filename.test.py’. So I change into test_filename.py then it works!

 

Import SQL File into MySQL in XAMPP from Command Line in MacOS

To import SQL file in XAMPP MySQL, first has to go to XAMPP MySQL bin then do the command standard Mysql import command line.

XAMPP Path in MacOS

Command in Terminal

Example:

XAMPP mysql import sql file

 

Selenium – This version of ChromeDriver only supports Chrome version error on Ubuntu for version higher than 114

I use Selenium web driver to scrap some website data that I need for my property analysis. In this case I use chromedriver.

When I want to run it after I upgraded my Chrome browser on Ubuntu I found this error.

My steps to resolve this

1) Check my Chrome browser version and found it is 123.

chrome browser version 123
chrome browser version 123

2) Check my chromedriver version by typing

chromedriver version 114
chromedriver version 114

I found my version is 114 not same as my browser.

3) Check the location of the chrome driver by typing

In my case I found my chrome driver at /usr/local/sbin

chrome driver location on ubuntu
chrome driver location on ubuntu

4) Download the latest chromedriver at

Google Chrome Driver for version after version 114

5) After extracting the latest chromedriver, replace the old one.

Ensure you download the right chrome driver which is highlighted in blue.

download chromedriver v123
download chromedriver v123

ubuntu replace old chromedriver
ubuntu replace old chromedriver

Then when I run back my program, it works!

Access XAMPP 8.2.4 MySQL via Command Line in MacOS

You can access XAMPP 8.2.4 MySQL via command line in MacOS by following these steps.

1. Go to XAMPP 8.2.4 MySQL folder in MacOS

2. Type This Command

If successful, XAMPP will connect to MariabDB instead of MySQL

You can double check by getting all databases in the MariaDB.

 

macos xampp 8.2.4 mysql access via command line
macos xampp 8.2.4 mysql access via command line

Typescript Factory Pattern without Switch or If Else

Factory pattern is a well known creator pattern. Problem with factory pattern is whenever there is a new concrete class that will be instantiate by the creator class (that have factory method), you have to add new ‘case’ in a switch or if / else.

So this violate the Open Close Principle which states open for extensions but close for modifications.

Code below using typescript, but the concept is same for other programming language.

Factory Pattern with Switch or If Else

If you want to add Square shape, we need to add another case in the factory.

The concept is the same with If Else

So I need to open my factory class every time there is a new shape to be add in.

Factory Pattern without Switch & If / Else

There are 3 main classes

1) Factory class
– it will import the shape class files in the folder /shapes
– instantiate the object based on shape type

2) Shape abstract class
– abstract method of getType()

3) Shape concrete class
– implement the getType() to tell what type is the shape e.g round
– located in /shapes folder

The concrete class Shape will implement a function to tell it is what kind of Shape.

The factory class will import all the shape file path and instantiate the object when call by user class.

How To Use It?

Impact

It requires async / await because needs to load dynamically the shape class.

This factory pattern can’t be used in a constructor.

 

 

How To Install PHP MySQL & Apache on MacOS Ventura on MacBook Pro M1

In my previous post, I was unable to install PHP & MySQL. This time after 2 years, I tried again but still fail, so I decided to use XAMPP to install PHP & MySQL and Apache on MacOS Ventura and it works!

But I had to disable default installed apache2 and change MySQL default port at XAMPP before MySQL and Apache2 can run on XAMPP.

How To Install PHP, MySQL & Apache using XAMPP

1. Download and install XAMPP

Mount the xampp dmg and run it. If you face unknown publish then do Control + Click on the XAMPP installer then run it again.

2. Follow the step by step instruction in installing XAMPP

Just follow the instructions.

3. Run the servers

If you unable to run the MySQL & Apache web server than follow below solution.

xampp macos ventura - mysql and apache webserver unable to start
xampp macos ventura – mysql and apache webserver unable to start

If you can run MySQL change it default ports. I use 3316 instead of default 3306.

xampp macos ventura - change the default port to 3316
xampp macos ventura – change the default port to 3316

I tried to stop mysql service but MacOS informed no MySQL service was running. Only by changing MySQL default port it works.

xampp macos ventura - mysql not running
xampp macos ventura – mysql not running

If you can’t run the apache2 server, stop default apache2 server by typing at the terminal.

Then run again Apache Web Server at XAMPP.

xamp macos ventura - apache webserver now running
xamp macos ventura – apache webserver now running

4. Test PHP & MySQL by accessing PhpMyAdmin

If you can access phpMyAdmin and create table or user from phpMyAdmin then your XAMPP installation works!

xampp macos ventura - access localhost and click phpmyadmin
xampp macos ventura – access localhost and click phpmyadmin
xamp macos ventura - successful installed php and mysql now can access phpmyadmin
xamp macos ventura – successful installed php and mysql now can access phpmyadmin

Ubuntu 22 Unable to Install Python3 pip3 due to dpkg: error processing archive

I tried to install pip3 on my Ubuntu version 22 but it produced error dpkg: error processing archive.

How To Solve dpkg : error processing when installing python3 pip3

1. Install python3 pip3 command

Error it produces of unmet dependencies

ubuntu 22 unmet dependencies when installing pip3
ubuntu 22 unmet dependencies when installing pip3

2. Command to fix the broken dependencies

Error of dpkg: error processing archive <filename>

ubuntu 22 error when try to solve unmet dependencies when install pip3
ubuntu 22 error when try to solve unmet dependencies when install pip3

3. Force overwrite that caused the error processing

In my case its is my nodejs

The output after did the command

ubuntu 22 dpkg after force overwrite
ubuntu 22 dpkg after force overwrite

4. Install again the pip

Now it is successfully installed.

ubuntu 22 successfully installed pip3 after force dpkg part 1
ubuntu 22 successfully installed pip3 after force dpkg part 1
ubuntu 22 successfully installed pip3 after force dpkg part 2
ubuntu 22 successfully installed pip3 after force dpkg part 2

5. Get your pip3 version

ubuntu 22 pip3 version
ubuntu 22 pip3 version