主頁 > 知識庫 > Python使用OPENCV的目標跟蹤算法實現(xiàn)自動視頻標注效果

Python使用OPENCV的目標跟蹤算法實現(xiàn)自動視頻標注效果

熱門標簽:哈爾濱外呼系統(tǒng)代理商 湛江電銷防封卡 電話機器人適用業(yè)務 鄭州智能外呼系統(tǒng)運營商 不錯的400電話辦理 獲客智能電銷機器人 南昌辦理400電話怎么安裝 徐州天音防封電銷卡 佛山防封外呼系統(tǒng)收費

先上效果

 1.首先,要使用opencv的目標跟蹤算法,必須要有opencv環(huán)境

 使用:opencv==4.4.0 和 opencv-contrib-python==4.4.0.46,lxml   這三個環(huán)境包。

也可以使用以下方法進行下載 : 

pip install opencv-python==4.4.0
pip install opencv-contrib-python==4.4.0.4

pip install lxml

2.使用方法:

        (1):英文狀態(tài)下的 “s” 是進行標注

        (2):使用小鍵盤 1-9 按下對應的標簽序號,標簽序號和標簽可自定義(需要提前定義)

       (3):對目標進行繪制

       (4):按空格鍵繼續(xù)

        重復進行 (1)(2)(3)(4)步驟,可實現(xiàn)多個目標的跟蹤繪制

        英文狀態(tài)下的 “r” 是所有清除繪制

         英文狀態(tài)下的 “q” 是退出

          當被跟蹤目標丟失時,自動清除所有繪制

import cv2
import os
import time
from lxml import etree
 
#視頻路徑
Vs = cv2.VideoCapture('peaple.avi')
#自定義標簽
Label = {1:"people",2:"car",3:"Camera"}
#圖片保存路徑 ,一定使用要用絕對路徑??!
imgpath = r"C:\Users\BGT\Desktop\opencv\img"
#xml保存路徑 ,一定使用要用絕對路徑?。?
xmlpath = r"C:\Users\BGT\Desktop\opencv\xml"
#設置視頻縮放
cv2.namedWindow("frame", 0)
#設置視頻寬高
cv2.resizeWindow("frame", 618, 416)
 
#定義生成xml類
class Gen_Annotations:
    def __init__(self, json_info):
        self.root = etree.Element("annotation")
 
        child1 = etree.SubElement(self.root, "folder")
        child1.text = str(json_info["pic_dirname"])
 
        child2 = etree.SubElement(self.root, "filename")
        child2.text = str(json_info["filename"])
 
        child3 = etree.SubElement(self.root, "path")
        child3.text = str(json_info["pic_path"])
 
        child4 = etree.SubElement(self.root, "source")
 
        child5 = etree.SubElement(child4, "database")
        child5.text = "My name is BGT"
 
    def set_size(self, witdh, height, channel):
        size = etree.SubElement(self.root, "size")
        widthn = etree.SubElement(size, "width")
        widthn.text = str(witdh)
        heightn = etree.SubElement(size, "height")
        heightn.text = str(height)
        channeln = etree.SubElement(size, "depth")
        channeln.text = str(channel)
        segmented = etree.SubElement(self.root, "segmented")
        segmented.text = "0"
 
    def savefile(self, filename):
        tree = etree.ElementTree(self.root)
        tree.write(filename, pretty_print=True, xml_declaration=False, encoding='utf-8')
 
    def add_pic_attr(self, label, x0, y0, x1, y1):
        object = etree.SubElement(self.root, "object")
        namen = etree.SubElement(object, "name")
        namen.text = label
        pose = etree.SubElement(object, "pose")
        pose.text = "Unspecified"
        truncated = etree.SubElement(object, "truncated")
        truncated.text = "0"
        difficult = etree.SubElement(object, "difficult")
        difficult.text = "0"
        bndbox = etree.SubElement(object, "bndbox")
        xminn = etree.SubElement(bndbox, "xmin")
        xminn.text = str(x0)
        yminn = etree.SubElement(bndbox, "ymin")
        yminn.text = str(y0)
        xmaxn = etree.SubElement(bndbox, "xmax")
        xmaxn.text = str(x1)
        ymaxn = etree.SubElement(bndbox, "ymax")
        ymaxn.text = str(y1)
 
 #定義生成xml的方法
