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
 
 
 
 
4323222
5a24f0e
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
import os
from pathlib import Path
from typing import List, Union
from PIL import Image
import ezdxf.units
import numpy as np
import torch
from torchvision import transforms
from ultralytics import YOLOWorld, YOLO
from ultralytics.engine.results import Results
from ultralytics.utils.plotting import save_one_box
from transformers import AutoModelForImageSegmentation
import cv2
import ezdxf
import gradio as gr
import gc
from scalingtestupdated import calculate_scaling_factor
from scipy.interpolate import splprep, splev
from scipy.ndimage import gaussian_filter1d
import json

# Language translations
TRANSLATIONS = {
    "english": {
        "input_image": "Input Image",
        "offset_value": "Offset value for Mask(mm)",
        "coin_diameter": "Diameter of reference coin(mm). Adjust according to coin.",
        "output_image": "Output Image",
        "outlines": "Outlines of Objects",
        "dxf_file": "DXF file",
        "mask": "Mask",
        "scaling_factor": "Scaling Factor(mm)",
        "scaling_placeholder": "Every pixel is equal to mentioned number in millimeters",
        "language_selector": "Select Language",
    },
    "dutch": {
        "input_image": "Invoer Afbeelding",
        "offset_value": "Offset waarde voor Masker(mm)",
        "coin_diameter": "Diameter van referentiemunt(mm). Pas aan volgens munt.",
        "output_image": "Uitvoer Afbeelding",
        "outlines": "Contouren van Objecten",
        "dxf_file": "DXF bestand",
        "mask": "Masker",
        "scaling_factor": "Schalingsfactor(mm)",
        "scaling_placeholder": "Elke pixel is gelijk aan genoemd aantal in millimeters",
        "language_selector": "Selecteer Taal",
    }
}


birefnet = AutoModelForImageSegmentation.from_pretrained(
    "zhengpeng7/BiRefNet", trust_remote_code=True
)

device = "cpu"
torch.set_float32_matmul_precision(["high", "highest"][0])

birefnet.to(device)
birefnet.eval()
transform_image = transforms.Compose(
    [
        transforms.Resize((1024, 1024)),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
    ]
)

def remove_bg(image: np.ndarray) -> np.ndarray:

    image = Image.fromarray(image)
    input_images = transform_image(image).unsqueeze(0).to("cpu")

    # Prediction
    with torch.no_grad():
        preds = birefnet(input_images)[-1].sigmoid().cpu()
    pred = preds[0].squeeze()

    # Show Results
    pred_pil: Image = transforms.ToPILImage()(pred)
    print(pred_pil)
    # Scale proportionally with max length to 1024 for faster showing
    scale_ratio = 1024 / max(image.size)
    scaled_size = (int(image.size[0] * scale_ratio), int(image.size[1] * scale_ratio))
    print(f"scaled size {scaled_size}")

    return np.array(pred_pil.resize(scaled_size))

def make_square(img: np.ndarray):
    # Get dimensions
    height, width = img.shape[:2]

    # Find the larger dimension
    max_dim = max(height, width)

    # Calculate padding
    pad_height = (max_dim - height) // 2
    pad_width = (max_dim - width) // 2

    # Handle odd dimensions
    pad_height_extra = max_dim - height - 2 * pad_height
    pad_width_extra = max_dim - width - 2 * pad_width

    # Create padding with edge colors
    if len(img.shape) == 3:  # Color image
        # Pad the image
        padded = np.pad(
            img,
            (
                (pad_height, pad_height + pad_height_extra),
                (pad_width, pad_width + pad_width_extra),
                (0, 0),
            ),
            mode="edge",
        )
    else:  # Grayscale image
        padded = np.pad(
            img,
            (
                (pad_height, pad_height + pad_height_extra),
                (pad_width, pad_width + pad_width_extra),
            ),
            mode="edge",
        )

    return padded

