owned this note
owned this note
Published
Linked with GitHub
# Python筆記
```python=
from pynput.keyboard import Key
from pynput.keyboard import Listener
import time
class Pair:
def __init__(self, key, time_stamp, type):
self.key = key
self.time_stamp = time_stamp
self.type = type
his_list = []
def on_press(key):
his_list.append(Pair(key=key, time_stamp=time.time(), type=0))
# print('{0} pressed'.format(key))
def on_release(key):
his_list.append(Pair(key=key, time_stamp=time.time(), type=1))
# print('{0} release'.format(key))
if key == Key.esc:
# Stop listener
return False
# Collect events until released
with Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
his_dict = dict()
log_list = []
for item in his_list:
print(vars(item))
if item.type == 0:
if not(item.key in his_dict):
his_dict[item.key] = 1
log_list.append(item)
else:
if not(item.key in his_dict):
print(f"{item.key} not found")
else:
his_dict.pop(item.key)
log_list.append(item)
print("===================")
for item in log_list:
print(vars(item))
```
```python=
import cv2
import matplotlib.pyplot as plt
import numpy as np
def calculateDHash(img):
flatten = np.reshape(img, (-1))
dhash = np.zeros((flatten.shape[0]-1))
for i in range(flatten.shape[0]-1):
if flatten[i+1] >= flatten[i]:
dhash[i] = 1
return dhash
def calculateHamingDistance(d1, d2):
return (d1.shape[0] - np.logical_xor(d1, d2).sum())/d1.shape[0]
img1 = cv2.imread("test.png", cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread("test1.png", cv2.IMREAD_GRAYSCALE)
d1 = calculateDHash(img1)
d2 = calculateDHash(img2)
hd = calculateHamingDistance(d1, d2)
print(hd)
# cv2.imshow("123", img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
```
```python=
import mss
import cv2
import numpy as np
import matplotlib.pyplot as plt
with mss.mss() as sct:
# Get information of monitor 2
monitor_number = 1
mon = sct.monitors[monitor_number]
print(mon)
# The screen part to capture
monitor = {
"top": mon["top"],
"left": mon["left"],
"width": mon["width"],
"height": mon["height"],
"mon": monitor_number,
}
# output = "sct-mon{mon}_{top}x{left}_{width}x{height}.png".format(**monitor)
# Grab the data
img = np.array(sct.grab(monitor))
plt.imshow(img)
```
```python=
import cv2
import matplotlib.pyplot as plt
import numpy as np
import mss
import time
def calculateDHash(img):
flatten = np.reshape(img, (-1))
dhash = np.zeros((flatten.shape[0]-1))
for i in range(flatten.shape[0]-1):
if flatten[i+1] >= flatten[i]:
dhash[i] = 1
return dhash
def calculateHamingDistance(d1, d2):
return (d1.shape[0] - np.logical_xor(d1, d2).sum())/d1.shape[0]
def getScreenShot():
with mss.mss() as sct:
# Get information of monitor 2
monitor_number = 2
mon = sct.monitors[monitor_number]
# The screen part to capture
monitor = {
"top": mon["top"],
"left": mon["left"],
"width": mon["width"],
"height": mon["height"],
"mon": monitor_number,
}
# output = "sct-mon{mon}_{top}x{left}_{width}x{height}.png".format(**monitor)
# Grab the data
return cv2.cvtColor(np.array(sct.grab(monitor)), cv2.COLOR_BGR2GRAY)
# img1 = cv2.imread("test.png", cv2.IMREAD_GRAYSCALE)
# img2 = cv2.imread("test1.png", cv2.IMREAD_GRAYSCALE)
for i in range(10):
ts1 = time.time()
img1 = getScreenShot()[800:, 1600:]
img2 = getScreenShot()[800:, 1600:]
d1 = calculateDHash(img1)
d2 = calculateDHash(img2)
hd = calculateHamingDistance(d1, d2)
ts2 = time.time()
print(ts2-ts1)
time.sleep(3)
# cv2.imshow("123", img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
```