Example #1
0
def image_to_HED(path):
    '''
    Here we load all the pretrained models and register the CropLayer class in it.
    Then we construct a blob from the image and pass it to the net.Then we resize the 
    output to our desired shape and 0-255 colour scale and ensure that its of uint8 type.
    '''
    protoPath = os.path.join("deeppixel", "edge_detection", "hed_model",
                             "deploy.prototxt")
    modelPath = os.path.join("deeppixel", "edge_detection", "hed_model",
                             "hed_pretrained_bsds.caffemodel")
    net = cv2.dnn.readNetFromCaffe(protoPath, modelPath)

    image = cv2.imread(path)
    cv2.dnn_registerLayer("Crop", CropLayer)
    (H, W) = image.shape[:2]
    blob = cv2.dnn.blobFromImage(image,
                                 scalefactor=1.0,
                                 size=(W, H),
                                 mean=(104.00698793, 116.66876762,
                                       122.67891434),
                                 swapRB=False,
                                 crop=False)
    net.setInput(blob)
    hed_img = net.forward()
    hed_img = cv2.resize(hed_img[0, 0], (W, H))
    hed_img = (255 * hed_img).astype("uint8")
    return hed_img
Example #2
0
    def do_pb(self):

        path = pjoin(self.root_path, 'precomp_desc', 'pb')
        # print('getting probability boundaries...')
        if (os.path.exists(path)):
            # print('found directory {}. Delete to re-run'.format(path))
            return
        else:
            os.makedirs(path)

        self.get_model()
        self.model = cv.dnn.readNet(self.model_arch_path,
                                    self.model_weights_path)
        cv.dnn_registerLayer('Crop', CropLayer)

        print('will save frames to {}'.format(path))
        pbar = tqdm(total=len(self.dl))
        for s in self.dl:
            im = (s['image'] * 255).astype(np.uint8)
            inp = cv.dnn.blobFromImage(im,
                                       scalefactor=1.0,
                                       size=(512, 512),
                                       mean=(104.00698793, 116.66876762,
                                             122.67891434),
                                       swapRB=False,
                                       crop=False)
            self.model.setInput(inp)
            out = self.model.forward()
            out = out[0, 0]
            out = cv.resize(out, (im.shape[1], im.shape[0]))
            out = 255 * out
            out = out.astype(np.uint8)

            io.imsave(pjoin(path, s['frame_name']), out)
            pbar.update(1)
Example #3
0
    def edgify_picture(self, input_picture: pathlib.Path,
                       output_picture: pathlib.Path):
        print(input_picture.resolve())
        print(output_picture.resolve())

        cv2.dnn_registerLayer('Crop', CropLayer)

        frame = cv2.imread(str(input_picture.resolve()))

        # Get width and height
        height, width, ch = frame.shape

        inp = cv2.dnn.blobFromImage(frame,
                                    scalefactor=1.0,
                                    size=(width, height),
                                    mean=(104.00698793, 116.66876762,
                                          122.67891434),
                                    swapRB=False,
                                    crop=False)
        self.net.setInput(inp)
        out = self.net.forward()
        out = out[0, 0]
        out = cv2.resize(out, (frame.shape[1], frame.shape[0]))
        out = 255 * out
        out = out.astype(np.uint8)
        out = cv2.cvtColor(out, cv2.COLOR_GRAY2BGR)

        cv2.imwrite(str(output_picture.resolve()), out)
Example #4
0
def holisticallyNestedEdgeDetection(img, noiseImg, register=False):
    img = cv.cvtColor(img, cv.COLOR_GRAY2RGB)

    if register:
        # ! [Register]
        cv.dnn_registerLayer('Crop', CropLayer)
        # ! [Register]

    # Load the model.
    net = cv.dnn.readNetFromCaffe(
        "C:/Users/rebeb/Documents/TU_Wien/Dipl/project/caffe/deploy.prototxt",
        "C:/Users/rebeb/Documents/TU_Wien/Dipl/project/caffe/hed_pretrained_bsds.caffemodel"
    )

    height, width, _ = img.shape
    inp = cv.dnn.blobFromImage(img,
                               scalefactor=1.0,
                               size=(width, height),
                               mean=(104.00698793, 116.66876762, 122.67891434),
                               swapRB=False,
                               crop=False)
    net.setInput(inp)

    out = net.forward()
    out = out[0, 0]
    out = cv.resize(out, (img.shape[1], img.shape[0]))

    out = out * noiseImg
    return out
