写了一个小东西,还有看到的一些东西,分享给大家
# -*- coding: UTF-8 -*-
import cv2
import numpy as np
import easygui
import sys
import time
import pygame
from PIL import Image
from PIL import ImageGrab
# 截取图片
def get_picture(p_num):
p = p_num
img_0 = ImageGrab.grab()
img_0.save("E:\py\huyan\huyan" + str(p) + ".png")
# img.show()
def calculate(image1, image2):
# 灰度直方图算法
# 计算单通道的直方图的相似值
b = False
hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0])
hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0])
# 计算直方图的重合度
degree = 0
for i in range(len(hist1)):
if hist1[i] != hist2[i]:
degree = degree + \
(1 - abs(hist1[i] - hist2[i]) / max(hist1[i], hist2[i]))
else:
degree = degree + 1
degree = degree / len(hist1)
print("degree", degree)
if degree > 0.65:
b = True
return b
def play_music():
filepath = r"E:\py\xuanke 1.0.3\the sound of silence_2019518202423.mp3"
pygame.mixer.init()
# 加载音乐
print("正在播放音乐")
pygame.mixer.music.load(filepath)
pygame.mixer.music.play(start=0.0)
ssec = 300
time.sleep(ssec)
# 播放时长,没有此设置,音乐不会播放,会一次性加载完
# for i in range(0, 300):
# time.sleep(1)
# mi = int(ssec / 60)
# sec = ssec % 60
# show = "还有 " + str(mi) + "分 " + str(sec) + "秒"
# easygui.msgbox(show)
# time.sleep(1)
# sys.exit(0)
# ssec -= 1
pygame.mixer.music.stop()
if __name__ == "__main__":
s_z = True
s_d = False
time_c = 0
p_n = 0
count = 0
while s_z: # making a loop
time.sleep(1)
# print(time.ctime())
try: # used try so that if user pressed other than the given key error will not be shown
if not s_z:
break
else:
time_c += 1
# print("time_c",time_c)
if time_c >= 60 and p_n == 0: # 每隔一分钟截取一张屏幕
p_n = 1
get_picture(p_n)
if time_c >= 120 and p_n == 1:
p_n = 2
get_picture(p_n)
s_d = True
if s_d:
img1 = Image.open(r"E:\py\huyan\huyan" + str(p_n) + ".png")
img2 = Image.open(r"E:\py\huyan\huyan" + str(p_n - 1) + ".png")
img1 = cv2.cvtColor(np.asarray(img1), cv2.COLOR_RGB2BGR)
img2 = cv2.cvtColor(np.asarray(img2), cv2.COLOR_RGB2BGR)
c = calculate(img1, img2)
s_d = False
time_c = 0
p_n = 0
# 初始化清零
if c:
count += 1 # 每两分钟一组,十分钟五组
if count >= 25:
if easygui.ccbox("该休息啦!", choices=["来首音乐放松一下", "知道啦"], title="tip"):
play_music()
else:
sys.exit(0)
count = 0
time.sleep(60)
except:
pass # if user pressed other than the given key the loop will break
# import pyHook
# import pythoncom
# 这是一个比较厉害的库,存一下
# #
# # # 监听到鼠标事件调用
# # def onMouseEvent(event):
# # if (event.MessageName != "mouse move"): # 因为鼠标一动就会有很多mouse move,所以把这个过滤下
# # print(event.MessageName)
# # return True # 为True才会正常调用,如果为False的话,此次事件被拦截
#
#
# # 监听到键盘事件调用
# def onKeyboardEvent(event):
# print(event.Key) # 返回按下的键
# return True
#
#
# def main():
# # 创建管理器
# hm = pyHook.HookManager()
# # 监听键盘
# hm.KeyDown = onKeyboardEvent
# hm.HookKeyboard()
# # 监听鼠标
# # hm.MouseAll = onMouseEvent
# # hm.HookMouse()
# # 循环监听
# pythoncom.PumpMessages()
#
#
# if __name__ == "__main__":
# main()
#
# # 一份很好的代码
# import cv2
# import numpy as np
# from PIL import Image
# import requests
# from io import BytesIO
# import matplotlib
#
# matplotlib.use('TkAgg')
# import matplotlib.pyplot as plt
#
#
# def aHash(img):
# # 均值哈希算法
# # 缩放为8*8
# img = cv2.resize(img, (8, 8))
# # 转换为灰度图
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# # s为像素和初值为0,hash_str为hash值初值为''
# s = 0
# hash_str = ''
# # 遍历累加求像素和
# for i in range(8):
# for j in range(8):
# s = s + gray[i, j]
# # 求平均灰度
# avg = s / 64
# # 灰度大于平均值为1相反为0生成图片的hash值
# for i in range(8):
# for j in range(8):
# if gray[i, j] > avg:
# hash_str = hash_str + '1'
# else:
# hash_str = hash_str + '0'
# return hash_str
#
#
# def dHash(img):
# # 差值哈希算法
# # 缩放8*8
# img = cv2.resize(img, (9, 8))
# # 转换灰度图
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# hash_str = ''
# # 每行前一个像素大于后一个像素为1,相反为0,生成哈希
# for i in range(8):
# for j in range(8):
# if gray[i, j] > gray[i, j + 1]:
# hash_str = hash_str + '1'
# else:
# hash_str = hash_str + '0'
# return hash_str
#
#
# def pHash(img):
# # 感知哈希算法
# # 缩放32*32
# img = cv2.resize(img, (32, 32)) # , interpolation=cv2.INTER_CUBIC
#
# # 转换为灰度图
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# # 将灰度图转为浮点型,再进行dct变换
# dct = cv2.dct(np.float32(gray))
# # opencv实现的掩码操作
# dct_roi = dct[0:8, 0:8]
#
# hash = []
# avreage = np.mean(dct_roi)
# for i in range(dct_roi.shape[0]):
# for j in range(dct_roi.shape[1]):
# if dct_roi[i, j] > avreage:
# hash.append(1)
# else:
# hash.append(0)
# return hash
#
#
# def calculate(image1, image2):
# # 灰度直方图算法
# # 计算单通道的直方图的相似值
# hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0])
# hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0])
# # 计算直方图的重合度
# degree = 0
# for i in range(len(hist1)):
# if hist1[i] != hist2[i]:
# degree = degree + \
# (1 - abs(hist1[i] - hist2[i]) / max(hist1[i], hist2[i]))
# else:
# degree = degree + 1
# degree = degree / len(hist1)
# return degree
#
#
# def classify_hist_with_split(image1, image2, size=(256, 256)):
# # RGB每个通道的直方图相似度
# # 将图像resize后,分离为RGB三个通道,再计算每个通道的相似值
# image1 = cv2.resize(image1, size)
# image2 = cv2.resize(image2, size)
# sub_image1 = cv2.split(image1)
# sub_image2 = cv2.split(image2)
# sub_data = 0
# for im1, im2 in zip(sub_image1, sub_image2):
# sub_data += calculate(im1, im2)
# sub_data = sub_data / 3
# return sub_data
#
#
# def cmpHash(hash1, hash2):
# # Hash值对比
# # 算法中1和0顺序组合起来的即是图片的指纹hash。顺序不固定,但是比较的时候必须是相同的顺序。
# # 对比两幅图的指纹,计算汉明距离,即两个64位的hash值有多少是不一样的,不同的位数越小,图片越相似
# # 汉明距离:一组二进制数据变成另一组数据所需要的步骤,可以衡量两图的差异,汉明距离越小,则相似度越高。汉明距离为0,即两张图片完全一样
# n = 0
# # hash长度不同则返回-1代表传参出错
# if len(hash1) != len(hash2):
# return -1
# # 遍历判断
# for i in range(len(hash1)):
# # 不相等则n计数+1,n最终为相似度
# if hash1[i] != hash2[i]:
# n = n + 1
# return n
#
#
# def getImageByUrl(url):
# # 根据图片url 获取图片对象
# html = requests.get(url, verify=False)
# image = Image.open(BytesIO(html.content))
# return image
#
#
# def PILImageToCV():
# # PIL Image转换成OpenCV格式
# path = "/Users/waldenz/Documents/Work/doc/TestImages/t3.png"
# img = Image.open(path)
# plt.subplot(121)
# plt.imshow(img)
# print(isinstance(img, np.ndarray))
# img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
# print(isinstance(img, np.ndarray))
# plt.subplot(122)
# plt.imshow(img)
# plt.show()
#
#
# def CVImageToPIL():
# # OpenCV图片转换为PIL image
# path = "/Users/waldenz/Documents/Work/doc/TestImages/t3.png"
# img = cv2.imread(path)
# # cv2.imshow("OpenCV",img)
# plt.subplot(121)
# plt.imshow(img)
#
# img2 = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# plt.subplot(122)
# plt.imshow(img2)
# plt.show()
#
#
# def bytes_to_cvimage(filebytes):
# # 图片字节流转换为cv image
# image = Image.open(filebytes)
# img = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR)
# return img
#
#
# def runAllImageSimilaryFun(para1, para2):
# # 均值、差值、感知哈希算法三种算法值越小,则越相似,相同图片值为0
# # 三直方图算法和单通道的直方图 0-1之间,值越大,越相似。 相同图片为1
#
# # t1,t2 14;19;10; 0.70;0.75
# # t1,t3 39 33 18 0.58 0.49
# # s1,s2 7 23 11 0.83 0.86 挺相似的图片
# # c1,c2 11 29 17 0.30 0.31
#
# if para1.startswith("http"):
# # 根据链接下载图片,并转换为opencv格式
# img1 = getImageByUrl(para1)
# img1 = cv2.cvtColor(np.asarray(img1), cv2.COLOR_RGB2BGR)
#
# img2 = getImageByUrl(para2)
# img2 = cv2.cvtColor(np.asarray(img2), cv2.COLOR_RGB2BGR)
# else:
# # 通过imread方法直接读取物理路径
# img1 = cv2.imread(para1)
# img2 = cv2.imread(para2)
#
# hash1 = aHash(img1)
# hash2 = aHash(img2)
# n1 = cmpHash(hash1, hash2)
# print('均值哈希算法相似度aHash:', n1)
#
# hash1 = dHash(img1)
# hash2 = dHash(img2)
# n2 = cmpHash(hash1, hash2)
# print('差值哈希算法相似度dHash:', n2)
#
# hash1 = pHash(img1)
# hash2 = pHash(img2)
# n3 = cmpHash(hash1, hash2)
# print('感知哈希算法相似度pHash:', n3)
#
# n4 = classify_hist_with_split(img1, img2)
# print('三直方图算法相似度:', n4)
#
# n5 = calculate(img1, img2)
# print("单通道的直方图", n5)
# print("%d %d %d %.2f %.2f " % (n1, n2, n3, round(n4[0], 2), n5[0]))
# print("%.2f %.2f %.2f %.2f %.2f " % (1 - float(n1 / 64), 1 -
# float(n2 / 64), 1 - float(n3 / 64), round(n4[0], 2), n5[0]))
#
# plt.subplot(121)
# plt.imshow(Image.fromarray(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)))
# plt.subplot(122)
# plt.imshow(Image.fromarray(cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)))
# plt.show()
#
#
# if __name__ == "__main__":
# p1 = "https://ww3.sinaimg.cn/bmiddle/007INInDly1g336j2zziwj30su0g848w.jpg"
# p2 = "https://ww2.sinaimg.cn/bmiddle/007INInDly1g336j10d32j30vd0hnam6.jpg"
#
# runAllImageSimilaryFun(p1,p2)