J007acky commited on
Commit
6ba01ac
·
verified ·
1 Parent(s): 0676242

Update FastAPI.py

Browse files
Files changed (1) hide show
  1. FastAPI.py +73 -4
FastAPI.py CHANGED
@@ -39,10 +39,18 @@ class ImgInput(BaseModel):
39
  class ImgOutput(BaseModel):
40
  user_id: list
41
 
42
-
43
  class UserSaved(BaseModel):
44
  status: str
45
 
 
 
 
 
 
 
 
 
 
46
 
47
  def recognize_face(image_url: HttpUrl) -> ImgOutput:
48
 
@@ -108,6 +116,8 @@ def add_face(image_url: HttpUrl,user_name : str):
108
  rgb_img = cv2.cvtColor(new_image, cv2.COLOR_BGR2RGB)
109
 
110
  encode = face_recognition.face_encodings(new_image)[0]
 
 
111
  face_encodings.append(encode)
112
  labels.append(user_name)
113
 
@@ -122,6 +132,54 @@ def add_face(image_url: HttpUrl,user_name : str):
122
  # Upload the pickle file to Firebase Storage
123
  pkl_blob = storage.child(f"{storage_folder}pkl/face_encodings.pkl")
124
  pkl_blob.put("face_encodings.pkl")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
 
127
  @app.post('/')
@@ -132,6 +190,17 @@ async def scoring_endpoint(item:ImgInput):
132
 
133
  @app.post('/user/')
134
  async def scoring_endpoint(item:ImgSave):
135
- add_face(item.image_url, item.user_name)
136
- results = "User Saved"
137
- return UserSaved(status=results)
 
 
 
 
 
 
 
 
 
 
 
 
39
  class ImgOutput(BaseModel):
40
  user_id: list
41
 
 
42
  class UserSaved(BaseModel):
43
  status: str
44
 
45
+ class UserDelete(BaseModel):
46
+ label: str
47
+
48
+ class Message(BaseModel):
49
+ message: str
50
+
51
+ class CleanPickle(BaseModel):
52
+ confirm: bool
53
+
54
 
55
  def recognize_face(image_url: HttpUrl) -> ImgOutput:
56
 
 
116
  rgb_img = cv2.cvtColor(new_image, cv2.COLOR_BGR2RGB)
117
 
118
  encode = face_recognition.face_encodings(new_image)[0]
119
+ if len(encode) == 0:
120
+ return "No face found"
121
  face_encodings.append(encode)
122
  labels.append(user_name)
123
 
 
132
  # Upload the pickle file to Firebase Storage
133
  pkl_blob = storage.child(f"{storage_folder}pkl/face_encodings.pkl")
134
  pkl_blob.put("face_encodings.pkl")
135
+ return "User Saved"
136
+
137
+
138
+ # Function to delete a user
139
+ def delete_user(user_name: str):
140
+ # Load the stored face encodings and labels from the pickle file
141
+ with open("face_encodings.pkl", "rb") as file:
142
+ data = pickle.load(file)
143
+ face_encodings = data["encodings"]
144
+ labels = data["labels"]
145
+
146
+ if user_name in labels:
147
+ index = labels.index(user_name)
148
+ del labels[index]
149
+ del face_encodings[index]
150
+
151
+ # Save the updated encodings and labels to the pickle file
152
+ data = {"encodings": face_encodings, "labels": labels}
153
+ with open("face_encodings.pkl", "wb") as file:
154
+ pickle.dump(data, file)
155
+
156
+ # Upload the updated pickle file to Firebase Storage
157
+ pkl_blob = storage.child(f"{storage_folder}pkl/face_encodings.pkl")
158
+ pkl_blob.put("face_encodings.pkl")
159
+
160
+ return {"message": f"User '{user_name}' deleted successfully."}
161
+ else:
162
+ return {"message": f"User '{user_name}' not found."}
163
+
164
+
165
+ def clean_pickle(confirm: bool):
166
+ if confirm:
167
+ # Remove the pickle file
168
+ if os.path.exists("face_encodings.pkl"):
169
+ os.remove("face_encodings.pkl")
170
+
171
+ # Create an empty pickle file
172
+ with open("face_encodings.pkl", "wb") as file:
173
+ data = {"encodings": [], "labels": []}
174
+ pickle.dump(data, file)
175
+
176
+ # Upload the empty pickle file to Firebase Storage
177
+ pkl_blob = storage.child(f"{storage_folder}pkl/face_encodings.pkl")
178
+ pkl_blob.put("face_encodings.pkl")
179
+
180
+ return {"message": "Pickle file cleaned and uploaded successfully."}
181
+ else:
182
+ return {"message": "Confirmation required to clean the pickle file."}
183
 
184
 
185
  @app.post('/')
 
190
 
191
  @app.post('/user/')
192
  async def scoring_endpoint(item:ImgSave):
193
+ results = add_face(item.image_url, item.user_name)
194
+ return Message(message=results)
195
+
196
+
197
+ @app.delete('/user_delete/')
198
+ async def scoring_endpoint(item: UserDelete):
199
+ result = delete_user(item.label)
200
+ return Message(message=result["message"])
201
+
202
+
203
+ @app.delete('/clean/')
204
+ async def clean_pickle_endpoint(item: CleanPickle):
205
+ result = clean_pickle(item.confirm)
206
+ return result