def voc_opencv_xml(a,b,c,d,e,f,boxes,Label,Label_a,save="1.xml"):
    json_info = {}
    json_info["pic_dirname"] = a
    json_info["pic_path"] = b
    json_info["filename"] = c
    anno = Gen_Annotations(json_info)
 
    anno.set_size(d, e, f)
 
    for box in range(len(boxes)):
        x,y,w,h = [int(v) for v in boxes[box]]
        anno.add_pic_attr(Label[Label_a[box]],x,y,x+w,y+h)
    anno.savefile(save)
  
if __name__ == '__main__':
    Label_a = []
    contents = os.path.split(imgpath)[1]
    trackers = cv2.MultiTracker_create()
    while True:
        Filename_jpg = str(time.time()).split(".")[0] + "_" + str(time.time()).split(".")[1] + ".jpg"
        Filename_xml = str(time.time()).split(".")[0] + "_" + str(time.time()).split(".")[1] + ".xml"
 
        path_Filename_jpg = os.path.join(imgpath,Filename_jpg)
        path_Filename_xml = os.path.join(xmlpath,Filename_xml)
 
        ret,frame = Vs.read()
        if not ret:
            break
 
        success,boxes = trackers.update(frame)
        if len(boxes)>0:
            cv2.imwrite(path_Filename_jpg, frame)
            judge = True
        else:
            judge = False
 
         if success==False:
            print("目標丟失")
            trackers = cv2.MultiTracker_create()
            Label_a = []
            judge = False
        if judge:
            voc_opencv_xml(contents,Filename_jpg,path_Filename_jpg,frame.shape[1],frame.shape[0],frame.shape[2],boxes,Label,Label_a,path_Filename_xml)
        if judge:
            for box in range(len(boxes)):
                x,y,w,h = [int(v) for v in boxes[box]]
                cv2.putText(frame, Label[Label_a[box]], (x, y), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 1)
                cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
  
        cv2.imshow('frame',frame)
 
        var = cv2.waitKey(30)
 
        if var == ord('s'):
            imgzi = cv2.putText(frame, str(Label), (50, 50), cv2.FONT_HERSHEY_TRIPLEX, 1, (0, 255, 0), 2)
            cv2.imshow('frame', frame)
            var = cv2.waitKey(0)
            if var-48len(Label) or var-48=len(Label):
                Label_a.append(int(var-48))
            box = cv2.selectROI("frame", frame, fromCenter=False,showCrosshair=True)
            tracker = cv2.TrackerCSRT_create()
            trackers.add(tracker,frame,box)
        elif var == ord("r"):
            trackers = cv2.MultiTracker_create()
            Label_a = []
        elif var == ord('q'): #退出
            break
 
    Vs.release()
    cv2.destroyAllWindows()
 

3.得到xml和img數(shù)據(jù)是VOC格式,img和xml文件以時間戳進行命名。防止同名覆蓋。

4.最后使用 labelImg軟件  對獲取到的img和xml進行最后的檢查和微調

到此這篇關于Python使用OPENCV的目標跟蹤算法進自動視頻標注效果的文章就介紹到這了,更多相關OPENCV目標跟蹤自動視頻標注內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • opencv3/C++基于顏色的目標跟蹤方式
  • Python+OpenCV目標跟蹤實現(xiàn)基本的運動檢測
  • Opencv基于CamShift算法實現(xiàn)目標跟蹤

標簽:蘭州 安康 懷化 吉安 呂梁 蕪湖 廣西 紹興

巨人網(wǎng)絡通訊聲明:本文標題《Python使用OPENCV的目標跟蹤算法實現(xiàn)自動視頻標注效果》,本文關鍵詞  Python,使用,OPENCV,的,目標,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Python使用OPENCV的目標跟蹤算法實現(xiàn)自動視頻標注效果》相關的同類信息!
  • 本頁收集關于Python使用OPENCV的目標跟蹤算法實現(xiàn)自動視頻標注效果的相關信息資訊供網(wǎng)民參考!
  • 推薦文章