云天 发表于 2022-9-14 11:56:21

行空板——流光溢彩


【项目背景】
”流光溢彩灯“有非常炫酷的效果,它可以根据不同的画面和内容,配备在显示器后方的墙壁上投射出不同颜色的LED灯光。这个行空板——流光溢彩灯能够通过Python识别当前视频相应区域RGB颜色,自动调节出与当前屏幕画面相近的灯效,将屏幕上的画面延伸到屏幕之外,营造出更大的让人身临其境的视觉氛围。

【项目技术】
1、cv2播放视频
2、使用到了列表求行列平均值mean(计算视频区域颜色平均值)
3、Pinpong库控制LED灯带
【知识准备】
mean()函数功能:求取均值
经常操作的参数为axis,以m * n矩阵举例:
axis 不设置值,对 m*n 个数求均值,返回一个实数
axis = 0:压缩行,对各列求均值,返回 1* n 矩阵
axis =1 :压缩列,对各行求均值,返回 m *1 矩阵
举例:
>>>import numpy as np
>>> num1 = np.array([,,,])
>>> now2 = np.mat(num1)
>>> now2matrix([,
      ,
      ,
      ])

>>> np.mean(now2) # 对所有元素求均值3.5
>>> np.mean(now2,0) # 压缩行,对各列求均值matrix([[ 2.5,3.5,4.5]])
>>> np.mean(now2,1) # 压缩列,对各行求均值matrix([[ 2.],
      [ 3.],
      [ 4.],
      [ 5.]])

【测试操作】
首先使用笔记本电脑,在Mind+的Python模式下,OpenCV播放视频,使用Pinpong库连接UNO控制RGB灯。
1.播放视频



import pygame
import time
import cv2

import numpy as np

vd = cv2.VideoCapture()
vd.open("red.mp4")


vd.set(cv2.CAP_PROP_BUFFERSIZE, 1)   #设置OpenCV内部的图像缓存,可以极大提高图像的实时性。
pygame.mixer.init()
pygame.mixer.music.load("red.mp3")
pygame.mixer.music.set_volume(50 / 100)
currenttime=time.time()
bs=True

while True:


if bs==True:
         bs=False
         pygame.mixer.music.play()   
if time.time()-currenttime>=0.039:
      currenttime=time.time()
      ret, img = vd.read()
      if ret:

            cv2.imshow('windows', img)

      else:
            cv2.destroyAllWindows()
            vd.set(cv2.CAP_PROP_POS_MSEC, 0)   

            bs=True
if cv2.waitKey(1) & 0xff== 27:
            break



2、根据识别视频相应区域颜色,控制RGB灯


import pygame
import time
import cv2

from pinpong.board import Board,Pin
from pinpong.board import NeoPixel
import numpy as np

bs=True

vd = cv2.VideoCapture()
vd.open("red.mp4")
screen_rotation = True
vd.set(cv2.CAP_PROP_FRAME_WIDTH, 320)#设置视频图像宽度
vd.set(cv2.CAP_PROP_FRAME_HEIGHT, 240) #设置视频摄像头图像高度
vd.set(cv2.CAP_PROP_BUFFERSIZE, 1)   #设置OpenCV内部的图像缓存,可以极大提高图像的实时性。
pygame.mixer.init()
pygame.mixer.music.load("red.mp3")
pygame.mixer.music.set_volume(50 / 100)
currenttime=time.time()
currenttime1=time.time()
Board("UNO").begin()
p_p22_out=Pin(Pin.D8, Pin.OUT)
np1 = NeoPixel(p_p22_out,1)

while True:


    if bs==True:
          bs=False

          pygame.mixer.music.play()   
    if time.time()-currenttime>=0.039:
      currenttime=time.time()
      ret, img = vd.read()
      if ret:
   
            if screen_rotation:
      
                pass
            if time.time()-currenttime1>=0.2:
                currenttime1=time.time()
                color=img.mean(axis=(0,1))
                print(color)

                np1.range_color(0,0,int(color)*65536+int(color)*256+int(color))   
            cv2.imshow('windows', img)
            cv2.imshow('windows1', img)
      else:
            cv2.destroyAllWindows()
            vd.set(cv2.CAP_PROP_POS_MSEC, 0)   
            
            bs=True
    if cv2.waitKey(1) & 0xff== 27:
            break



https://www.bilibili.com/video/BV1hT411M796?share_source=copy_web
【行空板流光溢彩】




import pygame
import time
import cv2
from unihiker import GUI
from pinpong.board import Board,Pin
from pinpong.board import NeoPixel

bf=False
bs=True
# 事件回调函数
def button_click1():
    global bf
    bf=True

Board().begin()
u_gui=GUI()
屏幕=u_gui.draw_image(image="back.JPG",x=0,y=0)
按钮=u_gui.draw_image(image="an.png",x=90,y=130)
按钮.config(onclick=button_click1)

vd = cv2.VideoCapture()
vd.open("red.mp4")
screen_rotation = True
vd.set(cv2.CAP_PROP_FRAME_WIDTH, 320)#设置视频图像宽度
vd.set(cv2.CAP_PROP_FRAME_HEIGHT, 240) #设置视频摄像头图像高度
vd.set(cv2.CAP_PROP_BUFFERSIZE, 1)   #设置OpenCV内部的图像缓存,可以极大提高图像的实时性。
pygame.mixer.init()
pygame.mixer.music.load("red.mp3")
pygame.mixer.music.set_volume(50 / 100)
p_p22_out=Pin(Pin.P22, Pin.OUT)
np1 = NeoPixel(p_p22_out,18)
np1.clear()
currenttime=time.time()
currenttime1=time.time()

while True:
   if bf==True:
      if bs==True:
          bs=False
          cv2.namedWindow('windows',cv2.WND_PROP_FULLSCREEN)    #窗口全屏
          cv2.setWindowProperty('windows', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)   #窗口全屏
          pygame.mixer.music.play()   
      if time.time()-currenttime>=0.039:
      currenttime=time.time()
      ret, img = vd.read()
      if ret:
            
            if screen_rotation:
                img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE) #旋转屏幕
            cv2.imshow('windows', img)
            if time.time()-currenttime1>=0.4:
                currenttime1=time.time()
                color=img.mean(axis=(0,1))
                np1.range_color(9,14,int(color)*65536+int(color)*256+int(color))
               
                color=img.mean(axis=(0,1))
                np1.range_color(6,8,int(color)*65536+int(color)*256+int(color))
                color=img.mean(axis=(0,1))
                np1.range_color(1,5,int(color)*65536+int(color)*256+int(color))   
                color=img.mean(axis=(0,1))
                np1.range_color(15,17,int(color)*65536+int(color)*256+int(color))   
               
      else:
            cv2.destroyAllWindows()
            vd.set(cv2.CAP_PROP_POS_MSEC, 0)   
            bf=False
            bs=True
            np1.clear()
      if cv2.waitKey(1) & 0xff== 27:
            break




https://www.bilibili.com/video/BV1Qe411T7BD?share_source=copy_web

安卓机器人 发表于 2022-9-14 16:18:06

炫 幻 彩

云天 发表于 2022-9-14 19:27:13

如果我能再打印一个外壳就更好了
页: [1]
查看完整版本: 行空板——流光溢彩