1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
import cv2 import os import numpy as np import pickle import paho.mqtt.client as mqtt import base64 import time
mqtt_broker = "192.168.137.1" mqtt_port = 1883
mqtt_topic = "video_stream"
client = mqtt.Client("RaspberryPi") client.connect(mqtt_broker, mqtt_port, 60)
cap = cv2.VideoCapture(0) cap.set(3, 640) cap.set(4, 480)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml') current_id = 0 label_ids = {} x_train = [] y_labels = []
def face_recognition(): num_count = 0 label_id_name = {} with open("label.pickle", 'rb') as f: origin_labels = pickle.load(f).items() label_id_name = {v: h for h, v in origin_labels} print(label_id_name) recognizer = cv2.face.LBPHFaceRecognizer_create() recognizer.read('my_trainer.xml') while True: ret, img = cap.read(); img = cv2.flip(img, 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) num_count += 1 for (x, y, w, h) in faces: if num_count >= 5: id_, conf = recognizer.predict(gray[y:y + h, x:x + w]) print("id = ",label_id_name[id_],"conf = ",conf) if 20 <= conf <= 54: cv2.putText(img, str(label_id_name[id_]), (x + 5, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) else: cv2.putText(img, str("NULL"), (x + 5, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90] result, encoded_frame = cv2.imencode('.jpg', img, encode_param)
if result: frame_base64 = base64.b64encode(encoded_frame.tobytes()) client.publish(mqtt_topic, frame_base64) k = cv2.waitKey(100) & 0xff if k == 27: break
def input_face(name): count = 0 all_num = 60 folder = os.path.exists('./dataset/' + name) if not folder: os.makedirs('./dataset/' + name) print("创建文件夹成功") while True: ret, img = cap.read() img = cv2.flip(img, 1) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5)
folder = os.path.exists('./dataset/' + name) if not folder: os.makedirs('./dataset/' + name) print("创建文件夹成功") for (x, y, w, h) in faces: if count%20==0: print("请输入任意键继续") if count == 0: print("请摆正你的脸,等待开启") cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) count += 1 path_jpg = "./dataset/" + name + '/' + str(count) + ".jpg" cv2.imwrite(path_jpg, gray[y:y + h, x:x + w]) print(path_jpg) encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90] result, encoded_frame = cv2.imencode('.jpg', img, encode_param)
if result: frame_base64 = base64.b64encode(encoded_frame.tobytes()) client.publish(mqtt_topic, frame_base64)
k = cv2.waitKey(100) & 0xff if k == 27: break elif count >= all_num: break
def train_face(): global current_id recognizer = cv2.face.LBPHFaceRecognizer_create() for root, dirs, files in os.walk('dataset'): for file in files: path = os.path.join(root, file) image = cv2.imread(path) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) image_array = np.array(gray, "uint8") label = os.path.basename(root)
if label not in label_ids: label_ids[label] = current_id current_id += 1 id_ = label_ids[label]
faces = face_cascade.detectMultiScale(image_array, scaleFactor=1.5, minNeighbors=5)
for (x, y, w, h) in faces: roi = image_array[y:y + h, x:x + w] x_train.append(roi) y_labels.append(id_) with open("label.pickle", "wb") as f: pickle.dump(label_ids, f) print(label_ids) recognizer.train(x_train, np.array(y_labels)) recognizer.save("my_trainer.xml")
name = input("please input name :") if len(name) != 0: input_face(name) train_face() face_recognition()
cap.release() cv2.destroyAllWindows()
|