高空抛物目标检测
项目地址:https://download.csdn.net/download/weixin_43710038/72735746
项目描述:高空抛物算法持续监测摄像头是否有运动物体,若有运动物体,则拍照保存为图片,显示物体运动轨迹
测试图片:
项目代码指导
运行环境为python3.7、opencv
-
log文件夹
error 日志文件主要包括项目运行错误输出
warn 日志文件主要包括抛物检测输出
-
pic文件夹
主要包括抛物检测图片,例为以上展示两张 -
camThread.py
主要为摄像头运行线程代码
class camThread(threading.Thread):
def __init__(self, camid, rtsp, instanceId, config):
threading.Thread.__init__(self)
self.camid = camid
self.rtsp = rtsp
self.instanceId = instanceId
self.config = config
def run(self):
try:
print(f"摄像头{self.camid}开始连接。")
pv = ProcessVideo(self.rtsp, self.camid, self.instanceId, self.config)
pv.process_video()
print(f"摄像头{self.camid}关闭连接。")
except Exception as e:
print(e)
- ProcessVideo.py
主要为抛物检测主要代码:
class ProcessVideo:
def __init__(self, rtsp, camid, instanceId, config):
self.rtsp = rtsp
self.camid = camid
self.instanceId = instanceId
self.config = config
self.left_x = 0
self.right_x = 0
self.top_y = 0
self.bottom_y = 0
self.pts = deque(maxlen=64)
self.day_time = None
self.warn_filename = None
self.error_filename = None
self.cur_time = None
def process_video(self):
path = "./log"
if not os.path.exists(path):
self.mkdir(path)
cap = cv.VideoCapture(self.rtsp)
if not cap.isOpened():
msg = f"在{datetime.now().isoformat().replace(':', '-').replace('T', ' ').split('.')[0]}时,摄像头{self.camid}无法连接rtsp视频流。\n"
print(msg, end="")
self.error_filename = path+'/'+datetime.now().isoformat().replace(':', '-').replace('T', ' ').split(' ')[0]+" error.txt"
self.create_or_write_file(self.error_filename, msg)
raise Exception(msg)
......
......
def process_frame(self, frame):
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
binary = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 25, 7)
return binary
def draw_trajectory(self, frame, boundingBox):
self.pts.appendleft(boundingBox)
for i in range(0, len(self.pts)):
cv.rectangle(frame, self.pts[i], (0, 0, 255), 2)
if (0 <= self.bottom_y-boundingBox[1]-boundingBox[3]//2 <= 50 or 0 <= boundingBox[0]+boundingBox[2]//2-self.left_x <= 50 or 0 <= self.right_x-boundingBox[0]-boundingBox[2]//2 <= 50) and len(self.pts) > 5:
self.pts.clear()
path = "./pic/"
self.mkdir(path)
filename = path+self.cur_time + ".png"
msg = f"在{self.cur_time}时,可能有抛物,摄像头id:{self.camid}。\n"
print(msg, end="")
self.create_or_write_file(self.warn_filename, msg)
cv.imwrite(filename, frame)
image_url = self.post_picture(filename)
......
......
def post_picture(self, filename):
try:
params = {"appKey" : self.config["appkey"]}
image_file = {"file" : (filename, open(filename, "rb"), "image/png")}
r = requests.post(self.config["uploadsingleurl"], params=params, files=image_file)
r.raise_for_status()
print(f"post_picture_url:{r.url}")
......
......
def post_notify(self, image_url):
try:
params = {"appKey" : self.config["appkey"], "instanceId" : self.instanceId, "cameraId" : self.camid, "imageUrl" : image_url}
r = requests.post(self.config["notifyurl"], params=params)
r.raise_for_status()
print(f"post_notify_url:{r.url}")
except Exception as e:
......
......
def mkdir(self, path):
if not os.path.exists(path):
os.makedirs(path)
def create_or_write_file(self, filename, msg):
f = open(filename, 'a', encoding="utf-8")
f.write(msg)
f.close()
- test.py
主要为测试代码,代码就不贴省略了,需要的自取
还包括两个高空抛物测试视频