Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -54,144 +54,8 @@ webcam_path = 'captured_image.jpg'
|
|
| 54 |
IMAGE_SHAPE = 640
|
| 55 |
|
| 56 |
# Set Title
|
| 57 |
-
st.title("
|
| 58 |
-
|
| 59 |
-
# Define Image Processor
|
| 60 |
-
#image_processor_prod = ViTImageProcessor.from_pretrained(MODEL_TRANSFORMER, attn_implementation="sdpa", torch_dtype=torch.float16)
|
| 61 |
-
|
| 62 |
-
# Define ML Model
|
| 63 |
-
#class FaceEmbeddingModel(torch.nn.Module):
|
| 64 |
-
# def __init__(self, model_name, embedding_size):
|
| 65 |
-
# super(FaceEmbeddingModel, self).__init__()
|
| 66 |
-
# self.config = ViTConfig.from_pretrained(model_name, id2label=idx_to_label, label2id=label_to_idx, return_dict=True)
|
| 67 |
-
# self.backbone = ViTModel.from_pretrained(model_name, config=self.config) # Load ViT model
|
| 68 |
-
# self.fc = torch.nn.Linear(self.backbone.config.hidden_size, embedding_size) # Convert to 512D feature vector
|
| 69 |
-
#
|
| 70 |
-
# def forward(self, images):
|
| 71 |
-
# x = self.backbone(images).last_hidden_state[:, 0] # Extract embeddings
|
| 72 |
-
# x = self.fc(x) # Convert to 512D embedding
|
| 73 |
-
# return torch.nn.functional.normalize(x) # Normalize for cosine similarity
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
# Load the model
|
| 77 |
-
#model_pretrained = torch.load(model_path, map_location=device, weights_only=False)
|
| 78 |
-
|
| 79 |
-
# Define the ML model - Evaluation function
|
| 80 |
-
#def prod_function(transformer_model, prod_dl, webcam_dl):
|
| 81 |
-
# # Initialize accelerator
|
| 82 |
-
# accelerator = Accelerator()
|
| 83 |
-
#
|
| 84 |
-
# # to INFO for the main process only.
|
| 85 |
-
# #if accelerator.is_main_process:
|
| 86 |
-
# # datasets.utils.logging.set_verbosity_warning()
|
| 87 |
-
# # transformers.utils.logging.set_verbosity_info()
|
| 88 |
-
# #else:
|
| 89 |
-
# # datasets.utils.logging.set_verbosity_error()
|
| 90 |
-
# # transformers.utils.logging.set_verbosity_error()
|
| 91 |
-
#
|
| 92 |
-
# # The seed need to be set before we instantiate the model, as it will determine the random head.
|
| 93 |
-
# set_seed(42)
|
| 94 |
-
#
|
| 95 |
-
# # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the prepare method.
|
| 96 |
-
# accelerated_model, acclerated_prod_dl, acclerated_webcam_dl = accelerator.prepare(transformer_model, prod_dl, webcam_dl)
|
| 97 |
-
#
|
| 98 |
-
# # Evaluate at the end of the epoch
|
| 99 |
-
# accelerated_model.eval()
|
| 100 |
-
#
|
| 101 |
-
# # Find Embedding of the image to be evaluated
|
| 102 |
-
# for batch in acclerated_webcam_dl:
|
| 103 |
-
# with torch.no_grad():
|
| 104 |
-
# #img_prod = acclerated_prod_data['pixel_values']
|
| 105 |
-
# emb_prod = accelerated_model(batch['pixel_values'])
|
| 106 |
-
#
|
| 107 |
-
# prod_preds = []
|
| 108 |
-
#
|
| 109 |
-
# for batch in acclerated_prod_dl:
|
| 110 |
-
# #img = batch['pixel_values']
|
| 111 |
-
# with torch.no_grad():
|
| 112 |
-
# emb = accelerated_model(batch['pixel_values'])
|
| 113 |
-
# distance = F.pairwise_distance(emb, emb_prod)
|
| 114 |
-
#
|
| 115 |
-
# prod_preds.append(distance)
|
| 116 |
-
# return prod_preds
|
| 117 |
-
|
| 118 |
-
# Creation of Dataloader
|
| 119 |
-
#class CustomDatasetProd(Dataset):
|
| 120 |
-
# def __init__(self, image_path, webcam):
|
| 121 |
-
# self.image_path = image_path
|
| 122 |
-
# self.webcam = webcam
|
| 123 |
-
#
|
| 124 |
-
# def __len__(self):
|
| 125 |
-
# return len(self.image_path)
|
| 126 |
-
#
|
| 127 |
-
# def __getitem__(self, idx):
|
| 128 |
-
# if webcam == False:
|
| 129 |
-
# img = cv2.imread(image_path[idx])
|
| 130 |
-
# else:
|
| 131 |
-
# img = image_path
|
| 132 |
-
# faces = app.get(img)
|
| 133 |
-
#
|
| 134 |
-
# if not faces:
|
| 135 |
-
# raise Exception("No face detected")
|
| 136 |
-
#
|
| 137 |
-
# pixel_values = faces[0].embedding # embedding is a 512-dimensional vector
|
| 138 |
-
# item = {
|
| 139 |
-
# 'pixel_values': pixel_values.squeeze(0),
|
| 140 |
-
# }
|
| 141 |
-
# return item
|
| 142 |
-
|
| 143 |
-
# Creation of Dataset
|
| 144 |
-
#class CreateDatasetProd():
|
| 145 |
-
# def __init__(self, image_processor):
|
| 146 |
-
# super().__init__()
|
| 147 |
-
# self.image_processor = image_processor
|
| 148 |
-
# # Define a transformation pipeline
|
| 149 |
-
# self.transform_prod = transforms.v2.Compose([
|
| 150 |
-
# transforms.v2.ToImage(),
|
| 151 |
-
# transforms.v2.ToDtype(torch.uint8, scale=False)
|
| 152 |
-
# ])
|
| 153 |
-
#
|
| 154 |
-
# def get_pixels(self, img_paths):
|
| 155 |
-
# pixel_values = []
|
| 156 |
-
# for path in img_paths:
|
| 157 |
-
# # Read and process Images
|
| 158 |
-
# img = Image.open(path)
|
| 159 |
-
# img = self.transform_prod(img)
|
| 160 |
-
#
|
| 161 |
-
# # Scaling the video to ML model's desired format
|
| 162 |
-
# img = self.image_processor(img, return_tensors='pt') #, input_data_format='channels_first')
|
| 163 |
-
#
|
| 164 |
-
# pixel_values.append(img['pixel_values'].squeeze(0))
|
| 165 |
-
#
|
| 166 |
-
# # Force garbage collection
|
| 167 |
-
# del img
|
| 168 |
-
# gc.collect()
|
| 169 |
-
# return pixel_values
|
| 170 |
-
#
|
| 171 |
-
# def get_pixel(self, img_path):
|
| 172 |
-
# # Read and process Images
|
| 173 |
-
# img = Image.open(img_path)
|
| 174 |
-
# img = self.transform_prod(img)
|
| 175 |
-
#
|
| 176 |
-
# # Scaling the video to ML model's desired format
|
| 177 |
-
# img = self.image_processor(img, return_tensors='pt') #, input_data_format='channels_first')
|
| 178 |
-
#
|
| 179 |
-
# pixel_values = img['pixel_values'] #.squeeze(0)
|
| 180 |
-
#
|
| 181 |
-
# # Force garbage collection
|
| 182 |
-
# del img
|
| 183 |
-
# gc.collect()
|
| 184 |
-
#
|
| 185 |
-
# return pixel_values
|
| 186 |
-
#
|
| 187 |
-
# def create_dataset(self, image_paths, webcam=False):
|
| 188 |
-
# if webcam == True:
|
| 189 |
-
# pixel_values = self.get_pixel(image_paths)
|
| 190 |
-
# else:
|
| 191 |
-
# pixel_values = torch.stack(self.get_pixels(image_paths))
|
| 192 |
-
#
|
| 193 |
-
# return CustomDatasetProd(pixel_values=pixel_values)
|
| 194 |
-
|
| 195 |
# Read images from directory
|
| 196 |
image_paths = []
|
| 197 |
image_file = glob(os.path.join(data_path, '*.jpg'))
|
|
@@ -204,16 +68,6 @@ image_paths.extend(image_file)
|
|
| 204 |
app = FaceAnalysis(name="buffalo_l") # buffalo_l includes ArcFace model
|
| 205 |
app.prepare(ctx_id=-1, det_size=(IMAGE_SHAPE, IMAGE_SHAPE)) # Use ctx_id=-1 if you want CPU, and ctx_id=0 for GPU
|
| 206 |
|
| 207 |
-
# Create DataLoader for Employees image
|
| 208 |
-
#dataset_prod_obj = CreateDatasetProd(image_processor_prod)
|
| 209 |
-
#prod_ds = dataset_prod_obj.create_dataset(image_paths, webcam=False)
|
| 210 |
-
#prod_dl = DataLoader(prod_ds, webcam=False, batch_size=BATCH_SIZE)
|
| 211 |
-
|
| 212 |
-
## Testing the dataloader
|
| 213 |
-
#prod_inputs = next(iter(prod_dl))
|
| 214 |
-
#st.write(prod_inputs['pixel_values'].shape)
|
| 215 |
-
|
| 216 |
-
|
| 217 |
# Define the ML model - Evaluation function
|
| 218 |
def prod_function(app, prod_path, webcam_path):
|
| 219 |
webcam_img = Image.open(webcam_path)
|
|
@@ -299,18 +153,7 @@ with app_tab:
|
|
| 299 |
enable = st.checkbox("Enable camera")
|
| 300 |
picture = st.camera_input("Take a picture", disabled=not enable)
|
| 301 |
if picture is not None:
|
| 302 |
-
|
| 303 |
-
#picture.save(webcam_path, "JPEG")
|
| 304 |
-
#st.write('Image saved as:',webcam_path)
|
| 305 |
-
|
| 306 |
-
## Create DataLoader for Webcam Image
|
| 307 |
-
#webcam_ds = dataset_prod_obj.create_dataset(picture, webcam=True)
|
| 308 |
-
#webcam_dl = DataLoader(picture, webcam=True, batch_size=BATCH_SIZE)
|
| 309 |
-
|
| 310 |
-
## Testing the dataloader
|
| 311 |
-
#prod_inputs = next(iter(webcam_dl))
|
| 312 |
-
#st.write(prod_inputs['pixel_values'].shape)
|
| 313 |
-
|
| 314 |
with st.spinner("Wait for it...", show_time=True):
|
| 315 |
# Run the predictions
|
| 316 |
prediction = prod_function(app, image_paths, picture)
|
|
@@ -321,6 +164,7 @@ with app_tab:
|
|
| 321 |
|
| 322 |
# Display the results
|
| 323 |
if prediction[match_idx] >= 0.6:
|
| 324 |
-
|
|
|
|
| 325 |
else:
|
| 326 |
st.write("Match not found")
|
|
|
|
| 54 |
IMAGE_SHAPE = 640
|
| 55 |
|
| 56 |
# Set Title
|
| 57 |
+
st.title("AIML-Student Attendance System")
|
| 58 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
# Read images from directory
|
| 60 |
image_paths = []
|
| 61 |
image_file = glob(os.path.join(data_path, '*.jpg'))
|
|
|
|
| 68 |
app = FaceAnalysis(name="buffalo_l") # buffalo_l includes ArcFace model
|
| 69 |
app.prepare(ctx_id=-1, det_size=(IMAGE_SHAPE, IMAGE_SHAPE)) # Use ctx_id=-1 if you want CPU, and ctx_id=0 for GPU
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
# Define the ML model - Evaluation function
|
| 72 |
def prod_function(app, prod_path, webcam_path):
|
| 73 |
webcam_img = Image.open(webcam_path)
|
|
|
|
| 153 |
enable = st.checkbox("Enable camera")
|
| 154 |
picture = st.camera_input("Take a picture", disabled=not enable)
|
| 155 |
if picture is not None:
|
| 156 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
with st.spinner("Wait for it...", show_time=True):
|
| 158 |
# Run the predictions
|
| 159 |
prediction = prod_function(app, image_paths, picture)
|
|
|
|
| 164 |
|
| 165 |
# Display the results
|
| 166 |
if prediction[match_idx] >= 0.6:
|
| 167 |
+
pname = image_paths[match_idx].split('/')[-1].split('.')[0]
|
| 168 |
+
st.write('Welcome: ',pname)
|
| 169 |
else:
|
| 170 |
st.write("Match not found")
|