Linux 使用python3 & opencv实现人脸识别demo流程

1.Linux下安装python3环境

	。。。 。。。

2.Linux下编译opencv源码 :

$cd opencv
$mkdir build && cd build
$cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D OPENCV_EXTRA_MODULES_PATH=~/Downloads/opencv_contrib/modules \
      -D ENABLE_NEON=ON \
      -D ENABLE_VFPV3=ON \
      -D BUILD_TESTS=OFF \
      -D OPENCV_ENABLE_NONFREE=ON \
      -D INSTALL_PYTHON_EXAMPLES=OFF \
      -D CMAKE_SHARED_LINKER_FLAGS='-latomic' \
      -D WITH_FFMPEG=ON \
      -D BUILD_EXAMPLES=OFF ..
$make -j4
$make install

在make install 时,可以通过修改.sh文件中的路径来指定安装路径,最后会生成 /usr/local/lib/python3.7/site-packages/cv2/python3.7/cv2.so

3.在shell输入

$pip3 install  dlib -vvv
$pip3 install  face_recognition
$pip3 install  numpy
$pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python		
//如果没有编译opencv源码可以通过该命令获取到cv2.so库
$python3
>>>import cv2				//加入cv2库
>>>cv2.__version__ 			//查看版本
>>> import face_recognition     //加入该库,有添加,训练人脸功能
>>>exit()
$ pip3 list					//如果失败,可以通过查看是否有可用包确定

4. $python3 face.py启动文件

5. 附face.py源码

import face_recognition
import cv2
import numpy as np

video_capture = cv2.VideoCapture(0)

obama_image = face_recognition.load_image_file("./0.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]

biden_image = face_recognition.load_image_file("./1.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]

known_face_encodings = [
    obama_face_encoding,
    biden_face_encoding
]
known_face_names = [
    "Barack Obama",
    "Joe Biden"
]

faceFound = 0
name = ""
prevName = ""

while True:
    if faceFound == 1 and name != prevName:
        print(name + " is at your door")
        message = client.messages \
            .create(
                 body= name + " is at your door.",
                 from_=fromPhone,
                 to=toPhone
             )   

    ret, frame = video_capture.read()
    rgb_frame = frame[:, :, ::-1]
    face_locations = face_recognition.face_locations(rgb_frame)
    face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
    for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
        matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
        face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
        best_match_index = np.argmin(face_distances)
        if matches[best_match_index]:
            faceFound=1
            prevName = name
            name = known_face_names[best_match_index]

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

        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        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()

猜你喜欢

转载自blog.csdn.net/weixin_43069863/article/details/120370455
今日推荐