Download app.py from ByteJoseph/realtime-digit-draw: direct link, hf CLI and curl.
- Browser
- Download file 1.96 kB
-
https://huggingface.co/ByteJoseph/realtime-digit-draw/resolve/main/app.py
- Command line
-
hf download hf://ByteJoseph/realtime-digit-draw/app.py
-
curl -L -o app.py https://huggingface.co/ByteJoseph/realtime-digit-draw/resolve/main/app.py
1.96 kB
| import pygame, torch, cv2, numpy as np | |
| from inference import predict | |
| def draw_digi(): | |
| pygame.init() | |
| window_size = 280 | |
| display_height = window_size+50 | |
| screen = pygame.display.set_mode((window_size,display_height)) | |
| pygame.display.set_caption("Draw a digit") | |
| clock = pygame.time.Clock() | |
| screen.fill((0,0,0)) | |
| drawing = False | |
| prediction = None | |
| font = pygame.font.Font(None,36) | |
| while True: | |
| # screen.fill((0,0,0)) | |
| for event in pygame.event.get(): | |
| if event.type == pygame.QUIT: | |
| pygame.quit() | |
| return | |
| if event.type == pygame.MOUSEBUTTONDOWN: | |
| drawing = True | |
| if event.type == pygame.MOUSEBUTTONUP: | |
| drawing = False | |
| prediction = predict_digit(screen) | |
| if event.type == pygame.KEYDOWN: | |
| if event.key == pygame.K_c: | |
| screen.fill((0,0,0)) | |
| prediction = None | |
| if event.type == pygame.MOUSEMOTION and drawing: | |
| pygame.draw.circle(screen,(255,255,255),event.pos,8) | |
| if prediction is not None: | |
| text = font.render(f"Prediction: {prediction}",True,(0,255,0)) | |
| screen.blit(text,(10,window_size+10)) | |
| pygame.display.flip() | |
| clock.tick(60) | |
| def process_drawing(screen): | |
| surface = pygame.surfarray.array3d(screen) | |
| gray = np.dot(surface[...,:3],[0.2989,0.5870,0.1140]) | |
| gray = np.transpose(gray,(1,0)) | |
| gray = cv2.resize(gray,(28,28),interpolation=cv2.INTER_AREA) | |
| gray = gray.astype(np.float32)/255.0 | |
| gray = (gray - 0.5)/0.5 | |
| tensor = torch.tensor(gray,dtype=torch.float32).unsqueeze(0).unsqueeze(0) | |
| return tensor | |
| def predict_digit(screen): | |
| image = process_drawing(screen) | |
| if image is None: | |
| return None | |
| return predict(image) | |
| draw_digi() |