資源簡介
博文:基于python的行人與車輛檢測和跟蹤實現(HOG+SVM/HAAR)
鏈接:https://blog.csdn.net/qq_38523834/article/details/89619697
文件里面有我提到的視頻,cars.xml文件和myhaar.xml文件。需要的Python庫在requirements.txt有提及:
cmake==3.12.0
dlib==19.16.0
numpy==1.15.3
opencv-python==3.4.3.18
這些是最低版本,可以比這個高。我是用python3.6運行的。
沒有安裝dlib庫的同學,注意在安裝好所需庫之后,需要自行下載一個.whl文件后才能用pip install dlib 安裝成功哦。
相關下載鏈接在這個博文里:https://blog.csdn.net/wzx479/article/details/79890440
代碼片段和文件信息
import?cv2
import?dlib
import?time
import?threading
import?numpy?as?np
#?from?imutils.object_detection?import?non_max_suppression
import?math
hog?=?cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
#?carCascade?=?cv2.CascadeClassifier(‘myhaar.xml‘)
video?=?cv2.VideoCapture(‘project_output_haar1.avi‘)
def?estimateSpeed(location1?location2):
????d_pixels?=?math.sqrt(math.pow(location2[0]?-?location1[0]?2)?+?math.pow(location2[1]?-?location1[1]?2))
????#?ppm?=?location2[2]?/?carWidht
????ppm?=?8.8
????d_meters?=?d_pixels?/?ppm
????#?print(“d_pixels=“?+?str(d_pixels)?“d_meters=“?+?str(d_meters))
????fps?=?18
????speed?=?d_meters?*?fps?*?3.6
????return?speed
def?trackMultipleobjects():
????out?=?None
????counter_right?=?[]
????counter_moto?=?[]
????counter_wrong?=?[]
????counter_wrong_detect?=?[]
????rects?=?[]
????line_pass_left?=?np.array([[115200][115320]])
????line_pass_right?=?np.array([[930?200]?[930?320]])
????mask_people?=?np.array([line_pass_left[0]?line_pass_left[1]?line_pass_right[1]?line_pass_right[0]])
????#?line_down?=?[(0?360)?(800?360)]
????#?rectangleColor?=?(0?255?0)
????frameCounter?=?0
????currentCarID?=?0
????fps?=?0
????carTracker?=?{}
????carNumbers?=?{}
????carLocation1?=?{}
????carLocation2?=?{}
????speed?=?[None]?*?1000
????HEIGHT?=?720
????WIDTH?=?1280
????#?EXIT_COLOR?=?(66?183?42)
????#?Write?output?to?video?file
????out?=?cv2.VideoWriter(‘project_output_haar_and_svm1.avi‘?cv2.VideoWriter_fourcc(‘m‘‘p‘‘4‘‘v‘)?10?(WIDTH?HEIGHT))
????while?True:
????????start_time?=?time.time()
????????rc?image?=?video.read()
????????if?type(image)?==?type(None):
????????????break
????????image?=?cv2.resize(image?(WIDTH?HEIGHT))
????????resultImage?=?image.copy()
????????frameCounter?=?frameCounter?+?1
????????carIDtoDelete?=?[]
????????for?carID?in?carTracker.keys():
????????????trackingQuality?=?carTracker[carID].update(image)
????????????if?trackingQuality?7:
????????????????carIDtoDelete.append(carID)
????????for?carID?in?carIDtoDelete:
????????????print?(‘Removing?carID?‘?+?str(carID)?+?‘?from?list?of?trackers.‘)
????????????print?(‘Removing?carID?‘?+?str(carID)?+?‘?previous?location.‘)
????????????print?(‘Removing?carID?‘?+?str(carID)?+?‘?current?location.‘)
????????????carTracker.pop(carID?None)
????????????carLocation1.pop(carID?None)
????????????carLocation2.pop(carID?None)
????????#?detecting
????????if?not?(frameCounter?%?10):
????????????#gray?=?cv2.cvtColor(image?cv2.COLOR_BGR2GRAY)
????????????(rects?weights)?=?hog.detectMultiScale(image?winStride=(4?4)
????????????????????????????????????????????????????padding=(8?8)?scale=1.05)
????????????for?(_x?_y?_w?_h)?in?rects:
????????????????x?=?int(_x)
????????????????y?=?int(_y)
????????????????w?=?int(_w)
????????????????h?=?int(_h)
????????????????x_bar?=?x?+?0.5?*?w
????????????????y_bar?=?y?+
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????.C.....??????1278??2018-10-24?00:52??vehicle-speed-check-master\.gitignore
????.CA....???1385458??2018-10-24?00:52??vehicle-speed-check-master\cars.mp4
????.CA....????118803??2017-09-21?00:01??vehicle-speed-check-master\cars.xm
????.CA....????375639??2018-10-24?00:52??vehicle-speed-check-master\myhaar.xm
????.CA....??????9893??2019-04-23?15:15??vehicle-speed-check-master\people_tracking.py
????.CA....??11913613??2019-04-22?08:08??vehicle-speed-check-master\project.mp4
????.C.....??????1058??2018-10-24?00:52??vehicle-speed-check-master\README.md
????.C.....????????66??2018-10-24?00:52??vehicle-speed-check-master\requirements.txt
????.CA....??????7610??2019-04-23?15:02??vehicle-speed-check-master\speed_check.py
????.C.D...?????????0??2019-04-28?00:57??vehicle-speed-check-master
-----------?---------??----------?-----??----
?????????????13813418????????????????????10
評論
共有 條評論