樹莓派4b安裝人臉識別face_recognition、opencv、opencv_contrib

                                             首先是face_recognition安裝

一、到github上面查找中文資料,然後查看樹莓派安裝教程

https://github.com/ageitgey/face_recognition

樹莓派安裝的教程路徑是下面這個(中間有些地方和我不一樣,我照這個鏈接,沒成功過,安裝那個face_rec網速太慢,根本沒下載下來過)

https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65

二、需要更正的步驟

1、libatlas-dev 無法候選,這裏安裝替換的 libatlas-base-dev

2、dlib安裝按照例子如下,但是這裏需要更改

 

我是按照這樣安裝,網上查資料聽說這個版本才般配

sudo pip3 install dlib==19.7.0

然後直接開始安裝就好了

sudo pip3 install face_recognition

三、進行測試face_recognition

然後按照原鏈接還原更換交換區大小,不用下載示例代碼,我下載了之後好像不能用啥的,你可以參考這個鏈接直接使用

https://github.com/ageitgey/face_recognition/blob/master/README_Simplified_Chinese.md

主要使用就是這句話,中間那個文件夾是你知道的人照片文件夾,名字是圖片的文件名,後面那個文件夾放的是你不認識的人的圖片,然後會識別出名字並且打印出來,但是經過測試,速度真慢,而且只是單純的圖片識別,你難道不想玩玩在線攝像頭實時識別嗎?

face_recognition ./pictures_of_people_i_know/ ./unknown_pictures/

如果有攝像頭的小朋友,可以開始安裝openCV開始攝像頭識別了。

                                    最後是安裝opencv、opencv_contrib

一、配置樹莓派並打開攝像頭

打開攝像頭設置,啓用攝像頭

sudo raspi-config 

選擇是就好了

更新樹莓派軟件

sudo apt-get update 

sudo apt-get upgrade

二、安裝OpenCV的相關工具

sudo apt-get install build-essential cmake git pkg-config

三、安裝OpenCV的圖像工具包

sudo apt-get install libjpeg8-dev 
sudo apt-get install libtiff5-dev 
sudo apt-get install libjasper-dev 
sudo apt-get install libpng12-dev 

四、安裝視頻I/O包

sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev

五、安裝gtk2.0和優化函數包

sudo apt-get install libgtk2.0-dev
sudo apt-get install libatlas-base-dev gfortran

六、下載OpenCV源碼

git clone https://github.com/opencv/opencv.git

七、下載OpenCV_contrib

git clone https://github.com/opencv/opencv_contrib.git

八、安裝OpenCV

// 根據下載的版本而定
cd opencv
// 創建release文件夾
mkdir release
// 進入release目錄下
cd release
// cmake讀入所有源文件之後,自動生成makefile,複製下面所有(更改第三行的路徑),粘貼回車
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=/home/pi/software/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 \
-DCMAKE_SHARED_LINKER_FLAGS='-latomic' \
-D BUILD_EXAMPLES=OFF ..
// 編譯(建議 sudo make -j4 速度快呀)
sudo make -j4
// 安裝
sudo make install
//更新動態鏈接庫
sudo ldconfig


九、測試代碼來了

https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py

這個鏈接,就是測試代碼,速度比較快

import face_recognition
import cv2
import numpy as np

# This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
# other example, but it includes some basic performance tweaks to make things run a lot faster:
#   1. Process each video frame at 1/4 resolution (though still display it at full resolution)
#   2. Only detect faces in every other frame of video.

# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.

# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)

# Load a sample picture and learn how to recognize it.
obama_image = face_recognition.load_image_file("obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]

# Load a second sample picture and learn how to recognize it.
biden_image = face_recognition.load_image_file("biden.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]

# Create arrays of known face encodings and their names
known_face_encodings = [
    obama_face_encoding,
    biden_face_encoding
]
known_face_names = [
    "Barack Obama",
    "Joe Biden"
]

# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

while True:
    # Grab a single frame of video
    ret, frame = video_capture.read()

    # Resize frame of video to 1/4 size for faster face recognition processing
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

    # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
    rgb_small_frame = small_frame[:, :, ::-1]

    # Only process every other frame of video to save time
    if process_this_frame:
        # Find all the faces and face encodings in the current frame of video
        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:
            # See if the face is a match for the known face(s)
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
            name = "Unknown"

            # # If a match was found in known_face_encodings, just use the first one.
            # if True in matches:
            #     first_match_index = matches.index(True)
            #     name = known_face_names[first_match_index]

            # Or instead, use the known face with the smallest distance to the new face
            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_names[best_match_index]

            face_names.append(name)

    process_this_frame = not process_this_frame


    # Display the results
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        # Scale back up face locations since the frame we detected in was scaled to 1/4 size
        top *= 4
        right *= 4
        bottom *= 4
        left *= 4

        # Draw a box around the face
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

        # Draw a label with a name below the face
        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)

    # Display the resulting image
    cv2.imshow('Video', frame)

    # Hit 'q' on the keyboard to quit!
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()
//下載一下奧巴馬和拜登兩個人的照片各一張放到路徑下
python3 python3 facerec_from_webcam_faster.py

這裏沒有屏幕的兄弟姐妹們,下載個Xmanager,然後就可以了,會顯示一個圖相框來,我這裏識別王力宏和蒲巴甲

測試圖片

十、測試demo下載

速度還比較快速,比直接對比兩張圖片快得多,demo下載的路徑是這個

https://github.com/ageitgey/face_recognition

我用的那個網絡攝像頭識別實時視頻中的人臉,更快的版本,大家裝好了以上三個,就可以開啓自己人臉識別旅程了。

打賞二維碼,多謝支持

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章