赞
踩
- python main.py
运行上述指令,则可以看到介入下界面!
!!必须安装相应版本的库
!!否则,将出现以下错误:not enough values to unpack (expected 5, got 4)
注意事项
ultralytics 遵循 GPL-3.0 协议,如需商业使用,需获得其许可。
资源文件为 resources.qrc,如果您修改了默认图标,需要使用 pyside6-rcc resoures.qrc >
ui/resources_rc.py 命令来重新生成 .py 文件。
class YoloPredictor(BasePredictor, QObject):
yolo2main_pre_img = Signal(np.ndarray) # raw image signal
yolo2main_res_img = Signal(np.ndarray) # test result signal
yolo2main_status_msg = Signal(str) # Detecting/pausing/stopping/testing complete/error reporting signal
yolo2main_fps = Signal(str) # fps
yolo2main_labels = Signal(dict) # Detected target results (number of each category)
yolo2main_progress = Signal(int) # Completeness
yolo2main_class_num = Signal(int) # Number of categories detected
yolo2main_target_num = Signal(int) # Targets detected
def __init__(self, cfg=DEFAULT_CFG, overrides=None):
super(YoloPredictor, self).__init__()
QObject.__init__(self)
self.args = get_cfg(cfg, overrides)
project = self.args.project or Path(SETTINGS['runs_dir']) / self.args.task
name = f'{self.args.mode}'
self.save_dir = increment_path(Path(project) / name, exist_ok=self.args.exist_ok)
self.done_warmup = False
if self.args.show:
self.args.show = check_imshow(warn=True)
# GUI args
self.used_model_name = None # The detection model name to use
self.new_model_name = None # Models that change in real time
self.source = '' # input source
self.stop_dtc = False # Termination detection
self.continue_dtc = True # pause
self.save_res = False # Save test results
self.save_txt = False # save label(txt) file
self.iou_thres = 0.45 # iou
self.conf_thres = 0.25 # conf
self.speed_thres = 10 # delay, ms
self.labels_dict = {} # return a dictionary of results
self.progress_value = 0 # progress bar
# Usable if setup is done
self.model = None
self.data = self.args.data # data_dict
self.imgsz = None
self.device = None
self.dataset = None
self.vid_path, self.vid_writer = None, None
self.annotator = None
self.data_path = None
self.source_type = None
self.batch = None
self.callbacks = defaultdict(list, callbacks.default_callbacks) # add callbacks
callbacks.add_integration_callbacks(self)
# main for detect
@smart_inference_mode()
def run(self):
try:
if self.args.verbose:
LOGGER.info('')
# set model
self.yolo2main_status_msg.emit('Loding Model...')
if not self.model:
self.setup_model(self.new_model_name)
self.used_model_name = self.new_model_name
# set source
self.setup_source(self.source if self.source is not None else self.args.source)
# Check save path/label
if self.save_res or self.save_txt:
(self.save_dir / 'labels' if self.save_txt else self.save_dir).mkdir(parents=True, exist_ok=True)
# warmup model
if not self.done_warmup:
self.model.warmup(imgsz=(1 if self.model.pt or self.model.triton else self.dataset.bs, 3, *self.imgsz))
self.done_warmup = True
self.seen, self.windows, self.dt, self.batch = 0, [], (ops.Profile(), ops.Profile(), ops.Profile()), None
# start detection
# for batch in self.dataset:
count = 0 # run location frame
start_time = time.time() # used to calculate the frame rate
batch = iter(self.dataset)
while True:
# Termination detection
if self.stop_dtc:
if isinstance(self.vid_writer[-1], cv2.VideoWriter):
self.vid_writer[-1].release() # release final video writer
self.yolo2main_status_msg.emit('Detection terminated!')
break
# Change the model midway
if self.used_model_name != self.new_model_name:
# self.yolo2main_status_msg.emit('Change Model...')
self.setup_model(self.new_model_name)
self.used_model_name = self.new_model_name
# pause switch
if self.continue_dtc:
# time.sleep(0.001)
self.yolo2main_status_msg.emit('Detecting...')
batch = next(self.dataset) # next data
self.batch = batch
path, im, im0s, vid_cap, s = batch
visualize = increment_path(self.save_dir / Path(path).stem, mkdir=True) if self.args.visualize else False
# Calculation completion and frame rate (to be optimized)
count += 1 # frame count +1
if vid_cap:
all_count = vid_cap.get(cv2.CAP_PROP_FRAME_COUNT) # total frames
else:
all_count = 1
self.progress_value = int(count/all_count*1000) # progress bar(0~1000)
if count % 5 == 0 and count >= 5: # Calculate the frame rate every 5 frames
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。