摄像头实时视频 tensorflow分析时间戳

背景:现在摄像头RTSP协议不能给我们,相机拍摄时间,所以需要用人工智能分析 H256视频 时间显示的时间

1、项目流程图:


业务流程.png

2、tensorflow 数据预处理

import tensorflow as tf
import glob
import matplotlib.pyplot as plt
from keras_preprocessing.image import load_img,img_to_array,save_img
import numpy as np

# image_name = './1668159814053_time.jpg'
image_list = glob.glob(r"./*.jpg")
index = 1
for image_name in image_list:
    image = load_img(image_name)

    # plt.imshow(image)
    #将图片转换为数组
    image = img_to_array(image)

    image = image.astype(dtype='uint8')
    image = tf.image.rgb_to_grayscale(image)

    b = tf.less_equal(image, 210)
    c = tf.where(condition=b, x=0, y=255)


    height = 68
    width = 740
    length = int(740/32)
    for x in range(length):
        if x == 4 or x == 7 or x == 10 or x == 13 or x == 16 or x == 19:
            continue
        offset_width = 32*x
        d = tf.image.crop_to_bounding_box(c, 0, offset_width, height, 32);
        name = "./sub/"+str(index)+".jpg"
        save_img(name, d)
        index = index +1

2、跑出模型,并保存

import tensorflow as tf
import random
from keras_preprocessing.image import load_img,img_to_array,save_img
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
import numpy as np
from tensorflow import keras



def create_record(records_path, data_path, img_txt):
    # 读取图片信息,并且将读入的图片顺序打乱
    img_list = []
    with open(img_txt, 'r') as fr:
        img_list = fr.readlines()
    random.shuffle(img_list)
    cnt = 0
    # 遍历每一张图片信息
    temp_img = []
    temp_label = []
    for img_info in img_list:
        # 图片相对路径
        img_name = img_info.split(' ')[0]
        # 图片类别
        img_cls = int(img_info.split(' ')[1])
        img_path = data_path + img_name
        img = load_img(img_path)
        #将图片转换为数组
        img = img_to_array(img)
        img = tf.less_equal(img, 210)
        img = tf.where(condition=img, x=0, y=255)

        img = tf.image.rgb_to_grayscale(img)

        img = img.numpy().reshape(1, -1).reshape(32, 68)
        img = img.astype(dtype='uint8')

        temp_img.append(img)
        temp_label.append(img_cls)

    temp_img = np.array(temp_img)
    temp_label = np.array(temp_label)

    x_valid,x_train,x_test=temp_img[:400],temp_img[400:1400],temp_img[1400:]
    y_valid,y_train,y_test=temp_label[:400],temp_label[400:1400],temp_label[1400:]
  
    scaler=StandardScaler()

    # 训练
    x_train_scaled = scaler.fit_transform(x_train.astype(np.float32).reshape(-1,1)).reshape(-1,32,68)
    #验证
    x_valid_scaled = scaler.transform(x_valid.astype(np.float32).reshape(-1,1)).reshape(-1,32,68)
    # 测试
    x_test_scaled = scaler.transform(x_test.astype(np.float32).reshape(-1,1)).reshape(-1,32,68)

    #激活函数选择了relu,优化器选择SGD

    model= keras.models.Sequential([
            keras.layers.Flatten(input_shape=[32, 68]),
            keras.layers.Dense(200, activation='relu'),
            keras.layers.Dense(50, activation='relu'),
            keras.layers.Dense(10, activation='softmax')
        ])
    print ('1 ----------------')
    model.compile(optimizer='sgd',
                loss='sparse_categorical_crossentropy',
                metrics=['accuracy'])
    print ('2 ----------------')
    history=model.fit(x_train_scaled,y_train, epochs=42,validation_data=(x_valid_scaled,y_valid))
    print ('3 ----------------')
    score = model.evaluate(x_test_scaled,y_test)
    print (score)

    # modelPath = "./numberModel"
    # tf.keras.models.save_model(
    #     model, modelPath, overwrite=True,
    #     include_optimizer=True, save_format=None,
    #     signatures=None, options=None)


    a = model.predict(x_test[2].reshape(-1, 32, 68))
    a = a[0]
    print (np.argwhere(a == 1))
    print (y_test[2])

records_path = './number.tfrecords'
data_path = './sub/'
img_txt = './label.text'
create_record(records_path, data_path, img_txt)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容