Example #5
0
def holistically_nested_edge_image(img, image_path=True):
    '''
    use the hlistically nested edge to detect the edges
    img: file name or image itself (read by the imread)
    image_path = True => img is the path
            = False => already the image, datasets
    '''
    args = {
        'image': img,
        'edge_detector': './holistically-nested-edge-detection/hed_model'
    }

    roots = './holistically-nested-edge-detection/hed_model'
    protoPath = os.path.join(args['edge_detector'], 'deploy.prototxt')
    modelPath = os.path.join(args['edge_detector'],
                             'hed_pretrained_bsds.caffemodel')

    net = cv2.dnn.readNetFromCaffe(protoPath, modelPath)

    # register our new layer with the model
    cv2.dnn_registerLayer('Crop', CropLayer)

    # load the input image and grab its dimensions
    if image_path:
        image = cv2.imread(args['image'])
    else:
        image = args['image']
    (H, W) = image.shape[:2]

    # convert the image to grayscale, blur it, and perform Canny edge detection
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    # canny = cv2.Canny(blurred, 30, 150)
    canny = cv2.Canny(gray, 20,
                      30)  # to show the paper edge, stop to do the blur

    # construct a blob out of the input image for the Holistically-Nested Edge Detector
    blob = cv2.dnn.blobFromImage(image,
                                 scalefactor=1.0,
                                 size=(W, H),
                                 mean=(104.00698793, 116.66876762,
                                       122.67891434),
                                 swapRB=True,
                                 crop=True)

    # set the blob as the input to the network and perform a forward pass to compute the edges
    net.setInput(blob)
    hed = net.forward()
    hed = cv2.resize(hed[0, 0], (W, H))
    hed = (255 * hed).astype('uint8')

    # show the output edge detection results for Canny and Holistically-Nested Edge Detection
    cv2.imshow('Input', image)
    cv2.imshow('Canny', canny)
    cv2.imshow('HED', hed)
    cv2.waitKey(0)
Example #6
0
def remove_background_hed(img_rgb, debug):
    """
    https://www.pyimagesearch.com/2019/03/04/holistically-nested-edge-detection-with-opencv-and-deep-learning/
    """
    # load our serialized edge detector from disk
    print("[INFO] loading edge detector...")
    proto_path = os.path.sep.join(["hed_model", "deploy.prototxt"])
    model_path = os.path.sep.join(
        ["hed_model", "hed_pretrained_bsds.caffemodel"])
    net = cv2.dnn.readNetFromCaffe(proto_path, model_path)

    # register our new layer with the model
    cv2.dnn_registerLayer("Crop", CropLayer)

    # load the input image and grab its dimensions
    (H, W) = img_rgb.shape[:2]

    # construct a blob out of the input image for the Holistically-Nested
    # Edge Detector
    blob = cv2.dnn.blobFromImage(img_rgb,
                                 scalefactor=1.0,
                                 size=(W, H),
                                 mean=(104.00698793, 116.66876762,
                                       122.67891434),
                                 swapRB=False,
                                 crop=False)

    # set the blob as the input to the network and perform a forward pass
    # to compute the edges
    logger.info("performing holistically-nested edge detection...")
    net.setInput(blob)
    hed = net.forward()
    hed = cv2.resize(hed[0, 0], (W, H))
    hed = (255 * hed).astype("uint8")

    # Sharpen the hed mask
    hed[hed < 40] = 0

    contours, hierarchy = cv2.findContours(image=hed,
                                           mode=cv2.RETR_EXTERNAL,
                                           method=cv2.CHAIN_APPROX_NONE)

    mask_shape = np.zeros(img_rgb.shape[:2], dtype="uint8")
    cv2.drawContours(image=mask_shape,
                     contours=contours,
                     contourIdx=-1,
                     color=255,
                     thickness=cv2.FILLED)

    filtered_img = cv2.bitwise_and(img_rgb, img_rgb, mask=mask_shape)

    if debug:
        cv2.imshow("HED", hed)
        cv2.imshow("Bkgrd Mask", filtered_img)
        cv2.waitKey(0)

    return mask_shape
Example #7
0
    def __init__(self, args):
        self._args = args
        if not self._args.use_canny:
            cv2.dnn_registerLayer('Crop', CropLayer)
            # Load caffe model
            self._net = cv2.dnn.readNetFromCaffe(self._args.model_def,
                                                 self._args.pretrained_model)

        self._get_img_paths()
