python實現目標跟蹤(opencv)

2021-09-02 23:53:37 字數 3713 閱讀 5187

1.單目標跟蹤

import cv2

import sys

(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')

print(major_ver, minor_ver, subminor_ver)

if __name__ == '__main__':

# 建立***

tracker_type = 'mil'

tracker = cv2.trackermil_create()

video = cv2.videocapture("./data/1.mp4")

# 讀入第一幀

ok, frame = video.read()

if not ok:

print('cannot read video file')

sys.exit()

# 定義乙個bounding box

bbox = (287, 23, 86, 320)

bbox = cv2.selectroi(frame, false)

# 用第一幀初始化

ok = tracker.init(frame, bbox)

while true:

ok, frame = video.read()

if not ok:

break

# start timer

timer = cv2.gettickcount()

# update tracker

ok, bbox = tracker.update(frame)

# cakculate fps

fps = cv2.gettickfrequency() / (cv2.gettickcount() - timer)

# draw bonding box

if ok:

p1 = (int(bbox[0]), int(bbox[1]))

p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))

cv2.rectangle(frame, p1, p2, (255,0,0), 2, 1)

else:

cv2.puttext(frame, "tracking failed detected", (100, 80), cv2.font_hershey_******x, 0.75, (0, 0, 255), 2)

# 展示tracker型別

cv2.puttext(frame, tracker_type+"tracker", (100, 20), cv2.font_hershey_******x, 0.75, (50, 170, 50), 2)

# 展示fps

cv2.puttext(frame, "fps:"+str(fps), (100, 50), cv2.font_hershey_******x, 0.75, (50, 170, 50), 2)

# result

cv2.imshow("tracking", frame)

# exit

k = cv2.waitkey(1) & 0xff

if k ==27 : break

2.多目標跟蹤

使用goturn作為***時,須將goturn.caffemodel和goturn.prototxt放到工作目錄才能執行,解決問題鏈結

import cv2

import sys

(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')

print(major_ver, minor_ver, subminor_ver)

if __name__ == '__main__':

# 建立***

# 'boosting', 'mil','kcf', 'tld', 'medianflow', 'goturn', 'mosse'

tracker_type = 'mil'

tracker = cv2.multitracker_create()

# 建立視窗

cv2.namedwindow("tracking")

video = cv2.videocapture("./data/1.mp4")

# 讀入第一幀

ok, frame = video.read()

if not ok:

print('cannot read video file')

sys.exit()

# 定義乙個bounding box

box1 = cv2.selectroi("tracking", frame)

box2 = cv2.selectroi("tracking", frame)

box3 = cv2.selectroi("tracking", frame)

# 用第一幀初始化

ok = tracker.add(cv2.trackermil_create(), frame, box1)

ok1 = tracker.add(cv2.trackermil_create(), frame, box2)

ok2 = tracker.add(cv2.trackermil_create(), frame, box3)

while true:

ok, frame = video.read()

if not ok:

break

# start timer

timer = cv2.gettickcount()

# update tracker

ok, boxes = tracker.update(frame)

print(ok, boxes)

# cakculate fps

fps = cv2.gettickfrequency() / (cv2.gettickcount() - timer)

for box in boxes:

# draw bonding box

if ok:

p1 = (int(box[0]), int(box[1]))

p2 = (int(box[0] + box[2]), int(box[1] + box[3]))

cv2.rectangle(frame, p1, p2, (255, 0, 0), 2, 1)

else:

cv2.puttext(frame, "tracking failed detected", (100, 80), cv2.font_hershey_******x, 0.75, (0, 0, 255),2)

# 展示tracker型別

cv2.puttext(frame, tracker_type+"tracker", (100, 20), cv2.font_hershey_******x, 0.75, (50, 170, 50), 2)

# 展示fps

cv2.puttext(frame, "fps:"+str(fps), (100, 50), cv2.font_hershey_******x, 0.75, (50, 170, 50), 2)

# result

cv2.imshow("tracking", frame)

# exit

k = cv2.waitkey(1) & 0xff

if k ==27 : break

使用SSD實現目標檢測

本人主要參考這位大神的研究 利用這位大神的 執行了一遍ssd,之後會進一步研究ssd框架的原理和利用ssd布置自己的機械人。安裝和實現的過程中遇到了下面的一些編譯和執行的問題,我想先記錄下來,以備不時之需。第一 在對進行檢測的時候遇到 import matplotlib.pyplot as plt ...

python opencv實現目標區域裁剪功能

這個任務是自己在專案中資料處理的一部分內容,待處理的如下所示 我需要將目標區域給裁剪出來,要不然在後期訓練網路的時候整幅影象過大,且目標區域過小,得到結果不好,還會加劇計算量。在網上找了各個大佬的部落格看,沒找到合適的,便自己動手寫了,順便自己的小破站剛搭建起來,記錄一下自己的思路。思路去尋找目標區...

python練習題,利用多型實現目標

print 租用 s的天數是 d,是 d self.name,self.days,600 self.days class bwm car def init self,name,days super init name self.days days def price self print 租用 s的...