Spaces:
Running
Running
Update app.py
Browse filesmade changes to user our segmentation model instead of roboflow one
app.py
CHANGED
@@ -95,63 +95,29 @@ def main():
|
|
95 |
f.write(image_file.getbuffer())
|
96 |
|
97 |
# Load the YOLO models
|
98 |
-
model = YOLO("
|
99 |
|
100 |
|
101 |
-
|
102 |
|
103 |
-
#
|
104 |
-
|
105 |
-
|
106 |
-
# xyxy = r[0].boxes.xyxy
|
107 |
-
# x_min, y_min, x_max, y_max = map(int, xyxy[0])
|
108 |
-
# new_img = img[y_min:y_max, x_min:x_max]
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
image = cv2.imread(temp_file_path)
|
114 |
-
|
115 |
-
result = CLIENT.infer(image, model_id="chessboard-segmentation/1")
|
116 |
-
|
117 |
-
if image is None:
|
118 |
-
st.write("Error: Image not loaded.")
|
119 |
-
|
120 |
-
|
121 |
-
prediction_data = result
|
122 |
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
if 'x' in prediction and 'y' in prediction and 'width' in prediction and 'height' in prediction:
|
130 |
-
x, y, w, h = prediction['x'], prediction['y'], prediction['width'], prediction['height']
|
131 |
-
# print(f"Bounding box coordinates: ({x}, {y}), width={w}, height={h}")
|
132 |
-
|
133 |
-
x1, y1 = int(x - w / 2), int(y - h / 2)
|
134 |
-
x2, y2 = int(x + w / 2), int(y + h / 2)
|
135 |
-
|
136 |
-
src_pts = np.array([[x1, y1], [x2, y1], [x2, y2], [x1, y2]], dtype="float32")
|
137 |
-
# print(f"Source Points: {src_pts}")
|
138 |
-
|
139 |
-
chessboard_size = 600
|
140 |
-
dst_pts = np.array([[0, 0], [chessboard_size - 1, 0], [chessboard_size - 1, chessboard_size - 1], [0, chessboard_size - 1]], dtype="float32")
|
141 |
-
# print(f"Destination Points: {dst_pts}")
|
142 |
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
transformed_chessboard = cv2.warpPerspective(image, matrix, (chessboard_size, chessboard_size))
|
147 |
-
|
148 |
-
|
149 |
-
# Convert images to RGB for display
|
150 |
-
new_img = cv2.cvtColor(transformed_chessboard, cv2.COLOR_BGR2RGB)
|
151 |
-
|
152 |
|
153 |
|
154 |
-
|
|
|
155 |
|
156 |
image = cv2.resize(new_img, (224, 224))
|
157 |
st.image(image, caption="Segmented Chessboard", use_container_width=True)
|
|
|
95 |
f.write(image_file.getbuffer())
|
96 |
|
97 |
# Load the YOLO models
|
98 |
+
model = YOLO("ChessDetection3d") # Replace with your trained model weights file
|
99 |
|
100 |
|
101 |
+
seg_model = YOLO("segmentation.pt")
|
102 |
|
103 |
+
# Load and process the image
|
104 |
+
img = cv2.imread(temp_file_path)
|
105 |
+
r = seg_model.predict(source=temp_file_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
+
if len(r) == 0 || len(r) > 1:
|
108 |
+
if len(r) == 0:
|
109 |
+
st.write("NO BOARD IN THE IMAGE")
|
110 |
+
elif len(r) > 1:
|
111 |
+
st.write("Multiple boards are there in the image, please take only at a time")
|
112 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
+
xyxy = r[0].boxes.xyxy
|
115 |
+
x_min, y_min, x_max, y_max = map(int, xyxy[0])
|
116 |
+
new_img = img[y_min:y_max, x_min:x_max]
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
|
119 |
+
|
120 |
+
|
121 |
|
122 |
image = cv2.resize(new_img, (224, 224))
|
123 |
st.image(image, caption="Segmented Chessboard", use_container_width=True)
|