我想实现这一目标:
。
到目前为止,我的应用程序能够接收用户的出勤率,但即使我使用了return (),也不会将其呈现到考勤详细信息页面。它将留在主页上,摄像头仍在运行。有什么办法可以解决这个问题吗?还是我搞错了?我已经尝试过像这样手动更改请求细节,但是它不起作用。
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
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
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,但是保持视频流运行,就像在循环中一样,这样我就可以用Javascript从Attendance.html重定向到摄像头页面,并且仍然运行视频流。抱歉没说清楚。
发布于 2021-10-20 04:59:19
哦..。为什么我没注意到..。
问题是:
...
if foundFace==True:
takeAttendance(request,names)
...是的,您执行在getFace中返回呈现输出的函数。仅此而已,getFace根本不使用返回值。
正确的代码应该是:
...
if foundFace==True:
returned_render = takeAttendance(request,names)
return returned_render
...或者简单地说:
...
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)
...https://stackoverflow.com/questions/69551660
复制相似问题