【发布时间】: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