# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'close.ui' # # Created by: PyQt5 UI code generator 5.15.9 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. import sys import time import cv2 import cv2 as cv from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QApplication, QMainWindow class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(989, 649) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(280, 240, 191, 61)) self.pushButton.setObjectName("pushButton") self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_2.setGeometry(QtCore.QRect(550, 250, 121, 51)) self.pushButton_2.setObjectName("pushButton_2") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 989, 26)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) self.pushButton.clicked.connect(MainWindow.close) # type: ignore QtCore.QMetaObject.connectSlotsByName(MainWindow) self.pushButton_2.clicked.connect(self.opencap) def opencap(self): import numpy as np import cv2 import os, time import tensorflow as tf from object_detection.utils import label_map_util from object_detection.utils import visualization_utils as vis_utils # Init camera cap = cv2.VideoCapture(0) cap.set(3, 640) # set Width cap.set(4, 480) # set Height TEST_CAM_ONLY = False if TEST_CAM_ONLY: t_start = time.time() fps = 0 while True: ret, frame = cap.read() # frame = cv2.flip(frame, -1) # Flip camera vertically gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) canny = cv2.Canny(frame, 50, 150) fps = fps + 1 mfps = fps / (time.time() - t_start) cv2.putText(frame, "FPS " + str(int(mfps)), (10, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) cv2.imshow('frame', frame) cv2.imshow('Canny', canny) k = cv2.waitKey(30) & 0xff if k == 27: # press 'ESC' to quit break cap.release() cv2.destroyAllWindows() exit() # Init tf model MODEL_NAME = 'ssdlite_mobilenet_v2_coco_2018_05_09' # fast PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb' 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: while True: ret, frame = cap.read() # frame = cv2.flip(frame, -1) # Flip camera vertically # frame = cv2.resize(frame,(320,240)) ############## 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) ############## fps = fps + 1 mfps = fps / (time.time() - t_start) cv2.putText(frame, "FPS " + str(int(mfps)), (10, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) cv2.imshow('frame', frame) k = cv2.waitKey(30) & 0xff if k == 27: # press 'ESC' to quit break cap.release() cv2.destroyAllWindows() def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.pushButton.setText(_translate("MainWindow", "关闭窗口")) self.pushButton_2.setText(_translate("MainWindow", "打开摄像头")) if __name__ =='__main__':#运行要添加的模板 app=QApplication(sys.argv) MainWindow1=QMainWindow()#mainwidow1随便改,创建窗体对象 ui=Ui_MainWindow()#随便改,与上边一致 ui.setupUi(MainWindow1)#对窗体对象进行初始化设置 MainWindow1.show()#显示窗体 sys.exit(app.exec_())
qt可视化例子
最新推荐文章于 2024-08-19 13:32:52 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)