Spaces:
Sleeping
Sleeping
Update doors_fasterrcnn.py
Browse files- 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 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
#
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|