Example #8
0
    def __init__(self, edge_detector_path):
        # load our serialized edge detector from disk
        print("[INFO] loading edge detector...")
        protoPath = os.path.sep.join([edge_detector_path, "deploy.prototxt"])
        modelPath = os.path.sep.join(
            [edge_detector_path, "hed_pretrained_bsds.caffemodel"])
        self.net = cv2.dnn.readNetFromCaffe(protoPath, modelPath)

        # register our new layer with the model
        cv2.dnn_registerLayer("Crop", CropLayer)
Example #9
0
 def __init__(self,
              width=500,
              height=500,
              prototxt="deploy.prototxt",
              caffemodel="hed_pretrained_bsds.caffemodel"):
     self.prototxt = prototxt
     self.caffemodel = caffemodel
     self.height = height
     self.width = width
     self.net = cv.dnn.readNet(self.prototxt, self.caffemodel)
     cv.dnn_registerLayer('Crop', CropLayer)
Example #10
0
 def holistic_edge(self, name="holistic.png"):
     net = cv2.dnn.readNetFromCaffe(self.holistic_proto_path, self.holistic_model_path)
     cv2.dnn_registerLayer("Crop", self.CropLayer)
     (H, W) = self.image.shape[:2]
     blob = cv2.dnn.blobFromImage(self.image, scalefactor=1.0, size=(W, H),
                                  mean=(104.00698793, 116.66876762, 122.67891434),
                                  swapRB=False, crop=False)
     net.setInput(blob)
     hed = net.forward()
     hed = cv2.resize(hed[0, 0], (W, H))
     hed = (255 * hed).astype("uint8")
     cv2.imwrite(name, hed)
Example #11
0
def hed_edges(image):
    import cv2 as cv

    # based on https://github.com/opencv/opencv/blob/master/samples/dnn/edge_detection.py
    class CropLayer(object):
        def __init__(self, params, blobs):
            self.xstart = 0
            self.xend = 0
            self.ystart = 0
            self.yend = 0

        # Our layer receives two inputs. We need to crop the first input blob
        # to match a shape of the second one (keeping batch size and number of channels)
        def getMemoryShapes(self, inputs):
            inputShape, targetShape = inputs[0], inputs[1]
            batchSize, numChannels = inputShape[0], inputShape[1]
            height, width = targetShape[2], targetShape[3]
            self.ystart = int((inputShape[2] - targetShape[2]) / 2)
            self.xstart = int((inputShape[3] - targetShape[3]) / 2)
            self.yend = self.ystart + height
            self.xend = self.xstart + width
            return [[batchSize, numChannels, height, width]]

        def forward(self, inputs):
            return [
                inputs[0][:, :, self.ystart:self.yend, self.xstart:self.xend]
            ]

    # Load the pretrained model (source: https://github.com/s9xie/hed)
    script_path = Path(__file__).parent.absolute()
    hed_path = Path.joinpath(script_path, 'HED')
    net = cv.dnn.readNetFromCaffe(
        str(hed_path / 'deploy.prototxt'),
        str(hed_path / 'hed_pretrained_bsds.caffemodel'))
    cv.dnn_registerLayer('Crop', CropLayer)

    image = cv.resize(image, (image.shape[1], image.shape[0]))
    # prepare image as input dataset (mean values from full image dataset)
    inp = cv.dnn.blobFromImage(
        image,
        scalefactor=1.0,
        size=(image.shape[1], image.shape[0]),  #w,h
        mean=(104.00698793, 116.66876762, 122.67891434),
        swapRB=False,
        crop=False)
    net.setInput(inp)
    out = net.forward()
    cv.dnn_unregisterLayer('Crop')  # get rid of issues when run in a loop
    out = out[0, 0]
    return out
Example #12
0
def holistically_nested(image):
    p_i("Starting Holistically-Nested Edge Detection...")
    net = cv2.dnn.readNetFromCaffe(
        "data/hed_model/deploy.prototxt",
        "data/hed_model/" + "hed_pretrained_bsds.caffemodel",
    )
    height, width = image.shape[:2]
    cv2.dnn_registerLayer("Crop", CropLayer)
    blob = cv2.dnn.blobFromImage(image, size=(width, height))
    net.setInput(blob)
    hed = (255 * cv2.resize(net.forward()[0, 0],
                            (width, height))).astype("uint8")
    p_i("Holistically-Nested Edge Detection complete!")
    cv2.dnn_unregisterLayer("Crop")
    return hed
    def __init__(self, image_):
        # initializing hed model
        proto_path = os.path.sep.join(['hed_model', "deploy.prototxt"])
        model_path = os.path.sep.join(
            ['hed_model', "hed_pretrained_bsds.caffemodel"])
        self.net = cv2.dnn.readNetFromCaffe(proto_path,
                                            model_path)  # uploading HED

        # register our new layer with the model
        cv2.dnn_registerLayer("Crop", CropLayer)

        # instances of cv2 and PIL images
        self.image, self.out_pil = image_, image_
        # image sizes
        self.H, self.W = None, None
