如何在django中使用网络摄像头读取条形码



我正在做一个项目,试图在django中打开一个网络摄像头作为代码读取器。我遵循了这个教程:;https://www.youtube.com/watch?v=xz9MvyKGYio&list=WL&索引=24&t=12s";如何打开django的摄像头。我想让网络摄像头读取条形码,并将此代码重定向到另一个功能。但我不知道如何打破网络摄像头和重定向时,代码是存在的。这就是我的观点

from django.shortcuts import render
import cv2
import threading
from django.http import StreamingHttpResponse, HttpResponse
from django.views.decorators import gzip
from pyzbar.pyzbar import decode
import time
from django.shortcuts import redirect
def index(request):
return render(request, 'webcam/home.html')
# Create your views here.
@gzip.gzip_page
def home(request):
#https://www.youtube.com/watch?v=xz9MvyKGYio&list=WL&index=23&t=12s
try:
cam = VideoCamera()
return StreamingHttpResponse(gen(cam), content_type="multipart/x-mixed-replace;boundary=frame")
except:
pass
return render(request, 'app1.html')
def helloView(request, code):
http = f"Hello {code}"
return HttpResponse(http)
#to capture video class
class VideoCamera(object):
def __init__(self):
self.video = cv2.VideoCapture(0)
(self.grabbed, self.frame ) = self.video.read()
threading.Thread(target=self.update, args=()).start()
def __del__(self):
self.video.release()
def get_frame(self):
image = self.frame
_, jpeg = cv2.imencode('.jpg', image)
################################
# capture code /simple example / 
n_1 = '9788366384088'
for code in decode(image):
n = str(code.data.decode('utf-8'))
if n_1 == n:
print(n)
time.sleep(5)
# how to redirect to helloView?
###########################
return jpeg.tobytes()
def update(self):
while True:
(self.grabbed, self.frame) = self.video.read()
def gen(camera):
while True:
frame = camera.get_frame()
yield (b'--framern'
b'Content-Type: image/jpegrnrn' + frame + b'rnrn')

在VideoCamera.get_frame中,我添加了一个简单的例子,我尝试捕获n_1代码,但无法将该代码重定向到helloView。长话短说:我打开了有实时网络摄像头的浏览器->我扫描ean代码->如果ean代码正确,重定向到helloView((有可能吗?我应该在哪里捕获ean代码?

您不能在前端运行python代码。您需要使用Javascript加载相机并将图片传输到后端。然后可以使用openCV提取代码。

了解webrtc(https://webrtc.org/)访问前端的摄像头。

最新更新