|
- import math
- import time
- import itertools as it
- import cv2
- import colorsys
- from pythonosc import udp_client
- import numpy as np
-
- DENSITY = 4
- RED = [0, 0, 255]
- N_COLORS = 3
- LAST_MESSAGE_TIME = 0
-
-
- def analyze_block(frame, osc, index, sp, ep, send=False):
- """ Analyze a block """
- block = frame[sp[1]:ep[1], sp[0]:ep[0], 0:3]
- average_color = np.average(block, (0, 1)) / 255
- h, s, v = colorsys.rgb_to_hsv(*average_color)
-
- # Configure the oscillator
- if send:
- osc.send_message(
- "/radio",
- [int(index), float(h),
- float(s), float(v), .5])
- return frame
-
-
- def analyze(frame, osc):
- """ Analyze the full frame """
- global LAST_MESSAGE_TIME
- height, width, d = frame.shape
- n = DENSITY
- m = math.ceil(n * (height / width))
- dx = width / n
- dy = height / m
- send = False
- if time.time() - LAST_MESSAGE_TIME > 0.1:
- LAST_MESSAGE_TIME = time.time()
- send = True
- for index, (y, x) in enumerate(it.product(range(m), range(n))):
- sp = (int(x * dx), int(y * dy))
- ep = (int(x * dx + dx), int(y * dy + dy))
- frame = analyze_block(frame, osc, index, sp, ep, send)
- return frame
-
-
- if __name__ == '__main__':
- camera = cv2.VideoCapture("/dev/video2")
- #camera = cv2.VideoCapture(0)
- osc = udp_client.SimpleUDPClient("0.0.0.0", 5005)
- print("Radio is running...")
-
- while True:
- ret, frame = camera.read()
- frame = analyze(frame, osc)
-
- camera.release()
|