Example #14
0
def edRun(imInput):
    # load our serialized edge detector from disk
    print("[INFO] loading edge detector...")
    protoPath = os.path.sep.join(["hed_model/", "deploy.prototxt"])
    modelPath = os.path.sep.join(
        ["hed_model/", "hed_pretrained_bsds.caffemodel"])
    net = cv2.dnn.readNetFromCaffe(protoPath, modelPath)

    # register our new layer with the model
    cv2.dnn_registerLayer("Crop", CropLayer)

    # load the input image and grab its dimensions
    image = cv2.imread(imInput)  #from GUI
    (H, W) = image.shape[:2]

    # construct a blob out of the input image for the Holistically-Nested
    # Edge Detector
    #blob = cv2.dnn.blobFromImage(image, scalefactor=1.0, size=(W, H),
    #	mean=(104.00698793, 116.66876762, 122.67891434),
    #	swapRB=False, crop=False)
    blob = cv2.dnn.blobFromImage(image,
                                 scalefactor=1.0,
                                 size=(W, H),
                                 mean=(104.00698793, 116.66876762,
                                       122.67891434),
                                 swapRB=False,
                                 crop=False)

    # set the blob as the input to the network and perform a forward pass
    # to compute the edges
    print("[INFO] performing holistically-nested edge detection...")
    net.setInput(blob)
    hed = net.forward()
    hed = cv2.resize(hed[0, 0], (W, H))
    hed = (255 * hed).astype("uint8")

    print(hed.shape)

    new_hed = Image.fromarray(hed)
    new_hed.save("hed_result.jpg")
    '''
	#for test only
	cv2.imshow('input', image) 
	cv2.imshow('output', hed)
	cv2.waitKey(0)
	'''
    return new_hed
Example #15
0
def find_hed(image):
    class CropLayer():
        def __init__(self, params, blobs):
            self.xstart = 0
            self.xend = 0
            self.ystart = 0
            self.yend = 0

        def getMemoryShapes(self, inputs):
            input_shape, target_shape = inputs[0], inputs[1]
            batch_size, num_channels = input_shape[0], input_shape[1]
            height, width = target_shape[2], target_shape[3]

            self.ystart = (input_shape[2] - target_shape[2]) // 2
            self.xstart = (input_shape[3] - target_shape[3]) // 2
            self.yend = self.ystart + height
            self.xend = self.xstart + width

            return [[batch_size, num_channels, height, width]]

        def forward(self, inputs):
            return [
                inputs[0][:, :, self.ystart:self.yend, self.xstart:self.xend]
            ]

    Height, Width = image.shape[:2]

    cv2.dnn_registerLayer('Crop', CropLayer)
    net = cv2.dnn.readNet(hed_prototext_path, hed_model_path)

    blob = cv2.dnn.blobFromImage(image,
                                 scalefactor=1.0,
                                 size=(Width, Height),
                                 mean=(104.00698793, 116.66876762,
                                       122.67891434),
                                 swapRB=False,
                                 crop=False)
    net.setInput(blob)
    hed = net.forward()
    hed = cv2.resize(hed[0, 0], (Width, Height))
    hed = (255 * hed).astype("uint8")

    return hed
Example #16
0
def get_model(model_root_path=os.path.expanduser(pjoin('~', '.models'))):

    if not os.path.exists(model_root_path):
        os.makedirs(model_root_path)

    model_weights_path = os.path.expanduser(
        pjoin(model_root_path, model_weights_file))

    model_arch_path = os.path.expanduser(
        pjoin(model_root_path, model_arch_file))

    if (not os.path.exists(model_weights_path)):
        print('Downloading HED model weights to {}'.format(model_weights_path))
        urllib.request.urlretrieve(model_weights_url, model_weights_path)

    if (not os.path.exists(model_arch_path)):
        print('Downloading HED model prototype to {}'.format(model_arch_path))
        urllib.request.urlretrieve(model_arch_url, model_arch_path)

    model = cv.dnn.readNet(model_arch_path, model_weights_path)
    cv.dnn_registerLayer('Crop', CropLayer)

    return model