def exclude_scaling_box(
    image: np.ndarray,
    bbox: np.ndarray,
    orig_size: tuple,
    processed_size: tuple,
    expansion_factor: float = 1.2,
) -> np.ndarray:
    # Unpack the bounding box
    x_min, y_min, x_max, y_max = map(int, bbox)

    # Calculate scaling factors
    scale_x = processed_size[1] / orig_size[1]  # Width scale
    scale_y = processed_size[0] / orig_size[0]  # Height scale

    # Adjust bounding box coordinates
    x_min = int(x_min * scale_x)
    x_max = int(x_max * scale_x)
    y_min = int(y_min * scale_y)
    y_max = int(y_max * scale_y)

    # Calculate expanded box coordinates
    box_width = x_max - x_min
    box_height = y_max - y_min
    expanded_x_min = max(0, int(x_min - (expansion_factor - 1) * box_width / 2))
    expanded_x_max = min(
        image.shape[1], int(x_max + (expansion_factor - 1) * box_width / 2)
    )
    expanded_y_min = max(0, int(y_min - (expansion_factor - 1) * box_height / 2))
    expanded_y_max = min(
        image.shape[0], int(y_max + (expansion_factor - 1) * box_height / 2)
    )

    # Black out the expanded region
    image[expanded_y_min:expanded_y_max, expanded_x_min:expanded_x_max] = 0

    return image

def resample_contour(contour):
    # Get all the parameters at the start:
    num_points = 1000
    smoothing_factor = 5
    spline_degree = 3  # Typically k=3 for cubic spline

    smoothed_x_sigma = 1
    smoothed_y_sigma = 1

    # Ensure contour has enough points
    if len(contour) < spline_degree + 1:
        raise ValueError(f"Contour must have at least {spline_degree + 1} points, but has {len(contour)} points.")

    contour = contour[:, 0, :]

    tck, _ = splprep([contour[:, 0], contour[:, 1]], s=smoothing_factor)
    u = np.linspace(0, 1, num_points)
    resampled_points = splev(u, tck)

    smoothed_x = gaussian_filter1d(resampled_points[0], sigma=smoothed_x_sigma)
    smoothed_y = gaussian_filter1d(resampled_points[1], sigma=smoothed_y_sigma)

    return np.array([smoothed_x, smoothed_y]).T



def save_dxf_spline(inflated_contours, scaling_factor, height):
    degree = 3
    closed = True

    # Create a new DXF document with millimeters as the unit
    doc = ezdxf.new(units=ezdxf.units.MM)
    doc.units = ezdxf.units.MM  # Ensure units are millimeters
    doc.header["$INSUNITS"] = ezdxf.units.MM  # Set insertion units to millimeters

    msp = doc.modelspace()

    for contour in inflated_contours:
        try:
            resampled_contour = resample_contour(contour)
            points = [
                (x * scaling_factor, (height - y) * scaling_factor)
                for x, y in resampled_contour
            ]
            if len(points) >= 3:
                if np.linalg.norm(np.array(points[0]) - np.array(points[-1])) > 1e-2:
                    points.append(points[0])

                spline = msp.add_spline(points, degree=degree)
                spline.closed = closed

        except ValueError as e:
            print(f"Skipping contour: {e}")

    dxf_filepath = os.path.join("./outputs", "out.dxf")
    doc.saveas(dxf_filepath)

    return dxf_filepath


def extract_outlines(binary_image: np.ndarray) -> np.ndarray:
    """
    Extracts and draws the outlines of masks from a binary image.
    Args:
        binary_image: Grayscale binary image where white represents masks and black is the background.
    Returns:
        Image with outlines drawn.
    """
    # Detect contours from the binary image
    contours, _ = cv2.findContours(
        binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE
    )

    outline_image = np.zeros_like(binary_image)

    # Draw the contours on the blank image
    cv2.drawContours(
        outline_image, contours, -1, (255), thickness=1
    )  # White color for outlines

    return cv2.bitwise_not(outline_image), contours

