【问题标题】:OpenCV - overwriting live videoOpenCV - 覆盖实时视频
【发布时间】:2020-11-02 12:59:54
【问题描述】:

我制作了一个卷积神经网络,它可以预测人脸并返回坐标 (y1, x1, y2, x2)。我能够创建用作覆盖所需坐标的掩码的矩形。我需要一种实时覆盖图像的方法。有没有办法在不保存帧的情况下获得实时图像序列,只需覆盖它们,以及如何在 openCV 中提取坐标?我正在使用 pyplot 并正在保存图像,它很慢且无效。

【问题讨论】:

  • 您要实时修改和渲染图像吗?
  • 想实时修改。
  • 到目前为止你尝试了什么?我们可以帮助改进您的代码,但没有它就不能说什么。请编辑帖子以包含最小的示例代码。
  • 很想分享代码,我可以分享的是我如何获得图片中的坐标。我不知道的是,如何从视频中提取帧而不将图像保存在任何文件中,只是在变量中,所以我可以在不保存图片的情况下处理图片,而不是再次将帧组合到视频中。
  • 您使用的是哪个神经网络?在其代码中发现帧保存到文件的行后,您可以在该点获取 numpy 数组以进行处理而无需保存。

标签: python opencv


【解决方案1】:

是的,所以我设法想出了一个解决方案,但我发现,1 帧大约需要 0.54s 来计算,所以 2FPS,不适合直播,所以我切换到 haarcascade .

下面的代码用于配置和调用模型。

from numpy import expand_dims
from mrcnn.config import Config
from mrcnn.model import MaskRCNN
from mrcnn.model import mold_image
import cv2
import time



# define the prediction configuration
class PredictionConfig(Config):
    # define the name of the configuration
    NAME = "face_cfg"
    # number of classes (background + face)
    NUM_CLASSES = 1 + 1
    # simplify GPU config
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1

def classify_image(image,model,cfg):
    # convert pixel values (e.g. center)
    scaled_image = mold_image(image, cfg)
    # convert image into one sample
    sample = expand_dims(scaled_image, 0)
    # make prediction
    tic = time.time()
    yhat = model.detect(sample, verbose=0)[0]
    print(time.time() - tic)
    return yhat['rois']

def image_bnd_highlight(image,coordinates):
    for box in coordinates:
        # get coordinates
        y1, x1, y2, x2 = box
        # create the shape
        new_img = cv2.rectangle(image,(x1,y1),(x2,y2),(255,255,255),5)
        return new_img

# create config
cfg: PredictionConfig = PredictionConfig()
# define the model
model = MaskRCNN(mode='inference', model_dir='./', config=cfg)
# load model weights
model_path = 'mask_rcnn_face_cfg_0029.h5'
model.load_weights(model_path, by_name=True)
definitive_model = model

然后我调用我在上面创建的函数。

import cv2 as cv
import acapture
from RealTime import definitive_model
from RealTime import cfg
from RealTime import classify_image
from RealTime import image_bnd_highlight
import time

# cap = acapture.open(0)
cap = cv.VideoCapture(0)
cap.set(3,128) #set frame width
cap.set(4,128) #set frame height
cap.set(cv.CAP_PROP_FPS, 2) #adjusting fps to 2
# cap.set(cv.CAP_PROP_BUFFERSIZE,3)

# if not cap.isOpened():
#     print("Cannot open camera")
#     exit()
while True:
    # Capture frame-by-frame
    ret, frame = cap.read()
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    # let's resize our image to be 150 pixels wide, but in order to
    # prevent our resized image from being skewed/distorted, we must
    # first calculate the ratio of the *new* width to the *old* width
    r = 150.0 / frame.shape[1]
    dim = (150, int(frame.shape[0] * r))
    # perform the actual resizing of the image
    resized = cv.resize(frame, dim, interpolation=cv.INTER_AREA)

    # tic = time.time()
    coords = classify_image(resized,definitive_model,cfg)
    # print(time.time() - tic)
    image = image_bnd_highlight(resized,coords)
    # Display the resulting frame
    cv.imshow('frame', image)
    if cv.waitKey(1) == ord('q'):
        break


# When everything done, release the capture
cap.release()
cv.destroyAllWindows()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多