opencv Practice facial recognition , Missing video file , Crawling the video failed , I want to save and use images by screenshots .
Screen pixel size , Need to pass other modules , When you practice, you can use your own screen directly , If you want to change according to different screen pixels , You can add .
In addition, since the screenshot , You can directly recognize the face after the image is read , Save one step to save the conversion .
import uuid,socket,time
import pyautogui
import cv2
import numpy as np
import pyttsx3 # Voice playback
# Get the computer device number
def get_mac():
#mac
mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
# Get the hostname
hostname = socket.gethostname()
# obtain IP
ip = socket.gethostbyname(hostname)
# Result data export
pc_mac = {‘mac’:mac,‘hostname’:hostname,‘ip’:ip}
return pc_mac
def get_current_time():
ct = time.time()
local_time = time.localtime(ct)
data_head = time.strftime(“%Y%m%d%H%M%S”, local_time)
data_secs = abs(ct - round(ct)) * 1000
time_stamp = “%s%03d” % (data_head, data_secs)
return time_stamp
def pyttsx_yybf(text):
engine = pyttsx3.init()
engine.setProperty(‘rate’, 160) # Speech speed
engine.say(text) # Reading content
engine.runAndWait()
def get_win_gui(mac,in_time,output):
# Represent the : Top left coordinates , Wide and high
img = pyautogui.screenshot(region=[0,0,1366,768])
# Convert the acquired image into two-dimensional matrix form , And then I will RGB Turn into BGR
# because imshow, The default channel order is BGR, and pyautogui The default is RGB So we need to switch , Otherwise there will be some problems
img_cv = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
#cv2.imwrite(‘./image/win_gui/’+mac+‘_’+in_time+‘.jpeg’ , img_cv)
output.write(img_cv)
if name == ‘main’:
pyttsx_yybf(' Countdown starts screenshot ')
for i in range(10):
pyttsx_yybf('%d second ' % (10-i))
time.sleep(0.5)
pyttsx_yybf(' Screenshot start ')
# Get system data
mac_id = get_mac()
# Display data
print(' Computer parameter display ', mac_id)
# Get content
mac, hostname, ip = mac_id['mac'], mac_id['hostname'], mac_id['ip']
# Screen recording preparation
fourcc = cv2.VideoWriter_fourcc('X','V','I','D')
fps = 10
output = cv2.VideoWriter('jieping.avi',fourcc,fps,(1366,768))
frame_num = 60 * fps
# Loop execution
while True and frame_num > 0:
in_time = get_current_time()
#print('in_time',in_time)
get_win_gui(mac,in_time,output)
frame_num -= 1
time.sleep(0.0005)
pyttsx_yybf(' The screenshot ends ')