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.

74 lines
2.2KB

  1. import math
  2. import itertools as it
  3. import cv2
  4. from pythonosc import udp_client
  5. import numpy as np
  6. DENSITY = 4
  7. RED = [0, 0, 255]
  8. N_COLORS = 3
  9. def draw_rectangle(frame, sp, ep):
  10. """ Draw a rectangle on the frame """
  11. return cv2.rectangle(frame, sp, ep, RED, 1)
  12. def analyze_block(frame, osc, index, sp, ep):
  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))
  16. sp2 = tuple(int(x) for x in (2 * np.array(sp) + np.array(ep)) / 3)
  17. ep2 = tuple(int(x) for x in (np.array(sp) + 2 * np.array(ep)) / 3)
  18. average_color = [int(x) for x in average_color]
  19. frame = cv2.rectangle(frame, sp2, ep2, average_color, -1)
  20. average_color = np.uint8([[average_color]])
  21. h, s, v = cv2.cvtColor(average_color, cv2.COLOR_BGR2HSV)[0][0]
  22. # Configure the oscillator
  23. # osc.send_message("block/update", [int(x) for x in (index, h, s, v)])
  24. osc.send_message("/goodbye", 1)
  25. # Draw the image
  26. for thickness, color in ((3, (0, 0, 0)), (1, (255, 255, 255))):
  27. frame = cv2.putText(frame,
  28. text=f"{h:03d} {s:03d} {v:03d}",
  29. org=(sp[0] + 10, sp[1] + 20),
  30. fontScale=.5,
  31. color=color,
  32. fontFace=2,
  33. thickness=thickness)
  34. return frame
  35. def analyze(frame, osc):
  36. """ Analyze the full frame """
  37. height, width, d = frame.shape
  38. n = DENSITY
  39. m = math.ceil(n * (height / width))
  40. dx = width / n
  41. dy = height / m
  42. for index, (x, y) in enumerate(it.product(range(n), range(m))):
  43. sp = (int(x * dx), int(y * dy))
  44. ep = (int(x * dx + dx), int(y * dy + dy))
  45. frame = draw_rectangle(frame, sp, ep)
  46. frame = analyze_block(frame, osc, index, sp, ep)
  47. return frame
  48. if __name__ == '__main__':
  49. camera = cv2.VideoCapture(0)
  50. osc = udp_client.SimpleUDPClient("0.0.0.0", 5005)
  51. while True:
  52. ret, frame = camera.read()
  53. frame = analyze(frame, osc)
  54. cv2.imshow('Input', frame)
  55. c = cv2.waitKey(1)
  56. if c == 27:
  57. break
  58. camera.release()
  59. cv2.destroyAllWindows()