bug fix

swadhinbiswas committed 2 years ago
1e90d79
1 parent 6e8251e
commit 1e90d79
9 files changed
+164 -70
▼backend/app/admin/handelars.py
+35-14
4
4
from app.settings.connectdb import Firebase
5
5
from typing import Dict,Any,Optional
6
6
from datetime import datetime
7
+from app.settings.connectdb import Firebase
7
8
from app.functions.encodeGenarator import EndcodeGenarator
9
+from app.admin.uploadphotoandencode import Encoding
10
+from app.admin.imagepath import imagepaths
11
+import asyncio
12
+
8
13
9
14
10
15
db_add=APIRouter()
11
16
db=Firebase()
12
-imagepath=EndcodeGenarator().imagepath
17
+
13
18
picklepath=EndcodeGenarator().picklepath
14
19
20
+async def uploadimage(filename:str,img):
21
+ try:
22
+ content = await img.read()
23
+ x=f"{imagepaths}/{filename}"
24
+ print(x)
25
+ with open(x, "wb+") as file:
26
+ file.write(content)
27
+ await asyncio.sleep(3)
28
+ Firebase.upload_picture(x)
29
+
30
+
31
+
32
+ except Exception as e:
33
+ return {"message":str(e)}
15
34
16
35
@db_add.post("/create")
17
36
async def add_to_db(Student:StudentModelshema):
26
45
"Section": Student.Section,
27
46
"Total_Attendance": Student.Total_Attendance,
28
47
"last_seen": f"{datetime.now().strftime('%d-%m-%Y %H:%M:%S')}",
29
- }
48
+ }
30
49
}
31
50
x=db.set_data(data)
51
+
32
52
return x
33
53
54
+@db_add.post("/uploadphoto")
55
+async def upload_photo(id:str,file:UploadFile=File(...)):
56
+
57
+ tempfilename=file.filename
58
+ x=tempfilename.split(".")[1]
59
+ studentfilename=f"{id}.{x}"
60
+ await asyncio.sleep(3)
61
+ await uploadimage(studentfilename,file)
62
+ y=Encoding(studentfilename,imagepaths)
63
+ y.encodings()
64
+ return {"message":"Image Uploaded Successfully & Encoded"}
65
+
34
66
@db_add.get("/getstudents")
35
67
async def get_from_db():
36
68
data=db.get_data()
69
101
data=db.get_data_by_child_limit("Total_Attendance",limit)
70
102
return data
71
103
72
-@db_add.get("/uploadimage")
73
-async def uploadimage(img:UploadFile=File(...)):
74
- try:
75
- content = await img.read()
76
- with open(imagepath, "wb") as file:
77
- file.write(content)
78
- return {"message":"Image uploaded"}
79
-
80
- except Exception as e:
81
- return {"message":str(e)}
82
- finally:
83
- file.close()
104
+
84
105
85
106
86
107
87
108
▼backend/app/admin/imagepath.py
+6-0
1
+import os
2
+
3
+imagepaths=os.path.join(os.path.dirname(__file__), 'photos')
4
+
5
+
6
+
0
7
▼backend/app/admin/photos/222211111.png
+0-0
Binary file not shown.
▼backend/app/admin/uploadphotoandencode.py
+48-0
1
+from face_recognition import face_encodings, load_image_file
2
+from app.settings.connectdb import Firebase
3
+from app.admin.imagepath import imagepaths
4
+import cv2
5
+import os
6
+import pickle
7
+from app.picklepath import picklepathcode
8
+
9
+class Encoding:
10
+ def __init__(self,id,path):
11
+ self.known_face_encodings = []
12
+ self.known_face_names = []
13
+ self.picklepathcode=picklepathcode
14
+ self.imagepath=imagepaths
15
+ self.id=id
16
+ self.ref=Firebase().ref
17
+ self.bucket=Firebase().bucket
18
+ self.idsref=Firebase().idsref
19
+ self.known_face_encodings = []
20
+ self.studentids = []
21
+ self.encodingslist = []
22
+
23
+ def encodings(self):
24
+ img=f"{self.imagepath}/{self.id}"
25
+ try:
26
+ studentid=img.split(".")[0]
27
+ if not self.ref.child(studentid).get():
28
+ cv2img=cv2.imread(f"{self.imagepath}/{img}")
29
+ img=cv2.cvtColor(cv2img,cv2.COLOR_BGR2RGB)
30
+ encode=face_encodings(img)[0]
31
+ self.known_face_encodings.append(encode)
32
+ self.known_face_names.append(studentid)
33
+ self.idsref.child(studentid).set(encode)
34
+ encodeKnowlist=[self.known_face_encodings,self.known_face_names]
35
+ file=open(self.picklepathcode,"wb")
36
+ pickle.dump(encodeKnowlist,file)
37
+ file.close()
38
+ except Exception as e:
39
+ return {"message":str(e)}
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
0
49
▼backend/app/models/studentmodel.py
+2-0
1
1
from pydantic import BaseModel
2
+from fastapi import UploadFile,File
2
3
3
4
class StudentModelshema(BaseModel):
4
5
name: str
9
10
Section: str
10
11
Total_Attendance: str
11
12
id: str
13
+ file: UploadFile=File(...)
12
14
13
15
▼backend/app/picklepath.py
+3-0
1
+import os
2
+
3
+picklepathcode=os.path.join(os.path.dirname(__file__), "/functions/encodings.pickle")
0
4
▼backend/app/settings/connectdb.py
+6-1
64
64
def get_data_by_present(self)->Dict[str,Any]:
65
65
return self.ref.order_by_child("status").equal_to("Present").get()
66
66
67
-
67
+ def upload_picture(self,img:str)->Dict[str,Any]:
68
+ bucket=storage.bucket()
69
+ blob=bucket.blob(f"faceappimage/{img}")
70
+ blob.upload_from_filename(f"img")
71
+
72
+ return {"message":"Image Uploaded Successfully"}
68
73
def get_picture(self,img:str)->Dict[str,Any]:
69
74
bucket=storage.bucket()
70
75
return bucket.get_blob(f"faceappimage/{img}")
71
76
▼backend/path.py
+0-0
0
0
▼test3.py
+64-55
1
-import cv2
2
-import time
3
-import numpy as np
4
-
5
-import pickle
6
-from face_recognition import face_locations, face_encodings, compare_faces, face_distance
7
-
8
-folderpath="/home/swadhin/GitHubproject/liveattendence_with_anti_cheat/backend/app/functions/pictures/222156259.jpeg"
9
-
10
-pic=cv2.imread(folderpath)
11
-pic=cv2.resize(pic,(0,0),None,0.25,0.25)
12
-encode=face_encodings(pic)[0]
13
-studentIds = ['222156259']
14
-encodelist=[]
15
-encodelist.append(encode)
16
-incodelistnown=[encodelist,studentIds]
17
-file=open('EncodeFile.p','wb')
18
-pickle.dump(incodelistnown,file)
19
-file.close()
20
-
21
-
22
-cap = cv2.VideoCapture("https://192.168.0.100:8080/video")
23
-cap.set(3, 640)
24
-cap.set(4, 480)
25
-
26
-file = open('EncodeFile.p', 'rb')
27
-encodeListKnownWithIds = pickle.load(file)
28
-
29
-encodeListKnown, studentIds = encodeListKnownWithIds
30
-
31
-
32
-while True:
33
- success, img = cap.read()
34
- imgS = cv2.resize(img, (0, 0), None, 0.25, 0.25)
35
- imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
36
- faceCurFrame = face_locations(imgS)
37
- encodeCurFrame =face_encodings(imgS, faceCurFrame)
38
- if faceCurFrame:
39
- for encodeFace, faceLoc in zip(encodeCurFrame, faceCurFrame):
40
- matches = compare_faces(encodeListKnown, encodeFace)
41
- faceDis = face_distance(encodeListKnown, encodeFace)
42
- matchIndex = np.argmin(faceDis)
43
- if matches[matchIndex]:
44
- y1, x2, y2, x1 = faceLoc
45
- y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
46
- bbox = 55 + x1, 162 + y1, x2 - x1, y2 - y1
47
- cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
48
- cv2.rectangle(img, (x1, y2 - 35), (x2, y2), (0, 255, 0), cv2.FILLED)
49
- cv2.putText(img, studentIds[matchIndex], (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
50
- cv2.imshow('Webcam', img)
51
- if cv2.waitKey(1) & 0xFF == ord('q'):
52
- break
53
-cap.release()
54
-
55
-cv2.destroyAllWindows()
1
+# import cv2
2
+# import time
3
+# import numpy as np
4
+
5
+# import pickle
6
+# from face_recognition import face_locations, face_encodings, compare_faces, face_distance
7
+
8
+# folderpath="/home/swadhin/GitHubproject/liveattendence_with_anti_cheat/backend/app/functions/pictures/222156259.jpeg"
9
+
10
+# pic=cv2.imread(folderpath)
11
+# pic=cv2.resize(pic,(0,0),None,0.25,0.25)
12
+# encode=face_encodings(pic)[0]
13
+# studentIds = ['222156259']
14
+# encodelist=[]
15
+# encodelist.append(encode)
16
+# incodelistnown=[encodelist,studentIds]
17
+# file=open('EncodeFile.p','wb')
18
+# pickle.dump(incodelistnown,file)
19
+# file.close()
20
+
21
+
22
+# cap = cv2.VideoCapture("https://192.168.0.100:8080/video")
23
+# cap.set(3, 640)
24
+# cap.set(4, 480)
25
+
26
+# file = open('EncodeFile.p', 'rb')
27
+# encodeListKnownWithIds = pickle.load(file)
28
+
29
+# encodeListKnown, studentIds = encodeListKnownWithIds
30
+
31
+
32
+# while True:
33
+# success, img = cap.read()
34
+# imgS = cv2.resize(img, (0, 0), None, 0.25, 0.25)
35
+# imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
36
+# faceCurFrame = face_locations(imgS)
37
+# encodeCurFrame =face_encodings(imgS, faceCurFrame)
38
+# if faceCurFrame:
39
+# for encodeFace, faceLoc in zip(encodeCurFrame, faceCurFrame):
40
+# matches = compare_faces(encodeListKnown, encodeFace)
41
+# faceDis = face_distance(encodeListKnown, encodeFace)
42
+# matchIndex = np.argmin(faceDis)
43
+# if matches[matchIndex]:
44
+# y1, x2, y2, x1 = faceLoc
45
+# y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
46
+# bbox = 55 + x1, 162 + y1, x2 - x1, y2 - y1
47
+# cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
48
+# cv2.rectangle(img, (x1, y2 - 35), (x2, y2), (0, 255, 0), cv2.FILLED)
49
+# cv2.putText(img, studentIds[matchIndex], (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
50
+# cv2.imshow('Webcam', img)
51
+# if cv2.waitKey(1) & 0xFF == ord('q'):
52
+# break
53
+# cap.release()
54
+
55
+# cv2.destroyAllWindows()
56
+
57
+
58
+
59
+test={
60
+ "hello":"world",
61
+}
62
+x=list(test.keys())[0]
63
+print(x)
64
+
56
65
57
66
58
67
59
68