【问题标题】:How do I make Open CV work on deployed web app如何使 Open CV 在已部署的 Web 应用程序上工作
【发布时间】:2019-11-04 11:53:14
【问题描述】:

我有一个可以在本地运行的应用程序,让它打开我的网络摄像头,然后检测二维码。当我在本地运行应用程序时它运行良好,但后来我在我的 Web 服务器上使用了该应用程序,它得到了服务器错误 (500)。

该应用是在 Django 上制作的,然后部署时我使用了数字海洋(ubuntu),我还使用了 nginx 和 gunicorn。

html

<li style= "margin: auto; width:auto"><a style="color:white" href="{% url 'attendees:qrscanner' %}" target="_blank"><button style="margin:15px; width:200px" class="btn btn-outline-primary">Check QR Scanner</button></a></li>

views.py

from django.shortcuts import get_object_or_404, render, redirect
from django.utils import timezone
from django.db.models import Sum
from .models import Attendee, ActivityLog

import re, qrcode, cv2, csv, io, os
import numpy as np
import pyzbar.pyzbar as pyzbar

def qrscanner(request):
    cap = cv2.VideoCapture(0)
    font = cv2.FONT_HERSHEY_PLAIN

    frame_width = int(cap.get(3))
    frame_height = int(cap.get(4))

    aid = None

    while aid is None:
        _, frame = cap.read()

        decodedObjects = pyzbar.decode(frame)

        for obj in decodedObjects:
            aid = re.findall('/attendees/confirmation/([0-9]+)', str(obj.data))
            try:
                attendee = Attendee.objects.get(pk=str(aid[0]))
                attendee.present = True
                attendee.timestamp = timezone.now()
                attendee.activity.update_or_create(time_log=attendee.timestamp)
                attendee.save()
                context = {
                    'scan': 'QR Successfully Scanned',
                    'attendee': attendee,
                }
            except:
                context = {
                    'noscan': 'QR Scan Unsuccessful'
                }


        cv2.imshow("QR Reader", frame)

        if aid is not None:
            cv2.destroyAllWindows()
            break

        key = cv2.waitKey(1) & 0xFF
        if key == 27:
            context = {
                'noscan': 'No QR Scanned',
            }
            cv2.destroyAllWindows()
            break

    return render(request, 'attendees/qrscanner.html', context)

预期结果: - 就像我的应用程序在本地运行时一样,我转到包含该应用程序的站点,单击 qr 扫描仪按钮,“cap = cv2.VideoCapture(0)”使用我的计算机摄像头打开一个框架以检测 qr。

实际结果: -本地一切都很好,我单击 qr 扫描仪按钮,“cap = cv2.VideoCapture(0)”使用我的计算机摄像头打开一个框架来检测 qr。 但是在我的网站上,一旦我访问应用程序的该部分,我就会收到服务器错误 (500)。

注意: - 我必须通过 pip 为在线应用程序安装一些附加文件,才能让应用程序显示一些 html,并且我在网站上有其他应用程序。 本地安装的库(pip freeze):

colorama==0.4.1
dj-database-url==0.5.0
Django==2.2.3
django-qr-code==1.0.0
django-widget-tweaks==1.4.5
docutils==0.15.2
mysqlclient==1.4.2
numpy==1.17.0
opencv-python==4.1.0.25
pathlib==1.0.1
Pillow==6.0.0
python-decouple==3.1
pytz==2019.1
pyzbar==0.1.8
qrcode==6.1
six==1.12.0
sqlparse==0.3.0

网站中安装的库(pip freeze):

beautifulsoup4==4.8.0
certifi==2019.9.11
chardet==3.0.4
cycler==0.10.0
demjson==2.2.4
Django==2.2.6
django-authentication==3.2
django-qr==2.0
django-realestate==4.0
django-widget-tweaks==1.4.5
gunicorn==19.9.0
idna==2.8
jtutils==0.0.6
kiwisolver==1.1.0
leven==1.0.4
matplotlib==3.1.1
nose==1.3.7
numpy==1.17.2
opencv-python==4.1.1.26
opencv-python-headless==4.1.1.26
pandas==0.25.1
Pillow==6.2.0
pkg-resources==0.0.0
psycopg2==2.8.3
psycopg2-binary==2.8.3
pyparsing==2.4.2
python-csv==0.0.11
python-dateutil==2.8.0
pytz==2019.2
pyzbar==0.1.8
qrcode==6.1
requests==2.22.0
six==1.12.0
soupsieve==1.9.4
sqlparse==0.3.0
urllib3==1.25.6
xlrd==1.2.0
xmltodict==0.12.0

【问题讨论】:

    标签: python django opencv


    【解决方案1】:

    问题是 cap = cv2.VideoCapture(0) 实际上是在尝试打开您将应用程序部署到的服务器上的网络摄像头,而不是您计算机上的网络摄像头。

    【讨论】:

    • 那么 OP 如何从客户端屏幕捕获它,只需告诉他如何完成以及他需要遵循的程序即可。否则,由于这不是答案,请尝试将其作为评论发布。
    • 没有足够的评论声誉......但我认为没有办法使用 OpenCV 打开客户端网络摄像头
    • 有没有其他方法可以让相同的功能发挥作用?打开用户网络摄像头,创建一个显示摄像头视图的框架,并从该框架中读取二维码。
    • @StormLamas 如果用户允许,可能只有页面上的 JavaScript 可以访问相机。 davidwalsh.name/browser-camera
    • @furas 对 JS 还不太熟悉。希望有人可以提供一些简短的 JS 脚本(如果可能的话),我可以将它们插入到应用程序中。
    【解决方案2】:

    Here 是实际的 JavaScript 指南

    Git Source

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-09
      • 2012-10-13
      • 1970-01-01
      • 2013-08-01
      • 2021-06-12
      相关资源
      最近更新 更多