# from src.gcxlThread import videoThread
import os
import sys
import threading
from PyQt5 import QtCore
from PIL import Image
from torchvision.transforms import transforms
sys.path.append("/home/pi/Downloads/gcxls/")
from Net.model import MobileNetV2
from PyQt5.QtMultimediaWidgets import QVideoWidget
# import RPi.GPIO as GPIO
from PyQt5.QtCore import QUrl, pyqtSignal
from PyQt5.QtWidgets import QMainWindow, QApplication, QVBoxLayout, QFileDialog, QLabel, QWidget, QPushButton, \
QHBoxLayout, QLineEdit
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
import cv2 as cv
import torch
import json
import time
import pyttsx3
import inspect
import ctypes
from src.write_Serial import main_write
from src.read_Serial import main_read
# from gpiozero import LED, Button, PWMLED # 引入LED类
# from signal import pause
from time import sleep
import layout.Mainwindow as Mainwindow
from omxplayer import OMXPlayer
from pathlib import Path
from time import sleep
import datetime
# from Net.predict_local import init_artificial_neural_network, prediction_result_from_img
print("sys.path:", sys.path)
class FullThread(QtCore.QThread):
_Full = pyqtSignal(int)
def __init__(self):
super(FullThread, self).__init__()
def __del__(self):
self.wait()
def run(self):
while True:
self.a = main_read()
if self.a == "e" or "f" or "g" or "h":
self._Full.emit(self.a)
class VideoThread(QtCore.QThread):
_display = pyqtSignal(int)
def __init__(self, camera, data_transform, model):
super(VideoThread, self).__init__()
self.camera = camera
self.data_transform = data_transform
self.model = model
self.candrop = True
self.detected = False
def __del__(self):
self.wait()
def run(self):
while True:
flag_read = main_read()
if flag_read == 4:
sleep(2)
res, cur_frame = self.camera.read()
cv.imwrite("a.jpg", cur_frame)
self.check()
print("1")
def check(self):
time.sleep(1)
# 调用神经网络去识别
img = Image.open("a.jpg")
img = self.data_transform(img)
# expand batch dimension
img = torch.unsqueeze(img, dim=0)
with torch.no_grad():
# predict class
output = torch.squeeze(self.model(img))
predict = torch.softmax(output, dim=0)
predict_cla = torch.argmax(predict).numpy()
predict_cla = int(predict_cla)
print("predict_cla", predict_cla)
self._display.emit(predict_cla)
print("write")
main_write(predict_cla)
class Mainwin(Mainwindow.Ui_MainWindow, QMainWindow):
res = "我要开始投放垃圾了"
PATH = "/home/pi/Downloads/gcxls/src/2.mp4"
def __init__(self):
super(Mainwin, self).__init__()
self.setupUi(self)
self.initUI()
def closeEvent(self, event):
super(Mainwin, self).closeEvent()
self.camera.release()
cv.destroyAllWindows()
# 初始化所有程序
'''
1. 进入一个循环,启动摄像头,检测是否有东西投入。
2. 若检测到有东西投入,进入中断,调用神经网络,识别是何种垃圾。
3. 将识别垃圾代号传入到语音播报,界面展示,串口输出等函数中
4. 垃圾处理完毕后,串口输入,同时使用GPIO判断垃圾桶是否满了。若满了,则该垃圾桶无法投入。
5. 返回步骤1
'''
def initUI(self):
# 神经网络初始化
self.ress = ""
self.data_transform = transforms.Compose(
[transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
self.model = MobileNetV2(num_classes=4)
model_weight_path = "../Net/mobilenet_v2_1.4_224.pth"
self.model.load_state_dict(torch.load(model_weight_path))
self.model.eval()
self.video_stop.setEnabled(False)
self.camera = cv.VideoCapture(0)
print('start')
self.btn_play.clicked.connect(self.play)
self.btn_pause.clicked.connect(self.pause)
self.btn_select_video.clicked.connect(self.select_file)
self.video_start.clicked.connect(self.startVideo)
self.video_stop.clicked.connect(self.stopVideo)
self.btn_play.setEnabled(False)
self.btn_pause.setEnabled(False)
self.detected = False # 标志位,是否检测到。
# 是否能投放的标志位
self.candrop = True
# 播放的标志位
self.canplay = True
self.show()
# 判断垃圾是否满了
'''
"0": "Harmful_waste", a
"1": "Kitchen_waste", b
"2": "Other_waste", c
"3": "Recyclable_waste"
'''
def jud(self, i):
if i == 0:
self.ress = self.ress +"\n" + "有害垃圾已满"
self.textBrowser2.setText(self.ress)
self.textBrowser2.repaint()
if i == 1:
self.ress = self.ress +"\n" + "厨余垃圾已满"
self.textBrowser2.setText(self.ress)
self.textBrowser2.repaint()
if i == 2:
self.ress = self.ress +"\n" + "其他垃圾已满"
self.textBrowser2.setText(self.ress)
self.textBrowser2.repaint()
if i == 3:
self.ress = self.ress +"\n" + "可回收垃圾已满"
self.textBrowser2.setText(self.ress)
self.textBrowser2.repaint()
def judget(self, i):
from PyQt5.QtWidgets import QMessageBox
if i == 0:
self.jud(i)
QMessageBox.critical(self, "错误", "有害垃圾已满")
elif i == 1:
self.jud(i)
QMessageBox.critical(self, "错误", "厨余垃圾已满")
elif i == 2:
self.jud(i)
QMessageBox.critical(self, "错误", "其他垃圾已满")
elif i == 3:
self.jud(i)
QMessageBox.critical(self, "错误", "可回收垃圾已满")
self.candrop = False
# self.stopVideo()
def play(self):
self.player = OMXPlayer(self.PATH)
sleep(10)
self.player.quit()
def pause(self):
print(1)
self.player.quit()
def select_file(self):
url = QFileDialog.getOpenFileUrl()[0]
self.btn_play.setEnabled(True)
self.btn_pause.setEnabled(True)
def _async_raise(self, tid, exctype):
"""raises the exception, performs cleanup if needed"""
tid = ctypes.c_long(tid)
if not inspect.isclass(exctype):
exctype = type(exctype)
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
if res == 0:
raise ValueError("invalid thread id")
elif res != 1:
# """if it returns a number greater than one, you're in trouble,
# and you should call it again with exc=NULL to revert the effect"""
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
raise SystemError("PyThreadState_SetAsyncExc failed")
def stop_thread(self, thread):
self._async_raise(thread.ident, SystemExit)
def startVideo(self):
# self.v = VideoThread(self.camera, self.data_transform, self.model)
# self.v._display.connect(self.display)
self.f = FullThread()
self.f._Full.connect(self.judget)
print("display")
# sel
没有合适的资源?快使用搜索试试~ 我知道了~
工程训练赛-智能垃圾分类【包括界面设计和神经网络识别】
共2000个文件
jpg:1200个
jpeg:789个
py:10个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 69 浏览量
2023-10-02
11:41:21
上传
评论
收藏 561.75MB RAR 举报
温馨提示
我在此次工程训练赛中写的代码,包括界面设计和神经网络识别
资源推荐
资源详情
资源评论
收起资源包目录
工程训练赛-智能垃圾分类【包括界面设计和神经网络识别】 (2000个子文件)
img_电池_452.jpeg 1.12MB
img_塑料瓶_244.jpeg 931KB
img_塑料瓶_67.jpeg 931KB
img_电池_541.jpeg 909KB
img_电池_376.jpeg 894KB
img_电池_517.jpeg 801KB
img_电池_632.jpeg 780KB
img_电池_474.jpeg 692KB
img_塑料瓶_188.jpeg 651KB
img_电池_448.jpeg 641KB
img_塑料瓶_92.jpeg 626KB
img_塑料瓶_233.jpeg 623KB
img_电池_660.jpeg 601KB
img_电池_331.jpeg 588KB
img_电池_616.jpeg 566KB
img_塑料瓶_9.jpeg 555KB
img_塑料瓶_27.jpeg 543KB
img_电池_584.jpeg 501KB
img_塑料瓶_266.jpeg 498KB
img_电池_263.jpeg 493KB
img_塑料瓶_204.jpeg 490KB
img_塑料瓶_31.jpeg 490KB
img_塑料瓶_19.jpeg 488KB
img_电池_609.jpeg 471KB
img_塑料瓶_174.jpeg 463KB
img_电池_560.jpeg 460KB
img_电池_412.jpeg 453KB
img_塑料瓶_88.jpeg 444KB
img_塑料瓶_217.jpeg 436KB
img_塑料瓶_42.jpeg 434KB
img_电池_685.jpeg 430KB
img_电池_382.jpeg 429KB
img_塑料瓶_214.jpeg 421KB
img_塑料瓶_126.jpeg 418KB
img_塑料瓶_137.jpeg 413KB
img_塑料瓶_104.jpeg 413KB
img_电池_274.jpeg 411KB
img_塑料瓶_124.jpeg 406KB
img_电池_453.jpeg 405KB
img_电池_553.jpeg 399KB
img_电池_619.jpeg 398KB
img_电池_371.jpeg 398KB
img_塑料瓶_133.jpeg 392KB
img_电池_665.jpeg 390KB
img_电池_565.jpeg 383KB
img_电池_554.jpeg 382KB
img_电池_248.jpeg 361KB
img_电池_413.jpeg 355KB
img_塑料瓶_4.jpeg 352KB
img_电池_509.jpeg 346KB
img_电池_372.jpeg 344KB
img_电池_276.jpeg 334KB
img_塑料瓶_277.jpeg 330KB
img_塑料瓶_164.jpeg 329KB
img_塑料瓶_58.jpeg 324KB
img_电池_626.jpeg 319KB
img_塑料瓶_167.jpeg 308KB
img_塑料瓶_286.jpeg 308KB
img_电池_458.jpeg 305KB
img_塑料瓶_326.jpeg 303KB
img_电池_336.jpeg 299KB
img_电池_356.jpeg 298KB
img_电池_532.jpeg 294KB
img_电池_708.jpeg 292KB
img_电池_269.jpeg 291KB
img_电池_468.jpeg 289KB
img_电池_320.jpeg 288KB
img_电池_648.jpeg 284KB
img_电池_469.jpeg 276KB
img_电池_455.jpeg 273KB
img_电池_253.jpeg 268KB
img_电池_312.jpeg 263KB
img_塑料瓶_325.jpeg 260KB
img_电池_655.jpeg 260KB
img_电池_505.jpeg 255KB
img_电池_611.jpeg 255KB
img_塑料瓶_323.jpeg 245KB
img_电池_305.jpeg 243KB
img_电池_561.jpeg 242KB
img_电池_491.jpeg 242KB
img_塑料瓶_66.jpeg 242KB
img_电池_669.jpeg 242KB
img_电池_424.jpeg 239KB
img_电池_288.jpeg 238KB
img_电池_658.jpeg 235KB
img_电池_398.jpeg 231KB
img_电池_497.jpeg 231KB
img_塑料瓶_263.jpeg 229KB
img_电池_346.jpeg 228KB
img_塑料瓶_268.jpeg 222KB
img_电池_449.jpeg 218KB
img_电池_568.jpeg 215KB
img_电池_510.jpeg 214KB
img_电池_649.jpeg 213KB
img_塑料瓶_246.jpeg 212KB
img_电池_630.jpeg 211KB
img_电池_613.jpeg 211KB
img_电池_306.jpeg 211KB
img_塑料瓶_189.jpeg 210KB
img_塑料瓶_327.jpeg 202KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
小风飞子
- 粉丝: 332
- 资源: 1522
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功