首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Django中从StreamingHttpResponse重定向

如何在Django中从StreamingHttpResponse重定向
EN

Stack Overflow用户
提问于 2021-10-13 07:54:08
回答 1查看 338关注 0票数 0

我想实现这一目标:

  1. 用户的脸是通过摄像头在主页
  2. 中检测到的,应用程序获取用户的出勤率,用考勤细节
  3. 显示网页考勤细节页面将在几秒钟后重定向回主页,摄像头仍在运行

到目前为止,我的应用程序能够接收用户的出勤率,但即使我使用了return (),也不会将其呈现到考勤详细信息页面。它将留在主页上,摄像头仍在运行。有什么办法可以解决这个问题吗?还是我搞错了?我已经尝试过像这样手动更改请求细节,但是它不起作用。

代码语言:javascript
复制
request.resolver_match = resolve('/takeAttendance/')
        request.path='/takeAttendance/'
        request.path_info='/takeAttendance/'

一个类似于How to redirect to another url after detect face in django的问题,但没有一个答案对我有用。所涉及的代码如下:

views.py

代码语言:javascript
复制
from django.shortcuts import render, redirect
from django.contrib import messages
from django.http import HttpResponse , StreamingHttpResponse
from datetime import datetime, date
import cv2
import face_recognition
import numpy as np
import threading

foundFace = False
vs = cv2.videoCapture(0)
lock = threading.Lock()
frame = None

def videoFeed(request):
        return StreamingHttpResponse(getFace(request),content_type="multipart/x-mixed-replace;boundary=frame")

def getFace(request):
    global vs,outputFrame,lock,foundFace
    known_face_names,known_face_encodings = getFiles() # get the image files from my project directory
    face_location = []
    face_encoding = []
    while foundFace==False:
        check,frame = vs.read()
        small_frame = cv2.resize(frame,(0,0),fx=0.5,fy=0.5)
        face_roi = small_frame[:,:,::-1]
        face_location = face_recognition.face_locations(face_roi)
        face_encoding = face_recognition.face_encodings(face_roi,face_location)
        face_names = []
        names=[]
        for encoding in face_encoding:
            matches = face_recognition.compare_faces(known_face_encodings,np.array(encoding),tolerance=0.6)
            distances = face_recognition.face_distance(known_face_encodings,encoding)
            matches = face_recognition.compare_faces(known_face_encodings,np.array(encoding),tolerance=0.6)
            distances = face_recognition.face_distance(known_face_encodings,encoding)
            best_match_index = np.argmin(distances) 

            if matches[best_match_index]:
                name = known_face_names[best_match_index]
                face_names.append(name)
                if name not in names:
                    names.append(name)
        #process the frame (add text and rectangle, add the name of the identified user to names)
        with lock:
            (flag,encodedImg) = cv2.imencode(".jpg",frame)
            
        if len(names)!=0:
            foundFace=True
        if foundFace==True:
            takeAttendance(request,names)
            foundFace==False
        yield(b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + 
            bytearray(encodedImg) + b'\r\n')

def takeAttendance(request,names):
    context={}
    if request.method=='GET':
        if user_id in names:
            attendance = Attendance(user_ID = str(user_id),
                                            date_in = date.today(),
                                            time_in = datetime.now())
            attendance.save()
            context={'attendance':attendance}
            messages.success(request,'Check in successful')
            return render(request,'Attendance/attendance.html',context)
        else:
            messages.error(request,'Check in failed')
            return redirect('home')
    else:
        return redirect('home')

urls.py

代码语言:javascript
复制
from django.urls import path
from . import views
urlpatterns=[
    path('home/',views.home,name='home'),
    path('takeAttendance/',views.takeAttendance,name='takeAttendance'),
    path('videoFeed/',views.videoFeed,name='videoFeed'),

]

我正在使用Django 3.1,我对它非常陌生,谢谢!

编辑

实际上,我想重定向到Attendance.html,但是保持视频流运行,就像在循环中一样,这样我就可以用JavascriptAttendance.html重定向到摄像头页面,并且仍然运行视频流。抱歉没说清楚。

EN

回答 1

Stack Overflow用户

发布于 2021-10-20 04:59:19

哦..。为什么我没注意到..。

问题是:

代码语言:javascript
复制
...
        if foundFace==True:
            takeAttendance(request,names)
...

是的,您执行在getFace中返回呈现输出的函数。仅此而已,getFace根本不使用返回值。

正确的代码应该是:

代码语言:javascript
复制
...
        if foundFace==True:
            returned_render = takeAttendance(request,names)
            return returned_render
...

或者简单地说:

代码语言:javascript
复制
...
        if foundFace==True:
            return takeAttendance(request,names)
            # or maybe you should use yield instead of return?
            # I don't know. Check both
            yield takeAttendance(request,names)
...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69551660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档