def process(self): """ Process of recognition faces in video by frames. Writes id as…
- User
def process(self): """ Process of recognition faces in video by frames. Writes id as uuid4. Returns: tuple: with list of faces and list of names """ name = uuid.uuid4() while True: ret, frame = self.video.read() if not ret: break
codegray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = self.face_cascade.detectMultiScale( gray, scaleFactor=1.1, minNeighbors=5, minSize=(100, 100)) for (x, y, w, h) in faces: cur_face = gray[y:y + h, x:x + w] rec = cv2.face.LBPHFaceRecognizer.create() rec.train([cur_face], np.array(0)) f = True for face in self.faces_list: _, confidence = rec.predict(face) if confidence < self.threshold: f = False if f: label = name name = uuid.uuid4() self.faces_list.append(cur_face) self.names_list.append(label) self._close() return self.faces_list, self.names_listРазбей здесь обработку каждого кадра в 8 потоков
- Assistant
Чтобы разбить обработку каждого кадра в 8 потоков, можно использовать модуль
concurrent.futuresдля создания пула потоков. Вот измененный код, который выполняет обработку кадров в 8 потоках:python - User
Пиши код с такими ковычками "
- Assistant
import concurrent.futures
def process_frame(frame, face_cascade, faces_list, names_list, threshold, name): gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale( gray, scaleFactor=1.1, minNeighbors=5, minSize=(100, 100))
codefor (x, y, w, h) in faces: cur_face = gray[y:y + h, x:x + w] rec = cv2.face.LBPHFaceRecognizer.create() rec.train([cur_face], np.array(0)) f = True for face in faces_list: _, confidence = rec.predict(face) if confidence < threshold: f = False if f: label = name name = uuid.uuid4() faces_list.append(cur_face) names_list.append(label)