def init_edge_network(self):

    protoPathEdgeNet = "models/edge/deploy.prototxt"
    modelPathEdgeNet = "models/edge/hed_pretrained_bsds.caffemodel"

    self.edgeNet = cv.dnn.readNet(protoPathEdgeNet, modelPathEdgeNet)
    if self.cpu_type == 1:
        self.edgeNet.setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD)
    if self.cpu_type == 2:
        self.edgeNet.setPreferableTarget(cv2.dnn.DNN_TARGET_OPENCL)

    cv.dnn_registerLayer('Crop', CropLayer)

    self.pb.pack(expand=True, fill=tki.BOTH, padx=[200, 0])
    self.pb.start()

    text = "Show me what you got :D"

    self.top_label_text.set(text)
    img = ImageTk.PhotoImage(Image.open("ui/images/poseestima.png"))
    self.panel_pass.configure(image=img)
    self.panel_pass.image = img

    self.active_menu = 8
Example #18
0
def hedConvert(image):
    protoPath = os.path.sep.join(["hed_model", "deploy.prototxt"])
    modelPath = os.path.sep.join(
        ["hed_model", "hed_pretrained_bsds.caffemodel"])
    net = cv2.dnn.readNetFromCaffe(protoPath, modelPath)

    cv2.dnn_registerLayer("Crop", CropLayer)

    (H, W) = image.shape[:2]
    blob = cv2.dnn.blobFromImage(image,
                                 scalefactor=1.0,
                                 size=(W, H),
                                 mean=(104.00698793, 116.66876762,
                                       122.67891434),
                                 swapRB=False,
                                 crop=False)

    net.setInput(blob)
    hed = net.forward()
    hed = cv2.resize(hed[0, 0], (W, H))
    hed = (255 * hed).astype("uint8")

    cv2.dnn_unregisterLayer("Crop")
    return hed
Example #19
0
        batchSize, numChannels = inputShape[0], inputShape[1]
        height, width = targetShape[2], targetShape[3]

        self.ystart = (inputShape[2] - targetShape[2]) / 2
        self.xstart = (inputShape[3] - targetShape[3]) / 2
        self.yend = self.ystart + height
        self.xend = self.xstart + width

        return [[batchSize, numChannels, height, width]]

    def forward(self, inputs):
        return [inputs[0][:,:,self.ystart:self.yend,self.xstart:self.xend]]
#! [CropLayer]

#! [Register]
cv.dnn_registerLayer('Crop', CropLayer)
#! [Register]

# Load the model.
net = cv.dnn.readNet(cv.samples.findFile(args.prototxt), cv.samples.findFile(args.caffemodel))

kWinName = 'Holistically-Nested Edge Detection'
cv.namedWindow('Input', cv.WINDOW_NORMAL)
cv.namedWindow(kWinName, cv.WINDOW_NORMAL)

cap = cv.VideoCapture(args.input if args.input else 0)
while cv.waitKey(1) < 0:
    hasFrame, frame = cap.read()
    if not hasFrame:
        cv.waitKey()
        break
ap.add_argument("-s", "--start", type=int, required=True,
	help="start index for images")
ap.add_argument("-e", "--end", type=int, required=True,
	help="end index")
ap.add_argument("-p", "--display", type=int, required=True,
	help="end index")
args = vars(ap.parse_args())

# Create Network Connections based off trained model
protoPath = os.path.sep.join([args["edge_detector"],
"deploy.prototxt"])
modelPath = os.path.sep.join([args["edge_detector"],
	"hed_pretrained_bsds.caffemodel"])
net = cv2.dnn.readNetFromCaffe(protoPath, modelPath)
# register our new layer with the model holding our desired image size
cv2.dnn_registerLayer("Crop", HEDmodel.HEDmodel)


path = args["path"] + '/*.png'
listOfGT = ['images/GT']
# avg_set = [[], [], []]
# listOfPaths = ['images/00', 'images/01', 'images/02', 'images/03', 'images/04', 'images/05', 'images/06', 'images/07', 'images/08', 'images/09', 'images/10', 'images/11', 'images/12', 'images/13', 'images/14', 'images/15', 'images/16', 'images/17', 'images/18']
listOfPaths = ['images/Test']

