cv2 使用最小化窗口捕获视频

问题描述

我有一个使用 cv2 的小型 python 脚本来捕获检测到的第一张人脸并仅在 cv2 窗口中显示该区域。一切正常。

目前,视频源在最小化时会冻结。 如果我将 cv2 窗口最小化到托盘,如何让我的脚本继续捕获视频?

编辑

我还想知道是否有更好的方法来减少 cpu负载。当前运行此脚本将使用 14 - 20% 的 cpu

from __future__ import division
from imutils.video import VideoStream
import face_recognition
import imutils
import cv2

POINTS = []


def landmarkTrackSmoothing(Box,factor,maxPoints=30):
    top = Box[0][0]
    bottom = Box[0][1]
    left = Box[0][2]
    right = Box[0][3]
    if len(POINTS) < maxPoints:
        maxPoints = len(POINTS)
    else:
        del POINTS[0]

    POINTS.append([top,bottom,left,right])
    mean = [int((sum(col)/len(col))/factor) for col in zip(*POINTS)]
    return mean


def cartoonFilter(roi):
    # 1) Edges
    gray = cv2.cvtColor(roi,cv2.COLOR_RGB2GRAY)
    gray = cv2.medianBlur(gray,5)
    edges = cv2.adaptiveThreshold(
        gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,9,9)

    # 2) Color
    color = cv2.bilateralFilter(roi,300,300)

    # 3) Cartoon
    return cv2.bitwise_and(color,color,mask=edges)


def OpenCamera():
    vs = VideoStream(0 + cv2.CAP_DSHOW,framerate=120).start()
    vs.stream.set(cv2.CAP_PROP_FRAME_WIDTH,1280)
    vs.stream.set(cv2.CAP_PROP_FRAME_HEIGHT,1024)
    roi = [0,0]
    prev = [0,0]

    # Add filter flags
    cartoonEffect = False

    # loop over frames from the video file stream
    while True:
        # grab the frame from the threaded video stream
        frame = vs.read()

        # downscale and convert to grayscale for fast processing
        # of landmark locations
        gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        gray = imutils.resize(frame,width=240)

        # calculate upscale factor for landmark locations
        factor = float(gray.shape[1]) / frame.shape[1]

        # detect the (x,y)-coordinates of the bounding Boxes
        # corresponding to each face in the input frame,then
        # the facial embeddings for each face
        Boxes = face_recognition.face_locations(gray)
        Box = list(map(list,Boxes))
        # t,b,l,r = 0,0

        # upscale landmark locations
        for i in range(len(Box)):
            Box = [landmarkTrackSmoothing(Box,factor)]

        # loop over the recognized faces
        if (len(Box) > 0):
            i = 0
            for (top,right,left) in Box:
                # grab frames from face coordinates
                if (i == 0):
                    roi = frame[top:bottom,left:right]
                    prev = top,right
                    if cartoonEffect:
                        roi = cartoonFilter(roi)
                    i += 1

        # check to see if we are supposed to display the output frame to
        # the screen
        if (len(Box) == 0):
            if (prev[0] > 0):
                roi = frame[prev[0]:prev[1],prev[2]:prev[3]]
            else:
                roi = frame

        cv2.namedWindow("Frame",cv2.WINDOW_norMAL)
        if (roi.any()):
            cv2.imshow("Frame",roi)
        cv2.resizeWindow("Frame",512,512)

        # continue looping until quit: expandable to add dynamic key commands for filters
        key = cv2.waitKey(1) & 0xFF

        if key == ord("q"):
            break
        if key == ord('c'):
            if cartoonEffect:
                cartoonEffect = False
            else:
                cartoonEffect = True

            # do a bit of cleanup on quit
    cv2.destroyAllWindows()
    vs.stop()


# Begin capturing
OpenCamera()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)