preface

I’ve long seen news employees spying on their bosses through facial recognition.

The environment

  • Raspberry PI 3+ python3.7
  • Windows 7 python3.6

process

First, the raspberry PI and the computer must be under an Intranet, under a router. Turn on the camera in raspberry PI Settings and install CV2, which has a lot of libraries that need to be installed manually. Principle introduction, face recognition is mainly dependent on OpencV open source face detection recognition classifier, just need to import it, classifier github download address: github.com/opencv/open… Download the entire project and unzip it, find the haarcascade_frontalface_default. XML file, and put it in the script file. The project address at the bottom of the article can be downloaded separately. Use HTTP to access the Intranet url to tell the working computer to press Win + D. This requires the computer to install a service. I used the simplest web.py, and specify the version when installing web.py.

import cv2
import time
import requests
capture = cv2.VideoCapture(0)Get the camera object
casc_path = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(casc_path)
color = (0.255.0)
font = cv2.FONT_HERSHEY_SIMPLEX


end_time=0
countdown=0
while(True) :# Read a frame of image
    ret,frame=capture.read()The first return value is bool. The second return value is bool
    if ret:
        # Convert to grayscale
        grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faceRects = faceCascade.detectMultiScale(grey, scaleFactor = 1.2, minNeighbors = 3, minSize = (32.32))# This is an array of parameters that can be output to see the size range of the face
        count = str(len(faceRects))

        if int(count) > 0:      If # is greater than 0, the face is detected
            start_time = time.time()Make sure the interval between requests is no less than 30 seconds, otherwise the experience will be bad
            if end_time<1:
                requests.get("http://192.168.1.165:8080/")
                end_time = time.time()
            countdown= int(start_time-end_time)   
            if start_time-end_time>30:
                requests.get("http://192.168.1.165:8080/") Change the Intranet IP address every time you restart your computer
                end_time = time.time()
            for faceRect in faceRects: # Draw box, frame each face individually
                x, y, w, h = faceRect    
                cv2.rectangle(frame, (x - 10, y - 10), (x + w + 10, y + h + 10), color, 2)                     
        cv2.putText(frame, "count:"+count, (10.40), font, 0.8, (0.255.255), 2)# Add a face number of text display
        # display image
        cv2.imshow("test", frame)    
        c = cv2.waitKey(10)# Wait for exit key
        if c & 0xFF == ord('q') :break   


Copy the code

The next step is to get your computer to accept HTTP requests. To get your computer to press Win + D, you need the PyAutoGUI library. At the beginning, I thought I needed shell script to search for a long time. The web. Py library was also searched by Baidu. The collection of various libraries fully demonstrates python’s ease of use, haha


# -*- coding: UTF-8 -*-
import web
import pyautogui
import time
urls = (
    '/' (. *).'hello'
)

app = web.application(urls, globals())


class hello:
    def GET(self, name):

        pyautogui.keyDown('win')
        pyautogui.keyDown('d')
        pyautogui.keyUp('d')
        pyautogui.keyUp('win')
        print("Performing")
        return "go"

if __name__ == "__main__":
    app.run()
Copy the code

Here’s how it looks:

conclusion

The next step might be to disguise the camera, use it for other colleagues, run specific software editors, or identify specific people, etc. Github address: github.com/koala9527/p…