zhoude

import sys
import sqlite3
from PyQt5.QtSql import QSqlDatabase, QSqlQuery
from PyQt5.QtCore import QDateTime
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QMessageBox
from PyQt5.QtGui import QPixmap, QImage
import numpy as np
import cv2
import os
import time
import tensorflow as tf
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_utils
from PyQt5.QtCore import QDateTime, QTimer
#忽略
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        # 设置窗口大小
        self.setGeometry(100, 100, 800, 700)
        # 设置窗口名称
        self.setWindowTitle("object_detection")

        # 创建一个标签用于显示图像
        self.label = QLabel(self)
        self.label.setGeometry(10, 10, 640, 480)

        # 创建一个按钮用于打开摄像头
        self.open_camera_button = QPushButton('Open Camera', self)
        self.open_camera_button.setGeometry(10, 550, 200, 60)
        self.open_camera_button.clicked.connect(self.on_open_camera_button_clicked)

        # 创建一个按钮用于保存识别结果到数据库
        self.save_to_db_button = QPushButton('Save to Database', self)
        self.save_to_db_button.setGeometry(550, 500, 200, 30)
        self.save_to_db_button.clicked.connect(self.on_save_to_db_button_clicked)

        # 创建一个按钮用于保存识别结果到数据库
        self.save_to_db_button = QPushButton('Save to Database', self)
        self.save_to_db_button.setGeometry(550, 500, 200, 30)
        self.save_to_db_button.clicked.connect(self.on_save_to_db_button_clicked)

        # 创建一个按钮用于保存图像
        self.save_image_button = QPushButton('Save Image', self)
        self.save_image_button.setGeometry(280, 550, 200, 60)
        self.save_image_button.clicked.connect(self.on_save_image_button_clicked)

        # 创建一个按钮用于关闭应用程序
        self.close_button = QPushButton('Close', self)
        self.close_button.setGeometry(550, 550, 200, 60)
        self.close_button.clicked.connect(self.close)

        # 创建一个标签用于显示日期和时间
        self.datetime_label = QLabel(self)
        self.datetime_label.setGeometry(10, 500, 300, 30)

        # 更新日期和时间
        self.update_datetime()

        # 创建一个定时器,每秒钟更新一次日期和时间
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update_datetime)
        self.timer.start(1000)

        # 初始化摄像头
        self.cap = cv2.VideoCapture(0)


        # 加载模型
        self.MODEL_NAME = 'ssdlite_mobilenet_v2_coco_2018_05_09'
        self.PATH_TO_CKPT = "ssdlite_mobilenet_v2_coco_2018_05_09/frozen_inference_graph.pb"
        self.PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')
        self.NUM_CLASSES = 90
        self.detection_graph = tf.Graph()
        with self.detection_graph.as_default():
            od_graph_def = tf.compat.v1.GraphDef()
            with open(self.PATH_TO_CKPT, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')
        label_map = label_map_util.load_labelmap(self.PATH_TO_LABELS)
        categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=self.NUM_CLASSES,
                                                                    use_display_name=True)
        self.category_index = label_map_util.create_category_index(categories)
        # 开始计时
        self.t_start = time.time()
        self.fps = 0

    def on_save_to_db_button_clicked(self):
        # 获取识别结果
        class_names = self.class_names
        scores = self.scores
        # 获取当前时间
        # 连接到数据库
        db = QSqlDatabase.addDatabase('QSQLITE')
        db.setDatabaseName('object_detection.db')
        if not db.open():
            QMessageBox.critical(self, "Error", "Failed to connect to database.")
            return

        # 创建表格
        query = QSqlQuery()
        query.exec_(
            "CREATE TABLE IF NOT EXISTS detection_results (id INTEGER PRIMARY KEY AUTOINCREMENT, class_name TEXT, score REAL)")

        # 将识别结果插入数据库
        for i in range(len(class_names)):
            query.prepare("INSERT INTO detection_results (class_name, score) VALUES (?, ?)")
            query.bindValue(0, class_names[i])
            query.bindValue(1, scores[i])
            if not query.exec_():
                QMessageBox.critical(self, "Error", "Failed to insert data into database.")
        # 查询数据库中的数据
        query.exec_("SELECT * FROM detection_results")
        # 在窗口中显示数据库内容
        result = ""
        while query.next():
            id = query.value(0)
            class_name = query.value(1)
            score = query.value(2)
            result += f"ID: {id}, Name: {class_name}\n"
        QMessageBox.information(self, "Save to Database", "Data saved to database successfully.")
        # 显示数据库内容
        QMessageBox.information(self, "Detection Results", result)
    def on_open_camera_button_clicked(self):
        while True:
            ret, frame = self.cap.read()
            self.display_image(frame)
            self.fps += 1
            mfps = self.fps / (time.time() - self.t_start)
            cv2.putText(frame, "FPS " + str(int(mfps)), (10, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
            # 检查是否按下了ESC键
            k = cv2.waitKey(30) & 0xff
            if k == 27:  # 按下ESC退出循环
                break
    def on_save_image_button_clicked(self):
        # 保存图像
        sess = tf.compat.v1.Session(graph=self.detection_graph)
        image = self.label.pixmap().toImage()
        image.save('temp.jpg')
        frame=cv2.imread('temp.jpg')
        MODEL_NAME = 'ssdlite_mobilenet_v2_coco_2018_05_09'  # fast
        PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'
        print(PATH_TO_CKPT)
        PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')
        NUM_CLASSES = 90
        IMAGE_SIZE = (12, 8)
        fileAlreadyExists = os.path.isfile(PATH_TO_CKPT)

        if not fileAlreadyExists:
            print('Model does not exsist !')
            exit

        # LOAD GRAPH
        print('Loading...')
        detection_graph = tf.Graph()
        with detection_graph.as_default():
            od_graph_def = tf.compat.v1.GraphDef()
            with tf.io.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')
        label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
        categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES,
                                                                    use_display_name=True)
        category_index = label_map_util.create_category_index(categories)
        print('Finish Load Graph..')

        # Main
        t_start = time.time()
        fps = 0

        with detection_graph.as_default():
            with tf.compat.v1.Session(graph=detection_graph) as sess:
                frame = cv2.imread('temp.jpg')
                ##############
                image_np_expanded = np.expand_dims(frame, axis=0)
                image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
                detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
                detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
                detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
                num_detections = detection_graph.get_tensor_by_name('num_detections:0')

                print('Running detection..')
                (boxes, scores, classes, num) = sess.run(
                    [detection_boxes, detection_scores, detection_classes, num_detections],
                    feed_dict={image_tensor: image_np_expanded})
                print('Done.  Visualizing..')
                vis_utils.visualize_boxes_and_labels_on_image_array(
                    frame,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    use_normalized_coordinates=True,
                    line_thickness=8)
                class_names = [self.category_index[class_id]['name'] for class_id in classes[0]]
                scores = scores[0]
                classs=[]
                scoress=[]
                for index, score in enumerate(scores):
                    if score >= 0.6:
                        classs.append(class_names[index])
                        scoress.append(score)
                self.class_names = classs
                self.scores = scoress
        cv2.imwrite('zhoudetest.jpg',frame)
        QMessageBox.information(self, "Save Image", "Image saved successfully.")
        #print(class_names[0])
        #print(scores[0])


    def display_image(self, frame):
        # 将OpenCV图像转换为Qt图像
        h, w, ch = frame.shape
        bytes_per_line = ch * w
        qt_image = QImage(frame.data, w, h, bytes_per_line, QImage.Format_RGB888).rgbSwapped()
        # 在标签中显示图像
        pixmap = QPixmap.fromImage(qt_image)
        self.label.setPixmap(pixmap)

    def update_datetime(self):
        # 获取当前日期和时间
        current_datetime = QDateTime.currentDateTime()
        # 格式化日期和时间为字符串
        datetime_str = current_datetime.toString("yyyy-MM-dd hh:mm:ss")
        # 在标签上显示日期和时间
        self.datetime_label.setText(datetime_str)



if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的海滨体育馆管理系统,源码+数据库+毕业论文+视频演示 本基于Spring Boot的海滨体育馆管理系统设计目标是实现海滨体育馆的信息化管理,提高管理效率,使得海滨体育馆管理工作规范化、高效化。 本文重点阐述了海滨体育馆管理系统的开发过程,以实际运用为开发背景,基于Spring Boot框架,运用了Java技术和MySQL作为系统数据库进行开发,充分保证系统的安全性和稳定性。本系统界面良好,操作简单方便,通过系统概述、系统分析、系统设计、数据库设计、系统测试这几个部分,详细的说明了系统的开发过程,最后并对整个开发过程进行了总结,实现了海滨体育馆相关信息管理的重要功能。 本系统的使用使管理人员从繁重的工作中解脱出来,实现无纸化办公,能够有效的提高海滨体育馆管理效率。 关键词:海滨体育馆管理,Java技术,MySQL数据库,Spring Boot框架 本基于Spring Boot的海滨体育馆管理系统主要实现了管理员功能模块和学生功能模块两大部分,这两大功能模块分别实现的功能如下: (1)管理员功能模块 管理员登录后可对系统进行全面管理操作,包括个人中心、学生管理、器材管理、器材借出管理、器材归还管理、器材分类管理、校队签到管理、进入登记管理、离开登记管理、活动预约管理、灯光保修管理、体育论坛以及系统管理。 (2)学生功能模块 学生在系统前台可查看系统信息,包括首页、器材、体育论坛以及体育资讯等,没有账号的学生可进行注册操作,注册登录后主要功能模块包括个人中心、器材管理、器材借出管理、器材归还管理、校队签到管理、进入登记管理、离开登记管理、活动预约管理。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值