31
loading...
This website collects cookies to deliver better user experience
I am not writing about hardware automation in this article but if you are interested then do let me know in the discussion below... 🙂
Template matching: is a technique in digital image processing for finding small parts of an image which match a template image. It can be used in manufacturing as a part of quality control, a way to navigate a mobile robot, or as a way to detect edges in images.
source: Wikipedia
import cv2 # pip install opencv-python
def get_location(target_image, sample_image):
# read screenshot image
img_rgb = cv2.imread(sample_image)
# convert BGR to grayscale image
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
# read target image
template = cv2.imread(target_image, 0)
# extracting height and width of target image
height, width = template.shape[::]
# template match using cv2
res = cv2.matchTemplate(img_gray, template, cv2.TM_SQDIFF)
# extract top_left position of target image in screenshot image
_, _, top_left, _ = cv2.minMaxLoc(res)
# calculating the bottom right position of target image
bottom_right = (top_left[0] + width, top_left[1] + height)
# calculating x, y postion
return (top_left[0]+bottom_right[0])//2, (top_left[1]+bottom_right[1])//2
sample_image = take_screenshot() # sample_image.png
with open("target_image.png") as f:
target_image = f.read()
x, y = get_location(target_image, sample_image)
click(x,y)
PyAutoGUI: PyAutoGUI is an amazing library created by asweigart. This library has multiple features like clicking, taking screenshots, keystrokes, writing text etc. This tool is a must-have if you developing an automation script for PC. It is compatible with windows, mac & ubuntu.
pip install pyautogui
os & subprocess: If you want to call os commands, retrieve output use external CLI with your program, delete a file, create file or folder. Then these two libraries are very important. You don't have to install them. These are pre-installed with python.
File management & shutil: If you are planning to use automation for file management then open()
is very important. It helps you read data from any file in bytes or as text and even lets you change, append or write any data. Moreover, you can use shutil which offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal.
Template Matching
Fun Fact: You can create a .pyw file that runs in the background so if you want to make a program something like file sorting for your downloads folder then you can write the code in the .pyw file. You can also make this .pyw file at the startup of your machine so that you don't have to run the python file again & again.
requests: Requests library is used to send requests using methods like GET, POST, PUT etc. Using this library can be used to extract HTML, JSON, XML data from any website URL. This is a very basic package and you will be using this library in one way or another. I would like to tell you that this library is not available with python. You have to install using
pip install requests
BeautifulSoup: This library is also very important and lets you pull data from HTML and XML files. It helps to parse the HTML file and can find any tag by id, class, XPath, CSS etc. This library can be installed by using
pip install beautifulsoup4
Selenium: This library is very powerful. It lets you access the web like a user. The features of selenium are typing in input fields, clicking buttons, switching tabs, taking screenshots, downloading files, executing javascript and much more. It is used for little complex automation projects. Selenium uses webdriver and can run on different browsers like chrome, firefox, safari etc. Also, selenium can run headless meaning the browser window can be hidden from the user. You can install selenium using
pip install selenium
webbot: This library is developed using selenium. The only point is that selenium is a little complex with id, class, CSS & XPath selectors. Unlike selenium using webbot is extremely easy. You can install webbot using
pip install selenium
webdriver: As I have told you earlier webbot and selenium depends on webdriver of chrome, firefox, safari or any other browser.
ADB: ADB(Android Debug Bridge) is a command-line tool that lets you communicate with android devices.*
import cv2, subprocess
ADB
in which the user will pass the os command and the function will return output from the terminal.def adb(command):
proc = subprocess.Popen(command.split(' '), stdout=subprocess.PIPE, shell=True)
(out, _) = proc.communicate()
return out.decode('utf-8')
I have already created a gist for you if you want to access the ADB code. You can find the gist here
Swipe Gesture
def swipe(start_x, start_y, end_x, end_y, duration_ms):
adb("adb shell input swipe {} {} {} {} {}".format(start_x, start_y, end_x, end_y, duration_ms))
Clicking
def click(tap_x, tap_y):
adb("adb shell input tap {} {}".format(tap_x, tap_y))
Take Screenshots
def take_screenshot(final):
adb(f"adb exec-out screencap -p > ./images/{final}.png")
Send SMS
def send_sms(number, body):
adb(f'adb shell am start -a android.intent.action.SENDTO -d sms:{number} --es sms_body "{body}" --ez exit_on_sent true')
Calling
def call(number):
adb(f"adb shell am start -a android.intent.action.CALL -d tel:{number}")
Download File from phone to PC
def download(path, output_path):
adb(f"adb pull {path} {output_path}") #/sdcard/xxx.mp4
Remove Dir
def remove(path):
adb(f"adb shell rm {path}") #/sdcard/...
ScreenRecord
def screen_record(name, time):
adb(f"adb shell screenrecord /sdcard/{name} --time-limit {time}")
download(f"/sdcard/{name}",f"./mobile/{name}")
remove(f"/sdcard/{name}")
Power On/Off
def switch_phone_on_off():
adb("adb shell input keyevent 26")
Keyevents (list of keyevent codes here)
def keyevent(key):
adb(f"adb shell input keyevent {key}")
Send whatsapp message
def send_whatsapp_message(phone, message):
adb(f'adb shell am start -a android.intent.action.VIEW -d "https://api.whatsapp.com/send?phone={phone}"')
adb('ping 127.0.0.1 -n 2 > nul')
adb(f'adb shell input text "{message}"')
adb('adb shell keyevent 22')
adb('adb shell keyevent 22')
adb('adb shell input keyevent 22')
adb('adb shell input keyevent 22')
adb('adb shell input keyevent 66')
Template Matching: You can use template matching to detect icon location and position by taking a screenshot and then clicking. If you don't know template matching then I've explained that earlier in this blog.