def to_dxf(contours):
    # Create a new DXF document with millimeters as the unit
    doc = ezdxf.new(units=ezdxf.units.MM)
    doc.units = ezdxf.units.MM  # Ensure units are millimeters
    doc.header["$INSUNITS"] = ezdxf.units.MM  # Set insertion units to millimeters)
    msp = doc.modelspace()

    try:
        for contour in contours:
            points = [(point[0][0], point[0][1]) for point in contour]
            msp.add_lwpolyline(points, close=True)  # Add a polyline for each contour
    except Exception as e:
        raise gr.Error(f"Unable to generate DXF: {e}")

    output_path = "./outputs/out.dxf"
    doc.saveas(output_path)
    return output_path

def smooth_contours(contour):
    epsilon = 0.01 * cv2.arcLength(contour, True)  # Adjust factor (e.g., 0.01)
    return cv2.approxPolyDP(contour, epsilon, True)


def scale_image(image: np.ndarray, scale_factor: float) -> np.ndarray:
    """
    Resize image by scaling both width and height by the same factor.
    Args:
        image: Input numpy image
        scale_factor: Factor to scale the image (e.g., 0.5 for half size, 2 for double size)
    Returns:
        np.ndarray: Resized image
    """
    if scale_factor <= 0:
        raise ValueError("Scale factor must be positive")

    current_height, current_width = image.shape[:2]

    # Calculate new dimensions
    new_width = int(current_width * scale_factor)
    new_height = int(current_height * scale_factor)

    # Choose interpolation method based on whether we're scaling up or down
    interpolation = cv2.INTER_AREA if scale_factor < 1 else cv2.INTER_CUBIC

    # Resize image
    resized_image = cv2.resize(
        image, (new_width, new_height), interpolation=interpolation
    )

    return resized_image

def detect_reference_square(img) -> np.ndarray:
    box_detector = YOLO("./best1.pt")
    res = box_detector.predict(img, conf=0.05)
    del box_detector
    return save_one_box(res[0].cpu().boxes.xyxy, res[0].orig_img, save=False), res[
        0
    ].cpu().boxes.xyxy[0]


def resize_img(img: np.ndarray, resize_dim):
    return np.array(Image.fromarray(img).resize(resize_dim))


def predict(image, offset, coin_size_mm):

    if offset < 0:
        raise gr.Error("Offset Value Can't be negative")

    try:
        reference_obj_img, scaling_box_coords = detect_reference_square(image)
    except:
        raise gr.Error("Unable to detect the COIN. Please try again with different magnification.")

    reference_obj_img = make_square(reference_obj_img)

    reference_square_mask = remove_bg(reference_obj_img)

    reference_square_mask = resize_img(reference_square_mask, (reference_obj_img.shape[1], reference_obj_img.shape[0]))

    try:
        scaling_factor= calculate_scaling_factor(
            target_image=reference_square_mask,
            reference_obj_size_mm = coin_size_mm,
            feature_detector="ORB",
        )
    except Exception as e:
        scaling_factor = None
        print(f"Error calculating scaling factor: {e}")

    # Default to a scaling factor if calculation fails
    if scaling_factor is None or scaling_factor == 0:
        scaling_factor = 0.07
        print("Using default scaling factor due to calculation error")

    orig_size = image.shape[:2]
    objects_mask = remove_bg(image)
    processed_size = objects_mask.shape[:2]

    objects_mask = exclude_scaling_box(
        objects_mask,
        scaling_box_coords,
        orig_size,
        processed_size,
        expansion_factor=1.2,
    )
    objects_mask = resize_img(objects_mask, (image.shape[1], image.shape[0]))
    
    # Ensure offset_inches is valid
    if scaling_factor != 0:
        offset_pixels = (float(offset) / float(scaling_factor)) * 2 + 1
    else:
        offset_pixels = 1  # Default value in case of invalid scaling factor

    dilated_mask = cv2.dilate(objects_mask, np.ones((int(offset_pixels), int(offset_pixels)), np.uint8))

    Image.fromarray(dilated_mask).save("./outputs/scaled_mask_new.jpg")
    outlines, contours = extract_outlines(dilated_mask)
    shrunked_img_contours = cv2.drawContours(image, contours, -1, (0, 0, 255), thickness=2)
    dxf = save_dxf_spline(contours, scaling_factor, processed_size[0])
    # dxf = to_dxf(contours)

    return (
        shrunked_img_contours,
        outlines,
        dxf,
        dilated_mask,
        scaling_factor,
    )

