高空抛物目标检测

高空抛物目标检测
项目地址: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
    主要为测试代码,代码就不贴省略了,需要的自取
    还包括两个高空抛物测试视频
    在这里插入图片描述
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

睡不醒.

创作不易,我想喝一杯卡布奇诺~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值