for pathPart in listOfPaths:
	path = pathPart + '/*.png'
	verticalStitch = []
	start = args["start"]
	end = args["end"]
	shouldShow = args["display"]
	diff = end - start
Example #21
0
 def __init__(self, name):
   self.name = name
   self.net = cv2.dnn_registerLayer('Crop',DNNPreprocessingImgage.CropLayer)
   self.net = cv2.dnn.readNet(os.path.join("./module",'deploy.prototxt'),os.path.join("./module",'./hed_pretrained_bsds.caffemodel'))
Example #22
0
def holistically_nested_edge_video(video_path):
    '''
    use the hlistically nested edge to detect the edges
    img: file name or image itself (read by the imread)
    image_path = True => img is the path
            = False => already the image, datasets
    '''
    args = {
        'input': video_path,
        'edge_detector': './holistically-nested-edge-detection/hed_model'
    }

    roots = './holistically-nested-edge-detection/hed_model'
    protoPath = os.path.join(args['edge_detector'], 'deploy.prototxt')
    modelPath = os.path.join(args['edge_detector'],
                             'hed_pretrained_bsds.caffemodel')

    # get the video
    # vs = cv2.VideoCapture(args['input'])
    fvs = FileVideoStream(args['input']).start()
    # time.sleep(1.0)

    fps = FPS().start()

    net = cv2.dnn.readNetFromCaffe(protoPath, modelPath)

    # register our new layer with the model
    cv2.dnn_registerLayer('Crop', CropLayer)

    # loop over frames from the video streams
    # while True:
    while fvs.more():

        # grab the next frame and handle if we are reading from either videocapture or video stream
        # frame = vs.read()[1]
        frame = fvs.read()

        # # for the vs part
        # if frame is None:
        #     break

        frame = imutils.resize(frame, width=500)
        (H, W) = frame.shape[:2]

        # convert the image to grayscale, blur it, and perform Canny edge detection
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # blurred = cv2.GaussianBlur(gray, (5, 5), 0)
        blurred = cv2.GaussianBlur(gray, (5, 5), 0)
        # canny = cv2.Canny(blurred, 30, 150)
        canny = cv2.Canny(gray, 30,
                          150)  # to show the paper edge, stop to do the blur

        # construct a blob out of the input image for the Holistically-Nested Edge Detector
        blob = cv2.dnn.blobFromImage(frame,
                                     scalefactor=1.0,
                                     size=(W, H),
                                     mean=(104.00698793, 116.66876762,
                                           122.67891434),
                                     swapRB=False,
                                     crop=False)

        # set the blob as the input to the network and perform a forward pass to compute the edges
        net.setInput(blob)
        hed = net.forward()
        hed = cv2.resize(hed[0, 0], (W, H))
        hed = (255 * hed).astype('uint8')

        # show the output edge detection results for Canny and Holistically-Nested Edge Detection
        cv2.imshow('Frame', frame)
        cv2.imshow('Canny', canny)
        cv2.imshow('HED', hed)
        key = cv2.waitKey(1) & 0xFF

        fps.update()

        if key == ord('q'):
            break

    fps.stop()
    # vs.release()
    cv2.destroyAllWindows()
    fvs.stop()
Example #23
0
 def __init__(self):
     self.net = cv.dnn.readNetFromCaffe(
         os.path.join("hed", "deploy.prototxt"),
         os.path.join("hed", "hed_pretrained_bsds.caffemodel"))
     cv.dnn_registerLayer('Crop', CropLayer)
