original_docstring: Detects PUIDs on image. Return list of PUIDs or empty list
docstring: Detects PUIDs on image. Return list of PUIDs or empty list
docstring_tokens: Detects PUIDs on image . Return list of PUIDs or empty list
code: def detect_data_matrix(image) -> list:
      """Detects PUIDs on image. Return list of PUIDs or empty list"""
  
      start_time_decoding = time.time()
  
      # resize image if it's too large
      image_for_decode = copy.deepcopy(image)
      i = Image.open(image_for_decode)
      w, h = i.size
  
      # optimal size of one side of image for decoding
      opt_size = 600
      if w > opt_size  or h > opt_size: 
          start_time_resizing = time.time()
          side = w if w > h else h
          scale_factor = opt_size / side
          image_resized = i.resize((int(w*scale_factor), int(h*scale_factor)),)
          img_byte_arr = io.BytesIO()
          image_resized.save(img_byte_arr, format='PNG')
          img = cv2.imdecode(np.frombuffer(img_byte_arr.getvalue(), np.uint8), 1)
          # TODO: add logger maybe?
          print(f'Resizing time: { time.time() - start_time_resizing } sec')
      else:
          image = image.file
          img = cv2.imdecode(np.frombuffer(image.read(), np.uint8), 1)
     
      gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
      ret,thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
      result: list = pylibdmtx.decode(thresh)
  
      detected_puids = []
      for i in result:
          puid = i.data.decode("utf-8") 
          detected_puids.append(puid)
  
      # remove duplicates
      detected_puids = list(set(detected_puids))
  
      # TODO: add logger maybe?
      print(f'PUID decoding image ({w}x{h}) time: { time.time() - start_time_decoding } sec')
  
      return detected_puids
code_tokens: def detect_data_matrix ( image ) -> list : start_time_decoding = time . time ( ) image_for_decode = copy . deepcopy ( image ) i = Image . open ( image_for_decode ) w , h = i . size opt_size = 600 if w > opt_size or h > opt_size : start_time_resizing = time . time ( ) side = w if w > h else h scale_factor = opt_size / side image_resized = i . resize ( ( int ( w * scale_factor ) , int ( h * scale_factor ) ) , ) img_byte_arr = io . BytesIO ( ) image_resized . save ( img_byte_arr , format = 'PNG' ) img = cv2 . imdecode ( np . frombuffer ( img_byte_arr . getvalue ( ) , np . uint8 ) , 1 ) print ( f'Resizing time: { time.time() - start_time_resizing } sec' ) else : image = image . file img = cv2 . imdecode ( np . frombuffer ( image . read ( ) , np . uint8 ) , 1 ) gray = cv2 . cvtColor ( img , cv2 . COLOR_BGR2GRAY ) ret , thresh = cv2 . threshold ( gray , 0 , 255 , cv2 . THRESH_BINARY | cv2 . THRESH_OTSU ) result : list = pylibdmtx . decode ( thresh ) detected_puids = [ ] for i in result : puid = i . data . decode ( "utf-8" ) detected_puids . append ( puid ) detected_puids = list ( set ( detected_puids ) ) print ( f'PUID decoding image ({w}x{h}) time: { time.time() - start_time_decoding } sec' ) return detected_puids
short_docstring: Detects PUIDs on image.
short_docstring_tokens: Detects PUIDs on image .
comment: """Detects PUIDs on image. Return list of PUIDs or empty list"""$SEP$# resize image if it's too large$SEP$# optimal size of one side of image for decoding$SEP$# TODO: add logger maybe?$SEP$# remove duplicates$SEP$# TODO: add logger maybe?