国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

OpenCV與AI深度學(xué)習(xí) | 實戰(zhàn) | 基于YOLOv9和OpenCV實現(xiàn)車輛跟蹤計數(shù)(步驟 + 源碼)

這篇具有很好參考價值的文章主要介紹了OpenCV與AI深度學(xué)習(xí) | 實戰(zhàn) | 基于YOLOv9和OpenCV實現(xiàn)車輛跟蹤計數(shù)(步驟 + 源碼)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

本文來源公眾號“OpenCV與AI深度學(xué)習(xí)”,僅用于學(xué)術(shù)分享,侵權(quán)刪,干貨滿滿。

原文鏈接:實戰(zhàn) | 基于YOLOv9和OpenCV實現(xiàn)車輛跟蹤計數(shù)(步驟 + 源碼)

導(dǎo)? 讀

????本文主要介紹使用YOLOv9和OpenCV實現(xiàn)車輛跟蹤計數(shù)(步驟 + 源碼)。?

實現(xiàn)步驟

圖像識別經(jīng)過的車輛并計數(shù)深度學(xué)習(xí),深度學(xué)習(xí)拓展閱讀,人工智能,opencv,深度學(xué)習(xí),python,計算機(jī)視覺,YOLO,算法

????監(jiān)控攝像頭可以有效地用于各種場景下的車輛計數(shù)和交通流量統(tǒng)計。先進(jìn)的計算機(jī)視覺技術(shù)(例如對象檢測和跟蹤)可應(yīng)用于監(jiān)控錄像,以識別和跟蹤車輛在攝像機(jī)視野中移動。

【1】安裝ultralytics,因為它擁有直接使用 YoloV9 預(yù)訓(xùn)練模型的方法。

pip install ultralytics

【2】完成后,就可以創(chuàng)建跟蹤器函數(shù)來跟蹤對象了。我們只是為此創(chuàng)建了一個名為tracker.py的python文件。

import math

class CustomTracker:
    def __init__(self):
        # Store the center positions of the objects
        self.custom_center_points = {}
        # Keep the count of the IDs
        # each time a new object id detected, the count will increase by one
        self.custom_id_count = 0

    def custom_update(self, custom_objects_rect):
        # Objects boxes and ids
        custom_objects_bbs_ids = []

        # Get center point of new object
        for custom_rect in custom_objects_rect:
            x, y, w, h = custom_rect
            cx = (x + x + w) // 2
            cy = (y + y + h) // 2

            # Find out if that object was detected already
            same_object_detected = False
            for custom_id, pt in self.custom_center_points.items():
                dist = math.hypot(cx - pt[0], cy - pt[1])

                if dist < 35:
                    self.custom_center_points[custom_id] = (cx, cy)
                    custom_objects_bbs_ids.append([x, y, w, h, custom_id])
                    same_object_detected = True
                    break

            # New object is detected we assign the ID to that object
            if same_object_detected is False:
                self.custom_center_points[self.custom_id_count] = (cx, cy)
                custom_objects_bbs_ids.append([x, y, w, h, self.custom_id_count])
                self.custom_id_count += 1

        # Clean the dictionary by center points to remove IDS not used anymore
        new_custom_center_points = {}
        for custom_obj_bb_id in custom_objects_bbs_ids:
            _, _, _, _, custom_object_id = custom_obj_bb_id
            center = self.custom_center_points[custom_object_id]
            new_custom_center_points[custom_object_id] = center

        # Update dictionary with IDs not used removed
        self.custom_center_points = new_custom_center_points.copy()
        return custom_objects_bbs_ids

【3】編寫車輛計數(shù)的主要代碼。

# Import the Libraries
import cv2
import pandas as pd
from ultralytics import YOLO
from tracker import *

????導(dǎo)入所有必要的庫后,就可以導(dǎo)入模型了。我們不必從任何存儲庫下載模型。Ultralytics 做得非常出色,讓我們可以更輕松地直接下載它們。

model=YOLO('yolov9c.pt')

????這會將 yolov9c.pt 模型下載到當(dāng)前目錄中。該模型已經(jīng)在由 80 個不同類別組成的 COCO 數(shù)據(jù)集上進(jìn)行了訓(xùn)練?,F(xiàn)在讓我們指定類:

???????class_list = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']

現(xiàn)在,下一步是加載您要使用的視頻。

tracker=CustomTracker()
count=0

cap = cv2.VideoCapture('traffictrim.mp4')

# Get video properties
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

# Create VideoWriter object to save the modified frames
output_video_path = 'output_video.mp4'
fourcc = cv2.VideoWriter_fourcc(*'mp4v')  # You can use other codecs like 'XVID' based on your system
out = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))

在這里,我們在加載視頻后獲取視頻屬性,因為它們對于使用計數(shù)器重新創(chuàng)建視頻并最終將其存儲在本地非常有用。

# Looping over each frame and Performing the Detection

