File size: 63,029 Bytes
3e78d8d 1d05f23 6c7d570 a05c6af 6c7d570 1d05f23 6c7d570 3e78d8d 6c7d570 590ae13 3e78d8d 1d05f23 218da62 35fdaa6 1d05f23 0f72fcc 1d05f23 6616d77 1d05f23 0f72fcc 1d05f23 8bd6d1c cfd803f 35297ed 1d05f23 b2e2267 1d05f23 b2e2267 1d05f23 3e78d8d 6c7d570 3e78d8d 1d05f23 3e78d8d 7c9bfe2 3e78d8d 17d820f 3e78d8d 6c7d570 1d05f23 6c7d570 1d05f23 6c7d570 1d05f23 6c7d570 7c9bfe2 6c7d570 bc52949 7c9bfe2 6c7d570 7c9bfe2 6c7d570 7c9bfe2 6c7d570 7c9bfe2 6c7d570 7c9bfe2 6c7d570 7c9bfe2 6c7d570 7c9bfe2 6c7d570 7c9bfe2 6c7d570 1d05f23 6c7d570 1d05f23 6c7d570 9b18ad6 1d05f23 6c7d570 1d05f23 6c7d570 1d05f23 6c7d570 1d05f23 6c7d570 1d05f23 6c7d570 1d05f23 6c7d570 1d05f23 6c7d570 f3c4d99 6c7d570 bc52949 6c7d570 bc52949 6c7d570 bc52949 6c7d570 46ed17d 6c7d570 46ed17d bc52949 6c7d570 bc52949 6c7d570 f1da025 6c7d570 e04c753 6c7d570 46ed17d 6c7d570 385a20e 9d7c583 385a20e 6c7d570 385a20e 6c7d570 385a20e 6c7d570 385a20e 6c7d570 385a20e 6c7d570 385a20e 6c7d570 385a20e 6c7d570 385a20e 6c7d570 385a20e 11e8888 385a20e 59f1018 11e8888 385a20e 11e8888 385a20e 11e8888 385a20e 6c7d570 385a20e e04c753 3533d1c 6c7d570 3533d1c 6c7d570 3533d1c 6c7d570 3533d1c 6c7d570 3533d1c 6c7d570 e04c753 6c7d570 e04c753 6c7d570 1d05f23 6c7d570 73ce62a 6c7d570 73ce62a 1d05f23 73ce62a 6c7d570 73ce62a 6c7d570 73ce62a 6c7d570 e04c753 6c7d570 e04c753 6c7d570 e04c753 6c7d570 1d05f23 6c7d570 73ce62a 6c7d570 ea300c9 6c7d570 1d05f23 6c7d570 c71f69e 09adc16 6c7d570 46ed17d 53b3ee8 46ed17d 6c7d570 01458e8 c71f69e 5d8b850 6c7d570 c71f69e 6c7d570 b8dd6a5 46ed17d 6c7d570 4299d41 c71f69e 6c7d570 4299d41 6c7d570 46ed17d b8dd6a5 46ed17d b8dd6a5 46ed17d b8dd6a5 46ed17d 6c7d570 c71f69e 6c7d570 46ed17d e3f9142 c71f69e e3f9142 5d8b850 e3f9142 c71f69e 5d8b850 6c7d570 c71f69e 35fdaa6 6c7d570 4299d41 6c7d570 4299d41 6c7d570 e04c753 5d8b850 6c7d570 73ce62a 6c7d570 b78f513 6c7d570 b78f513 6c7d570 3e78d8d 6c7d570 3e78d8d 6c7d570 b78f513 6c7d570 b8dd6a5 46ed17d b8dd6a5 6c7d570 95a1473 6c7d570 590ae13 6c7d570 b8dd6a5 6c7d570 b8dd6a5 6c7d570 df4b73a 6c7d570 4299d41 6c7d570 4299d41 e04c753 6c7d570 5d8b850 6c7d570 73ce62a 6c7d570 ea300c9 6c7d570 1d05f23 6c7d570 b8dd6a5 6c7d570 46ed17d b8dd6a5 6c7d570 f6dc96f 6c7d570 c71f69e 6c7d570 3e78d8d 6c7d570 b8dd6a5 6c7d570 b8dd6a5 6c7d570 3e78d8d 6b4b8bf 80ab6b0 6c7d570 1d05f23 7ebbda6 6c7d570 ab4d87b 1d05f23 590ae13 6c7d570 5d8b850 6c7d570 35fdaa6 13b733f 218da62 005ddc6 2a7903d 005ddc6 35fdaa6 a05c6af 35fdaa6 027f19f b8b04c8 992a57c c278db5 917cc22 c278db5 b8b04c8 c278db5 35fdaa6 c278db5 ca87682 35fdaa6 13b733f 35fdaa6 c278db5 35fdaa6 ddf9041 35fdaa6 ddf9041 c278db5 35fdaa6 ddf9041 35fdaa6 897f6e3 35fdaa6 897f6e3 5d8b850 1d05f23 4b6cc81 96e5d9b ca87682 1d05f23 5d8b850 ca87682 218da62 374d6ce 6c7d570 96e5d9b 6c7d570 95a1473 6c7d570 96e5d9b 6c7d570 95a1473 6c7d570 90a2e6b 6c7d570 218da62 897f6e3 6c7d570 ca87682 3e78d8d e04c753 3e78d8d 436ca6c 6c7d570 5d8b850 6c7d570 5d8b850 6c7d570 5d8b850 6c7d570 5d8b850 6c7d570 ca87682 6c7d570 ca87682 6c7d570 ca87682 6c7d570 ca87682 6c7d570 3b9712b 793dcc0 6c7d570 897f6e3 1d05f23 39b1b3b 027f19f 1d05f23 3e78d8d 1d05f23 3e78d8d d648acb 3e78d8d 9502d95 ca87682 6616d77 d2d1361 a2d8188 8bd6d1c 61a556c ea5ead4 863b4c9 9502d95 ca87682 b8bf755 0f72fcc 3e78d8d 51e19cc 6c7d570 1d05f23 6c7d570 3e78d8d 4b6cc81 1d05f23 73f3a12 84ea6bd 3845b2b 1d39d96 73f3a12 c580ec9 1d39d96 1d05f23 4dc1809 ec1419a 321c3af ca87682 ae60199 090fa64 ae60199 6c7d570 ae60199 863b4c9 b21ea9b 005ddc6 c278db5 3e78d8d 39b1b3b 84ea6bd 4fda369 73f3a12 39f0275 6c7d570 d8d1239 ca87682 03bc3e2 6c7d570 c8b09e7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 |
# import numpy as np
import cv2
import sched, time
import math
import fitz
import pandas as pd
import random
# # import imutils
# # from imutils import contours
# import colorsys
from PIL import Image , ImageDraw, ImageFont , ImageColor
import numpy as np
import gradio as gr
# from pdf_annotate import PdfAnnotator, Location, Appearance ,Metadata
import os
from db import dropbox_upload_file
from pathlib import Path
from PreprocessingFoundation import rmv_text,rmv_dashedLines
# from __future__ import print_function
from googleapiclient.discovery import build
from google.oauth2 import service_account
import pygsheets
#############################################################################################
'''push output to dropbox'''
#os.remove('dropbox_plans/Trees.pdf')
def pushToDropbox(plan1,area,df):
plan=Path(os.path.split(plan1)[1]).stem
# p=dropbox_upload_file('.',local_file=perim,dropbox_file_path='/savedMeasurements/'+plan+'perim.png')
a=dropbox_upload_file('.',local_file=area,dropbox_file_path='/savedMeasurements/'+plan+'area.png')
# if df !=None:
d=dropbox_upload_file('.',local_file=df,dropbox_file_path='/savedMeasurements/'+plan+'summary.csv')
#print(f)
def exportToExcel(plan,Dictionary):
# Dictionary.to_excel("summary.xlsx")
d=dropbox_upload_file('.',local_file=Dictionary,dropbox_file_path='/savedMeasurements/'+plan+'summary.csv')
def auth(username,password):
if username=="alaa" and password=="1234":
return True
def plan2img(plan):
if 'foundation' in plan.lower():
noTextImg=rmv_text(plan)
clean_img=rmv_dashedLines(noTextImg)
return clean_img
else:
# fname = plan
#op='pictures/found.png'
doc = fitz.open(plan)
for page in doc:
pix = page.get_pixmap(dpi=200) # render page to an image
pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
pl1=np.array(pl)
# pix = page.get_pixmap() # render page to an image
return pl1
# path='/content/drive/MyDrive/Colab Notebooks/Pile caps plans/13886-CRH-B3-FN-DR-S-31001 - Copy.pdf'
# pip install aspose-pdf
# pip install img2pdf
def detectCircles(imgOriginal ):
im=imgOriginal.copy()
imgGry1 = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
kernel=np.ones((3,3),np.uint8)
er1=cv2.erode(imgGry1,kernel, iterations=2)
er1=cv2.dilate(er1,kernel, iterations=1)
gray_blurred = cv2.blur(er1, (3,3 ))
# Apply Hough transform on the blurred image.
# min distance between circles, Upper threshold for the internal Canny edge detector.
detected_circles = cv2.HoughCircles( gray_blurred, cv2.HOUGH_GRADIENT, 1, 50, param1= 550,
param2 =21, minRadius = 20, maxRadius = 40) #18 param2
# Draw circles that are detected.
if detected_circles is not None:
# Convert the circle parameters a, b and r to integers.
detected_circles = np.uint16(np.around(detected_circles))
detected_circles = np.round(detected_circles[0, :]).astype("int")
#DRAW CIRCLES
for (x, y, r) in detected_circles:
cv2.circle(im, (x, y), r, (255, 255, 255), 5)
im=cv2.medianBlur(im,1)
print('circles')
# cv2_imshow(im)
return im
def detectSmallCircles(img ):
#Remove tiny TOC points that interfere with shapes
im=img.copy()
imgGry1 = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
kernel=np.ones((3,3),np.uint8)
er1=cv2.erode(imgGry1,kernel, iterations=1)
# Apply Hough transform on the blurred image.
# min distance between circles, Upper threshold for the internal Canny edge detector.
detected_circles = cv2.HoughCircles( imgGry1, cv2.HOUGH_GRADIENT, 1, 60, param1 =550,
param2 =13, minRadius = 1, maxRadius = 10) #18 param2
# Draw circles that are detected.
if detected_circles is not None:
# Convert the circle parameters a, b and r to integers.
detected_circles = np.uint16(np.around(detected_circles))
detected_circles = np.round(detected_circles[0, :]).astype("int")
#DRAW CIRCLES
for (x, y, r) in detected_circles:
cv2.circle(im, (x, y), r+1, (255, 255, 255), -1)
# cv2_imshow(im)
return im
# c=detectCircles(img)
def DashedPreprocessing(imgOriginal,imgnoSmall):
h,w=imgOriginal.shape[0:2]
#remove the gray contours from the plan
imgBW=cv2.threshold(imgnoSmall, 180, 255, cv2.THRESH_BINARY)[1]
im_copy=imgBW.copy()
im_copy1=im_copy
kernel1 = np.ones((3,5),np.uint8)
kernel2 = np.ones((9,9),np.uint8)
kernel3= np.ones((3,3),np.uint8)
imgGray=cv2.cvtColor(imgBW,cv2.COLOR_BGR2GRAY)
imgBW1=cv2.threshold(imgGray, 200, 255, cv2.THRESH_BINARY_INV)[1]
img1=cv2.erode(imgBW1, kernel1, iterations=1)
img2=cv2.dilate(img1, kernel2, iterations=3)
img3 = cv2.bitwise_and(imgBW1,img2)
img3= cv2.bitwise_not(img3)
img4 = cv2.bitwise_and(imgBW1,imgBW1,mask=img3)
img4=cv2.blur(img4,(7,7))
if h > w :
max = h
min = w
else:
max = w
min = h
return img4, imgBW, max,min
def removeDashedLines(img4, imgBW ,max,min):
imgLines= cv2.HoughLinesP(img4,1,np.pi/310,30,minLineLength=(max-min)//1.8,maxLineGap = 120) #was w-h , gap=150 0.99
#1 120
for i in range(len(imgLines)):
for x1,y1,x2,y2 in imgLines[i]:
cv2.line(imgBW,(x1,y1),(x2,y2),(0,255,0),2)
im_copy=imgBW.copy()
green=im_copy[:,:,1]
# cv2_imshow(im_copy)
return green
def removeSmallDashes(imgOriginal,green):
smalldashes=green.copy()
smalldashes=cv2.bitwise_not(smalldashes)
kernel3= np.ones((3,3),np.uint8)
img1=cv2.dilate(smalldashes, kernel3, iterations=2)
img2=cv2.erode(img1, kernel3, iterations=2)
smalldashes=cv2.medianBlur(img2,5)
smalldashes=cv2.medianBlur(smalldashes,7)
# cv2_imshow(smalldashes)
smalldashesOut=green.copy()
smalldashesOut=cv2.cvtColor(smalldashesOut,cv2.COLOR_GRAY2BGR)
imgLines= cv2.HoughLinesP(smalldashes,1,np.pi/150,27,minLineLength=10,maxLineGap = 70) #was w-h , gap=150
imgCopy=imgOriginal.copy()
for i in range(len(imgLines)):
for x1,y1,x2,y2 in imgLines[i]:
cv2.line(smalldashesOut,(x1,y1),(x2,y2),(0,255,0),2)
smalldashesOut=smalldashesOut[:,:,1]
# cv2_imshow(smalldashesOut)
for i in range(len(imgLines)):
for x1,y1,x2,y2 in imgLines[i]:
cv2.line(imgCopy,(x1,y1),(x2,y2),(0,255,0),6)
imgCopy=imgCopy[:,:,1]
# cv2_imshow(imgCopy)
return imgCopy,smalldashesOut
def euclidian_distance(point1, point2):
return sum([(point1[x] - point2[x]) ** 2 for x in range(len(point1))]) ** 0.5
def removeDashedLinesSmall(img4, imgBW ,max,min):
imgBW=cv2.cvtColor(imgBW,cv2.COLOR_GRAY2BGR)
imgLines= cv2.HoughLinesP(img4,1,np.pi/100,20,minLineLength=(max-min)//2.2,maxLineGap = 70) #2.1
for i in range(len(imgLines)):
for x1,y1,x2,y2 in imgLines[i]:
dist=euclidian_distance((x1,y1), (x2,y2))
# if dist > 1300 and dist <1450:
if dist >= (max-min)//2.1 and dist < (max-min)//1.9: #1.4
cv2.line(imgBW,(x1,y1),(x2,y2),(0,255,0),3)
im_copy=imgBW.copy()
green=im_copy[:,:,1]
# cv2_imshow(im_copy)
return green
def ConnectBeamLines(smalldashesOut):
green1=cv2.bitwise_not(smalldashesOut)
green2=smalldashesOut.copy()
green2=cv2.cvtColor(green2,cv2.COLOR_GRAY2BGR)
imgLines= cv2.HoughLinesP(green1,0.05,np.pi/250,10,minLineLength=25,maxLineGap = 20) #was w-h , gap=150 #50
for i in range(len(imgLines)):
for x1,y1,x2,y2 in imgLines[i]:
cv2.line(green2,(x1,y1),(x2,y2),(0,0,0),1)
imgLines= cv2.HoughLinesP(green1,0.3,np.pi/360,10,minLineLength=25,maxLineGap = 20) #try 180
for i in range(len(imgLines)):
for x1,y1,x2,y2 in imgLines[i]:
cv2.line(green2,(x1,y1),(x2,y2),(0,0,0),1)
# cv2_imshow(green2)
return green2
def allpreSteps(imgOriginal):
noCircles=detectCircles(imgOriginal)
imgnoSmall=detectSmallCircles(noCircles )
img4,imgBW,max,min=DashedPreprocessing(imgOriginal,imgnoSmall)
green=removeDashedLines(img4,imgBW,max,min)
imgCopy,smalldashesOut=removeSmallDashes(imgOriginal,green)
noSmallDashes=removeDashedLinesSmall(img4, smalldashesOut ,max,min)
green2=ConnectBeamLines(noSmallDashes)
# cv2_imshow(green2)
return green2
def ChangeBrightness(img,k):
imgdarker = 255 * (img/255)**k # k>1 darker , k <1 lighter
# cv2_imshow(imgdarker)
imgdarker = imgdarker.astype('uint8')
return imgdarker
def preprocessold(img,number):
# imcopy=detectCircles(img)
blurG = cv2.GaussianBlur(ChangeBrightness(img,6),(3,3),0)
imgGry = cv2.cvtColor(blurG, cv2.COLOR_BGR2GRAY)
kernel=np.ones((3,3),np.uint8)
er1=cv2.dilate(imgGry,kernel, iterations=2) #thinning
er2=cv2.erode(er1,kernel, iterations=3) #thicken
er3=cv2.dilate(er2,kernel, iterations=4)
if number == 0:
ret3, thresh = cv2.threshold(er3, 200, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
else:
ret3, thresh = cv2.threshold(er3, 220, 255, cv2.THRESH_BINARY_INV) #`140 - 141
# cv2_imshow(thresh)
return thresh
# preprocessold(img,0)
def preprocess(imgOriginal,number,green2):
#first preprocessing ( old method - black img with white shapes)
img1=preprocessold(imgOriginal,number)
imgGry0 = cv2.cvtColor(imgOriginal , cv2.COLOR_BGR2GRAY)
kernel=np.ones((3,3),np.uint8)
anding=cv2.bitwise_and(green2,green2,mask=img1)
anding = cv2.cvtColor(anding , cv2.COLOR_BGR2GRAY)
return anding
"""# ROI (levels)
## Detect regions with specific color and mask them
"""
def hexRGB(color):
color=color.lstrip('#')
color= tuple(int(color[i:i+2], 16) for i in (0, 2, 4)) #hex to rgb
color=np.array(color) #rgb to bgr
return color
def DetectColor(img,color=0):
imgCopy=img.copy()
imgCopy=cv2.cvtColor(imgCopy,cv2.COLOR_BGR2HSV)
tol=5 #tolerance
color=hexRGB(color)
h,s,v = cv2.cvtColor(np.uint8([[[color[2],color[1],color[0]]]]),cv2.COLOR_BGR2HSV)[0][0]
lower =np.array( [h- tol, 100, 100 ], dtype='uint8')
upper = np.array( [h + tol, 255, 255],dtype='uint8')
mask = cv2.inRange(imgCopy, lower , upper)
detectedColors = cv2.bitwise_and(imgCopy,imgCopy, mask= mask) # Bitwise-AND mask and original image
kernel=np.ones((3,3),np.uint8)
mask=cv2.dilate(mask,kernel, iterations=5)
mask=cv2.erode(mask,kernel, iterations=4)
detectedColors=cv2.dilate(detectedColors,kernel, iterations=5)
detectedColors=cv2.erode(detectedColors,kernel, iterations=4)
detectedColors=cv2.cvtColor(detectedColors,cv2.COLOR_HSV2BGR)
detectedColors=cv2.medianBlur(detectedColors,7)
# cv2_imshow(detectedColors)
return mask, detectedColors, color
def detectAllColors(img,finalColorArray):
for i in range(len(finalColorArray)):
detectedColors= DetectColor(img,finalColorArray[i])[1]
if i == 0:
allcolorsImg=cv2.bitwise_or(detectedColors,detectedColors)
else:
allcolorsImg=cv2.bitwise_or(allcolorsImg,detectedColors)
allcolorsImg= cv2.medianBlur(allcolorsImg,7)
return allcolorsImg
def colorOrder(img,finalColorArray):
newimg=img.copy()
arraycolor=[]
allcolorsImg= detectAllColors(img,finalColorArray)
allcolorsImgG= cv2.cvtColor(allcolorsImg, cv2.COLOR_BGR2GRAY)
ColoredContour, Coloredhierarchy = cv2.findContours(allcolorsImgG, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
Coloredhierarchy=Coloredhierarchy[0]
for cnt in ColoredContour :
Blackmask = np.zeros(img.shape[:2], dtype="uint8")
cv2.drawContours(Blackmask,[cnt],0,(255,255,255),20)
coloredand=cv2.bitwise_and(allcolorsImg,allcolorsImg,mask=Blackmask)
for colors in finalColorArray:
getColor=DetectColor(coloredand,colors)[1]
pil_image=Image.fromarray(getColor)
extrema = pil_image.convert("L").getextrema()
if extrema != (0, 0): # if image is not black --> has a colored mask within
arraycolor.append(colors)
break
res = []
[res.append(x) for x in arraycolor if x not in res]
return res
def getinnerColor(BlackmaskDetected,img,detectedColors,finalColorArray,num1,num2,flag,eachcolor):
countBlackMasks=0
xored=detectedColors
invertedmask=detectedColors
imgc=img.copy()
imgNewCopy=img.copy()
Blackmask = np.zeros(img.shape[:2], dtype="uint8")
for eachcolor in finalColorArray:
masked=DetectColor(detectedColors,eachcolor)[0]
pil_image=Image.fromarray(masked)
extrema = pil_image.convert("L").getextrema()
if extrema != (0, 0): # if image is not black --> has a colored mask within
cc=detectedColors.copy()
# cc1=detectedColorsB.copy()
ColoredContour, Coloredhierarchy = cv2.findContours(masked, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for cnt in ColoredContour:
area1 = cv2.contourArea(cnt)
if (area1 > 1000 ):
x, y , width, height = cv2.boundingRect(cnt)
# cv2.rectangle(cc, (x,y ), (x+width, y+height), (255,255,255), -1)
# cv2.rectangle(Blackmask, (x,y ), (x+width, y+height), 255, -1)
#to get rid of the edge of the inner reectangles
cv2.drawContours(cc,[cnt],0,(255,255,255), 3)
cv2.drawContours(Blackmask,[cnt] ,0, (255,255,255), 3)
cv2.drawContours(cc,[cnt],0,(255,255,255), -1) # (x-5,y-5 ), (x+width, y+height),
cv2.drawContours(Blackmask,[cnt] ,0, (255,255,255), -1) #,(x,y ), (x+width, y+height)
cv2.drawContours(BlackmaskDetected,[cnt] ,0, (0,0,0), -1) #,(x,y ), (x+width, y+height)
invertedmask = cv2.bitwise_and(imgc,imgc, mask= Blackmask)
xored=cc
# masked b abyad
detectedColors=xored
else: #black mask , no other levels are found # to check law count == number of colors in array yb2a no more levels and break
countBlackMasks+=1
return xored,invertedmask , BlackmaskDetected
def allLevelsofColor(BlackmaskDetected,img,levelonly, invertedmask,color,finalColorArray):
# cc=levelonly.copy()
firstLevel=levelonly
firstLevel1=levelonly
print('in')
Blackmask = np.zeros(img.shape[:2], dtype="uint8")
masked,maskedColor,rgbcolor=DetectColor(invertedmask,color)
color=hexRGB(color)
color=[color[0],color[1],color[2]]
rgbcolor=[rgbcolor[0],rgbcolor[1],rgbcolor[2]]
print(rgbcolor,color)
pil_image=Image.fromarray(masked)
extrema = pil_image.convert("L").getextrema()
if extrema != (0, 0): # if image is not black --> has a colored mask within
if rgbcolor==color: #found level tany gowa b nfs el lon
print('kkkkkkkk')
ColoredContour, Coloredhierarchy = cv2.findContours(masked, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
Coloredhierarchy=Coloredhierarchy[0]
for component in zip(ColoredContour,Coloredhierarchy):
cnt=component[0]
hier=component[1]
area1 = cv2.contourArea(cnt)
if (area1 > 1000 ):
if hier[3]> -1:
cv2.drawContours(Blackmask,[cnt],0,(255,255,255), -1)
cv2.drawContours(Blackmask,[cnt],0,(0,0,0), 20)
cv2.drawContours(BlackmaskDetected,[cnt],0,(255,255,255), -1)
firstLevel=cv2.bitwise_and(invertedmask,invertedmask,mask=Blackmask)
####remove black pixels and let them be all white
# get (i, j) positions of all RGB pixels that are black (i.e. [0, 0, 0])
black_pixels = np.where(
(firstLevel[:, :, 0] == 0) &
(firstLevel[:, :, 1] == 0) &
(firstLevel[:, :, 2] == 0)
)
# set those pixels to white
firstLevel[black_pixels] = [255, 255, 255]
firstLevel1=cv2.bitwise_and(levelonly,firstLevel)
# cv2_imshow(firstLevel1)
# cv2_imshow(firstLevel1)
for othercolor in finalColorArray:
othercolor2=hexRGB(othercolor)
othercolor2=[othercolor2[0],othercolor2[1],othercolor2[2]]
print(othercolor2,color)
if othercolor2!=color:
print('anothre')
masked0=DetectColor(firstLevel,othercolor)[0]
pil_image0=Image.fromarray(masked0)
extrema0 = pil_image0.convert("L").getextrema()
if extrema != (0, 0): # if image is not black --> has a colored mask within
ColoredContour0, Coloredhierarchy0 = cv2.findContours(masked0, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for cnt in ColoredContour0:
area1 = cv2.contourArea(cnt)
if (area1 > 1000 ):
cv2.drawContours(firstLevel1,[cnt],0,(255,255,255), -1)
cv2.drawContours(firstLevel1,[cnt],0,(255,255,255), 10)
cv2.drawContours(BlackmaskDetected,[cnt],0,(0,0,0), -1)
# cv2.drawContours(Blackmask,[cnt],0,(255,255,255), -1)
# cv2.drawContours(Blackmask,[cnt],0,(255,255,255), 10)
# cv2_imshow(firstLevel1)
# cv2_imshow(Blackmask)
return firstLevel1, BlackmaskDetected
def getColoredContour(mask,img,finalColorArray,num1,num2,flag,eachcolor):
print('uuuuuuuuummmmmmmmmmmmm')
ColoredContour, Coloredhierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
Coloredhierarchy=Coloredhierarchy[0]
imgc= img.copy()
detectedColors=np.zeros(img.shape[:2], dtype="uint8")
Blackmask = np.zeros(img.shape[:2], dtype="uint8")
for component in zip( ColoredContour, Coloredhierarchy):
cnt=component[0]
hier=component[1]
area1 = cv2.contourArea(cnt)
if (area1 > 3000 ):
# cv2.drawContours(imgNewCopy, [cnt], 0,(255,255,255), 20) #(x+20,y+20 ), (x+width-20, y+height-20),
if hier[3] >-1:
x, y , width, height = cv2.boundingRect(cnt)
cv2.drawContours(Blackmask, [cnt], 0,(255,255,255), -1) #(x+20,y+20 ), (x+width-20, y+height-20),
cv2.drawContours(Blackmask, [cnt], 0,(0,0,0), 10) #(x+20,y+20 ), (x+width-20, y+height-20),
detectedColors = cv2.bitwise_and(imgc,imgc, mask= Blackmask)
pil_image=Image.fromarray(detectedColors)
extrema = pil_image.convert("L").getextrema()
if extrema == (0, 0) :#and extremaB==(0,0): # if image is not black --> has a colored mask within
break
levelOnly,invertedmask,BlackmaskDetected=getinnerColor(Blackmask,img,detectedColors,finalColorArray,num1,num2,flag,eachcolor) #mask inner levels b abyad
firstLevel1, BlackmaskDetected1= allLevelsofColor(BlackmaskDetected,img,levelOnly, invertedmask,eachcolor,finalColorArray)
# cv2_imshow(firstLevel1)
print('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')
return firstLevel1,invertedmask, BlackmaskDetected1
"""# contours"""
def findContoursFullImage(green2,img,number,finalColorArray,num1,num2,flag,color=[0,0,0]):
if number == 0:
thresh=preprocess(img,number,green2)
contourss, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
return contourss
else:
mask, detectedColors, rgbcolor =DetectColor(img,color)
pil_image=Image.fromarray(mask)
extrema = pil_image.convert("L").getextrema()
if extrema != (0, 0): # if image is not black --> has a colored mask within
coloredregions,invertedmask,BlackmaskDetected1=getColoredContour(mask,img,finalColorArray,num1,num2,flag,color)
thresh=preprocess(coloredregions,number,green2)
x=cv2.bitwise_and(thresh,thresh,mask=BlackmaskDetected1)
contourss, hierarchy = cv2.findContours(x, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
return contourss,rgbcolor ,invertedmask
else:
thresh=preprocess(img,number,green2)
contourss, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
hierarchy = hierarchy[0]
return contourss,color ,mask
def StraightenImage(contour,imgArea):
rect = cv2.minAreaRect(contour)
(center, (width, height), angleR) = cv2.minAreaRect(contour)
box = cv2.boxPoints(rect)
box = np.int0(box)
# get width and height of the detected rectangle
width = int(rect[1][0])
height = int(rect[1][1])
# src_pts = box.astype("float32")
# dst_pts = np.array([[0, height-1],
# [0, 0],
# [width-1, 0],
# [width-1, height-1]], dtype="float32")
# # the perspective transformation matrix
# M = cv2.getPerspectiveTransform(src_pts, dst_pts)
# # directly warp the rotated rectangle to get the straightened rectangle
# warped = cv2.warpPerspective(imgArea, M, (width, height))
##############
return angleR,width,height
def getAreasPerimeter(green2,img,number,num1,num2,flag,finalColorArray,color=[0,0,0]):
appended=[]
if number==0:
contourss=findContoursFullImage(green2,img,number,finalColorArray,num1,num2,flag,color)
else:
contourss=findContoursFullImage(green2,img,number,finalColorArray,num1,num2,flag,color)[0]
for contour in contourss:
area1 = cv2.contourArea(contour)
perimeter1 = cv2.arcLength(contour, True)
x, y , width, height = cv2.boundingRect(contour)
angleR,widthR ,heightR= StraightenImage(contour,img)
if (angleR != 90.0 and angleR != -90.0 and angleR != 0.0 and angleR != -0.0 ): #inclined b ay degree
width=widthR
height=heightR
if (area1 > 4000 ): #check perimeter kman fl condition -- 2800
if num1!=0 and num2!=0:
if flag=='area':
# addedMargin=area1+perimeter1*2
# areaa=round(addedMargin* (num1/(num2+perimeter1*2) ), 3) # true value of area of any shape/ area px value of same shape
areaa=round(area1*(num1/num2),3)
appended.append([areaa,width,height])
else:
perimeter=round(perimeter1*(num1/num2),3)
appended.append([perimeter,width,height])
return appended
def FillDictionary(green2,SimilarAreaDictionary,img,number,num1,num2,flag,finalColorArray,rgbcolor=[0,0,0],color=[0,0,0]):
#fills dictionary with key areas and number of occurences
print('wttttt')
areas_Perimeters=sorted(getAreasPerimeter(green2,img,number,num1,num2,flag,finalColorArray,color) )
print(areas_Perimeters)
indices=[]
colorRanges=[[255,153,153],[51,255,51],[201,56,147],[255,0,0],[255,0,255],[0,102,204],[102,0,102],[153,0,76],[200,92,135],[52,161,99],[235,250,24],[40,30,170],[98,149,63],[100,30,179],[200,55,67],[150,80,200],[0,102,102],[250,28,191],[101,27,101],[230,150,76],[3,65,127],[114,39,39],[250,36,100],[180,30,40],[10,250,60],[140,30,253],[114,58,245],[47,255,255],[18,236,206],[225,105,29],[189,65,121],[206,204,48],[126,7,247],[3,168,251]]
print(colorRanges[0])
print(colorRanges[0][0],colorRanges[0][1], colorRanges[0][2])
colorsUsed=[]
for i in range(len(areas_Perimeters)):
print(colorRanges)
# colorRGB=hexRGB(color)
item1 = areas_Perimeters[i][0]
width1 = areas_Perimeters[i][1]
height1 = areas_Perimeters[i][2]
widthMin= width1-5
widthMax= width1+5
heightMin=height1-5
heightMax= height1+5
areaPerimeterMin= round(item1,1) - 0.3
areaPerimeterMax= round(item1,1) + 0.3
# print (areaMin, areaMax)
if color != [0,0,0]: #colored images
mydata=[[rgbcolor[0],rgbcolor[1],rgbcolor[2] ],round(item1,1),width1,height1,0, 1,0,0,0,0]
# mydata=[round(item1,1),width1,height1,0, 1,0,[rgbcolor[0],rgbcolor[1],rgbcolor[2] ],colorRanges[0][2],colorRanges[0][1],colorRanges[0][0]]
# colorRanges.pop(0)
else:
# print('??')
mydata=[' ', round(item1,1),width1,height1,0, 1,0,0,0,0]
# if (( round(item1,1) in SimilarAreaDictionary['Rounded'].values) or (areaMin in SimilarAreaDictionary['Rounded'].values )or (areaMax in SimilarAreaDictionary['Rounded'].values )):
# myindex= SimilarAreaDictionary.index[( SimilarAreaDictionary['Rounded']== round(item1,1) ) ].tolist()
myindex= SimilarAreaDictionary.index[((SimilarAreaDictionary['Rounded'] >=areaPerimeterMin) &(SimilarAreaDictionary['Rounded']<=areaPerimeterMax) )].tolist()
print(myindex)
# for i in myindex:
# SimilarAreaDictionary['Rounded'].loc[i]
if color!= [0,0,0]: #leveled image
checkifColorExists=0 # to check whether this row was found or not( area and color )
for i in myindex: # loop on indices that were found --> rows containing this area to check its color and add occ.
if SimilarAreaDictionary['Color'].loc[i]==[rgbcolor[0],rgbcolor[1],rgbcolor[2]] and ( SimilarAreaDictionary['Rounded'].loc[i] >= areaPerimeterMin and SimilarAreaDictionary['Rounded'].loc[i] <= areaPerimeterMax) :
if (SimilarAreaDictionary['Width'].loc[i] <=widthMax and SimilarAreaDictionary['Width'].loc[i] >= widthMin) and (SimilarAreaDictionary['Height'].loc[i] <= heightMax and SimilarAreaDictionary['Height'].loc[i] >= heightMin ) or (SimilarAreaDictionary['Width'].loc[i] <=heightMax and SimilarAreaDictionary['Width'].loc[i] >= heightMin) and (SimilarAreaDictionary['Height'].loc[i] <= widthMax and SimilarAreaDictionary['Height'].loc[i] >= widthMin ) :
checkifColorExists=1 #found and incremented
SimilarAreaDictionary['Occurences'].loc[i]+=1
if checkifColorExists==0: #couldnt find the color , doesnt exist so add it
SimilarAreaDictionary.loc[len(SimilarAreaDictionary)] =mydata
else: #full image
# print('here')
#same code bs mgher color
checkifColorExists=0
for i in myindex: #(SimilarAreaDictionary['Rounded'].loc[i] == round(item1,1) ) or
if ( SimilarAreaDictionary['Rounded'].loc[i] <= areaPerimeterMax and SimilarAreaDictionary['Rounded'].loc[i] >= areaPerimeterMin) :
# print(SimilarAreaDictionary['Rounded'].loc[i] ,'in rng if', areaMin,areaMax)
if (SimilarAreaDictionary['Width'].loc[i] <=widthMax and SimilarAreaDictionary['Width'].loc[i] >= widthMin) and (SimilarAreaDictionary['Height'].loc[i] <= heightMax and SimilarAreaDictionary['Height'].loc[i] >= heightMin ) or (SimilarAreaDictionary['Width'].loc[i] <=heightMax and SimilarAreaDictionary['Width'].loc[i] >= heightMin) and (SimilarAreaDictionary['Height'].loc[i] <= widthMax and SimilarAreaDictionary['Height'].loc[i] >= widthMin ) :
checkifColorExists=1 #found and incremented
SimilarAreaDictionary['Occurences'].loc[i]+=1
# SimilarAreaDictionary['R'].loc[i] =colorRanges[0][2]
# SimilarAreaDictionary['G'].loc[i] =colorRanges[0][1]
# SimilarAreaDictionary['B'].loc[i] = colorRanges[0][0]
# colorRanges.pop(0)
if checkifColorExists==0: #couldnt find the color , doesnt exist so add it
SimilarAreaDictionary.loc[len(SimilarAreaDictionary)] =mydata
s= SimilarAreaDictionary
for i in range(len(SimilarAreaDictionary)):
SimilarAreaDictionary.loc[i, "R"] = colorRanges[i][0]
SimilarAreaDictionary.loc[i, "G"] = colorRanges[i][1]
SimilarAreaDictionary.loc[i, "B"] = colorRanges[i][2]
# x='#%02x%02x%02x' % (colorRanges[i])
# xx='#%02x%02x%02x' % ( colorRanges[i][0] , colorRanges[i][1] , colorRanges[i][2] )
# print( xx)
colorsUsed.append(colorRanges[i])
# s=( SimilarAreaDictionary.style.apply(
# lambda col: ['background-color: %s' % ('#%02x%02x%02x' % ( colorRanges[i][0] , colorRanges[i][1] , colorRanges[i][2] )) for i in range(len(SimilarAreaDictionary))],
# subset=['Color']
# ) )
# '#%02x%02x%02x' % (colorRanges[i])
return SimilarAreaDictionary, colorsUsed
def drawAllContours(plan,green2,img,number,finalColorArray,num1,num2,flag):
# colors=[color,color1,color2,color3,color4,color5,color6,color7]
# img=np.array(img)
areasinImage=[]
totaldf=pd.DataFrame()
imgArea1= img.copy()
imgPerimeter1=img.copy()
imgtransparent1=img.copy()
Blackmask = np.zeros(img.shape[:2], dtype="uint8")
invertedmask=img
allpoints=[]
if number ==220:
finalColorArray= colorOrder(img,finalColorArray)
if flag== 'area':
SimilarAreaDictionary= pd.DataFrame(columns=['Color','Rounded','Width','Height','Area','Occurences','Total Area' , 'R','G','B']) #
else:
SimilarAreaDictionary= pd.DataFrame(columns=['Color','Rounded','Width','Height','Perimeter','Occurences','Total Perimeter' ,'R','G','B'])
firstcolor=finalColorArray[0]
# print(lastcolor)
counter=0
maskDone=img.copy()
for eachcolor in finalColorArray:
if eachcolor==firstcolor: # 3shan a3rf el array of colors et3adet kam mara - to support embedded levels
counter+=1
contourss,rgbcolor,invertedmask=findContoursFullImage(green2,maskDone,number,finalColorArray,num1,num2,flag,eachcolor)
SimilarAreaDictionary ,colorsUsed= FillDictionary(green2,SimilarAreaDictionary,maskDone,number,num1,num2,flag,finalColorArray,rgbcolor,eachcolor)
a = SimilarAreaDictionary.to_numpy()
for contour in contourss:
shape=[]
area1 = cv2.contourArea(contour)
perimeter1 = cv2.arcLength(contour, True)
if (area1 > 4000 ): #check perimeter kman fl condition -- 2800
angleR,widthR ,heightR= StraightenImage(contour,imgArea1)
rect = cv2.minAreaRect(contour)
# (center, (width, height), angleR) = cv2.minAreaRect(contour)
# box = cv2.boxPoints(rect)
# box = box.astype('int')
x, y , width, height = cv2.boundingRect(contour)
# cv2.drawContours(imgArea1,contours=[box], contourIdx=0 , color=(0, 0, 255), thickness=10)
approx = cv2.approxPolyDP(contour, 0.005 * perimeter1, True)
for point in approx:
x1, y1 = point[0]
# shape.append([int(x1*ratio2),int(y1*ratio2)])
cv2.circle(imgArea1, (x1, y1), 4, (0, 255, 0), -1)
# allpoints.append(shape)
if (angleR != 90.0 and angleR != -90.0 and angleR != 0.0 and angleR != -0.0 ): #inclined b ay degree
width=widthR
height=heightR
widthMin= width-5
widthMax= width+5
heightMin=height-5
heightMax= height+5
if num1 !=0 and num2!=0:
widthh=round(width*(num1/num2),3)
heightt=round(height*(num1/num2),3)
if flag=='area':
addedMargin=area1+perimeter1*2
areaa=round(addedMargin* (num1/(num2+perimeter1*2) ), 3) # true value of area of any shape/ area px value of same shape
elif flag=='perimeter':
perimeterr=round(perimeter1* (num1/num2 ), 3)
else:
areaa=area1
perimeterr=perimeter1
if flag=='area':
areaPerimeterMin= round(areaa,1) - 0.3
areaPerimeterMax= round(areaa,1) + 0.3
masked=SimilarAreaDictionary.loc[SimilarAreaDictionary.index[((SimilarAreaDictionary['Rounded'] >=areaPerimeterMin) &(SimilarAreaDictionary['Rounded']<=areaPerimeterMax) )]]
# masked=SimilarAreaDictionary.loc[SimilarAreaDictionary['Rounded'] ==round(areaa,1)]
# if (round(areaa,1) in masked['Rounded'].values ) :
passed=0
for i, row in masked.iterrows():
if passed ==0:
if SimilarAreaDictionary['Color'].loc[i] == [rgbcolor[0],rgbcolor[1],rgbcolor[2]] and ( SimilarAreaDictionary['Rounded'].loc[i] <= areaPerimeterMax and SimilarAreaDictionary['Rounded'].loc[i] >= areaPerimeterMin) :
if (SimilarAreaDictionary['Width'].loc[i] <=widthMax and SimilarAreaDictionary['Width'].loc[i] >= widthMin) and (SimilarAreaDictionary['Height'].loc[i] <= heightMax and SimilarAreaDictionary['Height'].loc[i] >= heightMin ) or (SimilarAreaDictionary['Width'].loc[i] <=heightMax and SimilarAreaDictionary['Width'].loc[i] >= heightMin) and (SimilarAreaDictionary['Height'].loc[i] <= widthMax and SimilarAreaDictionary['Height'].loc[i] >= widthMin ) :
SimilarAreaDictionary['Total Area'].loc[i]+=areaa
SimilarAreaDictionary['Area'].loc[i]=areaa
passed=1
# print(index)
cv2.drawContours(imgArea1, [contour], 0, (int(rgbcolor[2]), int(rgbcolor[1]), int(rgbcolor[0])), -1)
# apdf.add_annotation(
# 'polygon',
# Location(points=shape ,page=0),
# Appearance(stroke_color=(1, 0, 1), stroke_width=5,fill=(0.2,0.3,0.8),fill_transparency=0.5),
# )
cv2.putText(imgtransparent1,'Area= '+str(areaa) + ' m', (x+50,y-10) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
areasinImage.append(areaa)
elif flag=='perimeter':
areaPerimeterMin= round(perimeterr,1) - 0.3
areaPerimeterMax= round(perimeterr,1) + 0.3
masked=SimilarAreaDictionary.loc[SimilarAreaDictionary.index[((SimilarAreaDictionary['Rounded'] >=areaPerimeterMin) &(SimilarAreaDictionary['Rounded']<=areaPerimeterMax) )]]
passed=0
# if (round(perimeterr,1) in masked['Rounded'].values ) :
for i, row in masked.iterrows():
if passed ==0:
if SimilarAreaDictionary['Color'].loc[i] == [rgbcolor[0],rgbcolor[1],rgbcolor[2]] and ( SimilarAreaDictionary['Rounded'].loc[i] <= areaPerimeterMax and SimilarAreaDictionary['Rounded'].loc[i] >= areaPerimeterMin) :
if (SimilarAreaDictionary['Width'].loc[i] <=widthMax and SimilarAreaDictionary['Width'].loc[i] >= widthMin) and (SimilarAreaDictionary['Height'].loc[i] <= heightMax and SimilarAreaDictionary['Height'].loc[i] >= heightMin ) or (SimilarAreaDictionary['Width'].loc[i] <=heightMax and SimilarAreaDictionary['Width'].loc[i] >= heightMin) and (SimilarAreaDictionary['Height'].loc[i] <= widthMax and SimilarAreaDictionary['Height'].loc[i] >= widthMin ) :
SimilarAreaDictionary['Total Perimeter'].loc[i]+=perimeterr
SimilarAreaDictionary['Perimeter'].loc[i]=perimeterr
passed=1
cv2.drawContours(imgPerimeter1, [contour], 0, (int(rgbcolor[2]), int(rgbcolor[1]), int(rgbcolor[0])), 3)
cv2.putText(imgPerimeter1,'Perimeter'+str(perimeterr), (x+50,y-10) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
# cv2.putText(imgPerimeter1,'width='+str(width), (x+50,y-10) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
# cv2.putText(imgPerimeter1,'height='+str(height), (x+30,y-30) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
areasinImage.append(perimeterr)
# cv2_imshow(imgArea1)
for i,row in SimilarAreaDictionary.iterrows():
# print(row)
if row[3] not in areasinImage: # column of area
SimilarAreaDictionary = SimilarAreaDictionary.drop(SimilarAreaDictionary.loc[SimilarAreaDictionary.index==i].index)
else:
if flag=='area':
SimilarAreaDictionary= pd.DataFrame(columns=['Color','Rounded','Width','Height','Area','Occurences','Total Area','R','G','B']) #
# print('generated')
else:
SimilarAreaDictionary= pd.DataFrame(columns=['Color','Rounded','Width','Height','Perimeter','Occurences','Total Perimeter','R','G','B'])
contourss=findContoursFullImage(green2,img,number,finalColorArray,num1,num2,flag)
SimilarAreaDictionary , colorsUsed= FillDictionary(green2,SimilarAreaDictionary,img,number,num1,num2,flag,finalColorArray)
for contour in contourss:
area1 = cv2.contourArea(contour)
perimeter1 = cv2.arcLength(contour, True)
if (area1 >4000 ):
angleR,widthR ,heightR= StraightenImage(contour,imgArea1)
x, y , width, height = cv2.boundingRect(contour)
if (angleR != 90.0 and angleR != -90.0 and angleR != 0.0 and angleR != -0.0 ): #inclined b ay degree
width=widthR
height=heightR
widthMin= width-5
widthMax= width+5
heightMin=height-5
heightMax= height+5
if num1 !=0 and num2!=0:
if flag=='area':
# addedMargin=area1+perimeter1*2
# areaa=round(addedMargin* (num1/(num2+perimeter1*2) ), 3) # true value of area of any shape/ area px value of same shape
areaa=round(area1*(num1/num2),3)
elif flag=='perimeter':
perimeterr=round(perimeter1* (num1/num2 ), 3)
else:
areaa=area1
perimeterr=perimeter1
if flag=='area':
areaPerimeterMin= round(areaa,1) - 0.3
areaPerimeterMax= round(areaa,1) + 0.3
masked=SimilarAreaDictionary.loc[SimilarAreaDictionary.index[((SimilarAreaDictionary['Rounded'] >=areaPerimeterMin) &(SimilarAreaDictionary['Rounded']<=areaPerimeterMax) )]]
passed=0
# if (round(areaa,1) in masked['Rounded'].values ) :
for i, row in masked.iterrows():
if passed ==0:
if ( SimilarAreaDictionary['Rounded'].loc[i] <= areaPerimeterMax and SimilarAreaDictionary['Rounded'].loc[i] >= areaPerimeterMin) :
if (SimilarAreaDictionary['Width'].loc[i] <=widthMax and SimilarAreaDictionary['Width'].loc[i] >= widthMin) and (SimilarAreaDictionary['Height'].loc[i] <= heightMax and SimilarAreaDictionary['Height'].loc[i] >= heightMin ) or (SimilarAreaDictionary['Width'].loc[i] <=heightMax and SimilarAreaDictionary['Width'].loc[i] >= heightMin) and (SimilarAreaDictionary['Height'].loc[i] <= widthMax and SimilarAreaDictionary['Height'].loc[i] >= widthMin ) :
SimilarAreaDictionary['Total Area'].loc[i]+=areaa
SimilarAreaDictionary['Area'].loc[i]=areaa
passed=1
cv2.drawContours(imgArea1, [contour], 0, ( int(SimilarAreaDictionary['B'].loc[i]), int(SimilarAreaDictionary['G'].loc[i]), int(SimilarAreaDictionary['R'].loc[i])), -1)
cv2.putText(imgtransparent1,'Area= '+str(areaa) + ' m', (x+50,y-10) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
cv2.drawContours(imgArea1, [contour], 0, (0, 0, 255), 4)
elif flag=='perimeter':
areaPerimeterMin= round(perimeterr,1) - 0.3
areaPerimeterMax= round(perimeterr,1) + 0.3
masked=SimilarAreaDictionary.loc[SimilarAreaDictionary.index[((SimilarAreaDictionary['Rounded'] >=areaPerimeterMin) &(SimilarAreaDictionary['Rounded']<=areaPerimeterMax) )]]
passed=0
# if (round(perimeterr,1) in masked['Rounded'].values ) :
for i, row in masked.iterrows():
if passed ==0:
if ( SimilarAreaDictionary['Rounded'].loc[i] <= areaPerimeterMax and SimilarAreaDictionary['Rounded'].loc[i] >= areaPerimeterMin) :
if (SimilarAreaDictionary['Width'].loc[i] <=widthMax and SimilarAreaDictionary['Width'].loc[i] >= widthMin) and (SimilarAreaDictionary['Height'].loc[i] <= heightMax and SimilarAreaDictionary['Height'].loc[i] >= heightMin ) or (SimilarAreaDictionary['Width'].loc[i] <=heightMax and SimilarAreaDictionary['Width'].loc[i] >= heightMin) and (SimilarAreaDictionary['Height'].loc[i] <= widthMax and SimilarAreaDictionary['Height'].loc[i] >= widthMin ) :
SimilarAreaDictionary['Total Perimeter'].loc[i]+=perimeterr
SimilarAreaDictionary['Perimeter'].loc[i]=perimeterr
passed=1
cv2.drawContours(imgPerimeter1, [contour], 0, (0, 0, 255), 4)
# cv2.putText(imgPerimeter1,'width='+str(width), (x+50,y-10) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
# cv2.putText(imgPerimeter1,'height='+str(height), (x+30,y-30) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
cv2.putText(imgPerimeter1,'Perimeter='+str(perimeterr), (x+50,y-10) ,cv2.FONT_HERSHEY_SIMPLEX, 0.6, (50, 50, 255), 2)
cv2.drawContours(imgArea1, [contour], 0, (0, 0, 255), 4)
alpha = 0.4 # Transparency factor.
image_new1 = cv2.addWeighted(imgArea1, alpha, imgtransparent1, 1 - alpha, 0)
# if flag=='area':
# cv2_imshow(image_new1)
# else:
# cv2_imshow(imgPerimeter1)
SimilarAreaDictionary.drop(['Rounded', 'Width','Height','R','G','B'], axis=1, inplace=True)
# apdf.write('b.pdf')
# annotationsDraw
return imgPerimeter1,image_new1,SimilarAreaDictionary , colorsUsed
# drawAllContours(img,0,[],1.11,25579,'area') #,[190,47,250] ,[47,251,255] ,[80,240,15],[253,163,40]
# imgPerimeter1,image_new1,Dictionary=drawAllContours(img,220,[47,251,255],[251,163,47],1.105,27233.5,'area')
###############################################
##Google Sheets Legend
def retrieveMCCol(gc):
ws=gc.open_by_key('1A8VtqLFhe2NXPxIjfAilbxF9xV2eSzZ-yZ9GP8_5jSo')
worksheet = ws.worksheet(0)
mcT_Names=worksheet.get_col(1)
newMcTNames=[]
for i in mcT_Names:
if i != '':
newMcTNames.append(i)
return newMcTNames
def getdropdownValues(): #gc,spreadsheet_service
SCOPES = [
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive'
]
credentials = service_account.Credentials.from_service_account_file('credentials.json', scopes=SCOPES)
spreadsheet_service = build('sheets', 'v4', credentials=credentials)
drive_service = build('drive', 'v3', credentials=credentials)
gc = pygsheets.authorize(custom_credentials=credentials, client_secret='credentials.json')
dropdownValues=[]
allIds=gc.spreadsheet_ids()
for spreadsheetId in allIds:
if spreadsheetId != '1A8VtqLFhe2NXPxIjfAilbxF9xV2eSzZ-yZ9GP8_5jSo':
print(spreadsheetId)
ws=gc.open_by_key('1A8VtqLFhe2NXPxIjfAilbxF9xV2eSzZ-yZ9GP8_5jSo') ## spreadsheet containing mc-t names
worksheet = ws.worksheet(0)
response = spreadsheet_service.spreadsheets().get(
spreadsheetId=spreadsheetId, fields='*',
ranges='A2:A60',includeGridData=True).execute()
r=list(response['sheets'][0]['data'][0]['rowData'][0]['values'][0])
print(r)
if 'dataValidation' in r:
print('yes')
colvals= response['sheets'][0]['data'][0]['rowData'][0]['values'][0]['dataValidation']
colvalsList=list(colvals.items())
# print(colvalsList[0][1])
lengthVals=len(colvalsList[0][1]['values'])
for i in range(lengthVals):
dictVal=(colvalsList[0][1]['values'][i].values())
# val=[*dictVal]
x=[*dictVal][0]
# print(x)
if x not in dropdownValues:
dropdownValues.append(*dictVal)
print(dropdownValues)
# worksheet.delete_cols(1,1)
worksheet.update_col(index=1, values=dropdownValues)
return dropdownValues
def createGoogleSheet(plan):
print('create1')
# authorize uing json file
# SimilarAreaDictionary.drop(['Rounded', 'Width','Height','R','G','B'], axis=1, inplace=True)
SCOPES = [
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive'
]
credentials = service_account.Credentials.from_service_account_file('credentials.json', scopes=SCOPES)
spreadsheet_service = build('sheets', 'v4', credentials=credentials)
drive_service = build('drive', 'v3', credentials=credentials)
gc = pygsheets.authorize(custom_credentials=credentials, client_secret='credentials.json')
spreadsheet_details = {
'properties': {
'title': 'Legend of ' + str(plan)
}
}
sheet = spreadsheet_service.spreadsheets().create(body=spreadsheet_details,
fields='spreadsheetId').execute()
spreadsheetId = sheet.get('spreadsheetId')
# print('Spreadsheet ID: {0}'.format(spreadsheetId))
permission1 = {
'type': 'anyone',
'role': 'writer',
# 'emailAddress': 'marthe.adr@gmail.com'
}
drive_service.permissions().create(fileId=spreadsheetId, body=permission1).execute()
print('createee')
return spreadsheetId,spreadsheet_service,gc
def legendGoogleSheets(path,SimilarAreaDictionary,colorsUsed, spreadsheetId,spreadsheet_service,gc):
########
titles=gc.spreadsheet_titles()
# print(titles)
# if legendTitle in titles:
# print('found sheet ')
# else:
####### create new sheet
print('creating new sheeet')
###################3
#open sheet
# spreadsheetId='1dtDi_6-g3jkn6ePVlzM6PM3FE8wIHzyL2Rt4ksH59SE'
ws=gc.open_by_key(spreadsheetId)
worksheet = ws.worksheet(0)
#get lengths of df
columnsLen=len(SimilarAreaDictionary.columns.values.tolist()) #kam column -- last col = columnsLen+1 3shan base0
lastUsedCol=columnsLen+1
rowsLen=len(SimilarAreaDictionary.values.tolist()) #kam row -- last row = rowsLen +1
lastUsedRow=rowsLen+1
#append to googlesheet
worksheet.update_row(1,SimilarAreaDictionary.columns.values.tolist() ,col_offset=1)
worksheet.append_table(SimilarAreaDictionary.values.tolist(), dimension='ROWS' )
#names
worksheet.update_col(index=1,values=['MC-Template Names'],row_offset=0)
#apply 'A1' notation
firstcell=worksheet.cell((2,1)) #row,col
firstcellNotation=str(firstcell.address.label)
lastcell=worksheet.cell((rowsLen+1,1)) #row,col
lastcellNotation=str(lastcell.address.label)
lastcolumn=worksheet.cell((1,lastUsedCol)) #row,col
lastcolumnNotation=str(lastcolumn.address.label)
#dropdowns -
mcT_Names=retrieveMCCol(gc)
worksheet.set_data_validation(firstcellNotation,lastcellNotation, condition_type='ONE_OF_LIST', condition_values=mcT_Names, showCustomUi=True)
#format first row as bold
model_cell =worksheet.cell('A1')
model_cell.set_text_format('bold', True)
pygsheets.DataRange('A1',lastcolumnNotation, worksheet=worksheet).apply_format(model_cell)
worksheet.adjust_column_width(start=1,end=lastUsedCol)
sheetId = '0' # Please set sheet ID.
for i in range(len(colorsUsed)):
print(colorsUsed[i])
r,g,b=colorsUsed[i]
body = {
"requests": [
{
"updateCells": {
"range": {
"sheetId": sheetId,
"startRowIndex": i+1,
# "endRowIndex":4 ,
"startColumnIndex":1,
# "endColumnIndex": 0
},
"rows": [
{
"values": [
{
"userEnteredFormat": {
"backgroundColor": {
"red": r/255,
"green": g/255,
"blue": b/255,
# "alpha": 0.8
}
}
}
]
}
],
"fields": "userEnteredFormat.backgroundColor",
}
}
]
}
res = spreadsheet_service.spreadsheets().batchUpdate(spreadsheetId=spreadsheetId, body=body).execute()
spreadsheet_url = "https://docs.google.com/spreadsheets/d/%s" % spreadsheetId
print(spreadsheet_url)
# return gr.HTML( """Click <a href=%s> on this Google Sheet </a> to continue to the Legend.""" %spreadsheet_url)
return gc,spreadsheet_service,spreadsheetId , spreadsheet_url
def MainFunc( spreadsheetId,spreadsheet_service,gc,plan,green2,img,dp,finalColorArray,number,num1,num2,flag):
imgPerimeter1,image_new1,Df2,colorsUsed=drawAllContours(plan,green2,img,number,finalColorArray,num1,num2,flag)
gc,spreadsheet_service,spreadsheetId ,spreadsheet_url =legendGoogleSheets(plan,Df2,colorsUsed, spreadsheetId,spreadsheet_service,gc)
# x = path.split("/")
# x=x.pop()
Df2=Df2.T
Df2.loc['MC-Template-Name']=''
Df2.loc['Unit'] = str(dp) #get from user through a dropdown
Df2 = Df2.astype(str)
if (number ==220):
if flag=='area':
Df2=Df2.reindex(['MC-Template-Name','Area','Color', 'Occurences', 'Total Area', 'Unit'])
else:
Df2=Df2.reindex(['MC-Template-Name','Perimeter','Color', 'Occurences', 'Total Perimeter', 'Unit'])
else:
if flag=='area':
Df2=Df2.reindex(['MC-Template-Name','Area', 'Occurences', 'Total Area','Color', 'Unit'])
else:
Df2=Df2.reindex(['MC-Template-Name','Perimeter', 'Occurences', 'Total Perimeter','Color', 'Unit'])
# display(Df2)
return Df2, imgPerimeter1,image_new1 , spreadsheet_url
def PickColorContours( spreadsheetId,spreadsheet_service,gc,plan,dp,img,radioButton,radioButton1, color,color1,color2,color3,color4,color5,color6,color7,color8,num1=0,num2=0):
print(type(img))
green2=allpreSteps(img)
colorArray=[color,color1,color2,color3,color4,color5,color6,color7,color8]
finalColorArray=[]
for c in colorArray:
checkcolor=c.lstrip('#')
if checkcolor == '000000':
continue
else:
finalColorArray.append(c)
if num1==0 and num2 ==0 :
if radioButton=="Measure Full Image" :
if radioButton1=='Area':
imgPerimeter1,image_new1,df ,colorsUsed =drawAllContours(plan,green2,img,0,finalColorArray,num1,num2,'area')
return image_new1
else:
imgPerimeter1,image_new1,df ,colorsUsed =drawAllContours(plan,green2,img,0,finalColorArray,num1,num2,'perimeter')
return imgPerimeter1
else:
if radioButton1=='Area':
imgPerimeter1,image_new1,df ,colorsUsed =drawAllContours(plan,green2,img,220,finalColorArray,num1,num2,'area') #rgbValue
return image_new1
else:
imgPerimeter1,image_new1,df ,colorsUsed =drawAllContours(plan,green2,img,220,finalColorArray,num1,num2,'perimeter') #rgbValue
return imgPerimeter1
else:
if radioButton=="Measure Full Image":
if radioButton1=='Area':
Dictionary, imgPerimeter1,image_new1 ,spreadsheet_url =MainFunc( spreadsheetId,spreadsheet_service,gc,plan,green2,img,dp,finalColorArray,0,num1,num2,'area')
else:
Dictionary, imgPerimeter1,image_new1 ,spreadsheet_url=MainFunc( spreadsheetId,spreadsheet_service,gc,plan,green2,img,dp,finalColorArray,0,num1,num2,'perimeter')
else:
if radioButton1=='Area':
Dictionary, imgPerimeter1,image_new1 ,spreadsheet_url=MainFunc( spreadsheetId,spreadsheet_service,gc,plan,green2,img,dp,finalColorArray,220,num1,num2,'area')
else:
Dictionary, imgPerimeter1,image_new1 ,spreadsheet_url=MainFunc( spreadsheetId,spreadsheet_service,gc,plan,green2,img,dp,finalColorArray,220,num1,num2,'perimeter')
Dictionary=Dictionary.T
# s=s.to_html()
# Dictionary.to_excel("output.xlsx")
if (radioButton1=='Area'):
return image_new1,Dictionary ,spreadsheet_url#,str(rgbValue),
return imgPerimeter1,Dictionary ,spreadsheet_url
''' General measurement function'''
def getMeasurement(plan,SaveOP,check1, dp,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11,in12,num1=0,num2=0):
spreadsheetId,spreadsheet_service,gc=createGoogleSheet(plan)
if plan==None:
area,perim,df=None,None,None
elif 'foundation' in plan:
plan1='dropbox_plans/'+str(plan)
img=plan2img(plan1)
area,perim,df=IsolatedFoundations(img)
area,perim,df=img,None,None
elif 'piles' or 'pc' in plan: #any pile cap
# else:
plan1='dropbox_plans/'+str(plan)
# plan1='/content/drive/MyDrive/Colab Notebooks/Pile caps plans/13886-B3-NO_TEXT.pdf'
img=plan2img(plan1)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
if num1!=0 and num2!=0:
imgPerimeter1,Dictionary , spreadsheet_url = PickColorContours( spreadsheetId,spreadsheet_service,gc,plan1,dp,img,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11,in12,num1,num2)
imgPerimeter1= cv2.cvtColor(imgPerimeter1, cv2.COLOR_RGB2BGR)
if SaveOP:
pushToDropbox(plan,imgPerimeter1,Dictionary)
if check1:
exportToExcel(plan,Dictionary)
return imgPerimeter1,Dictionary , gr.HTML.update( """Click <a href=%s target="_blank"> on this Google Sheet </a> to continue to the Legend.""" %spreadsheet_url)
# return imgPerimeter1,Dictionary , gr.HTML.update( """Click <a href=%s> on this Google Sheet </a> to continue to the Legend.""" %spreadsheet_url)
else:
area=PickColorContours( spreadsheetId,spreadsheet_service,gc,plan1,dp,img,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11,in12)
area = cv2.cvtColor(area, cv2.COLOR_BGR2RGB)
# if SaveOP:
# pushToDropbox(plan, area)
return area
# else:
# areaPerimeterImg,df=PickColorContours(dp,img,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11,num1,num2)
# if SaveOP:
# pushToDropbox(plan,area,perim,df)
# return area,perim,df
###################################################
##########################################################
def update_dropdown(project):
plans_inrepo=os.listdir('dropbox_plans')
#if 'foundation' in radio and project in plans_inrepo
matches=[]
if project==None:
drop=gr.Dropdown.update(choices=matches)
else:
for x in plans_inrepo:
if (project in x): #project name and section in a plan
matches.append(x)
drop=gr.Dropdown.update(choices=matches)
return drop
######################################################################################
def clear(demo):
return None,None,None,None
###############################################################
def saveExcel(Dictionary,plan):
print('sdjbfkhbf')
plan2=str(plan)
# Dictionary.to_excel("output.xlsx",index=False)
# xmlFile=Dictionary.to_xml()
# print(xmlFile)
d=dropbox_upload_file('.',local_file=Dictionary,dropbox_file_path='/savedMeasurements/'+plan2+'summaryXML.xml')
# with gr.Column():
# with gr.Accordion("User Guide "):
# gr.Markdown("Please read this before you use this tool. \n Note : The algorithm is based on detecting shapes from the plan. Some shapes such as tables and textboxes containing information about the plan may be seen as a shape and inserted into the algorithm as well. To avoid this, please clean up the plan before you choose to measure it by erasing them or drawing a white rectangle above them using any external tool. \n Please note that the pdfs inserted in this tab should be pile caps only.\n The tool is divided into two main parts:\n * First Part (until the measure button) \n - You will find three dropdowns. Choose the name of the project you want to measure, along with the project part, and the project section. \n - Then choose whether this project contains levels or not. If you wish to measure all of the plan choose Measure full image. If you wish to measure certain regions of the plan or divide the plan into regions please choose Measure Specific regions. \n - If you choose Measure specific regions, please draw shapes around the desired regions on Bluebeam or any external tool before choosing to measure this plan. \n Also, you should insert the colors in rgb format (Red-Green-Blue) of the levels. \n The output of the first part will be in the unit pixels. To convert the measurements into a metric unit. Please refer to the the second part for detailed information. \n * Second part (Unit Conversion - This occurs once only) \n - Choose one of the shapes of the measured plan (preferably the largest shape) and measure it in Bluebeam. \n - In the first field labeled Real value, please enter the value you obtained from the measurement you made on Bluebeam. \n - In the second field labeled Pixel value, enter the pixel value of the same shape in which you measured that is shown in the output of the first part. \n - The unit in which the conversion will be into. \n This creates a ratio so that the conversion would be as accurate as possible. You can think of this as the scale check logic in Bluebeam. \n Outputs:\n The first output is an image of the plan measured and color sorted. \n The second output is a representation of the Legend exported.\n The column named MC-Template Name is empty for you to enter the MC-T name. \n The column named Area is the area measured of one of the shape.\n The column named Occurences is the number of count of this shape - This is just to make sure every shape was seen correctly and thus summed later correctly.\n The column named Total Area contains the summed areas of each shape \n The column named Unit refers to the unit in which the conversion was made to.")
with gr.Blocks(css="#search {background: green}") as mainBlock:
# spreadsheetId,spreadsheet_service,gc=createGoogleSheet()
# spreadsheet_url = "https://docs.google.com/spreadsheets/d/%s" % spreadsheetId
with gr.Tab("Main UI"):
with gr.Row():
# with gr.Column():
with gr.Accordion("User Guide ",open=False):
gr.Markdown("Please read this before you use this tool. \n Note : The algorithm is based on detecting shapes from the plan. Some shapes such as tables and textboxes containing information about the plan may be seen as a shape and inserted into the algorithm as well. To avoid this, please clean up the plan before you choose to measure it by erasing them or by drawing a white rectangle above them using any external tool. \n Please note that the pdfs inserted in this tab should be pile caps only.\n \n The tool is divided into two main parts: \n \n * First Part (until the measure button) \n \t - You will find three dropdowns. Choose the name of the project you want to measure, along with the project part, and the project section. \n \t- Then choose whether this project contains levels or not. If you wish to measure all of the plan choose Measure full image. If you wish to measure certain regions of the plan or divide the plan into regions please choose Measure Specific regions. \n \t - If you choose Measure specific regions, please draw shapes around the desired regions on Bluebeam or any external tool before choosing to measure this plan. \n Also, you should insert the colors in rgb format (Red-Green-Blue) of the levels. \n \n The output of the first part will be in the unit pixels. To convert the measurements into a metric unit. Please refer to the the second part for detailed information. \n \n * Second part (Unit Conversion - This occurs once only) \n \t - Choose one of the shapes of the measured plan (preferably the largest shape) and measure it in Bluebeam. \n \t - In the first field labeled Real value, please enter the value you obtained from the measurement you made on Bluebeam. \n \t - In the second field labeled Pixel value, enter the pixel value of the same shape in which you measured that is shown in the output of the first part. \n \t - The unit in which the conversion will be into. \n This creates a ratio so that the conversion would be as accurate as possible. You can think of this as the scale check logic in Bluebeam. \n Outputs:\n \t The first output is an image of the plan measured and color sorted. \n \t The second output is a representation of the Legend exported.\n \t The column named MC-Template Name is empty for you to enter the MC-T name. \n \t The column named Area is the area measured of one of the shape.\n \t The column named Occurences is the number of count of this shape - This is just to make sure every shape was seen correctly and thus summed later correctly.\n \t The column named Total Area contains the summed areas of each shape \n \t The column named Unit refers to the unit in which the conversion was made to.")
with gr.Row():
with gr.Column():
project=gr.Dropdown(choices=['BMW job1','BMW job2','Project C'],interactive=True,label='Projects')
drop=gr.Dropdown(choices=None,interactive=True,label='project parts')
radio_button = gr.Dropdown(choices=['foundation','external','interior'], value=None, interactive=True,label='sections')
# with gr.Row():
in2=gr.Radio(label="Measurement",choices=["Measure Full Image", "Measure Specified Regions"])
in3=gr.Radio(label="Area or Perimeter",choices=["Area", "Perimeter"])
with gr.Row():
in4=gr.ColorPicker(label="color" )
in5=gr.ColorPicker(label="color" )
in6=gr.ColorPicker(label="color" )
in7=gr.ColorPicker(label="color" )
in8=gr.ColorPicker(label="color" )
in9=gr.ColorPicker(label="color" )
in10=gr.ColorPicker(label="color" )
in11=gr.ColorPicker(label="color" )
in12=gr.ColorPicker(label="color" )
# clr_btn=gr.Button(value='Clear')
#######################################################
with gr.Column():
img1=gr.Image()
# img2=gr.Image()
# df=gr.Dataframe()
show_button = gr.Button(value="Measure",elem_id='search')
with gr.Row():
with gr.Column():
num1=gr.Number(label='Real value')
num2=gr.Number(label='Pixel value')
dp=gr.Dropdown(["m", "cm", "mm"])
btn = gr.Button("Submit Ratio")
check=gr.Checkbox(label='SaveOutput')
check1=gr.Checkbox(label='Export to Excel')
with gr.Column():
out1=gr.Image(label="Image", type="pil", image_mode="RGBA")
out2=gr.Dataframe(label='Dictionary', interactive=True) # row_count = (5, "fixed")
# out3=gr.HTML( elem_id="coords", visible=True)
buttonSaveDf=gr.Button("Save dataframe")
with gr.Tab("Google Sheets Legend"):
# out3=gr.HTML( )
out3=gr.HTML()
btn1=gr.Button('Save updated MC-T Names')
# getMeasurement(plan,SaveOP) #drop, check
show_button.click(fn=getMeasurement, inputs=[drop, check ,check1, dp,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11,in12],outputs=img1)
buttonSaveDf.click(fn=saveExcel,inputs=[out2,project])
btn1.click(fn=getdropdownValues,every=20)
# clr_btn.click(fn=clear,outputs=[project,radio_button,check,drop])
# btn1.click(fn=PickColorContours, inputs=[dp,in1,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11], outputs=out1)
#secoond part
# btn.click(fn=PickColorContours, inputs=[dp,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11,num1,num2], outputs=outputs1)
btn.click(fn=getMeasurement, inputs=[drop, check,check1 ,dp,in2,in3,in4,in5,in6,in7,in8,in9,in10,in11,in12,num1,num2],outputs=[out1,out2,out3])
project.change(fn=update_dropdown, inputs=[project], outputs=drop)
mainBlock.launch(debug=True,enable_queue=True)
|