J007acky commited on
Commit
56a798e
·
verified ·
1 Parent(s): e42f9b9

Update FastAPI.py

Browse files
Files changed (1) hide show
  1. FastAPI.py +31 -15
FastAPI.py CHANGED
@@ -37,8 +37,13 @@ class ImgInput(BaseModel):
37
  image_url: HttpUrl
38
 
39
  class ImgOutput(BaseModel):
 
 
 
 
40
  label: str
41
 
 
42
  def recognize_face(image_url: HttpUrl) -> ImgOutput:
43
 
44
  storage.child().download("Faces/pkl/face_encodings.pkl","face_encodings.pkl")
@@ -53,28 +58,38 @@ def recognize_face(image_url: HttpUrl) -> ImgOutput:
53
  face_encodings = data["encodings"]
54
  labels = data["labels"]
55
 
56
- # Load a new image you want to recognize
57
- new_image = cv2.imread("examp.jpg")
58
 
59
- new_face_encoding = face_recognition.face_encodings(new_image)
 
 
 
 
 
 
 
 
 
 
60
 
61
- if len(new_face_encoding) == 0:
62
- print("No faces found in the new image.")
63
- else:
64
- # Compare the new face encoding to the stored encodings
65
- results = face_recognition.compare_faces(face_encodings, new_face_encoding[0])
66
 
67
- os.remove("examp.jpg")
 
 
68
 
69
- for i, result in enumerate(results):
70
- if result:
71
- return ImgOutput(label=labels[i])
72
 
73
- return ImgOutput(label="unable to detect")
 
 
 
 
74
 
75
 
76
 
77
- def add_face(image_url: HttpUrl,user_name : str) -> ImgOutput:
78
  # Downloading image
79
  response = requests.get(image_url)
80
  with open("examp.jpg", 'wb') as file:
@@ -118,4 +133,5 @@ async def scoring_endpoint(item:ImgInput):
118
  @app.post('/user/')
119
  async def scoring_endpoint(item:ImgSave):
120
  add_face(item.image_url, item.user_name)
121
- return ImgOutput(label="User Saved")
 
 
37
  image_url: HttpUrl
38
 
39
  class ImgOutput(BaseModel):
40
+ label: list
41
+
42
+
43
+ class UserSaved(BaseModel):
44
  label: str
45
 
46
+
47
  def recognize_face(image_url: HttpUrl) -> ImgOutput:
48
 
49
  storage.child().download("Faces/pkl/face_encodings.pkl","face_encodings.pkl")
 
58
  face_encodings = data["encodings"]
59
  labels = data["labels"]
60
 
 
 
61
 
62
+ # Load the new image you want to recognize
63
+ new_image = cv2.imread("examp.jpg")
64
+
65
+ # Find face encodings in the new image
66
+ new_face_encodings = face_recognition.face_encodings(new_image)
67
+
68
+ if len(new_face_encodings) == 0:
69
+ print("No faces found in the new image.")
70
+ return ImgOutput(label=["unable to detect"])
71
+ else:
72
+ output_labels = []
73
 
74
+ for new_face_encoding in new_face_encodings:
75
+ # Compare the new face encoding to the stored encodings
76
+ results = face_recognition.compare_faces(face_encodings, new_face_encoding)
 
 
77
 
78
+ for i, result in enumerate(results):
79
+ if result:
80
+ output_labels.append(labels[i])
81
 
82
+ os.remove("examp.jpg")
 
 
83
 
84
+ if output_labels:
85
+ return ImgOutput(label=output_labels)
86
+ else:
87
+ out = ["unable to detect"]
88
+ return ImgOutput(label=out)
89
 
90
 
91
 
92
+ def add_face(image_url: HttpUrl,user_name : str):
93
  # Downloading image
94
  response = requests.get(image_url)
95
  with open("examp.jpg", 'wb') as file:
 
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 ImgOutput(label=results)