新增推理视频流的逻辑

This commit is contained in:
2025-03-14 09:35:44 +08:00
parent 9d369b9898
commit 8478b4a102
2 changed files with 16 additions and 29 deletions

View File

@ -128,8 +128,7 @@ def run_detect_yolo(detect_in: ProjectDetectLogIn, detect: ProjectDetect, train:
return detect_log
async def run_commend(weights: str, source: str, project: str, name: str,
log_id: int, detect_id: int, session: Session):
async def run_detect_img(weights: str, source: str, project: str, name: str, log_id: int, detect_id: int, session: Session):
"""
执行yolov5的推理
:param weights: 权重文件
@ -180,9 +179,10 @@ async def run_commend(weights: str, source: str, project: str, name: str,
pdc.add_detect_imgs(detect_log_imgs, session)
def run_detect_rtsp(weights_pt: str, rtsp_url: str, data: str):
async def run_detect_rtsp(weights_pt: str, rtsp_url: str, data: str, detect_id: int):
"""
rtsp 视频流推理
:param detect_id: 训练集的id
:param weights_pt: 权重文件
:param rtsp_url: 视频流地址
:param data: yaml文件
@ -233,43 +233,21 @@ def run_detect_rtsp(weights_pt: str, rtsp_url: str, data: str):
# Process predictions
for i, det in enumerate(pred): # per image
seen += 1
p, im0, frame = path[i], im0s[i].copy(), dataset.count
s += f"{i}: "
p = Path(p) # to Path
s += "{:g}x{:g} ".format(*im.shape[2:]) # print string
gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh
imc = im0.copy() if False else im0 # for save_crop
annotator = Annotator(im0, line_width=3, example=str(names))
if len(det):
# Rescale boxes from img_size to im0 size
det[:, :4] = scale_boxes(im.shape[2:], det[:, :4], im0.shape).round()
# Print results
for c in det[:, 5].unique():
n = (det[:, 5] == c).sum() # detections per class
s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string
# Write results
for *xyxy, conf, cls in reversed(det):
c = int(cls) # integer class
label = names[c] if False else f"{names[c]}"
confidence = float(conf)
confidence_str = f"{confidence:.2f}"
c = int(cls) # integer class
label = None if False else (names[c] if False else f"{names[c]} {conf:.2f}")
annotator.box_label(xyxy, label, color=colors(c, True))
# Stream results
im0 = annotator.result()
if platform.system() == "Linux" and p not in windows:
windows.append(p)
cv2.namedWindow(str(p), cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) # allow window resize (Linux)
cv2.resizeWindow(str(p), im0.shape[1], im0.shape[0])
cv2.imshow(str(p), im0)
cv2.waitKey(1) # 1 millisecond