/ aegisvision / test2.py
swadhinbiswas have to fix bug · 5/3/2024
f5f0447
121 lines · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

import cv2
import streamlit as st
from fastapi import FastAPI
import face_recognition
import numpy as np
import time
import os
from fastapi.responses import StreamingResponse
app = FastAPI()
from face_recognition import face_locations, face_encodings
from firebase_admin import db ,credentials

import firebase_admin


imagepath="/home/swadhin/GitHubproject/liveattendence_with_anti_cheat/backend/app/functions/pictures"
piclepath="/home/swadhin/GitHubproject/liveattendence_with_anti_cheat/backend/app/functions/encodings.pickle"

firbaseurl="https://database-faceapp-default-rtdb.asia-southeast1.firebasedatabase.app/"
servicekey="/home/swadhin/GitHubproject/liveattendence_with_anti_cheat/backend/app/settings/serviceAccountKey.json"


cred=credentials.Certificate(servicekey)

firebase_admin.initialize_app(cred,{
  'databaseURL': f'{firbaseurl}',
  'storageBucket': 'database-faceapp.appspot.com',
})

path="/home/swadhin/GitHubproject/liveattendence_with_anti_cheat/backend/app/functions/pictures/222156259.jpeg"
img=cv2.imread(path)
encodex=face_encodings(img)[0]
print(encodex)

@st.cache_resource
def get_cap():
    return 0

def detect_and_recognize_face(frame,encode):
    # Convert frame to RGB for face recognition
    
    cut_frame=cv2.resize(frame,(0,0),None,0.25,0.25)
    rgb_frame = cv2.cvtColor(cut_frame, cv2.COLOR_BGR2RGB)
    frameencode=face_encodings(rgb_frame)
    
    
    

    # Find all faces and their encodings
    face_locations_list = face_locations(rgb_frame)
    currentfaceencode = face_encodings(frameencode, face_locations_list)
    
    if face_locations_list:

    # Loop through each detected face
      for face_encoding, face_location in zip(currentfaceencode, face_locations_list):
          # Compare face encoding with encodings in database/pickle file
          matches = face_recognition.compare_faces(encode, face_encoding)
          facedistance=face_recognition.face_distance(encode,face_encoding)
          print(facedistance)
          name = "Unknown"
          matchindex=np.argmin(facedistance)
          print(matchindex)
          
          # Draw rectangle and name around the face
          top, right, bottom, left = face_location
          cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
          cv2.putText(frame, name, (left + 6, top - 6), cv2.FONT_HERSHEY_DUPLEX, 0.8, (255, 255, 255), 1)

    return frame

def generate_frames():
  cap=cv2.VideoCapture("https://192.168.0.100:8080/video")
  cap.set(3,640)
  cap.set(4,480)
  while True:
        
        success, frame = cap.read()  # read the camera frame
        
        frame=detect_and_recognize_face(frame,encode=encodex)
        
        if not success:
              break
        ret, buffer = cv2.imencode('.jpg', frame)
        frame = buffer.tobytes()
        yield (b'--frame\r\n'
                  b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
        
        # save_image=cv2.imwrite(os.path.join(/home/swadhin/GitHubproject/liveattendence_with_anti_cheat/backend/app/functions/screens)"test.jpg",x)
        
      
        
        






# def video_feed():
#     cap = get_cap()  # 0 for default webcam
#     return StreamingResponse(generate_frames(cap), media_type="multipart/x-mixed-replace; boundary=frame")

@app.get("/")
async def home():
    return {"message":"Welcome to the Live Attendance System"}
  

@app.get("/video_feed")
async def video_feed():
    return StreamingResponse(generate_frames(), media_type="multipart/x-mixed-replace;boundary=frame")
  
def main():
  app.run()
  
if __name__ == '__main__':
    import uvicorn
    
    uvicorn.run(app, host="localhost", port=8000)