|
@@ -1,4 +1,5 @@ |
|
|
import math |
|
|
import math |
|
|
|
|
|
import time |
|
|
import itertools as it |
|
|
import itertools as it |
|
|
import cv2 |
|
|
import cv2 |
|
|
from pythonosc import udp_client |
|
|
from pythonosc import udp_client |
|
@@ -7,6 +8,7 @@ import numpy as np |
|
|
DENSITY = 4 |
|
|
DENSITY = 4 |
|
|
RED = [0, 0, 255] |
|
|
RED = [0, 0, 255] |
|
|
N_COLORS = 3 |
|
|
N_COLORS = 3 |
|
|
|
|
|
LAST_MESSAGE_TIME = 0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def draw_rectangle(frame, sp, ep): |
|
|
def draw_rectangle(frame, sp, ep): |
|
@@ -14,7 +16,7 @@ def draw_rectangle(frame, sp, ep): |
|
|
return cv2.rectangle(frame, sp, ep, RED, 1) |
|
|
return cv2.rectangle(frame, sp, ep, RED, 1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def analyze_block(frame, osc, index, sp, ep): |
|
|
|
|
|
|
|
|
def analyze_block(frame, osc, index, sp, ep, send=False): |
|
|
""" Analyze a block """ |
|
|
""" Analyze a block """ |
|
|
block = frame[sp[1]:ep[1], sp[0]:ep[0], 0:3] |
|
|
block = frame[sp[1]:ep[1], sp[0]:ep[0], 0:3] |
|
|
average_color = np.average(block, (0, 1)) |
|
|
average_color = np.average(block, (0, 1)) |
|
@@ -26,13 +28,20 @@ def analyze_block(frame, osc, index, sp, ep): |
|
|
h, s, v = cv2.cvtColor(average_color, cv2.COLOR_BGR2HSV)[0][0] |
|
|
h, s, v = cv2.cvtColor(average_color, cv2.COLOR_BGR2HSV)[0][0] |
|
|
|
|
|
|
|
|
# Configure the oscillator |
|
|
# Configure the oscillator |
|
|
osc.send_message( |
|
|
|
|
|
"/radio", |
|
|
|
|
|
[int(index), |
|
|
|
|
|
float(h / 255), |
|
|
|
|
|
float(s / 255), |
|
|
|
|
|
float(v / 255), .5]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if send and index == 0: |
|
|
|
|
|
print("Sending message") |
|
|
|
|
|
osc.send_message( |
|
|
|
|
|
"/radio", |
|
|
|
|
|
[int(index), |
|
|
|
|
|
float(h / 255), |
|
|
|
|
|
float(1), |
|
|
|
|
|
float(v / 255), .5]) |
|
|
|
|
|
# osc.send_message( |
|
|
|
|
|
# "/radio", |
|
|
|
|
|
# [int(index), |
|
|
|
|
|
# float(h / 255), |
|
|
|
|
|
# float(s / 255), |
|
|
|
|
|
# float(v / 255), .5]) |
|
|
# Draw the image |
|
|
# Draw the image |
|
|
for thickness, color in ((3, (0, 0, 0)), (1, (255, 255, 255))): |
|
|
for thickness, color in ((3, (0, 0, 0)), (1, (255, 255, 255))): |
|
|
frame = cv2.putText(frame, |
|
|
frame = cv2.putText(frame, |
|
@@ -47,16 +56,21 @@ def analyze_block(frame, osc, index, sp, ep): |
|
|
|
|
|
|
|
|
def analyze(frame, osc): |
|
|
def analyze(frame, osc): |
|
|
""" Analyze the full frame """ |
|
|
""" Analyze the full frame """ |
|
|
|
|
|
global LAST_MESSAGE_TIME |
|
|
height, width, d = frame.shape |
|
|
height, width, d = frame.shape |
|
|
n = DENSITY |
|
|
n = DENSITY |
|
|
m = math.ceil(n * (height / width)) |
|
|
m = math.ceil(n * (height / width)) |
|
|
dx = width / n |
|
|
dx = width / n |
|
|
dy = height / m |
|
|
dy = height / m |
|
|
|
|
|
send = False |
|
|
|
|
|
if time.time() - LAST_MESSAGE_TIME > 0.1: |
|
|
|
|
|
LAST_MESSAGE_TIME = time.time() |
|
|
|
|
|
send = True |
|
|
for index, (x, y) in enumerate(it.product(range(n), range(m))): |
|
|
for index, (x, y) in enumerate(it.product(range(n), range(m))): |
|
|
sp = (int(x * dx), int(y * dy)) |
|
|
sp = (int(x * dx), int(y * dy)) |
|
|
ep = (int(x * dx + dx), int(y * dy + dy)) |
|
|
ep = (int(x * dx + dx), int(y * dy + dy)) |
|
|
frame = draw_rectangle(frame, sp, ep) |
|
|
frame = draw_rectangle(frame, sp, ep) |
|
|
frame = analyze_block(frame, osc, index, sp, ep) |
|
|
|
|
|
|
|
|
frame = analyze_block(frame, osc, index, sp, ep, send) |
|
|
return frame |
|
|
return frame |
|
|
|
|
|
|
|
|
|
|
|
|
|
|