目的:编写一个Python脚本,可以检测图像中的人脸,并将所有的人脸保存在一个文件夹中。
提示:可以使用haar级联分类器对人脸进行检测。它返回的人脸坐标信息,可以保存在一个文件中。
安装:OpenCV。
import cv2
# Load the cascade
face_cascade = cv2.CascadeClassifier(‘haarcascade_frontalface_default.xml’)
# Read the input image
img = cv2.imread(‘images/img0.jpg’)
# Convert into grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.3, 4)
# Draw rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
crop_face = img[y:y + h, x:x + w]
cv2.imwrite(str(w) + str(h) + ‘_faces.jpg’, crop_face)
# Display the output
cv2.imshow(‘img’, img)
cv2.imshow(“imgcropped”,crop_face)
cv2.waitKey()
目的:创建一个提醒应用程序,在特定的时间提醒你做一些事情(桌面通知)。
提示:Time模块可以用来跟踪提醒时间,toastnotifier库可以用来显示桌面通知。
安装:win10toast
from win10toast
import ToastNotifier
import time toaster = ToastNotifier()
try: print(“Title of reminder”)
header = input()
print(“Message of reminder”)
text = input() print(“In how many minutes?”)
time_min = input() time_min=float(time_min)
except:
header = input(“Title of reminder\n”)
text = input(“Message of remindar\n”)
time_min=float(input(“In how many minutes?\n”))
time_min = time_min * 60 print(“Setting up reminder..”)
time.sleep(2) print(“all set!”)
time.sleep(time_min) toaster.show_toast(f”{header}”, f”{text}”, duration=10, threaded=True) while toaster.notification_active(): time.sleep(0.005)
更多项目源码,请继续关注小编,如果大家在学习中遇到困难,想找一个python学习交流环境,可以加入我们的python裙,关注小编,并私信“01”即可进裙,领取python学习资料,会节约很多时间,减少很多遇到的难题。
目的:创建一个脚本,可以根据查询条件从谷歌搜索获取数据。
from bs4
import BeautifulSoup
import requests
headers = {‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3’} def google(query):
query = query.replace(” “,”+”)
try:
url = f’https://www.google.com/search?q={query}&oq={query}&aqs=chrome..69i57j46j69i59j35i39j0j46j0l2.4948j0j7&sourceid=chrome&ie=UTF-8′
res = requests.get(url,headers=headers)
soup = BeautifulSoup(res.text,’html.parser’)
except:
print(“Make sure you have a internet connection”)
try:
try:
ans = soup.select(‘.RqBzHd’)[0].getText().strip() except: try: title=soup.select(‘.AZCkJd’)[0].getText().strip()
try:
ans=soup.select(‘.e24Kjd’)[0].getText().strip()
except: ans=”” ans=f'{title}\n{ans}’ except:
try:
ans=soup.select(‘.hgKElc’)[0].getText().strip()
except:
ans=soup.select(‘.kno-rdesc span’)[0].getText().strip()
except:
ans = “can’t find on google”
return ans result = google(str(input(“Query\n”))) print(result)
目的:编写一个Python脚本,将用户按下的所有键保存在一个文本文件中。
提示:pynput是Python中的一个库,用于控制键盘和鼠标的移动,它也可以用于制作键盘记录器。简单地读取用户按下的键,并在一定数量的键后将它们保存在一个文本文件中。
from pynput.keyboard import Key, Controller,Listener
import time
keyboard = Controller()
keys=[]
def on_press(key):
global keys
#keys.append(str(key).replace(“‘”,””))
string = str(key).replace(“‘”,””)
keys.append(string)
main_string = “”.join(keys)
print(main_string)
if len(main_string)>15:
with open(‘keys.txt’, ‘a’) as f:
f.write(main_string)
keys= []
def on_release(key):
if key == Key.esc:
return False
with listener(on_press=on_press,on_release=on_release) as listener: listener.join()
目的:编写一个Python脚本,使用API缩短给定的URL。
from __future__ import with_statement
import contextlib
try:
from urllib.parse import urlencode
except ImportError:
from urllib import urlencode
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlope
import sys
def make_tiny(url):
request_url = (‘http://tinyurl.com/api-create.php?’ +
urlencode({‘url’:url}))
with contextlib.closing(urlopen(request_url)) as response:
return response.read().decode(‘utf-8’)
def main(): for tinyurl in map(make_tiny, sys.argv[1:]):
print(tinyurl) if __name__ == ‘__main__’: main()
—————————–OUTPUT————————
python url_shortener.py https://www.wikipedia.org/
https://tinyurl.com/buf3qt3
最后多说一句,小编是一名python开发工程师,这里有我自己整理了一套最新的python系统学习教程,包括从基础的python脚本到web开发、爬虫、数据分析、数据可视化、机器学习等。
国庆假日,青岛这处海滩火了!蛤蜊一挖一麻袋,根本挖不完
国庆假日,青岛这处海滩火了!蛤蜊一挖一麻袋,根本挖不完,蛤蜊,螃蟹,海鲜,蛏子,赶海,青岛市,国庆假日(0)人阅读时间:2024-10-06以色列的攻击,已是箭在弦上(以色列精确打击)
以色列的攻击,已是箭在弦上,以色列,拜登,哈马斯,内塔尼亚胡,伊朗,真主党(0)人阅读时间:2024-10-06miui是什么(miui是什么文件夹)
miui是什么(手机出现MIUI什么意思)。本站来告诉相关信息,希望对您有所帮助。“万物基于MIUI”这句话已经深入人心,它也确确实实关系到小米手机的口碑和销量,作为国内用户数量最多和定制程度最深的U(0)人阅读时间:2024-10-06矢量图是什么(矢量图是什么图)
矢量图是什么(位图和矢量图的区别)。本站来告诉相关信息,希望对您有所帮助。1、 位图和矢量图位图和矢量图,是根据软件运用以及最终存储方式的不同而生成的两种不同的文件类型。在图像处理过程中,分清位图和矢(0)人阅读时间:2024-10-06唐山高新区是哪个区(唐山高新区是哪个区划分出来的)
唐山市高新区是国家级高新技术开发区,不是行政区划。其行政级别为副厅级,位置在路北区和丰润区之间,归唐山市直接管理。唐山市高新技术..2024-10-06唐诗分为哪几个阶段 我们一般把唐朝和唐诗分为哪几个阶段
唐诗发展分为四个阶段,分别为初唐时期、盛唐时期、中唐时期和晚唐时期。1、初唐时期在文风上,初唐时期的诗人作品中气象万千,雄浑博大,已..2024-10-06唐宋八大家是哪八位及朝代(唐宋八大家是哪几位?分别是什么朝代的?)
唐宋八大家,又称为“唐宋散文八大家”,是唐代和宋代八位散文家的合称,分别为唐代柳宗元、韩愈和宋代欧阳修、苏洵、苏轼、苏辙、王安石..2024-10-06唐宋八大家(唐宋八大家分别有谁)
唐宋八大家唐朝有韩愈、柳宗元,他们是唐代古文运动的领导者。古文运动就是文体改革运动,“古文”就是由韩愈提出的,韩愈大力推行散行单..2024-10-06