Update FastAPI.py
Browse files36 unmodified lines37383940414243448 unmodified lines5354555657585960616263646566676869707172737475767778798037 unmodified lines11811912012136 unmodified linesimage_url: HttpUrlclass ImgOutput(BaseModel):label: strdef recognize_face(image_url: HttpUrl) -> ImgOutput:storage.child().download("Faces/pkl/face_encodings.pkl","face_encodings.pkl")8 unmodified linesface_encodings = data["encodings"]labels = data["labels"]# Load a new image you want to recognizenew_image = cv2.imread("examp.jpg")new_face_encoding = face_recognition.face_encodings(new_image)if len(new_face_encoding) == 0:print("No faces found in the new image.")else:# Compare the new face encoding to the stored encodingsresults = face_recognition.compare_faces(face_encodings, new_face_encoding[0])os.remove("examp.jpg")for i, result in enumerate(results):if result:return ImgOutput(label=labels[i])return ImgOutput(label="unable to detect")def add_face(image_url: HttpUrl,user_name : str) -> ImgOutput:# Downloading imageresponse = requests.get(image_url)with open("examp.jpg", 'wb') as file:37 unmodified lines@app.post('/user/')async def scoring_endpoint(item:ImgSave):add_face(item.image_url, item.user_name)return ImgOutput(label="User Saved")36 unmodified lines373839404142434445464748498 unmodified lines585960616263646566676869707172737475767778798081828384858687888990919293949537 unmodified lines13313413513613736 unmodified linesimage_url: HttpUrlclass ImgOutput(BaseModel):label: listclass UserSaved(BaseModel):label: strdef recognize_face(image_url: HttpUrl) -> ImgOutput:storage.child().download("Faces/pkl/face_encodings.pkl","face_encodings.pkl")8 unmodified linesface_encodings = data["encodings"]labels = data["labels"]# Load the new image you want to recognizenew_image = cv2.imread("examp.jpg")# Find face encodings in the new imagenew_face_encodings = face_recognition.face_encodings(new_image)if len(new_face_encodings) == 0:print("No faces found in the new image.")return ImgOutput(label=["unable to detect"])else:output_labels = []for new_face_encoding in new_face_encodings:# Compare the new face encoding to the stored encodingsresults = face_recognition.compare_faces(face_encodings, new_face_encoding)for i, result in enumerate(results):if result:output_labels.append(labels[i])os.remove("examp.jpg")if output_labels:return ImgOutput(label=output_labels)else:out = ["unable to detect"]return ImgOutput(label=out)def add_face(image_url: HttpUrl,user_name : str):# Downloading imageresponse = requests.get(image_url)with open("examp.jpg", 'wb') as file:37 unmodified lines@app.post('/user/')async def scoring_endpoint(item:ImgSave):add_face(item.image_url, item.user_name)results = ["User Saved"]return ImgOutput(label=results)
FastAPI.py+31-15