18_edgeDetection.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python
  2. #version : 2023.12.31
  3. #language : ch
  4. from maix import camera
  5. from maix import display
  6. from maix import image
  7. import numpy as np
  8. import os
  9. ScreenOrientation = False
  10. try:
  11. if os.path.exists("/etc/cameraSize.cfg"):
  12. cameraSize = True
  13. else:
  14. cameraSize = False
  15. except:
  16. cameraSize = False
  17. def getLcdRotation(cameraCapture):
  18. global cameraSize
  19. if cameraSize:
  20. return lcdRotationNew(cameraCapture)
  21. else:
  22. return lcdRotation(cameraCapture)
  23. def lcdRotationNew(inputImg):
  24. global cameraSize,ScreenOrientation
  25. imageRotationBuffer = inputImg.crop(0, 0, 320, 240)
  26. if ScreenOrientation:
  27. imgRotationAim = image.new(size = (240, 320))
  28. rotationAngle = 90
  29. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  30. else:
  31. imgRotationAim = image.new(size = (320, 240))
  32. GETROTATION = imageRotationBuffer
  33. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  34. return GETROTATION
  35. def lcdRotation(inputImg):
  36. global cameraSize,ScreenOrientation
  37. imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
  38. if ScreenOrientation:
  39. imgRotationAim = image.new(size = (240, 320))
  40. rotationAngle = 180
  41. else:
  42. imgRotationAim = image.new(size = (320, 240))
  43. rotationAngle = 90
  44. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  45. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  46. return GETROTATION
  47. if cameraSize==True:
  48. camera.camera.config(size=(320,240))
  49. else:
  50. camera.camera.config(size=(240,320))
  51. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  52. class Edge:
  53. model = {
  54. "param": "/root/preset/model/sobel_int8.param",
  55. "bin": "/root/preset/model/sobel_int8.bin"
  56. }
  57. input_size = (224, 224, 3)
  58. output_size = (222, 222, 3)
  59. options = {
  60. "model_type": "awnn",
  61. "inputs": {
  62. "input0": input_size
  63. },
  64. "outputs": {
  65. "output0": output_size
  66. },
  67. "mean": [127.5, 127.5, 127.5],
  68. "norm": [0.0078125, 0.0078125, 0.0078125],
  69. }
  70. def __init__(self):
  71. from maix import nn
  72. print("-- load model:", self.model)
  73. self.model = nn.load(self.model, opt=self.options)
  74. print("-- load ok")
  75. def __del__(self):
  76. del self.model
  77. m = Edge()
  78. canvas = image.new(size = (320, 240))
  79. while True:
  80. canvas.clear()
  81. img_edgedetection = getLcdRotation(camera.capture())
  82. img_edgedetection = img_edgedetection.resize(224, 224, padding = 0)
  83. out = m.model.forward(img_edgedetection, quantize=True, layout="hwc")
  84. out = out.astype(np.float32).reshape(m.output_size)
  85. out = (np.ndarray.__abs__(out) * 255 / out.max()).astype(np.uint8)
  86. data = out.tobytes()
  87. edgeModel = img_edgedetection.load(data,(222, 222), mode="RGB")
  88. # canvas = edgeModel
  89. if ScreenOrientation:
  90. canvasVER = canvas.crop(0,0,240,320)
  91. canvasVER = canvasVER.rotate(-90, adjust=1)
  92. display.show(canvasVER)
  93. else:
  94. canvas.draw_image(edgeModel,48,8)
  95. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  96. display.show(canvas)