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("los_angeles.mp4") # Exit if video not opened. if not video.isOpened(): print("Could not open video") sys.exit() # Read first frame. ok, frame = video.read()
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): speed = 100 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 goturnTracker(video): tracker = cv2.TrackerGOTURN_create() excute(video, tracker)
def __init__(self): self.tracker = cv2.TrackerGOTURN_create() self.init_flag = False # False, the tracker was not initialized. self.check_model()
def __init__(self): self.tracker = cv2.TrackerGOTURN_create()
def test_createTracker(self): t = cv.TrackerMIL_create() try: t = cv.TrackerGOTURN_create() except cv.error as e: pass # may fail due to missing DL model files
def detect_moving_target(old_gray, new_gray): # open the camera for warming up cap = cv2.VideoCapture('slow.MOV') # grab the first frame old_frame = get_frame_from(cap) old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY) # initiate KCF tracker tracker = cv2.TrackerKCF_create() # possible movements ROI # start the tracking while True: # Record FPS timer = cv2.getTickCount() # read the current frame cur_frame = get_frame_from(cap) # covert to gray scale cur_gray = cv2.cvtColor(cur_frame, cv2.COLOR_BGR2GRAY) # Calculate Frames per second (FPS) fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer) # Display FPS on frame cv2.putText(cur_frame, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2); # display cur frame cv2.imshow('video', cur_frame) # waitkey if cv2.waitKey(1) == ord('q'): break #(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.') if __name__ == '__main__': # Set up tracker. # Instead of MIL, you can also use tracker_types = ['BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'GOTURN'] 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() # Read video video = cv2.VideoCapture('slow.MOV') # Exit if video not opened. if not video.isOpened(): print "Could not open video" sys.exit() # Read first frame. ok, frame = video.read() frame = cv2.resize(frame, dsize=(0, 0), fx=0.5, fy=0.5) 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) # Initialize tracker with first frame and bounding box ok = tracker.init(frame, bbox) while True: # Read a new frame ok, frame = video.read() frame = cv2.resize(frame, dsize=(0, 0), fx=0.5, fy=0.5) 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 k = cv2.waitKey(1) & 0xff if k == 27: break
# if the class label is not a person, ignore it if CLASSES[idx] != "person": continue # compute the (x, y)-coordinates of the bounding box box = detections[0, 0, i, 3:7] * np.array([W, H, W, H]) (startX, startY, endX, endY) = box.astype("int") # save detection as image # print(startX, startY, endX, endY) roi = frame[startY:endY, startX:endX] cv2.imwrite("roi" + str(numberOfDetection) + ".jpg", roi) numberOfDetection += 1 # inicialize GOTURN tracker goturn = cv2.TrackerGOTURN_create() goturn.init(frame, (startX, startY, endX, endY)) # add the tracker to list of trackers trackers.append(goturn) # otherwise, run tracking algorithm else: # loop over the trackers for tracker in trackers: # set the status status = "Tracking" # update the tracker and grab the updated position (success, box) = goturn.update(frame)
def main(): """ LoadsMulti-Level Otsu masks and tracks the face. Returns a set of ROIs containing only the face. :return: """ tag = 'MLO_' filepath = 'E:\\GitHub\\CovPySourceFile\\MultiLevelOtsu\\' otsu_masks = load_images_from_folder( folder=filepath, name_tag=tag, ) # region MIL Tracking # use binary otsu mask to detect the face (major_ver, minor_ver, subminor_ver) = cv2.__version__.split('.') # Set up tracker tracker_types = [ 'BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT' ] tracker_type = tracker_types[4] 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() initial_frame = otsu_masks[0] # Define initial bounding box from roi bbox = cv2.selectROI(initial_frame, showCrosshair=True, fromCenter=False) # Initialize tracker with first frame and bounding box ok = tracker.init(initial_frame, bbox) # roi points points = [] failed_idx = [] for n, mask in enumerate(otsu_masks): # Update tracker ok, bbox = tracker.update(mask) # Draw bounding box if ok: # Tracking success p1 = [int(bbox[0]), int(bbox[1])] p2 = [int(bbox[0] + bbox[2]), int(bbox[1])] p3 = [int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3])] p4 = [int(bbox[0]), int(bbox[1] + bbox[3])] points.append([p1, p2, p3, p4]) cv2.rectangle(mask, tuple(p1), tuple(p3), (255, 0, 0), 2, 1) else: # Tracking failure cv2.putText(mask, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) failed_idx.append(n) # Display result cv2.imshow("Tracking", mask) # Exit if ESC pressed k = cv2.waitKey(1) & 0xff if k == 27: break cv2.destroyAllWindows() # endregion # get rois rois = [] for n, rp in enumerate(points): img = Image.open( r"E:\GitHub\CovPySourceFile\Normalized\NF_{}.png".format(n)) left = rp[0][0] top = rp[0][1] right = rp[2][0] bottom = rp[2][1] cropped = img.crop((left, top, right, bottom)) rois.append(cropped) # plt.imshow(cropped) # plt.show() destination_dir = 'E:\\GitHub\\CovPySourceFile\\FaceROI\\' if not os.path.exists(destination_dir): os.makedirs(destination_dir) for n, r in enumerate(rois): r.save(destination_dir + 'FR_{}.png'.format(n))
def main(): dataset, timestamps = load_thermal_file( _filename='ThermalData_18_06_2020_13_19_36.h5', _folder='E:\\GitHub\\CovPySourceFile') # region Control Variables is_writing = False is_drawing = False # endregion # region Data Pre-Processing # region Timestamps to Sampling Rate # # convert timestamps into datetime objects # dt_obj = [datetime.fromtimestamp(ts / 1000).time() for ts in timestamps] # # convert datetime objects into time strings # time_strings = [dt.strftime("%M:%S:%f") for dt in dt_obj] # # finally convert time strings into seconds # timestamp_in_seconds = [] # for s in time_strings: # date_time = datetime.strptime(s, "%M:%S:%f") # a_timedelta = date_time - datetime(1900, 1, 1) # in_seconds = a_timedelta.total_seconds() # timestamp_in_seconds.append(in_seconds) # # # calculate the mean interval between samples from seconds # ts_mean = np.mean(np.diff(timestamp_in_seconds)) # # finally calculate the mean sampling rate of the signal # fs = int(1 / ts_mean) # endregion # region Get Raw Thermal Data # get data set attributes n_frames, height, width, total_time_ms = [ dataset.attrs[i] for i in list(dataset.attrs) ] # extract thermal frames from the hdf5 dataset thermal_frames = [] # convert raw data into temperature values [deg Celsius] # temp_frames = [] # normalize raw data for further processing steps [0 - 255] norm_frames = [] for n in range(0, n_frames): raw_frame = load_frame_from_dataset(dataset, height, n) thermal_frames.append(raw_frame) # temp_frames.append(raw_frame * 0.1 - 273.15) norm_frames.append( cv2.normalize(raw_frame, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)) # get unsharpened img for edge detection later on unsharp_frames = [] # for n, n_frame in enumerate(norm_frames): # u_frame = unsharp_mask(image=n_frame, radius=3, amount=2) # unsharp_frames.append(u_frame) # # if is_writing: # cv2.imwrite('E:\\GitHub\\CovPySourceFile\\UnsharpenedMask\\UM_{}.png'.format(n), u_frame) # # if is_drawing: # fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(10, 3.5)) # # # Plotting the original image. # ax[0].imshow(norm_frames[n]) # ax[0].set_title('Thermal Data - Normalized') # # # ax[1].imshow(temp_frames[n]) # # ax[1].set_title('Temp Frame [C]') # # ax[1].imshow(unsharp_frames[n]) # ax[1].set_title('Unsharpened Image') # # # ax[1].imshow(norm_frames[n]) # # ax[1].set_title('Thermal Data - Normalized [0-255]') # # plt.subplots_adjust() # plt.show() # # if is_drawing: # plt.close('all') # endregion # endregion # region Feature Extraction Algorithm # region Automatic ROI Detection # face segmentation using multi-level Otsu otsu_masks = multi_level_otsu(images=norm_frames, n_regions=4, target_region=3, method=OtsuMethods.BINARY, write=is_writing, draw=is_drawing) # to proceed the masks need to be converted into 3d array empty_array = np.zeros((height, width)) _3d_otsu_masks = [ np.dstack((mask, empty_array, empty_array)) for mask in otsu_masks ] # use binary otsu mask to detect the face (major_ver, minor_ver, subminor_ver) = cv2.__version__.split('.') # Set up tracker tracker_types = [ 'BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT' ] tracker_type = tracker_types[4] 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() # video = cv2.VideoCapture('E:\\GitHub\\CovPySourceFile\\Video\\OtsuMask.avi') # # # Exit if video not opened. # if not video.isOpened(): # print("Could not open video file!") # sys.exit() # # # Read first frame # ok, frame = video.read() # if not ok: # print("Could not read video file!") # sys.exit() tracked_frame = _3d_otsu_masks[0] # Define initial bounding box from roi bbox = cv2.selectROI(tracked_frame, showCrosshair=True, fromCenter=False) # Initialize tracker with first frame and bounding box ok = tracker.init(tracked_frame, bbox) # roi points roi_points = [] tracked_frames = [] # while True: # # Read a new frame # ok, frame = video.read() # if not ok: # break for mask in _3d_otsu_masks: tracked_frame = mask # Start timer timer = cv2.getTickCount() # Update tracker ok, bbox = tracker.update(tracked_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]) p3 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3])) p4 = (int(bbox[0]), int(bbox[1] + bbox[3])) cv2.rectangle(tracked_frame, p1, p3, (255, 0, 0), 2, 1) points = [p1, p2, p3, p4] # roi_values = get_values_from_roi(points, t_frame) roi_points.append(points) else: # Tracking failure cv2.putText(tracked_frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) roi_points.append([]) # Display tracker type on frame cv2.putText(tracked_frame, tracker_type + " Tracker", (100, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2) # Display FPS on frame cv2.putText(tracked_frame, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2) tracked_frames.append(tracked_frame) # Display result cv2.imshow("Tracking", tracked_frame) # Exit if ESC pressed k = cv2.waitKey(1) & 0xff if k == 27: break is_writing = True if is_writing: for n, img in enumerate(tracked_frames): cv2.imwrite( 'E:\\GitHub\\CovPySourceFile\\TrackedFrames\\TF_{}.png'.format( n), img) norm_face_rois = [] for n in range(0, len(roi_points)): # get values inside of roi norm_roi_values = get_values_from_roi(roi_points[n], norm_frames[n]) # my_roi = np.zeros((roi_shapes[n][2], roi_shapes[n][3])) x1 = roi_points[n][0][0] x2 = roi_points[n][2][0] y1 = roi_points[n][0][1] y2 = roi_points[n][2][1] norm_face_roi = norm_roi_values[y1:y2, x1:x2] if is_drawing: cv2.imshow("ROI", norm_face_roi) # Exit if ESC pressed k = cv2.waitKey(1) & 0xff if k == 27: break norm_face_rois.append(norm_face_roi) if is_writing: for n, img in enumerate(tracked_frames): cv2.imwrite( 'E:\\GitHub\\CovPySourceFile\\FaceROI\\TF_{}.png'.format(n), img) # endregion # endregion print('Bye Bye')
def run_concurrent(self, args, sign_progress): annotation_id = args[0] bbox = tuple(args[1]) movie_path = args[2] fps = args[5] start_frame = ms_to_frames(args[3], fps) end_frame = ms_to_frames(args[4], fps) method = args[6] resolution = args[7] keys = [] # TRACKING if method == 'BOOSTING': tracker = cv2.TrackerBoosting_create() elif method == 'MIL': tracker = cv2.TrackerMIL_create() elif method == 'KCF': tracker = cv2.TrackerKCF_create() elif method == 'TLD': tracker = cv2.TrackerTLD_create() elif method == 'MEDIANFLOW': tracker = cv2.TrackerMedianFlow_create() elif method == 'GOTURN': tracker = cv2.TrackerGOTURN_create() else: raise Exception("Tracking Method not identifiable. " + str(method)) # Read video capture = cv2.VideoCapture(movie_path) # Exit if video not opened. if not capture.isOpened(): raise RuntimeError("Tracking: Could not open video.") # Read first frame. capture.set(cv2.CAP_PROP_POS_FRAMES, start_frame) ok, frame = capture.read() if not ok: raise RuntimeError("Tracking: Could not read Frame.") # Initialize tracker with first frame and bounding box ok = tracker.init(frame, bbox) for i in range(start_frame, end_frame, 1): sign_progress( round(float(i - start_frame) / (end_frame - start_frame), 2)) # Read a new frame ok, frame = capture.read() if not ok: break # Update tracker ok, bbox = tracker.update(frame) # Draw bounding box if ok: # Tracking success if i % resolution == 0: time = frame2ms(i, fps) pos = [bbox[0], bbox[1]] keys.append([time, pos]) 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) # cv2.imshow("Returned", frame) # cv2.waitKey(30) return [annotation_id, keys]
def single_tracking(filepath): # Set up tracker. # Instead of MIL, you can also use 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(filepath) # 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('tracking', frame) # Initialize tracker with first frame and bounding box t1 = time.clock() ok = tracker.init(frame, bbox) t = time.clock() - t1 print('cost time: ', t) 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 k = cv2.waitKey(1) & 0xff if k == 27: break
def track_ecoli(regions): tracker_types = ['BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'GOTURN'] # MIL appears to work best... 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() root_path = 'C:\\dev\\courses\\2.131 - Advanced Instrumentation\\' img_path = root_path + 'E.coli.tif' pil_img = Image.open(img_path) frame = np.array(pil_img) # frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) fourcc = cv2.VideoWriter_fourcc(*'DIVX') # out = cv2.VideoWriter(root_path + 'ecoli_track1.avi', # fourcc, 5.0,(640,480) ) # 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) csvfile = open(root_path + 'E.coli1.csv', 'w', newline='') bac_writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) bac_writer.writerow(['BacteriaId', 'Time', 'X', 'Y']) for i in range(149): # Read a new frame pil_img.seek(i) frame = np.array(pil_img) # 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) bac_writer.writerow( [1, i, int(bbox[0] + bbox[2] / 2), int(bbox[1] + bbox[3] / 2)]) 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, "2.131 E.Coli 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) frame = frame[:480, :640, ...] array_alpha = np.array([1.45]) array_beta = np.array([-150.0]) cv2.add(frame, array_beta, frame) # multiply every pixel value by alpha cv2.multiply(frame, array_alpha, frame) # out.write(frame) # out.write(frame) # Display result cv2.imshow("Tracking", frame) # Exit if ESC pressed k = cv2.waitKey(1) & 0xff if k == 27: break csvfile.close()
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() # 追跡する枠の座標とサイズ x = 200 y = 200 w = 224 h = 224 track_window=(x,y,w,h) # Reference Distance L0 = 100 #S0 = 50176 #224x224 # Base Distance LB = 100 # Define an initial bounding box bbox = (x, y, w, h) #(287, 23, 86, 320) drone = tellopy.Tello() drone.connect() container = av.open(drone.get_video_stream()) drone.takeoff() #drone.is_autopilot="True" drone.is_autopilot="False" while True: for frame in container.decode(video=0): image = cv2.cvtColor(numpy.array(frame.to_image()), cv2.COLOR_RGB2BGR) # Start timer timer = cv2.getTickCount() # Update tracker ok, bbox = tracker.update(image) # Calculate Frames per second (FPS) fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer); # Draw bounding box if 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]) CY=int(bbox[1]+0.5*bbox[3]) S0=bbox[2]*bbox[3] print("CX,CY,S0=",CX,CY,S0) # Tracking success p1 = (x, y) p2 = (x + w, y + h) cv2.rectangle(image, p1, p2, (255,0,0), 2, 1) else : # Tracking failure cv2.putText(image, "Tracking failure detected", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2) # Display tracker type on frame cv2.putText(image, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2) # Display FPS on frame cv2.putText(image, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2) cv2.imshow('test',image) key = cv2.waitKey(1)&0xff if key == ord('a'): drone.is_autopilot="True" elif key == ord('s'): drone.is_autopilot="False" if drone.is_autopilot=="True": d = round(L0 * m.sqrt(S0 / (w * h))) dx = x + w/2 - CX dy = y + h/2 - CY print(d,dx,dy,drone.is_autopilot,w,h) tracking(drone,d,dx,dy,LB) elif drone.is_autopilot=="False": key_Operation(drone,key) print("key=",key,ord('q')) #key = cv2.waitKey(1)&0xff print("key=",key,ord('q')) if key == ord('q'): cv2.destroyAllWindows() break elif key == ord('r'): bbox = cv2.selectROI(image, False) print(bbox) (x,y,w,h) = (int(bbox[0]),int(bbox[1]),int(bbox[2]),int(bbox[3])) # Initialize tracker with first frame and bounding box ok = tracker.init(image, bbox) break drone.down(50) sleep(5) drone.land() drone.subscribe(drone.EVENT_FLIGHT_DATA, handler) drone.quit()
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) # 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) # 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 k = cv2.waitKey(1) & 0xff if k == 27: break