Always-on computer music
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.5KB

  1. import math
  2. import time
  3. import itertools as it
  4. import cv2
  5. import colorsys
  6. from pythonosc import udp_client
  7. import numpy as np
  8. DENSITY = 4
  9. RED = [0, 0, 255]
  10. N_COLORS = 3
  11. LAST_MESSAGE_TIME = 0
  12. def analyze_block(frame, osc, index, sp, ep, send=False):
  13. """ Analyze a block """
  14. block = frame[sp[1]:ep[1], sp[0]:ep[0], 0:3]
  15. average_color = np.average(block, (0, 1)) / 255
  16. h, s, v = colorsys.rgb_to_hsv(*average_color)
  17. # Configure the oscillator
  18. if send:
  19. osc.send_message(
  20. "/radio",
  21. [int(index), float(h),
  22. float(s), float(v), .5])
  23. return frame
  24. def analyze(frame, osc):
  25. """ Analyze the full frame """
  26. global LAST_MESSAGE_TIME
  27. height, width, d = frame.shape
  28. n = DENSITY
  29. m = math.ceil(n * (height / width))
  30. dx = width / n
  31. dy = height / m
  32. send = False
  33. if time.time() - LAST_MESSAGE_TIME > 0.1:
  34. LAST_MESSAGE_TIME = time.time()
  35. send = True
  36. for index, (y, x) in enumerate(it.product(range(m), range(n))):
  37. sp = (int(x * dx), int(y * dy))
  38. ep = (int(x * dx + dx), int(y * dy + dy))
  39. frame = analyze_block(frame, osc, index, sp, ep, send)
  40. return frame
  41. if __name__ == '__main__':
  42. camera = cv2.VideoCapture("/dev/video2")
  43. #camera = cv2.VideoCapture(0)
  44. osc = udp_client.SimpleUDPClient("0.0.0.0", 5005)
  45. print("Radio is running...")
  46. while True:
  47. ret, frame = camera.read()
  48. frame = analyze(frame, osc)
  49. camera.release()