背景:現(xiàn)在攝像頭RTSP協(xié)議不能給我們,相機(jī)拍攝時(shí)間坏怪,所以需要用人工智能分析 H256視頻 時(shí)間顯示的時(shí)間
1、項(xiàng)目流程圖:
2题暖、tensorflow 數(shù)據(jù)預(yù)處理
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)
#將圖片轉(zhuǎn)換為數(shù)組
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:
# 圖片相對(duì)路徑
img_name = img_info.split(' ')[0]
# 圖片類別
img_cls = int(img_info.split(' ')[1])
img_path = data_path + img_name
img = load_img(img_path)
#將圖片轉(zhuǎn)換為數(shù)組
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ùn)練
x_train_scaled = scaler.fit_transform(x_train.astype(np.float32).reshape(-1,1)).reshape(-1,32,68)
#驗(yàn)證
x_valid_scaled = scaler.transform(x_valid.astype(np.float32).reshape(-1,1)).reshape(-1,32,68)
# 測(cè)試
x_test_scaled = scaler.transform(x_test.astype(np.float32).reshape(-1,1)).reshape(-1,32,68)
#激活函數(shù)選擇了relu惜纸,優(yōu)化器選擇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)