def update_interface(language):
    """Updates the interface labels based on selected language"""
    return [
        gr.Image(label=TRANSLATIONS[language]["input_image"], type="numpy"),
        gr.Number(label=TRANSLATIONS[language]["offset_value"], value=0.15),
        gr.Number(label=TRANSLATIONS[language]["coin_diameter"], value=20),
        gr.Image(label=TRANSLATIONS[language]["output_image"]),
        gr.Image(label=TRANSLATIONS[language]["outlines"]),
        gr.File(label=TRANSLATIONS[language]["dxf_file"]),
        gr.Image(label=TRANSLATIONS[language]["mask"]),
        gr.Textbox(
            label=TRANSLATIONS[language]["scaling_factor"],
            placeholder=TRANSLATIONS[language]["scaling_placeholder"],
        ),
    ]

if __name__ == "__main__":
    os.makedirs("./outputs", exist_ok=True)

    with gr.Blocks() as demo:
        # Language selector
        language = gr.Dropdown(
            choices=["english", "dutch"],
            value="english",
            label="Select Language",
            interactive=True
        )
        
        # Initialize interface components
        input_image = gr.Image(label=TRANSLATIONS["english"]["input_image"], type="numpy")
        offset = gr.Number(label=TRANSLATIONS["english"]["offset_value"], value=0.15)
        coin_size = gr.Number(label=TRANSLATIONS["english"]["coin_diameter"], value=20)
        
        output_image = gr.Image(label=TRANSLATIONS["english"]["output_image"])
        outlines = gr.Image(label=TRANSLATIONS["english"]["outlines"])
        dxf_file = gr.File(label=TRANSLATIONS["english"]["dxf_file"])
        mask = gr.Image(label=TRANSLATIONS["english"]["mask"])
        scaling = gr.Textbox(
            label=TRANSLATIONS["english"]["scaling_factor"],
            placeholder=TRANSLATIONS["english"]["scaling_placeholder"]
        )

        # Create submit button
        submit_btn = gr.Button("Submit")
        
        # Handle language change
        language.change(
            fn=lambda x: [
                gr.update(label=TRANSLATIONS[x]["input_image"]),
                gr.update(label=TRANSLATIONS[x]["offset_value"]),
                gr.update(label=TRANSLATIONS[x]["coin_diameter"]),
                gr.update(label=TRANSLATIONS[x]["output_image"]),
                gr.update(label=TRANSLATIONS[x]["outlines"]),
                gr.update(label=TRANSLATIONS[x]["dxf_file"]),
                gr.update(label=TRANSLATIONS[x]["mask"]),
                gr.update(
                    label=TRANSLATIONS[x]["scaling_factor"],
                    placeholder=TRANSLATIONS[x]["scaling_placeholder"]
                ),
            ],
            inputs=[language],
            outputs=[
                input_image, offset, coin_size,
                output_image, outlines, dxf_file,
                mask, scaling
            ]
        )

        # Handle prediction
        submit_btn.click(
            fn=predict,
            inputs=[input_image, offset, coin_size],
            outputs=[output_image, outlines, dxf_file, mask, scaling]
        )

        # Add examples
        gr.Examples(
            examples=[
                ["./examples/Test20.jpg", 0.15],
                ["./examples/Test21.jpg", 0.15],
                ["./examples/Test22.jpg", 0.15],
                ["./examples/Test23.jpg", 0.15],
            ],
            inputs=[input_image, offset]
        )

    demo.launch(share=True)