From 83fa30f9390175ba5c88a525555f9bc00d8a1c73 Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Sun, 15 May 2022 20:02:34 -0700 Subject: [PATCH] Save frame every two seconds --- .gitignore | 1 + headless.py | 6 +++- radio.py | 86 ----------------------------------------------------- 3 files changed, 6 insertions(+), 87 deletions(-) delete mode 100644 radio.py diff --git a/.gitignore b/.gitignore index 7b8aafb..8f5dccf 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ __pycache__/ *.py[cod] *$py.class +frame.png # C extensions *.so diff --git a/headless.py b/headless.py index 78f941f..cef808a 100644 --- a/headless.py +++ b/headless.py @@ -10,6 +10,7 @@ DENSITY = 4 RED = [0, 0, 255] N_COLORS = 3 LAST_MESSAGE_TIME = 0 +LAST_SNAPSHOT_TIME = 0 def analyze_block(frame, osc, index, sp, ep, send=False): @@ -29,7 +30,7 @@ def analyze_block(frame, osc, index, sp, ep, send=False): def analyze(frame, osc): """ Analyze the full frame """ - global LAST_MESSAGE_TIME + global LAST_MESSAGE_TIME, LAST_SNAPSHOT_TIME height, width, d = frame.shape n = DENSITY m = math.ceil(n * (height / width)) @@ -39,6 +40,9 @@ def analyze(frame, osc): if time.time() - LAST_MESSAGE_TIME > 0.1: LAST_MESSAGE_TIME = time.time() send = True + if time.time() - LAST_SNAPSHOT_TIME > 2: + LAST_SNAPSHOT_TIME = time.time() + cv2.imwrite("frame.png", frame) 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)) diff --git a/radio.py b/radio.py deleted file mode 100644 index 523a2ea..0000000 --- a/radio.py +++ /dev/null @@ -1,86 +0,0 @@ -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 draw_rectangle(frame, sp, ep): - """ Draw a rectangle on the frame """ - return cv2.rectangle(frame, sp, ep, RED, 1) - - -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: - print("Sending message", h, s, v) - osc.send_message( - "/radio", - [int(index), float(h), - float(s), float(v), .5]) - - # Draw a blob of the average color - sp2 = tuple(int(x) for x in (2 * np.array(sp) + np.array(ep)) / 3) - ep2 = tuple(int(x) for x in (np.array(sp) + 2 * np.array(ep)) / 3) - frame = cv2.rectangle(frame, sp2, ep2, - [int(x * 255) for x in average_color], -1) - - # Draw the image - for thickness, color in ((3, (0, 0, 0)), (1, (255, 255, 255))): - frame = cv2.putText(frame, - text=f"{h:.2f} {s:.2f} {v:.2f}", - org=(sp[0] + 10, sp[1] + 20), - fontScale=.5, - color=color, - fontFace=2, - thickness=thickness) - 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() - 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 = draw_rectangle(frame, sp, ep) - frame = analyze_block(frame, osc, index, sp, ep, True) - return frame - - -if __name__ == '__main__': - camera = cv2.VideoCapture("/dev/video2") - # camera = cv2.VideoCapture(0) - osc = udp_client.SimpleUDPClient("0.0.0.0", 5005) - - while True: - ret, frame = camera.read() - frame = analyze(frame, osc) - cv2.imshow('Input', frame) - - c = cv2.waitKey(1) - if c == 27: - break - - camera.release() - cv2.destroyAllWindows()