def draw_text(self): if len(self.level_text[0]) > 0: util.draw_text(self.screen, self.level_text[0], co.TEXT_SIZE, (co.TEXT_X, co.TEXT_Y1), (0, 0, 0)) if len(self.level_text[1]) > 0: util.draw_text(self.screen, self.level_text[1], co.TEXT_SIZE, (co.TEXT_X, co.TEXT_Y2), (0, 0, 0))
def draw(self): super().draw() if self.visible and len(self.items) > 0: if len(self.items) > 0: if self.title == None: yoffset = 0 else: yoffset = 32 self.surface.fill( game_constants.COLOR_DARKRED, pygame.Rect(4, self.index * 16 + yoffset, self.width - 8, 16)) if self.itemType == 0: for itemIndex in range(len(self.items)): util.draw_text(self.surface, self.items[itemIndex].name, game_constants.POPUP_OFFSET_X, itemIndex * 16 + yoffset, game_constants.FONT_PERFECTDOS, self.items[itemIndex].color) if len(self.quantities) == len(self.items): text = game_constants.FONT_PERFECTDOS.render( str(self.quantities[itemIndex]), False, game_constants.COLOR_WHITE) text_shadow = game_constants.FONT_PERFECTDOS.render( str(self.quantities[itemIndex]), False, game_constants.COLOR_SHADOW) self.surface.blit( text_shadow, (self.width - text.get_width() - 4 + 2, itemIndex * 16 + yoffset + 2)) self.surface.blit(text, (self.width - text.get_width() - 4, itemIndex * 16 + yoffset)) elif self.itemType == 1: for itemIndex in range(len(self.items)): util.draw_text(self.surface, self.items[itemIndex][0], game_constants.POPUP_OFFSET_X, itemIndex * 16 + yoffset, game_constants.FONT_PERFECTDOS, self.items[itemIndex][1]) if len(self.quantities) == len(self.items): text = game_constants.FONT_PERFECTDOS.render( str(self.quantities[itemIndex]), False, game_constants.COLOR_WHITE) text_shadow = game_constants.FONT_PERFECTDOS.render( str(self.quantities[itemIndex]), False, game_constants.COLOR_SHADOW) self.surface.blit( text_shadow, (self.width - text.get_width() - 4 + 2, itemIndex * 16 + yoffset + 2)) self.surface.blit(text, (self.width - text.get_width() - 4, itemIndex * 16 + yoffset)) GAME.surface_windows.blit(self.surface, (self.x, self.y)) else: GAME.controlsText = game_constants.TEXT_ONMAP self.destroy()
def draw(self, window): pygame.draw.rect( window, pygame.Color("DarkGrey"), (self.rect.left, self.rect.top, self.rect.width, self.rect.height), 3) draw_text(((self.rect.left + (self.rect.width / 2)), (self.rect.top + (self.rect.height / 2))), self.text, self.font_size, window)
def draw(self): if self.visible: self.surface.fill(game_constants.COLOR_COLORKEY) if self.image != None: self.surface.blit(self.image, (0, 0)) if self.title != '': util.draw_text(self.surface, self.title, game_constants.POPUP_OFFSET_X, game_constants.POPUP_OFFSET_Y, game_constants.FONT_PERFECTDOS, game_constants.COLOR_WHITE)
def show_game_over(): window_surface.fill((255, 255, 255)) util.draw_text("GAME OVER!", font, window_surface, window_width / 1.5, window_height / 1.5) util.draw_text("Press a key to begin", font, window_surface, window_width / 1.5, window_height * (3 / 4)) pygame.display.flip() waiting = True while waiting: clock.tick(fps) for event in pygame.event.get(): if event.type == pygame.QUIT: util.terminate() if event.type == pygame.KEYUP: waiting = False
def test_rotate_image(image_path, item_size=(256, 256), scale=0.2, item_num=6): image_list = util.load_images(image_path) image = util.build_image(image_list, item_size, scale, item_num) pixels = image.reshape(-1, image.shape[2]) unique_elements, counts_elements = np.unique(pixels, axis=0, return_counts=True) counts_elements = np.sort(counts_elements)[::-1] angles = [0, 90, 180, 270] while True: angle = angles[np.random.randint(0, len(angles))] rot_image = np.clip(image * 255, 0, 255).astype(np.uint8) rot_image = util.rotate_image(rot_image, angle) util.draw_text(rot_image, 'angle: %d' % angle, color=(255, 0, 0)) cv2.imshow('image', rot_image) key = cv2.waitKey(1000000) & 0xFF if key == 27: # 'esc break
def predict(face_recognizer, test_img): img = test_img.copy() try: face, rect = data_manager.detect_face(img) except data_manager.NoFaces: print('No faces found') return label, confidence = face_recognizer.predict(face) print('...', confidence) # TODO: more info label_text = 'ID: {}'.format(label) util.draw_rectangle(img, rect) util.draw_text(img, label_text, rect[0], rect[1]-5) return img
def _welcome_screen(self): welcome_message = """ PIZZA DOG """ intro_music = load_sound("welcome_screen_music") intro_music.play() self.screen.blit(self.background, (0, 0)) draw_text(self.screen, welcome_message, self.font, self.WIDTH/2, self.HEIGHT/3) draw_text(self.screen, "Press [ENTER] To Begin", self.font, self.WIDTH/2, (self.HEIGHT/2)+40) intro = True while intro: for event in pygame.event.get(): if event.type == pygame.QUIT: quit() elif event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN: intro_music.stop() intro = False pygame.display.flip()
def draw_eol(self, is_last_level): if not is_last_level: self.screen.blit(self.eol, (co.EOL_X, co.EOL_Y)) else: self.screen.blit(self.eog, (co.EOL_X, co.EOL_Y)) if self.level_required_neutrons > 0: util.draw_text(self.screen, str(self.level_required_neutrons), co.EOL_TEXT_SIZE, (co.EOL_REQUIRED_TEXT_X, co.EOL_REQUIRED_TEXT_Y), co.NEUTRON_COUNT_0_COLOR) else: util.draw_text(self.screen, "?", co.EOL_TEXT_SIZE, (co.EOL_REQUIRED_TEXT_X, co.EOL_REQUIRED_TEXT_Y), co.NEUTRON_COUNT_0_COLOR) if self.level_required_neutrons < 1: color = co.NEUTRON_COUNT_0_COLOR elif self.launched_neutrons < self.level_required_neutrons: color = co.NEUTRON_COUNT_LESS_COLOR elif self.launched_neutrons == self.level_required_neutrons: color = co.NEUTRON_COUNT_EQUAL_COLOR elif self.launched_neutrons > self.level_required_neutrons: color = co.NEUTRON_COUNT_MORE_COLOR util.draw_text(self.screen, str(self.launched_neutrons), co.EOL_TEXT_SIZE, (co.EOL_NUMBER_TEXT_X, co.EOL_NUMBER_TEXT_Y), color)
def draw_neutron_count(self): self.screen.blit(textures.NEUTRON_TEXTURE.convert_alpha(), (co.NEUTRON_COUNT_X, co.NEUTRON_COUNT_Y)) if self.launched_neutrons == 0: color = co.NEUTRON_COUNT_0_COLOR elif self.launched_neutrons < self.level_required_neutrons: color = co.NEUTRON_COUNT_LESS_COLOR elif self.launched_neutrons == self.level_required_neutrons: color = co.NEUTRON_COUNT_EQUAL_COLOR elif self.launched_neutrons > self.level_required_neutrons: color = co.NEUTRON_COUNT_MORE_COLOR if self.level_required_neutrons > 0: util.draw_text( self.screen, "{}/{}".format(self.launched_neutrons, self.level_required_neutrons), co.NEUTRON_COUNT_TEXT_SIZE, (co.NEUTRON_COUNT_TEXT_X, co.NEUTRON_COUNT_TEXT_Y), color) else: util.draw_text(self.screen, str(self.launched_neutrons), co.NEUTRON_COUNT_TEXT_SIZE, (co.NEUTRON_COUNT_TEXT_X, co.NEUTRON_COUNT_TEXT_Y), co.NEUTRON_COUNT_0_COLOR)
def main(args): hists = [[util.get_tobject(args, h.lstrip("-")) for h in hlist] for hlist in args.hstack] util.validate_hists(args.hstack, hists) hists = [[util.resize_histo(args, h) for h in hlist] for hlist in hists] canvas = ROOT.TCanvas("canvas", "Canvas", 250, 100, 700, 500) hstack = ROOT.THStack() for i, (hlist, hnames) in enumerate(zip(hists, args.hstack)): h = hlist[0] if len(hlist) > 1: for hadd, hname in zip(hlist[1:], hnames[1:]): #if "InTime-" in hname: # h.Add(hadd, 10. / 1837.) if hname.startswith("-"): h.Add(hadd, -1) else: h.Add(hadd) if args.area_normalize and h.Integral() > 1e-4: h.Scale(1. / h.Integral()) color = util.namecolors(args.names[i] if args.names else hnames[0]) if args.stack: h.SetFillColor(color) else: h.SetLineColor(color) h.SetLineWidth(3) if args.names: name = args.names[i] if args.nevent_in_legend: name += " (%i)" % int(h.Integral()) h.SetTitle(name) hstack.Add(h) drawstr = "HIST" if args.stack else "NOSTACK HIST" hstack.Draw(drawstr) util.style(args, canvas, hstack) if args.legend_position: legend = ROOT.gPad.BuildLegend(*(args.legend_position + [""])) if args.logy: canvas.SetLogy() box = util.draw_text(args) canvas.Update() util.wait(args) util.write(args, canvas)
def draw(self): if self.visible: self.surface.blit(game_constants.SPRITE_DESCRIPTIONWINDOW, (0, 0)) if self.itemType == 0: if self.item.sprite_list != None: self.surface.blit( pygame.transform.scale2x(self.item.sprite), (16, 16)) util.draw_text(self.surface, self.item.name, 112, 24, game_constants.FONT_PERFECTDOS_MEDIUM, self.item.color) util.draw_text(self.surface, self.item.itemType, 112, 48, game_constants.FONT_PERFECTDOS, game_constants.COLOR_WHITE) for lineIndex in range(len(self.item.description)): xoffset = 0 for element in self.item.description[lineIndex]: text = game_constants.FONT_PERFECTDOS.render( element[0], False, element[1]) self.surface.blit(text, (xoffset + 16, 96 + lineIndex * 16)) xoffset += text.get_width() elif self.itemType == 1: if self.item[2] != None: self.surface.blit(pygame.transform.scale2x(self.item[2]), (16, 16)) util.draw_text(self.surface, self.item[0], 112, 24, game_constants.FONT_PERFECTDOS_MEDIUM, self.item[1]) util.draw_text(self.surface, self.item[3], 112, 48, game_constants.FONT_PERFECTDOS, game_constants.COLOR_WHITE) for lineIndex in range(len(self.item[4])): xoffset = 0 for element in self.item[4][lineIndex]: text = game_constants.FONT_PERFECTDOS.render( element[0], False, element[1]) self.surface.blit(text, (xoffset + 16, 96 + lineIndex * 16)) xoffset += text.get_width() GAME.update_rects.append( GAME.surface_windows.blit(self.surface, (self.x, self.y)))
def main(args): util.optstat(args) f = ROOT.TFile(args.input) hist = f.Get(args.hist) util.validate_hists([args.hist], [hist]) hist = util.resize_histo(args, hist, "") hstack = ROOT.THStack() canvas = ROOT.TCanvas("canvas", "Canvas", 250, 100, 700, 500) class ArgDict(dict): __getattr__ = dict.get hists = [] for i, (low, high) in enumerate(zip(args.lows, args.highs)): this_args = ArgDict({"y_min": low, "y_max": high, "projectionX": True}) h = util.resize_histo(this_args, hist.Clone(), str(i)) h.SetLineColor(util.fillcolors(i)) h.SetLineWidth(3) if args.names: name = args.names[i] h.SetTitle(name) hists.append(h) hstack.Add(h) hstack.Draw("NOSTACK HIST") legend = ROOT.gPad.BuildLegend(*(args.legend_position + [""])) if args.title is not None: hstack.SetTitle(args.title) util.style(args, canvas, hstack) box = util.draw_text(args) canvas.Update() util.wait(args) util.write(args, canvas)
def test_random_atals(image_path, item_size=(256, 256), atlas_size=(1024, 1024), scale=0.2): grid = int(atlas_size[0] / item_size[0]) image_list = util.load_images(image_path) while True: row_images = [] for i in range(grid): images = [] for j in range(grid): item_num = np.random.randint(1, 20) tmp_scale = scale + np.random.random() * 0.05 img = util.build_image(image_list, item_size, tmp_scale, item_num) img = np.clip(img * 255, 0, 255).astype(np.uint8) util.draw_text(img, '%d' % item_num, color=(255, 255, 255)) images.append(img) row_images.append(cv2.hconcat(images)) image = cv2.vconcat(row_images) util.draw_text(image, 'press [esc] to exit, other key to refresh.', color=(255, 0, 0)) cv2.imshow('image', image) key = cv2.waitKey(1000000) & 0xFF if key == 27: # 'esc break util.draw_text(image, 'building image, please wait...', pos=(10, 35), color=(0, 0, 255)) cv2.imshow('image', image) cv2.waitKey(50)
mario.move_left = keys[K_LEFT] and not keys[K_RIGHT] mario.move_right = keys[K_RIGHT] and not keys[K_LEFT] mario.move_up = keys[K_UP] and not keys[K_DOWN] mario.move_down = keys[K_DOWN] and not keys[K_UP] ## go through game loop and add koopas koopa_add_counter += 1 if koopa_add_counter == koopa_army.bad_rate: koopa_add_counter = 0 koopa_army.generate_koopa(window_surface) # draw game world window_surface.fill(background) window_surface.blit(sky, (0, 0)) util.draw_text("Score : %s" % (score), font, window_surface, 450, 510) util.draw_text("Top Score : %s" % (topscore), font, window_surface, 450, 540) # move characters mario.move_and_attack(window_surface, koopa_army) peach.move_towards(flower) koopa_army.move(peach) # draw updated_positions mario.draw_character(window_surface) peach.draw_character(window_surface) flower.draw_character(window_surface) koopa_army.draw_army(window_surface) pygame.display.update()
from sort import DeepSort from util import DurationTimer, draw_text, iterate_video, draw_trackers, draw_bbox from yolo import Detecter if __name__ == '__main__': cap = cv2.VideoCapture('vdo.avi') orig_dim = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)), int( cap.get(cv2.CAP_PROP_FRAME_WIDTH)) inp_dim = tuple((x // 4 // 2**5 + 1) * 2**5 for x in orig_dim) detecter = Detecter(inp_dim) tracker = DeepSort('weights/ckpt.t7') # tracker = Sort() for frame in iterate_video(cap): with DurationTimer() as d: detections = detecter.detect(frame) # tracks = tracker.update(detections[0]) tracks = tracker.update(*detections, frame) draw_bbox(frame, detections[0]) draw_trackers(frame, tracks) draw_text(frame, f'FPS: {int(1 / d.duration)}', [255, 255, 255], upper_right=(frame.shape[1] - 1, 0)) cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break
def draw_status(): GAME.surface_status.blit(game_constants.SPRITE_STATUS, (0, 0)) GAME.surface_status.blit( game_content.SPRITESHEET_ICONS.image_at( (0, 0, 16, 16), colorkey=game_constants.COLOR_COLORKEY), (16, 8)) #DRAW HP pygame.draw.rect(GAME.surface_status, game_constants.COLOR_DARKESTGRAY, pygame.Rect(48, 8, 200, 13)) pygame.draw.rect( GAME.surface_status, game_constants.COLOR_HP, pygame.Rect(48, 8, 200 * GAME.player.hp / GAME.player.stats[0], 13)) text = game_constants.FONT_PERFECTDOS.render(str(GAME.player.hp), False, game_constants.COLOR_WHITE) GAME.surface_status.blit(text, (256, 8)) text = game_constants.FONT_PERFECTDOS.render(str(GAME.player.stats[0]), False, game_constants.COLOR_WHITE) GAME.surface_status.blit(text, (330 - text.get_width(), 8)) GAME.surface_status.blit( game_content.SPRITESHEET_ICONS.image_at( (0, 16, 16, 16), colorkey=game_constants.COLOR_COLORKEY), (16, 32)) #DRAW MANA pygame.draw.rect(GAME.surface_status, game_constants.COLOR_DARKESTGRAY, pygame.Rect(48, 32, 200, 13)) pygame.draw.rect( GAME.surface_status, game_constants.COLOR_CYAN, pygame.Rect(48, 32, 200 * GAME.player.mana / GAME.player.stats[1], 13)) text = game_constants.FONT_PERFECTDOS.render(str(GAME.player.mana), False, game_constants.COLOR_WHITE) GAME.surface_status.blit(text, (256, 32)) text = game_constants.FONT_PERFECTDOS.render(str(GAME.player.stats[1]), False, game_constants.COLOR_WHITE) GAME.surface_status.blit(text, (330 - text.get_width(), 32)) GAME.surface_status.blit( game_content.SPRITESHEET_ICONS.image_at( (16, 0, 16, 16), colorkey=game_constants.COLOR_COLORKEY), (16, 56)) #DRAW FOOD pygame.draw.rect(GAME.surface_status, game_constants.COLOR_DARKESTGRAY, pygame.Rect(48, 56, 80, 13)) pygame.draw.rect( GAME.surface_status, game_constants.COLOR_HUNGER, pygame.Rect(48, 56, 80 * GAME.player.hunger / game_constants.MAX_HUNGER, 13)) GAME.surface_status.blit( game_content.SPRITESHEET_ICONS.image_at( (32, 0, 16, 16), colorkey=game_constants.COLOR_COLORKEY), (138, 56)) #DRAW CARRY pygame.draw.rect(GAME.surface_status, game_constants.COLOR_DARKESTGRAY, pygame.Rect(168, 56, 80, 13)) pygame.draw.rect( GAME.surface_status, game_constants.COLOR_YELLOW, pygame.Rect(168, 56, 80 * GAME.player.currentWeight() / GAME.player.stats[10], 13)) for textIndex in range(len(GAME.controlsText)): #DRAW CONTROLS xOffset = (textIndex // 3) * 200 util.draw_text(GAME.surface_status, GAME.controlsText[textIndex][0], 500 + xOffset, (textIndex % 3) * 16 + 4, game_constants.FONT_PERFECTDOS, game_constants.COLOR_WHITE) util.draw_text(GAME.surface_status, GAME.controlsText[textIndex][1], 550 + xOffset, (textIndex % 3) * 16 + 4, game_constants.FONT_PERFECTDOS, game_constants.COLOR_WHITE)
def main(args): util.optstat(args) hist = util.get_tobject(args, args.hist) util.validate_hists([args.hist], [hist]) hist = util.resize_histo(args, hist) if args.make_percentages: if isinstance(hist, ROOT.TH2D): for i in range(1, hist.GetNbinsX() + 1): this_norm = hist.Integral(i, i, 1, hist.GetNbinsY()) for j in range(1, hist.GetNbinsY() + 1): if this_norm < 1e-3: hist.SetBinContent(i, j, 0) else: hist.SetBinContent( i, j, hist.GetBinContent(i, j) / this_norm) else: for k in range(1, hist.GetNbinsZ() + 1): for i in range(1, hist.GetNbinsX() + 1): this_norm = hist.Integral(i, i, 1, hist.GetNbinsY(), k, k) for j in range(1, hist.GetNbinsY() + 1): if this_norm < 1e-3: hist.SetBinContent(i, j, k, 0) else: hist.SetBinContent( i, j, k, hist.GetBinContent(i, j, k) / this_norm) hist2 = None if args.meanY: hist2 = mean_and_err(hist) canvas = ROOT.TCanvas("canvas", "Canvas", 250, 100, 700, 500) drawstr = "" if args.drawstr is not None: drawstr = args.drawstr elif isinstance(hist, ROOT.TH2D): drawstr = "COLZ" if args.draw_text: drawstr += " TEXT" hist.SetMarkerSize(3) ROOT.gStyle.SetPaintTextFormat("1.3f") elif isinstance(hist, ROOT.TH1D): drawstr = "HIST" elif isinstance(hist, ROOT.TGraph): drawstr = "AL" hist.Draw(drawstr) if hist2: hist2.SetLineColor(ROOT.kRed) hist2.SetLineWidth(3) hist2.SetMarkerSize(1) hist2.SetMarkerStyle(21) hist2.SetMarkerColor(ROOT.kRed) hist2.Draw("P") if args.title is not None: hist.SetTitle(args.title) util.style(args, canvas, hist) if args.logy: canvas.SetLogy() if args.logz: canvas.SetLogz() box = util.draw_text(args) canvas.Update() util.wait(args) util.write(args, canvas)
def label_objects(self): for obj_class in self.collidable_objects: for obj in obj_class: util.draw_text(self.window, obj.__str__(), obj.get_position().to_tuple(), 24)
vc = cv2.VideoCapture(0) rval = vc.isOpened() im_cnt = 0 save_faces = False win = dlib.image_window() person = data_manager._Person(-1) # fake person current_text = None fails_cnt = 0 while rval: rval, frame = vc.read() image = frame[:, :, ::-1] if current_text is not None: util.draw_text(frame, current_text, 210, 40) win.set_image(frame[:, :, ::-1]) else: win.set_image(image) win.clear_overlay() try: person.detect_face(image, win) except data_manager.NoFaces: fails_cnt += 1 if fails_cnt > 3: current_text = None else: fails_cnt = 0 vec = person.vecs[-1] best_dist = None