示例#1
0
def prob_map(model_path, img_ori):
    model = torch.jit.load(model_path)
    model = torch.nn.DataParallel(model).cuda()

    with torch.no_grad():
        model.eval()
        try:
            img, _ = cv2.decolor(img_ori)
            img = np.array(img, dtype=np.float32)
            img = (img - 128.0) / 128.0
            img = torch.from_numpy(img.copy())
            print("img:", img.size())
            img = img.unsqueeze(0)
            input = img.unsqueeze(0)
            input = input.cuda()
            output = model(input)
            img_size_1, img_size_2 = input.size(2), input.size(3)
            output = output.squeeze()
            output = output.cpu().numpy()
            mask = np.zeros((img_size_1, img_size_2))
            x, y = np.where(output > 0.5)
            mask[x, y] = 255
            print('end')

        except:

            img, _ = cv2.decolor(img_ori)
            w, h = img.shape
            img = np.array(img, dtype=np.float32)
            img = (img - 128.0) / 128.0
            img = torch.from_numpy(img.copy())
            img = img.unsqueeze(0)
            img = img.unsqueeze(0)
            print("img:", img.size())
            img_slice1 = img[:, :, 0:w // 2, 0:h // 2]
            img_slice2 = img[:, :, w // 2:, 0:h // 2]
            img_slice3 = img[:, :, 0:w // 2, h // 2:]
            img_slice4 = img[:, :, w // 2:, h // 2:]
            # print("img slice1:", img_slice1.size())
            input1 = img_slice1.cuda()
            output1 = model(input1)
            input2 = img_slice2.cuda()
            output2 = model(input2)
            input3 = img_slice3.cuda()
            output3 = model(input3)
            input4 = img_slice4.cuda()
            output4 = model(input4)
            output = torch.cat((torch.cat(
                (output1, output2), 2), torch.cat((output3, output4), 2)), 3)
            output = output.squeeze()
            output = output.cpu().numpy()
            mask = np.zeros((w, h))
            x, y = np.where(output > 0.5)
            mask[x, y] = 255
            print('end oversize')
    return mask
示例#2
0
def decolor(img):
    gray = np.zeros(img.shape, np.uint8)
    gray = cv2.cvtColor(gray, cv2.CV_8UC1)
    color_boost = np.zeros(img.shape, np.uint8)
    color_boost = cv2.cvtColor(color_boost, cv2.CV_8UC3)
    decolor = cv2.decolor(img, gray, color_boost)
    return decolor
示例#3
0
def demo(device_num, ext='jpg', delay=1):
    cap = setup_webcam(device_num)
    # cascade = cv2.CascadeClassifier('cascades/haarcascade_frontalface_alt2.xml')
    cascade = cv2.CascadeClassifier(
        'cascades/haarcascade_frontalface_default.xml')
    p = Predictor(quant=True)
    inference_result = None

    while True:
        ret, frame = cap.read()
        key = cv2.waitKey(delay) & 0xFF
        if key == ord('q'):
            break

        # Execute Face Detection
        faces = detect_face(frame, cascade)

        # Draw Detected Face
        for (x, y, w, h) in faces:
            # Inference test data
            face_img = frame[y:y + h, x:x + w]
            face_img, _ = cv2.decolor(face_img)
            face_img = cv2.resize(face_img,
                                  dsize=(dataset.IMAGE_SIZE,
                                         dataset.IMAGE_SIZE))
            # cv2.imshow(WINDOW_NAME, face_img)
            start = time.time()
            inference_result = p(np.array(face_img, dtype=np.uint8))
            end = time.time()
            measured_time = (end - start) * 1000
            frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0),
                                  2)

        if inference_result is not None:
            expression = dataset.CLASS_NAME[np.argmax(inference_result)]
            if not np.argmax(inference_result) in [0, 3, 5, 6]:
                expression = "None"

            percentage = ": {0:.2f}".format(
                inference_result[np.argmax(inference_result)] * 100)
            cv2.putText(frame, expression + ": " + percentage, (10, 400),
                        cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2, cv2.LINE_AA)

            cv2.putText(
                frame,
                "{0:.2f} ms, {1:.2f} fps".format(measured_time,
                                                 1000.0 / measured_time),
                (10, 440), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2,
                cv2.LINE_AA)

        cv2.imshow(WINDOW_NAME, frame)

    cv2.destroyWindow(WINDOW_NAME)
示例#4
0
def threshold_otu(image: np.ndarray) -> np.ndarray:
    """大津の2値化

    Args:
      image: 変換元の画像

    Returns:
      変換後の画像

    """
    #: 退色処理
    image_gray, _ = cv2.decolor(image)

    #: 2値化
    image_gray[image_gray < 128] = 16
    image_gray[image_gray >= 128] = 240

    return image_gray
示例#5
0
def gray(img):
    g, _ = cv2.decolor(img)
    return g
示例#6
0
import cv2
import numpy as np

image = None
dec = None


image = cv2.imread('media/lena.jpg',1)
dec = (cv2.decolor(image))
cv2.imshow('gray',dec[0])
cv2.imshow('color',dec[1])
if cv2.waitKey(0)&0xff == 27:
  pass
cv2.destroyAllWindows()
示例#7
0
 def monochrome(self):
   copy_img = self.img.copy()
   # グレースケールはやり方がいくつかある。
   # self.reimg = cv2.cvtColor(copy_img, cv2.COLOR_BGR2GRAY)  # RGB2〜 でなく BGR2〜 を指定
   self.reimg, _ = cv2.decolor(copy_img)
示例#8
0
    def __getitem__(self, idx):
        mask = Image.open(self.mask[idx])
        img_name = self.mask[idx].split('/')[-1].split(
            '_mask')[0] + '_origin_cut_x1.png'
        img = Image.open(self.imgs[img_name])
        if self._way == "train":
            if np.random.rand() > 0.5:
                img = img.transpose(Image.FLIP_LEFT_RIGHT)
                mask = mask.transpose(Image.FLIP_LEFT_RIGHT)
            if self.use_scale:
                img, mask = self._generate_scale_label(np.array(img),
                                                       np.array(mask))
        img, mask = np.array(img), np.array(mask)
        img_h, img_w = mask.shape
        pad_h = max(self._crop_size - img_h, 0)
        pad_w = max(self._crop_size - img_w, 0)
        if pad_h > 0 or pad_w > 0:
            img_pad = cv2.copyMakeBorder(img,
                                         0,
                                         pad_h,
                                         0,
                                         pad_w,
                                         cv2.BORDER_CONSTANT,
                                         value=(255.0, 255.0, 255.0))
            mask_pad = cv2.copyMakeBorder(mask,
                                          0,
                                          pad_h,
                                          0,
                                          pad_w,
                                          cv2.BORDER_CONSTANT,
                                          value=(0.0, ))
        else:
            img_pad, mask_pad = img, mask
        img_h, img_w = mask_pad.shape
        h_off = random.randint(0, img_h - self._crop_size)
        w_off = random.randint(0, img_w - self._crop_size)
        while (np.all(mask_pad[h_off:h_off + self._crop_size,
                               w_off:w_off + self._crop_size] == 0)):
            h_off = random.randint(0, img_h - self._crop_size)
            w_off = random.randint(0, img_w - self._crop_size)
        img = np.asarray(img_pad[h_off:h_off + self._crop_size,
                                 w_off:w_off + self._crop_size])
        mask = np.asarray(
            mask_pad[h_off:h_off + self._crop_size,
                     w_off:w_off + self._crop_size], np.float32)

        img_cp = np.asarray(img, np.uint8)
        img, _ = cv2.decolor(img)

        img = np.array(img, dtype=np.float32)

        img = self.norm_or_Standard(img, self._normalize)

        if self._way == "train":
            # rotate
            num_rotate = np.random.randint(0, 4)
            img = np.rot90(img, num_rotate)
            mask = np.rot90(mask, num_rotate)
        mask = self._mask_transform(mask)
        img = torch.from_numpy(img.copy())

        img = img.unsqueeze(0)

        return img, mask, img_cp, img_name
    def paint_text(self, text, i):
        """ 使用PIL绘制文本图像,传入画布尺寸,返回文本图像
        :param h: 画布高度
        :param w: 画布宽度
        :return: img
        """
        # 创建画布背景
        bg_b = np.random.randint(0, 255)  # 背景色
        bg_g = np.random.randint(0, 255)
        bg_r = np.random.randint(0, 255)
        # 前景色
        fg_b = np.random.randint(0, 255)  # 背景色
        fg_g = np.random.randint(0, 255)
        fg_r = np.random.randint(0, 255)
        # 计算前景和背景的彩色相似度
        bg_color = sRGBColor(bg_b, bg_g, bg_r)
        bg_color = convert_color(bg_color, CMYKColor)  # 转cmyk
        bg_color = convert_color(bg_color, LabColor)
        fg_color = sRGBColor(fg_b, fg_g, fg_r)
        fg_color = convert_color(fg_color, CMYKColor)  # 转cmyk
        fg_color = convert_color(fg_color, LabColor)
        delta_e = delta_e_cie2000(bg_color, fg_color)
        while delta_e < 150 and delta_e > 250:  # 150-250
            # 创建画布背景色
            bg_b = np.random.randint(0, 255)
            bg_g = np.random.randint(0, 255)
            bg_r = np.random.randint(0, 255)
            # 文字前景色
            fg_b = np.random.randint(0, 255)
            fg_g = np.random.randint(0, 255)
            fg_r = np.random.randint(0, 255)
            # 计算前景和背景的彩色相似度
            bg_color = sRGBColor(bg_b, bg_g, bg_r)
            bg_color = convert_color(bg_color, LabColor)
            fg_color = sRGBColor(fg_b, fg_g, fg_r)
            fg_color = convert_color(fg_color, LabColor)
            delta_e = delta_e_cie2000(bg_color, fg_color)

        # 随机选择字体
        np.random.shuffle(self.font_name)
        cur_fonts = self.fonts.get(self.font_name[0])
        keys = list(cur_fonts.keys())
        np.random.shuffle(keys)
        font = cur_fonts.get(keys[0])
        text_size = font.getsize(text)

        # 根据字体大小创建画布
        img_w = text_size[0]
        img_h = text_size[1]

        # 文本区域上限
        h_space = np.random.randint(6, 20)
        w_space = 6
        h = img_h + h_space
        w = img_w + w_space
        canvas = Image.new('RGB', (w, h), (bg_b, bg_g, bg_r))
        draw = ImageDraw.Draw(canvas)

        # 随机平移
        start_x = np.random.randint(2, w_space-2)
        start_y = np.random.randint(2, h_space-2)

        # 绘制当前文本行
        draw.text((start_x, start_y), text, font=font, fill=(fg_b, fg_g, fg_r))
        img_array = np.array(canvas)
        # 透视失真
        src = np.float32([[start_x, start_y],
                          [start_x + w, start_y],
                          [start_x + w, start_y + h],
                          [start_x, start_y + h]])

        dst = np.float32([[start_x + np.random.randint(0, 10), start_y + np.random.randint(0, 5)],
                          [start_x + w - np.random.randint(0, 10), start_y + np.random.randint(0, 5)],
                          [start_x + w - np.random.randint(0, 10), start_y + h - np.random.randint(0, 5)],
                          [start_x + np.random.randint(0, 10), start_y + h - np.random.randint(0, 5)]])
        M = cv2.getPerspectiveTransform(src, dst)
        img_array = cv2.warpPerspective(img_array.copy(), M, (w, h),
                                  borderMode=cv2.BORDER_CONSTANT,
                                  borderValue=(bg_b, bg_g, bg_r))
        # Image.fromarray(img_array).show()
        # 随机旋转
        angle = np.random.randint(-8, 8)
        rotated = rotate_bound(img_array, angle=angle, bg_color=(bg_b, bg_g, bg_r))
        canvas = Image.fromarray(rotated)
        img_array = np.array(canvas.convert('CMYK'))[:,:,0:3]  # rgb to cmyk
        img_array = cv2.resize(img_array.copy(), (128, 32), interpolation=cv2.INTER_CUBIC)  # resize
        # noisy = add_noise(img_array)
        noisy = img_array
        # 统计
        # psnr.append(measure.compare_psnr(img_array, noisy))
        # ssim.append(measure.compare_ssim(img_array, noisy, multichannel=True))
        if not self.color:
            # 灰度化
            gray = np.zeros_like(img_array)  # 初始化灰度图输出矩阵
            cv2.decolor(img_array, gray)  # opencv以指针方式直接修改

            ndimg = Image.fromarray(gray)
            # ndimg.show()
            # 保存
            save_path = os.path.join(self.save_path, '{}.jpeg'.format(i))  # 类别序列即文件名
            ndimg.save(save_path)
        else:
            # 加噪
            # 画图看一下
            ndimg = Image.fromarray(noisy).convert('CMYK')
            # ndimg.show()
            # 保存
            save_path = os.path.join(self.save_path, '{}.jpeg'.format(i))  # 类别序列即文件名
            ndimg.save(save_path)
示例#10
0
def PreProcessImage(image):

    ## TODO searchwindow size based on image size
    ## Step 0 Non local means denoising
    
    ## nonLocalMeans needs nvidia gpu
    #x = cv2.cuda.nonLocalMeans(image.copy(),h=7,search_window=35,block_size=7)
    #showimage(x,'slownlmeans-0')
    
    ## scikit has slower version of nonlocalmeans but running time is very high even for 1mb image
    """
    sigma_est = np.mean(estimate_sigma(image.copy(), multichannel=False))
    print(f"estimated noise standard deviation = {sigma_est}")

    patch_kw = dict(patch_size=7,      # 5x5 patches
                    patch_distance=13, # 35x35 search area
                    multichannel=False)

    # slow algorithm
    image = denoise_nl_means(image.copy(), h=0.6 * sigma_est, fast_mode=False, **patch_kw)
    showimage(image, 'slow-nlmeans-0')
    """    

    image = cv2.fastNlMeansDenoisingColored(image,None,h=7,hColor=7,templateWindowSize=7,searchWindowSize=35)
    showimage(image, 'nlmeans-0')
    
       
    ##canonical1 Contours 502, good rects 490, compound rects 12, discarded rects 0, chars 427, dropped chars 3
    ##           Contours 504, good rects 493, compound rects 11, discarded rects 0, chars 428, dropped chars 3
    
    ##canonical2 Contours 668, good rects 664, compound rects 4, discarded rects 0, chars 562, dropped chars 6
    ##           Contours 625, good rects 623, compound rects 2, discarded rects 0, chars 556, dropped chars 5
    ##           Contours 786, good rects 784, compound rects 1, discarded rects 1, chars 571, dropped chars 1
    ##           Contours 661, good rects 659, compound rects 2, discarded rects 0, chars 567, dropped chars 3
    
    ##canonical3 Contours 471, good rects 463, compound rects 8, discarded rects 0, chars 406, dropped chars 2
    ##           Contours 474, good rects 466, compound rects 8, discarded rects 0, chars 406, dropped chars 2
    
    ##devnagri/style1 Contours 655, good rects 653, compound rects 0, discarded rects 2, chars 499, dropped chars 1
    ##                Contours 733, good rects 733, compound rects 0, discarded rects 0, chars 515, dropped chars 0
    ##                Contours 671, good rects 671, compound rects 0, discarded rects 0, chars 503, dropped chars 1
    ##                Contours 669, good rects 667, compound rects 0, discarded rects 2, chars 504, dropped chars 1
    ##                Contours 654, good rects 652, compound rects 0, discarded rects 0, chars 500
    
    ##tough1 Contours 721, good rects 682, compound rects 34, discarded rects 5, chars 449, dropped chars 22
    ##       Contours 729, good rects 696, compound rects 32, discarded rects 1, chars 514, dropped chars 22
    ##       Contours 687, good rects 648, compound rects 35, discarded rects 4, chars 466, dropped chars 27
    ##       Contours 670, good rects 638, compound rects 30, discarded rects 2, chars 511, dropped chars 23
    ##       Contours 721, good rects 689, compound rects 30, discarded rects 2, chars 513, dropped chars 22
    ##       Contours 721, good rects 689, compound rects 30, discarded rects 2, chars 513, dropped chars 22
    ##       Contours 667, good rects 629, compound rects 36, discarded rects 2, chars 450, dropped chars 45
    ##       Contours 667, good rects 629, compound rects 36, discarded rects 2, chars 454, dropped chars 36
    
    
    ##1238 Contours 793, good rects 788, compound rects 3, discarded rects 2, chars 631, dropped chars 8
    
    ##1239 Contours 858, good rects 853, compound rects 5, discarded rects 0, chars 658, dropped chars 21
    
    ##1240 Contours 826, good rects 816, compound rects 8, discarded rects 2, chars 663, dropped chars 11

    ## Step 1 bilateral with conservative params, but multiple times
    for i in range(100):
        image = cv2.bilateralFilter(image,2,10,10)
    showimage(image, 'bilateral-1')
    
    ## Step 2 median blur with conservative params
    image = cv2.medianBlur(image,3)
    showimage(image,'medianblurred-2')
    
    ## TODO see if this should be used.
    image,_ = cv2.decolor(image.copy())
    showimage(image, 'decolor-3')
    
    hist = cv2.calcHist([image],[0],None,[256],[0,256]) 
    plt.plot(hist)
    plt.xlabel('decolor')
    plt.show()
    
    ## Step 3 convert to grey scale
    #image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    #showimage(image, 'grayimage-3')
    #hist = cv2.calcHist([image],[0],None,[256],[0,256]) 
    #plt.plot(hist)
    #plt.xlabel('regular')
    #plt.show()
   
    return image
示例#11
0
def convert2(im):
    import cv2
    import numpy as np

    return Image.fromarray(cv2.decolor(np.array(im.convert('RGB')))[0])