image - 仅在cv2视频帧的ROI部分上预测模型类

标签 image numpy opencv image-processing keras

以下是我的带有ROI矩形的代码。我只想基于ROI框中捕获的图像而不是整个图像对模型进行预测

import numpy as np
import cv2
from keras.models import load_model
import keras
model = keras.models.load_model('Desktop/BCD_nothing_rmsprop.h5')
def sketch_transform(frame):
    return frame

cam_capture = cv2.VideoCapture(0)
cv2.destroyAllWindows()
upper_left = (50, 50)
bottom_right = (300, 300)
while True:
    _, image_frame = cam_capture.read()
    #Rectangle marker
    r = cv2.rectangle(image_frame, upper_left, bottom_right, (100, 50, 200), 5)
    rect_img = image_frame[upper_left[1] : bottom_right[1], upper_left[0] : bottom_right[0]]
    
    sketcher_rect = rect_img
    sketcher_rect = sketch_transform(sketcher_rect)
    
    
    #Replacing the sketched image on Region of Interest
    image_frame[upper_left[1] : bottom_right[1], upper_left[0] : bottom_right[0]] = sketcher_rect
    classes = model.predict(np.array([rect_img]))
    cv2.imshow("Sketcher ROI", image_frame)
    k = cv2.waitKey(30) & 0xff
    if k ==27:
        break
        
cam_capture.release()
cv2.destroyAllWindows()
    
如何仅预测矩形ROI或变量rect_img内的图像。

最佳答案

之前创建大小调整功能。然后尝试

while True:
    _, image_frame = cam_capture.read()
    #Rectangle marker
    r = cv2.rectangle(image_frame, upper_left, bottom_right, (100, 50, 200), 5)
    rect_img = image_frame[upper_left[1] : bottom_right[1], upper_left[0] : bottom_right[0]]

    sketcher_rect = rect_img
    ##Resize ROI first by create function
    sketcher_rect = resize_function(sketcher_rect,(width,height))


    #Replacing the sketched image on Region of Interest
    image_frame[upper_left[1] : bottom_right[1], upper_left[0] : bottom_right[0]] = sketcher_rect
    classes = model.predict(sketcher_rect.reshape(1,width,height,3))
    cv2.imshow("Sketcher ROI", image_frame)
    k = cv2.waitKey(30) & 0xff
    if k ==27:
        break
cam_capture.release()
cv2.destroyAllWindows()

关于image - 仅在cv2视频帧的ROI部分上预测模型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64296191/

相关文章:

image - 对燃气表进行 OCR 识别

java - 缓冲图像 INT/4BYTE/USHORT

python - numpy.random 没有属性 'choice'

python - 对平面上有 4 个点的图片进行不失真/去扭曲处理

c++ - 如何在 SuperpixelSLIC 中找到段的唯一标签

html - 使 <img> 标签可切换 - 响应 TAB 键导航

css - 如何在 Maven 元素中分离图像文件夹和 css 文件夹以相互协作

python - 如何使用 scipy 稀疏矩阵 column_stack 一个 numpy 数组?

python - 如何在迭代器中屏蔽除第 n 个元素之外的每个元素?

c# - 如何使用 Emgu CV 检测图像中的箭头?