import cv2 import matplotlib.pyplot as plt import numpy as np %matplotlib inline trackers = { "BOOSTING": cv2.TrackerBoosting_create(), "MIL": cv2.TrackerMIL_create(), "KCF": cv2.TrackerKCF_create(), "TLD": cv2.TrackerTLD_create(), "MEDIANFLOW": cv2.TrackerMedianFlow_create(), "GOTURN": cv2.TrackerGOTURN_create(), "MOSSE": cv2.TrackerMOSSE_create(), "CSRT": cv2.TrackerCSRT_create(), } tracker = trackers["CSRT"] # Read video video = cv2.VideoCapture(0) while True: ok, frame = video.read() mask = np.full_like(frame, 150, dtype=np.uint8) mask[165:315, 245:395] = 0 background = cv2.add(frame, mask) # create the rect frame with blurring background cv2.imshow(None, background) k = cv2.waitKey(60) & 0xFF if k == 32: # it triggers tracking
import cv2 import sys my_track_method = cv2.TrackerMOSSE_create() # Doc file video cap = cv2.VideoCapture("src\\images\\race.mp4") # Doc frame dau tien de nguoi dung chon doi truong can track ret, frame = cap.read() if not ret: print('Khong tim thay file video') sys.exit() # Chon doi tuong va init tracking select_box = cv2.selectROI(frame, showCrosshair=True) my_track_method.init(frame, select_box) while True: # Read a new frame ok, frame = cap.read() if not ok: # Neu khong doc duoc tiep thi out break # Update tracker ret, select_box = my_track_method.update(frame) if ret: # Neu nhu tracking duoc thanh cong tl, br = (int(select_box[0]),
def tracking_face(vi_path="try.avi", t_type=7): # Set up tracker. # Instead of MIL, you can also use (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.') tracker_types = [ 'BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT' ] tracker_type = tracker_types[t_type] # if int(minor_ver) < 3: if int(major_ver) < 3: tracker = cv2.Tracker_create(tracker_type) else: if tracker_type == 'BOOSTING': tracker = cv2.TrackerBoosting_create() if tracker_type == 'MIL': tracker = cv2.TrackerMIL_create() if tracker_type == 'KCF': tracker = cv2.TrackerKCF_create() if tracker_type == 'TLD': tracker = cv2.TrackerTLD_create() if tracker_type == 'MEDIANFLOW': tracker = cv2.TrackerMedianFlow_create() if tracker_type == 'GOTURN': tracker = cv2.TrackerGOTURN_create() if tracker_type == 'MOSSE': tracker = cv2.TrackerMOSSE_create() if tracker_type == "CSRT": tracker = cv2.TrackerCSRT_create() # Read video video = cv2.VideoCapture(vi_path) # Exit if video not opened. if not video.isOpened(): print("Could not open video") sys.exit() # Read first frame. ok, frame = video.read() if not ok: print('Cannot read video file') sys.exit() # Define an initial bounding box height, width, layers = frame.shape # box # bbox_transfer() bbox, score = bbox_score(frame) bbox = np.squeeze(bbox)[0] score = np.squeeze(score)[0] while True: if score > 0.7: break ok, frame = video.read() bbox, score = bbox_score(frame) bbox = np.squeeze(bbox)[0] score = np.squeeze(score)[0] bbox = bbox_transfer(bbox, height, width) #(269,47,62,80) # bbox = (bbox[1]*width,bbox[0]*height,bbox[2]-bbox[0],bbox[3]-bbox[1]) # bbox = tuple(bbox) # bbox = (265, 48, 70, 82)# x,y, w, h # bbox = (48,151,128,186) # *H ymin, xmin, ymax, xmax # bbox = (85,270,228,332) # *W # bbox = (85,151,228,186) A - 151 85 (228-85) 186-151 # bbox = (48,270,128,332) B # print(bbox.shape,score.shape) # Uncomment the line below to select a different bounding box # bbox = cv2.selectROI(frame, False) # Initialize tracker with first frame and bounding box ok = tracker.init(frame, bbox) # bbox = bbox_transfer(bbox) while True: # Read a new frame ok, frame = video.read( ) # I think this line should be put after tracker.update if not ok: break # Start timer timer = cv2.getTickCount() # Update tracker ok, bbox = tracker.update(frame) # bbox --> [x1,y1,width,height] # Calculate Frames per second (FPS) fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer) # Draw bounding box if ok: # Tracking success p1 = (int(bbox[0]), int(bbox[1])) #(x1,y1) p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3])) #(x2,y2) cv2.rectangle(frame, p1, p2, (255, 0, 0), 2, 1) else: # Tracking failure cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) # Display tracker type on frame cv2.putText(frame, tracker_type + " Tracker", (100, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2) # Display FPS on frame cv2.putText(frame, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2) # Display result cv2.imshow("Tracking", frame) # Exit if ESC pressed k = cv2.waitKey(1) & 0xff if k == 27: break
import cv2 import time #set camera / tracker cam = cv2.VideoCapture(1) cam.set(10, 5) tracker = cv2.TrackerMOSSE_create() #600fps tracker = cv2.TrackerCSRT_create() #40fps def main(): img, bbox = pickimg(cam, tracker) track(cam, tracker, img, bbox) cv2.destroyAllWindows() cam.release() def drawBox(img, bbox): x, y, w, h = int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3]) cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 255), 1) pass def pickimg(cam, tracker): while 1: success, img = cam.read() cv2.putText(img, "Mode:Init", (75, 75), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) cv2.imshow("Track", img)
def tracking_face(img_path="./data_try/0__0", t_type=0): # Set up tracker. # Instead of MIL, you can also use (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.') tracker_types = [ 'BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT' ] tracker_type = tracker_types[t_type] global tracker # if int(minor_ver) < 3: if int(major_ver) < 3: tracker = cv2.Tracker_create(tracker_type) else: if tracker_type == 'BOOSTING': tracker = cv2.TrackerBoosting_create() if tracker_type == 'MIL': tracker = cv2.TrackerMIL_create() if tracker_type == 'KCF': tracker = cv2.TrackerKCF_create() if tracker_type == 'TLD': tracker = cv2.TrackerTLD_create() if tracker_type == 'MEDIANFLOW': tracker = cv2.TrackerMedianFlow_create() if tracker_type == 'GOTURN': tracker = cv2.TrackerGOTURN_create() if tracker_type == 'MOSSE': tracker = cv2.TrackerMOSSE_create() if tracker_type == "CSRT": tracker = cv2.TrackerCSRT_create() images_list = [img for img in os.listdir(img_path) if img.endswith(".png")] images_list = sort_img(images_list) img_txt_path = img_path.replace(img_path.split("/")[0], "./txt") # txt_path = img_txt_path+"_txt" txt_path = img_txt_path if not os.path.isdir(txt_path): os.makedirs(txt_path) frame = cv2.imread(os.path.join(img_path, images_list[0])) # Define an initial bounding box height, width, layers = frame.shape for i in range(len(images_list)): frame = cv2.imread(os.path.join(img_path, images_list[i])) bbox, score = bbox_score(frame) bbox = np.squeeze(bbox)[0] score = np.squeeze(score)[0] # write_txt(txt_path,images_list[i]) if score <= 0.7: bbox = bbox_transfer(bbox, height, width) write_txt(txt_path, images_list[i], bbox, "bad") else: bbox = bbox_transfer(bbox, height, width) #(269,47,62,80) # bbox = (269,47,62,80) tracker.init(frame, bbox) while i < len(images_list): frame = cv2.imread(os.path.join(img_path, images_list[i])) timer = cv2.getTickCount() # Update tracker ok, bbox = tracker.update(frame) bbox_w = bbox_int(bbox) write_txt(txt_path, images_list[i], bbox_w) # Calculate Frames per second (FPS) fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer) # Draw bounding box if ok: # Tracking success 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) i = i + 1 else: # Tracking failure cv2.putText(frame, "Tracking failure, re-initialize ...", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) # Display tracker type on frame cv2.putText(frame, tracker_type + " Tracker", (100, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2) # Display FPS on frame cv2.putText(frame, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2) # Display result cv2.imshow("Tracking", frame) # Exit if ESC pressed k = cv2.waitKey(1) & 0xff if k == 27: break break
#if video frame dosent exist, break if not ret: break #when tracking is not on if initBB is None: facerect = cascade.detectMultiScale(frame, scaleFactor=1.3, minNeighbors=1, minSize=(80, 80)) # when face detected if len(facerect) > 0: initBB = (facerect[0][0] / 2, facerect[0][1] / 2, facerect[0][0] + facerect[0][2], facerect[0][1] + facerect[0][3]) print("Detected!:") print(initBB) tracker = cv2.TrackerMOSSE_create( ) #MedianFlow is also pretty good #tracker = cv2.TrackerTLD_create() tracker.init(frame, initBB) for rect in facerect: cv2.rectangle(frame, tuple(rect[0:2]), tuple(rect[0:2] + rect[2:4]), rectangle_color, thickness=2) #when tracking is on else: (success, box) = tracker.update(frame) # check to see if the tracking was a success if success: (x, y, w, h) = [int(v) for v in box]
def main(): # Set up tracker. # Instead of MIL, you can also use tracker_types = [ 'BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT' ] tracker_type = tracker_types[7] print('Using tracker: {}'.format(tracker_type)) if int(minor_ver) < 3: tracker = cv2.Tracker_create(tracker_type) else: if tracker_type == 'BOOSTING': tracker = cv2.TrackerBoosting_create() if tracker_type == 'MIL': tracker = cv2.TrackerMIL_create() if tracker_type == 'KCF': tracker = cv2.TrackerKCF_create() if tracker_type == 'TLD': tracker = cv2.TrackerTLD_create() if tracker_type == 'MEDIANFLOW': tracker = cv2.TrackerMedianFlow_create() if tracker_type == 'GOTURN': tracker = cv2.TrackerGOTURN_create() if tracker_type == 'MOSSE': tracker = cv2.TrackerMOSSE_create() if tracker_type == "CSRT": tracker = cv2.TrackerCSRT_create() input_dict = {0: 'video', 1: 'images'} input_type = 'images' # We provide a either a video or sequence of frames # VideoFolder = '/Volumes/DEEPAK-SSD/GravityMachine/AbioticExperiments/InfiniteBeadDance.MOV' # VideoFolder = '/Volumes/GRAVMACH1/GravityMachine/SedimentingGlassBeads_2017/StableParticlePairsMVI_1902.MOV' # VideoFolder = '/Volumes/DEEPAK-SSD/GravityMachine/AbioticExperiments/SedimentingBeads/StablePair_Trimmed.MOV' # ImagesFolder = '/Volumes/DEEPAK-SSD/GravityMachine/AbioticExperiments/SedimentingBeads/StablePair_Trimmed.MOV' # ImagesFolder = '/Volumes/DEEPAK-SSD/GravityMachine/AbioticExperiments/ProcessedImages' # ImagesFolder = '/Volumes/DEEPAK-SSD/GravityMachine/TrackingFailureData/Starfish10_failure' # ImagesFolder = '/Volumes/DEEPAK-SSD/GravityMachine/TrackingFailureData/AcornWorm3_failure' # ImagesFolder = '/Volumes/DEEPAK-SSD/GravityMachine/TrackingFailureData/Polychaete1_failure' # ImagesFolder = '/Volumes/DEEPAK-SSD/GravityMachine/TrackingFailureData/Poly1_failure' # ImagesFolder = '/Volumes/DEEPAK-SSD/GravityMachine/TrackingFailureData/Poly2_failure/*.tif' # ImagesFolder = '/Volumes/DEEPAK-SSD/GravityMachine/TrackingFailureData/Starfish6_feeding' # ImagesFolder = '/Volumes/DEEPAK-SSD/GravityMachine/TrackingFailureData/Starfish6_wall_failure' # ImagesFolder = '/Volumes/GRAVMACH1/PyroTracks_2018_12_21/Pyro_Ballooning_GS' # ImagesFolder = '/Volumes/GRAVMACH1/HopkinsEmbroyologyCourse_GoodData/2018_06_12/Starfish/StarFish6/images' # ImagesFolder = '/Volumes/GRAVMACH1/HopkinsEmbroyologyCourse_GoodData/2018_06_11/Dendraster_starved_11Days_nofood/Dendraster3/images/' ImagesFolder = '/Users/deepak/Dropbox/GravityMachine/ExperimentResults/TestData/seacucmber4_auto_verylong_goodtrack/images' rootPath, *rest = os.path.split(ImagesFolder) savePath = os.path.join(rootPath, 'ParticleTracks2.csv') overwrite = True if (input_type == 'video'): # Read video video = cv2.VideoCapture(VideoFolder) # nFrames = video.get(CV_CAP_PROP_FRAME_COUNT) FPS = 30 deltaT = 1 / FPS print('Video FPS : {}'.format(FPS)) # Exit if video not opened. if not video.isOpened(): print("Could not open video") sys.exit() # Read first frame. ok, frame = video.read() if not ok: print('Cannot read video file') sys.exit() elif (input_type == 'images'): frames = pims.ImageSequence(ImagesFolder) nFrames = len(frames) frame = frames[0] frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # thresh_low = (0,0,95) # thresh_high = (255,255,255) # Contour = PIV_Functions.findContours(frame_hsv,thresh_low, thresh_high,'largest') # cv2.drawContours(frame_gray, [Contour], 0,(255,255,255),3) # cv2.imshow('frame',frame_gray) # cv2.imwrite('Dendraster_segmented.png',frame_gray) key = cv2.waitKey(0) & 0xFF # if the 'q' key is pressed, stop the loop if key == ord("q"): pass if (not os.path.exists(savePath) or overwrite == True): # Define an initial bounding box # bbox = (287, 23, 86, 320) # Uncomment the line below to select a different bounding box bbox = cv2.selectROI(frame, False) # Initialize tracker with first frame and bounding box ok = tracker.init(frame, bbox) nFrames = 50 Centroids = np.zeros((nFrames, 2)) Time = np.zeros((nFrames, 1)) counter = 0 while True and counter < nFrames: # Read a new frame if (input_type == 'video'): ok, frame = video.read() if not ok: break elif (input_type == 'images'): frame = frames[counter] # Start timer timer = cv2.getTickCount() # Update tracker ok, bbox = tracker.update(frame) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Calculate the center of the bounding box and store it x_pos = bbox[0] + bbox[2] / 2 y_pos = bbox[1] + bbox[3] / 2 # if(counter>0): # Time[counter,:] = Time[counter-1,:] + deltaT Centroids[counter, :] = [x_pos, y_pos] # Calculate Frames per second (FPS) fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer) # Draw bounding box if ok: # Tracking success 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: # Tracking failure cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255), 2) # Display tracker type on frame # cv2.putText(frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255,255,50),2); # Display FPS on frame # cv2.putText(frame, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255,255,50), 2); # Display result cv2.imshow("Tracking", frame) # cv2.imwrite(str(counter)+'.png',frame) # Exit if ESC pressed k = cv2.waitKey(1) & 0xff if k == 27: break print(counter) counter += 1 # Save data # Velocity = np.diff(Centroids,axis=0)/np.tile(np.diff(Time,axis=0),(1,2)) # Velocity = np.insert(Velocity, 0 ,[np.nan, np.nan], axis = 0) # df = pd.DataFrame({'X':np.squeeze(Centroids[:,0]),'Z':np.squeeze(Centroids[:,1])}) # df.to_csv(savePath, encoding='utf-8', index=False) else: df = pd.read_csv(savePath)
def __init__(self, x_center, half_width, y_center, half_height, y_bar_start, half_bar_width, half_bar_height, tracker_type='BOOSTING'): """ Parameters ---------- x_center : int Horizontal position of the center of the main area. half_width : int Half the width of the main area. y_center : int Vertical position of the center of the main area. half_height : int Half the height of the main area. y_bar_start : int Vertical position of the inner area. This value is relative to the main area. half_bar_width : int Half the width of the inner area. half_bar_height : int Half the height of the inner area. tracker_type : str Name of one of the built-in trackers in OpenCV. """ SectionProcessor.__init__(self) # Top-left corner of the main area. self.x_start = int(x_center - half_width) self.y_start = int(y_center - half_height) # Bottom-right corner of the main area. self.x_end = int(x_center + half_width) self.y_end = int(y_center + half_height) # Top-left corner of the inner area. This position is relative to the # main area. self.y_bar_start = y_bar_start self.x_bar_start = int(half_width - half_bar_width) # Inner area self.bbox = (self.x_bar_start, self.y_bar_start, half_bar_width * 2, half_bar_height * 2) # Vertical positions of the top-left corner of the inner area self.y_pos_history = None # Indices of the frames where the inner area is in the bottom position. self.peaks = None self.events = None # Tracker (major_ver, minor_ver, subminor_ver) = (cv.__version__).split('.') if int(minor_ver) < 3: self.tracker = cv.Tracker_create(tracker_type) else: if tracker_type == 'BOOSTING': self.tracker = cv.TrackerBoosting_create() if tracker_type == 'MIL': self.tracker = cv.TrackerMIL_create() if tracker_type == 'KCF': self.tracker = cv.TrackerKCF_create() if tracker_type == 'TLD': self.tracker = cv.TrackerTLD_create() if tracker_type == 'MEDIANFLOW': self.tracker = cv.TrackerMedianFlow_create() if tracker_type == 'GOTURN': self.tracker = cv.TrackerGOTURN_create() if tracker_type == 'MOSSE': self.tracker = cv.TrackerMOSSE_create() if tracker_type == "CSRT": self.tracker = cv.TrackerCSRT_create()
finger_tracker = cv2.TrackerMIL_create() phone_tracker = cv2.TrackerMIL_create() if tracker_type == 'KCF': finger_tracker = cv2.TrackerKCF_create() phone_tracker = cv2.TrackerKCF_create() if tracker_type == 'TLD': finger_tracker = cv2.TrackerTLD_create() phone_tracker = cv2.TrackerTLD_create() if tracker_type == 'MEDIANFLOW': finger_tracker = cv2.TrackerMedianFlow_create() phone_tracker = cv2.TrackerMedianFlow_create() if tracker_type == 'GOTURN': finger_tracker = cv2.TrackerGOTURN_create() phone_tracker = cv2.TrackerGOTURN_create() if tracker_type == 'MOSSE': finger_tracker = cv2.TrackerMOSSE_create() phone_tracker = cv2.TrackerMOSSE_create() if tracker_type == "CSRT": finger_tracker = cv2.TrackerCSRT_create() phone_tracker = cv2.TrackerCSRT_create() finger_ok = finger_tracker.init(frame, finger_box) phone_ok = phone_tracker.init(frame, phone_box) l = [] while True: # Read a new frame ok, frame = video.read() if not ok: break
def __process_feature_detect_then_track(self, video, video_file_or_camera): """Read video frame by frame and collect changes to the ROI. After sufficient frames have been collected, analyse the results""" tracking = False self.__start_capture(video) if self.config["headless"] is False: self.hr_charts = HRCharts(self.logger) for tracker in self.tracker_list: self.hr_charts.add_chart(tracker.name, sub_charts_rows=2, sub_charts_columns=2) while video.is_opened(): ret, frame = video.read_frame() if ret: self.total_frames_read += 1 if self.config["headless"] is False: # If the original frame is not writable and we wish to modify the frame. E.g.change the ROI to Pk-Pk self.last_frame = frame.copy() else: self.last_frame = frame self.frame_number += 1 if not tracking: found, x, y, w, h = self.roi_selector.select_roi( self.last_frame) if found: self.logger.info("Tracking after face detect") for tracker in self.tracker_list: tracker.initialize(x, y, w, h, self.last_frame) cv2.rectangle(self.last_frame, (x, y), (x + w, y + h), (255, 0, 0), 1) track_box = (x, y, w, h) self.logger.info("Using {} tracker".format( self.config["opencv_tracker"])) if self.config["opencv_tracker"] == "CSRT": self.tracker = cv2.TrackerCSRT_create() elif self.config["opencv_tracker"] == "MOSSE": self.tracker = cv2.TrackerMOSSE_create() else: self.tracker = cv2.TrackerKCF_create() # self.tracker = cv2.TrackerCSRT_create() # self.tracker = cv2.TrackerKCF_create() # self.tracker = cv2.TrackerMOSSE_create() self.tracker.init(self.last_frame, track_box) tracking = True else: self.__start_capture(video) else: # Update tracker ok, bbox = self.tracker.update(self.last_frame) if ok: x = int(bbox[0]) y = int(bbox[1]) w = int(bbox[2]) h = int(bbox[3]) for tracker in self.tracker_list: tracker.update(x, y, w, h, self.last_frame) if self.config["headless"] is False: cv2.rectangle(self.last_frame, (x, y), (x + w, y + h), (225, 0, 0), 1) else: self.logger.warning("Tracker failed") self.__start_capture(video) tracking = False if self.config["headless"] is False: pulse_rate_pk_pk = self.pulse_rate_bpm_pk_pk if isinstance( self.pulse_rate_bpm_pk_pk, str) else round( self.pulse_rate_bpm_pk_pk, 2) pulse_rate_fft = self.pulse_rate_bpm_fft if isinstance( self.pulse_rate_bpm_fft, str) else round( self.pulse_rate_bpm_fft, 2) cv2.putText( self.last_frame, "HR: PkPk:{} - FFT:{} Frame: {}. Total Frames: {}". format(pulse_rate_pk_pk, pulse_rate_fft, self.frame_number, self.total_frames_read), (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2) cv2.imshow('Frame', self.last_frame) if self.frame_number > self.config["pulse_sample_frames"]: fps = video.get_actual_fps( ) if video_file_or_camera == 0 else video.get_frame_rate() self.__update_results(fps, video) self.logger.info( "Processing time: {} seconds. FPS: {}. Frame count: {}" .format( round(time.time() - self.start_process_time, 2), round( self.frame_number / (time.time() - self.start_process_time), 2), self.frame_number)) if self.config["pause_between_samples"] and self.config[ "headless"] is False: input("Hit enter to continue") self.__start_capture(video) tracking = False if self.config["headless"] is False: # Allow UI key = cv2.waitKey(10) & 0xFF if key == ord('q'): break # Exit elif key == ord('g'): self.config["show_pulse_charts"] = True if self.config[ "show_pulse_charts"] is False else True else: self.logger.info("Video stream ended") break return
video = args["image"] tracker_type = args["tracker"] cap = cv2.VideoCapture(video) ret, frame = cap.read() initBB = cv2.selectROI("Object to track", frame) cv2.destroyWindow("Object to track") OPENCV_OBJECT_TRACKERS = { "csrt": cv2.TrackerCSRT_create(), "kcf": cv2.TrackerKCF_create(), "boosting": cv2.TrackerBoosting_create(), "mil": cv2.TrackerMIL_create(), "tld": cv2.TrackerTLD_create(), "medianflow": cv2.TrackerMedianFlow_create(), "mosse": cv2.TrackerMOSSE_create() } if tracker_type: tracker = OPENCV_OBJECT_TRACKERS[tracker_type] else: tracker = cv2.TrackerKCF_create() tracker.init(frame, initBB) while cap.isOpened(): ret, frame = cap.read() if not ret: break (success, box) = tracker.update(frame)
def thermal_tracker(mat_3d, quantization_method='optimal', im_tracker_name = 'TLD', print_mode=False, nonfixed_ROI_size=True, img_viewer=True, min_T=28, max_T=38): if im_tracker_name == 'TLD': im_tracker = cv2.TrackerTLD_create() elif im_tracker_name == 'MEDIANFLOW': im_tracker = cv2.TrackerMedianFlow_create() elif im_tracker_name == 'GOTURN': im_tracker = cv2.TrackerGOTURN_create() elif im_tracker_name == 'MOSSE': im_tracker = cv2.TrackerMOSSE_create() elif im_tracker_name == "CSRT": im_tracker = cv2.TrackerCSRT_create() elif im_tracker_name == 'BOOSTING': im_tracker = cv2.TrackerBoosting_create() elif im_tracker_name == 'MIL': im_tracker = cv2.TrackerMIL_create() elif im_tracker_name == 'KCF': im_tracker = cv2.TrackerKCF_create() temp_mat = copy.deepcopy(mat_3d[:,:,0]) tracked_imgs = np.zeros(mat_3d.shape,np.uint8) if quantization_method == 'optimal': tracked_imgs[:,:,0] = optimal_quantization(temp_mat, print_mode) elif quantization_method == 'non-optimal': tracked_imgs[:,:,0] = nonoptimal_quantization(temp_mat, min_T, max_T, print_mode) # ROI selection mROI = np.zeros((4, mat_3d.shape[2])) mROI[:,0] = cv2.selectROI('ROI selection', tracked_imgs[:,:,0], False) # mROI.shape cv2.destroyWindow('ROI selection') # cv2.namedWindow('TrackViewer') # Initialization of a tracker out = im_tracker.init(tracked_imgs[:,:,0], tuple(mROI[:,0])) for i in range(1,mat_3d.shape[2]): if np.mod(i,20)==0: print("frame: %d"%i) temp_mat = copy.deepcopy(mat_3d[:,:,i]) if quantization_method == 'optimal': current_frame = optimal_quantization(temp_mat, print_mode) elif quantization_method == 'non-optimal': current_frame = nonoptimal_quantization(temp_mat, min_T, max_T, print_mode) # Updating the tracker if nonfixed_ROI_size: result, mROI[:,i] = im_tracker.update(current_frame) else: result, bbox = im_tracker.update(current_frame) mROI[2,i]=mROI[2,0] mROI[3,i]=mROI[3,0] mROI[0,i]=bbox[0]+np.round(bbox[2]/2) -np.round(mROI[2,0]/2) mROI[1,i]=bbox[1]+np.round(bbox[3]/2) -np.round(mROI[3,0]/2) # Visualise the tracked ROI if result==True: point1 = (int(mROI[0,i]), int(mROI[1,i])) point2 = (int(mROI[0,i] + mROI[2,i]), int(mROI[1,i] + mROI[3,i])) cv2.rectangle(current_frame, point1, point2, (255,255,0), 3, 0) tracked_imgs[:,:,i]=current_frame else : print("tracking error occurred at %d frame"%(i)) tracked_imgs[:,:,i]=current_frame if img_viewer: cv2.imshow('TrackViewer',current_frame) # Exit if ESC pressed k = cv2.waitKey(1) & 0xff if k == 27 : break # cv2.namedWindow('TrackViewer') # count=0 # while True: # cv2.imshow('TrackViewer',tracked_imgs[:,:,count]) # if count<tracked_imgs.shape[2]-1: # count+=1 # # Press ESC to exit # k = cv2.waitKey(1) & 0xff # if k == 27 : break # cv2.destroyAllWindows() return mROI, tracked_imgs
def object_track(): # Set up tracker. # Instead of MIL, you can also use tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT'] tracker_type = tracker_types[1] if int(minor_ver) < 3: tracker = cv2.Tracker_create(tracker_type) else: if tracker_type == 'BOOSTING': tracker = cv2.TrackerBoosting_create() if tracker_type == 'MIL': tracker = cv2.TrackerMIL_create() if tracker_type == 'KCF': tracker = cv2.TrackerKCF_create() if tracker_type == 'TLD': tracker = cv2.TrackerTLD_create() if tracker_type == 'MEDIANFLOW': tracker = cv2.TrackerMedianFlow_create() if tracker_type == 'GOTURN': tracker = cv2.TrackerGOTURN_create() if tracker_type == 'MOSSE': tracker = cv2.TrackerMOSSE_create() if tracker_type == "CSRT": tracker = cv2.TrackerCSRT_create() # Read video video = cv2.VideoCapture(0) # Exit if video not opened. if not video.isOpened(): print("Could not open video") sys.exit() # Read first frame. ok, frame = video.read() if not ok: print('Cannot read video file') sys.exit() # Define an initial bounding box bbox = (0,0,500,500) # Uncomment the line below to select a different bounding box # bbox = cv2.selectROI(frame, False) cv2.destroyAllWindows() print(bbox) # Initialize tracker with first frame and bounding box frames_required = 10 p1 = (int(bbox[0]), int(bbox[1])) p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3])) # while(True): # k = cv2.waitKey(30) # if (k==32): # break k =0 key_p = 0 while (True): k = cv2.waitKey(30) ret, frame = video.read() if k==32: key_p = k # break if ret: # # hand initialization # cv2.imshow() if (key_p==32): # print(p1,p2) fcopy = frame[p1[1]:p2[1],p1[0]:p2[0]].copy() mask,im_masked = detect_skin(fcopy) # print(p1[1],p1[1],p2[0],p2[0]) # print(im_masked.shape) frame[p1[1]:p2[1],p1[0]:p2[0]] = im_masked cv2.imshow('H',frame) if (np.average(mask.reshape(mask.shape[0]*mask.shape[1])) > 255*0.3): if (frames_required > 0): frames_required -=1 if (frames_required == 5): # frame = im_masked frame[p1[1]:p2[1],p1[0]:p2[0]] = im_masked break # cv2.imshow('masked_box',im_masked) continue else: frames_required = 10 continue cv2.rectangle(frame, p1, p2, (255,0,0), 2, 1) # frame = cv2.flip(frame,1) cv2.imshow("Initialize Tracking", cv2.flip(frame,1)) # break # k = cv2.waitKey(30) # if (key_p==32): # break # cv2.destroyAllWindows() cv2.imshow('Hello1',frame) ok = tracker.init(frame, bbox) while True: # Read a new frame ok, frame = video.read() if not ok: break # Start timer timer = cv2.getTickCount() # Update tracker ok, bbox = tracker.update(frame) # Calculate Frames per second (FPS) fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer) # Draw bounding box if ok: # Tracking success 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 : # Tracking failure cv2.putText(frame, "Tracking failure detected", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2) # Display tracker type on frame cv2.putText(frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2) # Display FPS on frame cv2.putText(frame, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2) # Display result # frame = cv2.flip(frame,1) cv2.imshow("Tracking", frame) # Exit if ESC pressed k = cv2.waitKey(30) & 0xff if k == 27 : break
def _get_tracker(self): return cv2.TrackerMOSSE_create()
def tracking(): arduino.write(struct.pack('>B', 0)) global inFrame global personDetected global lost_frames global trackFail trackFail = False tracker_types = [ 'BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT' ] tracker_type = tracker_types[2] if int(minor_ver) < 3: tracker = cv2.Tracker_create(tracker_type) else: if tracker_type == 'BOOSTING': tracker = cv2.TrackerBoosting_create() if tracker_type == 'MIL': tracker40 = cv2.TrackerMIL_create() if tracker_type == 'KCF': tracker = cv2.TrackerKCF_create() if tracker_type == 'TLD': tracker = cv2.TrackerTLD_create() if tracker_type == 'MEDIANFLOW': tracker = cv2.TrackerMedianFlow_create() if tracker_type == 'GOTURN': tracker = cv2.TrackerGOTURN_create() if tracker_type == 'MOSSE': tracker = cv2.TrackerMOSSE_create() if tracker_type == "CSRT": tracker = cv2.TrackerCSRT_create() # Define an initial bounding box #bbox = (287, 23, 86, 320) # Uncomment the line below to select a different bounding box # Initialize tracker with first frame and bounding box #ok = tracker.init(frame, bbox) # Why do we have this twice? #ok, frame = video.read() while True: ok, frame = video.read() while inFrame and personDetected and not SLEEP: #last_coord = 300, 300 tracker.clear() tracker = cv2.TrackerKCF_create() bbox, name = find_face() #print name + " was detected" if name != followName: #lost_frames = lost_frames + 1 #if not lost_frames<3: if trackFail: inFrame = False else: personDetected = False continue trackFail = False #ask if you are okay because the face cannot be found #print bbox[2] ok = tracker.init(frame, bbox) n = 0 #lost_frames = 0; while n < 100 and not SLEEP: # Read a new frame ok, frame = video.read() #if not ok: # break # Start timertracker = cv2.TrackerKCF_create() timer = cv2.getTickCount() # Update tracker ok, bbox = tracker.update(frame) x = bbox[0] + bbox[2] / 2 y = bbox[1] + bbox[3] / 2 #print x, y # Calculate Frames per second (FPS) fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer) # Draw bounding box if ok: #last_coord = x, y # Tracking success #found = True inFrame = True #lost_frames = 0 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) x_center = 320 y_center = 160 x_window = 65 y_window = 20 #print x, y if not (x_center - x_window) <= x <= ( x_center + x_window ): #Make horizontal displacement prioritized if x < (x_center - x_window): # face is left arduino.write(struct.pack('>B', 2)) elif x > (x_center + x_window): # face is right arduino.write(struct.pack('>B', 3)) else: # face is straight arduino.write(struct.pack('>B', 0)) else: if y < (y_center - y_window): # face is too close arduino.write(struct.pack('>B', 4)) elif y > (y_center + y_window): # face is too far arduino.write(struct.pack('>B', 1)) else: # face is centered arduino.write(struct.pack('>B', 0)) else: # Tracking failure #lost_frames = lost_frames + 1 cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) arduino.write(struct.pack('>B', 0)) n = 100 trackFail = True # Display result cv2.imshow("Tracking", frame) # Exit if ESC pressed k = cv2.waitKey(1) & 0xff if k == 27: break n = n + 1
def detection_video(path,weight):#识别的video global image_size,tracker_rgb,init_num flag = 0 net = build_ssd('test', 300, num_classes) net.eval() net.load_weights(weight)#导入模型参数 cap = cv2.VideoCapture(path) fps = cap.get(cv2.CAP_PROP_FPS) size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) init_num=0 t4=time.time() while cap.isOpened(): ret,image = cap.read() if init_num==0:#初始化程序 flag += 1 if ret == False: print("video is over!") break if flag % 3 != 0:#每三帧处理一次,为了防止jetson nano速率不够 continue t0 = time.time() rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) resize_image = cv2.resize(image, (300, 300)).astype(np.float32) resize_image -= (104, 117, 123)#对SSD实现均值化 resize_image = resize_image.astype(np.float32)#转为float32 resize_image = resize_image[:, :, ::-1].copy() torch_image = torch.from_numpy(resize_image).permute(2, 0, 1)#重新排列传入torch input_image = Variable(torch_image.unsqueeze(0))#扩展第一列 if torch.cuda.is_available(): input_image = input_image.cuda()#设置为CUDA形式 out = net(input_image)#传入到模型当中 colors = cfg.COLORS detections = out.data scale = torch.Tensor(rgb_image.shape[1::-1]).repeat(2)#[ 起始下标 : 终止下标 : 间隔距离 ] rgb_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)#转化为BGR参数 idx_obj = -1#初始为-1 center_point=[0,0] gallery_best_draw=[0,0,0,0] for i in range(detections.size(1)):#获取所有的参数 j = 0#都要循环类的次数 #print(detections.size()) #print(i) if detections[0,i,j,0] >= 0.95:#设定阈值 idx_obj += 1#物体数+1 score = detections[0,i,j,0]#计算得分 label_name = labels[i-1]#得到名称 display_txt = '%s %.2f'%(label_name, score)#显示目标物体位置 pt = (detections[0,i,j,1:]*scale).cpu().numpy()#获取四个点位置 #j += 1 # 求得四个边角,并防止溢出 pt[0] = max(pt[0],0) pt[1] = max(pt[1],0) pt[2] = min(pt[2],size[1]) pt[3] = min(pt[3],size[0]) #print(pt[0],pt[3]) if abs(pt[2]-pt[0])*abs(pt[3]-pt[1])>500: print((pt[2]-pt[0])*(pt[3]-pt[1])) if (pt[0]+pt[2])/2>100 and (pt[1]+pt[3])/2>140 and (pt[0]+pt[1]+pt[2]+pt[3])/2>(center_point[0]+center_point[1]):#处理一帧中的最优点 center_point=[(pt[0]+pt[2])/2,(pt[1]+pt[3])/2]#更新最优点 gallery_best_draw=[pt[0],pt[1],pt[2],pt[3]] #init_num=1 #print(pt[0],pt[1],pt[2],pt[3]) #print(center_point) else: print("error",(pt[2]-pt[0])*(pt[3]-pt[1])) continue color = colors[idx_obj%len(colors)]#选择颜色 textsize = cv2.getTextSize(display_txt, cv2.FONT_HERSHEY_COMPLEX, 1, 2)[0]#显示文本文字 text_x = int(pt[0])#文本位置 text_y = int(pt[1]) cv2.rectangle(rgb_image,(int(pt[0]), int(pt[1])),(int(pt[2]), int(pt[3])),color,4)#框选位置 cv2.putText(rgb_image, display_txt, (text_x + 4, text_y), cv2.FONT_HERSHEY_COMPLEX, 1,(255 - color[0], 255 - color[1], 255 - color[2]), 2)#输出结果 if gallery_best_draw[0]!=0: track_roi=(gallery_best_draw[0],gallery_best_draw[1],abs(gallery_best_draw[2]-gallery_best_draw[0]),abs(gallery_best_draw[3]-gallery_best_draw[1])) print("track_roi:",track_roi) try: tracker_rgb=cv2.TrackerMOSSE_create()#重置 tracker_rgb.init(rgb_image, track_roi)#初始化对应的参数 except: pass #t1 = time.time() #cv2.putText(rgb_image, "FPS: %.2f" % (1 / (t1 - t0)), (5, 30), cv2.FONT_HERSHEY_COMPLEX, 1.2, (255, 255, 255), 2) #cv2.imshow("result",rgb_image) elif init_num==1: t0 = time.time() images = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) images = cv2.cvtColor(images, cv2.COLOR_RGB2BGR)#转化为BGR参数 rgb_image=images.copy() (success, box) = tracker_rgb.update(rgb_image) if time.time()-t4>10: init_num=0 t4=time.time() #print(time.time()-t4) if success: (x, y, w, h) = [int(v) for v in box] csrt_best_draw=[int(x),int(y),int(x+w),int(y+h)] cv2.rectangle(rgb_image,tuple(csrt_best_draw),color,4)#框选位置 t1 = time.time() cv2.putText(rgb_image, "FPS: %.2f" % (1 / (t1 - t0)), (5, 30), cv2.FONT_HERSHEY_COMPLEX, 1.2, (255, 255, 255), 2) cv2.imshow("result",rgb_image) if cv2.waitKey(1) & 0xFF == ord('q'): cap.release() cv2.destroyAllWindows()
def object_detection(self, frame, frameCenter): self.model.setInput( cv2.dnn.blobFromImage(frame, size=(300, 300), swapRB=True)) output = self.model.forward() frame_width, frame_height = 300, 300 # tracking tracker = cv2.TrackerMOSSE_create() if self.objBBox is not None: # new bounding box, returns 0 if it fails (success, box) = tracker.update(frame) print(box) print("Tracking Status:", success) if success: (x, y, w, h) = [int(v) for v in box] cv2.rectangle(frame, (x, y), (x + w, y + w), (0, 255, 0), 2) info = [ ("Tracker", "MOSSE"), ("Success", "Yes" if success else "No"), ] H = 150 W = 150 # loop over the info tuples and draw them on frame for (i, (k, v)) in enumerate(info): text = "{}: {}".format(k, v) cv2.putText(frame, text, (10, H - ((i * 20) + 20)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) if success: objX = x + (w // 2) objY = y + (h // 2) objCenter = (objX, objY) objBBox = (box_x, box_y, box_width, box_height) self.objBBox = objBBox return (objCenter, objBBox) # If no object tracking try detection for detection in output[0, 0, :, :]: confidence = detection[2] if confidence > .5: class_id = detection[1] class_name = self.id_class_name(class_id, self.classNames) print( str( str(class_id) + " " + str(detection[2]) + " " + class_name)) if 'person' in class_name: box_x = detection[3] * frame_width box_y = detection[4] * frame_height box_width = detection[5] * frame_width box_height = detection[6] * frame_height cv2.rectangle(frame, (int(box_x), int(box_y)), (int(box_width), int(box_height)), (23, 230, 210), thickness=1) cv2.putText(frame, class_name, (int(box_x), int(box_y + .05 * frame_height)), cv2.FONT_HERSHEY_SIMPLEX, (.005 * frame_width), (0, 0, 255)) objX = box_x + (box_width // 2) objY = box_y + (box_height // 2) objCenter = (objX, objY) objBBox = (box_x, box_y, box_width, box_height) self.objBBox = objBBox # Initialize the tracker tracker.init(frame, objBBox) print("objCenter:", objCenter) return (objCenter, objBBox) return (frameCenter, None)
def tracking_detect(frame): global curr_trackers,car_number,obj_cnt,frame_count boxes = [] imH = 500 imW = 700 x_point_1 = int(imW / 2) y_point_1 = imH x_point_2 = imW y_point_2 = int(imH / 2) distance_1_2 = math.sqrt((x_point_2 - x_point_1) ** 2 + (y_point_2 - y_point_1) ** 2) laser_line = imH - 100 laser_line_color = (0, 0, 255) old_trackers = curr_trackers curr_trackers = [] # duyệt qua các tracker cũ for car in old_trackers: tracker = car['tracker'] (_, box) = tracker.update(frame) boxes.append(box) new_obj = dict() new_obj['tracker_id'] = car['tracker_id'] new_obj['tracker'] = tracker # tính toán tâm đối tượng x, y, w, h, center_X, center_Y = model.get_box_info(box) # Ve hinh chu nhat quanh doi tuong cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) # Ve hinh tron tai tam doi tuong cv2.circle(frame, (center_X, center_Y), 4, (0, 255, 0), -1) if area_to_point(center_X,center_Y)!=area(): # Neu vuot qua thi khong track nua ma dem xe laser_line_color = (0, 255, 255) car_number += 1 else: # Con khong thi track tiep curr_trackers.append(new_obj) # Thuc hien object detection moi 5 frame if frame_count % 5 == 0: # Detect doi tuong boxes_d, classed = model.get_object(frame, detect_fn) for box in boxes_d: old_obj = False xd, yd, wd, hd, center_Xd, center_Yd = get_box_info(box) if st == 1: if area_to_point(center_Xd,center_Yd)==area(): # Duyet qua cac box, neu sai lech giua doi tuong detect voi doi tuong da track ko qua max_distance thi coi nhu 1 doi tuong if not model.is_old(center_Xd, center_Yd, boxes): cv2.rectangle(frame, (xd, yd), ((xd + wd), (yd + hd)), (0, 255, 255), 2) # Tao doi tuong tracker moi tracker = cv2.TrackerMOSSE_create() obj_cnt += 1 new_obj = dict() tracker.init(frame, tuple(box)) new_obj['tracker_id'] = obj_cnt new_obj['tracker'] = tracker curr_trackers.append(new_obj) # Tang frame frame_count += 1 return frame
def trakcing(self, video_stream): """:param video_stream :return video frame """ tracker_types = [ 'BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT' ] tracker_type = tracker_types[2] tracker = None if int(minor_ver) < 3: pass # tracker = cv2.Tracker_create(tracker_type) else: if tracker_type == 'BOOSTING': tracker = cv2.TrackerBoosting_create() if tracker_type == 'MIL': tracker = cv2.TrackerMIL_create() if tracker_type == 'KCF': tracker = cv2.TrackerKCF_create() if tracker_type == 'TLD': tracker = cv2.TrackerTLD_create() if tracker_type == 'MEDIANFLOW': tracker = cv2.TrackerMedianFlow_create() if tracker_type == 'GOTURN': tracker = cv2.TrackerGOTURN_create() if tracker_type == 'MOSSE': tracker = cv2.TrackerMOSSE_create() if tracker_type == "CSRT": tracker = cv2.TrackerCSRT_create() # Read video if video_stream is "": video_stream = 0 # 读取摄像头 video = cv2.VideoCapture(video_stream) if not video.isOpened(): print("Could not open video") sys.exit() # Read first frame. ok, next_frame = video.read() if not ok: print('Cannot read video file') sys.exit() # frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Define an initial bounding box bbox = (287, 23, 86, 320) # Uncomment the line below to select a different bounding box bbox = cv2.selectROI(next_frame, False) # Initialize tracker with first frame and bounding box ok = tracker.init(next_frame, bbox) while True: # Read a new frame ok, frame = video.read() if not ok: break # Start timer timer = cv2.getTickCount() # Update tracker ok, bbox = tracker.update(frame) # Calculate Frames per second (FPS) fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer) # Draw bounding box if ok: # Tracking success 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: # Tracking failure cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) # Display tracker type on frame cv2.putText(frame, tracker_type + " Tracker", (100, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2) # Display FPS on frame cv2.putText(frame, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2) # Display result cv2.imshow("Tracking", frame) # Exit if ESC pressed if cv2.waitKey(1) & 0xFF == ord('q'): break
"1. CSRT 2. KCF\n3. TLD 4. MIL\n5. MOSSE 6. Boosting\n7. MedianFlow\n>> " ) try: print("\n追蹤算法 :", method[number]) name = input("影片名稱 : ") filetype = input("影片格式 : ") ''' if elif else 單行寫法 True1 if Condition1 else True2 if Condition2 else True3 ... if Condition#n-1 else True#n ''' tracker = cv2.TrackerCSRT_create() if int( number) == 1 else cv2.TrackerKCF_create() if int( number) == 2 else cv2.TrackerTLD_create() if int( number) == 3 else cv2.TrackerMIL_create( ) if int(number) == 4 else cv2.TrackerMOSSE_create( ) if int(number) == 5 else cv2.TrackerBoosting_create( ) if int(number) == 6 else cv2.TrackerMedianFlow_create() #print(type(tracker)) #tracker = cv2.TrackerCSRT_create() #tracker = cv2.TrackerKCF_create() #tracker = cv2.TrackerTLD_create() #tracker = cv2.TrackerMIL_create() #tracker = cv2.TrackerMOSSE_create() #tracker = cv2.TrackerBoosting_create() #tracker = cv2.TrackerMedianFlow_create() try: #cap = cv2.VideoCapture('C:/Users/Relieak/Desktop/{}.{}'.format(name,filetype)) cap = cv2.VideoCapture('{}.{}'.format(name, filetype))
def main(): tracker_types = [ 'BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT' ] tracker_type = tracker_types[2] if int(minor_ver) < 3: tracker = cv2.Tracker_create(tracker_type) else: if tracker_type == 'BOOSTING': tracker = cv2.TrackerBoosting_create() if tracker_type == 'MIL': tracker = cv2.TrackerMIL_create() if tracker_type == 'KCF': tracker = cv2.TrackerKCF_create() if tracker_type == 'TLD': tracker = cv2.TrackerTLD_create() if tracker_type == 'MEDIANFLOW': tracker = cv2.TrackerMedianFlow_create() if tracker_type == 'GOTURN': tracker = cv2.TrackerGOTURN_create() if tracker_type == 'MOSSE': tracker = cv2.TrackerMOSSE_create() if tracker_type == "CSRT": tracker = cv2.TrackerCSRT_create() x0 = 200 y0 = 200 w0 = 224 h0 = 224 track_window = (x0, y0, w0, h0) # Reference Distance L0 = 100 S0 = 50176 #224x224 #take#here. # Base Distance LB = 100 # Define an initial bounding box bbox = (x0, y0, w0, h0) #(287, 23, 86, 320) #CX=int(bbox[0]+0.5*bbox[2]+3) #adding #CY=int(bbox[1]+0.5*bbox[3]+3) #adding drone = tellopy.Tello() try: drone.connect() drone.wait_for_connection(60.0) retry = 3 container = None while container is None and 0 < retry: retry -= 1 try: container = av.open(drone.get_video_stream()) except av.AVError as ave: print(ave) print('retry...') drone.takeoff() # skip first 300 frames frame_skip = 300 while True: #------------------------------------------for start for frame in container.decode(video=0): if 0 < frame_skip: frame_skip = frame_skip - 1 continue start_time = time.time() image = cv2.cvtColor(numpy.array(frame.to_image()), cv2.COLOR_RGB2BGR) # Start timer timer = cv2.getTickCount() #cv2.imshow('Canny', cv2.Canny(image, 100, 200)) #cv2.waitKey(1) # Update tracker ok, bbox = tracker.update(image) # Calculate Frames per second (FPS) fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer) # Draw bounding box if ok: #print('Tracking ok') (x, y, w, h) = (int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])) CX = int(bbox[0] + 0.5 * bbox[2]) #Center of X CY = int(bbox[1] + 0.5 * bbox[3]) S0 = bbox[2] * bbox[3] print("CX,CY,S0,x,y=", CX, CY, S0, x, y) # Tracking success p1 = (x, y) p2 = (x + w, y + h) cv2.rectangle(image, p1, p2, (255, 0, 0), 2, 1) p10 = (x0, y0) p20 = (x0 + w0, y0 + h0) cv2.rectangle(image, p10, p20, (0, 255, 0), 2, 1) d = round(L0 * m.sqrt(S0 / (w * h))) dx = x + w / 2 - CX0 #no change dx dy = y + h / 2 - CY0 print(d, dx, dy) tracking(drone, d, dx, dy, LB) else: # Tracking failure #print('Tracking failure') cv2.putText(image, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) cv2.imshow('Original', image) key = cv2.waitKey(1) & 0xff if key == ord('q'): print('Q!') break if key == ord('r'): roi_time = time.time() bbox = cv2.selectROI(image, False) print(bbox) (x0, y0, w0, h0) = (int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])) CX0 = int(x0 + 0.5 * w0) #Center of X CY0 = int(y0 + 0.5 * h0) # Initialize tracker with first frame and bounding box ok = tracker.init(image, bbox) ''' if frame.time_base < 1.0/60: time_base = 1.0/60 else: time_base = frame.time_base frame_skip2 = int((time.time() - roi_time)/time_base) if 0 < frame_skip2: frame_skip2 = frame_skip2 - 1 continue ''' if frame.time_base < 1.0 / 60: time_base = 1.0 / 60 else: time_base = frame.time_base frame_skip = int((time.time() - start_time) / time_base) #-------------------------------------------------for end break print('stop fly') except Exception as ex: exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback) print(ex) finally: drone.quit() drone.land() cv2.destroyAllWindows()
def main(): parser = argparse.ArgumentParser() parser.add_argument("--video", type=str, help="Path to video") args = parser.parse_args() # Baseline # tracker = cv2.TrackerMIL_create() # Faster FPS lower accuracy # tracker = cv2.TrackerKCF_create() # High accuracy slower FPS # tracker = cv2.TrackerCSRT_create() # Very very fast tracker = cv2.TrackerMOSSE_create() init_bbox = None video = cv2.VideoCapture(args.video) fps = None while True: success, frame = video.read() if not success: break # Resize frame frame = imutils.resize(frame, width=800) height, width = frame.shape[:2] # If bbox if defined if init_bbox: # Track object success, bbox = tracker.update(frame) if success: x, y, w, h = [int(x) for x in bbox] cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.putText(frame, f"Success: {success}", (10, height - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 0), 2) fps.update() fps.stop() cv2.putText(frame, f"FPS: {fps.fps():.4f}", (10, height - 40), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 0), 2) # Show frame cv2.imshow("Frame", frame) key = cv2.waitKey(2) & 0xFF # Press 's' key to select bbox, then 'Enter' to confirm. Use 'Esc' to reselect if key == ord("s"): init_bbox = cv2.selectROI("Frame", frame, fromCenter=False, showCrosshair=True) # Initialize tracker tracker.init(frame, init_bbox) fps = FPS().start() elif key == ord("q"): break video.release() cv2.destroyAllWindows()
def streamBegin(): frame_read = me.get_frame_read() myFrame = frame_read.frame tracking = False while True: global bbox global trackerCreated global recognisedFace global success global counter frame_read = me.get_frame_read() myFrame = frame_read.frame gray = cv2.cvtColor(myFrame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.1, 4) me.left_right_velocity = 0 me.for_back_velocity = 0 me.up_down_velocity = 0 me.yaw_velocity = 0 # check if its the time for the tracker # tracker = False is equivalent to tracker takes place of detection if followmode: if timing == False: # check if the tracker was already created if trackerCreated == False: tracker = cv2.TrackerMOSSE_create() frame_read = me.get_frame_read() myFrame = frame_read.frame success = True bbox = recognisedFace try: tracker.init(frame_read.frame, bbox) except: pass trackerCreated = True print(bbox) # check if the tracker was already createdq if followmode: if trackerCreated: success, bbox = tracker.update(myFrame) if videoRecording: print("recording") writer.write(myFrame) if timing: distance_x = int(recognisedFace[0] + (recognisedFace[2] / 2)) - int(width / 2) distance_y = int(recognisedFace[1] + (recognisedFace[3] / 2)) - int(height / 2) trackerCreated = False if followmode: followedFace = [(0, 0, 0, 0)] facesTuples = list(map(tuple, faces)) if len(facesTuples) > 0: nearestFace = facesTuples[0] else: nearestFace = (0, 0, 0, 0) for (x, y, w, h) in facesTuples: dist = distance(bbox, x, y, w, h) print(dist) if distance(nearestFace, bbox[0], bbox[1], bbox[2], bbox[3]) > dist: nearestFace = (x, y, w, h) followedFace = [nearestFace] for (x, y, w, h) in followedFace: if w > 30: # middle of person middle_x = (x + (w / 2)) middle_y = (y + (h / 2)) cv2.rectangle(myFrame, (x, y), (x + w, y + h), (255, 0, 0), 3, 1) cv2.putText(myFrame, "Recognition", (175, 75), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 255, 0), 2) recognisedFace = (x, y, w, h) if middle_x < 400: #print("Go left") dir = 1 elif middle_x > 500: #print("Go right") dir = 2 elif middle_y < 190: #print("Go up") dir = 3 elif middle_y > 210: #print("Go Down") dir = 4 elif w > 270: dir = 5 elif w < 250: dir = 6 else: dir = 0 if dir == 1: me.left_right_velocity = 0 me.for_back_velocity = 0 me.up_down_velocity = 0 me.yaw_velocity = pixelsToCm(distance_x) elif dir == 2: me.left_right_velocity = 0 me.for_back_velocity = 0 me.up_down_velocity = 0 me.yaw_velocity = pixelsToCm(distance_x) elif dir == 3: me.left_right_velocity = 0 me.for_back_velocity = 0 me.up_down_velocity = pixelsToCm(distance_y) me.yaw_velocity = 0 elif dir == 4: me.left_right_velocity = 0 me.for_back_velocity = 0 me.up_down_velocity = pixelsToCm(distance_y) me.yaw_velocity = 0 elif dir == 0: me.left_right_velocity = 0 me.for_back_velocity = 0 me.up_down_velocity = 0 me.yaw_velocity = 0 elif dir == 5: me.left_right_velocity = 0 me.for_back_velocity = 30 me.up_down_velocity = 0 me.yaw_velocity = 0 elif dir == 6: me.left_right_velocity = 0 me.for_back_velocity = -30 me.up_down_velocity = 0 me.yaw_velocity = 0 reactThread = threading.Thread( target=react, args=(me.left_right_velocity, me.for_back_velocity, me.up_down_velocity, me.yaw_velocity)) if me.send_rc_control: reactThread.start() else: distance_x = int(bbox[0] + (bbox[2] / 2)) - int(width / 2) distance_y = int(bbox[1] + (bbox[3] / 2)) - int(height / 2) if followmode: if "success" in globals(): x, y, w, h = int(bbox[0]), int(bbox[1]), int(bbox[2]), int( bbox[3]) cv2.rectangle(myFrame, (x, y), ((x + w), (y + h)), (255, 0, 255), 3, 1) cv2.putText(myFrame, "Tracking", (75, 75), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 255, 0), 2) if checkLossMethodLaunched == False: checkLossWithArg = partial(checkLoss, bbox) checkLossThread = threading.Thread( target=checkLossWithArg) checkLossThread.start() else: cv2.putText(myFrame, "Lost", (75, 75), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 0, 255), 2) if followmode: if w > 30: # middle of person middle_x = (x + (w / 2)) middle_y = (y + (h / 2)) if middle_x < 400: #print("Go left") dir = 1 elif middle_x > 500: #print("Go right") dir = 2 elif middle_y < 190: #print("Go up") dir = 3 elif middle_y > 210: #print("Go Down") dir = 4 elif w > 270: dir = 5 elif w < 250: dir = 6 else: dir = 0 if dir == 1: me.left_right_velocity = 0 me.for_back_velocity = 0 me.up_down_velocity = 0 me.yaw_velocity = pixelsToCm(distance_x) elif dir == 2: me.left_right_velocity = 0 me.for_back_velocity = 0 me.up_down_velocity = 0 me.yaw_velocity = pixelsToCm(distance_x) elif dir == 3: me.left_right_velocity = 0 me.for_back_velocity = 0 me.up_down_velocity = pixelsToCm(distance_y) me.yaw_velocity = 0 elif dir == 4: me.left_right_velocity = 0 me.for_back_velocity = 0 me.up_down_velocity = pixelsToCm(distance_y) me.yaw_velocity = 0 elif dir == 0: me.left_right_velocity = 0 me.for_back_velocity = 0 me.up_down_velocity = 0 me.yaw_velocity = 0 elif dir == 5: me.left_right_velocity = 0 me.for_back_velocity = 30 me.up_down_velocity = 0 me.yaw_velocity = 0 elif dir == 6: me.left_right_velocity = 0 me.for_back_velocity = -30 me.up_down_velocity = 0 me.yaw_velocity = 0 reactThread = threading.Thread( target=react, args=(me.left_right_velocity, me.for_back_velocity, me.up_down_velocity, me.yaw_velocity)) if me.send_rc_control: reactThread.start() cv2.imshow("Result", myFrame) if cv2.waitKey(1) & 0xFF == ord('q'): me.land() break frame_read.release() cv2.destroyAllWindows()
def object_track(): # Set up tracker. # Instead of MIL, you can also use tracker_types = [ 'BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT' ] tracker_type = tracker_types[0] if int(minor_ver) < 3: tracker = cv2.Tracker_create(tracker_type) else: if tracker_type == 'BOOSTING': tracker = cv2.TrackerBoosting_create() if tracker_type == 'MIL': tracker = cv2.TrackerMIL_create() if tracker_type == 'KCF': tracker = cv2.TrackerKCF_create() if tracker_type == 'TLD': tracker = cv2.TrackerTLD_create() if tracker_type == 'MEDIANFLOW': tracker = cv2.TrackerMedianFlow_create() if tracker_type == 'GOTURN': tracker = cv2.TrackerGOTURN_create() if tracker_type == 'MOSSE': tracker = cv2.TrackerMOSSE_create() if tracker_type == "CSRT": tracker = cv2.TrackerCSRT_create() # Read video video = cv2.VideoCapture(0) # Exit if video not opened. if not video.isOpened(): print "Could not open video" sys.exit() # Read first frame. ok, frame = video.read() if not ok: print 'Cannot read video file' sys.exit() # Define an initial bounding box bbox = (287, 23, 86, 320) # Uncomment the line below to select a different bounding box bbox = cv2.selectROI(frame, False) cv2.destroyAllWindows() # Initialize tracker with first frame and bounding box while (True): ret, frame = video.read() if ret: k = cv2.waitKey(30) 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) frame = cv2.flip(frame, 1) cv2.imshow("Initialize Tracking", frame) if k == 32: break cv2.destroyAllWindows() ok = tracker.init(frame, bbox) while True: # Read a new frame ok, frame = video.read() if not ok: break # Start timer timer = cv2.getTickCount() # Update tracker ok, bbox = tracker.update(frame) # Calculate Frames per second (FPS) fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer) # Draw bounding box if ok: # Tracking success 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: # Tracking failure cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) # Display tracker type on frame cv2.putText(frame, tracker_type + " Tracker", (100, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2) # Display FPS on frame cv2.putText(frame, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2) # Display result frame = cv2.flip(frame, 1) cv2.imshow("Tracking", frame) # Exit if ESC pressed k = cv2.waitKey(30) & 0xff if k == 27: break
USE_LIVE_CAM = True if USE_LIVE_CAM: cap = cv2.VideoCapture(cv2.CAP_DSHOW + 1) cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1600) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1200) else: # Open sample video cap = cv2.VideoCapture('test_files/table3_sample.wmv') cv2.namedWindow('Tracking', cv2.WINDOW_NORMAL) cv2.resizeWindow('Tracking', 1200, 600) # Create tracker tracker = cv2.TrackerMOSSE_create( ) # super fast and full occlusion recovery (less accurate?) # tracker = cv2.TrackerKCF_create() # slower but more accurate # Create output video # fourcc = cv2.VideoWriter_fourcc(*'XVID') # out = cv2.VideoWriter('output3.avi',fourcc, 20.0, (1600,1200)) # Read first frame ret, frame = cap.read() # Manual bounding box selection bbox = cv2.selectROI('Tracking', frame, False, False) # Initialise tracker ret = tracker.init(frame, bbox)
def Analise(result, ui=False, vid_folder="/content/TRAIN_0"): data = { 'ATA': 0.0, 'F': 0.0, 'F1': 0.0, 'OTP': 0.0, 'OTA': 0.0, 'Deviation': 0.0, 'PBM': 0.0, 'FPS': 0.0, 'Ms': 0, 'fp': 0, 'tp': 0, 'fn': 0, 'g': 0 } result[1] = data tracker_type = result[0] result = result[1:] seq_ID = result[1]["my_object"]["video"] del result[1]["my_object"]["video"] frames_folder = os.path.join(vid_folder, "frames", seq_ID) anno_file = os.path.join(vid_folder, "anno", seq_ID + ".txt") anno = np.loadtxt(anno_file, delimiter=",") frames_list = [ frame for frame in os.listdir(frames_folder) if frame.endswith(".jpg") ] if not len(anno) == len(frames_list): print("Not the same number of frames and annotation!") return # Define an initial bounding box bbox = (anno[0][0], anno[0][1], anno[0][2], anno[0][3]) frame = cv2.imread(os.path.join(frames_folder, "0.jpg")) if tracker_type == "MOSSE": tracker = cv2.TrackerMOSSE_create() elif tracker_type == "TLD": tracker = cv2.TrackerTLD_create() elif tracker_type == "GOTURN": tracker = cv2.TrackerGOTURN_create() elif tracker_type == "BOOSTING": tracker = cv2.TrackerBoosting_create() elif tracker_type == "MIL": tracker = cv2.TrackerMIL_create() elif tracker_type == "KCF": tracker = cv2.TrackerKCF_create() elif tracker_type == "MEDIANFLOW": tracker = cv2.TrackerMedianFlow_create() elif tracker_type == "CSRT": tracker = cv2.TrackerCSRT_create() file_path = rndStr() + ".json" file1 = open(file_path, 'w') file1.writelines(json.dumps(result[1])) file1.close() fs = cv2.FileStorage(file_path, cv2.FILE_STORAGE_READ) tracker.read(fs.getFirstTopLevelNode()) os.remove(file_path) ok = tracker.init(frame, bbox) if not ok: print("Initialisation error") continue data["Ms"] += 1 data["tp"] += 1 data["ATA"] += IOU(bbox, bbox) data["Deviation"] += NCDist(bbox, bbox) data["F1"] += F1(bbox, bbox) data["PBM"] += 1 - L1(bbox, bbox) for i in range(1, len(frames_list)): frame_file = str(i) + ".jpg" imgs_file = os.path.join(frames_folder, frame_file) frame = cv2.imread(imgs_file) anno_bbox = (anno[i][0], anno[i][1], anno[i][2], anno[i][3]) x_min = int(anno[i][0]) x_max = int(anno[i][2] + anno[i][0]) y_min = int(anno[i][1]) y_max = int(anno[i][3] + anno[i][1]) cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0, 0, 255), 2, 1) # Start timer timer = cv2.getTickCount() # Update tracker ok, bbox = tracker.update(frame) # Calculate Frames per second (FPS) fpS = cv2.getTickFrequency() / (cv2.getTickCount() - timer) # Draw bounding box if ok: # Tracking success p1 = (int(bbox[0]), int(bbox[1])) p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3])) if ui: cv2.rectangle(frame, p1, p2, (255, 0, 0), 2, 1) else: # Tracking failure # print("Tracking failure detected") if True: data["fn"] += 1 if ui: cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) # Display tracker type on frame if ui: cv2.putText(frame, tracker_type + " Tracker", (100, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2) # Display FPS on frame cv2.putText(frame, "FPS : " + str(int(fpS)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2) # Display result cv2.imshow("Tracking", frame) iou = IOU(bbox, anno_bbox) data["ATA"] += iou data["FPS"] += fpS data["F1"] += F1(bbox, anno_bbox) data["PBM"] += 1 - (L1(bbox, anno_bbox) if iou > 0 else Th( bbox, anno_bbox)) / Th(bbox, anno_bbox) if True: data["g"] += 1 if iou >= 0.5: data["tp"] += 1 elif iou > 0: data["OTP"] += iou data["Deviation"] += NCDist(bbox, anno_bbox) data["Ms"] += 1 else: data["fp"] += 1 # Exit if ESC pressed if ui: k = cv2.waitKey(1) & 0xff if k == 27: sys.exit() # print(tracker_type, i) data["F"] = F(data["tp"][j], data["fp"][j], data["fn"][j]) data["F1"] /= len(frames_list) data["OTA"] = 1 - (data["fp"][j] + data["fn"][j]) / data["g"][j] data["OTP"] /= data["Ms"][j] data["ATA"] /= len(frames_list) data["FPS"] /= len(frames_list) data["Deviation"] = 1 - data["Deviation"][j] / data["Ms"][j] if data["Deviation"] < -1: data["Deviation"] = -1 data["PBM"] /= len(frames_list) result[0].update(result[1]["my_object"]) result = result[0]
print('Failed to read video') exit() img_h, img_w, c = image_pre.shape image = cv2.resize(image_pre, (int(img_w / 2), int(img_h / 2))) # 初始化num_obj个目标 num_obj = 20 bbox_list = [] tracker_list = [] for i in range(0, num_obj): bbox_tmp = cv2.selectROI('tracking', image) bbox_list.append(bbox_tmp) tracker_tmp = cv2.TrackerMOSSE_create() ok_tmp = tracker_tmp.init(image, bbox_tmp) tracker_list.append(tracker_tmp) # 循环处理 while camera.isOpened(): ok_cam, image_pre = camera.read() if not ok_cam: print('no image to read') break img_h, img_w, c = image_pre.shape image = cv2.resize(image_pre, (int(img_w / 2), int(img_h / 2)))
def tracking(video): """ Executes the tracking of the cable MOSSE tracker from OpenCV is used to locate the pantograph """ # Initialize the figure for graph plotting fig = plt.figure() ax = fig.add_subplot(1, 1, 1) xs = [] ys = [] # Set up the tracker tracker = cv2.TrackerMOSSE_create() try: # Stops program if there is any problem with the video if not video.isOpened(): sys.exit() # Reads the first frame so we can create the bounding box ok, frame = video.read() if not ok: sys.exit() # selectROI lets us select an area in the first frame that is the # region of interest bounding_box = cv2.selectROI("Frame", frame, fromCenter=False, showCrosshair=True) # Initialize the MOSSE tracker tracker.init(frame, bounding_box) frame_number = 0 # Run loop as long as there are more frames in the video while True: ok, frame = video.read() if not ok: break # Update the tracker and the bounding box for each frame ok, bounding_box = tracker.update(frame) if ok: # Crop the frame above the bounding box, find the contour cropped = crop(frame, bounding_box) contour = find_contour(cropped) transposed_contour = transpose(contour, bounding_box) # Find the intersection point and draw it on the frame intersection_point = (contour[1][0][0], contour[1][0][1]) cv2.drawContours(frame, [transposed_contour], -1, (0, 255, 0), thickness=3) cv2.circle(frame, intersection_point, 8, (128, 0, 255), -1) cv2.imshow('frame', frame) # Set up plot to call animate() function for every fifth point frame_number += 1 if frame_number % 5 == 0: point = contour[1][0][0] animate(frame_number, xs, ys, point, fig, ax) # Quit when no more frames or on pressed 'q' if cv2.waitKey(25) & 0xFF == ord('q'): break # Clean up video.release() cv2.destroyAllWindows() # Exit if there is an error with leading the video except BaseException: sys.exit()
if tracker_type == 'BOOSTING': tracker = cv2.TrackerBoosting_create() if tracker_type == 'MIL': tracker = cv2.TrackerMIL_create() if tracker_type == 'KCF': tracker = cv2.TrackerKCF_create() if tracker_type == 'TLD': tracker = cv2.TrackerTLD_create() if tracker_type == 'MEDIANFLOW': tracker = cv2.TrackerMedianFlow_create() if tracker_type == "CSRT": tracker = cv2.TrackerCSRT_create() if tracker_type == "MOSSE": tracker = cv2.TrackerMOSSE_create() # Read video video = cv2.VideoCapture(0) # Exit if video not opened. if not video.isOpened(): print("Could not open video") sys.exit() # Read first frame. ok, frame = video.read() if not ok: print('Cannot read video file') sys.exit() # Define an initial bounding box
def detectFrame(self): try: self.idsR =[0] self.idsL =[0] self.Lcorners,self.idsL,_ = cv2.aruco.detectMarkers(self.frameLtemp.copy() ,aruco_dict) self.Rcorners,self.idsR,_ = cv2.aruco.detectMarkers(self.frameRtemp.copy() ,aruco_dict) if self.idsL is None or self.idsR is None: self.idsR =[0] self.idsL =[0] except: pass if self.Currentid in self.idsR and self.Currentid in self.idsL: self.frameL = self.frameLtemp.copy() self.frameR = self.frameRtemp.copy() #unpack the corners for ptsr,ptsl in zip(([self.Rcorners[list(self.idsR).index(self.Currentid)]]),([self.Lcorners[list(self.idsL).index(self.Currentid)]])): pass self.xcorr0= (ptsr[:,0])[:,0] self.ycorr0= (ptsr[:,0])[:,1] self.xcorr2 = (ptsr[:,2])[:,0] self.ycorr2= (ptsr[:,2])[:,1] self.xcorl0= (ptsl[:,0])[:,0] self.ycorl0= (ptsl[:,0])[:,1] self.xcorl2= (ptsl[:,2])[:,0] self.ycorl2= (ptsl[:,2])[:,1] # give four corners of the object and get a bounding box cordinate (x_tl, y_tl, w, h) [xl, yl, wl, hl] = cv2.boundingRect(np.array([[self.xcorl0,self.ycorl0],[self.xcorl2,self.ycorl2],[(ptsl[:,1])[:,0] ,(ptsl[:,1])[:,1]], [(ptsl[:,3])[:,0] ,(ptsl[:,3])[:,1]] ])) [xr, yr, wr, hr] = cv2.boundingRect(np.array([[self.xcorr0,self.ycorr0],[self.xcorr2,self.ycorr2],[(ptsr[:,1])[:,0] ,(ptsr[:,1])[:,1]], [(ptsr[:,3])[:,0] ,(ptsr[:,3])[:,1]] ])) # Define an initial bounding box self.BBMFL = (xl, yl, wl, hl) # (x_tl, y_tl, w, h) self.BBMFR = (xr, yr, wr, hr) # (x_tl, y_tl, w, h) # Initialize tracker with first frame and bounding box self.selectedTracker = self.trackerType[self.tractID] if self.selectedTracker == 'MOSSE': self.trackerL = cv2.TrackerMOSSE_create() self.trackerR = cv2.TrackerMOSSE_create() if self.selectedTracker == "CSRT": self.trackerL = cv2.TrackerCSRT_create() self.trackerR = cv2.TrackerCSRT_create() if self.selectedTracker == 'BOOSTING': self.trackerL = cv2.TrackerBoosting_create() self.trackerR = cv2.TrackerBoosting_create() if self.selectedTracker == 'MIL': self.trackerL = cv2.TrackerMIL_create() self.trackerR = cv2.TrackerMIL_create() if self.selectedTracker == 'KCF': self.trackerL = cv2.TrackerKCF_create() self.trackerR = cv2.TrackerKCF_create() if self.selectedTracker == 'TLD': self.trackerL = cv2.TrackerTLD_create() self.trackerR = cv2.TrackerTLD_create() if self.selectedTracker == 'MEDIANFLOW': self.trackerL = cv2.TrackerMedianFlow_create() self.trackerR = cv2.TrackerMedianFlow_create() if self.selectedTracker == 'GOTURN': self.trackerL = cv2.TrackerGOTURN_create() self.trackerR = cv2.TrackerGOTURN_create() # size of tracking frame realtive to the main frame self.TFMFL = (self.BBMFL[0] - self.expand,self.BBMFL[1] - self.expand,self.BBMFL[2] + self.expand*2,self.BBMFL[3] + self.expand*2 ) self.TFMFR = (self.BBMFR[0] - self.expand,self.BBMFR[1] - self.expand,self.BBMFR[2] + self.expand*2,self.BBMFR[3] + self.expand*2 ) #Crop image to tracking frame only self.frameL = self.frameL[int(self.TFMFL[1]):int(self.TFMFL[1] + self.TFMFL[3]), int(self.TFMFL[0]):int(self.TFMFL[0] + self.TFMFL[2])] self.frameR = self.frameR[int(self.TFMFR[1]):int(self.TFMFR[1] + self.TFMFR[3]), int(self.TFMFR[0]):int(self.TFMFR[0] + self.TFMFR[2])] # BBTFL size bounding box relative to Tracking frame self.BBTFL = (self.BBMFL[0]-self.TFMFL[0],self.BBMFL[1]-self.TFMFL[1],self.BBMFL[2],self.BBMFL[3]) self.BBTFR = (self.BBMFR[0]-self.TFMFR[0],self.BBMFR[1]-self.TFMFR[1],self.BBMFR[2],self.BBMFR[3]) self.okL = self.trackerL.init(self.frameL, self.BBTFL) self.okR = self.trackerR.init(self.frameR, self.BBTFR) self.exitloop = True self.found = True