import tensorflow as tf
from tensorflow import keras
import cv2
import numpy as np
import glob
import os
import sys
import imutils
import random
import pymssql
import pyodbc
import openpyxl
import xlwt
import time
from PIL import Image, ImageTk
from tensorflow.keras import losses,layers,optimizers
from tensorflow.keras.callbacks import EarlyStopping
from PyQt5.QtWidgets import QApplication, QMainWindow, QDockWidget, QTextEdit, QLabel, QWidget, QPushButton, QVBoxLayout, QListWidget, QLineEdit, QStyleFactory
from PyQt5.QtCore import Qt, QTimer, QThread
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtSql import QSqlDatabase , QSqlQuery
from pathlib import Path, PureWindowsPath
from pyecharts import options as opts
from pyecharts.charts import Scatter
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineSettings
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
#导入HIK的相机接口
sys.path.append("D:/Software/HIK Camrea/MVS/Development/Samples/Python/MvImport")
from MvCameraControl_class import *
QApplication.setStyle(QStyleFactory.create('Fusion'))
#定义目标文件夹为全局变量,给各个函数进行调用
targetfilePath = ''
isPredicting = True
temp = ''
count = 0
model = object
trainingfilePath = ''
mynet = object
Camera_status = False
saveImage = object
logResult = ''
deviceList = MV_CC_DEVICE_INFO_LIST()
tlayerType = MV_GIGE_DEVICE | MV_USB_DEVICE
Camera1_Image = ''
Camera2_Image = ''
Camera3_Image = ''
Camera4_Image = ''
isCapturing = False
cameraIP = []
#---------------------------------------------------------------------------------训练模块---------------------------------------------------------------------------------------------------#
#训练参数设置
tf.random.set_seed(2222)
np.random.seed(2222)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
assert tf.__version__.startswith('2.')
#预处理训练图片
def preprocessTraining(x,y):
x = tf.image.resize(x,[244,244])
x = tf.image.random_flip_left_right(x)
x = tf.image.random_crop(x,[224,224,3])
x = tf.cast(x, dtype=tf.float32) / 255.
x = normalize(x)
y = tf.convert_to_tensor(y)
y = tf.one_hot(y, depth=5)
return x,y
#加载训练图片以及Resize转换
def Data_Generation(filePath):
X_data = []
Y_data = []
path_data = []
path_label = []
files = os.listdir(filePath)
#循环加载文件
for file in files:
if os.path.exists(filePath + '\\' + file+ '\\') == True:
for path in glob.glob(filePath + '\\' + file + '\*.*'):
if 'jpg' or 'png' or 'jpeg' in path:
path_data.append(path)
else:
for path in glob.glob(filePath + '\*.*'):
if 'jpg' or 'png' or 'jpeg' in path:
path_data.append(path)
#打乱数据
random.shuffle(path_data)
#按照图片分类进行label标记
for paths in path_data:
if 'Normal' in paths:
path_label.append(0)
elif 'BKG' in paths:
path_label.append(1)
img = cv2.imdecode(np.fromfile(paths, dtype=np.uint8), -1)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
img = cv2.resize(img,(224,224))
X_data.append(img)
L = len(path_data)
Y_data = path_label
X_data = np.array(X_data,dtype=float)
Y_data = np.array(Y_data,dtype='uint8')
X_train = X_data[0:int(L*0.6)]
Y_train = Y_data[0:int(L*0.6)]
X_valid = X_data[int(L*0.6):int(L*0.8)]
Y_valid = Y_data[int(L*0.6):int(L*0.8)]
X_test = X_data[int(L*0.8):]
Y_test = Y_data[int(L*0.8):]
return X_train,Y_train,X_valid,Y_valid,X_test,Y_test,L
#获取训练集,测试集
def trainingModel(self):
#文件加载路径
trainingfilePath = targetfilePath
if len(trainingfilePath) == 0:
self.logText.setText('请先选择文件夹再开始训练!')
return
else:
self.logText.setText('开始训练!')
X_train,Y_train,X_valid,Y_valid,X_test,Y_test,L = Data_Generation(trainingfilePath)
batchsz = 32
#print(shape(X_data), shape(Y_data))
train_db = tf.data.Dataset.from_tensor_slices((X_train,Y_train))
train_db = train_db.shuffle(10000).map(preprocessTraining).batch(batchsz)
valid_db = tf.data.Dataset.from_tensor_slices((X_valid,Y_valid))
valid_db = valid_db.map(preprocessTraining).batch(batchsz)
test_db = tf.data.Dataset.from_tensor_slices((X_test,Y_test))
test_db = test_db.map(preprocessTraining).batch(batchsz)
#这里使用了自带的DenseNet121网络 你也可以用keras.Sequential DIY模型
net = keras.applications.DenseNet121(weights='imagenet',include_top=False,pooling='max')
net.trainable = False
global mynet
mynet = keras.Sequential([
net,
layers.Dense(1024,activation='relu'),
layers.BatchNormalization(), #BN层 标准化数据
layers.Dropout(rate=0.2),
layers.Dense(5)])
mynet.build(input_shape=(4,224,224,3))
mynet.summary()
#防止过拟合
early_stopping = EarlyStopping(monitor='val_accuracy', min_delta=0.01, patience=3)
mynet.compile(optimizer=optimizers.Adam(lr=1e-3), loss=losses.CategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
history = mynet.fit(train_db, validation_data=valid_db, validation_freq=1, epochs=50, callbacks=[early_stopping])
history = history.history
mynet.evaluate(test_db)
self.logText.setText('训练完成!')
#保存模型
def saveModel(self):
FolderName = QtWidgets.QFileDialog.getExistingDirectory()
if '/' in FolderName :
# 用\替换/,注意'\\'的用法,
FolderName.replace('/', '\\')
#训练结束以后保存mmodel文件到本地方便做图片分类的时候直接调用
#保存model成 .h5格式 里面包含了模型结构和训练好的模型参数
if len(FolderName) == 0:
self.logText.setText('未保存模型文件!')
else:
mynet.save(FolderName + '\densenet.h5')
self.logText.setText('模型文件已保存!')
#---------------------------------------------------------------------------------识别模块---------------------------------------------------------------------------------------------------#
#图片归一化
def normalize(x):
img_mean = tf.constant([0.485, 0.456, 0.406])
img_std = tf.constant([0.229, 0.224, 0.225])
x = (x - img_mean)/img_std
return x
#初始化图片
def preprocess(x):
x = tf.expand_dims(x,axis=0)
x = tf.cast(x, dtype=tf.float32) / 255.
x = normalize(x)
return x
#加载模型
def loadModel(self):
File = QtWidgets.QFileDialog.getOpenFileName()
modelFile = File[0]
if '/' in modelFile:
# 用\替换/,注意'\\'的用法,
modelFile.replace('/', '\\')
if len(modelFile) == 0:
self.logText.setText('未加载模型文件!')
else:
self.modelSelectText.setText(modelFile)
network = keras.models.load_model(modelFile)
network.summary()
self.logText.setText('检测模型加载成功:' + modelFile)
global model
model = network
#打开目标文件夹
def openFileFolder(self):
FolderName = QtWidgets.QFileDialog.getExistingDirectory()
if '/' in FolderName :
# 用\替换/,注意'\\'的用法,
FolderName.replace('/', '\\')
if len(FolderName) == 0 :
self.logText.setText('未选择文件夹路径!')
else :
#print('目标文件夹:',FolderName)
self.fileSelectText.setText(FolderName)
self.logText.setText('目标文件夹:' + FolderName)
#全局定义目标文件夹路�
没有合适的资源?快使用搜索试试~ 我知道了~
MY101 BKG digital system - 0821
共11个文件
py:5个
jpg:4个
ui:1个
需积分: 0 0 下载量 179 浏览量
2023-08-21
18:56:52
上传
评论
收藏 187KB RAR 举报
温馨提示
BKG检测多线程0821完善
资源推荐
资源详情
资源评论
收起资源包目录
MY101 BKG digital system - 0821.rar (11个子文件)
MY101 BKG digital system
BKG detect system mutiple threadhold - 0821 多线程拍照OK.py 23KB
BKG detect system mutiple threadhold - 0819顺序拍照.py 22KB
BKG detect system mutiple threadhold - 0821 多线程拍照.py 24KB
BKG detect system mutiple threadhold.py 23KB
Image files
Camera2.jpg 48KB
Camera1.jpg 51KB
Camera0.jpg 48KB
Images
Logo.jpg 180KB
Logo.ico 66KB
BKG detect system.ui 34KB
BKG detect system mutiple threadhold - 0819.py 22KB
共 11 条
- 1
资源评论
JL295235943
- 粉丝: 0
- 资源: 24
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Code for the complete guide to tkinter tutorial
- 关于百货中心供应链管理系统.zip
- SimpleFolderIcon-master 修改Unity的Project下的文件夹图标
- A python Tkinter widget to display tile based maps
- A pure Python library for adding tables to a Tkinter application
- Vector资源文件.zip
- MobaXterm-Installer
- MicroMsg.xlsx
- 88-520告白(520气球).zip
- HTML+CSS+JS精品网页模板H126.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功