testttt

import sys

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

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_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_open_camera_button_clicked(self):
        sess = tf.compat.v1.Session(graph=self.detection_graph)  # 创建 TensorFlow 会话对象

        while True:
            ret, frame = self.cap.read()

            # 进行目标检测
            image_np_expanded = np.expand_dims(frame, axis=0)
            # image_np_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)  # 将帧转换为 RGB 格式
            image_tensor = self.detection_graph.get_tensor_by_name('image_tensor:0')
            detection_boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0')
            detection_scores = self.detection_graph.get_tensor_by_name('detection_scores:0')
            detection_classes = self.detection_graph.get_tensor_by_name('detection_classes:0')
            num_detections = self.detection_graph.get_tensor_by_name('num_detections:0')

            (boxes, scores, classes, num) = sess.run(
                [detection_boxes, detection_scores, detection_classes, num_detections],
                feed_dict={image_tensor: image_np_expanded})

            # 在图像上绘制结果
            vis_utils.visualize_boxes_and_labels_on_image_array(
                frame,
                np.squeeze(boxes),
                np.squeeze(classes).astype(np.int32),
                np.squeeze(scores),
                self.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]
            # print(class_names)
            # 创建文本字符串
            text = ''
            for i in range(len(class_names)):
                text += f'{class_names[i]}: {scores[i]:.2f}\n'
            # 在图像上绘制文本
            cv2.putText(frame, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)

            # 显示图像
            self.display_image(frame)

            # 更新FPS
            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

        sess.close()  # 关闭 TensorFlow 会话对象

    def on_save_image_button_clicked(self):
        # 保存图像
        image = self.label.pixmap().toImage()
        image.save('image.jpg')
        image = self.label.pixmap().toImage()
        if image.save('image.jpg'):
            QMessageBox.information(self, "Save Image", "Image saved successfully.")
        else:
            QMessageBox.warning(self, "Save Image", "Failed to save image.")

    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_())
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值