Skip to content
The Chat Archive
gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

Есть код: or imagepath in imagepaths: a. Считываем изображение из файла image =…

  1. User

    Есть код: or image_path in image_paths:

    a. Считываем изображение из файла

    image = cv2.imread(image_path)

    b. Детектируем лицо на изображении

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) faces = detector(gray)

    c. Вырезаем лицо из изображения (будет выбрано первое найденное лицо)

    if len(faces) == 1: face = faces[0] landmarks = sp(gray, face) aligned_face = dlib.get_face_chip(image, landmarks)

    d. Вычисляем вектор признаков из изображения лица

    face_descriptor = np.array(face_descriptor_extractor.compute_face_descriptor(aligned_face)) if face_descriptor.shape[0] == 128: # Проверяем размерность вектора признаков face_descriptors.append(face_descriptor)

    2. Индексируем векторы признаков

    face_descriptors = np.array(face_descriptors) face_descriptors = face_descriptors.reshape(-1, 128) kdtree = KDTree(face_descriptors)

    3. Загружаем свое изображение

    your_image_path = “/content/gdrive/MyDrive/photo_2022-08-22_20-53-55.jpg” your_image = cv2.imread(your_image_path) gray = cv2.cvtColor(your_image, cv2.COLOR_BGR2GRAY) your_faces = detector(gray)

    Исправь ошибки в этой части:

    if len(your_faces) == 1: your_face = your_faces[0] landmarks = sp(gray, your_face) aligned_face = dlib.get_face_chip(your_image, landmarks)

    d. Вычисляем вектор признаков вашего лица

    your_face_descriptor = np.array(face_descriptor_extractor.compute_face_descriptor(aligned_face))

    4. Находим 3 наиболее похожих фотографии

    _, indices = kdtree.query([your_face_descriptor], k=4)

    5. Выводим найденные 3 наиболее похожих фотографии

    for i, idx in enumerate(indices[0][1:]): similar_image = cv2.imread(image_paths[idx]) cv2.imshow(f"Similar Face {i+1}", similar_image)

    cv2.waitKey(0) cv2.destroyAllWindows()

  2. Assistant

    Исправления:

    1. Добавлен импорт KDTree:

    from sklearn.neighbors import KDTree

    1. Исправлен путь к изображению в строке your_image_path:

    your_image_path = "/content/gdrive/MyDrive/photo_2022-08-22_20-53-55.jpg"

    1. Исправлено название переменной с your_face на your_faces, так как она содержит список лиц:

    if len(your_faces) == 1: your_face = your_faces[0] landmarks = sp(gray, your_face) aligned_face = dlib.get_face_chip(your_image, landmarks)

    1. Добавлены проверки на размерность вектора признаков before_face_descriptor и your_face_descriptor:

    before_face_descriptor = np.array(face_descriptor_extractor.compute_face_descriptor(aligned_face)) if before_face_descriptor.shape[0] == 128: face_descriptors.append(before_face_descriptor)

    ...

    if your_face_descriptor.shape[0] == 128:

    4. Находим 3 наиболее похожих фотографии

    _, indices = kdtree.query([your_face_descriptor], k=4)

    5. Выводим найденные 3 наиболее похожих фотографии

    for i, idx in enumerate(indices[0][1:]): similar_image = cv2.imread(image_paths[idx]) cv2.imshow(f"Similar Face {i+1}", similar_image)

    cv2.waitKey(0) cv2.destroyAllWindows()