簡體   English   中英

我怎么可能使用從我的網絡攝像頭生成的幀作為我的 Keras model 的輸入?

[英]How possibly can I use the frames generated from my webcam to be the input of my Keras model?

我想將img_path更改為從 opencv 生成的幀:

img = image.load_img(img_path, target_size=(224, 224))

我該如何重寫它?

我假設您正在使用的image.load_img() function 是來自keras_utils package 的那個。

正如文檔中所述, load_img()接受圖像的路徑作為第一個參數並返回:

返回:一個 PIL Image 實例。

It is not mentioned in the question, but if you read the frames from the camera using opencv they should already be numpy arrays which you could to pass to your model. 當然,您應該在之前將它們調整為 (224,224)( 如何使用 opencv 調整圖像大小)。

但是,如果您想要 PIL 圖像(具有與load_img()返回的圖像相同的類型),您需要將 opencv 幀(numpy 數組)轉換為 PIL 圖像。 按照@ZdaR 的這個問答進行此轉換:

import cv2
import numpy as np
from PIL import Image

img = cv2.imread("path/to/img.png")

# You may need to convert the color.
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
im_pil = Image.fromarray(img)

# For reversing the operation:
im_np = np.asarray(im_pil)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM