UPDATE Vision 更新行人检测方法

This commit is contained in:
HJW
2024-10-26 11:39:52 +08:00
parent 343e24bccd
commit 60f145c608
3 changed files with 49 additions and 34 deletions

View File

@ -11,6 +11,7 @@ import random
import cv2
import numpy as np
import torch
from Vision.tool.CameraHIK import camera_HIK
from ultralytics.nn.autobackend import AutoBackend
from ultralytics.utils import ops
@ -23,6 +24,11 @@ class DetectionPerson:
"""
def __init__(self) -> None:
model_path = ''.join([os.getcwd(), '/Vision/model/pt/person.pt'])
ip = "192.168.1.65"
port = 554
name = "admin"
pw = "lzhk.159"
self.HIk_Camera = camera_HIK(ip, port, name, pw)
self.imgsz = 640
self.cuda = 'cpu'
self.conf = 0.40
@ -36,30 +42,33 @@ class DetectionPerson:
{self.names[i]: (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
for i in range(len(self.names))})
def get_person(self, img_src):
def get_person(self):
ret, img_src = self.HIk_Camera.get_img()
person = False
# img_src = cv2.imread(img_path)
img = self.precess_image(img_src, self.imgsz, self.half, self.cuda)
preds = self.model(img)
det = ops.non_max_suppression(preds, self.conf, self.iou, classes=None, agnostic=False, max_det=300,
nc=len(self.names))
if ret:
# img_src = cv2.imread(img_path)
img = self.precess_image(img_src, self.imgsz, self.half, self.cuda)
preds = self.model(img)
det = ops.non_max_suppression(preds, self.conf, self.iou, classes=None, agnostic=False, max_det=300,
nc=len(self.names))
for i, pred in enumerate(det):
lw = max(round(sum(img_src.shape) / 2 * 0.003), 2) # line width
tf = max(lw - 1, 1) # font thickness
sf = lw / 3 # font scale
pred[:, :4] = ops.scale_boxes(img.shape[2:], pred[:, :4], img_src.shape)
results = pred.cpu().detach().numpy()
# cv2.imshow('img2', img_src)
# cv2.waitKey(1)
for result in results:
if self.names[result[5]]=="person":
person = True
conf = round(result[4], 2)
print(conf)
self.draw_box(img_src, result[:4], conf, self.names[result[5]], lw, sf, tf)
for i, pred in enumerate(det):
lw = max(round(sum(img_src.shape) / 2 * 0.003), 2) # line width
tf = max(lw - 1, 1) # font thickness
sf = lw / 3 # font scale
pred[:, :4] = ops.scale_boxes(img.shape[2:], pred[:, :4], img_src.shape)
results = pred.cpu().detach().numpy()
# cv2.imshow('img2', img_src)
# cv2.waitKey(1)
for result in results:
if self.names[result[5]]=="person":
person = True
conf = round(result[4], 2)
#print(conf)
self.draw_box(img_src, result[:4], conf, self.names[result[5]], lw, sf, tf)
return person, img_src
return person, img_src
return person, None
def draw_box(self, img_src, box, conf, cls_name, lw, sf, tf):
color = self.color[cls_name]

View File

@ -23,14 +23,14 @@ class camera_HIK():
def __init__(self, ip, port, name, pw):
# "rtsp://admin:zlzk.123@192.168.1.64:554"
self.camera_url = "rtsp://" + name + ":" + pw + "@" + ip + ":" + str(port)
ret = portisopen(ip, port)
self.camera_url = "rtsp://" + str(name) + ":" + str(pw) + "@" + str(ip) + ":" + str(port)
self.ip = ip
self.port = port
self.init_success = False
if ret:
self.cap = cv2.VideoCapture(self.camera_url)
self.init_sucess = True
self.init_success = True
else:
print('海康摄像头网络错误请检测IP')

View File

@ -51,17 +51,23 @@ def take_photo_position_test():
def detectionPerson_test():
detectionPerson = DetectionPerson()
video_path = ''.join([os.getcwd(), '/Vision/model/data/1.mp4'])
cam = cv2.VideoCapture(video_path)
while cam.isOpened():
ret, frame = cam.read()
if ret:
person, img = detectionPerson.get_person(frame)
print(person)
cv2.imshow('img', img)
while True:
person, img_src = detectionPerson.get_person()
if img_src is not None:
cv2.imshow('detectPerson', img_src)
cv2.waitKey(1)
else:
break
print("person", person)
# video_path = ''.join([os.getcwd(), '/Vision/model/data/1.mp4'])
# cam = cv2.VideoCapture(video_path)
# while cam.isOpened():
# ret, frame = cam.read()
# if ret:
# person, img = detectionPerson.get_person(frame)
# print(person)
# cv2.imshow('img', img)
# cv2.waitKey(1)
# else:
# break
def HIK_Camera_test():
MY_CAMERA = camera_HIK("192.168.1.121", 554, "admin", "zlzk.123")
@ -87,4 +93,4 @@ def detectionBagNum_test():
if __name__ == '__main__':
detectionBagNum_test()
detectionPerson_test()