18_edgeDetection.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python
  2. #version : 2023.04.04
  3. #language : en
  4. from maix import camera
  5. from maix import display
  6. from maix import image
  7. import numpy as np
  8. camera.camera.config(size=(320,240))
  9. ScreenOrientation = False
  10. image.load_freetype("/root/preset/fonts/CascadiaCodePL-Italic.ttf")
  11. class Edge:
  12. model = {
  13. "param": "/root/preset/model/sobel_int8.param",
  14. "bin": "/root/preset/model/sobel_int8.bin"
  15. }
  16. input_size = (224, 224, 3)
  17. output_size = (222, 222, 3)
  18. options = {
  19. "model_type": "awnn",
  20. "inputs": {
  21. "input0": input_size
  22. },
  23. "outputs": {
  24. "output0": output_size
  25. },
  26. "mean": [127.5, 127.5, 127.5],
  27. "norm": [0.0078125, 0.0078125, 0.0078125],
  28. }
  29. def __init__(self):
  30. from maix import nn
  31. print("-- load model:", self.model)
  32. self.model = nn.load(self.model, opt=self.options)
  33. print("-- load ok")
  34. def __del__(self):
  35. del self.model
  36. m = Edge()
  37. def lcdRotation(inputImg,rotationAngle):
  38. from maix import image
  39. imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
  40. if ScreenOrientation:
  41. imgRotationAim = image.new(size = (240, 320))
  42. else:
  43. imgRotationAim = image.new(size = (320, 240))
  44. return imgRotationAim.draw_image(imageRotationBuffer.rotate(rotationAngle, adjust=1),0,0,alpha=1)
  45. if ScreenOrientation:
  46. CAMERAROTATE = +180
  47. else:
  48. CAMERAROTATE = +90
  49. canvas = image.new(size = (320, 240))
  50. while True:
  51. canvas.clear()
  52. img_edgedetection = camera.capture()
  53. img_edgedetection = img_edgedetection.resize(224, 224, padding = 0)
  54. out = m.model.forward(img_edgedetection, quantize=True, layout="hwc")
  55. out = out.astype(np.float32).reshape(m.output_size)
  56. out = (np.ndarray.__abs__(out) * 255 / out.max()).astype(np.uint8)
  57. data = out.tobytes()
  58. edgeModel = img_edgedetection.load(data,(222, 222), mode="RGB")
  59. # canvas = edgeModel
  60. if ScreenOrientation:
  61. canvasVER = canvas.crop(0,0,240,320)
  62. canvasVER = canvasVER.rotate(-90, adjust=1)
  63. display.show(canvasVER)
  64. else:
  65. canvas.draw_image(edgeModel,48,8)
  66. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  67. display.show(canvas)