程序示例精选
Python OpenCV高速公路道路汽车车辆侦测检测识别统计数量
如需安装运行环境或远程调试,可点击 博主头像进入个人主页查看博主联系方式,由专业技术人员远程协助!
前言
这篇博客针对Python OpenCV高速公路道路汽车车辆侦测检测识别统计数量编写代码,代码整洁,规则,易读。 学习与应用推荐首选。
运行结果
文章目录
一、所需工具软件二、使用步骤
1. 主要代码
2. 运行结果
三、在线协助
一、所需工具软件
1. Pycharm, Python2.OpenCV
二、使用步骤
代码如下(示例):
import cv2 import numpy as np import time cap=cv2.VideoCapture("video.mp4") fgbg=cv2.createBack(detectShadows=False,history=200,varThreshold = 90) kernalOp = np.ones((3,3),np.uint8) kernalOp2 = np.ones((5,5),np.uint8) kernalCl = np.ones((11,11),np.uint8) font = cv2.FONT_HERSHEY_SIMPLEX cars = [] max_p_age = 5 pid = 1 cnt_up=0 cnt_down=0 print("Car counting and classification") line_up=400 line_down=250 up_limit=230 down_limit=int(4.5*(500/5)) while(cap.isOpened()): ret,frame=cap.read() frame=cv2.resize(frame,(900,500)) for i in cars: i.age_one() fgmask=fgbg.apply(frame) mask,countours0,hierarchy=cv2.findContours(mask,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) for cnt in countours0: area=cv2.contourArea(cnt) print(area) if area>300: m=cv2.moments(cnt) cx=int(m['m10']/m['m00']) cy=int(m['m01']/m['m00']) x,y,w,h=cv2.boundingRect(cnt) new=True if cy in range(up_limit,down_limit): for i in cars: if abs(x - i.getX()) <= w and abs(y - i.getY()) <= h: new = False i.updateCoords(cx, cy) if i.going_UP(line_down,line_up)==True: cnt_up+=1 elif i.going_DOWN(line_down,line_up)==True: cnt_down+=1 break if new==True: p=vehicles.Car(pid,cx,cy,max_p_age) cars.append(p) pid+1 cv2.circle(frame, (cx, cy), 2, (0, 0, 255), -1) for i in cars: cv2.putText(frame, str(i.getId()), (i.getX(), i.getY()), font, 0.3, (255,255,0), 1, cv2.LINE_AA) if line_down+20<= i.getY() <= line_up-20: a = (h + (.74*w)- 100) if a >= 0: cv2.putText(frame, "Truck", (i.getX(), i.getY()), font, 1, (0,0,255), 2, cv2.LINE_AA) else: cv2.putText(frame, "car", (i.getX(), i.getY()), font, 1, (0,0,255), 2, cv2.LINE_AA) str_up='UP: '+str(cnt_up) str_down='DOWN: '+str(cnt_down) frame=cv2.line(frame,(0,line_up),(900,line_up),(0,0,255),3,8) frame=cv2.line(frame,(0,up_limit),(900,up_limit),(0,0,0),1,8) frame=cv2.line(frame,(0,down_limit),(900,down_limit),(255,255,0),1,8) frame = cv2.line(frame, (0, line_down), (900, line_down), (255, 0,0), 3, 8) cv2.imshow('Frame',frame) if cv2.waitKey(1)&0xff==ord('q'): break else: break cap.release() cv2.destroyAllWindows()
运行结果
三、在线协助:
如需安装运行环境或远程调试,可点击博主头像,进入个人主页查看博主联系方式,由专业技术人员远程协助!
1)远程安装运行环境,代码调试
2)Visual Studio, Qt, C++, Python编程语言入门指导
3)界面美化
4)软件制作