NaturalHistoryMuseum / pylibdmtx

Read Data Matrix barcodes from Python 2 and 3.
MIT License
147 stars 56 forks source link

decoding from camera is too slow and non robust #98

Open denizsincar29 opened 1 year ago

denizsincar29 commented 1 year ago

Hello. I tryed this code and it seams to be very slow. using pygame camera and libdmtx to decode datamatrix, i got 2 scans in second and it doesn't decode properly.

import glob
import os
from time import sleep, time
import pygame
import pygame.camera
from pylibdmtx.pylibdmtx import decode
from PIL import Image

pygame.init()
size=(640, 480)

gameDisplay = pygame.display.set_mode(size)  # 640 480

pygame.mixer.init()
globals().update({os.path.splitext(os.path.basename(i))[0]: pygame.mixer.Sound(i) for i in glob.glob("audio/*.wav")})
success.play()  # sound
pygame.camera.init()
cam = pygame.camera.Camera(0, size, "")
cam.start()
while True:
    img = cam.get_image()
    gameDisplay.blit(img,(0,0))

    arr=Image.frombytes("RGB", (img.get_width(), img.get_height()), pygame.image.tobytes(img,"RGB"))
    decinfo=decode(arr)
    if len(decinfo)>0: print(decinfo[0])
    pygame.display.update()
    for event in pygame.event.get() :
        if event.type == pygame.QUIT :
            cam.stop()
            pygame.quit()
            exit()
    dong.play()  # sound playing
    sleep(0.05)
phlibi commented 1 year ago

I found that there is a huge speedup by telling the library to look for at most one code using decode(arr, max_count=1). Further speed gains may be possible by downsampling the image prior to decoding.

Concerning the robustness, I've found an even illumination and a proper quiet zone to be very important for successful decoding.