edge_detection.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import cv2 as cv
  2. import argparse
  3. parser = argparse.ArgumentParser(
  4. description='This sample shows how to define custom OpenCV deep learning layers in Python. '
  5. 'Holistically-Nested Edge Detection (https://arxiv.org/abs/1504.06375) neural network '
  6. 'is used as an example model. Find a pre-trained model at https://github.com/s9xie/hed.')
  7. parser.add_argument('--input', help='Path to image or video. Skip to capture frames from camera')
  8. parser.add_argument('--prototxt', help='Path to deploy.prototxt', required=True)
  9. parser.add_argument('--caffemodel', help='Path to hed_pretrained_bsds.caffemodel', required=True)
  10. parser.add_argument('--width', help='Resize input image to a specific width', default=500, type=int)
  11. parser.add_argument('--height', help='Resize input image to a specific height', default=500, type=int)
  12. args = parser.parse_args()
  13. #! [CropLayer]
  14. class CropLayer(object):
  15. def __init__(self, params, blobs):
  16. self.xstart = 0
  17. self.xend = 0
  18. self.ystart = 0
  19. self.yend = 0
  20. # Our layer receives two inputs. We need to crop the first input blob
  21. # to match a shape of the second one (keeping batch size and number of channels)
  22. def getMemoryShapes(self, inputs):
  23. inputShape, targetShape = inputs[0], inputs[1]
  24. batchSize, numChannels = inputShape[0], inputShape[1]
  25. height, width = targetShape[2], targetShape[3]
  26. self.ystart = (inputShape[2] - targetShape[2]) // 2
  27. self.xstart = (inputShape[3] - targetShape[3]) // 2
  28. self.yend = self.ystart + height
  29. self.xend = self.xstart + width
  30. return [[batchSize, numChannels, height, width]]
  31. def forward(self, inputs):
  32. return [inputs[0][:,:,self.ystart:self.yend,self.xstart:self.xend]]
  33. #! [CropLayer]
  34. #! [Register]
  35. cv.dnn_registerLayer('Crop', CropLayer)
  36. #! [Register]
  37. # Load the model.
  38. net = cv.dnn.readNet(cv.samples.findFile(args.prototxt), cv.samples.findFile(args.caffemodel))
  39. kWinName = 'Holistically-Nested Edge Detection'
  40. cv.namedWindow('Input', cv.WINDOW_NORMAL)
  41. cv.namedWindow(kWinName, cv.WINDOW_NORMAL)
  42. cap = cv.VideoCapture(args.input if args.input else 0)
  43. while cv.waitKey(1) < 0:
  44. hasFrame, frame = cap.read()
  45. if not hasFrame:
  46. cv.waitKey()
  47. break
  48. cv.imshow('Input', frame)
  49. inp = cv.dnn.blobFromImage(frame, scalefactor=1.0, size=(args.width, args.height),
  50. mean=(104.00698793, 116.66876762, 122.67891434),
  51. swapRB=False, crop=False)
  52. net.setInput(inp)
  53. out = net.forward()
  54. out = out[0, 0]
  55. out = cv.resize(out, (frame.shape[1], frame.shape[0]))
  56. cv.imshow(kWinName, out)