Example #24
0
    def HED(input=('ImagePin', 0),
            img=(REF, ('ImagePin', 0)),
            _canny=(REF, ('ImagePin', 0))):
        """Holistically-Nested Edge Detection"""
        image = input.image

        class CropLayer(object):
            def __init__(self, params, blobs):
                # initialize our starting and ending (x, y)-coordinates of
                # the crop
                self.startX = 0
                self.startY = 0
                self.endX = 0
                self.endY = 0

            def getMemoryShapes(self, inputs):
                # the crop layer will receive two inputs -- we need to crop
                # the first input blob to match the shape of the second one,
                # keeping the batch size and number of channels
                (inputShape, targetShape) = (inputs[0], inputs[1])
                (batchSize, numChannels) = (inputShape[0], inputShape[1])
                (H, W) = (targetShape[2], targetShape[3])
                # compute the starting and ending crop coordinates
                self.startX = int((inputShape[3] - targetShape[3]) / 2)
                self.startY = int((inputShape[2] - targetShape[2]) / 2)
                self.endX = self.startX + W
                self.endY = self.startY + H
                # return the shape of the volume (we'll perform the actual
                # crop during the forward pass
                return [[batchSize, numChannels, H, W]]

            def forward(self, inputs):
                # use the derived (x, y)-coordinates to perform the crop
                return [
                    inputs[0][:, :, self.startY:self.endY,
                              self.startX:self.endX]
                ]

        (H, W) = image.shape[:2]
        # convert the image to grayscale, blur it, and perform Canny edge detection
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        blurred = cv2.GaussianBlur(gray, (5, 5), 0)
        canny = cv2.Canny(blurred, 30, 150)

        blob = cv2.dnn.blobFromImage(image,
                                     scalefactor=1.0,
                                     size=(W, H),
                                     mean=(104.00698793, 116.66876762,
                                           122.67891434),
                                     swapRB=False,
                                     crop=False)
        # set the blob as the input to the network and perform a forward pass to compute the edges
        protoPath = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                                 "..", "res", "deploy.prototxt")
        modelPath = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                                 "..", "res", "hed_pretrained_bsds.caffemodel")
        net = cv2.dnn.readNetFromCaffe(protoPath, modelPath)
        # register our new layer with the model
        cv2.dnn_registerLayer("Crop", CropLayer)
        net.setInput(blob)
        hed = net.forward()
        hed = cv2.resize(hed[0, 0], (W, H))
        hed = (255 * hed).astype("uint8")

        _canny(canny)
        img(hed)
Example #25
0
# load our serialized edge detector from disk
print("[INFO] loading edge detector...")

fpath = os.path.abspath(__file__)
fdir = os.path.dirname(fpath)
print(fdir)
protoPath = os.path.sep.join([fdir, "hed_model", "deploy.prototxt"])
print(protoPath)
modelPath = os.path.sep.join(
    [fdir, "hed_model", "hed_pretrained_bsds.caffemodel"])
print(modelPath)

net = cv2.dnn.readNetFromCaffe(protoPath, modelPath)

# register our new layer with the model
cv2.dnn_registerLayer("Crop", CropLayer)

# load the input image and grab its dimensions
source_img = cv2.imread(path)

# -====================================
# cv2.imshow("source_img", source_img)
# while cv2.waitKey(1) != 13: pass
# cv2.destroyAllWindows()
# -====================================

(H, W) = source_img.shape[:2]
# image = source_img
# image = source_img[y:y+h, x:x+w]

imgOut = np.copy(source_img[:, :, 0])
Example #26
0
    onnx_name = "./yolo-fastest-xl.onnx"
    model = onnx.load(onnx_name)
    #检查IR是否良好
    onnx.checker.check_model(model)

    # 添加 ExpLayer
    class ExpLayer(object):
        def __init__(self, params, blobs):
            super(ExpLayer, self).__init__()

        def getMemoryShapes(self, inputs):
            return inputs

        def forward(self, inputs):
            return [np.exp(inputs[0])]
    cv2.dnn_registerLayer('Exp', ExpLayer)

    # opencv dnn加载
    import numpy as np
    net = cv2.dnn.readNetFromONNX(onnx_name)
    img = inputs.numpy()
    img = img[0]
    img = img.transpose((1,2,0))
    img = img.astype('uint8')
    blob = cv2.dnn.blobFromImage(img, size=(320, 320))      # img 必须是uint8
    net.setInput(blob)
    out = net.forward("out0")
    print(out)

    # 检查opencv加载onnx以后的结果和原来的结果是否相同
    outputs = outputs[0].detach().numpy()
Example #27
0
    x = [i for i in range(edges.shape[0]) if np.count_nonzero(edges[i] == True, axis = 0)>0]
    
#     for i in range(0,edges.shape[0]):
#         if (edges[i].any() == True):
#             x.append(i)
    y = [i for i in range(edges.shape[1]) if np.count_nonzero(edges[:,i] == True, axis = 0)>0]
#     for i in range(0,edges.transpose().shape[0]):
#         if (edges.transpose()[i].any() == True):
#             y.append(i)
    if ((len(x)>0) and (len(y)>0)):
        image = image[min(x):max(x),min(y):max(y)]
    
    return image# , min(x),max(x),min(y),max(y)


cv2.dnn_registerLayer('Crop', CropLayer)

