如何调整图片大小以适合屏幕尺寸(python)

时间:2020-05-16 09:58:44

标签: python opencv image-resizing

我有一个机器学习的面部识别脚本,该脚本读取您放置在同一目录中的图像,并显示带有标签的面部。但是,如果我给它提供一个大到适合在屏幕上显示的图像,那么它就不合适了。我将如何在Python脚本中调整此图像的大小以适合我的屏幕。 谢谢

CODE

import face_recognition as fr
import os
import cv2
import face_recognition
import numpy as np
from time import sleep


def get_encoded_faces():
    """
    looks through the faces folder and encodes all
    the faces

    :return: dict of (name, image encoded)
    """
    encoded = {}

    for dirpath, dnames, fnames in os.walk("./faces"):
        for f in fnames:
            if f.endswith(".jpg") or f.endswith(".png"):
                face = fr.load_image_file("faces/" + f)
                encoding = fr.face_encodings(face)[0]
                encoded[f.split(".")[0]] = encoding

    return encoded


def unknown_image_encoded(img):
    """
    encode a face given the file name
    """
    face = fr.load_image_file("faces/" + img)
    encoding = fr.face_encodings(face)[0]

    return encoding


def classify_face(im):
    faces = get_encoded_faces()
    faces_encoded = list(faces.values())
    known_face_names = list(faces.keys())

    face_locations = face_recognition.face_locations(img)
    unknown_face_encodings = face_recognition.face_encodings(img, face_locations)

    face_names = []
    for face_encoding in unknown_face_encodings:
        # See if the face is a match for the known face(s)
        matches = face_recognition.compare_faces(faces_encoded, face_encoding)
        name = "Unknown"

        # use the known face with the smallest distance to the new face
        face_distances = face_recognition.face_distance(faces_encoded, face_encoding)
        best_match_index = np.argmin(face_distances)
        if matches[best_match_index]:
            name = known_face_names[best_match_index]

        face_names.append(name)

        for (top, right, bottom, left), name in zip(face_locations, face_names):
            # Draw a box around the face
            cv2.rectangle(img, (left-20, top-20), (right+20, bottom+20), (255, 0, 0), 2)

            # Draw a label with a name below the face
            cv2.rectangle(img, (left-20, bottom -15), (right+20, bottom+20), (255, 0, 0), cv2.FILLED)
            font = cv2.FONT_HERSHEY_DUPLEX
            cv2.putText(img, name, (left -20, bottom + 15), font, 1.0, (255, 255, 255), 2)


    # Display the resulting image
    while True:

        cv2.imshow('Image', img)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            return face_names 


print(classify_face("test.jpg"))

如您所见,我添加了一些注释以帮助完成此过程。 任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

使用matplotlib.pyplot.imshow,那么您就不会遇到这个问题。在大多数情况下,OpenCV都会这样做,所以我更喜欢matplotlib

import matplotlib.pyplot as plt

plt.imshow(img)
plt.show()

答案 1 :(得分:0)

您可以安装以下模块:

pip install screeninfo

然后使用以下代码获取屏幕尺寸:

import screeninfo

for monitor in screeninfo.get_monitors():
     print(str(monitor))

从字符串中提取尺寸,然后使用cv.resize()函数使图像适合屏幕。

如果您已经知道屏幕的分辨率,另一种解决方案是添加一个简单的if语句,如果图像的尺寸超过显示器的尺寸,则可以将其调整为当前尺寸。