Spaces:
Sleeping
Sleeping
Update deploying_3_3.py
Browse files- deploying_3_3.py +62 -61
deploying_3_3.py
CHANGED
|
@@ -1013,68 +1013,69 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath=0,pdfname=0):
|
|
| 1013 |
|
| 1014 |
|
| 1015 |
imgslice = WhiteImgFinal[y:y+h, x:x+w]
|
| 1016 |
-
|
| 1017 |
-
|
| 1018 |
-
|
| 1019 |
-
# Convert flippedSlice to PIL for color extraction
|
| 1020 |
-
flippedSlice_pil = Image.fromarray(flippedSlice)
|
| 1021 |
-
|
| 1022 |
-
# Define patch size for color sampling (e.g., 10x10 pixels)
|
| 1023 |
-
patch_size = 60
|
| 1024 |
-
patch_colors = []
|
| 1025 |
-
|
| 1026 |
-
# Loop through patches in the image
|
| 1027 |
-
for i in range(0, flippedSlice_pil.width, patch_size):
|
| 1028 |
-
for j in range(0, flippedSlice_pil.height, patch_size):
|
| 1029 |
-
# Crop a patch from the original image
|
| 1030 |
-
patch = flippedSlice_pil.crop((i, j, i + patch_size, j + patch_size))
|
| 1031 |
-
patch_colors += patch.getcolors(patch_size * patch_size)
|
| 1032 |
-
|
| 1033 |
-
# Calculate the dominant color from all patches
|
| 1034 |
-
max_count = 0
|
| 1035 |
-
dominant_color = None
|
| 1036 |
-
tolerance = 5
|
| 1037 |
-
black_threshold = 30 # Max RGB value for a color to be considered "black"
|
| 1038 |
-
white_threshold = 225 # Min RGB value for a color to be considered "white"
|
| 1039 |
-
|
| 1040 |
-
for count, color in patch_colors:
|
| 1041 |
-
# Exclude colors within the black and white ranges
|
| 1042 |
-
if not (all(c <= black_threshold for c in color) or all(c >= white_threshold for c in color)):
|
| 1043 |
-
# Update if the current color has a higher count than previous max
|
| 1044 |
-
if count > max_count:
|
| 1045 |
-
max_count = count
|
| 1046 |
-
dominant_color = color
|
| 1047 |
-
|
| 1048 |
-
|
| 1049 |
-
|
| 1050 |
-
# Append dominant color to ColorCheck and update NewColors
|
| 1051 |
-
if dominant_color is not None:
|
| 1052 |
-
ColorCheck.append(dominant_color)
|
| 1053 |
-
|
| 1054 |
-
NewColors = None
|
| 1055 |
-
|
| 1056 |
-
for color in ColorCheck:
|
| 1057 |
-
# Check if the current color is within the tolerance
|
| 1058 |
-
print("color = ",color)
|
| 1059 |
-
print("dominant_color = ",dominant_color)
|
| 1060 |
-
if (abs(color[0] - dominant_color[0]) < 20 and
|
| 1061 |
-
abs(color[1] - dominant_color[1]) < 20 and
|
| 1062 |
-
abs(color[2] - dominant_color[2]) < 20):
|
| 1063 |
-
NewColors = (color[2], color[1], color[0]) # Set the new color
|
| 1064 |
-
break
|
| 1065 |
-
else:
|
| 1066 |
-
# If no color in ColorCheck meets the tolerance, use the dominant color
|
| 1067 |
-
NewColors = (dominant_color[2], dominant_color[1], dominant_color[0])
|
| 1068 |
-
|
| 1069 |
-
if NewColors not in ColorCheck:
|
| 1070 |
-
ColorCheck.append(NewColors)
|
| 1071 |
-
|
| 1072 |
|
| 1073 |
-
|
| 1074 |
-
|
| 1075 |
-
|
| 1076 |
-
|
| 1077 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1078 |
|
| 1079 |
|
| 1080 |
cv2.drawContours(imgg, [np.array(cntPoints)], -1, ([NewColors[2],NewColors[1],NewColors[0]]), thickness=-1)
|
|
|
|
| 1013 |
|
| 1014 |
|
| 1015 |
imgslice = WhiteImgFinal[y:y+h, x:x+w]
|
| 1016 |
+
if(imgslice.shape[0] != 0 and imgslice.shape[1] != 0):
|
| 1017 |
+
flippedSlice=flip(imgslice)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1018 |
|
| 1019 |
+
|
| 1020 |
+
# Convert flippedSlice to PIL for color extraction
|
| 1021 |
+
flippedSlice_pil = Image.fromarray(flippedSlice)
|
| 1022 |
+
|
| 1023 |
+
# Define patch size for color sampling (e.g., 10x10 pixels)
|
| 1024 |
+
patch_size = 100
|
| 1025 |
+
patch_colors = []
|
| 1026 |
+
|
| 1027 |
+
# Loop through patches in the image
|
| 1028 |
+
for i in range(0, flippedSlice_pil.width, patch_size):
|
| 1029 |
+
for j in range(0, flippedSlice_pil.height, patch_size):
|
| 1030 |
+
# Crop a patch from the original image
|
| 1031 |
+
patch = flippedSlice_pil.crop((i, j, i + patch_size, j + patch_size))
|
| 1032 |
+
patch_colors += patch.getcolors(patch_size * patch_size)
|
| 1033 |
+
|
| 1034 |
+
# Calculate the dominant color from all patches
|
| 1035 |
+
max_count = 0
|
| 1036 |
+
dominant_color = None
|
| 1037 |
+
tolerance = 5
|
| 1038 |
+
black_threshold = 30 # Max RGB value for a color to be considered "black"
|
| 1039 |
+
white_threshold = 225 # Min RGB value for a color to be considered "white"
|
| 1040 |
+
|
| 1041 |
+
for count, color in patch_colors:
|
| 1042 |
+
# Exclude colors within the black and white ranges
|
| 1043 |
+
if not (all(c <= black_threshold for c in color) or all(c >= white_threshold for c in color)):
|
| 1044 |
+
# Update if the current color has a higher count than previous max
|
| 1045 |
+
if count > max_count:
|
| 1046 |
+
max_count = count
|
| 1047 |
+
dominant_color = color
|
| 1048 |
+
|
| 1049 |
+
|
| 1050 |
+
|
| 1051 |
+
# Append dominant color to ColorCheck and update NewColors
|
| 1052 |
+
if dominant_color is not None:
|
| 1053 |
+
ColorCheck.append(dominant_color)
|
| 1054 |
+
|
| 1055 |
+
NewColors = None
|
| 1056 |
+
|
| 1057 |
+
for color in ColorCheck:
|
| 1058 |
+
# Check if the current color is within the tolerance
|
| 1059 |
+
print("color = ",color)
|
| 1060 |
+
print("dominant_color = ",dominant_color)
|
| 1061 |
+
if (abs(color[0] - dominant_color[0]) < 20 and
|
| 1062 |
+
abs(color[1] - dominant_color[1]) < 20 and
|
| 1063 |
+
abs(color[2] - dominant_color[2]) < 20):
|
| 1064 |
+
NewColors = (color[2], color[1], color[0]) # Set the new color
|
| 1065 |
+
break
|
| 1066 |
+
else:
|
| 1067 |
+
# If no color in ColorCheck meets the tolerance, use the dominant color
|
| 1068 |
+
NewColors = (dominant_color[2], dominant_color[1], dominant_color[0])
|
| 1069 |
+
|
| 1070 |
+
if NewColors not in ColorCheck:
|
| 1071 |
+
ColorCheck.append(NewColors)
|
| 1072 |
+
|
| 1073 |
+
|
| 1074 |
+
if flagcolor == 1:
|
| 1075 |
+
SimilarAreaDictionary.at[index, 'Color'] = NewColors
|
| 1076 |
+
# print(f"Updated Color at index {index} with {NewColors}.")
|
| 1077 |
+
elif flagcolor == 2:
|
| 1078 |
+
SimilarAreaDictionary.at[i, 'Color'] = NewColors
|
| 1079 |
|
| 1080 |
|
| 1081 |
cv2.drawContours(imgg, [np.array(cntPoints)], -1, ([NewColors[2],NewColors[1],NewColors[0]]), thickness=-1)
|