Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python for real-time monitoring

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to use Python real-time monitoring. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Recently, I suddenly had a wonderful idea, that is, when I look at the computer screen, the computer will first identify whether the face on the screen is me or not. If the recognition is me, I need to answer the code words spoken by the computer. Only when I answer correctly will I unlock it and have three chances. If you don't answer correctly, you will send me an email informing me that someone is touching my computer and uploading that person's profile picture.

Process

The environment is win10 code. I use python3, so I need to install some dependent packages before starting. Please install them sequentially, otherwise an error will be reported.

Pip install cmake-I https://pypi.tuna.tsinghua.edu.cn/simple

Pip install dlib-I https://pypi.tuna.tsinghua.edu.cn/simple

Pip install face_recognition-I https://pypi.tuna.tsinghua.edu.cn/simple

Pip install opencv-python-I https://pypi.tuna.tsinghua.edu.cn/simple

The next step is to build code that recognizes other people's faces and compares them.

Import face_recognition

Import cv2

Import numpy as np

Video_capture = cv2.VideoCapture (0)

My_image = face_recognition.load_image_file ("my.jpg")

My_face_encoding = face_recognition.face_encodings (my_image) [0]

Known_face_encodings = [

My_face_encoding

]

Known_face_names = [

"Admin"

]

Face_names = []

Face_locations = []

Face_encodings = []

Process_this_frame = True

While True:

Ret, frame = video_capture.read ()

Small_frame = cv2.resize (frame, (0,0), fx=0.25, fy=0.25)

Rgb_small_frame = small_frame [:,:,:-1]

If process_this_frame:

Face_locations = face_recognition.face_locations (rgb_small_frame)

Face_encodings = face_recognition.face_encodings (rgb_small_frame, face_locations)

Face_names = []

For face_encoding in face_encodings:

Matches = face_recognition.compare_faces (known_face_encodings, face_encoding)

Name = "Unknown"

Face_distances = face_recognition.face_distance (known_face_encodings, face_encoding)

Best_match_index = np.argmin (face_distances)

If matches[best _ match_index]:

Name = known_face_ namespace [best _ match_index]

Face_names.append (name)

Process_this_frame = not process_this_frame

For (top, right, bottom, left), name in zip (face_locations, face_names):

Top * = 4

Left * = 4

Right * = 4

Bottom * = 4

Font = cv2.FONT_HERSHEY_DUPLEX

Cv2.rectangle (frame, (left, top), (right, bottom), (0,0,255), 2)

Cv2.rectangle (frame, (left, bottom-35), (right, bottom), (0,0,255), cv2.FILLED)

Cv2.putText (frame, name, (left + 6, bottom-6), font, 1.0, (255,255,255), 1)

Cv2.imshow ('Video', frame)

If cv2.waitKey (1) & 0xFF = = ord ('q'):

Break

Video_capture.release ()

Cv2.destroyAllWindows ()

Among them, my.jpg requires you to shoot and upload it yourself. If you run it, you can find the frame of Admin on your face. I went to the Internet to find a picture like this.

The recognition function has been completed, followed by speech recognition and speech synthesis, which needs to be realized by using Baidu AI. Log in to Baidu AI's official website, go to the console and select the voice technology on the left, then click the create Application button on the panel to create the application interface.

Create a computer version of the face screen unlocking artifact

After creation, you will get AppID, API Key, Secret Key to write down, and then start to write the speech synthesis code. Install the dependency package provided by Baidu AI

Pip install baidu-aip-I https://pypi.tuna.tsinghua.edu.cn/simple

Pip install playsound-I https://pypi.tuna.tsinghua.edu.cn/simple

Then there is a simple voice playback code. Run the following code to hear the cute girl's voice.

Import sys

From aip import AipSpeech

From playsound import playsound

APP_ID =''

API_KEY =''

SECRET_KEY =''

Client = AipSpeech (APP_ID, API_KEY, SECRET_KEY)