# Load the model.
net = cv2.dnn.readNet(args.prototxt, args.caffemodel)


# load the input image and grab its dimensions
image = cv2.imread(args.input)
# image =cv2.equalizeHist(img)
# image = cv2.pyrMeanShiftFiltering(image1,10,20)

## Create a display window
kWinName = 'Holistically-Nested_Edge_Detection'
cv2.namedWindow(kWinName, cv2.WINDOW_AUTOSIZE)

inp = cv2.dnn.blobFromImage(image, scalefactor=1.0, size=(args.width, args.height),
Example #28
0
        out_h = inp_w * ratio
        start = int(center_h - out_h // 2)
        end = int(center_h + out_h // 2)
        person_img = person_img[start:end, ...]
    else:
        center_w = inp_w // 2
        out_w = inp_h / ratio
        start = int(center_w - out_w // 2)
        end = int(center_w + out_w // 2)
        person_img = person_img[:, start:end, :]

    cloth_img = cv.imread(args.input_cloth)
    pose = get_pose_map(person_img, findFile(args.openpose_proto),
                        findFile(args.openpose_model), args.backend, args.target)
    segm_image = parse_human(person_img, args.segmentation_model)
    segm_image = cv.resize(segm_image, (192, 256), cv.INTER_LINEAR)

    cv.dnn_registerLayer('Correlation', CorrelationLayer)

    model = CpVton(args.gmm_model, args.tom_model, args.backend, args.target)
    agnostic = model.prepare_agnostic(segm_image, person_img, pose)
    warped_cloth = model.get_warped_cloth(cloth_img, agnostic)
    output = model.get_tryon(agnostic, warped_cloth)

    cv.dnn_unregisterLayer('Correlation')

    winName = 'Virtual Try-On'
    cv.namedWindow(winName, cv.WINDOW_AUTOSIZE)
    cv.imshow(winName, output)
    cv.waitKey()
Example #29
0
    def test_custom_layer(self):
        class CropLayer(object):
            def __init__(self, params, blobs):
                self.xstart = 0
                self.xend = 0
                self.ystart = 0
                self.yend = 0

            # Our layer receives two inputs. We need to crop the first input blob
            # to match a shape of the second one (keeping batch size and number of channels)
            def getMemoryShapes(self, inputs):
                inputShape, targetShape = inputs[0], inputs[1]
                batchSize, numChannels = inputShape[0], inputShape[1]
                height, width = targetShape[2], targetShape[3]
                self.ystart = (inputShape[2] - targetShape[2]) // 2
                self.xstart = (inputShape[3] - targetShape[3]) // 2
                self.yend = self.ystart + height
                self.xend = self.xstart + width
                return [[batchSize, numChannels, height, width]]

            def forward(self, inputs):
                return [
                    inputs[0][:, :, self.ystart:self.yend,
                              self.xstart:self.xend]
                ]

        cv.dnn_registerLayer('CropCaffe', CropLayer)
        proto = '''
        name: "TestCrop"
        input: "input"
        input_shape
        {
            dim: 1
            dim: 2
            dim: 5
            dim: 5
        }
        input: "roi"
        input_shape
        {
            dim: 1
            dim: 2
            dim: 3
            dim: 3
        }
        layer {
          name: "Crop"
          type: "CropCaffe"
          bottom: "input"
          bottom: "roi"
          top: "Crop"
        }'''

        net = cv.dnn.readNetFromCaffe(bytearray(proto.encode()))
        for backend, target in self.dnnBackendsAndTargets:
            if backend != cv.dnn.DNN_BACKEND_OPENCV:
                continue

            printParams(backend, target)

            net.setPreferableBackend(backend)
            net.setPreferableTarget(target)
            src_shape = [1, 2, 5, 5]
            dst_shape = [1, 2, 3, 3]
            inp = np.arange(0, np.prod(src_shape),
                            dtype=np.float32).reshape(src_shape)
            roi = np.empty(dst_shape, dtype=np.float32)
            net.setInput(inp, "input")
            net.setInput(roi, "roi")
            out = net.forward()
            ref = inp[:, :, 1:4, 1:4]
            normAssert(self, out, ref)

        cv.dnn_unregisterLayer('CropCaffe')
Example #30
0
def load_dnn(prototxt, caffemodel):
    cv2.dnn_registerLayer('Crop', CropLayer)
    # Load the model.
    return cv2.dnn.readNet(prototxt, caffemodel)