Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

This is a sample Python script. Press Shift+F10 to execute it or replace it with your…

  1. User

    This is a sample Python script.

    Press Shift+F10 to execute it or replace it with your code.

    Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

    import os import pickle

    import tkinter as tk from tkinter import messagebox import face_recognition

    def get_button(window, text, color, command, fg='white'): button = tk.Button( window, text=text, activebackground="black", activeforeground="white", fg=fg, bg=color, command=command, height=2, width=20, font=('Helvetica bold', 20) )

    return button
    

    def get_img_label(window): label = tk.Label(window) label.grid(row=0, column=0) return label

    def get_text_label(window, text): label = tk.Label(window, text=text) label.config(font=("sans-serif", 21), justify="left") return label

    def get_entry_text(window): inputtxt = tk.Text(window, height=2, width=15, font=("Arial", 32)) return inputtxt

    def msg_box(title, description): messagebox.showinfo(title, description)

    def recognize(img, db_path): # it is assumed there will be at most 1 match in the db

    embeddings_unknown = face_recognition.face_encodings(img)
    if len(embeddings_unknown) == 0:
        return 'no_persons_found'
    else:
        embeddings_unknown = embeddings_unknown[0]
    
    db_dir = sorted(os.listdir(db_path))
    
    match = False
    j = 0
    while not match and j < len(db_dir):
        path_ = os.path.join(db_path, db_dir[j])
    
        file = open(path_, 'rb')
        embeddings = pickle.load(file)
    
        match = face_recognition.compare_faces([embeddings], embeddings_unknown)[0]
        j += 1
    
    if match:
        return db_dir[j - 1][:-7]
    else:
        return 'unknown_person'
    

    помоги мне сделать программу, которая будет считывать данные с вебкамеры, и если в камере есть человек котоый есть в базе данных, будет выведен в консоль текст с именем человека

  2. Assistant

    Sure! I can help you with that. Here's a Python script that uses the opencv-python library to capture video from the webcam and face_recognition library to recognize faces from the captured frames. It will then check if the recognized face exists in the database and display the name of the person in the console:

    import cv2
    import os
    import pickle
    import face_recognition
    
    def recognize_faces_from_webcam(db_path):
        # Load face embeddings from the database
        db_dir = sorted(os.listdir(db_path))
        embeddings_db = []
        for file_name in db_dir:
            path = os.path.join(db_path, file_name)
            with open(path, 'rb') as file:
                embeddings = pickle.load(file)
                embeddings_db.append(embeddings)
    
        # Open the webcam
        video_capture = cv2.VideoCapture(0)
    
        while True:
            # Capture frame-by-frame
            ret, frame = video_capture.read()
    
            # Convert the frame from BGR color (used by OpenCV) to RGB color (used by face_recognition)
            rgb_frame = frame[:, :, ::-1]
    
            # Find all face locations and their encodings in the current frame
            face_locations = face_recognition.face_locations(rgb_frame)
            face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
    
            for face_encoding in face_encodings:
                # Compare the face encoding with the faces in the database
                matches = face_recognition.compare_faces(embeddings_db, face_encoding)
    
                # Check if there is a match
                if any(matches):
                    # Get the index of the matched face in the database
                    matched_face_index = matches.index(True)
    
                    # Get the name of the person from the database file name
                    matched_face_name = db_dir[matched_face_index][:-7]
    
                    # Print the name of the matched person
                    print("Matched person:", matched_face_name)
    
            # Display the resulting image
            cv2.imshow('Video', frame)
    
            # Press 'q' to quit
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
    
        # Release the video capture and close the window
        video_capture.release()
        cv2.destroyAllWindows()
    
    # Replace 'db_path' with the path to your database directory
    db_path = 'path/to/your/database'
    recognize_faces_from_webcam(db_path)
    

    Before running this script, make sure you have opencv-python and face_recognition libraries installed. You can install them using the following command:

    pip install opencv-python face-recognition
    

    Also, replace 'path/to/your/database' with the actual path to your database directory, which contains the face embeddings of the known persons.

    Once you run the script, it will start capturing frames from the webcam, detect faces in each frame, compare them with the faces in your database, and print the name of the matched person in the console.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.