class Client: def __init__(self, config: ConfigurationFile): self.config = config self.database = Database(self.config.db_file) self.organizer = Organizer(self.config, self.config.db_file) self.downloader = Downloader(self.config.db_file, self.organizer, self.config) self.tracker = Tracker(self.config.db_file, self.downloader, self.config.update_period) self.tracker.start() self.downloader.start() self.organizer.start() def add_tvshow(self, tvshow_id: int): tvshow_name = showrss.get_name(tvshow_id) self.database.put_tvshow(TVShow(tvshow_id, tvshow_name)) def remove_tvshow(self, tvshow_id: int): self.database.remove_tvshow(tvshow_id) def list_tvshows(self): return self.database.tvshows() def list_episodes(self, state: EpisodeState = None): return self.database.episodes(state) def download_progress(self): return self.downloader.downloads() def exit(self): self.tracker.stop() self.downloader.stop()
def main (): #Introduction print ("This program graphically depicts the flight of a cannonball. ") print () #Get inputs a = eval(input("Enter the launch angle in degrees: ")) v = eval(input("Enter the initial velocity in meters per second: ")) h = eval(input("Enter the initial height in meters: ")) #Create tracker projectile = Projectile(a, v, h) win = GraphWin(200, 200) win.setCoords(0.0, 0.0, 25.0, 25.0) tracker = Tracker(win, projectile) time = 0.0 while projectile.getY() >= -5: time += .0005 projectile.update(time) tracker.update() #Close window win.getMouse() win.close()
def compute_gradient((model, dialog, n_data, )): print 'here' return 1 tracker = Tracker(model) tracker.new_dialog() last_state = tracker.get_state() accum_loss_grad = [] for shape in model.shapes: accum_loss_grad.append(np.zeros(shape, dtype=theano.config.floatX)) total_loss = 0.0 for act in dialog: act_ndx = model.acts[act] # Run tracker to get the new state and the true state. curr_state, true_state = tracker.next(act) # Compute the loss & gradient of the loss. val = [model.values[true_state[slot]] for slot in model.slots] total_loss += model.f_curr_slot_loss(curr_state, val) for param_loss_grad, accum in zip(model.loss_grads, accum_loss_grad): accum += 1.0 / n_data * param_loss_grad(last_state, act_ndx, val) last_state = curr_state return accum_loss_grad, total_loss
class ObjectTracker(OpenCVVideoFilter): __gstmetadata__ = ( "Object tracker plugin", "newelement", "Description", "Contact") def __init__(self): super(ObjectTracker, self).__init__() self.tracker = Tracker([self.h, self.w]) self.api = GstVideo.VideoRegionOfInterestMeta.get_info() def do_transform(self, b, outbuffer): img = self.gst_to_cv(b) ts = time.time() self.tracker.track(ts, img) tag_list = Gst.TagList.new_empty() for t in self.tracker.to_log(): blob = t.bestblob() info = Gst.Structure.new_empty('roi') info.set_value('id', t.id) info.set_value('ts', ts) info.set_value('bbox', blob.bbox ) sample = Gst.Sample.new(b, None, None, info) logging.debug('new tag with object track info for track %s',str(t.id)) tag_list.add_value(Gst.TagMergeMode.APPEND, Gst.TAG_APPLICATION_DATA, sample) if tag_list.n_tags()>0: outbuffer.add_meta(self.api, tag_list) self.post_message(Gst.Message.new_tag(self, tag_list)) return Gst.FlowReturn.OK
def test_canConvertItemToType(self): tracker = Tracker() contents = mock() timezone = mock() item = tracker._convertToItem(testType, contents, timezone ) storedItem, storedTimezone = item.contains() self.assertEqual(storedItem, contents) self.assertEqual(storedTimezone, timezone)
def iterations_from_file(config, filename): tracker = Tracker(dbdir=config.tracker.get('local_store_directory')) f = open(filename) contents = '' for l in f: contents += l f.close() iterations = tracker.parse_stories(contents) return iterations
def beer_fitness(population): for p in population: #perform simulation points = [] for i in range(10): # print p.ann t = Tracker(p.ann) points.append(t.run()) p.fitness = sum(points)*1.0/len(points)*1.0
def main (): #Introduction print ("This program uses a cannonball to shoot at a target.") #Create graphics window win = GraphWin(200, 200) win.setCoords(0.0, 0.0, 25.0, 25.0) target = Target(win) flag = False targetHit = False while not flag: #Get inputs print () a = eval(input("Enter the launch angle in degrees: ")) v = eval(input("Enter the initial velocity in meters per second: ")) h = eval(input("Enter the initial height in meters: ")) #Create tracker projectile = Projectile(a, v, h) tracker = Tracker(win, projectile) time = 0.0 while projectile.getY() >= -5: time += .0005 projectile.update(time) tracker.update() #Calculate if cannonball hit target points = target.points() center = tracker.circ.getCenter() center_x = center.getX() center_y = center.getY() radius = tracker.circ.getRadius() for point in points: x = point.getX() y = point.getY() square_dist = (center_x-x) ** 2 + (center_y-y) ** 2 if square_dist <= radius ** 2: targetHit = True if targetHit: print ("\nYou hit the target!") flag = True else: flag = False print ("\nTry again!") #Close window win.getMouse() win.close()
def linear_regression(points): if Tracker.distance(points[0], meanPoint(points)) < 10: return [0, 1] num_pts = len(points) xc = points[:, [0]] yc = points[:, [1]] ones = np.ones((num_pts, 1)) X = np.concatenate((ones, xc), axis=1) X_intermediary = np.dot(X.T, X) Y_intermediary = np.dot(X.T, yc) R = np.dot(inv(X_intermediary), Y_intermediary) k = R[1][0] last_2 = points[-2:] first_x = last_2[0][0] second_x = last_2[1][0] if first_x < second_x: return [1, k] else: return [-1, -k]
def translate(self, readline, result=None, no_imports=None): # Tracker to keep track of information as the file is processed self.tokens = Tokens(self.default_kls) self.tracker = Tracker(result, self.tokens, self.wrapped_setup) # Add import stuff at the top of the file if self.import_tokens and no_imports is not True: self.tracker.add_tokens(self.import_tokens) # Looking at all the tokens with self.tracker.add_phase() as tracker: for tokenum, value, (_, scol), _, _ in generate_tokens(readline): self.tracker.next_token(tokenum, value, scol) # Add attributes to our Describes so that the plugin can handle some nesting issues # Where we have tests in upper level describes being run in lower level describes if self.with_describe_attrs: self.tracker.add_tokens(self.tracker.make_describe_attrs()) # If setups should be wrapped, then do this at the bottom if self.wrapped_setup: self.tracker.add_tokens(self.tracker.wrapped_setups()) # Add lines to bottom of file to add __testname__ attributes self.tracker.add_tokens(self.tracker.make_method_names()) # Return translated list of tokens return self.tracker.result
def __init__(self, window): self.gwin = window self.projectile = Projectile(0, 0, 10) self.tracker = Tracker(self.gwin, self.projectile) self.target = Target(self.gwin) self.hits = False
class Torrent_Downloader(): ''' Manages download logic: - Creation and removal of peers. - Book keeping of pieces downloaded and in progress. - Checking completed pieces and writing to file. ''' def __init__(self, torrent, start_listener_callback): self.torrent = torrent self.message_handler = MessageHandler(self.torrent, self) self.start_listener_callback = start_listener_callback self.ip = self.get_IP_address() self.tracker = Tracker(self.torrent.announce, self.torrent.get_params()) self.peers = self.create_peers() self.io_loop = get_event_loop() self.index = 0 self.callback_dict = { 'check_piece' : self.torrent.check_piece_callback, 'pieces_changed' : self.pieces_changed_callback, 'start_listener' : self.start_listener_callback, } self.pieces_needed = [] def get_IP_address(self): response = get('http://api.ipify.org?format=json') ip_object = loads(response.text) return ip_object["ip"] def create_peers(self): peers = [] for p in self.tracker.parse_peer_address(): if p[0] == self.ip: continue peers.append(Peer(p[0], p[1], self)) return peers def pieces_changed_callback(self, peer): ''' Check if connected peer has pieces I need. Send interested message. Call choose_piece. If peer has no pieces I need, disconnect and remove from peers list. ''' self.torrent.update_pieces_needed() for i in self.torrent.pieces_needed: if peer.has_pieces[i]: self.io_loop.create_task(self.message_handler.send_message(peer=peer, message_id=2)) self.choose_piece(peer=peer) break else: self.peers.remove(peer) def choose_piece(self, peer): ''' Finds the next needed piece, updates self.have and self.pieces_needed. calls construct_request_payload. ''' piece_index = self.torrent.pieces_needed[0] self.torrent.have[piece_index] = True self.torrent.update_pieces_needed() self.message_handler.construct_request_payload(peer=peer, piece_index=piece_index)
def main(): file_location, download_dir = get_args() # parse the torrent file and make into an object torrentfile = TorrentFile(file_location) # create bittorrent client (brain behind everything) client = BitTorrentClient(download_dir, torrentfile) client.create_files() # create tracker and add it to the client tracker = Tracker(client.peer_id, torrentfile) client.add_tracker(tracker) # connect to the peers associated to the tracker tracker.get_peers_and_connect(client, torrentfile) reactor.run()
def main(ball): c = Camera() color = ball log.info("Ball: " + color) t = BallTracker(color) ballpos = None while ballpos is None: frame = c.get_frame() ballpos = t.getBallCoordinates(frame) ballpos = Tracker.transformCoordstoDecartes(ballpos) previous_positions = np.array([ballpos]) k = 20 while True: frame = c.get_frame() ballpos = t.getBallCoordinates(frame) log.debug(ballpos) if ballpos is None: continue ballpos = Tracker.transformCoordstoDecartes(ballpos) previous_positions = np.append(previous_positions, [ballpos], axis=0) last_k_positions = previous_positions[-k:] direction_vector = linear_regression(last_k_positions) direction_vector = [20 * x for x in direction_vector] p1 = Tracker.transformCoordstoCV(round_point(ballpos)) p2 = Tracker.transformCoordstoCV(round_point(add_points( ballpos, direction_vector))) frame = cv2.circle(frame, p1, 20, (0, 0, 0), 2) cv2.line(frame, p1, p2, (0, 255, 122), 2) cv2.imshow('frame', frame) l = cv2.waitKey(5) & 0xFF if l == 27: break c.close() cv2.destroyAllWindows()
class Main: def __init__(self, trackerUrl, trackerPass): self.tracker = Tracker(trackerUrl, trackerPass) def packetHandler(self, aprsString): print 'APRS String: %s' % aprsString packet = APRSPacket() if packet.parse(aprsString): print '%s -> %s' % (packet.source, packet.dest) print 'Report type: %s' % packet.reportType if packet.hasLocation: print 'Time: %sZ' % packet.time print 'Coordinates: %f, %f, Altitude: %d ft' % (packet.latitude, packet.longitude, packet.altitude) print 'Course: %d, Speed: %d kn, Bearing: %d' % (packet.course, packet.speed, packet.bearing) print 'Comment: %s' % packet.comment print 'Uploading to tracker' self.tracker.track(packet) print ''
def main(): options, files = getopt.getopt(sys.argv[1:], 'ivt:', ['info', 'verbose', 'threads:']) for flag, value in options: if flag == '-v' or flag == '--verbose': configuration['verbose'] = True elif flag == '-t' or flag == '--threads': try: configuration['threads'] = int(value) except: usage() elif flag == '-i' or flag == '--info': configuration['info'] = True else: usage() if len(files) != 1: usage() try: torrent = Torrent(files[0]) except: print 'Impossible to read torrent file/magnet link:', files[0] exit(1) if configuration['info'] == True: torrent.show() exit(0) torrent.start() generate_clientid() tracker = Tracker(torrent.data['announce']) tracker.update(torrent) swarm = Swarm() swarm.update_peers(tracker) threads = configuration['threads']
def visualize(self, out_filename="out/training_bs.html", out_filename_pickle="out/training_bs.pickle"): # Do bootstrap for the confusion table. n_bs = 1 widgets = [progressbar.Percentage(), ' ', progressbar.Bar(), ' ', progressbar.ETA(), ' ', progressbar.AdaptiveETA()] bs_progress = progressbar.ProgressBar(widgets=widgets).start() cts = [] for bs_iter in bs_progress(range(n_bs)): n_dialogs = len(self.training_dialogs) dataset = self.training_dialogs tracker = Tracker(self.model, inv=False) tracker.simulate(dataset) cts.append(tracker.out_data['confusion_tables']) ct = bootstrap.from_all_confusion_tables(cts) context = {} context['tracker'] = tracker.out_data context['bootstrap_ct'] = ct context['mean_score'] = np.mean([ctt.mean_score for ctt in ct.values()]) context['model'] = self.model context['training_metrics'] = self.training_metrics context['training_data'] = self.training_dialogs env = Environment(loader=FileSystemLoader('tpl')) env.globals.update(zip=zip) tpl = env.get_template('training.html') with open(out_filename, "w") as f_out: f_out.write(tpl.render(**context)) with open(out_filename_pickle, "w") as f_out: info = { 'mean_score': float(context['mean_score']), 'losses': [float(x) for x in context['training_metrics']['losses']], #'simulation': context['tracker']['simulation'] } f_out.write(json.dumps(info))
def __init__(self, config: ConfigurationFile): self.config = config self.database = Database(self.config.db_file) self.organizer = Organizer(self.config, self.config.db_file) self.downloader = Downloader(self.config.db_file, self.organizer, self.config) self.tracker = Tracker(self.config.db_file, self.downloader, self.config.update_period) self.tracker.start() self.downloader.start() self.organizer.start()
def __init__(self, lines, line_size, verbose): self.statistics_tracker = Tracker() self.processors = 4 self.verbose = verbose shared_bus = Bus() # Register one cache for each processor self.caches = [] for cache_id in range(self.processors): cache = Cache(cache_id, lines, line_size, shared_bus, self.statistics_tracker, self.verbose) self.caches.append(cache)
def do_trackers(self, callback=None): assert(len(self.hash) == 20) # talk to trackers in attempt to get peers if not self.started(): raise StopIteration if not self.meta: raise StopIteration if 'announce-list' in self.meta: tier1 = self.meta['announce-list'][0] for url in tier1: tracker = Tracker.instantiate(url, self.hash) self.trackers[tracker.get_key()] = tracker if tracker.can_announce(): result = yield gen.Task( tracker.announce ) logging.info('%s tracker announce got result %s' % ([self.hash], result))
def __init__(self, torrent, start_listener_callback): self.torrent = torrent self.message_handler = MessageHandler(self.torrent, self) self.start_listener_callback = start_listener_callback self.ip = self.get_IP_address() self.tracker = Tracker(self.torrent.announce, self.torrent.get_params()) self.peers = self.create_peers() self.io_loop = get_event_loop() self.index = 0 self.callback_dict = { 'check_piece' : self.torrent.check_piece_callback, 'pieces_changed' : self.pieces_changed_callback, 'start_listener' : self.start_listener_callback, } self.pieces_needed = []
def __init__(self, **kwargs): """Initialize the keycode bindings and all controllers, create output dir.""" settings = { 'experimentName' : 'someNameHere', 'outputRootDir' : os.path.normpath(os.path.expanduser("~/kauferdata")), 'keyBindings' : { 'quit' : 'q', 'toggleStimulator' : 't', 'triggerStimulator' : 's' }, 'audio' : {}, 'gui' : {}, 'stimulator' : { 'activeProtocolName' : 'nucleusAccumbensExample' }, 'tracker' : {}, 'videoIn' : {}, 'videoOut' : {}, 'writer' : {}, } settings.update(kwargs) trialDir = "{0}_{1}_{2}".format(time.strftime("%y%m%d%H%M%S"), settings['experimentName'], settings['stimulator']['activeProtocolName']) settings['outputDataDir'] = os.path.join(settings['outputRootDir'], trialDir) self.keyBindings = settings['keyBindings'] self.keycodeBindings = {k: getattr(opencvgui.keycodes, v) for k,v in self.keyBindings.items() } os.makedirs(os.path.join(settings['outputDataDir'], 'audio')) self.writeExperimentData(settings) for x in ['audio', 'videoOut', 'writer']: kwargs[x] = {} if not kwargs.has_key(x) else kwargs[x] kwargs[x]['outputDataDir'] = settings['outputDataDir'] # gui and writer both need a reference to the app instance because they # draw on the state of so many other controllers self.audio = Audio(**settings['audio']) self.gui = Gui(self, **settings['gui']) self.stimulator = Stimulator(**settings['stimulator']) self.tracker = Tracker(**settings['tracker']) self.videoIn = VideoIn(**settings['videoIn']) self.videoOut = VideoOut(**settings['videoOut']) self.writer = Writer(self, **settings['writer'])
def __init__(self, torr_dir="c:/torrent", data_dir="c:/torrent/files", tracker_ip="10.0.0.1:8000", callback=lambda: ".", session_dir="c:/torrent", db_name="torrent.db", ext=".torrent"): """ Intialize class. Parameters torr_dir: Location for torrent meta data files data_dir: Location for data files tracker_ip: IP:PORT string of tracker callback: option function which will be called while attempting to seed torrents. torr_db: Name of anydbm database where we save what we're serving. """ self.callback = callback self.data_dir = data_dir self.torr_dir = torr_dir self.ext = ext self.torr_db = os.path.join(session_dir, db_name) self.db = None # pointer to our torrent DB self.my_tracker = Tracker(tracker_ip) self.session = Session(session_dir, db_name) self.session.register(self.callback)
class Tokeniser(object): """Endpoint for tokenising a file""" def __init__(self, default_kls='object', with_describe_attrs=True, import_tokens=None): self.default_kls = default_kls self.import_tokens = import_tokens self.with_describe_attrs = with_describe_attrs ######################## ### TRANSLATE ######################## def translate(self, readline, result=None, no_imports=None): # Tracker to keep track of information as the file is processed self.tokens = Tokens(self.default_kls) self.tracker = Tracker(result, self.tokens) # Add import stuff at the top of the file if self.import_tokens and no_imports is not True: self.tracker.add_tokens(self.import_tokens) # Looking at all the tokens with self.tracker.add_phase() as tracker: for tokenum, value, (_, scol), _, _ in generate_tokens(readline): self.tracker.next_token(tokenum, value, scol) # Add attributes to our Describes so that the plugin can handle some nesting issues # Where we have tests in upper level describes being run in lower level describes if self.with_describe_attrs: self.tracker.add_tokens(self.tracker.make_describe_attrs()) # Add lines to bottom of file to add __testname__ attributes self.tracker.add_tokens(self.tracker.make_method_names()) # Return translated list of tokens return self.tracker.result
def __init__(self, *args): Tracker.__init__(self, *args) self.l = 400.0/self.video.image_shape()[1] self.radius = 24.0/self.video.image_shape()[1] self.renderer = PendulumLayer(self.l, self.radius)
from torrent_file import TorrentFile import argparse if __name__ == '__main__': parser = argparse.ArgumentParser(description='Bittorrent client') parser.add_argument('--tests', action='store_true', help='Run doctests') parser.add_argument('--url', default='http://173.230.132.32', help='URL of tracker') parser.add_argument('--gen', action='store_true', help='Generate new torrent file with random data.') parser.add_argument('--fname', metavar='filename', default='myfile.torrent', help='File to use for generation or reading.') args = parser.parse_args() if args.gen: # Generate new torrent file # torrent_file = torrent_file.TorrentFile(info_dict, args.fname, True) pass else: torrent_file = TorrentFile(args.fname) if args.tests: import doctest doctest.testmod() tracker = Tracker(torrent_file) tracker.connect()
class RoadEstimation: def __init__(self): # specify topic and data type self.sub_bbox_1 = rospy.Subscriber("camera1/detection/vision_objects", DetectedObjectArray, self.bbox_array_callback) self.sub_bbox_6 = rospy.Subscriber("camera6/detection/vision_objects", DetectedObjectArray, self.bbox_array_callback) self.sub_pcd = rospy.Subscriber("/points_raw", PointCloud2, self.pcd_callback) # self.sub_pose = rospy.Subscriber("/current_pose", PoseStamped, self.pose_callback) # Publisher self.pub_intersect_markers = rospy.Publisher( "/vision_objects_position_rviz", MarkerArray, queue_size=10) self.pub_plane_markers = rospy.Publisher("/estimated_plane_rviz", MarkerArray, queue_size=10) self.pub_plane = rospy.Publisher("/estimated_plane", Plane, queue_size=10) self.pub_pcd_inlier = rospy.Publisher("/points_inlier", PointCloud2, queue_size=10) self.pub_pcd_outlier = rospy.Publisher("/points_outlier", PointCloud2, queue_size=10) self.cam1 = camera_setup_1() self.cam6 = camera_setup_6() self.plane = Plane3D(-0.157, 0, 0.988, 1.9) self.plane_world = None self.plane_tracker = None self.sac = RANSAC(Plane3D, min_sample_num=3, threshold=0.22, iteration=200, method="MSAC") self.tf_listener = TransformListener() self.tfmr = Transformer() self.tf_ros = TransformerROS() def display_bboxes_in_world(self, cam, bboxes): vis_array = MarkerArray() xlist, ylist = [], [] for box_id, bbox in enumerate(bboxes): d, C = cam.bounding_box_to_ray(bbox) intersection = self.plane.plane_ray_intersection(d, C) # print(intersection[0,0],'\t', intersection[1,0],'\t', intersection[2,0]) marker = visualize_marker(intersection, mkr_id=box_id, scale=0.5, frame_id="velodyne", mkr_color=[0.0, 0.8, 0.0, 1.0]) vis_array.markers.append(marker) xlist.append(intersection[0, 0]) ylist.append(intersection[1, 0]) self.pub_intersect_markers.publish(vis_array) # plt.pause(0.001) def bbox_array_callback(self, msg): if msg.header.frame_id == "camera1": cam = self.cam1 elif msg.header.frame_id == "camera6": cam = self.cam6 else: rospy.logwarn( "unrecognized frame id {}, bounding box callback in road estimation fail", msg.header.frame_id) return # rospy.loginfo("camera {:d} message received!!".format(camera.id)) bboxes = [] for obj in msg.objects: # rospy.loginfo("{}".format(obj.label)) # rospy.loginfo("x:{} y:{} width:{} height:{} angle:{}".format(obj.x, obj.y, obj.width, obj.height, obj.angle)) bbox = BoundingBox(obj.x, obj.y, obj.width, obj.height, obj.angle, label=obj.label) bboxes.append(bbox) self.display_bboxes_in_world(cam, bboxes) def estimate_plane(self, pcd): # threshold_z = [2.0, -0.5] # pcd_z = clip_pcd_by_distance_plane(pcd, self.plane, threshold_z, in_only=True) vec1 = np.array([1, 0, 0]) vec2 = np.array([0, 0, 1]) pt1 = np.array([0, 0, 0]) threshold = [-3.0, 6.0] plane_from_vec = Plane3D.create_plane_from_vectors_and_point( vec1, vec2, pt1) pcd_close = clip_pcd_by_distance_plane(pcd, plane_from_vec, threshold, in_only=True) pcd_close = pcd_close.extract_low() seed = 0 np.random.seed(seed) plane1, _, _, _ = self.sac.ransac(pcd_close.data.T, constraint=self.plane.param, constraint_threshold=0.5) # vis(plane1, pcd, dim_2d=True) # normal = vec1.reshape([-1,1]) / np.linalg.norm(vec1) # depth = np.matmul(pcd_close.data.T , normal).reshape([-1]) distance = plane1.distance_to_plane(pcd_close.data.T) # threshold_outer = 0.3 # threshold_inner = 0.1 # mask_outer = distance < threshold_outer # mask_inner = distance < threshold_inner # bin_dist = 5.0 # depth_min = np.min(depth) # bin_num = int((np.max(depth) - depth_min)/ bin_dist) + 1 # for i in range(bin_num): # depth_thred_min, depth_thred_max = i*bin_dist+depth_min, (i+1)*bin_dist+depth_min # mask_depth = np.logical_and(depth > depth_thred_min, depth < depth_thred_max) # part_inner = np.logical_and(mask_depth, mask_inner) # part_outer = np.logical_and(mask_depth, mask_outer) # sum_outer = np.sum(part_outer) # sum_inner = np.sum(part_inner) # if sum_outer == 0: # ratio = 1 # else: # ratio = float(sum_inner) / sum_outer # if not ratio == 1: # print(i, "{:.1f}".format(depth_thred_min), "{:.1f}".format(depth_thred_max), sum_inner, sum_outer, "{:.4f}".format(ratio)) print('Plane params:', plane1.param.T) threshold_inlier = 0.15 pcd_inlier = pcd_close.data[:, distance <= threshold_inlier] pcd_outlier = pcd_close.data[:, distance > threshold_inlier] return plane1, pcd_inlier, pcd_outlier def create_and_publish_plane_markers(self, plane, frame_id='velodyne', center=None, normal=None): if normal is None: v1 = np.array([[0, 0, 1.0]]).T else: v1 = normal v2 = np.array([[plane.a, plane.b, plane.c]]).T q_self = Quaternion_self.create_quaternion_from_vector_to_vector( v1, v2) q = Quaternion(q_self.x, q_self.y, q_self.z, q_self.w) euler = np.array(euler_from_quaternion( (q.x, q.y, q.z, q.w))) * 180 / np.pi print("Euler: ", euler) marker_array = MarkerArray() if center is None: center = [10, 0, (-plane.a * 10 - plane.d) / plane.c] marker = visualize_marker(center, frame_id=frame_id, mkr_type='cube', orientation=q, scale=[20, 10, 0.05], lifetime=30, mkr_color=[0.0, 0.8, 0.0, 0.8]) marker_array.markers.append(marker) return marker_array # @profile # profiling for analysis def pcd_callback(self, msg): rospy.logwarn("Getting pcd at: %d.%09ds, (%d,%d)", msg.header.stamp.secs, msg.header.stamp.nsecs, msg.height, msg.width) pcd_original = pointcloud2_to_xyz_array(msg) pcd = PointCloud(pcd_original.T) self.plane, pcd_inlier, pcd_outlier = self.estimate_plane(pcd) transform_matrix, trans, rot, euler = get_transformation( frame_from='/velodyne', frame_to='/world', time_from=msg.header.stamp, time_to=msg.header.stamp, static_frame='/world', tf_listener=self.tf_listener, tf_ros=self.tf_ros) if not transform_matrix is None: plane_world_param = np.matmul( np.linalg.inv(transform_matrix).T, np.array( [[self.plane.a, self.plane.b, self.plane.c, self.plane.d]]).T) plane_world_param = plane_world_param / np.linalg.norm( plane_world_param[0:3]) if self.plane_tracker is None: self.plane_tracker = Tracker(msg.header.stamp, plane_world_param) else: self.plane_tracker.predict(msg.header.stamp) self.plane_tracker.update(plane_world_param) print("plane_world:", plane_world_param.T) print("plane_traker:", self.plane_tracker.filter.x_post.T) # self.plane_world = Plane3D(plane_world_param[0,0], plane_world_param[1,0], plane_world_param[2,0], plane_world_param[3,0]) self.plane_world = Plane3D(self.plane_tracker.filter.x_post[0, 0], self.plane_tracker.filter.x_post[1, 0], self.plane_tracker.filter.x_post[2, 0], self.plane_tracker.filter.x_post[3, 0]) center_pos = np.matmul( transform_matrix, np.array([[ 10, 0, (-self.plane.a * 10 - self.plane.d) / self.plane.c, 1 ]]).T) center_pos = center_pos[0:3].flatten() # normal = np.matmul( transform_matrix, np.array([[0., 0., 1., 0.]]).T) # normal = normal[0:3] normal = None marker_array = self.create_and_publish_plane_markers( self.plane_world, frame_id='world', center=center_pos, normal=normal) self.pub_plane_markers.publish(marker_array) plane_msg = Plane() plane_msg.coef[0], plane_msg.coef[1], plane_msg.coef[ 2], plane_msg.coef[ 3] = self.plane.a, self.plane.b, self.plane.c, self.plane.d self.pub_plane.publish(plane_msg) # pcd_msg_inlier = create_point_cloud(pcd_inlier.T, frame_id='velodyne') # pcd_msg_outlier = create_point_cloud(pcd_outlier.T, frame_id='velodyne') pcd_msg_inlier = xyz_array_to_pointcloud2(pcd_inlier.T, stamp=msg.header.stamp, frame_id='velodyne') pcd_msg_outlier = xyz_array_to_pointcloud2(pcd_outlier.T, stamp=msg.header.stamp, frame_id='velodyne') self.pub_pcd_inlier.publish(pcd_msg_inlier) self.pub_pcd_outlier.publish(pcd_msg_outlier) rospy.logwarn("Finished plane estimation") def pose_callback(self, msg): rospy.logdebug("Getting pose at: %d.%09ds", msg.header.stamp.secs, msg.header.stamp.nsecs)
centers = [] # y-cooridinate for speed detection line Y_THRESH = 320 blob_min_width_far = 15 blob_min_height_far = 15 blob_min_width_near = 20 blob_min_height_near = 20 frame_start_time = None # Create object tracker tracker = Tracker(80, 3, 2, 1) # Capture livestream cap = cv2.VideoCapture(BASE_URL + 'playlist.m3u8') cap = cv2.VideoCapture('cars.mp4') while True: centers = [] frame_start_time = datetime.utcnow() ret, frame = cap.read() orig_frame = copy.copy(frame) # Draw line used for speed detection cv2.line(frame, (0, Y_THRESH), (1600, Y_THRESH), (255, 0, 0), 2)
data = self.meta_info[b"info"][b"pieces"] pieces, offset, length = [], 0, len(data) pieces = [data[offset:offset + 20] for offset in range(0, length, 20)] return pieces @property def output_file(self): """ Returns the output filename that we will use to save the torrent data in """ logging.info("Torrent output file: {0}".format( self.meta_info[b"info"][b"name"].decode("utf-8"))) return self.meta_info[b"info"][b"name"].decode("utf-8") if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("-f", "--file", help="Torrent's absolute file path", type=str, required=True) args = parser.parse_args() t = Torrent(torrent_path=args.file) tr = Tracker(t) loop = asyncio.get_event_loop() r1 = loop.run_until_complete(tr.connect()) print(r1)
def test_board_scan(): t = Tracker(Board(5, 500, 500, 500, othelloAI())) t.bd.generate_board() out = t.board_scan() assert out is False
selection = handle.region() # Process the first frame imagefile = handle.frame() if not imagefile: sys.exit(0) # Initialize the Tracker image = cv2.imread(imagefile, cv2.IMREAD_COLOR) parameters = Params() parameters.init_pos = np.floor([selection.x+selection.width/2, selection.y+selection.height/2]) # Initial position parameters.pos = parameters.init_pos parameters.target_size = np.floor([selection.width, selection.height]) tracker = Tracker(image, parameters) tracker.train(image, True) while True: # ***************************************** # VOT: Call frame method to get path of the # current image frame. If the result is # null, the sequence is over. # ***************************************** imagefile = handle.frame() if not imagefile: break # Processing the current image image = cv2.imread(imagefile, cv2.IMREAD_COLOR) region, lost, xtf = tracker.detect(image)
"-c", "--config", action="store", dest="config", type="str", help="Config file, default %default", default="./config.ini") options, args = parser.parse_args() if not os.path.isfile(options.config): raise RuntimeError('Given config file was not found') config = Config() config.read(options.config) logger = build_logger(config.fetch('logs_dir'), logging.DEBUG) app = Tracker(config, logger) app.track() except KeyboardInterrupt as e: if logger is not None: logger.info('Tracker stopped by user. PID: {0}'.format(os.getpid())) else: print 'Tracker stopped by user. PID: {0}'.format(os.getpid()) except Exception as e: if logger is not None: logger.critical(e.message, exc_info=True) else: print traceback.format_exc()
def test_moves_avail(): t = Tracker(Board(5, 500, 500, 500, othelloAI())) out = t.moves_avail() assert out is False
class Cloud(object): """ Put or get a file from the Cloud. This class maintains the session, which includes all files that we're serving up to other clients. This class is reponsible for "put"-ting and "get"-ting files to and from the cloud. """ def __init__(self, torr_dir="c:/torrent", data_dir="c:/torrent/files", tracker_ip="10.0.0.1:8000", callback=lambda: ".", session_dir="c:/torrent", db_name="torrent.db", ext=".torrent"): """ Intialize class. Parameters torr_dir: Location for torrent meta data files data_dir: Location for data files tracker_ip: IP:PORT string of tracker callback: option function which will be called while attempting to seed torrents. torr_db: Name of anydbm database where we save what we're serving. """ self.callback = callback self.data_dir = data_dir self.torr_dir = torr_dir self.ext = ext self.torr_db = os.path.join(session_dir, db_name) self.db = None # pointer to our torrent DB self.my_tracker = Tracker(tracker_ip) self.session = Session(session_dir, db_name) self.session.register(self.callback) def put(self, backup_file): """ Put files to the tracker, given an encrypted ZIP archive, and then serve them. Parameters backup_file: Takes either a full path, or the basename (assumes a directory of self.data_dir in the latter case) """ log.debug("backup_file = %s, data_dir = %s, torr_dir = %s, tracker = %s" % (backup_file, self.data_dir, self.torr_dir, self.my_tracker.tracker_ip)) # setup our TorrentMetaInfo object and create a torrent ti = TorrentMetaInfo(self.torr_dir, self.data_dir, self.my_tracker.tracker_ip, os.path.basename(backup_file)) # Make the torrent file and save new TorrentMetaInfo object with torrent name if ti.is_valid_create_object(): t=CreateTorrent(ti) ti = t.create(self.my_tracker) else: raise Exception("TorrentMetaInfo object is not valid for CreateTorrent.") # Upload torrent to tracker. Return false if the upload fails. if not self.my_tracker.upload_torrent(ti): log.debug("Failed to upload torrent to tracker.") return False # Serve the torrent return self.session.serve(ti) def get(self, torr_hash_l): """ Pass a torrent info hash, download files for torrent. Parameters torr_hash_l: Either a string or a list. Right now, it only takes a single info hash of the torrent to pull down from the tracker. The data files associated with the torrent are then downloaded from the cloud. """ if self.get_torrents(torr_hash_l): self.get_files(torr_hash_l) return True else: return False def get_files(self, torr_hash_l): """ Download our files, given a torrent's info hash (string) or a list of torrent info hashes (strings). Used by the get() function to pull down data files from the cloud. Parameters torr_hash_l: string, or list of strings """ self.session.serve_torrent_by_hash(torr_hash_l, self.torr_dir, self.data_dir, self.my_tracker.tracker_ip, self.ext) def serve_torrents(self): """ Serve up all torrents in our torr directory. Does not take any parameters, simply checks for all torrents in our torrent meta data directory, and serves them. """ self.session.serve_all_torrents(self.torr_dir, self.data_dir, self.my_tracker.tracker_ip, self.ext) def get_torrents(self, torr_hash_l): """ Pull a torrent down from the tracker by ID. Parameter torr_hash_l: list or string of info hashes of torrents to download from the tracker. """ flag = 0 if hasattr(torr_hash_l, '__iter__'): for t in torr_hash_l: torr_loc = self.my_tracker.download_torrent(t, self.torr_dir) if not torr_loc: log.debug("Multiple Torr ID's: unable to get torrent location.") flag = 1 else: torr_loc = self.my_tracker.download_torrent(torr_hash_l, self.torr_dir) if not torr_loc: log.debug("Single ID: unable to get torrent location.") flag = 1 if flag: return False else: return True # Mark for removal ? def get_files_by_torr_name(self, torr_name_l): """ Download files given a torrent file name. This function will likely be going away as it's not part of our workflow at the moment. Parameters torr_name_l: string or list of torrent info hashes """ # setup our TorrentMetaInfo object and create a torrent if hasattr(torr_name_l, '__iter__'): for t in torr_name_l: ti = TorrentMetaInfo(self.torr_dir, self.data_dir, self.my_tracker.tracker_ip, torr_file=t) self.session.serve(ti) else: ti = TorrentMetaInfo(self.torr_dir, self.data_dir, self.my_tracker.tracker_ip, torr_file=torr_name_l) self.session.serve(ti) def del_torrent(self, ih): """ Given an info hash, delete a torrent locally """ try: self.db = anydbm.open(self.torr_db, 'c') except: return False for info_hash, filename in self.db.iteritems(): if info_hash == ih: self.unstor_torr(ih) if self.session.unserve(info_hash=ih): log.debug("Torrent unserved.") else: log.debug("Failed to unserve torrent.") return True self.db.close() return False ########################################### # Start/Stop Cloud ########################################### # incomplete def stop(self): """ Save a session's state """ # loop through our handles and write out fast resume data for h in handles: if h.is_valid() and h.has_metadata(): data = lt.bencode(h.write_resume_data()) open(os.path.join(options.save_path, h.get_torrent_info().name() + '.fastresume'), 'wb').write(data) # save session settings here def start(self): """ Start the cloud from a saved state """ if self.serving_files(): log.debug("Cloud already started.") return False # Open our database, and grind through it. try: self.db = anydbm.open(self.torr_db, 'c') except: return False flag = 0 for info_hash, filename in self.db.iteritems(): log.debug("Loading... %s %s..." % (info_hash, filename,)) print os.path.join(self.torr_dir, filename+self.ext) ti = TorrentMetaInfo(self.torr_dir, self.data_dir, self.my_tracker.tracker_ip, filename, str(filename+self.ext)) # if we have it, and the tracker has it, serve it if not ti.is_valid_torr(): log.debug("Torrent invalid: %s, %s." % (info_hash, filename, )) self.unstor_torr(info_hash) elif not self.my_tracker.has_torrent(ti): log.debug("Torrent does not exist on tracker: %s, %s." % (str(ti.info_hash), filename, )) self.unstor_torr(info_hash) else: self.session.serve(ti) flag = 1 # serve at least one torrent... self.db.close() if flag: return True else: log.debug("Nothing in the database to serve.") return False def serving_files(self): """ Tells us if the cloud is serving any files. """ if len(self.session.handles) > 0: return True else: return False ########################################### # Database functions ########################################### def stor_torr(self, info_hash, torr_name): """ Store torrents we're serving in the database. If it's already in our database, just ignore it. Pairs are stored as key => infohash, value=>torr_name Parameters torr_name: file name of torrent (string) info_hash: info hash of torrent (string) """ self.db = anydbm.open(self.torr_db, 'c') if not self.db.has_key(info_hash): self.db[info_hash] = torr_name log.debug("info_hash %s, torr_name: %s" % (info_hash, torr_name,)) self.db.close() def unstor_torr(self, info_hash): """ Delete a torrent from our database. Parameters info_hash: info hash of torrent to delete Returns True if deleted. False if not deleted, or failure. """ try: self.db = anydbm.open(self.torr_db, 'w') except: log.debug("Failed opening database for key removal.") return False else: try: del self.db[info_hash] except: log.debug("Failed removing key.") return False else: return True def show_db(self): """ Enumerate all values in our db """ self.db = anydbm.open(self.torr_db, 'c') for info_hash, filename in self.db.iteritems(): print info_hash, " ", filename self.db.close()
def setUp(self): """Set up a new database session.""" session = create_database_session('sqlite:///:memory:') self._tracker = Tracker(session)
class TrackerTest(unittest.TestCase): """Unit test for the tracker""" def setUp(self): """Set up a new database session.""" session = create_database_session('sqlite:///:memory:') self._tracker = Tracker(session) def test_empty_categories(self): has_category = self._tracker.has_category("anything") self.assertFalse(has_category) def test_one_category(self): self._tracker.create_category('one') self.assertTrue(self._tracker.has_category("one")) self.assertFalse(self._tracker.has_category("two")) def test_two_categories(self): self._tracker.create_category('one') self._tracker.create_category('two') self.assertTrue(self._tracker.has_category("one")) self.assertTrue(self._tracker.has_category("two")) def test_duplicated_category_creation(self): self._tracker.create_category('one') with self.assertRaises(ValueError): self._tracker.create_category('one') def test_default_category_type(self): self._tracker.create_category('one') self.assertFalse(self._tracker.is_integer_category('one')) def test_non_integer_category(self): self._tracker.create_category('one', is_integer=False) self.assertFalse(self._tracker.is_integer_category('one')) def test_empty_categories_listing(self): self.assertEqual(self._tracker.list_categories(), []) def test_multiple_categories_listing(self): self._tracker.create_category('one') self._tracker.create_category('two') self.assertEqual(self._tracker.list_categories(), ['one', 'two']) def test_category_renaming(self): self._tracker.create_category('one') self._tracker.create_category('two') self._tracker.rename_category('two', 'three') self.assertEqual(self._tracker.list_categories(), ['one', 'three']) def test_missing_category_renaming(self): self._tracker.create_category('one') with self.assertRaises(ValueError): self._tracker.rename_category('two', 'three') def test_invalid_category_renaming(self): self._tracker.create_category('one') self._tracker.create_category('two') with self.assertRaises(ValueError): self._tracker.rename_category('two', 'one') def test_value_type_correction(self): self._tracker.create_category('one') self._tracker.correct_value_type('one', is_integer=True) self.assertTrue(self._tracker.is_integer_category('one')) def test_value_correction_to_integer(self): self._tracker.create_category('one', is_integer=True) self._tracker.correct_value_type('one', is_integer=False) self.assertFalse(self._tracker.is_integer_category('one')) def test_remove_category(self): self._tracker.create_category('one') self._tracker.create_category('two') self._tracker.remove_category('one') self.assertEqual(self._tracker.list_categories(), ['two']) def test_remove_invalid_category(self): self._tracker.create_category('one') self._tracker.create_category('two') with self.assertRaises(ValueError): self._tracker.remove_category('three') def test_save_measurement(self): self._tracker.create_category('scores') self._tracker.save_measurement('scores', 5) def test_missing_category_for_measurement(self): with self.assertRaises(ValueError): self._tracker.save_measurement('missing', 1) def test_empty_category_listing(self): self._tracker.create_category('scores') self.assertEqual(self._tracker.list_measurements('scores'), []) def test_measurement_listing(self): self._tracker.create_category('scores') self._tracker.save_measurement('scores', 5) self._tracker.save_measurement('scores', 6) self._tracker.save_measurement('scores', 7) measurements = self._tracker.list_measurements('scores') self.assertEqual(measurements[0].id, 1) self.assertEqual(measurements[1].id, 2) self.assertEqual(measurements[2].id, 3) self.assertEqual(measurements[0].value, 5) self.assertEqual(measurements[1].value, 6) self.assertEqual(measurements[2].value, 7) def test_missing_category_measurement_listing(self): with self.assertRaises(ValueError): _ = self._tracker.list_measurements('missing') def test_measurement_correction(self): self._tracker.create_category('scores') self._tracker.save_measurement('scores', 5) self._tracker.save_measurement('scores', 6) self._tracker.save_measurement('scores', 7) self._tracker.correct_measurement(1, 1) self._tracker.correct_measurement(2, 2) self._tracker.correct_measurement(3, 3) measurements = self._tracker.list_measurements('scores') self.assertEqual(measurements[0].id, 1) self.assertEqual(measurements[1].id, 2) self.assertEqual(measurements[2].id, 3) self.assertEqual(measurements[0].value, 1) self.assertEqual(measurements[1].value, 2) self.assertEqual(measurements[2].value, 3) def test_remove_measurement(self): self._tracker.create_category('scores') self._tracker.save_measurement('scores', 5) self._tracker.save_measurement('scores', 6) self._tracker.save_measurement('scores', 7) self._tracker.remove_measurement(2) measurements = self._tracker.list_measurements('scores') self.assertEqual(measurements[0].id, 1) self.assertEqual(measurements[1].id, 3) self.assertEqual(measurements[0].value, 5) self.assertEqual(measurements[1].value, 7) def test_remove_missing_measurement(self): self._tracker.create_category('scores') with self.assertRaises(ValueError): self._tracker.remove_measurement(1)
cap = cv.VideoCapture(path) cap.set(3, WIDTH) cap.set(4, HEIGHT) cv.namedWindow('Main') cv.resizeWindow('Main', (WIDTH, HEIGHT)) total_frames = int(cap.get(cv.CAP_PROP_FRAME_COUNT)) print('ok 1') initial_lh, initial_hh = 120,220 initial_ls, initial_hs = 122, 140 initial_lv, initial_hv = 135, 160 t = Tracker(initial_lh, initial_hh, initial_ls, initial_hs, initial_lv, initial_hv) # TRACKER INIT cv.namedWindow('Tracker') cv.resizeWindow('Tracker', 600,600) cv.createTrackbar("L-H", 'Tracker', initial_lh, 255, lambda eventValue, name='l_h': t.update(value=eventValue, name=name)) cv.createTrackbar("H-H", 'Tracker', initial_hh, 360, lambda eventValue, name='h_h': t.update(value=eventValue, name=name)) cv.createTrackbar("L-S", 'Tracker', initial_ls, 255, lambda eventValue, name='l_s': t.update(value=eventValue, name=name)) cv.createTrackbar("H-S", 'Tracker', initial_hs, 255, lambda eventValue, name='h_s': t.update(value=eventValue, name=name)) cv.createTrackbar("L-V", 'Tracker', initial_lv, 255, lambda eventValue, name='l_v': t.update(value=eventValue, name=name)) cv.createTrackbar("H-V", 'Tracker', initial_hv, 255, lambda eventValue, name='h_v': t.update(value=eventValue, name=name)) cv.createTrackbar("Thereshold_Min", 'Tracker', 127, 255, lambda eventValue, name='threshold_min': t.update(value=eventValue, name=name)) cv.createTrackbar("Thereshold_Max", 'Tracker', 255, 255, lambda eventValue, name='threshold_man': t.update(value=eventValue, name=name))
class SVMCLF(): def __init__(self): # ROS nodes rospy.init_node('classifier', anonymous=True) rospy.Subscriber("camera/image", Image, self.callback, queue_size=1) self.pub = rospy.Publisher('driver_node/drivestate', Int8, queue_size=1, latch=True) self.imgcnn_pub = rospy.Publisher('camera/imgcnn', Image, queue_size=1) # publish "stop" on the drivestate topic print 'publishing -1' self.pub.publish(-1) print 'Importing tensorflow ...' import tensorflow as tf print 'Tensorflow imported.' # publish "go" on the drivestate topic # Trained SVM model fn_model = rospy.get_param('~cnnModelFile') graph_file = rospy.get_param('~cnnGraphFile') saver = tf.train.import_meta_graph(graph_file) self.sess = tf.Session() graph_dir = os.path.dirname(graph_file) graph = tf.get_default_graph() saver.restore(self.sess, tf.train.latest_checkpoint(graph_dir)) self.prediction_op = graph.get_tensor_by_name("prediction_op:0") self.logits_op = graph.get_tensor_by_name("logits_op:0") #self.probs = graph.get_tensor_by_name("probs:0") self.x_placeholder = graph.get_tensor_by_name("x:0") self.keep_prob = graph.get_tensor_by_name("keep_prob:0") # svmparams = pickle.load(open(fn_params, 'rb')) #pickle.load(f2) # self.fmean = svmparams['fmean'] # self.fstd = svmparams['fstd'] # Settings self.nhistory = 1 # tracker buffer self.dt = 0.1 # update interal self.K_detthresh_stop = 0.7 # detection threshold (factor of window count) self.K_detthresh_warn = 1.2 # detection threshold (factor of window count) self.K_mapthresh = 0.08 # discard threshold (factor of window count) self.K_stopbias = 0.4 # bias to favor STOP over WARN (factor of window count) # Updates self.drive_state = 0 self.img = [None] self.lastimgtime = -1 # tracker self.tracker = Tracker(self.nhistory) # start self.loop() def callback(self, rosimg): self.img = CvBridge().imgmsg_to_cv2(rosimg) self.lastimgtime = rosimg.header.stamp.secs def loop(self): rate = rospy.Rate(1 / self.dt) lastupdate = -1 while not rospy.is_shutdown(): if (self.lastimgtime != lastupdate): start_time = time.time() # ---- process frame --- rosimg = self.img # copy to local memory before processing dec, draw_img = self.processOneFrame(rosimg) lastupdate = self.lastimgtime # Publish results #print "State=" + str(dec) + " (" + str(time.time() - start_time) + "s)" self.drive_state = dec #print ("Drivestate = ",dec) self.pub.publish(self.drive_state) # Optional - Publish image for monitoring self.imgcnn_pub.publish(CvBridge().cv2_to_imgmsg( draw_img, "bgr8")) rate.sleep() #def getFeatures(self,img): # return [ # imagefunctions.num_corners(img), # imagefunctions.num_edges(img), # imagefunctions.num_red_pixels(img), # imagefunctions.num_white_pixels(img), # imagefunctions.abs_sobel_thresh(img, orient='y', sobel_kernel=3, thresh=(100, 200)), # imagefunctions.mag_thresh(img, sobel_kernel=5, mag_thresh=(100, 180)), # imagefunctions.dir_threshold(img, sobel_kernel=3, thresh=(np.pi/8, np.pi/4)) # ] #def normalize_features(self,feature_vector,fmn,fsd): # numDim = len(feature_vector) # normFeatures = [] # normfeat = [None]*numDim # for i in range(numDim): # normfeat[i] = (feature_vector[i]-fmn[i])/fsd[i] # normFeatures.append(normfeat) # #transpose result # res = np.array(normFeatures).T # return res def draw_labeled_bboxes(self, img, labels, boxcolor): # Iterate through all detected cars for item_number in range(1, labels[1] + 1): # Find pixels with each item_number label value nonzero = (labels[0] == item_number).nonzero() # Identify x and y values of those pixels nonzeroy = np.array(nonzero[0]) nonzerox = np.array(nonzero[1]) bbox = ((np.min(nonzerox), np.min(nonzeroy)), (np.max(nonzerox), np.max(nonzeroy))) # Draw the box on the image cv2.rectangle(img, bbox[0], bbox[1], boxcolor, 2) # Return the image return img def search_windows(self, img, windows, framenum=0): # preprocess frame img_prep = imagefunctions.preprocess_one_rgb(img[0:127][:]) fvec = [] for window in windows: # extract test window from image test_img = img_prep[window[0][1]:window[1][1], window[0][0]:window[1][0]] # extract features #feat = self.getFeatures(test_img) # normalize features #normfeat = self.normalize_features(feat,self.fmean,self.fstd) # assemble batch #testvec = np.asarray(normfeat).reshape(1,-1) fvec.append(test_img) # batch prediction if (np.array(fvec).ndim >= 3): #prob_vec = self.sess.run(self.probs, feed_dict={self.x_placeholder:np.asarray(fvec, dtype=np.float32), self.keep_prob:1.0}) rvec = self.sess.run(self.prediction_op, feed_dict={ self.x_placeholder: np.asarray(fvec, dtype=np.float32), self.keep_prob: 1.0 }) #np.array(fvec).squeeze(axis=1)) else: rvec = [] # list of positive stop sign detection windows stop_indices = [i for i, x in enumerate(rvec) if x == 1] stop_windows = [windows[i] for i in stop_indices] # list of positive warn sign detection windows warn_indices = [i for i, x in enumerate(rvec) if x == 2] warn_windows = [windows[i] for i in warn_indices] # return positve detection windows return stop_windows, warn_windows def find_signs(self, img): startx = 10 stopx = 68 #imgsize[0]-windowsize[0] #80 starty = 0 #20 #19 stopy = imgsize[1] - windowsize[1] #30 window_list = [] for x in range(startx, stopx, slidestep[0]): for y in range(starty, stopy, slidestep[1]): img_in = img[y:y + windowsize[1], x:x + windowsize[0]] #img_crop_pp = imagefunctions.preprocess_one_rgb(img_crop) #img_in = np.array(255*img_crop_pp, dtype=np.uint8) if (imagefunctions.num_red_pixels(img_in) > min_red_pixels): window_list.append( ((x, y), (x + windowsize[0], y + windowsize[1]))) #stop_windows, warn_windows = self.search_windows(img, window_list, framenum=random.randint(0,9999)) stop_windows, warn_windows = self.search_windows(img, window_list) # if no window to search numwin = len(window_list) if (numwin == 0): decision = 0 labels = [None] return decision, labels, img # Method 1 - Count windows # if ((len(stop_windows)<2) and (len(warn_windows)<2)): # return 0,[None] # elif (len(stop_windows)>=len(warn_windows)): # return 1,[None] # else: # return 2,[None] # Method 2 - Localized heatmap based decision heat_stop = np.zeros_like(img[:, :, 0]).astype(np.float) heat_warn = np.zeros_like(img[:, :, 0]).astype(np.float) for bbox in window_list: startx = bbox[0][0] starty = bbox[0][1] endx = bbox[1][0] endy = bbox[1][1] cv2.rectangle(img, (startx, starty), (endx, endy), (200, 0, 0), 1) for bbox in warn_windows: startx = bbox[0][0] starty = bbox[0][1] endx = bbox[1][0] endy = bbox[1][1] heat_warn[starty:endy, startx:endx] += 1. cv2.rectangle(img, (startx, starty), (endx, endy), (0, 255, 0), 1) for bbox in stop_windows: startx = bbox[0][0] starty = bbox[0][1] endx = bbox[1][0] endy = bbox[1][1] heat_stop[starty:endy, startx:endx] += 1. cv2.rectangle(img, (startx, starty), (endx, endy), (0, 0, 255), 1) score_stop = np.max(heat_stop) score_warn = np.max(heat_warn) print '[scores] stop:' + str(score_stop) + ' warn:' + str(score_warn) # ---- GET DECISION ---- # decision = self.get_decision(score_stop, score_warn, numwin) # plot final decision region mapthresh = self.K_mapthresh * numwin labels = [None] if (decision == 1): heatmap_stop = heat_stop heatmap_stop[heatmap_stop <= mapthresh] = 0 labels = label(heatmap_stop) elif (decision == 2): heatmap_warn = heat_warn heatmap_warn[heatmap_warn <= mapthresh] = 0 labels = label(heatmap_warn) return decision, labels, img def get_decision(self, score_stop, score_warn, numwin): # decision thresholds and biases detthresh_stop = self.K_detthresh_stop * numwin detthresh_warn = self.K_detthresh_warn * numwin #stopbias = self.K_stopbias * numwin print 'numwin = ' + str(numwin) + ', detthreshSTOP = ' + str( detthresh_stop) + ', detthreshWARN = ' + str(detthresh_warn) # Make Decision if (score_stop >= detthresh_stop): # and (score_stop + stopbias > score_warn): decision = 1 elif (score_warn >= detthresh_warn): decision = 2 else: decision = 0 return decision def processOneFrame(self, img): # get decision for current frame dec, labels, draw_img = self.find_signs(img) # combine with previous results (if applicable) self.tracker.new_data(dec) final_decision = self.tracker.combined_results() # return results and output image return final_decision, draw_img
def main(): # Create opencv video capture object cap = cv2.VideoCapture('G:/cmu/colonoscopy/New folder/Cold.mp4') #cap = cv2.VideoCapture('G:/cmu/colonoscopy/imagemark/Color-Tracker-master/Retroflect-at-end.mp4') # Create Object Detector detector = Detectors() # Create Object Tracker tracker = Tracker(160, 1000, 5, 100) # Variables initialization skip_frame_count = 0 track_colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (0, 255, 255), (255, 0, 255), (255, 127, 255), (127, 0, 255), (127, 0, 127)] pause = False num = 0 frame_num = 0 # Infinite loop to process video frames while (True): frame_num += 1 print(frame_num) # Capture frame-by-frame ret, frame = cap.read() frame = frame[30:550, 400:930] #frame = frame[40:400,130:450] # Make copy of original frame orig_frame = copy.copy(frame) # Skip initial frames that display logo if (skip_frame_count < 15): skip_frame_count += 1 continue # Detect and return centeroids of the objects in the frame centers = detector.Detect1(orig_frame) # If centroids are detected then track them if (len(centers) > 0): text = 'Biopsy' cv2.putText(orig_frame, text, (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2, lineType=cv2.LINE_AA) # Track object using Kalman Filter tracker.Update(centers) # For identified object tracks draw tracking line # Use various colors to indicate different track_id for i in range(len(tracker.tracks)): if (len(tracker.tracks[i].trace) > 1): for j in range(len(tracker.tracks[i].trace) - 1): # Draw trace line x1 = tracker.tracks[i].trace[j][0][0] y1 = tracker.tracks[i].trace[j][1][0] x2 = tracker.tracks[i].trace[j + 1][0][0] y2 = tracker.tracks[i].trace[j + 1][1][0] clr = tracker.tracks[i].track_id % 9 cv2.line(frame, (int(x1), int(y1)), (int(x2), int(y2)), track_colors[clr], 2) # Display the resulting tracking frame cv2.imshow('Tracking', frame) # Display the original frame cv2.imshow('Original', orig_frame) print(num) # Slower the FPS cv2.waitKey(20) # Check for key strokes k = cv2.waitKey(50) & 0xff if k == 27: # 'esc' key has been pressed, exit program. break if k == 112: # 'p' has been pressed. this will pause/resume the code. pause = not pause if (pause is True): print("Code is paused. Press 'p' to resume..") while (pause is True): # stay in this loop until key = cv2.waitKey(30) & 0xff if key == 112: pause = False print("Resume code..!!") break # When everything done, release the capture cap.release() cv2.destroyAllWindows()
def main(): global args, device parser = argparse.ArgumentParser( description='Tracking Video Visualization') parser.add_argument('--video_path', default='test_data/ohaqlzfnuv.mp4', help='video path') parser.add_argument('--ground_truth', help='ground truth file path') parser.add_argument('--model', default='models/SiamRPNOTB.model', help='model path') parser.add_argument('--net_name', type=str, default='SiamRPNotb', help='network, SiamRPNbatchOTBMobile, SiamRPNotb') parser.add_argument('--interval', type=int, default=-1, help='initialize interval') parser.add_argument('--mode', type=str, default='test', help='[test, eval]') parser.add_argument('--result_video', help='result video') parser.add_argument('--result_file', default='saved_data/result.txt', help='result file') parser.add_argument('-v', '--visualization', dest='visualization', action='store_true', help='whether visualize result') args = parser.parse_args() args.visualization = False tracker = Tracker(0, args.net_name, args.model) if args.mode == 'eval' and args.ground_truth is not None: with open(args.ground_truth) as f: labels = f.readlines() gt_rects = [] for label in labels: label = re.split(';|,|\t| |, |; ', label.strip('\n')) label = list(map(int, label)) gt_rects.append(label) # Todo: multi video test thresholds_overlap = np.arange(0, 1.05, 0.05) success_overlap = np.zeros(len(thresholds_overlap)) fps, regions = eval_video(gt_rects, args.interval, tracker) gt_rects = get_none_rotation_rect(np.array(gt_rects)) gt_rects = corner_to_rect(gt_rects) success_overlap = compute_success_overlap(gt_rects, np.array(regions)) auc = success_overlap.mean() logging.info( 'Mean Running AP:{:.4f} Mean Running Speed {:.1f}fps'.format( auc, fps)) elif args.mode == 'test': assert (args.interval == -1) fps, regions = test_video(tracker) print(fps)
# object detection img_size = (tracktor['width'], tracktor['height']) backbone = resnet_fpn_backbone(tracktor['backbone'], True) backbone.out_channels = 256 obj_detect = Jde_RCNN(backbone, num_ID=tracktor['num_ID'], min_size=img_size[1], max_size=img_size[0], version=tracktor['version']) checkpoint = torch.load(tracktor['weights'], map_location='cpu')['model'] # if tracktor['version']=='v2': # checkpoint['roi_heads.embed_extractor.extract_embedding.weight'] = checkpoint['roi_heads.box_predictor.extract_embedding.weight'] # checkpoint['roi_heads.embed_extractor.extract_embedding.bias'] = checkpoint['roi_heads.box_predictor.extract_embedding.bias'] print(obj_detect.load_state_dict(checkpoint, strict=False)) obj_detect.eval() obj_detect.cuda() tracker = Tracker(obj_detect, tracktor['tracker']) transforms = T.Compose([T.ToTensor()]) time_total = 0 num_frames = 0 mot_accums = [] for seq_path in os.listdir(tracktor['dataset']): tracker.reset() start = time.time() print(f"Tracking: {seq_path}") sequence = LoadImagesAndLabels(root, osp.join(tracktor['dataset'], seq_path), img_size, augment=False, transforms=transforms) data_loader = DataLoader(sequence, batch_size=1, shuffle=False)
class Phaser(pyglet.window.Window): MODE_NORMAL = 0 MODE_TRACING = 1 MODE_CREATING = 2 def __init__(self): super(Phaser, self).__init__( WIDTH, HEIGHT) # config=pyglet.gl.Config(sample_buffers=1, samples=2)) self.xdim = WIDTH self.ydim = HEIGHT self.load_fonts() self.gesture_sound = pyglet.media.StaticSource( pyglet.resource.media('gesture.wav')) # add the faders self.faders = [] self.title_fader = Fader(0.1, 0.9) self.faders.append(self.title_fader) self.mouse = (0, 0) self.title_image = pyglet.font.Text(self.fonts[40], 'PhaseSeq') self.mode_image = pyglet.font.Text(self.fonts[30], 'Trace mode') self.show_saved_image = pyglet.font.Text(self.fonts[20], 'Showing saved traces') self.create_image = pyglet.font.Text(self.fonts[30], 'Creating classifier') self.showinst_image = pyglet.font.Text( self.fonts[20], 'Press H to toggle instructions') instructions = 'To create a classifier:\n' instructions += '- Right click to begin\n' instructions += '- Left click to place first path segment\n' instructions += '- Left click again to extend path\n' instructions += '- Optionally use <A> and <D> to change width\n' instructions += '- Repeat until path complete\n' instructions += '- Right click to save\n' instructions += '(Press <U> to undo last point while drawing)\n\n' instructions += 'To preserve traces on screen:\n' instructions += '- Hold <Shift> and perform a movement\n' instructions += '- Release <Shift> to stop recording\n' instructions += '- Perform these steps as many times as needed\n' instructions += '- Press <T> to toggle showing all stored recordings\n' instructions += '- Press <X> to clear all stored recordings\n' instructions += '\nOther keybindings:\n' instructions += '<C> clears all saved classifiers\n' instructions += '<L> loads saved classifiers from file\n' instructions += '<S> saves current set of classifiers to file\n' self.inst_label = pyglet.text.Label(instructions, font_size=16, x=self.width / 2, y=self.height - 100, anchor_x='center', multiline=True, width=self.width * 0.75) self.phase_trace = [] self.trace_len = 150 self.sequences = [] self.active_sequence = None self.saved_traces = [] self.show_saved_traces = False self.extents = [-0.05, 1.1, -0.6, 0.6] self.thickness = 0.07 self.tracker = Tracker(WIDTH, HEIGHT) self.mode = Phaser.MODE_NORMAL self.show_instructions = False self.import_sequences() def start_sequence(self): self.active_sequence = [] def end_sequence(self): if len(self.active_sequence) > 1: self.sequences.append( TriPhaseSequence( self.make_path(self.active_sequence, self.thickness))) self.active_sequence = None def import_sequences(self): if not os.path.exists(SEQUENCES_FILE): print('No existing sequences file found!') return self.sequences = [] self.active_sequence = None with open(SEQUENCES_FILE, 'r') as f: seq_data = f.readlines() for sd in seq_data: points = [] sd = sd.split(',') for i in range(0, len(sd), 6): t = map(float, sd[i:i + 6]) points.append(((t[0], t[1]), (t[2], t[3]), (t[4], t[5]))) self.sequences.append(TriPhaseSequence(points)) print('Imported %d sequences' % (len(self.sequences))) def export_sequences(self): if len(self.sequences) == 0: print('No sequences to export') return print('Exporting %d sequences to %s' % (len(self.sequences), SEQUENCES_FILE)) with open(SEQUENCES_FILE, 'w') as f: for j, s in enumerate(self.sequences): points = list(itertools.chain.from_iterable(s.states)) for i, p in enumerate(points): f.write('%f,%f' % p) if i < len(points) - 1: f.write(',') else: if j < len(self.sequences) - 1: f.write('\n') print('Sequence exported (%d points)' % (len(points) / 3)) def load_fonts(self): # load fonts self.fonts = {} pyglet.font.add_file("rez.ttf") for i in range(6, 41): self.fonts[i] = pyglet.font.load('Rez', i) def start(self): pyglet.clock.schedule_interval(self.update, 1 / 60.0) pyglet.app.run() self.export_sequences() def update(self, dt): """Frame update""" # update the faders, so they fade in and out for fader in self.faders: fader.update(dt) self.tracker.update(pos=self.mouse) n = len(self.tracker.the_object.d_seq) for d in self.tracker.the_object.d_seq: state = (d[0, 0], -d[0, 1] * 4) self.phase_trace.append(state) if self.mode == Phaser.MODE_NORMAL: for i, seq in enumerate(self.sequences): seq.update(state[0], state[1], dt / n) if seq.active == 1.0: self.gesture_sound.play() self.title_image = pyglet.font.Text( self.fonts[40], 'Gesture %d' % i) self.title_fader.reset() if self.mode != Phaser.MODE_TRACING and len( self.phase_trace) > self.trace_len: self.phase_trace = self.phase_trace[len(self.phase_trace) - self.trace_len:] self.draw() def draw_axes(self): w = 0.8 * self.extents[1] y1 = 0.7 * self.extents[2] y2 = 0.7 * self.extents[3] glLineWidth(3.0) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) if self.mode == Phaser.MODE_TRACING: glColor4f(0.2, 0.9, 0.5, 0.3) else: glColor4f(0.2, 0.5, 0.9, 0.3) glBegin(GL_LINES) glVertex2f(0, y1) glVertex2f(0, y2) glVertex2f(0, 0) glVertex2f(w, 0) glEnd() if self.mode == Phaser.MODE_TRACING: glColor4f(0.2, 0.9, 0.6, 0.1) glBegin(GL_QUADS) glVertex2f(0, 0) glColor4f(0.2, 0.9, 0.6, 0.0) glVertex2f(0, y1) glVertex2f(w, y1) glColor4f(0.2, 0.9, 0.6, 0.1) glVertex2f(w, 0) else: glColor4f(0.2, 0.6, 0.9, 0.1) glBegin(GL_QUADS) glVertex2f(0, 0) glColor4f(0.2, 0.6, 0.9, 0.0) glVertex2f(0, y1) glVertex2f(w, y1) glColor4f(0.2, 0.6, 0.9, 0.1) glVertex2f(w, 0) glEnd() if self.mode == Phaser.MODE_TRACING: glColor4f(0.2, 0.9, 0.5, 0.4) else: glColor4f(0.2, 0.5, 0.9, 0.4) n = 10 for i in range(n): x = w * ((i + 1) / float(n)) glBegin(GL_LINES) glVertex2f(x, -0.01) glVertex2f(x, 0.01) glEnd() glLineWidth(1.0) def draw_trace(self): glBlendFunc(GL_SRC_ALPHA, GL_ONE) k = 1.0 glLineWidth(3.0) glBegin(GL_LINE_STRIP) for vertex in reversed(self.phase_trace): if vertex: glColor4f(0.9, 0.7, 0.7, k) glVertex2f(vertex[0], vertex[1]) if self.mode != Phaser.MODE_TRACING: k = k * 0.95 #0.9 glEnd() glLineWidth(1.0) def draw_tri(self, tri, color, phase=0): alpha = color[3] glColor4f(color[0], color[1], color[2], alpha) #/2.0) glBegin(GL_TRIANGLES) glVertex2f(*tri[0]) glVertex2f(*tri[1]) glVertex2f(*tri[2]) glEnd() # glBegin(GL_LINE_LOOP) # glColor4f(color[0], color[1], color[2], alpha) # glVertex2f(*tri[0]) # glVertex2f(*tri[1]) # glVertex2f(*tri[2]) # glEnd() def draw_saved_traces(self): if not self.show_saved_traces: return glBlendFunc(GL_SRC_ALPHA, GL_ONE) glLineWidth(3.0) colours = [[0.9, 0.2, 0.2, 0.8], [0.2, 0.9, 0.2, 0.8], [0.2, 0.2, 0.9, 0.8]] for i, st in enumerate(self.saved_traces): glBegin(GL_LINE_STRIP) glColor4f(*colours[i % 3]) for j, vertex in enumerate(st): glVertex2f(vertex[0], vertex[1]) glEnd() glLineWidth(1.0) def draw_sequences(self): for sequence in self.sequences: for i, elt in enumerate(sequence.states): if sequence.active > 0.1: self.draw_tri(elt, (1, 1, 1, sequence.active)) else: if i == sequence.state: self.draw_tri(elt, (0.6, 0.9, 0.6, sequence.trigger + 0.1)) else: self.draw_tri(elt, (0.6, 0.9, 0.6, 0.25), phase=i) if self.active_sequence: self.draw_thick_line(self.active_sequence) def make_path(self, seq, thickness): """Convert a linear sequence to a set of triangles""" last = None last_l = None tris = [] #if len(seq)>2: # spl = spline.CardinalSpline(seq, tension=0.2) # seq = [spl(x/3.0) for x in range(3*(len(seq)-1))] for pt in seq: if last: normal = geometry.normal(last, pt) strut = geometry.mul(normal, thickness) if not last_l: last_r = (geometry.sub(strut, last)) last_l = (geometry.add(strut, last)) tris.append((geometry.add(strut, pt), last_r, last_l)) tris.append( (geometry.add(strut, pt), geometry.sub(strut, pt), last_r)) last_l = geometry.add(strut, pt) last_r = geometry.sub(strut, pt) last = pt return tris def draw_thick_line(self, seq): glColor4f(0.3, 0.8, 0.2, 0.5) if len(seq) == 1: glPointSize(4.0) glBegin(GL_POINTS) glVertex2f(*seq[0]) glEnd() return tris = self.make_path(seq, self.thickness) for tri in tris: glBegin(GL_LINE_LOOP) for vertex in tri: glVertex2f(*vertex) glEnd() def draw_tracing_background(self): glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glBegin(GL_QUADS) glColor4f(0.33, 0.85, 0.26, 0.3) glVertex2f(0, 0) glColor4f(0.33, 0.85, 0.26, 0.3) glVertex2f(self.width, 0) glColor4f(0.55, 0.85, 0.46, 0.3) glVertex2f(self.width, self.height) glColor4f(0.55, 0.85, 0.46, 0.3) glVertex2f(0, self.height) glEnd() def draw_background(self): glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glBegin(GL_QUADS) glColor4f(0.02, 0.04, 0.12, 1) glVertex2f(0, 0) glColor4f(0.02, 0.04, 0.12, 1) glVertex2f(self.width, 0) glColor4f(0.03, 0.07, 0.22, 1) glVertex2f(self.width, self.height) glColor4f(0.13, 0.07, 0.22, 1) glVertex2f(0, self.height) glEnd() def draw(self): """Draw the entire screen""" glClearColor(0.02, 0.04, 0.12, 1) self.last_frame_time = time.clock() self.clear() if self.mode == Phaser.MODE_TRACING: self.draw_tracing_background() else: self.draw_background() glLoadIdentity() glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() glOrtho(self.extents[0], self.extents[1], self.extents[2], self.extents[3], -1, 1) glMatrixMode(GL_MODELVIEW) glLoadIdentity() self.draw_axes() self.draw_trace() self.draw_saved_traces() self.draw_sequences() glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glLoadIdentity() self.draw_title() self.draw_mode() self.draw_instructions() def on_key_press(self, symbol, modifiers): if self.mode == Phaser.MODE_NORMAL and symbol == pyglet.window.key.LSHIFT or symbol == pyglet.window.key.RSHIFT: self.mode = Phaser.MODE_TRACING self.phase_trace = [] pyglet.window.Window.on_key_press(self, symbol, modifiers) def on_key_release(self, symbol, modifiers): if symbol == pyglet.window.key.C: print('Clearing sequences') self.sequences = [] self.active_sequence = None if os.path.exists(SEQUENCES_FILE): os.unlink(SEQUENCES_FILE) elif symbol == pyglet.window.key.A: self.thickness -= 0.01 print('thickness =', self.thickness) elif symbol == pyglet.window.key.D: self.thickness += 0.01 print('thickness =', self.thickness) elif self.mode == Phaser.MODE_TRACING and symbol == pyglet.window.key.LSHIFT or symbol == pyglet.window.key.RSHIFT: self.mode = Phaser.MODE_NORMAL self.saved_traces.append(self.phase_trace) self.phase_trace = [] elif symbol == pyglet.window.key.S: self.export_sequences() elif symbol == pyglet.window.key.L: self.import_sequences() elif symbol == pyglet.window.key.H: self.show_instructions = not self.show_instructions elif symbol == pyglet.window.key.U: if self.mode == Phaser.MODE_CREATING: if len(self.active_sequence) > 1: self.active_sequence = self.active_sequence[:-1] else: self.end_sequence() self.mode = Phaser.MODE_NORMAL elif symbol == pyglet.window.key.X: self.saved_traces = [] print('cleared saved traces') elif symbol == pyglet.window.key.T: self.show_saved_traces = not self.show_saved_traces def on_mouse_motion(self, x, y, dx, dy): self.mouse = (x, y) def new_state(self, x, y): x = x / float(self.width) y = y / float(self.height) rx = (1 - x) * self.extents[0] + (x) * self.extents[1] ry = (1 - y) * self.extents[2] + (y) * self.extents[3] self.active_sequence.append((rx, ry)) def on_mouse_release(self, x, y, button, modifiers): self.mouse = (x, y) # left mouse click adds new segment to current sequence if button == pyglet.window.mouse.LEFT and self.active_sequence != None: self.new_state(x, y) return # right mouse ends an active sequence... if (button == pyglet.window.mouse.RIGHT \ or (button == pyglet.window.mouse.LEFT and modifiers & 2)) \ and self.active_sequence != None: self.end_sequence() self.mode = Phaser.MODE_NORMAL return # ...or starts a new one if (button == pyglet.window.mouse.RIGHT \ or (button == pyglet.window.mouse.LEFT and modifiers & 2)) \ and self.active_sequence == None: self.start_sequence() self.mode = Phaser.MODE_CREATING self.new_state(x, y) return def draw_instructions(self): glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) self.showinst_image.x = 10 self.showinst_image.y = 30 self.showinst_image.halign = 'left' self.showinst_image.valign = 'center' self.showinst_image.color = (1, 1, 1, 1) self.showinst_image.draw() if self.show_instructions: self.inst_label.draw() def draw_mode(self): glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) if self.mode == Phaser.MODE_TRACING: self.mode_image.x = self.width / 2 self.mode_image.y = self.height - 40 self.mode_image.halign = "center" self.mode_image.valign = "center" self.mode_image.color = (1, 1, 1, 1) self.mode_image.draw() elif self.mode == Phaser.MODE_CREATING: self.create_image.x = self.width / 2 self.create_image.y = self.height - 40 self.create_image.halign = "center" self.create_image.valign = "center" self.create_image.color = (1, 1, 1, 1) self.create_image.draw() if self.show_saved_traces: self.show_saved_image.x = self.width - 10 self.show_saved_image.y = 30 self.show_saved_image.halign = 'right' self.show_saved_image.valign = 'center' self.show_saved_image.color = (1, 1, 1, 1) self.show_saved_image.draw() def draw_title(self): """Draw the title, fading in and out""" k = self.title_fader.get() if k > 1e-3: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) self.title_image.x = self.width / 2 self.title_image.y = self.height / 2 self.title_image.halign = "center" self.title_image.valign = "center" self.title_image.color = (1, 1, 1, k) self.title_image.draw()
input_dim = (384, 512, 3) K.set_learning_phase(0) # 0 testing, 1 training mode field_width = 151 field_height = 91 radius = 5.5 blob_min_width = 4 blob_min_height = 4 boundary_thresh = 75 frame_start_time = None # Create object tracker tracker = Tracker(200, 4, 3, 1) hflip = 1 vflip = 0 if hflip and vflip: c = -1 else: c = 0 if vflip else 1 # Capture livestream cap = VideoCapture("http://192.168.43.1:8080/video") # cap = VideoCapture(0) ds = DataSender("127.0.0.1", 1835) dr = DataReciever("127.0.0.1", 1836)
def __init__(self): super(Phaser, self).__init__( WIDTH, HEIGHT) # config=pyglet.gl.Config(sample_buffers=1, samples=2)) self.xdim = WIDTH self.ydim = HEIGHT self.load_fonts() self.gesture_sound = pyglet.media.StaticSource( pyglet.resource.media('gesture.wav')) # add the faders self.faders = [] self.title_fader = Fader(0.1, 0.9) self.faders.append(self.title_fader) self.mouse = (0, 0) self.title_image = pyglet.font.Text(self.fonts[40], 'PhaseSeq') self.mode_image = pyglet.font.Text(self.fonts[30], 'Trace mode') self.show_saved_image = pyglet.font.Text(self.fonts[20], 'Showing saved traces') self.create_image = pyglet.font.Text(self.fonts[30], 'Creating classifier') self.showinst_image = pyglet.font.Text( self.fonts[20], 'Press H to toggle instructions') instructions = 'To create a classifier:\n' instructions += '- Right click to begin\n' instructions += '- Left click to place first path segment\n' instructions += '- Left click again to extend path\n' instructions += '- Optionally use <A> and <D> to change width\n' instructions += '- Repeat until path complete\n' instructions += '- Right click to save\n' instructions += '(Press <U> to undo last point while drawing)\n\n' instructions += 'To preserve traces on screen:\n' instructions += '- Hold <Shift> and perform a movement\n' instructions += '- Release <Shift> to stop recording\n' instructions += '- Perform these steps as many times as needed\n' instructions += '- Press <T> to toggle showing all stored recordings\n' instructions += '- Press <X> to clear all stored recordings\n' instructions += '\nOther keybindings:\n' instructions += '<C> clears all saved classifiers\n' instructions += '<L> loads saved classifiers from file\n' instructions += '<S> saves current set of classifiers to file\n' self.inst_label = pyglet.text.Label(instructions, font_size=16, x=self.width / 2, y=self.height - 100, anchor_x='center', multiline=True, width=self.width * 0.75) self.phase_trace = [] self.trace_len = 150 self.sequences = [] self.active_sequence = None self.saved_traces = [] self.show_saved_traces = False self.extents = [-0.05, 1.1, -0.6, 0.6] self.thickness = 0.07 self.tracker = Tracker(WIDTH, HEIGHT) self.mode = Phaser.MODE_NORMAL self.show_instructions = False self.import_sequences()
class TrackerClient(discord.Client): def __init__(self, *args, **kwargs): super(TrackerClient, self).__init__(*args, **kwargs) self.tracker = Tracker(self) self.no_track = [] self.commands = { 'ping': self.ping, 'stats': self.stats, 'track': self.track, 'help': self.help, 'chart': self.chart, 'age': self.age_users } try: with open('DATA/USER_TRACKED.json', 'r') as f: self.no_track = json.load(f) except FileNotFoundError: with open('DATA/USER_TRACKED.json', 'w') as f: json.dump([], f) async def on_ready(self): print('Online now!') await client.change_presence(activity=discord.Game( name='@TrackerBot help')) async def send(self): async with aiohttp.ClientSession() as session: async with session.post( 'https://api.fusiondiscordbots.com/{}/'.format( self.user.id), data={ 'token': 'rv:J-HuSELAfy0pb', 'guilds': len(self.guilds), 'members': len([x for x in self.get_all_members()]) }) as resp: print('returned {0.status} from api.fusiondiscordbots.com'. format(resp)) async def on_guild_join(self, *args): await self.send() async def on_guild_remove(self, *args): await self.send() async def on_message(self, message): if not await self.get_cmd(message): pass async def get_cmd(self, message): if self.user.id in map( lambda x: x.id, message.mentions) and len(message.content.split(' ')) > 1: if message.content.split(' ')[1] in self.commands.keys(): await self.commands[message.content.split(' ')[1]](message) return True return False elif isinstance(message.channel, discord.DMChannel): if message.content.split(' ')[0] in self.commands.keys(): await self.commands[message.content.split(' ')[0]](message) return True return False async def ping(self, message): t = message.created_at.timestamp() e = await message.channel.send('pong') delta = e.created_at.timestamp() - t await e.edit(content='Pong! {}ms round trip'.format(round(delta * 1000))) async def stats(self, message): target = [ x for x in message.content.split(' ')[1:] if x.startswith('<@') ] if len(target) > 0: target = target[0][1:-1] while target[0] not in '0123456789': try: target = target[1:] except IndexError: await message.channel.send( 'Error deciphering user tagged. Make sure it\'s formatted correctly!' ) return target = self.get_user(int(target)) if target is None: await message.channel.send( 'Couldn\'t find user tagged. Are you sure they\'re real and a patron?' ) return else: target = message.author if self.tracker.getUser(target.id) is None: return em = discord.Embed(title='{}\'s stats'.format(target.name)) for key, data in self.tracker.getUser(target.id).items(): em.add_field(name=key, value='{} minutes'.format( round((data * self.tracker.INTERVAL) / 60))) await message.channel.send(embed=em) async def track(self, message): if 'disable' in message.content: await message.channel.send('Disabled tracking :thumbsup:') self.no_track.append(message.author.id) elif message.author.id in self.no_track: await message.channel.send('Enabled tracking :thumbsup:') self.no_track.remove(message.author.id) else: await message.channel.send( 'Tracking is currently enabled for you. Use `track disable` to disable tracking' ) with open('DATA/USER_TRACKED.json', 'w') as f: json.dump(self.no_track, f) async def help(self, message): await message.channel.send(embed=discord.Embed(description=''' `help` : Show this page `stats [mention]` : Get online stats for a user `track [disable]` : Enable or disable tracking for yourself `chart [ignore="Offline,Online,GameName..."]` : Generate a pie chart of your activities ''')) async def chart(self, message): user = self.tracker.getUser(message.author.id) ignore_list = [] listing = False for i in message.content.split(' '): if i.startswith('ignore="'): ignore_list.append(i) listing = True elif listing: ignore_list.append(i) elif i[-1] == '"': ignore_list.append(i) listing = False break if len(ignore_list) > 0: ignore_list = ' '.join(ignore_list) listed = ignore_list.split('"')[1].replace('"', '').split(',') listed = [i.strip().lower() for i in listed] print(listed) user = {k: v for k, v in user.items() if k.lower() not in listed} pyplot.clf() pyplot.axis('equal') t = sum(user.values()) pyplot.pie(user.values(), labels=user.keys(), autopct=lambda x: '{}mins'.format( int((((x * t) / 100) * self.tracker.INTERVAL) / 60))) pyplot.savefig('curr.png') f = open('curr.png', 'rb') await message.channel.send(file=discord.File(f, 'chart.png')) f.close() async def Update(self): await client.wait_until_ready() while not client.is_closed(): await self.tracker.Update() await asyncio.sleep(self.tracker.INTERVAL) async def age_users(self, message): users = [x for x in message.guild.members] users.sort(key=lambda x: x.created_at.timestamp()) string = '\n'.join( map( lambda x: x.name + ' ' + x.created_at.strftime( '%Y-%m-%d %H:%M:%S'), users)) new_str = '' for i in string: new_str += i if len(new_str) > 1900: await message.channel.send(new_str) new_str = '' await message.channel.send(new_str) def get_patrons(self): try: p_server = self.get_guild(350391364896161793) p_server2 = self.get_guild(366542432671760396) p_roles = [ discord.utils.get(p_server.roles, name='Donor'), discord.utils.get(p_server2.roles, name='Premium!') ] premiums = [ user for user in itertools.chain(p_server.members, p_server2.members) if any([p in user.roles for p in p_roles]) ] return premiums except: return self.get_all_members()
class TorrentClient: """ TorrentClient is a local peer which is responsible for managing connections to other peers to download and upload the data for the torrent. It makes periodic calls to the tracker registered to the torrent meta-info to get peer information for the torrent. Each received peer is stored in the queue from which the PeerConnection objects consume (maximum peer connections is defined by global MAX_PEER_CONNECTIONS variable). """ def __init__(self, torrent): # Tracker object that defines methods to connect to the tracker self.tracker = Tracker(torrent) self.piece_manager = PieceManager(torrent) # Queue of potential peers which the PeerConnection objects will consume self.available_peers = asyncio.Queue() # List of PeerConnection objects which might be connected to the peer. # Else it is waiting to consume a peer from the available_peers queue self.peers = [] self.abort = False async def start(self): """ Start downloading the torrent data by connecting to the tracker and starting the workers """ self.peers = [ PeerConnection( self.available_peers, self.tracker.torrent.info_hash, self.tracker.peer_id, self.piece_manager, self._on_block_retrieved ) for _ in range(MAX_PEER_CONNECTIONS) ] # Timestamp of the last announce call to the tracker previous_request = None # Default request interval (in sec) to tracker request_interval = 300 # sec while True: if self.abort: logging.warning("Aborting download") break if self.piece_manager.complete: logging.info("Download completed") break current_time = time.time() # Check if request_interval time has passed so make announce call to tracker if (not previous_request) or (previous_request + request_interval) < current_time: tracker_response = await self.tracker.connect( first=previous_request if previous_request else False, uploaded=self.piece_manager.bytes_uploaded, downloaded=self.piece_manager.bytes_downloaded ) if tracker_response: logging.debug("Got tracker response") previous_request = current_time request_interval = tracker_response.interval self._empty_queue() for peer in tracker_response.peers: if peer: self.available_peers.put_nowait(peer) else: logging.warning("Waiting for next tracker announce call, interval is {request_interval}".format( request_interval=request_interval)) for peer in self.peers: logging.debug("State of peer {id} is {status}".format( id=peer.remote_id, status=peer.my_state)) await asyncio.sleep(5) self.stop() def stop(self): """ Stop download and send stop signal to all peer objects """ self.abort = True for peer in self.peers: peer.stop() self.piece_manager.close() self.tracker.close() def _empty_queue(self): """ Remove all peers from the queue """ while not self.available_peers.empty(): self.available_peers.get_nowait() def _on_block_retrieved(self, peer_id, piece_index, block_offset, data): """ Callback function passed to PeerConnection object called when block is retrieved from peer :param peer_id: The id of peer the block was retrieved from :param piece_index: The piece index of the block :param block_offset: Block offset inside the piece :param data: Binary block data :return: """ self.piece_manager.block_received( peer_id=peer_id, piece_index=piece_index, block_offset=block_offset, data=data )
from tracker import Tracker, get_model_base_path if args.benchmark > 0: model_base_path = get_model_base_path(args.model_dir) im = cv2.imread(os.path.join(model_base_path, "benchmark.bin"), cv2.IMREAD_COLOR) results = [] for model_type in [3, 2, 1, 0, -1]: tracker = Tracker( 224, 224, threshold=0.1, max_threads=args.max_threads, max_faces=1, discard_after=0, scan_every=0, silent=True, model_type=model_type, model_dir=args.model_dir, no_gaze=(model_type < 0), detection_threshold=0.1, use_retinaface=0, max_feature_updates=900, static_model=True if args.no_3d_adapt == 1 else False) tracker.detected = 1 tracker.faces = [(0, 0, 224, 224)] total = 0.0 for i in range(100): start = time.perf_counter() r = tracker.predict(im) total += time.perf_counter() - start print(1. / (total / 100.))
def main(): api_id, api_hash, phone = extract_telegram_cfg(get_cfg()) tr = Tracker(api_id=api_id, api_hash=api_hash, phone=phone) tr.start()
def pcd_callback(self, msg): rospy.logwarn("Getting pcd at: %d.%09ds, (%d,%d)", msg.header.stamp.secs, msg.header.stamp.nsecs, msg.height, msg.width) pcd_original = pointcloud2_to_xyz_array(msg) pcd = PointCloud(pcd_original.T) self.plane, pcd_inlier, pcd_outlier = self.estimate_plane(pcd) transform_matrix, trans, rot, euler = get_transformation( frame_from='/velodyne', frame_to='/world', time_from=msg.header.stamp, time_to=msg.header.stamp, static_frame='/world', tf_listener=self.tf_listener, tf_ros=self.tf_ros) if not transform_matrix is None: plane_world_param = np.matmul( np.linalg.inv(transform_matrix).T, np.array( [[self.plane.a, self.plane.b, self.plane.c, self.plane.d]]).T) plane_world_param = plane_world_param / np.linalg.norm( plane_world_param[0:3]) if self.plane_tracker is None: self.plane_tracker = Tracker(msg.header.stamp, plane_world_param) else: self.plane_tracker.predict(msg.header.stamp) self.plane_tracker.update(plane_world_param) print("plane_world:", plane_world_param.T) print("plane_traker:", self.plane_tracker.filter.x_post.T) # self.plane_world = Plane3D(plane_world_param[0,0], plane_world_param[1,0], plane_world_param[2,0], plane_world_param[3,0]) self.plane_world = Plane3D(self.plane_tracker.filter.x_post[0, 0], self.plane_tracker.filter.x_post[1, 0], self.plane_tracker.filter.x_post[2, 0], self.plane_tracker.filter.x_post[3, 0]) center_pos = np.matmul( transform_matrix, np.array([[ 10, 0, (-self.plane.a * 10 - self.plane.d) / self.plane.c, 1 ]]).T) center_pos = center_pos[0:3].flatten() # normal = np.matmul( transform_matrix, np.array([[0., 0., 1., 0.]]).T) # normal = normal[0:3] normal = None marker_array = self.create_and_publish_plane_markers( self.plane_world, frame_id='world', center=center_pos, normal=normal) self.pub_plane_markers.publish(marker_array) plane_msg = Plane() plane_msg.coef[0], plane_msg.coef[1], plane_msg.coef[ 2], plane_msg.coef[ 3] = self.plane.a, self.plane.b, self.plane.c, self.plane.d self.pub_plane.publish(plane_msg) # pcd_msg_inlier = create_point_cloud(pcd_inlier.T, frame_id='velodyne') # pcd_msg_outlier = create_point_cloud(pcd_outlier.T, frame_id='velodyne') pcd_msg_inlier = xyz_array_to_pointcloud2(pcd_inlier.T, stamp=msg.header.stamp, frame_id='velodyne') pcd_msg_outlier = xyz_array_to_pointcloud2(pcd_outlier.T, stamp=msg.header.stamp, frame_id='velodyne') self.pub_pcd_inlier.publish(pcd_msg_inlier) self.pub_pcd_outlier.publish(pcd_msg_outlier) rospy.logwarn("Finished plane estimation")
def test_tag_reset(): t = Tracker(Board(5, 500, 500, 500, othelloAI())) t.bd.generate_board() t.bd.disks[1][1].halo_tag = True t.tag_reset() assert t.bd.disks[1][1].halo_tag is False
# window_name = [window for window in windows if key_word in window][0] try: # initialize the WindowCapture class # wincap = WindowCapture(window_name) cap = cv.VideoCapture('mario.mp4') # load the trained models neutral_b = cv.CascadeClassifier('cascade_models/cascade_copy.xml') jab = cv.CascadeClassifier('cascade_models/jab.xml') sheild = cv.CascadeClassifier('cascade_models/sheild.xml') count = MoveCounter() t = TimeTracker() labels = MoveLabels() tracker = Tracker() # This stores the locations at which images are recognized. move_locs = {labels.neutral_b: [], labels.jab: [], labels.shield: []} mario_training_filter = HsvFilter(0, 5, 0, 179, 255, 255, 0, 17, 0, 0) # initialize vision class vision = Vision('') while (True): # get an updated image of the game # USE THIS IF YOU ARE USING SCREENCAPTURE # smash_screenshot = wincap.get_screenshot()
else: # Default target_pos = [0.5, 0.5] except ValueError: # Default target_pos = [0.5, 0.5] else: # Default target_pos = [0.5, 0.5] else: # Default target_pos = [0.5, 0.5] time_step = 0.0001 tracker = Tracker() #matlab_port = MatlabPort() pivot_threshold = 30 forward_speed = 80 navigator = Navigator() controller = Controller(time_step, forward_speed, pivot_threshold) target_heading = 0 my_pos = [0, 0] motor_input = 3 timer = time.time() while(not navigator.has_arrived()):
#!flask/bin/python from flask import Flask, render_template, redirect, url_for, request, jsonify from tracker import Tracker from stravalib import Client app = Flask(__name__) app.config.from_envvar('APP_SETTINGS') tracker = Tracker() @app.route("/") def login(): c = Client() url = c.authorization_url(client_id=app.config['STRAVA_CLIENT_ID'], redirect_uri=url_for('.logged_in', _external=True), approval_prompt='auto') return render_template('login.html', authorize_url=url) @app.route("/strava-oauth") def logged_in(): """ Method called by Strava (redirect) that includes parameters. - state - code - error """ error = request.args.get('error') state = request.args.get('state')
def test_constructor(): t = Tracker('Hi') assert t.bd == 'Hi' assert t.computer_moves == [] assert t.tag_count == 0
def process_img(img): img = cv2.undistort(img, mtx, dist, None, mtx) preprocess_image = np.zeros_like(img[:,:,0]) gradx = abs_sobel_thresh(img, orient='x', thresh=(12, 255)) grady = abs_sobel_thresh(img, orient='y', thresh=(25, 255)) c_binary = color_thresh(img, sthresh=(100, 255), vthresh=(50, 255)) preprocess_image[((gradx == 1) & (grady == 1) | (c_binary == 1))] = 255 img_size = (img.shape[1], img.shape[0]) bot_width = .76 mid_width = .08 height_pct = .62 bottom_trim = .935 src = np.float32([ [img.shape[1]*(.5-mid_width/2), img.shape[0]*height_pct], [img.shape[1]*(.5+mid_width/2), img.shape[0]*height_pct], [img.shape[1]*(.5+bot_width/2), img.shape[0]*bottom_trim], [img.shape[1]*(.5-bot_width/2), img.shape[0]*bottom_trim], ]) offset = img.shape[1]*.25 dst = np.float32([ [offset, 0], [img.shape[1]-offset, 0], [img.shape[1]-offset, img.shape[0]], [offset, img.shape[0]] ]) M = cv2.getPerspectiveTransform(src, dst) Minv = cv2.getPerspectiveTransform(dst, src) warped = cv2.warpPerspective(preprocess_image, M, img_size, flags=cv2.INTER_LINEAR) window_width = 25 window_height = 80 curve_centers = Tracker(Mywindow_width=window_width, Mywindow_height=window_height, Mymargin=25, My_ym=10/720, My_xm=4/384, Mysmooth_factor=15) window_centroids = curve_centers.find_window_centroids(warped) l_points = np.zeros_like(warped) r_points = np.zeros_like(warped) leftx = [] rightx = [] for level in range(len(window_centroids)): leftx.append(window_centroids[level][0]) rightx.append(window_centroids[level][1]) l_mask = window_mask(window_width, window_height, warped, window_centroids[level][0], level) r_mask = window_mask(window_width, window_height, warped, window_centroids[level][1], level) l_points[(l_points == 255) | (l_mask == 1)] = 255 r_points[(r_points == 255) | (r_mask == 1)] = 255 # Draw # template = np.array(r_points+l_points, np.uint8) # zero_channel=np.zeros_like(template) # template = np.array(cv2.merge((zero_channel, template, zero_channel)), np.uint8) # warpage = np.array(cv2.merge((warped, warped, warped)), np.uint8) # result = cv2.addWeighted(warpage, 1, template, 0.5, 0.0) # result = warped yvals = range(0, warped.shape[0]) res_yvals = np.arange(warped.shape[0]-(window_height/2), 0, -window_height) left_fit = np.polyfit(res_yvals, leftx, 2) left_fitx = left_fit[0]*yvals*yvals + left_fit[1]*yvals + left_fit[2] left_fitx = np.array(left_fitx, np.int32) right_fit = np.polyfit(res_yvals, rightx, 2) right_fitx = right_fit[0]*yvals*yvals + right_fit[1]*yvals + right_fit[2] right_fitx = np.array(right_fitx, np.int32) left_lane = np.array(list(zip(np.concatenate((left_fitx-window_width/2,left_fitx[::-1]+window_width/2), axis=0), np.concatenate((yvals, yvals[::-1]), axis=0))), np.int32) right_lane = np.array(list(zip(np.concatenate((right_fitx-window_width/2,right_fitx[::-1]+window_width/2), axis=0), np.concatenate((yvals, yvals[::-1]), axis=0))), np.int32) inner_lane = np.array(list(zip(np.concatenate((left_fitx+window_width/2,right_fitx[::-1]-window_width/2), axis=0), np.concatenate((yvals, yvals[::-1]), axis=0))), np.int32) road = np.zeros_like(img) road_bkg = np.zeros_like(img) cv2.fillPoly(road, [left_lane], color=[255, 0,0]) cv2.fillPoly(road, [right_lane], color=[0, 0, 255]) cv2.fillPoly(road, [inner_lane], color=[0, 255, 0]) cv2.fillPoly(road_bkg, [left_lane], color=[255, 255, 255]) cv2.fillPoly(road_bkg, [right_lane], color=[255, 255, 255]) road_warped = cv2.warpPerspective(road, Minv, img_size, flags=cv2.INTER_LINEAR) road_warped_bkg = cv2.warpPerspective(road_bkg, Minv, img_size, flags=cv2.INTER_LINEAR) base = cv2.addWeighted(img, 1.0, road_warped_bkg, -1.0, 0.0) result = cv2.addWeighted(base, 1.0, road_warped, .7, 0.0) ym_per_pix = curve_centers.ym_per_pix xm_per_pix= curve_centers.xm_per_pix curve_fit_cr = np.polyfit(np.array(res_yvals,np.float32)*ym_per_pix, np.array(leftx, np.float32)*xm_per_pix, 2) curverad = ((1 + (2*curve_fit_cr[0]*yvals[-1]*ym_per_pix + curve_fit_cr[1])**2)**1.5) / np.absolute(2*curve_fit_cr[0]) camera_center = (left_fitx[-1] + right_fitx[-1])/2 center_diff = (camera_center-warped.shape[1]/2)*xm_per_pix side_pos = 'left' if center_diff <=0: side_pos = 'right' cv2.putText(result, 'Radius of Curvature = '+str(round(curverad, 3))+'(m)',(50,50),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,255),2) cv2.putText(result, 'Vehicle is '+str(abs(round(center_diff, 3)))+'m '+side_pos+' of center',(50,100),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,255),2) return result
def run_camera(input_shape, model): num_classes = 21 conf_thresh = 0.5 bbox_util = BBoxUtility(num_classes) vid = cv2.VideoCapture(0) sleep(1.0) # Compute aspect ratio of video vidw = vid.get(cv2.CAP_PROP_FRAME_WIDTH) vidh = vid.get(cv2.CAP_PROP_FRAME_HEIGHT) trackers = Tracker() while True: ret, origin_image = vid.read() frame = origin_image if not ret: print("Done!") return None im_size = (input_shape[0], input_shape[1]) resized = cv2.resize(frame, im_size) rgb = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB) inputs = [image.img_to_array(rgb)] tmp_inp = np.array(inputs) x = preprocess_input(tmp_inp) y = model.predict(x) results = bbox_util.detection_out(y) if len(results) > 0 and len(results[0]) > 0: det_label = results[0][:, 0] det_conf = results[0][:, 1] det_xmin = results[0][:, 2] det_ymin = results[0][:, 3] det_xmax = results[0][:, 4] det_ymax = results[0][:, 5] top_indices = [i for i, conf in enumerate(det_conf) if conf >= conf_thresh] top_conf = det_conf[top_indices] top_label_indices = det_label[top_indices].tolist() top_xmin = det_xmin[top_indices] top_ymin = det_ymin[top_indices] top_xmax = det_xmax[top_indices] top_ymax = det_ymax[top_indices] if 15 not in top_label_indices: pass else: trackers.bbox = [] trackers.features_current = [] trackers.index = [] for i in range(top_conf.shape[0]): class_num = int(top_label_indices[i]) if class_num == 15: xmin = int(round((top_xmin[i] * vidw) * 0.9)) ymin = int(round((top_ymin[i] * vidh) * 0.9)) xmax = int(round((top_xmax[i] * vidw) * 1.1)) if int(round( (top_xmax[i] * vidw)) * 1.1) <= vidw else int(round( top_xmax[i] * vidw)) ymax = int(round((top_ymax[i] * vidh) * 1.1)) if int(round( (top_ymax[i] * vidh) * 1.1)) <= vidh else int(round(top_ymax[i] * vidh)) trackers.bbox.append([xmin, ymin, xmax, ymax]) trackers.features_current.append( Extract_feature(cv2.resize(frame[ymin:ymax, xmin:xmax, :], (32, 32)))) if trackers.features_previous is None: trackers.index.append(i for i in range(len(trackers.bbox))) for j in range(len(trackers.features_current)): cv2.rectangle(frame, (int(trackers.bbox[j][0]), int(trackers.bbox[j][1])), (int(trackers.bbox[j][2]), int(trackers.bbox[j][3])), (255, 0, 0), 2) cv2.putText(frame, "person: {}".format(trackers.index[j] + 1), (trackers.bbox[j][0] + 10, trackers.bbox[j][1] + 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 1) else: trackers.match() trackers.update() for j in range(len(trackers.features_current)): cv2.rectangle(frame, (int(trackers.bbox[j][0]), int(trackers.bbox[j][1])), (int(trackers.bbox[j][2]), int(trackers.bbox[j][3])), (255, 0, 0), 2) cv2.putText(frame, "person: {}".format(trackers.index[j] + 1), (trackers.bbox[j][0] + 10, trackers.bbox[j][1] + 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 1) cv2.imshow('tracking', frame) if cv2.waitKey(5) & 0xFF == ord('q'): break
for w in self.arc_weights: s+="w" s+=str(w) for b in self.biases: s+="b" s+=str(b) for g in self.gains: s+="g" s+=str(g) for t in self.time_constants: s+="t" s+=str(t) s+=")" return s def __repr__(self): return self.__str__() #Develop a single parameter, from the binary list representing it to a float. def dev_parameter(glist, start, stop, binlim, lim): return ((int( "".join(glist[start:stop]), 2 )*1.0/binlim)*lim) if __name__ == '__main__': beer = Beer() beer.development() print beer print beer.ann print beer.ann.arcs tracker = Tracker(beer.ann) tracker.run()
def main(): ### GET LIST OF INPUT FILES FROM STORAGE CONTAINER FOR SPARK TO READ # Read from config files storage_config = ConfigReader("config.cfg", "azure-storage").get_config() db_config = ConfigReader("config.cfg", "postgres").get_config() # Get Azure storage info from config storage_acct_name = storage_config["account_name"] storage_acct_access_key = storage_config["access_key"] storage_container = storage_config["container_name"] mount_root = storage_config["mount_root"] # Set Spark Azure storage account and key storage_acct_key_str = f"fs.azure.account.key.{storage_acct_name}.blob.core.windows.net" # Strings for setting accessing filenames from storage file_type = "txt" input_dir = "data" file_suffix = f".{file_type}" suffix_len = len(file_suffix) mount_base_path = f"{mount_root}/{storage_container}" # Set up container client blob_service_client = BlobServiceClient(account_url=f"https://{storage_acct_name}.blob.core.windows.net", \ credential=storage_acct_access_key) container_client = blob_service_client.get_container_client( storage_container) # Get list of file names blob_list = container_client.list_blobs(name_starts_with=input_dir) txtfile_paths = [ blob.name for blob in blob_list if blob.name[-suffix_len:] == file_suffix ] txtfile_full_paths = [ f"{mount_base_path}/{file}" for file in txtfile_paths ] ### SET UP SPARK SESSION AND RUN THROUGH STEPS # Start spark session and set up storage account key spark = SparkSession.builder.getOrCreate() spark.conf.set(storage_acct_key_str, storage_acct_access_key) steps = [] step_1 = PipelineStep1("Step 1: Ingest", spark=spark, mount_base_path=mount_base_path, input_path=",".join(txtfile_full_paths), output_path=f"{mount_base_path}/ingested-data") steps.append(step_1) step_2 = PipelineStep2("Step 2: Preprocess", spark=spark, mount_base_path=mount_base_path, input_path=step_1.output_path, output_path=f"{mount_base_path}/preprocessed-data") steps.append(step_2) step_3 = PipelineStep3("Step 3: ETL", spark=spark, mount_base_path=mount_base_path, input_path=step_2.output_path, output_path=f"{mount_base_path}/ETL-output") steps.append(step_3) # Attempt each step and insert results into db table job_ids = [] for step in steps: tracker = Tracker(job_name=step.name, db_config=db_config) job_ids.append(tracker.job_id) try: step.run() tracker.update_job_status("Success") except Exception as e: print(e) tracker.update_job_status("Failed")
import sys import os.path sys.path.append(os.path.join(os.path.dirname(__file__), '..')) import socket from tracker import Tracker import pysocket p = pysocket.Pysocket() t = Tracker((p.get_lan_ip(), 50000)) t.run()
def tracking(self, trackers, dboxes, features, appts, fid, mtracker_id, dist_th=0.1, iou_th=0.5, dist_resume_th=0.1, resume_times_th=10, area_th=2, iou_th_single=0.75, no_detection_frames_th=50, other_overlap_th=0.15, is_oks=True): ntrackers = trackers nmtracker_id = mtracker_id # Hungarian algorithm # valid trackers number vtn = 0 # key is valid trackers sequence id, value is all trackers sequence id valid_id = {} for i, tracker in enumerate(trackers): if tracker.state == 0: continue valid_id[vtn] = i vtn += 1 # valid tracker is existed if vtn > 0: dist_matrix = np.ones((len(dboxes), vtn)) iou_matrix = np.ones((len(dboxes), vtn)) oks_matrix = np.ones((len(dboxes), vtn)) for i, dbox in enumerate(dboxes): feature = features[i] dvtn = 0 for tracker in trackers: if tracker.state == 0: continue iou = compute_iou(tracker.latest_box[:4], dbox[:4]) iou_matrix[i, dvtn] = iou if is_oks: # oks = oks_iou(np.array(tracker.pose_pts[:17]), np.array(appts[i][:17]), # calc_area(tracker.latest_box), calc_area(dbox)) oks = oks_iou(np.array(tracker.pose_pts[:17]), np.array(appts[i][:17]), calc_area(tracker.latest_box), calc_area(dbox), in_vis_thre=0.05) oks_matrix[i, dvtn] = oks # calculate feature distance distance = GetDis(tracker.mfeature, feature) dist_matrix[i, dvtn] = distance dvtn += 1 # Hungarian algorithm, dist matrix row_ind, col_ind = linear_sum_assignment(dist_matrix) rind = row_ind.tolist() cind = col_ind.tolist() # Hungarian algorithm, iou matrix if is_oks: iou_matrix = oks_matrix row_ind, col_ind = linear_sum_assignment(iou_matrix, maximize=True) # rind_iou = row_ind.tolist() cind_iou = col_ind.tolist() # # Hungarian algorithm, iou matrix # row_ind, col_ind = linear_sum_assignment(oks_matrix, maximize=True) # # rind_iou = row_ind.tolist() # cind_oks = col_ind.tolist() # determine which is dependent, which is not # which box has been updated dbox_markers = [0] * len(dboxes) # which tracker has been updated tracker_markers = [0] * len(trackers) # which valid tracker has been updated valid_tracker_markers = [0] * vtn for i, ri in enumerate(rind): ci = cind[i] distance = dist_matrix[ri, ci] # ri of rind and rind_iou is same ci_iou = cind_iou[i] iou = iou_matrix[ri, ci_iou] # if distance < dist_th: if ci == ci_iou and distance < dist_th * 1.5 and iou > iou_th / 2: ti = valid_id[ci] dbox = dboxes[ri] feature = features[ri] appt = appts[ri] # dbox overlap with other tracker is_other_overlap = False ri_iou_list = iou_matrix[ri, :].tolist() for idx, ri_iou in enumerate(ri_iou_list): if idx == ci_iou: continue if ri_iou > other_overlap_th: is_other_overlap = True break trackers[ti].update(dbox, fid, feature, appt, is_track=False, is_other_overlap=is_other_overlap) dbox_markers[ri] = 1 tracker_markers[ti] = 1 valid_tracker_markers[ci] = 1 continue elif ci != ci_iou and iou > iou_th and dist_matrix[ ri, ci_iou] < dist_th: ti = valid_id[ci_iou] dbox = dboxes[ri] feature = features[ri] appt = appts[ri] # dbox overlap with other tracker is_other_overlap = False ri_iou_list = iou_matrix[ri, :].tolist() for idx, ri_iou in enumerate(ri_iou_list): if idx == ci_iou: continue if ri_iou > other_overlap_th: is_other_overlap = True break trackers[ti].update(dbox, fid, feature, appt, is_track=False, is_other_overlap=is_other_overlap) dbox_markers[ri] = 1 tracker_markers[ti] = 1 valid_tracker_markers[ci_iou] = 1 continue elif ci != ci_iou and distance < dist_th and iou_matrix[ ri, ci] > iou_th: ti = valid_id[ci] dbox = dboxes[ri] feature = features[ri] appt = appts[ri] # dbox overlap with other tracker is_other_overlap = False ri_iou_list = iou_matrix[ri, :].tolist() for idx, ri_iou in enumerate(ri_iou_list): if idx == ci: continue if ri_iou > other_overlap_th: is_other_overlap = True break trackers[ti].update(dbox, fid, feature, appt, is_track=False, is_other_overlap=is_other_overlap) dbox_markers[ri] = 1 tracker_markers[ti] = 1 valid_tracker_markers[ci] = 1 continue # add logic, try to match killed tracker ktn = 0 # key is valid trackers sequence id, value is all trackers sequence id killed_id = {} for i, tracker in enumerate(trackers): if tracker.state != 0 or (fid - tracker.latest_fid) > max( 50, self.drate * resume_times_th): # if tracker.state != 0: continue killed_id[ktn] = i ktn += 1 if dbox_markers.count(0) != 0 and ktn != 0: dist_matrix_resume = np.ones((dbox_markers.count(0), ktn)) kdm = 0 # key is unmatched dbox idx, value is dboxes idx unmatched_dbox_id = {} for i, dm in enumerate(dbox_markers): if dm != 0: continue unmatched_dbox_id[kdm] = i dbox = dboxes[i] # try to match feature = features[i] dktn = 0 for j, tracker in enumerate(trackers): if tracker.state != 0 or ( fid - tracker.latest_fid) > max( 50, self.drate * resume_times_th): # if tracker.state != 0: continue # calculate feature distance distance = GetDis(tracker.mfeature, feature) dist_matrix_resume[kdm, dktn] = distance dktn += 1 kdm += 1 # Hungarian algorithm row_ind, col_ind = linear_sum_assignment(dist_matrix_resume) rind = row_ind.tolist() cind = col_ind.tolist() for i, ri in enumerate(rind): ci = cind[i] distance = dist_matrix_resume[ri, ci] # similarity must be very high if distance < dist_resume_th: ti = killed_id[ci] di = unmatched_dbox_id[ri] dbox = dboxes[di] feature = features[di] appt = appts[di] # area compare darea = calc_area(dbox) latest_box = trackers[ti].latest_box larea = calc_area(latest_box) # predict box compare latest_fid = trackers[ti].latest_fid delta_fid = fid - latest_fid pred_velocity = trackers[ti].pred_velocity vof = [ pred_velocity[0] * delta_fid, pred_velocity[1] * delta_fid ] pbox = [ int(latest_box[0] + vof[0]), int(latest_box[1] + vof[1]), int(latest_box[2] + vof[0]), int(latest_box[3] + vof[1]) ] iou = compute_iou(dbox[:4], pbox) # resume or not if min(darea, larea) * area_th > max( darea, larea) and iou > 0.001: trackers[ti].resume() trackers[ti].update(dbox, fid, feature, appt, is_track=False) dbox_markers[di] = 1 tracker_markers[ti] = 1 # new tracker for i, dm in enumerate(dbox_markers): if dm != 0: continue # new tracker, using valid_tracker_markers info iou_list = iou_matrix[i, :].tolist() dist_list = dist_matrix[i, :].tolist() mark = True marked_vtm = {} vcid = None for cid, vtm in enumerate(valid_tracker_markers): # tracker not matched before and iou is large if vtm == 0 and (iou_list[cid] > iou_th or (iou_list[cid] > 0.01 and dist_list[cid] < dist_th / 2)): mark = False vcid = cid break # iou area / dbox or iou area / tracker.latest_box is high if vtm == 0: ti = valid_id[cid] dbox = dboxes[i] latest_box = trackers[ti].latest_box iou, area, darea, lbarea = compute_iou( dbox[:4], latest_box[:4], True) # if dist_list[cid] < dist_th and (area / darea > iou_th_single or area / lbarea > iou_th): # seems overlap area in tracker cannot be too large, so using a smaller thresholld, as the detected box will be changed (part/full) diou = area / darea lbiou = area / lbarea if diou > iou_th_single or lbiou > iou_th: mark = False # not creedy, otherwise mis-match marked_vtm[cid] = diou + lbiou if not mark: if len(marked_vtm) != 0: vcid = sorted(marked_vtm.items(), key=lambda item: item[1], reverse=True)[0][0] ti = valid_id[vcid] dbox = dboxes[i] feature = features[i] appt = appts[i] # dbox overlap with other tracker is_other_overlap = False ri_iou_list = iou_list for idx, ri_iou in enumerate(ri_iou_list): if idx == ti: continue if ri_iou > other_overlap_th: is_other_overlap = True break trackers[ti].update(dbox, fid, feature, appt, is_track=False, is_other_overlap=is_other_overlap) valid_tracker_markers[vcid] = 1 else: # new tracker nmtracker_id += 1 nt = Tracker(nmtracker_id) dbox = dboxes[i] feature = features[i] appt = appts[i] nt.update(dbox, fid, feature, appt, is_track=False) ntrackers.append(nt) # mark which tracker is not be detected for i, tm in enumerate(tracker_markers): if tm != 0 or ntrackers[i].state == 0: continue ntrackers[i].update_no_detection_times() # ntrackers[i].update_no_detection_times(no_detection_frames_th // self.drate) else: # new tracker for i, dbox in enumerate(dboxes): nmtracker_id += 1 nt = Tracker(nmtracker_id) feature = features[i] appt = appts[i] nt.update(dbox, fid, feature, appt, is_track=False) ntrackers.append(nt) return ntrackers, nmtracker_id