Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
class Damageable:
def __init__(self):
self.defense = 45
class Brain:
def __init__(self):
self.smarts = 9000
##########################
# Define some Processors:
##########################
class MovementProcessor(esper.Processor):
def __init__(self):
super().__init__()
def process(self):
for ent, (vel, pos) in self.world.get_components(Velocity, Position):
pos.x += vel.x
pos.y += vel.y
print("Current Position: {}".format((int(pos.x), int(pos.y))))
#############################
# Set up some dummy entities:
#############################
def create_entities(world, number):
for _ in range(number // 2):
world.create_entity(Position(), Velocity(), Health(), Command())
self.z = None
class ComponentD:
def __init__(self):
self.direction = "left"
self.previous = "right"
class ComponentE:
def __init__(self):
self.items = {"itema": None, "itemb": 1000}
self.points = [a + 2 for a in list(range(44))]
class CorrectProcessorA(esper.Processor):
def __init__(self):
super().__init__()
def process(self):
pass
class CorrectProcessorB(esper.Processor):
def __init__(self):
super().__init__()
def process(self):
pass
class IncorrectProcessor:
def test_add_processor(populated_world):
assert len(populated_world._processors) == 0
correct_processor_a = CorrectProcessorA()
assert isinstance(correct_processor_a, esper.Processor)
populated_world.add_processor(correct_processor_a)
assert len(populated_world._processors) == 1
assert isinstance(populated_world._processors[0], esper.Processor)
def world():
return esper.World()
def test_world_instantiation(world):
assert type(world) == esper.World
assert type(world._next_entity_id) == int
assert type(world._entities) == dict
assert type(world._components) == dict
assert type(world._processors) == list
def run():
# Initialize Pygame stuff
pygame.init()
window = pygame.display.set_mode(RESOLUTION)
pygame.display.set_caption("Esper Pygame example")
clock = pygame.time.Clock()
pygame.key.set_repeat(1, 1)
# Initialize Esper world, and create a "player" Entity with a few Components.
world = esper.World()
player = world.create_entity()
world.add_component(player, Velocity(x=0, y=0))
world.add_component(player, Renderable(image=pygame.image.load("redsquare.png"), posx=100, posy=100))
# Another motionless Entity:
enemy = world.create_entity()
world.add_component(enemy, Renderable(image=pygame.image.load("bluesquare.png"), posx=400, posy=250))
# Create some Processor instances, and asign them to be processed.
render_processor = RenderProcessor(window=window)
movement_processor = MovementProcessor(minx=0, maxx=RESOLUTION[0], miny=0, maxy=RESOLUTION[1])
world.add_processor(render_processor)
world.add_processor(movement_processor)
running = True
while running:
for event in pygame.event.get():
num_workers=5, worker=worker_config('n1-standard-32'), pipelines=[face_embedding.FaceEmbeddingPipeline])
configs = [(cfg, [
ScannerJobConfig(io_packet_size=500, work_packet_size=20, pipelines_per_worker=4),
ScannerJobConfig(io_packet_size=1000, work_packet_size=20, pipelines_per_worker=4),
ScannerJobConfig(io_packet_size=1000, work_packet_size=80, pipelines_per_worker=4),
ScannerJobConfig(io_packet_size=1000, work_packet_size=20, pipelines_per_worker=8),
])]
bench('embedding', {'videos': videos, 'frames': [frames_for_video(v) for v in videos]},
run_pipeline, configs, no_delete=True, force=True)
exit()
videos = list(Video.objects.filter(threeyears_dataset=True).order_by('id'))
def load_frames():
return par_for(frames_for_video, videos, workers=8)
frames = pcache.get('emb_frames', load_frames, force=True)
videos, frames = unzip([(v, f) for (v, f) in zip(videos, frames)
if len(f) > 0])
videos = list(videos)
frames = list(frames)
# Export packed embeddings and IDs into single files
if False:
def get_ids(video):
return [f['id'] for f in Face.objects.filter(frame__video=video).order_by('frame__number', 'id').values('id')]
all_ids = pcache.get('emb_ids', (lambda: par_for(get_ids, videos, workers=4)))
import struct
with open('/app/data/embs/sevenyears_ids.bin', 'wb') as f:
for i, ids in tqdm(enumerate(all_ids)):
path = '/app/data/embs/{:07d}.bin'.format(i)
exit()
videos = videos
cfg = cluster_config(
num_workers=100, worker=worker_config('n1-standard-64'),
pipelines=[gender_detection.GenderDetectionPipeline])
with make_cluster(cfg, sql_pool=2, no_delete=True) as db_wrapper:
db = db_wrapper.db
# if True:
# db_wrapper = ScannerWrapper.create()
frames = pcache.get('gender_frames', lambda: par_for(frames_for_video, videos, workers=8))
videos, frames = unzip([(v, f) for (v, f) in zip(videos, frames) if len(f) > 0])
videos = list(videos)
frames = list(frames)
detect_genders(
db,
videos=[v.for_scannertools() for v in videos],
db_videos=videos,
frames=frames,
faces=[ScannerSQLTable(Face, v, num_elements=len(f),
filter='query_frame.shot_boundary = false')
for v, f in zip(videos, frames)],
run_opts={
'io_packet_size': 500,
'work_packet_size': 20,
'pipeline_instances_per_node': 16
})
with open('/app/data/embs/sevenyears_embs.bin', 'wb') as f:
for i in tqdm(list(range(len(videos)))):
path = '/app/data/embs/{:07d}.bin'.format(i)
if os.path.isfile(path):
byts = open(path, 'rb').read()
if len(byts) / (4 * 128) != len(all_ids[i]):
print(i)
f.write(byts)
f.flush()
print('done')
exit()
if __name__ == "__main__":
cfg = cluster_config(
num_workers=0, worker=worker_config('n1-standard-64'),
pipelines=[face_embedding.FaceEmbeddingPipeline])
with make_cluster(cfg, sql_pool=2, no_delete=True) as db_wrapper:
# if True:
# db_wrapper = ScannerWrapper.create()
db = db_wrapper.db
embs = embed_faces(
db,
videos=[v.for_scannertools() for v in videos],
frames=frames,
faces=[ScannerSQLTable(Face, v, num_elements=len(f),
filter='query_frame.shot_boundary = false')
for v, f in zip(videos, frames)],
if False:
with Timer('benchmark'):
videos = videos[:50]
def run_pipeline(db, videos, frames, **kwargs):
return detect_genders(
db,
db_videos=videos,
videos=[v.for_scannertools() for v in videos],
frames=frames,
faces=[ScannerSQLTable(Face, v) #num_elements=len(f))
for v, f in zip(videos, frames)],
cache=False,
**kwargs)
cfg = cluster_config(
num_workers=5, worker=worker_config('n1-standard-32'), pipelines=[GenderDetectionPipeline])
configs = [(cfg, [
ScannerJobConfig(io_packet_size=1000, work_packet_size=20, pipelines_per_worker=4),
ScannerJobConfig(io_packet_size=1000, work_packet_size=20, pipelines_per_worker=8),
ScannerJobConfig(io_packet_size=1000, work_packet_size=20, pipelines_per_worker=16)
])]
bench('gender', {'videos': videos, 'frames': [frames_for_video(v) for v in videos]},
run_pipeline, configs, no_delete=True, force=True)
exit()
videos = videos
cfg = cluster_config(
num_workers=100, worker=worker_config('n1-standard-64'),
pipelines=[gender_detection.GenderDetectionPipeline])