Created
April 2, 2018 01:43
-
-
Save omiq/0c5b4a90fab8fc6a1c0bfda21940fcdb to your computer and use it in GitHub Desktop.
This version of the QRCode demo also displays on screen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import PIL | |
| from PIL import Image | |
| import pyzbar.pyzbar as pyzbar | |
| import pygame | |
| import pygame.camera | |
| from pygame.locals import * | |
| # set up the camera | |
| pygame.init() | |
| pygame.camera.init() | |
| # clock so the loop does not freeze the pi | |
| clock = pygame.time.Clock() | |
| # set up our viewing screen | |
| screen = pygame.display.set_mode((1280, 720)) | |
| # function that pulls data from the image | |
| def read_codes(input): | |
| # read the QR code(s) | |
| codes = pyzbar.decode(input) | |
| # Print results | |
| for this_code in codes: | |
| if "QRCODE" == this_code.type: | |
| print('DETECTED: {}'.format(str(this_code.data))) | |
| return codes | |
| # main function | |
| if __name__ == '__main__': | |
| # use the first camera in the list | |
| camlist = pygame.camera.list_cameras() | |
| DEVICE = camlist[0] | |
| # reasonably high res capture, in RGB mode | |
| SIZE = (1280, 720) | |
| camera = pygame.camera.Camera(DEVICE, SIZE, "RGB") | |
| # start capture | |
| camera.start() | |
| while True: | |
| # get the image and display it at 0,0 | |
| image = camera.get_image() | |
| pygame.image.save(image, "input.jpg") | |
| screen.blit(image, (0, 0)) | |
| pygame.display.update() | |
| # read the image | |
| input_image = PIL.Image.open("input.jpg") | |
| # check the image | |
| result = read_codes(input_image) | |
| # 60 frames per second - essentially real time | |
| clock.tick(60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment