【问题标题】:Webcam Frame to model.predict_generator - Jupyter Notebook & CV2网络摄像头框架到 model.predict_generator - Jupyter Notebook & CV2
【发布时间】:2021-05-10 16:38:22
【问题描述】:

我已经创建了这个自定义 CNN,对其进行了训练,现在希望尝试从我的网络摄像头实时传递帧以测试预测。

网络摄像头视频播放开始逐帧捕获,但是,我不确定如何处理帧以使其与 CNN 模型一起使用

任何建议将不胜感激

我已经提供了我想要达到的目标的完整代码

#imported necessities
import os
import pandas as pd 
import seaborn as sns
import matplotlib.pyplot as plt
import cv2
from matplotlib.image import imread
from IPython.display import clear_output
import time
import PIL.Image
from io import StringIO
import IPython.display
import numpy as np
from io import BytesIO
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dense, Conv2D, MaxPool2D, Dropout, Flatten, MaxPooling2D
from tensorflow.keras.callbacks import EarlyStopping

#Data Paths
data_dir = 'C:\\Users\\User\\Desktop\\DATAWeather'
test_path = data_dir+'\\Test\\'
train_path = data_dir+'\\Train\\'

#Variable to resize all of the images
image_shape = (224,224,3) #224*224*3 = 150528 Data Points : thats why we need image batch

#Apply a generator so it does not always get the same format of picture (recognizes different things)
image_gen = ImageDataGenerator(rotation_range=20, width_shift_range=0.1, height_shift_range=0.1, rescale=1/255, shear_range=0.1, zoom_range=0.1,horizontal_flip=True,fill_mode='nearest')

#setting up a base convolutional layer
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3,3),input_shape=image_shape, activation='relu',))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(filters=64, kernel_size=(3,3),input_shape=image_shape, activation='relu',))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(filters=64, kernel_size=(3,3),input_shape=image_shape, activation='relu',))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(4))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',optimizer='adam', metrics=['accuracy']), #model.summary()

#Create an early EPOCH stoppage based on the validation loss based off TWO epochs 
early_stop=EarlyStopping(monitor='val_loss', patience=2)

#TRAINING MODEL - use two to the power 
batch_size=32

#TWO generators 
train_image_gen = image_gen.flow_from_directory(train_path, target_size=image_shape[:2], color_mode='rgb', batch_size = batch_size, class_mode='categorical', shuffle=True)
test_image_gen = image_gen.flow_from_directory(test_path, target_size=image_shape[:2], color_mode='rgb', batch_size = batch_size, class_mode='categorical', shuffle=False)
results = model.fit_generator(train_image_gen, epochs=1, validation_data=test_image_gen, callbacks=[early_stop])


***

**def showarray(a, fmt='jpeg'):
    f = BytesIO()
    PIL.Image.fromarray(a).save(f, fmt)
    IPython.display.display(IPython.display.Image(data=f.getvalue()))
    
def get_frame(cam):
    # Capture frame-by-frame
        ret, frame = cam.read()
    
    #flip image for natural viewing
        frame = cv2.flip(frame, 1)
    
        return frame
        
cam = cv2.VideoCapture(0)
def make_1080p():
    cam.set(3, 224)
    cam.set(4, 224)
def change_res(width, height):
    cam.set(3, width)
    cam.set(4, height)
change_res(224, 224)
try:
    while(True):
            t1 = time.time()
            frame = get_frame(cam)
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            showarray(frame)
            t2 = time.time()
            print("%f FPS" % (1/(t2-t1)))
            # Display the frame until new frame is available
            clear_output(wait=True)
            Weather_Prediction_Cell = (frame)
            #Weather_Prediction_Cell /= 255
            model.predict_generator(frame)
            #print(Weather_Prediction_Cell)
            print(pred)**
            
            
except KeyboardInterrupt:
    cam.release()
    print("Stream stopped")
***

【问题讨论】:

    标签: tensorflow keras conv-neural-network jupyter cv2


    【解决方案1】:

    Keras 模型有一个方法叫做“predict”。它需要一个 np.array 或一个 np.arrays 列表作为输入(它应该与你的神经网络具有完全相同的形状。输入,包括批处理部分:例如(batch_count、width、height、channels))。您将其输入到 model.predict,然后它再次将结果作为 np.array 返回给您,并具有您的神经网络输出层的形状。我不习惯使用 opencv 的网络摄像头应用程序,但是如果您以某种方式在 np.array 中获取帧数据,您可以将其馈送到您的神经网络。也是。只要确定它的形状,并在需要时重新塑造它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-06
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2018-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多