player.pyw 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #! python3
  2. # This file is part of the OpenMV project.
  3. import sys, os
  4. import numpy as np
  5. import pygame
  6. import pyopenmv
  7. from time import sleep
  8. import config
  9. dir_path = os.path.dirname(os.path.realpath(__file__))
  10. # cam code loader
  11. with open(dir_path + '/cam.py', 'r') as contentFile:
  12. script = contentFile.read()
  13. # init pygame
  14. pygame.init()
  15. programIcon = pygame.image.load(dir_path + '/icon.png')
  16. pygame.display.set_icon(programIcon)
  17. pygame.display.set_caption('EkoSłupek')
  18. connected = False
  19. pyopenmv.disconnect()
  20. for i in range(10):
  21. try:
  22. # opens CDC port.
  23. # Set small timeout when connecting
  24. pyopenmv.init(config.portname, baudrate=921600, timeout=0.050)
  25. connected = True
  26. break
  27. except Exception as e:
  28. connected = False
  29. sleep(0.100)
  30. if not connected:
  31. print ( "Failed to connect to OpenMV's serial port.\n\n")
  32. sys.exit(1)
  33. # Set higher timeout after connecting for lengthy transfers.
  34. pyopenmv.set_timeout(1*2) # SD Cards can cause big hicups.
  35. pyopenmv.stop_script()
  36. pyopenmv.enable_fb(True)
  37. pyopenmv.exec_script(script)
  38. # init screen
  39. running = True
  40. Clock = pygame.time.Clock()
  41. font = pygame.font.SysFont("monospace", 15)
  42. while running:
  43. Clock.tick(60)
  44. # read framebuffer
  45. fb = pyopenmv.fb_dump()
  46. if fb != None:
  47. # scale
  48. width = fb[0] * config.scale
  49. height = fb[1] * config.scale
  50. # create image from RGB888
  51. image = pygame.image.frombuffer(fb[2].flat[0:], (fb[0], fb[1]), 'RGB')
  52. image = pygame.transform.scale(image, (width, height))
  53. # TODO check if res changed
  54. screen = pygame.display.set_mode((width, height), pygame.DOUBLEBUF, 32)
  55. # blit stuff
  56. screen.blit(image, (0, 0))
  57. # update display
  58. pygame.display.flip()
  59. for event in pygame.event.get():
  60. if event.type == pygame.QUIT:
  61. running = False
  62. elif event.type == pygame.KEYDOWN:
  63. if event.key == pygame.K_ESCAPE:
  64. running = False
  65. if event.key == pygame.K_c:
  66. pygame.image.save(image, dir_path + "/capture.png")
  67. pygame.quit()
  68. pyopenmv.stop_script()