Advertisement
MateuszGrabarczyk

sumlab2

Oct 23rd, 2023
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. 1:
  2. from gpiozero import LED, Button
  3. from signal import pause
  4. from time import sleep
  5.  
  6. button = Button(6, False)
  7. led1 = LED(13)
  8.  
  9. button.when_pressed = led1.on
  10. button.when_released = led1.off
  11.  
  12. pause()
  13.  
  14. 2 GPIO:
  15. from gpiozero import LED, Button
  16. from signal import pause
  17. from time import sleep
  18.  
  19. button = Button(6, False)
  20. led1 = LED(13)
  21.  
  22. button.when_pressed = led1.on
  23. button.when_released = led1.off
  24.  
  25. pause()
  26.  
  27. 3 I2C:
  28. import smbus2
  29. import bme280
  30.  
  31. port = 1
  32. address = 0x77
  33. bus = smbus2.SMBus(port)
  34.  
  35. calibration_params = bme280.load_calibration_params(bus, address)
  36.  
  37. # the sample method will take a single reading and return a
  38. # compensated_reading object
  39. data = bme280.sample(bus, address, calibration_params)
  40.  
  41. # the compensated_reading class has the following attributes
  42. print(data.id)
  43. print(data.timestamp)
  44. print(data.temperature)
  45. print(data.pressure)
  46. print(data.humidity)
  47.  
  48. # there is a handy string representation too
  49. print(data)
  50.  
  51. 4 i2c-2:
  52. import time
  53. import board
  54. import adafruit_tsl2591
  55.  
  56. # Create sensor object, communicating over the board's default I2C bus
  57. i2c = board.I2C() # uses board.SCL and board.SDA
  58. # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
  59.  
  60. # Initialize the sensor.
  61. sensor = adafruit_tsl2591.TSL2591(i2c)
  62.  
  63. # You can optionally change the gain and integration time:
  64. # sensor.gain = adafruit_tsl2591.GAIN_LOW (1x gain)
  65. # sensor.gain = adafruit_tsl2591.GAIN_MED (25x gain, the default)
  66. # sensor.gain = adafruit_tsl2591.GAIN_HIGH (428x gain)
  67. # sensor.gain = adafruit_tsl2591.GAIN_MAX (9876x gain)
  68. # sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_100MS (100ms, default)
  69. # sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_200MS (200ms)
  70. # sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_300MS (300ms)
  71. # sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_400MS (400ms)
  72. # sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_500MS (500ms)
  73. # sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_600MS (600ms)
  74.  
  75. # Read the total lux, IR, and visible light levels and print it every second.
  76. while True:
  77. # Read and calculate the light level in lux.
  78. lux = sensor.lux
  79. print("Total light: {0}lux".format(lux))
  80. # You can also read the raw infrared and visible light levels.
  81. # These are unsigned, the higher the number the more light of that type.
  82. # There are no units like lux.
  83. # Infrared levels range from 0-65535 (16-bit)
  84. infrared = sensor.infrared
  85. print("Infrared light: {0}".format(infrared))
  86. # Visible-only levels range from 0-2147483647 (32-bit)
  87. visible = sensor.visible
  88. print("Visible light: {0}".format(visible))
  89. # Full spectrum (visible + IR) also range from 0-2147483647 (32-bit)
  90. full_spectrum = sensor.full_spectrum
  91. print("Full spectrum (IR + visible) light: {0}".format(full_spectrum))
  92. time.sleep(1.0)
  93.  
  94.  
  95. 5 camera:
  96. import arducam_mipicamera as arducam
  97. import v4l2
  98. import time
  99. import cv2
  100. import copy
  101.  
  102. def align_down(size, align):
  103. return (size & ~((align)-1))
  104.  
  105. def align_up(size, align):
  106. return align_down(size + align - 1, align)
  107.  
  108. def set_controls(camera):
  109. try:
  110. camera.software_auto_exposure(enable = False)
  111. camera.software_auto_white_balance(enable = True)
  112. camera.set_control(v4l2.V4L2_CID_EXPOSURE, 15000)
  113. camera.set_control(v4l2.V4L2_CID_FOCUS_ABSOLUTE, 150)
  114. camera.manual_set_awb_compensation(20,150)
  115. except Exception as e:
  116. print(e)
  117.  
  118. # TODO: haarcascades file path
  119. xml_file = "/home/rpi/Desktop/labs rpi/haarcascades/haarcascade_frontalface_alt2.xml"
  120.  
  121. bodzenta = cv2.imread("/home/rpi/Desktop/labs rpi/bodzenta.png")
  122.  
  123.  
  124.  
  125.  
  126. classifier = cv2.CascadeClassifier(xml_file)
  127.  
  128.  
  129. camera = arducam.mipi_camera()
  130. camera.init_camera()
  131. fmt = camera.set_resolution(1920, 1080)
  132. print("Current resolution is {}".format(fmt))
  133. set_controls(camera)
  134.  
  135. started = True
  136. while started:
  137. frame = camera.capture(encoding = 'i420')
  138. height = int(align_up(fmt[1], 16))
  139. width = int(align_up(fmt[0], 32))
  140. image = frame.as_array.reshape(int(height * 1.5), width)
  141. image = cv2.cvtColor(image, cv2.COLOR_YUV2BGR_I420)
  142.  
  143. image = cv2.resize(image, (1280, 720))
  144. faces = classifier.detectMultiScale(image)
  145.  
  146. image2 = copy.copy(image)
  147. # TODO: Add rectangle / image drawing
  148. for (x, y, w, h) in faces:
  149. cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
  150. bodzenta2 = cv2.resize(bodzenta, (w,h))
  151. image2[y: y+h, x: x+w] = bodzenta2
  152.  
  153.  
  154. cv2.imshow("Faces", image2)
  155.  
  156. key = cv2.waitKey(1) & 0xFF
  157. if key == ord("q"):
  158. started = False
  159.  
  160. camera.close_camera()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement