def open_camera(self, use_camera, video=None): """打开摄像头,成功打开返回True""" cam1 = self.url1 # 默认摄像头 cam2 = self.url2 cam3 = self.url3 cam4 = self.url4 #print(cam) if not use_camera: cam1 = video # 视频流文件 cam2 = video cam3 = video cam4 = video self.cap1 = cv2.VideoCapture(cam1) self.cap2 = cv2.VideoCapture(cam2) self.cap3 = cv2.VideoCapture(cam3) self.cap4 = cv2.VideoCapture(cam4) # 打开camera if self.cap1 and self.cap2 and self.cap3 and self.cap4: self.opened = True # 已打开 return True else: msg = msg_box.MsgWarning() msg.setText('视频流开启失败!\n' '请确保摄像头已打开或视频文件真实存在!') msg.exec() return False
def oc_camera(self): if self.camera.cap.isOpened(): self.camera.close_camera() # 关闭摄像头 else: self.camera.open_camera( use_camera=self.config.check_camera.isChecked(), video=self.config.line_video.text()) self.camera.show_camera() # 目标检测 self.config.save_config() check = self.camera.yolo.set_config( weights=GLOBAL.config['weights'], device=GLOBAL.config['device'], img_size=GLOBAL.config['img_size'], conf=GLOBAL.config['conf_thresh'], iou=GLOBAL.config['iou_thresh'], agnostic=GLOBAL.config['agnostic'], augment=GLOBAL.config['augment']) if not check: msg = msg_box.MsgWarning() msg.setText('配置信息有误,无法正常加载YOLO模型!') msg.exec() self.camera.close_camera() # 关闭摄像头 return self.camera.yolo.load_model() self.camera.start_detect()
def oc_camera(self): self.config.save_config() check = self.camera.yolo.set_config( weights=GLOBAL.config['weights'], device=GLOBAL.config['device'], img_size=GLOBAL.config['img_size'], conf=GLOBAL.config['conf_thresh'], iou=GLOBAL.config['iou_thresh'], agnostic=GLOBAL.config['agnostic'], augment=GLOBAL.config['augment'], netstreamvedio = GLOBAL.config['netstreamvedio'] ) if not check: msg = msg_box.MsgWarning() msg.setText('配置信息有误,无法正常加载YOLO模型!') msg.exec() self.camera.close_camera() # 关闭摄像头 return #print(self.camera.cap.isOpened()) if self.camera.cap1.isOpened(): self.camera.close_camera() # 关闭摄像头 else: video=self.config.line_video.text() path,filename = os.path.split(video) #fname,ext = os.path.splitext(filename) #print(ext) #if ext in ['.jpg','.JPG','.jepg','.JEPG','.png','.PNG','.bmp','.BMP']: #self.camera.read_image(video) #self.camera.yolo.load_model() #self.camera.start_detectimage() #return #self.yolo.load_model() #self.muti_thread_detect(self.camera1) #self.muti_thread_detect(self.camera2) #self.muti_thread_detect(self.camera3) #self.muti_thread_detect(self.camera4) #while(True): #print(1) #time.sleep(0.33) #print(len(self.camera.objects)) self.camera.open_camera( use_camera=self.config.check_camera.isChecked(), video=self.config.line_video.text() ) self.camera.show_camera() # 目标检测 self.camera.yolo.load_model() self.camera.start_detect()
def oc_camera(self): self.config.save_config() check = self.yolo.set_config( weights=GLOBAL.config['weights'], device=GLOBAL.config['device'], img_size=GLOBAL.config['img_size'], conf=GLOBAL.config['conf_thresh'], iou=GLOBAL.config['iou_thresh'], agnostic=GLOBAL.config['agnostic'], augment=GLOBAL.config['augment'], netstreamvedio=GLOBAL.config['netstreamvedio']) if not check: msg = msg_box.MsgWarning() msg.setText('配置信息有误,无法正常加载YOLO模型!') msg.exec() self.camera1.close_camera() self.camera2.close_camera() self.camera3.close_camera() self.camera4.close_camera() # 关闭摄像头 return #print(self.camera.cap.isOpened()) if self.camera1.cap.isOpened(): self.camera1.close_camera() self.camera2.close_camera() self.camera3.close_camera() self.camera4.close_camera() # 关闭摄像头 else: video = self.config.line_video.text() path, filename = os.path.split(video) #fname,ext = os.path.splitext(filename) #print(ext) #if ext in ['.jpg','.JPG','.jepg','.JEPG','.png','.PNG','.bmp','.BMP']: #self.camera.read_image(video) #self.camera.yolo.load_model() #self.camera.start_detectimage() #return self.yolo.load_model() self.t1 = threading.Thread(target=self.muti_thread_detect, args=(self.camera1, )) self.t2 = threading.Thread(target=self.muti_thread_detect, args=(self.camera2, )) self.t3 = threading.Thread(target=self.muti_thread_detect, args=(self.camera3, )) self.t4 = threading.Thread(target=self.muti_thread_detect, args=(self.camera4, )) self.t1.start() self.t2.start() self.t3.start() self.t4.start()
def open_camera(self, use_camera, video): """打开摄像头,成功打开返回True""" cam = 0 # 默认摄像头 if not use_camera: cam = video # 视频流文件 flag = self.cap.open(cam) # 打开camera if flag: self.opened = True # 已打开 return True else: msg = msg_box.MsgWarning() msg.setText('视频流开启失败!\n' '请确保摄像头已打开或视频文件真实存在!') msg.exec() return False
def record_config(self, _dict): """将设置参数写入到本地文件保存,传入字典""" # 更新配置 for k, v in _dict.items(): self.config[k] = v if not os.path.exists('config'): os.mkdir('config') # 创建config文件夹 try: # 写入文件 with open('config/config.json', 'w') as file_config: file_config.write(json.dumps(self.config, indent=4)) except FileNotFoundError as err_file: print(err_file) msg = msg_box.MsgWarning() msg.setText('参数保存失败!') msg.exec()
def __init__(self): super().__init__() # 检查python版本是否满足要求 minimum = '3.6.2' current = platform.python_version() current, minimum = (pkg.parse_version(x) for x in (current, minimum)) if current < minimum: msg = msg_box.MsgWarning() msg.setText(f'当前Python版本({current})过低,请升级到{minimum}以上!') msg.exec() sys.exit() self.setWindowTitle(f'{APP_NAME} {APP_VERSION}') self.setWindowIcon(QIcon('img/yologo.png')) gb.init_logger() gb.clean_log() gb.init_config() self.camera = WidgetCamera() # 摄像头 self.info = WidgetInfo() # 信息面板 self.config = WidgetConfig() # Yolo配置界面 self.settings = SettingsDialog() self.signal_config_error.connect(self.slot_msg_dialog) # 模型加载线程 self.load_model_thread = threading.Thread(target=self.load_yolo) self.load_model_thread.start() self.config.btn_settings.clicked.connect(self.settings.exec) self.settings.accepted.connect(self.reload) self.status_icon = QLabel() self.status_text = QLabel() self.update_status('Loading model...', False) hbox = QHBoxLayout() hbox.addWidget(self.status_icon) hbox.addWidget(self.status_text) self.btn_camera = QPushButton('Open/Close Camera') # 开启或关闭摄像头 self.btn_camera.setEnabled(False) self.btn_camera.clicked.connect(self.oc_camera) self.btn_camera.setFixedHeight(60) vbox1 = QVBoxLayout() vbox1.setContentsMargins(0, 0, 0, 0) vbox1.addWidget(self.info) vbox1.addWidget(self.config) vbox1.addStretch() vbox1.addLayout(hbox) vbox1.addWidget(self.btn_camera) right_widget = QWidget() right_widget.setMaximumWidth(400) right_widget.setLayout(vbox1) hbox = QHBoxLayout() hbox.addWidget(self.camera, 3) hbox.addWidget(right_widget, 1) vbox = QVBoxLayout() vbox.addLayout(hbox) self.central_widget = QWidget() self.central_widget.setLayout(vbox) self.setCentralWidget(self.central_widget) # ---------- 自适应不同大小的屏幕 ---------- # screen = QDesktopWidget().screenGeometry(self) available = QDesktopWidget().availableGeometry(self) title_height = self.style().pixelMetric(QStyle.PM_TitleBarHeight) if screen.width() < 1280 or screen.height() < 768: self.setWindowState(Qt.WindowMaximized) # 窗口最大化显示 self.setFixedSize( available.width(), available.height() - title_height) # 固定窗口大小 else: self.setMinimumSize(QSize(1100, 700)) # 最小宽高 self.show() # 显示窗口
def slot_msg_dialog(self, text): msg = msg_box.MsgWarning() msg.setText(text) msg.exec()