【问题标题】:Is there a way to scan QR codes in colab?有没有办法在colab中扫描二维码?
【发布时间】:2021-02-28 08:35:06
【问题描述】:

我有一个 colab 笔记本,一直在尝试寻找一种方法来扫描我的网络摄像头上的 QR 码,但没有结果。如果实时 QR 码检测存在问题,我有捕获图像的代码(我从另一个笔记本中将其 yoinked,这就是为什么它有点奇怪):

from IPython.display import display, Javascript
from google.colab.output import eval_js
from base64 import b64decode

def take_photo(filename='photo.jpg', quality=0.8):
  js = Javascript('''
    async function takePhoto(quality) {
      const div = document.createElement('div');
      const capture = document.createElement('button');
      capture.textContent = 'Capture';
      div.appendChild(capture);

      const video = document.createElement('video');
      video.style.display = 'block';
      const stream = await navigator.mediaDevices.getUserMedia({video: true});

      document.body.appendChild(div);
      div.appendChild(video);
      video.srcObject = stream;
      await video.play();

      // Resize the output to fit the video element.
      google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);

      // Wait for Capture to be clicked.
      await new Promise((resolve) => capture.onclick = resolve);

      const canvas = document.createElement('canvas');
      canvas.width = video.videoWidth;
      canvas.height = video.videoHeight;
      canvas.getContext('2d').drawImage(video, 0, 0);
      stream.getVideoTracks()[0].stop();
      div.remove();
      return canvas.toDataURL('image/jpeg', quality);
    }
    ''')
  display(js)
  data = eval_js('takePhoto({})'.format(quality))
  binary = b64decode(data.split(',')[1])
  with open(filename, 'wb') as f:
    f.write(binary)
  return filename

from IPython.display import Image
try:
  filename = take_photo()
  print('Saved to {}'.format(filename))
  
  # Show the image which was just taken.
  display(Image(filename))
except Exception as err:
  # Errors will be thrown if the user does not have a webcam or if they do not
  # grant the page permission to access it.
  print(str(err))

我已经尝试了 pyzbar(跟随教程)和许多其他方法,但它们似乎都不适合我。

最终目标是从二维码中获取数据,并将其附加到我将转换为 .csv 的列表中(所有数据都来自二维码,如“姓名、电子邮件、电话#”)。让它与实时摄像头一起使用会很棒,所以它每次看到二维码时都会自动执行此操作。有没有办法做到这一点?

感谢您的帮助!

【问题讨论】:

    标签: python google-colaboratory


    【解决方案1】:

    嗨,你可以使用这个 pyzbar

    from pyzbar.pyzbar import decode
    from PIL import Image
    decode(Image.open('pyzbar/tests/code128.png'))
    

    【讨论】:

    • tysm 它打印这个:[Decoded(data=b'name namer,test@email.com,123456', type='QRCODE', rect=Rect(left=176, top=174, width=178, height=187), polygon=[Point(x=176, y=344), Point(x=345, y=361), Point(x=354, y=180), Point(x=181, y=174)])] 我看到它打印在那里但是我怎样才能得到返回的数据?还有没有办法输入实时提要而不是图片?
    【解决方案2】:

    编写安装:

    pip install pyzbar[scripts] or !pip install pyzbar[scripts]
    !apt install libzbar0
    

    稍后导入一些,但主要是pyzbar:

    import shutil
    import os
    import random
    import re
    import cv2 
    import numpy as np
    import pytesseract
    from pytesseract import Output
    %matplotlib inline
    import matplotlib as mpl
    import matplotlib.pyplot as plt
    import matplotlib.pylab as pylab
    import glob
    import pyzbar.pyzbar
    from PIL import Image
    

    然后检查一下:

    from pyzbar.pyzbar import decode
    from PIL import Image
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多