使用视频流计算对象的数量

时间:2018-06-02 19:40:38

标签: python opencv

我目前正在制作一款游戏,其中涉及使用openCV库,使用Python作为编程语言进行视频流传输。游戏由12个QR标记(对象)组成。如何使用OpenCV(cv2库)计算视频流上的对象数(基准标签或QR码)。以下函数调用库函数,该函数读取QR代码并使用或增强现实为每个标记创建一个框架:

def readAndDetect(image):

    global markerDictionary
    markers = detect_markers(image)

    for marker in markers:
        marker.highlite_marker(image)
        #Create a dictionary key == MarkerId , Values: (center)]

        markerDictionary[marker.id] = marker.center
        print(markerDictionary)
    cv2.imshow('Test Frame', image)

我的代码不断生成对象,即不断复制和计算标记。我使用历史来跟踪对象以限制重复。有什么办法可以实现限制重复吗?

1 个答案:

答案 0 :(得分:0)

解决方案是创建一个历史记录,以保持并跟踪对象的数量,如下所示:

global StringPair

    markers = detect_markers(image)    

    # to remove duplicate 
    refined_markers = dict( [ ( marker.id, marker ) for marker in markers if self.checkForExistance(marker.id) ]  )
    # And then Assign the values to the markers As result, markers has no duplicate     
    markers = [ refined_markers[idx] for idx in refined_markers  ]

    # create history
    self.history.insert(0, markers )
    #print (">>>", history)
    if len(self.history) > 30:
        self.history.pop()

    # get all markers from history
    #this will be my list for stuff
    all_markers = {}
    for entry in reversed(self.history):
        for marker in entry:
            all_markers[marker.id] = marker

    #Generate and highlight the markers          
    for idx in all_markers:
        marker = all_markers[idx]
        marker.highlite_marker(image)        

    # Number of detected markers        
    self.count2 = len(all_markers)