webcam.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. offline = False
  2. try:
  3. from luma.led_matrix.device import max7219
  4. from luma.core.interface.serial import spi, noop
  5. from luma.core.render import canvas
  6. except ModuleNotFoundError as e:
  7. offline = True
  8. import numpy as np
  9. from PIL import Image, ImageDraw
  10. import cv2
  11. cap = cv2.VideoCapture(-1)
  12. if not offline:
  13. # create matrix device
  14. serial = spi(port=0, device=0, gpio=noop())
  15. device = max7219(serial, cascaded=4, block_orientation=-90, rotate=0)
  16. print("Created device")
  17. while True:
  18. success, image = cap.read()
  19. if success:
  20. image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  21. width, h = image.shape
  22. height = int(width/32*8)
  23. offset = int(h/2-height/2)
  24. image = image[offset:offset+height, :]
  25. image = cv2.resize(image, (32,8), interpolation = cv2.INTER_AREA)
  26. image = cv2.Canny(image, 0, 200)
  27. im = Image.fromarray(image)
  28. im = im.convert('1')
  29. if offline:
  30. image = np.asarray(im.convert("RGB"))
  31. cv2.imshow("image", cv2.resize(image, (640,120), interpolation = cv2.INTER_NEAREST))
  32. if cv2.waitKey(1) & 0xFF == ord('q'):
  33. break
  34. else:
  35. with canvas(device, im) as draw:
  36. pass
  37. else:
  38. exit(1)