python - 如何在使用 cv2.resize() 放大图像时保持更高的 FPS

标签 python performance opencv ubuntu-16.04 webcam

我正在运行一个简单的代码来测量我的实时流媒体(使用网络摄像头)的 FPS。当我将图像调整为更大的帧时,FPS 会降低。有没有办法在扩大帧(通过调整大小功能)的同时保持 FPS。或者这是一个不可避免的权衡?

这是使用 face_recognition 库进行人脸识别的代码。当我调整到更大的尺寸时,FPS(每秒帧数)变慢。 有没有办法在使用 cv2.resize() 放大图像的同时保持更高的 FPS?

import face_recognition
import cv2

video_capture = cv2.VideoCapture(0)
#video_capture.set(cv2.CAP_PROP_FPS, 30)
# Load a sample picture and learn how to recognize it.
obama_image = face_recognition.load_image_file("osama LinkedIN.jpg")

obama_face_encoding = face_recognition.face_encodings(obama_image)[0]


# Load a second sample picture and learn how to recognize it.
imran_shafqat_image = face_recognition.load_image_file("haris intern3.jpg")
imran_shafqat_face_encoding = face_recognition.face_encodings(imran_shafqat_image)[0]


# Create arrays of known face encodings and their names
known_face_encodings = [
    obama_face_encoding,
    imran_shafqat_face_encoding,
   # obama_face_encoding2
   # biden_face_encoding
]
known_face_names = [
    "Osama Naeem",
    "Imran Shafqat"
 #   "random guy2"
]

# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
fxx = 1.5
fyy = 1.5
while True:
    # Grab a single frame of video
    ret, frame = video_capture.read()

    # Resize frame of video to 1/4 size for faster face recognition processing
    small_frame = cv2.resize(frame, (0, 0), fx=fxx, fy=fyy)

    # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
    rgb_small_frame = small_frame[:, :, ::-1]
    #rgb_small_frame = frame[:, :, ::-1]
    # Only process every other frame of video to save time
    if process_this_frame:
        # Find all the faces and face encodings in the current frame of video
        face_locations = face_recognition.face_locations(rgb_small_frame)
        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

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

            # If a match was found in known_face_encodings, just use the first one.
            if True in matches:
                first_match_index = matches.index(True)
                name = known_face_names[first_match_index]

            face_names.append(name)

    process_this_frame = not process_this_frame


    # Display the results
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        # Scale back up face locations since the frame we detected in was scaled to 1/4 size
        top *= (1/fxx)
        right *= (1/fxx)
        bottom *= (1/fyy)
        left *= (1/fyy)

        # Draw a box around the face
        cv2.rectangle(frame, (round(left), round(top)), (round(right), round(bottom)), (0, 0, 255), 2)

        # Draw a label with a name below the face

        #cv2.rectangle(frame, (round(left) - 35, round(bottom) - 40), (round(right), round(bottom)), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (round(left) + 6, round(bottom) - 6), font, 0.5, (255, 255, 255), 1)

    # Display the resulting image
    cv2.imshow('Video', 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()


代码工作正常,但我想在将它放大到更大尺寸时保持 FPS 保持相同的速率。

最佳答案

当您使用 cv2.resize() 放大图像时,您会创建一个更大的图像,从而增加每帧的处理时间。本质上,您的程序必须做额外的工作来处理更多的像素。但是,可以提高 FPS 的可能解决方案是使用 multithreading .此方法允许您通过减少 I/O 延迟来提高 FPS,而不是减少处理每个调整大小的帧所需的时间。这个想法是在主线程中进行处理时将阅读框架分离到它自己的独立线程中。这是一个小部件,显示如何将阅读框架和处理分离到单独的线程中。

from threading import Thread
import cv2, time

class VideoStreamWidget(object):
    def __init__(self, src=0):
        self.capture = cv2.VideoCapture(src)
        # Start the thread to read frames from the video stream
        self.thread = Thread(target=self.update, args=())
        self.thread.daemon = True
        self.thread.start()

    def update(self):
        # Read the next frame from the stream in a different thread
        while True:
            if self.capture.isOpened():
                (self.status, self.frame) = self.capture.read()
            time.sleep(.01)

    def show_frame(self):
        # Display frames in main program
        cv2.imshow('frame', self.frame)
        key = cv2.waitKey(1)
        if key == ord('q'):
            self.capture.release()
            cv2.destroyAllWindows()
            exit(1)

if __name__ == '__main__':
    video_stream_widget = VideoStreamWidget()
    while True:
        try:
            video_stream_widget.show_frame()
        except AttributeError:
            pass

关于python - 如何在使用 cv2.resize() 放大图像时保持更高的 FPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55727883/

相关文章:

python - 将 tensorflow 1.x.x 模型加载到 tensorflow 2.x.x

Python - 使字符串长度相等

python - Pytesser 不准确

python - 有没有比 np.diff 更快的替代品?

python - 为什么 range() 函数比乘法项目慢以获取嵌套列表中的副本?

python - 如何使用OpenCV使图像上的文字清晰锐利

image - 检测纸质目标的圆圈和射击

python - 绘制带有条件的 bool 数据框

c++ - 解释来自 OpenCV matchShapes() 的数字

mysql - 如何比较多个表列的多个值?