Spaces:
Sleeping
Sleeping
Update doors_fasterrcnn.py
Browse files- doors_fasterrcnn.py +63 -9
doors_fasterrcnn.py
CHANGED
|
@@ -101,6 +101,35 @@ def calculate_width(bbox):
|
|
| 101 |
door_width = bbox_height
|
| 102 |
return door_width
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
def calculate_midpoint(top_left, bottom_right):
|
| 105 |
x1, y1 = top_left
|
| 106 |
x2, y2 = bottom_right
|
|
@@ -109,8 +138,8 @@ def calculate_midpoint(top_left, bottom_right):
|
|
| 109 |
ym = int((y1 + y2) / 2)
|
| 110 |
return (xm, ym)
|
| 111 |
|
| 112 |
-
def mid_points_bbox(bbox
|
| 113 |
-
|
| 114 |
for i in range(len(bbox)):
|
| 115 |
x1 = int(bbox[i][0])
|
| 116 |
y1 = int(bbox[i][1])
|
|
@@ -118,20 +147,17 @@ def mid_points_bbox(bbox, ratio):
|
|
| 118 |
y2 = int(bbox[i][3])
|
| 119 |
top_left_corner = (x1, y1)
|
| 120 |
bottom_right_corner = (x2, y2)
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
midpoint = calculate_midpoint(top_left_corner, bottom_right_corner)
|
| 124 |
-
midpoints.append((midpoint, door_width))
|
| 125 |
-
return midpoints
|
| 126 |
|
| 127 |
def create_annotations(door_kind, midpoints):
|
| 128 |
door = door_kind
|
| 129 |
annotations = []
|
| 130 |
for i in range(len(midpoints)):
|
| 131 |
-
annotations.append((midpoints[i][0]
|
| 132 |
return annotations
|
| 133 |
|
| 134 |
-
def add_annotations_to_pdf(image, pdf_name, annotation_s, annotation_d):
|
| 135 |
image_width, image_height = image.size
|
| 136 |
|
| 137 |
# Create a new PDF document
|
|
@@ -161,8 +187,36 @@ def add_annotations_to_pdf(image, pdf_name, annotation_s, annotation_d):
|
|
| 161 |
annot.set_colors(stroke=(0, 1, 0), fill=None) # Set the stroke color to red
|
| 162 |
annot.update()
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
output_pdf_path = pdf_name+"_annotated.pdf"
|
| 165 |
# Save the PDF with annotations
|
|
|
|
| 166 |
return pdf_document
|
| 167 |
# pdf_document.save(output_pdf_path)
|
| 168 |
# pdf_document.close()
|
|
|
|
| 101 |
door_width = bbox_height
|
| 102 |
return door_width
|
| 103 |
|
| 104 |
+
def width_annotations(bbox, ratio):
|
| 105 |
+
lines = []
|
| 106 |
+
width = []
|
| 107 |
+
|
| 108 |
+
for box in bbox:
|
| 109 |
+
door_width = calculate_width(box)
|
| 110 |
+
door_width = round(door_width*ratio, 4)
|
| 111 |
+
x1,y1,x2,y2 = box
|
| 112 |
+
b_width = x2 - x1
|
| 113 |
+
b_height = y2 - y1
|
| 114 |
+
|
| 115 |
+
if b_width > b_height:
|
| 116 |
+
lines.append(((x1, y1), (x2, y1)))
|
| 117 |
+
x = (x1+x2)/2
|
| 118 |
+
y = (y1+y1)/2
|
| 119 |
+
width.append(((x,y),door_width))
|
| 120 |
+
else:
|
| 121 |
+
lines.append(((x1, y1), (x1, y2)))
|
| 122 |
+
x = (x1+x1)/2
|
| 123 |
+
y = (y1+y2)/2
|
| 124 |
+
width.append(((x,y), door_width))
|
| 125 |
+
return lines, width
|
| 126 |
+
|
| 127 |
+
def create_width_annotations(width_info):
|
| 128 |
+
annotations = []
|
| 129 |
+
for i in range(len(width_info)):
|
| 130 |
+
annotations.append(((width_info[i][0][0]),(width_info[i][0][1]),f"{width_info[i][1]} m"))
|
| 131 |
+
return annotations
|
| 132 |
+
|
| 133 |
def calculate_midpoint(top_left, bottom_right):
|
| 134 |
x1, y1 = top_left
|
| 135 |
x2, y2 = bottom_right
|
|
|
|
| 138 |
ym = int((y1 + y2) / 2)
|
| 139 |
return (xm, ym)
|
| 140 |
|
| 141 |
+
def mid_points_bbox(bbox):
|
| 142 |
+
midpoint = []
|
| 143 |
for i in range(len(bbox)):
|
| 144 |
x1 = int(bbox[i][0])
|
| 145 |
y1 = int(bbox[i][1])
|
|
|
|
| 147 |
y2 = int(bbox[i][3])
|
| 148 |
top_left_corner = (x1, y1)
|
| 149 |
bottom_right_corner = (x2, y2)
|
| 150 |
+
midpoint.append(calculate_midpoint(top_left_corner, bottom_right_corner))
|
| 151 |
+
return midpoint
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
def create_annotations(door_kind, midpoints):
|
| 154 |
door = door_kind
|
| 155 |
annotations = []
|
| 156 |
for i in range(len(midpoints)):
|
| 157 |
+
annotations.append((midpoints[i][0],midpoints[i][1], door))
|
| 158 |
return annotations
|
| 159 |
|
| 160 |
+
def add_annotations_to_pdf(image, pdf_name, annotation_s, annotation_d,width_ann_single, width_ann_double,line_single,line_double):
|
| 161 |
image_width, image_height = image.size
|
| 162 |
|
| 163 |
# Create a new PDF document
|
|
|
|
| 187 |
annot.set_colors(stroke=(0, 1, 0), fill=None) # Set the stroke color to red
|
| 188 |
annot.update()
|
| 189 |
|
| 190 |
+
#Annotations for width measurement (marra single we marra double)
|
| 191 |
+
for annotation in width_ann_single:
|
| 192 |
+
x, y, text = annotation
|
| 193 |
+
rect = fitz.Rect(x, y, x + 200, y + 50) # Adjust the width and height as needed
|
| 194 |
+
annot = page.add_freetext_annot(rect, text, fontsize=12, fontname="helv", text_color=(0,1,0))
|
| 195 |
+
annot.update()
|
| 196 |
+
for annotation in width_ann_double:
|
| 197 |
+
x, y, text = annotation
|
| 198 |
+
rect = fitz.Rect(x, y, x + 200, y + 50) # Adjust the width and height as needed
|
| 199 |
+
annot = page.add_freetext_annot(rect, text, fontsize=12, fontname="helv", text_color=(0,1,0))
|
| 200 |
+
annot.update()
|
| 201 |
+
|
| 202 |
+
#Annotation kind of the line drawings (marra single we marra double)
|
| 203 |
+
for line in line_single:
|
| 204 |
+
start_point, end_point = line
|
| 205 |
+
annot = page.add_line_annot(start_point, end_point)
|
| 206 |
+
annot.set_border(width=2, dashes=None) # Optional border styling
|
| 207 |
+
annot.set_colors(stroke=(1, 0, 0)) # Set the line color to red
|
| 208 |
+
annot.update()
|
| 209 |
+
for line in line_double:
|
| 210 |
+
start_point, end_point = line
|
| 211 |
+
annot = page.add_line_annot(start_point, end_point)
|
| 212 |
+
annot.set_border(width=2, dashes=None) # Optional border styling
|
| 213 |
+
annot.set_colors(stroke=(1, 0, 0)) # Set the line color to red
|
| 214 |
+
annot.update()
|
| 215 |
+
|
| 216 |
+
|
| 217 |
output_pdf_path = pdf_name+"_annotated.pdf"
|
| 218 |
# Save the PDF with annotations
|
| 219 |
+
|
| 220 |
return pdf_document
|
| 221 |
# pdf_document.save(output_pdf_path)
|
| 222 |
# pdf_document.close()
|