01_camera.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env python
  2. #version : 2024.03.14
  3. #language : ch
  4. #camera : CR5205
  5. from maix import camera
  6. from maix import display
  7. from maix import image #引入python模块包
  8. import time
  9. import sys
  10. sys.path.append('/root/')
  11. from CocoPi import BUTTON
  12. import os
  13. ScreenOrientation = False
  14. try:
  15. if os.path.exists("/etc/cameraSize.cfg"):
  16. cameraSize = True
  17. else:
  18. cameraSize = False
  19. except:
  20. cameraSize = False
  21. def getLcdRotation(cameraCapture):
  22. global cameraSize
  23. if cameraSize:
  24. return lcdRotationNew(cameraCapture)
  25. else:
  26. return lcdRotation(cameraCapture)
  27. def lcdRotationNew(inputImg):
  28. global cameraSize,ScreenOrientation
  29. imageRotationBuffer = inputImg.crop(0, 0, 320, 240)
  30. if ScreenOrientation:
  31. imgRotationAim = image.new(size = (240, 320))
  32. rotationAngle = 90
  33. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  34. else:
  35. imgRotationAim = image.new(size = (320, 240))
  36. GETROTATION = imageRotationBuffer
  37. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  38. return GETROTATION
  39. def lcdRotation(inputImg):
  40. global cameraSize,ScreenOrientation
  41. imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
  42. if ScreenOrientation:
  43. imgRotationAim = image.new(size = (240, 320))
  44. rotationAngle = 180
  45. else:
  46. imgRotationAim = image.new(size = (320, 240))
  47. rotationAngle = 90
  48. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  49. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  50. return GETROTATION
  51. _canvas_y,_canvas_x = 0,0
  52. def v831_display_show_canvas(displayShow):
  53. global _canvas_y,_canvas_x,ScreenOrientation,cameraSize
  54. if ScreenOrientation:
  55. displayShowCanvas = image.new(size = (240, 320))
  56. displayShowCanvas.draw_rectangle(0,0,240,320, color=(0,0,0), thickness=-1)
  57. displayShowCanvas.draw_image(displayShow,_canvas_x,_canvas_y,alpha=1)
  58. displayShowVER = displayShowCanvas.crop(0,0,240,320)
  59. displayShowVER = displayShowVER.rotate(-90, adjust=1)
  60. display.show(displayShowVER)
  61. else:
  62. displayShowCanvas = image.new(size = (320, 240))
  63. displayShowCanvas.draw_rectangle(0,0,320,240, color=(0,0,0), thickness=-1)
  64. displayShowCanvas.draw_image(displayShow,_canvas_x,_canvas_y,alpha=1)
  65. display.show(displayShowCanvas)
  66. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  67. buttonState=0
  68. newState=0
  69. oldState=0
  70. def buttonDetect():
  71. global buttonState,newState,oldState
  72. newState=key_C.is_pressed()
  73. if newState == True and oldState == False:
  74. buttonState=1
  75. elif newState == False and oldState == True:
  76. buttonState=2
  77. else:
  78. buttonState=0
  79. oldState=newState
  80. def timeStampConvert():
  81. timeListOutput=""
  82. timeListOutput = str(time.localtime().tm_year) + \
  83. str(time.localtime().tm_mon)+str(time.localtime().tm_mday) + \
  84. str(time.localtime().tm_hour)+str(time.localtime().tm_min) + \
  85. str(time.localtime().tm_sec)
  86. return timeListOutput
  87. key_C = BUTTON(13)
  88. if cameraSize==True:
  89. camera.camera.config(size=(320,240))
  90. else:
  91. camera.camera.config(size=(240,320))
  92. while True:
  93. buttonDetect()
  94. savePathHead="/root/user/img/photo_"
  95. savePathTail=".jpg"
  96. if buttonState!=2:
  97. img_backdrop = image.new(size=(320,240),color=(0, 0, 0)) #创建背景画布
  98. camera_img = getLcdRotation(camera.capture()) #从摄像头中获取一张图像
  99. #camera_img_capture = camera_img.crop(0, 0, 320, 240) #截取图像
  100. img_backdrop.draw_image(camera_img)
  101. img_backdrop.draw_image((image.open("/root/preset/img/camera_bfbfbf_24x24.png")).rotate(0, adjust=0),292,2,alpha=1)
  102. img_backdrop.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  103. v831_display_show_canvas(img_backdrop) #将图像显示出来
  104. else:
  105. img_backdrop = image.new(size=(320,240),color=(0, 0, 0)) #创建背景画布
  106. camera_img = getLcdRotation(camera.capture()) #从摄像头中获取一张图像
  107. camera_img_capture = camera_img.crop(0, 0, 320, 240) #截取图像
  108. # camera_img_rotate=camera_img_capture.rotate(90,adjust=1)
  109. camera_img_capture.save(savePathHead+timeStampConvert()+savePathTail)
  110. img_backdrop.draw_image(camera_img_capture.resize(280, 210),20,15)
  111. img_backdrop.draw_image((image.open("/root/preset/img/camera_bfbfbf_24x24.png")).rotate(0, adjust=0),292,2,alpha=1)
  112. img_backdrop.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  113. v831_display_show_canvas(img_backdrop) #将图像显示出来
  114. time.sleep(0.5)