简体   繁体   中英

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

I want to change the img_path to the frames generated from opencv:

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

How can I rewrite it?

I am assuming that the image.load_img() function that you are using is the one from keras_utils package.

As it is noted in the documentation, load_img() accepts path to the image as the first parameter and returns:

Returns: A PIL Image instance.

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. Of course you should resize them to (224,224) before ( how to resize the image using opencv ).

However, if you want to have the PIL images (to have the same type as the one returned by load_img() ), you need to convert your opencv frames (numpy array) to PIL image. Follow this question and answer by @ZdaR to do this conversion:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM