CNN(Convolutional Neural Network)是一种深度学习模型,主要用于图像识别、语音识别和自然语言处理等领域。CNN采用了卷积层和池化层等结构,可以提取图像特征并进行分类或回归任务。
MobileNet是一种轻量级的CNN模型,它的设计目标是在保持较高的准确率的同时,具有更小的模型体积和更低的计算开销,适用于移动设备等资源有限的场景。MobileNet通过使用深度可分离卷积(Depthwise Separable Convolution)来减少参数量和计算量,从而达到了模型轻量化的效果。
CNN和Mobolenet区别与联系如下:
- 模型结构:CNN模型是一种通用的深度学习模型,而MobileNet是一种特定设计的轻量级CNN模型,其模型结构和参数量都被精心设计以达到轻量化的目的。
- 参数量和计算量:相对于CNN模型来说,MobileNet在保持一定准确率的前提下,大大减少了参数量和计算量,使得模型在移动设备等资源有限的场景中更加适用。
- 应用场景:CNN模型可以在各个领域中应用,包括图像识别、语音识别、自然语言处理等;而MobileNet主要是为了在移动设备等资源有限的场景中实现图像识别等任务。
anaconda创建虚拟环境和-venv区别:
python虚拟环境的理解虚拟环境
- Anaconda 创建的虚拟环境默认会安装一些常用的科学计算包,而python -m venv 创建的虚拟环境不包含任何额外的包,需要手动安装所需的包。
- Anaconda 创建的虚拟环境可以跨平台使用,而python -m venv 创建的虚拟环境只能在Python3.3以上的版本中使用。
anaconda环境变量的设置
D:\Anaconda
D:\Anaconda\Scripts
D:\Anaconda\Library\bin
anaconda创建虚拟环境的一些常见命令
查看所有环境列表
conda env list
创建虚拟环境
conda create -n gumou python=3.8
激活
conda activate gumou
关闭
conda deactivate
移除
conda remove -n gumou --all
清华源
https://pypi.tuna.tsinghua.edu.cn/simple/
1.也许安装anconda在D盘但是创建环境显示在C盘 解决办法 其实不影响
2.新版PyCharm Conda环境找不到可执行文件
直接用这个加载 ,添加本地解释器--加载conda环境 然后选择上面创建的虚拟环境
D:\Anaconda3\Scripts\conda.exe
调用模型代码
import tensorflow as tf
import cv2
from PIL import Image
import numpy as np
class ImageRecognition:
# 初始化
def __init__(self):
# 模型初始化
self.model = tf.keras.models.load_model("models/mobilenet_fv.h5")
self.class_names = ['土豆', '圣女果', '大白菜', '大葱', '梨', '胡萝卜', '芒果', '苹果', '西红柿', '韭菜', '香蕉', '黄瓜']
# 预测图片
def predict_img(self, image_path):
# 读取图片
img = Image.open(image_path)
img = np.asarray(img)
# 将图片大小调整到224*224用于模型推理
img = cv2.resize(img, (224, 224))
# 将图片输入模型得到结果
outputs = self.model.predict(img.reshape(1, 224, 224, 3))
# 获得对应的类别索引
result_index = int(np.argmax(outputs))
# 获得对应的类别名称
result = self.class_names[result_index]
# 打印结果到控制台
print("识别结果:", result)
if __name__ == "__main__":
# 创建图像识别对象
recognizer = ImageRecognition()
# 假设有一个图片路径,这里需要替换成实际的图片路径
image_path = "images/12.jpg"
# 调用predict_img方法进行图片识别,并打印结果
recognizer.predict_img(image_path)
自己用pyqt写了UI (自用)
from gu import Ui_Form #UI
import sys
from PyQt5 import QtGui
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import q #qrc生成的py #样式表
import logo #任务栏图标
import tensorflow as tf
import cv2
from PIL import Image
import numpy as np
import ctypes
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("com.example.myapp")
#下载位置 #桌面
# 定义图像识别类
class ImageRecognition:
def __init__(self):
self.model = tf.keras.models.load_model("models/mobilenet_fv.h5")
self.class_names = ['土豆', '圣女果', '大白菜', '大葱', '梨', '胡萝卜', '芒果', '苹果', '西红柿', '韭菜', '香蕉', '黄瓜']
def predict_img(self, image_path):
img = Image.open(image_path)
img = np.asarray(img)
img = cv2.resize(img, (224, 224))
outputs = self.model.predict(img.reshape(1, 224, 224, 3))
result_index = int(np.argmax(outputs))
result = self.class_names[result_index]
return result
class MyThread(QThread):
name_received = pyqtSignal(str) # 定义信号,传递 str 类型的参数
def __init__(self, image_path):
super().__init__()
self.image_path = image_path
def run(self):
recognizer = ImageRecognition()
try:
result = recognizer.predict_img(self.image_path)
self.name_received.emit(result) # 发送识别结果
except Exception as e:
self.name_received.emit(f'识别错误: {str(e)}')
class guWindow(QWidget):
def __init__(self):
super().__init__()
self.gu = Ui_Form()
self.gu.setupUi(self)
an = self.gu.pushButton # 按钮
an.clicked.connect(self.gumou) # 给按钮绑定函数
# ✦✦✦✦✦✦✦✦✦✦设置无边框 和可拖动✦✦✦✦✦✦✦✦✦✦✦✦✦固定代码
self.setWindowOpacity(0.90) # 设置窗口透明度
self.setWindowFlag(Qt.FramelessWindowHint) # 去除边框
self.setAttribute(Qt.WA_TranslucentBackground) # 去除白色背景
self.offset = QPoint() # 记录鼠标按下的初始位置
def mousePressEvent(self, event):
self.offset = event.pos()
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton:
self.move(self.pos() + event.pos() - self.offset) # 移动窗口位置
def gumou(self): # 按钮绑定的函数 功能
# 设置文件选择对话框的过滤器,只显示图片文件
options = QFileDialog.Options()
file_path, _ = QFileDialog.getOpenFileName(
self,
"选择图片",
"",
"图片文件 (*.png *.jpeg *.jpg);;所有文件 (*)",
options=options
)
if file_path:
self.thread = MyThread(file_path)
self.thread.name_received.connect(self.receive_name)
self.thread.start()
def receive_name(self, name): #接收name
self.gu.textBrowser.clear()
self.gu.textBrowser.append(name)
if __name__ == '__main__':
app = QApplication(sys.argv)
icon = QtGui.QIcon(':/2.png')
app.setWindowIcon(icon)
# 创建可拖动窗口实例
ui = guWindow() #函数
# 显示窗口
ui.show()
# 启动应用程序事件循环
sys.exit(app.exec_())