26_internetCamera.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/env python
  2. #version : 2023.12.31
  3. #language : pi
  4. from maix import image #引入python模块包
  5. from maix import camera, mjpg, utils, display
  6. import time
  7. import qrcode
  8. import sys
  9. import os
  10. import socket
  11. sys.path.append('/root/')
  12. from CocoPi import BUTTON
  13. import os
  14. ScreenOrientation = False
  15. try:
  16. if os.path.exists("/etc/cameraSize.cfg"):
  17. cameraSize = True
  18. else:
  19. cameraSize = False
  20. except:
  21. cameraSize = False
  22. def getLcdRotation(cameraCapture):
  23. global cameraSize
  24. if cameraSize:
  25. return lcdRotationNew(cameraCapture)
  26. else:
  27. return lcdRotation(cameraCapture)
  28. def lcdRotationNew(inputImg):
  29. global cameraSize,ScreenOrientation
  30. imageRotationBuffer = inputImg.crop(0, 0, 320, 240)
  31. if ScreenOrientation:
  32. imgRotationAim = image.new(size = (240, 320))
  33. rotationAngle = 90
  34. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  35. else:
  36. imgRotationAim = image.new(size = (320, 240))
  37. GETROTATION = imageRotationBuffer
  38. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  39. return GETROTATION
  40. def lcdRotation(inputImg):
  41. global cameraSize,ScreenOrientation
  42. imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
  43. if ScreenOrientation:
  44. imgRotationAim = image.new(size = (240, 320))
  45. rotationAngle = 180
  46. else:
  47. imgRotationAim = image.new(size = (320, 240))
  48. rotationAngle = 90
  49. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  50. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  51. return GETROTATION
  52. def getWifiConnectState():
  53. wifiConnectState=False
  54. cmd = "wifi_get_connection_info_test 1"
  55. res = os.popen(cmd).read()
  56. if res.find('get connection infomation successfully!') != -1:
  57. wifiConnectState = True
  58. else:
  59. wifiConnectState = False
  60. return wifiConnectState
  61. def getPrivateIp():
  62. st = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
  63. try:
  64. st.connect(("10.255.255.255",1))
  65. IP = st.getsockname()[0]
  66. except Exception:
  67. IP = "127.0.0.1"
  68. finally:
  69. st.close()
  70. return IP
  71. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  72. PrivateIP=""
  73. canvas = image.new(size=(320, 240),color = (15,21,46),mode = "RGB")
  74. canvas.draw_string(8 , 100 , "检查网络连接状态...", scale = 1, color = (255,255,255), thickness = 1)
  75. display.show(canvas)
  76. wirelessTransmitPath=" "
  77. try:
  78. if getWifiConnectState()==True:
  79. PrivateIP=getPrivateIp()
  80. pathHead="http://"
  81. pathTail=":18811"
  82. wirelessTransmitPath=pathHead+PrivateIP+pathTail
  83. else:
  84. PrivateIP="N/A"
  85. wirelessTransmitPath="请先连接网络"
  86. except:
  87. wirelessTransmitPath="请先连接网络"
  88. pass
  89. os.system("rm /root/user/img/wirelessPictureTransmit.jpg")
  90. qrcode.make(wirelessTransmitPath).save("/root/user/img/wirelessPictureTransmit.jpg")
  91. key_A = BUTTON(14)
  92. key_B = BUTTON(8)
  93. key_C = BUTTON(13)
  94. key_D = BUTTON(7)
  95. buttonState=0
  96. newState=0
  97. oldState=0
  98. def buttonDetect():
  99. global buttonState,newState,oldState
  100. newState=key_C.is_pressed()
  101. if newState == True and oldState == False:
  102. buttonState=1
  103. elif newState == False and oldState == True:
  104. buttonState=2
  105. else:
  106. buttonState=0
  107. oldState=newState
  108. queue = mjpg.Queue(maxsize=8)
  109. mjpg.MjpgServerThread(
  110. "0.0.0.0", 18811, mjpg.BytesImageHandlerFactory(q=queue)).start()
  111. if cameraSize==True:
  112. camera.camera.config(size=(320,240))
  113. else:
  114. camera.camera.config(size=(240,320))
  115. qrShowState=0
  116. while True:
  117. buttonDetect()
  118. img_backdrop = image.new(size=(320,240),color=(0, 0, 0)) #创建背景画布
  119. camera_img = getLcdRotation(camera.capture()) #从摄像头中获取一张图像
  120. camera_img_capture = camera_img.crop(0, 0, 320, 240) #截取图像
  121. jpg = utils.rgb2jpg(camera_img.convert("RGB").tobytes(), 320, 240)
  122. queue.put(mjpg.BytesImage(jpg))
  123. img_backdrop.draw_image(camera_img)
  124. if buttonState==2:
  125. qrShowState=1-qrShowState
  126. if qrShowState==1:
  127. img_backdrop.draw_image(image.open("/root/user/img/wirelessPictureTransmit.jpg").resize(260,260), 30, -10,alpha=0.8)
  128. img_backdrop.draw_image((image.open("/root/preset/img/qrcode_3c83f0_24x24.png")).rotate(0, adjust=0),288,6,alpha=1)
  129. img_backdrop.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  130. display.show(img_backdrop) #将图像显示出来