Marthee commited on
Commit
91db295
·
verified ·
1 Parent(s): a1c91fe

Update doors_fasterrcnn.py

Browse files
Files changed (1) hide show
  1. doors_fasterrcnn.py +43 -33
doors_fasterrcnn.py CHANGED
@@ -167,39 +167,49 @@ def add_annotations_to_pdf(image, pdf_name, annotation_s, annotation_d):
167
  # pdf_document.close()
168
 
169
  def main_run(pdf_fullpath, weights_path, pdf_name):
170
- img_pillow = convert2pillow(pdf_fullpath)
171
- new_image = img_pillow.resize((2384,1684))
172
- # Specify the number of classes (including the background)
173
- num_classes = 6 # Ensure this matches the saved model's number of classes
174
- # Load the model with the specified number of classes
175
- model = get_model(num_classes)
176
- # Load the saved model's state dictionary with map_location to handle CPU
177
- device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
178
- # model.load_state_dict(torch.load(weights_path, map_location=device))
179
- model.load_state_dict(torch.load(weights_path, map_location=device), strict=False)
180
- # Set the model to evaluation mode
181
- model.eval()
182
- # Move the model to the appropriate device
183
- model.to(device)
184
-
185
- #START INFERENCE
186
- sbox, dbox = ev_model(new_image, model, device, 0.6)
187
-
188
- single_info = mid_points_bbox(sbox)
189
- double_info = mid_points_bbox(dbox)
190
-
191
- single_annotations = create_annotations("single door", single_info)
192
- double_annotations = create_annotations("double door", double_info)
193
-
194
- pdf_document=add_annotations_to_pdf(new_image, pdf_name, single_annotations, double_annotations)
195
-
196
- page=pdf_document[0]
197
- pix = page.get_pixmap() # render page to an image
198
- pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
199
- img=np.array(pl)
200
- annotatedimg = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
201
-
202
- return annotatedimg,pdf_document
 
 
 
 
 
 
 
 
 
 
203
 
204
  # model_path = '/content/drive/MyDrive/combined.pth'
205
  # #pdf_name = data
 
167
  # pdf_document.close()
168
 
169
  def main_run(pdf_fullpath, weights_path, pdf_name):
170
+ img_pillow = convert2pillow(pdf_fullpath)
171
+ new_image = img_pillow.resize((2384, 1684))
172
+
173
+ # Specify the number of classes (including the background)
174
+ num_classes = 6 # Ensure this matches the saved model's number of classes
175
+
176
+ # Load the model with the specified number of classes
177
+ model = get_model(num_classes)
178
+
179
+ # Load the saved model's state dictionary with map_location to handle CPU
180
+ device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
181
+
182
+ try:
183
+ model.load_state_dict(torch.load(weights_path, map_location=device), strict=False)
184
+ except RuntimeError as e:
185
+ print(f"Error loading model state_dict: {e}")
186
+ return
187
+
188
+ # Set the model to evaluation mode
189
+ model.eval()
190
+
191
+ # Move the model to the appropriate device
192
+ model.to(device)
193
+
194
+ # START INFERENCE
195
+ sbox, dbox = ev_model(new_image, model, device, 0.6)
196
+
197
+ single_info = mid_points_bbox(sbox)
198
+ double_info = mid_points_bbox(dbox)
199
+
200
+ single_annotations = create_annotations("single door", single_info)
201
+ double_annotations = create_annotations("double door", double_info)
202
+
203
+ # add_annotations_to_pdf(new_image, pdf_name, single_annotations, double_annotations)
204
+ pdf_document=add_annotations_to_pdf(new_image, pdf_name, single_annotations, double_annotations)
205
+
206
+ page=pdf_document[0]
207
+ pix = page.get_pixmap() # render page to an image
208
+ pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
209
+ img=np.array(pl)
210
+ annotatedimg = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
211
+
212
+ return annotatedimg,pdf_document
213
 
214
  # model_path = '/content/drive/MyDrive/combined.pth'
215
  # #pdf_name = data