  
【项目背景】 
”流光溢彩灯“有非常炫酷的效果,它可以根据不同的画面和内容,配备在显示器后方的墙壁上投射出不同颜色的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([[1,2,3],[2,3,4],[3,4,5],[4,5,6]])
 - >>> now2 = np.mat(num1)
 - >>> now2
 
  复制代码 matrix([[1, 2, 3], 
        [2, 3, 4], 
        [3, 4, 5], 
        [4, 5, 6]]) 
 
- >>> 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[0:30,0:30].mean(axis=(0,1))
 -                 print(color)
 - 
 -                 np1.range_color(0,0,int(color[2])*65536+int(color[1])*256+int(color[0]))    
 -             cv2.imshow('windows', img)
 -             cv2.imshow('windows1', img[0:30,0:30])
 -         else:
 -             cv2.destroyAllWindows()
 -             vd.set(cv2.CAP_PROP_POS_MSEC, 0)   
 -             
 -             bs=True
 -     if cv2.waitKey(1) & 0xff== 27:
 -             break
 - 
 - 
 - 
 
  复制代码
  
【行空板流光溢彩】 
  
 
  
- 
 - 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[120:220,0:10].mean(axis=(0,1))
 -                 np1.range_color(9,14,int(color[2])*65536+int(color[1])*256+int(color[0]))  
 -                 
 -                 color=img[309:319,115:125].mean(axis=(0,1))
 -                 np1.range_color(6,8,int(color[2])*65536+int(color[1])*256+int(color[0]))
 -                 color=img[229:239,155:165].mean(axis=(0,1))
 -                 np1.range_color(1,5,int(color[2])*65536+int(color[1])*256+int(color[0]))    
 -                 color=img[0:10,115:125].mean(axis=(0,1))
 -                 np1.range_color(15,17,int(color[2])*65536+int(color[1])*256+int(color[0]))    
 -                 
 -         else:
 -             cv2.destroyAllWindows()
 -             vd.set(cv2.CAP_PROP_POS_MSEC, 0)   
 -             bf=False
 -             bs=True
 -             np1.clear()
 -       if cv2.waitKey(1) & 0xff== 27:
 -             break
 - 
 - 
 - 
 
  复制代码
  
 
 |