down = {}
counter_down = set()
while True:
    ret, frame = cap.read()
    if not ret:
        break
    count += 1

    results = model.predict(frame)

    a = results[0].boxes.data
    a = a.detach().cpu().numpy()
    px = pd.DataFrame(a).astype("float")
    # print(px)

    list = []

    for index, row in px.iterrows():
        #        print(row)
        x1 = int(row[0])
        y1 = int(row[1])
        x2 = int(row[2])
        y2 = int(row[3])
        d = int(row[5])
        c = class_list[d]
        if 'car' in c:
            list.append([x1, y1, x2, y2])

    bbox_id = tracker.custom_update(list)
    # print(bbox_id)
    for bbox in bbox_id:
        x3, y3, x4, y4, id = bbox
        cx = int(x3 + x4) // 2
        cy = int(y3 + y4) // 2
        # cv2.circle(frame,(cx,cy),4,(0,0,255),-1) #draw ceter points of bounding box
        # cv2.rectangle(frame, (x3, y3), (x4, y4), (0, 255, 0), 2)  # Draw bounding box
        # cv2.putText(frame,str(id),(cx,cy),cv2.FONT_HERSHEY_COMPLEX,0.8,(0,255,255),2)

        y = 308
        offset = 7

        ''' condition for red line '''
        if y < (cy + offset) and y > (cy - offset):
            ''' this if condition is putting the id and the circle on the object when the center of the object touched the red line.'''

            down[id] = cy  # cy is current position. saving the ids of the cars which are touching the red line first.
            # This will tell us the travelling direction of the car.
            if id in down:
                cv2.circle(frame, (cx, cy), 4, (0, 0, 255), -1)
                #cv2.putText(frame, str(id), (cx, cy), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 255, 255), 2)
                counter_down.add(id)

                # # line
    text_color = (255, 255, 255)  # white color for text
    red_color = (0, 0, 255)  # (B, G, R)

    # print(down)
    cv2.line(frame, (282, 308), (1004, 308), red_color, 3)  # starting cordinates and end of line cordinates
    cv2.putText(frame, ('red line'), (280, 308), cv2.FONT_HERSHEY_SIMPLEX, 0.5, text_color, 1, cv2.LINE_AA)


    downwards = (len(counter_down))
    cv2.putText(frame, ('Vehicle Counter - ') + str(downwards), (60, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.5, red_color, 1,
                cv2.LINE_AA)

    cv2.line(frame,(282,308),(1004,308),red_color,3)  #  starting cordinates and end of line cordinates
    cv2.putText(frame,('red line'),(280,308),cv2.FONT_HERSHEY_SIMPLEX, 0.5, text_color, 1, cv2.LINE_AA)
    
    # This will write the Output Video to the location specified above
    out.write(frame)

????????在上面的代碼中,我們循環(huán)遍歷視頻中的每個幀,然后進(jìn)行檢測。然后,由于我們僅對車輛進(jìn)行計數(shù),因此僅過濾掉汽車的檢測結(jié)果。

????????之后,我們找到檢測到的車輛的中心,然后在它們穿過人工創(chuàng)建的紅線時對它們進(jìn)行計數(shù)。我們可以在下面的視頻快照中清楚地看到它們。

圖像識別經(jīng)過的車輛并計數(shù)深度學(xué)習(xí),深度學(xué)習(xí)拓展閱讀,人工智能,opencv,深度學(xué)習(xí),python,計算機(jī)視覺,YOLO,算法

圖像識別經(jīng)過的車輛并計數(shù)深度學(xué)習(xí),深度學(xué)習(xí)拓展閱讀,人工智能,opencv,深度學(xué)習(xí),python,計算機(jī)視覺,YOLO,算法

圖像識別經(jīng)過的車輛并計數(shù)深度學(xué)習(xí),深度學(xué)習(xí)拓展閱讀,人工智能,opencv,深度學(xué)習(xí),python,計算機(jī)視覺,YOLO,算法

圖像識別經(jīng)過的車輛并計數(shù)深度學(xué)習(xí),深度學(xué)習(xí)拓展閱讀,人工智能,opencv,深度學(xué)習(xí),python,計算機(jī)視覺,YOLO,算法

圖像識別經(jīng)過的車輛并計數(shù)深度學(xué)習(xí),深度學(xué)習(xí)拓展閱讀,人工智能,opencv,深度學(xué)習(xí),python,計算機(jī)視覺,YOLO,算法

我們可以看到,當(dāng)車輛越過紅線時,視頻左上角的計數(shù)器不斷增加。

THE END!

文章結(jié)束,感謝閱讀。您的點贊,收藏,評論是我繼續(xù)更新的動力。大家有推薦的公眾號可以評論區(qū)留言,共同學(xué)習(xí),一起進(jìn)步。文章來源地址http://www.zghlxwxcb.cn/news/detail-852094.html

到了這里,關(guān)于OpenCV與AI深度學(xué)習(xí) | 實戰(zhàn) | 基于YOLOv9和OpenCV實現(xiàn)車輛跟蹤計數(shù)(步驟 + 源碼)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包