Выпуск камеры доступен через OpenCV Python - PullRequest
0 голосов
/ 10 ноября 2019

Я пытаюсь транслировать кадры видеокамеры в колбу api и обрабатывать их, чтобы обнаружить лица во всех кадрах и сохранить их на диск. У меня есть мой код здесь,

def face(video_capture):

    # Grab the list of names and the list of encodings
    known_face_ids = list(all_face_encodings.keys())
    known_face_encodings = np.array(list(all_face_encodings.values()))

    while True:

        process_this_frame = True
        ret, frame = video_capture.read()

        small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

        rgb_small_frame = small_frame[:, :, ::-1]

        if process_this_frame:
            face_locations = face_recognition.face_locations(rgb_small_frame)
            face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

            names_present = []
            for face_encoding in face_encodings:
                matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
                name = "Unknown"

                if True in matches:
                    first_match_index = matches.index(True)
                    name = known_face_ids[first_match_index]
                names_present.append(name)

        process_this_frame = not process_this_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()

    return names_present

@app.route('/start_matching', methods=['POST'])
def attendance():

    if request.method == 'POST':
        if 'camera_id' not in request.args:
            return 'Please provide the camera details'

        camera_id = request.args.get('camera_id')
        video_capture = cv2.VideoCapture(int(camera_id))
        names_present = face(video_capture)

Я пытаюсь получить интерфейс с кнопкой запуска и остановки. 'start' должен вызвать вышеуказанный API 'start_matching'. Это прекрасно работает и захватывает поток, разбивая их на кадры, а затем сравнивает кодировку с известной кодировкой, чтобы выяснить имена, присутствующие там. Однако 'stop' должен остановить потоковую передачу камеры на вышеуказанный API и вернуть 'names_present'.

Я попробовал еще один вызов API,

@app.route('/stop_matching', methods=['POST'])
def stop_attendance():
    camera_id = request.args.get('camera_id')
    video_capture = cv2.VideoCapture(int(camera_id))
    video_capture.release()
    return "Stopped the camera"

Но это не останавливает камеру и не возвращает список names_present. Кто-нибудь может указать мне правильное направление?

...