Demo the welding defect detection

The model is hosted in AlfredWJLu/WeldingDefect-V1 @ ModelScope, and trained on a toy dataset.

In [1]:
import warnings

warnings.filterwarnings("ignore", category=UserWarning)

import requests
import base64, json
import numpy as np
from matplotlib import pyplot as plt
import cv2
from det.exp import *
from det.data.data_augment import *
from det.utils import *

from utils import *
from dotenv import load_dotenv, find_dotenv
from run import appMissTags, Predictor, Exp, prepare_det, calculate_overlap
import os, traceback, glob

detector = prepare_det()

load_dotenv(find_dotenv())
[2026-03-31 21:15:41 - INFO]: Model Summary: Params: 13.56M, Gflops: 39.47
[2026-03-31 21:15:41 - INFO]: loading checkpoint from best_ckpt.pth
[2026-03-31 21:15:41 - INFO]: loaded checkpoint done.
[2026-03-31 21:15:41 - INFO]: Model Summary: Params: 13.56M, Gflops: 39.47
[2026-03-31 21:15:41 - INFO]: loading checkpoint from best_ckpt.pth
[2026-03-31 21:15:41 - INFO]: loaded checkpoint done.
Out[1]:
True
In [ ]:
img_loc = 'YOU IMG LOC'
img_id = os.path.basename(img_loc).split('.')[0]
img_np = cv2.imread(img_loc,flags=cv2.IMREAD_GRAYSCALE)
img_np.shape
Out[ ]:
(640, 640)
In [3]:
chunk_size = 640
eval_img_loc = str(os.getenv("EVAL_IMG_LOC"))
eval_img_loc = os.path.join(eval_img_loc, img_id)
os.makedirs(os.path.join(eval_img_loc, img_id),exist_ok=True)

'''make chunks'''
img_np_x1, img_np_y1, img_np_x2, img_np_y2 = 0, 0, img_np.shape[1], img_np.shape[0]
img_width, img_height = img_np.shape[1], img_np.shape[0]

print('Received a dicom image shape = {:}(W) x {:}(H)'.format(img_width, img_height))

width_overlap, height_overlap = calculate_overlap(img_width, img_height, chunk_size)
print("W overlap = {:}, H overlap = {:}".format(width_overlap, height_overlap))

num_chunks_width = (img_width + chunk_size - 1) // chunk_size
num_chunks_height = (img_height + chunk_size - 1) // chunk_size

for i in range(num_chunks_height):
    for j in range(num_chunks_width):
        
        # 计算当前 chunk 的左上角和右下角坐标
        x_start = j * (chunk_size - width_overlap)
        y_start = i * (chunk_size - height_overlap)
        x_end = x_start + chunk_size
        y_end = y_start + chunk_size

        # 确保不超过图片边界
        x_start = max(0, x_start)
        y_start = max(0, y_start)
        x_end = min(img_width, x_end)
        y_end = min(img_height, y_end)

        img_slide = img_np[y_start:y_end, x_start:x_end]
        print(np.amin(img_slide), np.amax(img_slide))
        print("Chunk slide H {:04}-{:04}, W {:04}-{:04}".format(y_start, y_end, x_start, x_end))
        img_slide_name = 'Chunk.H{:04}-H{:04}-W{:04}-W{:04}.jpg'.format(\
            y_start, y_end, x_start, x_end)
        img_slide_name = os.path.join(eval_img_loc, img_slide_name)
        cv2.imwrite(img_slide_name, np.array(img_slide).astype(np.uint8))

'''make detection on these chunks'''

files = glob.glob(os.path.join(eval_img_loc, "Chunk*.jpg"))
result_image = np.zeros((img_np.shape[0],img_np.shape[1],3))
is_ok = True
for image_name in files:
    try:
        print('Infer {:}'.format(image_name))
        outputs, img_info = detector.inference(image_name)
        
        image_basename = os.path.basename(image_name)
        image_loc_part = str(image_basename.split('.')[1]).split('-')
        image_loc_int = [int(x[1:]) for x in image_loc_part]
        print("Patching back slide H {:}-{:}, W {:}-{:}".format(image_loc_int[0], image_loc_int[1], image_loc_int[2], image_loc_int[3]))

        patch_rlt, objs_in_img = detector.visual(outputs[0], img_info, detector.confthre)

        result_image[image_loc_int[0]:image_loc_int[1],image_loc_int[2]:image_loc_int[3]] = patch_rlt
            
        if len(objs_in_img) > 0:
            is_ok = False
    except Exception as e:
        err_msg = "Failed to Infer {:}".format(image_name)
        err_tb = traceback.format_exc()
        print(err_msg)
        print(err_tb)
Received a dicom image shape = 640(W) x 640(H)
W overlap = 0, H overlap = 0
0 255
Chunk slide H 0000-0640, W 0000-0640
Infer data/11/Chunk.H0000-H0640-W0000-W0640.jpg
Patching back slide H 0-640, W 0-640
In [4]:
from matplotlib import pyplot as plt
plt.imshow(result_image.astype(np.uint8))
Out[4]:
<matplotlib.image.AxesImage at 0x33c0e86d0>
No description has been provided for this image