Python pysimple gui opencv multitracking rubrika: Programování: Python

2 ToTR
položil/-a 8.8.2019

Hram sa trosku s multitrackingom pomocov opencv. Problem je ze si neviem poradit s vykreslenim "bbox = cv2.selectROI('MultiTracker', frame)" v okne pysimplegui a popravde nemam ani ziadnu ideu ci je to vobec mozne.
Druhy problem je ten ze sa mi z neznameho dovodu nechcu pridavat nove vybrane objekty, mozem si vybrat len jeden a ten to trackuje. Ma niekto tip?

import sys
import cv2
from random import randint
if sys.version_info[0] >= 3:
    import PySimpleGUI as sg
else:
    import PySimpleGUI27 as sg
globals
frame = ""
trackerTypes = ['BOOSTING', 'MIL', 'KCF','TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT']
 
def createTrackerByName(trackerType):
  # Create a tracker based on tracker name
  if trackerType == trackerTypes[0]:
    tracker = cv2.TrackerBoosting_create()
  elif trackerType == trackerTypes[1]: 
    tracker = cv2.TrackerMIL_create()
  elif trackerType == trackerTypes[2]:
    tracker = cv2.TrackerKCF_create()
  elif trackerType == trackerTypes[3]:
    tracker = cv2.TrackerTLD_create()
  elif trackerType == trackerTypes[4]:
    tracker = cv2.TrackerMedianFlow_create()
  elif trackerType == trackerTypes[5]:
    tracker = cv2.TrackerGOTURN_create()
  elif trackerType == trackerTypes[6]:
    tracker = cv2.TrackerMOSSE_create()
  elif trackerType == trackerTypes[7]:
    tracker = cv2.TrackerCSRT_create()
  else:
    tracker = None
    print('Incorrect tracker name')
    print('Available trackers are:')
    for t in trackerTypes:
      print(t)
 
  return tracker
 
# Set video to load
videoPath = "race_output_fast.avi"
 
# Create a video capture object to read videos
cap = cv2.VideoCapture(videoPath)
 
 
 
# OpenCV's selectROI function doesn't work for selecting multiple objects in Python
# So we will call this function in a loop till we are done selecting all objects
 
def create_tracker():
        frame_layout_top= [
                [sg.Image(filename='', key='image', size=(896,504)), sg.Output(size=(58, 20))]
                ]
        layout = [ 
              [sg.Frame('', frame_layout_top)]]
 
        window_select_object = sg.Window('Test',icon=None,
                       location=(100,0),resizable=False,use_default_focus=True,size=(1350,550))
        window_select_object.Layout(layout).Finalize()
 
         # Read first frame
        success, frame = cap.read()
          # quit if unable to read the video file
        if not success:
                print('Failed to read video')
                sys.exit(1)
         ## Select boxes
        bboxes = []
        colors = [] 
        trackerType = "CSRT"
        # OpenCV's selectROI function doesn't work for selecting multiple objects in Python
        # So we will call this function in a loop till we are done selecting all objects
        while True:
        # draw bounding boxes over objects
        # selectROI's default behaviour is to draw box starting from the center
        # when fromCenter is set to false, you can draw box starting from top left corner
             bbox = cv2.selectROI('MultiTracker', frame)
             bboxes.append(bbox)
             colors.append((randint(64, 255), randint(64, 255), randint(64, 255)))
             print("Press q to quit selecting boxes and start tracking")
             print("Press any other key to select next object")
             k = cv2.waitKey(0) & 0xFF
             if (k == 113):  # q is pressed
                break
 
        print('Selected bounding boxes {}'.format(bboxes))
 
        ## Initialize MultiTracker
        # There are two ways you can initialize multitracker
        # 1. tracker = cv2.MultiTracker("CSRT")
        # All the trackers added to this multitracker
        # will use CSRT algorithm as default
        # 2. tracker = cv2.MultiTracker()
        # No default algorithm specified

        # Initialize MultiTracker with tracking algo
        # Specify tracker type
  
        # Create MultiTracker object
        multiTracker = cv2.MultiTracker_create()
 
        # Initialize MultiTracker 
        for bbox in bboxes:
                multiTracker.add(createTrackerByName(trackerType), frame, bbox)
 
 
        # Process video and track objects
        while cap.isOpened():
                success, frame = cap.read()
                if not success:
                        break
 
        # get updated location of objects in subsequent frames
                success, boxes = multiTracker.update(frame)
 
        # draw tracked objects
                for i, newbox in enumerate(boxes):
                        p1 = (int(newbox[0]), int(newbox[1]))
                        p2 = (int(newbox[0] + newbox[2]), int(newbox[1] + newbox[3]))
                        cv2.rectangle(frame, p1, p2, colors[i], 2, 1)
 
        # show frame
                        cv2.imshow('MultiTracker', frame)
 
 
        # quit on ESC button
                if cv2.waitKey(1) & 0xFF == 27:  # Esc pressed
                        break

Pro plný přístup na Devel.cz se prosím přihlaste:

Rychlé přihlášení přes sociální sítě:

Nebo se přihlaste jménem a heslem:

Zadejte prosím svou e-mailovou adresu.
Zadejte své heslo.