Django - OpenCV Отображение вывода в HTML

Итак, я делаю систему учета посещаемости с распознаванием лиц на Django. Сейчас мой код открывает окно python, определяет лицо и записывает имя, дату и время в CSV файл. Я хочу отображать имя, время и дату в HTML после обнаружения лица. Как я могу это сделать?

views.py

from django.shortcuts import render
import cv2
import numpy as np
import face_recognition
import os 
from datetime import datetime
from django.core.files import File
from django.template.loader import render_to_string

path = 'attendancesystem/file/faces'
images = []
className = []

imgList = os.listdir(path)
print(imgList)

#read image from images folder
for cl in imgList:
    curImg = cv2.imread(f'{path}/{cl}')
    images.append(curImg)
    className.append(os.path.splitext(cl)[0])
print(className)

Это функция для отметки присутствия после обнаружения лица.

#mark attendance function
def markAttendance(name):
    with open('attendancesystem/static/attendancesystem/Attendance.csv', 'r+') as f:
        attendList = f.readlines()
        nameList = []

        for line in attendList:
            entry = line.split(',')
            nameList.append(entry[0])
        
        if name not in nameList:
            now = datetime.now()
            dateString = now.strftime('%d/%m/%Y')
            timeString = now.strftime('%H:%M:%S')
            f.writelines(f'\n{name},{dateString},{timeString}')

Это функция для кодирования изображения, которое вставляется в папку faces.

#encode image
def findEncoding(images):
    encodeList = []
    for img in images:
        img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
        encode = face_recognition.face_encodings(img)[0]
        encodeList.append(encode)
    return encodeList

encodeListKnown = findEncoding(images)
print("Encoding Complete")

Эта функция откроет камеру и обнаружит лицо.

def camera(request):

    camera = cv2.VideoCapture(1)
 
    while True:
        return_value,frame = camera.read()
        frameS = cv2.resize(frame,(0,0),None,0.25,0.25)
        frameS = cv2.cvtColor(frameS,cv2.COLOR_BGR2RGB)
 
        faceCurFrame = face_recognition.face_locations(frameS)
        encodeCurFrame = face_recognition.face_encodings(frameS,faceCurFrame)

        for encodeFace,faceLoc in zip(encodeCurFrame,faceCurFrame):
            matches = face_recognition.compare_faces(encodeListKnown,encodeFace)
            faceDis = face_recognition.face_distance(encodeListKnown,encodeFace)
            matchIndex = np.argmin(faceDis)

            if matches[matchIndex]:
                name = className[matchIndex].upper()
                y1,x2,y2,x1 = faceLoc
                y1,x2,y2,x1 = y1*4,x2*4,y2*4,x1*4
                cv2.rectangle(frame,(x1,y1),(x2,y2),(0,255,0),2)
                cv2.rectangle(frame,(x1,y2-35),(x2,y2),(0,255,0),cv2.FILLED)
                cv2.putText(frame,name,(x1+6,y2-6),cv2.FONT_HERSHEY_COMPLEX,1,(255,255,255),2)
                markAttendance(name)

        cv2.imshow('camera',frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    return render(request,'face_recog/index.html')
Вернуться на верх