Result = client.synthesis ('Hello A', 'zh', 1, {' vol': 5, 'per': 4,' spd': 5,})

If not isinstance (result, dict):

With open ('auido.mp3',' wb') as file:

File.write (result)

Filepath = eval (repr (sys.path [0]). Replace ('\','/') +'/ / auido.mp3'

Playsound (filepath)

With the above code, we can check whether it is in front of the computer (face recognition) and the computer reads the code (speech synthesis), and then we also need to answer the code to the computer, so we also need to complete speech recognition.

Import wave

Import pyaudio

From aip import AipSpeech

APP_ID =''

API_KEY =''

SECRET_KEY =''

Client = AipSpeech (APP_ID, API_KEY, SECRET_KEY)

CHUNK = 1024

FORMAT = pyaudio.paInt16

CHANNELS = 1

RATE = 8000

RECORD_SECONDS = 3

WAVE_OUTPUT_FILENAME = "output.wav"

P = pyaudio.PyAudio ()

Stream = p.open (format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)

Print ("* recording")

Frames = []

For i in range (0, int (RATE / CHUNK * RECORD_SECONDS)):

Data = stream.read (CHUNK)

Frames.append (data)

Print ("* done recording")

Stream.stop_stream ()

Stream.close ()

P.terminate ()

Wf = wave.open (WAVE_OUTPUT_FILENAME, 'wb')

Wf.setnchannels (CHANNELS)

Wf.setsampwidth (p.get_sample_size (FORMAT))

Wf.setframerate (RATE)

Wf.writeframes (b''.join (frames)) def get_file_content ():

With open (WAVE_OUTPUT_FILENAME, 'rb') as fp:

Return fp.read () result = client.asr (get_file_content (), 'wav', 8000, {' dev_pid': 1537,})

Print (result)

You need to install the pyaudio dependency package before running this code, and you can install it in the following ways because it will report an error on the win10 system. Go to this link https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio to download the corresponding installation package and install it.

After running, I said hello, and I can see and identify it. Then our small module functions are all done, and the next step is how to integrate them. It can be found that the judgment code of if matches [best _ match_index] in the face recognition code is to determine whether it is the owner of the computer, so we regard this judgment statement as the entry of the main function.

If matches[best _ match_index]:

# write the function after being identified here

Name = known_face_ namespace [best _ match_index]

So after recognition, we should ask the computer to issue an inquiry code, that is, the speech synthesis code, and then we package it into a function and reconstruct the face recognition code by the way.

Import cv2

Import time

Import numpy as np

Import face_recognition

Video_capture = cv2.VideoCapture (0)

My_image = face_recognition.load_image_file ("my.jpg")

My_face_encoding = face_recognition.face_encodings (my_image) [0]

Known_face_encodings = [

My_face_encoding

]

Known_face_names = [

"Admin"

]

Face_names = []

Face_locations = []

Face_encodings = []

Process_this_frame = Truedef speak (content):

Import sys

From aip import AipSpeech

From playsound import playsound

APP_ID =''

API_KEY =''

SECRET_KEY =''

Client = AipSpeech (APP_ID, API_KEY, SECRET_KEY)

Result = client.synthesis (content, 'zh', 1, {' vol': 5, 'per': 0,' spd': 5,})

If not isinstance (result, dict):

With open ('auido.mp3',' wb') as file:

File.write (result)

Filepath = eval (repr (sys.path [0]). Replace ('\','/') +'/ / auido.mp3'

Playsound (filepath) try:

While True:

Ret, frame = video_capture.read ()

Small_frame = cv2.resize (frame, (0,0), fx=0.25, fy=0.25)

Rgb_small_frame = small_frame [:,:,:-1]

If process_this_frame:

Face_locations = face_recognition.face_locations (rgb_small_frame)

Face_encodings = face_recognition.face_encodings (rgb_small_frame, face_locations)

Face_names = []

For face_encoding in face_encodings:

Matches = face_recognition.compare_faces (known_face_encodings, face_encoding)

Name = "Unknown"

Face_distances = face_recognition.face_distance (known_face_encodings, face_encoding)

Best_match_index = np.argmin (face_distances)

If matches[best _ match_index]:

Speak ("recognize the face, start asking for the code, please answer the next question")

Time.sleep (1)

Speak ("Heavenly King covers the Earth Tiger")

Error = 1 / 0

Name = known_face_ namespace [best _ match_index]

Face_names.append (name)

Process_this_frame = not process_this_frame

For (top, right, bottom, left), name in zip (face_locations, face_names):

Top * = 4

Left * = 4

Right * = 4

Bottom * = 4

Font = cv2.FONT_HERSHEY_DUPLEX

Cv2.rectangle (frame, (left, top), (right, bottom), (0,0,255), 2)

Cv2.rectangle (frame, (left, bottom-35), (right, bottom), (0,0,255), cv2.FILLED)

Cv2.putText (frame, name, (left + 6, bottom-6), font, 1.0, (255,255,255), 1)

Cv2.imshow ('Video', frame)

If cv2.waitKey (1) & 0xFF = = ord ('q'):

Break

Except Exception as e:

Print (e)

Finally:

Video_capture.release ()

Cv2.destroyAllWindows ()

One thing to note here is that because playsound will always occupy this resource when playing music, it will report an error when playing the next piece of music. The solution is to modify the playsound.py file under ~\ Python37\ Lib\ site-packages and find the following code

Add the winCommand ('close', alias) code under the sleep function and save it. Run and find that you can say both sentences normally. So after we say it, we are going to listen, and we also have to package a function.

Def record ():

Import wave

Import json

Import pyaudio

From aip import AipSpeech

APP_ID =''

API_KEY =''

SECRET_KEY =''

Client = AipSpeech (APP_ID, API_KEY, SECRET_KEY)

CHUNK = 1024

FORMAT = pyaudio.paInt16

CHANNELS = 1

RATE = 8000

RECORD_SECONDS = 3

WAVE_OUTPUT_FILENAME = "output.wav"

P = pyaudio.PyAudio ()

Stream = p.open (format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)

Print ("* recording")

Frames = []

For i in range (0, int (RATE / CHUNK * RECORD_SECONDS)):

Data = stream.read (CHUNK)

Frames.append (data)

Print ("* done recording")

Stream.stop_stream ()

Stream.close ()

P.terminate ()

Wf = wave.open (WAVE_OUTPUT_FILENAME, 'wb')

Wf.setnchannels (CHANNELS)

Wf.setsampwidth (p.get_sample_size (FORMAT))

Wf.setframerate (RATE)

Wf.writeframes (b''.join (frames))

Def get_file_content ():

With open (WAVE_OUTPUT_FILENAME, 'rb') as fp:

Return fp.read ()

Result = client.asr (get_file_content (), 'wav', 8000, {' dev_pid': 1537,})

Result = json.loads (str (result). Replace ("',''))

Return result ["result"] [0]

Modify the code after recognizing the face to the following

If matches[best _ match_index]:

Speak ("recognize the face, start asking for the code, please answer the next question")

Time.sleep (1)

Speak ("Heavenly King covers the Earth Tiger")

Flag = False

For times in range (0,3):

Content = record ()

If "Chicken stewed Mushroom" in content:

Speak ("password pass")

Flag = True

Break

Else:

Speak ("password failed, try again")

If flag:

Print ("unlock")

Else:

Print ("send email and upload pictures of bad faces!")

Error = 1 / 0

Name = known_face_ namespace [best _ match_index]

Run to see the effect, answer the computer chicken stewed mushrooms, the computer answer the code through. In this way, the function is basically complete.

Conclusion

As for the function of sending mail and the function of unlocking the lock screen, I will not realize them one by one. I think this should not be difficult for everyone. Lock screen function can HOOK to invalidate keyboard time, and then use the window to cover the entire desktop, as for the mailbox to send a lot of online articles.

The above is the editor for you to share how to use Python real-time monitoring, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report