Spaces:
Sleeping
Sleeping
Update deploying_3_3.py
Browse files- deploying_3_3.py +137 -62
deploying_3_3.py
CHANGED
|
@@ -11,36 +11,14 @@ Original file is located at
|
|
| 11 |
|
| 12 |
# pip install ezdxf
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
def
|
| 17 |
-
"""
|
| 18 |
-
return
|
| 19 |
|
| 20 |
-
def normalize_vertices(vertices):
|
| 21 |
-
"""Convert all vertices to tuples and sort them."""
|
| 22 |
-
return sorted([tuple(vertex) for vertex in vertices])
|
| 23 |
-
|
| 24 |
-
def vertices_are_close(vertices1, vertices2, threshold=0.01):
|
| 25 |
-
"""Check if all vertices in two sets are within a certain distance threshold."""
|
| 26 |
-
vertices1 = normalize_vertices(vertices1)
|
| 27 |
-
vertices2 = normalize_vertices(vertices2)
|
| 28 |
-
|
| 29 |
-
if len(vertices1) != len(vertices2):
|
| 30 |
-
return False
|
| 31 |
-
|
| 32 |
-
for v1, v2 in zip(vertices1, vertices2):
|
| 33 |
-
if distance(v1, v2) > threshold:
|
| 34 |
-
return False
|
| 35 |
-
return True
|
| 36 |
-
|
| 37 |
-
def vertices_exist(hatched_areas, vertices, threshold=0.01):
|
| 38 |
-
"""Check if a set of vertices exists in hatched_areas within a given threshold."""
|
| 39 |
-
for area in hatched_areas:
|
| 40 |
-
existing_vertices = area[0]
|
| 41 |
-
if vertices_are_close(existing_vertices, vertices, threshold):
|
| 42 |
-
return True
|
| 43 |
-
return False
|
| 44 |
|
| 45 |
from ctypes import sizeof
|
| 46 |
# -*- coding: utf-8 -*-wj
|
|
@@ -573,6 +551,7 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle):
|
|
| 573 |
trial=0
|
| 574 |
hatched_areas = []
|
| 575 |
threshold=0.01
|
|
|
|
| 576 |
for entity in msp:
|
| 577 |
if entity.dxftype() == 'HATCH':
|
| 578 |
# print(f"Processing HATCH entity: {entity}")
|
|
@@ -592,11 +571,17 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle):
|
|
| 592 |
if (poly.area > 0.9 and (height > 0.7 and width > 0.7)):
|
| 593 |
area1 = round(poly.area, 3)
|
| 594 |
perimeter = round(poly.length, 3)
|
|
|
|
| 595 |
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 600 |
hatched_areas.append([vertices, area1, perimeter, rgb_color])
|
| 601 |
|
| 602 |
elif str(path.type) == 'BoundaryPathType.EDGE':
|
|
@@ -617,10 +602,17 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle):
|
|
| 617 |
area1 = round(poly.area, 3)
|
| 618 |
perimeter = round(poly.length, 3)
|
| 619 |
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 624 |
hatched_areas.append([vert, area1, perimeter, rgb_color])
|
| 625 |
|
| 626 |
else:
|
|
@@ -636,9 +628,21 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle):
|
|
| 636 |
height = maxy - miny
|
| 637 |
|
| 638 |
if (poly.area > 0.9 and (height > 0.7 and width > 0.7)):
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 642 |
|
| 643 |
|
| 644 |
|
|
@@ -668,12 +672,17 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle):
|
|
| 668 |
area1 = round(poly.area, 3)
|
| 669 |
perimeter = round(poly.length, 3)
|
| 670 |
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 677 |
hatched_areas.append([vertices, area1, perimeter, rgb_color])
|
| 678 |
|
| 679 |
|
|
@@ -700,9 +709,18 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle):
|
|
| 700 |
if (poly.area > 0.9 and (height > 0.7 and width > 0.7)):
|
| 701 |
area1 = round(poly.area,3)
|
| 702 |
perimeter = round (poly.length,3)
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 706 |
|
| 707 |
|
| 708 |
elif entity.dxftype() == 'SPLINE':
|
|
@@ -724,9 +742,18 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle):
|
|
| 724 |
if (poly.area > 0.9 and (height > 0.7 and width > 0.7)):
|
| 725 |
area1 = round(poly.area,3)
|
| 726 |
perimeter = round (poly.length,3)
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 730 |
|
| 731 |
sorted_data = sorted(hatched_areas, key=lambda x: x[1])
|
| 732 |
return sorted_data
|
|
@@ -947,20 +974,58 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath=0,pdfname=0):
|
|
| 947 |
|
| 948 |
|
| 949 |
# Append dominant color to ColorCheck and update NewColors
|
| 950 |
-
if
|
| 951 |
-
|
| 952 |
-
|
| 953 |
-
|
| 954 |
-
|
| 955 |
-
|
| 956 |
-
|
| 957 |
-
|
| 958 |
-
|
| 959 |
-
|
| 960 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 961 |
|
| 962 |
# cv2.drawContours(imgg, [np.array(cntPoints)], -1, (NewColors), thickness=2)
|
| 963 |
cv2.drawContours(imgg, [np.array(cntPoints)], -1, ([NewColors[2],NewColors[1],NewColors[0]]), thickness=-1)
|
|
|
|
| 964 |
annot11 = page2.add_polygon_annot( points=shapee) # 'Polygon'
|
| 965 |
annot11.set_border(width=0.2)
|
| 966 |
annot11.set_colors(stroke=(int(NewColors[0])/255,int(NewColors[1])/255,int(NewColors[2])/255), fill= (int(NewColors[0])/255,int(NewColors[1])/255,int(NewColors[2])/255) )
|
|
@@ -986,6 +1051,16 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath=0,pdfname=0):
|
|
| 986 |
|
| 987 |
image_new1 = cv2.addWeighted(Correct_img, alpha, img, 1 - alpha, 0)
|
| 988 |
SimilarAreaDictionary = SimilarAreaDictionary.fillna(' ')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 989 |
gc,spreadsheet_service,spreadsheetId, spreadsheet_url , namepathArr=google_sheet_Legend.legendGoogleSheets(SimilarAreaDictionary , pdfname,pdfpath)
|
| 990 |
# dbxTeam=tsadropboxretrieval.ADR_Access_DropboxTeam('user')
|
| 991 |
# md, res =dbxTeam.files_download(path= pdfpath+pdfname)
|
|
|
|
| 11 |
|
| 12 |
# pip install ezdxf
|
| 13 |
|
| 14 |
+
def normalize_vertices(vertices):
|
| 15 |
+
"""Sort vertices to ensure consistent order."""
|
| 16 |
+
return tuple(sorted(tuple(v) for v in vertices))
|
| 17 |
|
| 18 |
+
def areas_are_similar(area1, area2, tolerance=0.2):
|
| 19 |
+
"""Check if two areas are within a given tolerance."""
|
| 20 |
+
return abs(area1 - area2) <= tolerance
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
from ctypes import sizeof
|
| 24 |
# -*- coding: utf-8 -*-wj
|
|
|
|
| 551 |
trial=0
|
| 552 |
hatched_areas = []
|
| 553 |
threshold=0.01
|
| 554 |
+
unique_shapes = []
|
| 555 |
for entity in msp:
|
| 556 |
if entity.dxftype() == 'HATCH':
|
| 557 |
# print(f"Processing HATCH entity: {entity}")
|
|
|
|
| 571 |
if (poly.area > 0.9 and (height > 0.7 and width > 0.7)):
|
| 572 |
area1 = round(poly.area, 3)
|
| 573 |
perimeter = round(poly.length, 3)
|
| 574 |
+
normalized_vertices = normalize_vertices(vertices)
|
| 575 |
|
| 576 |
+
duplicate_found = False
|
| 577 |
+
for existing_vertices, existing_area in unique_shapes:
|
| 578 |
+
if normalized_vertices == existing_vertices and areas_are_similar(area1, existing_area):
|
| 579 |
+
duplicate_found = True
|
| 580 |
+
break
|
| 581 |
+
|
| 582 |
+
if not duplicate_found:
|
| 583 |
+
rgb_color = get_hatch_color(entity) # Assuming this function exists
|
| 584 |
+
unique_shapes.append((normalized_vertices, area1))
|
| 585 |
hatched_areas.append([vertices, area1, perimeter, rgb_color])
|
| 586 |
|
| 587 |
elif str(path.type) == 'BoundaryPathType.EDGE':
|
|
|
|
| 602 |
area1 = round(poly.area, 3)
|
| 603 |
perimeter = round(poly.length, 3)
|
| 604 |
|
| 605 |
+
normalized_vertices = normalize_vertices(vert)
|
| 606 |
+
|
| 607 |
+
duplicate_found = False
|
| 608 |
+
for existing_vertices, existing_area in unique_shapes:
|
| 609 |
+
if normalized_vertices == existing_vertices and areas_are_similar(area1, existing_area):
|
| 610 |
+
duplicate_found = True
|
| 611 |
+
break
|
| 612 |
+
|
| 613 |
+
if not duplicate_found:
|
| 614 |
+
rgb_color = get_hatch_color(entity) # Assuming this function exists
|
| 615 |
+
unique_shapes.append((normalized_vertices, area1))
|
| 616 |
hatched_areas.append([vert, area1, perimeter, rgb_color])
|
| 617 |
|
| 618 |
else:
|
|
|
|
| 628 |
height = maxy - miny
|
| 629 |
|
| 630 |
if (poly.area > 0.9 and (height > 0.7 and width > 0.7)):
|
| 631 |
+
area1 = round(poly.area, 3)
|
| 632 |
+
perimeter = round(poly.length, 3)
|
| 633 |
+
normalized_vertices = normalize_vertices(vertices)
|
| 634 |
+
|
| 635 |
+
duplicate_found = False
|
| 636 |
+
for existing_vertices, existing_area in unique_shapes:
|
| 637 |
+
if normalized_vertices == existing_vertices or areas_are_similar(area1, existing_area):
|
| 638 |
+
duplicate_found = True
|
| 639 |
+
break
|
| 640 |
+
|
| 641 |
+
if not duplicate_found:
|
| 642 |
+
rgb_color = get_hatch_color(entity) # Assuming this function exists
|
| 643 |
+
unique_shapes.append((normalized_vertices, area1))
|
| 644 |
+
hatched_areas.append([vertices, area1, perimeter, rgb_color])
|
| 645 |
+
|
| 646 |
|
| 647 |
|
| 648 |
|
|
|
|
| 672 |
area1 = round(poly.area, 3)
|
| 673 |
perimeter = round(poly.length, 3)
|
| 674 |
|
| 675 |
+
normalized_vertices = normalize_vertices(vertices)
|
| 676 |
+
|
| 677 |
+
duplicate_found = False
|
| 678 |
+
for existing_vertices, existing_area in unique_shapes:
|
| 679 |
+
if normalized_vertices == existing_vertices or areas_are_similar(area1, existing_area):
|
| 680 |
+
duplicate_found = True
|
| 681 |
+
break
|
| 682 |
+
|
| 683 |
+
if not duplicate_found:
|
| 684 |
+
rgb_color = get_hatch_color(entity) # Assuming this function exists
|
| 685 |
+
unique_shapes.append((normalized_vertices, area1))
|
| 686 |
hatched_areas.append([vertices, area1, perimeter, rgb_color])
|
| 687 |
|
| 688 |
|
|
|
|
| 709 |
if (poly.area > 0.9 and (height > 0.7 and width > 0.7)):
|
| 710 |
area1 = round(poly.area,3)
|
| 711 |
perimeter = round (poly.length,3)
|
| 712 |
+
normalized_vertices = normalize_vertices(vertices)
|
| 713 |
+
|
| 714 |
+
duplicate_found = False
|
| 715 |
+
for existing_vertices, existing_area in unique_shapes:
|
| 716 |
+
if normalized_vertices == existing_vertices or areas_are_similar(area1, existing_area):
|
| 717 |
+
duplicate_found = True
|
| 718 |
+
break
|
| 719 |
+
|
| 720 |
+
if not duplicate_found:
|
| 721 |
+
rgb_color = get_hatch_color(entity) # Assuming this function exists
|
| 722 |
+
unique_shapes.append((normalized_vertices, area1))
|
| 723 |
+
hatched_areas.append([vertices, area1, perimeter, rgb_color])
|
| 724 |
|
| 725 |
|
| 726 |
elif entity.dxftype() == 'SPLINE':
|
|
|
|
| 742 |
if (poly.area > 0.9 and (height > 0.7 and width > 0.7)):
|
| 743 |
area1 = round(poly.area,3)
|
| 744 |
perimeter = round (poly.length,3)
|
| 745 |
+
normalized_vertices = normalize_vertices(vertices)
|
| 746 |
+
|
| 747 |
+
duplicate_found = False
|
| 748 |
+
for existing_vertices, existing_area in unique_shapes:
|
| 749 |
+
if normalized_vertices == existing_vertices or areas_are_similar(area1, existing_area):
|
| 750 |
+
duplicate_found = True
|
| 751 |
+
break
|
| 752 |
+
|
| 753 |
+
if not duplicate_found:
|
| 754 |
+
rgb_color = get_hatch_color(entity) # Assuming this function exists
|
| 755 |
+
unique_shapes.append((normalized_vertices, area1))
|
| 756 |
+
hatched_areas.append([vertices, area1, perimeter, rgb_color])
|
| 757 |
|
| 758 |
sorted_data = sorted(hatched_areas, key=lambda x: x[1])
|
| 759 |
return sorted_data
|
|
|
|
| 974 |
|
| 975 |
|
| 976 |
# Append dominant color to ColorCheck and update NewColors
|
| 977 |
+
if dominant_color is not None:
|
| 978 |
+
ColorCheck.append(dominant_color)
|
| 979 |
+
|
| 980 |
+
NewColors = None
|
| 981 |
+
|
| 982 |
+
for color in ColorCheck:
|
| 983 |
+
# Check if the current color is within the tolerance
|
| 984 |
+
print("color = ",color)
|
| 985 |
+
print("dominant_color = ",dominant_color)
|
| 986 |
+
if (abs(color[0] - dominant_color[0]) < 20 and
|
| 987 |
+
abs(color[1] - dominant_color[1]) < 20 and
|
| 988 |
+
abs(color[2] - dominant_color[2]) < 20):
|
| 989 |
+
NewColors = (color[2], color[1], color[0]) # Set the new color
|
| 990 |
+
break
|
| 991 |
+
else:
|
| 992 |
+
# If no color in ColorCheck meets the tolerance, use the dominant color
|
| 993 |
+
NewColors = (dominant_color[2], dominant_color[1], dominant_color[0])
|
| 994 |
+
|
| 995 |
+
if NewColors not in ColorCheck:
|
| 996 |
+
ColorCheck.append(NewColors)
|
| 997 |
+
|
| 998 |
+
|
| 999 |
+
ConditionColor = (SimilarAreaDictionary['Color'] == NewColors)
|
| 1000 |
+
|
| 1001 |
+
if ConditionColor.any():
|
| 1002 |
+
existing_index = SimilarAreaDictionary.loc[ConditionColor].index[0]
|
| 1003 |
+
|
| 1004 |
+
if flagcolor == 1:
|
| 1005 |
+
SimilarAreaDictionary.at[existing_index, 'Total Area'] = SimilarAreaDictionary.at[existing_index, 'Total Area'] + SimilarAreaDictionary.at[index, 'Area']
|
| 1006 |
+
SimilarAreaDictionary.at[existing_index, 'Total Perimeter'] = SimilarAreaDictionary.at[existing_index, 'Total Perimeter'] + SimilarAreaDictionary.at[index, 'Perimeter']
|
| 1007 |
+
SimilarAreaDictionary.at[existing_index, 'Occurences'] = SimilarAreaDictionary.at[existing_index, 'Occurences'] + 1
|
| 1008 |
+
|
| 1009 |
+
|
| 1010 |
+
elif flagcolor == 2:
|
| 1011 |
+
SimilarAreaDictionary.at[existing_index, 'Total Area'] = SimilarAreaDictionary.at[existing_index, 'Total Area'] + SimilarAreaDictionary.at[i, 'Area']
|
| 1012 |
+
SimilarAreaDictionary.at[existing_index, 'Total Perimeter'] = SimilarAreaDictionary.at[existing_index, 'Total Perimeter'] + SimilarAreaDictionary.at[i, 'Perimeter']
|
| 1013 |
+
SimilarAreaDictionary.at[existing_index, 'Occurences'] = SimilarAreaDictionary.at[existing_index, 'Occurences'] + 1
|
| 1014 |
+
|
| 1015 |
+
|
| 1016 |
+
|
| 1017 |
+
else:
|
| 1018 |
+
if flagcolor == 1:
|
| 1019 |
+
SimilarAreaDictionary.at[index, 'Color'] = NewColors
|
| 1020 |
+
# print(f"Updated Color at index {index} with {NewColors}.")
|
| 1021 |
+
elif flagcolor == 2:
|
| 1022 |
+
SimilarAreaDictionary.at[i, 'Color'] = NewColors
|
| 1023 |
+
# print(f"Updated Color at index {i} with {NewColors}.")
|
| 1024 |
+
|
| 1025 |
|
| 1026 |
# cv2.drawContours(imgg, [np.array(cntPoints)], -1, (NewColors), thickness=2)
|
| 1027 |
cv2.drawContours(imgg, [np.array(cntPoints)], -1, ([NewColors[2],NewColors[1],NewColors[0]]), thickness=-1)
|
| 1028 |
+
|
| 1029 |
annot11 = page2.add_polygon_annot( points=shapee) # 'Polygon'
|
| 1030 |
annot11.set_border(width=0.2)
|
| 1031 |
annot11.set_colors(stroke=(int(NewColors[0])/255,int(NewColors[1])/255,int(NewColors[2])/255), fill= (int(NewColors[0])/255,int(NewColors[1])/255,int(NewColors[2])/255) )
|
|
|
|
| 1051 |
|
| 1052 |
image_new1 = cv2.addWeighted(Correct_img, alpha, img, 1 - alpha, 0)
|
| 1053 |
SimilarAreaDictionary = SimilarAreaDictionary.fillna(' ')
|
| 1054 |
+
|
| 1055 |
+
# Define white color to filter out
|
| 1056 |
+
white_color = (255, 255, 255)
|
| 1057 |
+
|
| 1058 |
+
# Delete rows where 'Guess' equals white_color
|
| 1059 |
+
SimilarAreaDictionary = SimilarAreaDictionary[SimilarAreaDictionary['Color'] != white_color]
|
| 1060 |
+
|
| 1061 |
+
# Reset the index to update row numbering
|
| 1062 |
+
SimilarAreaDictionary.reset_index(drop=True, inplace=True)
|
| 1063 |
+
|
| 1064 |
gc,spreadsheet_service,spreadsheetId, spreadsheet_url , namepathArr=google_sheet_Legend.legendGoogleSheets(SimilarAreaDictionary , pdfname,pdfpath)
|
| 1065 |
# dbxTeam=tsadropboxretrieval.ADR_Access_DropboxTeam('user')
|
| 1066 |
# md, res =dbxTeam.files_download(path= pdfpath+pdfname)
|