虚谷号

 找回密码
 立即注册
搜索
热搜: 活动 交友
查看: 26104|回复: 4236

当Yeelight遇上虚谷号#12 智能变色灯

  [复制链接]

16

主题

40

帖子

161

积分

注册会员

Rank: 2

积分
161
发表于 2019-3-20 10:08:53 | 显示全部楼层 |阅读模式
本帖最后由 linmiaoyan 于 2019-3-30 14:53 编辑

12智能变色灯
在摄像头前放置不同颜色的物体,灯泡自动改变颜色。

摄像头前物体是什么颜色?看起来问题很简单,但是让计算机来回答并不容易。当我们要判断某个物体是什么颜色的时候,往往会先找出这一物体的最主要颜色,再判断这一颜色和哪种颜色最接近。计算机虽然说不出天蓝、橘黄之类的色彩名词,但可以精确地分析出图像中某一个像素点的RGB值,即RGB三种颜色的具体组成,但一个物体往往不会是纯色的,所以要确定一张含有多种颜色的图像的“颜色”,需要确定一个算法。

算法一:将这个图像的所有像素点的RGB值分别相加,取出RGB三种颜色的平均值。如果担心计算的效率太低,可以均匀地取出部分像素,应该可以得到大致的颜色平均值。
简单的取色方法,通过对图像参数的计算得出结果,拥有高准确度。
算法二:利用滤镜功能,给这个图像加上RGB三种颜色遮罩,处理为黑白图片,然后通过计算分别得到图像的白色区域面积大小,换算为0-255之间的数值。
更加类似于机器学习,通过颜色字典的学习,机器能够“智能”的识别主要物体的颜色,在拥有大量数据的情况下,机器的识别能力能够大大提升。

以上两种方法可以相互结合,得到更好的识别效果

import cv2
import time
from yeelight import Bulb

bulb = Bulb("192.168.31.127")
bulb.turn_on()

#得到图片
def get_pic():
    cap=cv2.VideoCapture(0)
    sucess,img=cap.read()
    cv2.imwrite("image.jpg",img)
    cap.release()

#处理图片(裁剪 读取像素点的RGB值并取平均)
def get_color(frame):
    #读取图片像素点的步长,50则为50个像素点读取一次
    step=0
    red=0
    blue=0
    green=0
    print('开始处理')
    high, width, _ = frame.shape
    print('剪裁前行数%d,列数%d' % (high, width))
    # 裁剪坐标为[x0:x1,y0:y1],我们截取图像的中心部分
    frame=frame[int(high/4):int(high*3/4),int(width/4):int(width*3/4)]
    high, width, _ = frame.shape
    print('剪裁后行数%d,列数%d' % (high, width))
    #读取图片中所有像素点的RGB值
    frame1 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    for i in frame1:
        #i中的数据类型是一整行的像素点[[0,0,0][0,0,0]]
        step+=1
        if (step==50):
            for point in i:
            #point中的数据类型是单个像素点
                red+=point[0]
                green+=point[1]
                blue+=point[2]
            times=0
    red=red/len(i)
    green=green/len(i)
    blue=blue/len(i)
    return red,green,blue


if __name__ == '__main__':
    while 1:
        get_pic()
        filename = r"image.jpg"
        frame = cv2.imread(filename)
        r,g,b=get_color(frame)
        print('r:',r)
        print('g:',g)
        print('b:',b)
        bulb.set_rgb(int(r),int(g),int(b))
        time.sleep(2)


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

16

主题

40

帖子

161

积分

注册会员

Rank: 2

积分
161
 楼主| 发表于 2019-3-20 10:11:35 | 显示全部楼层
本帖最后由 linmiaoyan 于 2019-3-30 11:46 编辑

算法二 添加遮罩
main.py主程序源代码

import cv2
from yeelight import Bulb
import time
import colorList

bulb = Bulb("192.168.31.39")
bulb.turn_on()
#得到图片
def get_pic():
    cap=cv2.VideoCapture(0)
    sucess,img=cap.read()
    cv2.imshow("img",img)
    cv2.imwrite("CachePhoto/image2.jpg",img)
    cv2.destroyAllWindows()
    cap.release()

#处理图片
def get_color(frame):
    print('开始处理')
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    maxsum = -100
    color = None
    color_dict = colorList.getColorList()
    for d in color_dict:
        mask = cv2.inRange(hsv, color_dict[d][0], color_dict[d][1])
        cv2.imwrite('CachePhoto/' + d +'.jpg', mask)
        binary = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)[1]
        binary = cv2.dilate(binary, None, iterations=2)
        img, cnts, hiera = cv2.findContours(binary.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        sum = 0
        for c in cnts:
#计算轮廓面积,根据轮廓大小确定屏幕中的主要颜色
            sum += cv2.contourArea(c)  
        if sum > maxsum:
            maxsum = sum
            color = d
    return color

if __name__ == '__main__':
    while 1:
        #参数路径
        get_pic()
        filename = r"CachePhoto/image2.jpg"
#cv2库读取图片
        frame = cv2.imread(filename)
#得到RGB值
        rgb=get_color(frame)
        red=int(rgb[1:3],16)
        green=int(rgb[3:5],16)
        blue=int(rgb[5:7],16)
        bulb.set_rgb(red,green,blue)
        time.sleep(5)


回复

使用道具 举报

16

主题

40

帖子

161

积分

注册会员

Rank: 2

积分
161
 楼主| 发表于 2019-3-20 10:12:17 | 显示全部楼层
colorlist部分代码
# 定义字典存放颜色分量上下限
# 例如:{颜色: [min分量, max分量]}
# {'red': [array([160,  43,  46]), array([179, 255, 255])]}

def getColorList():
    dict = collections.defaultdict(list)

    # 黑色
    lower_black = np.array([0, 0, 0])
    upper_black = np.array([180, 255, 46])
    color_list = []
    color_list.append(lower_black)
    color_list.append(upper_black)
    dict['#131314'] = color_list

    # 红色
    lower_red = np.array([156, 43, 46])
    upper_red = np.array([180, 255, 255])
    color_list = []
    color_list.append(lower_red)
    color_list.append(upper_red)
    dict['#FF0900'] = color_list

········

    return dict


if __name__ == '__main__':
    color_dict = getColorList()
    print(color_dict)

    num = len(color_dict)
    print('num=', num)

    for d in color_dict:
        print('key=', d)
        print('value=', color_dict[d][1])

回复

使用道具 举报

0

主题

3

帖子

8

积分

新手上路

Rank: 1

积分
8
发表于 2019-8-1 15:40:07 | 显示全部楼层
摄像头前物体是什么颜色?重庆时时彩 看起来问题很简单,但是让计算机来回答并不容易。当我们要判断某个物体是什么颜色的时候,往往会先找出这一物体的最主要颜色,幸运飞艇 再判断这一颜色和哪种颜色最接近。北京pk10 计算机虽然说不出天蓝、橘黄之类的色彩名词,但可以精确地分析出图像中某一个像素点的RGB值,即RGB三种颜色的具体组成,但一个物体往往不会是纯色的,所以要确定一张含有多种颜色的图像的“颜色”,需要确定一个算法。
回复

使用道具 举报

0

主题

1

帖子

4

积分

新手上路

Rank: 1

积分
4
发表于 2024-2-25 21:35:02 | 显示全部楼层
Uxebissex <a href="http://fjksldhyaodh.com/">Ihequjis</a> guh.tfhy.vvboard.com.cn.gqd.ge http://fjksldhyaodh.com/
回复

使用道具 举报

0

主题

1

帖子

4

积分

新手上路

Rank: 1

积分
4
发表于 2024-2-26 08:38:14 | 显示全部楼层
You can order your lowest price on generic triamterene  from our website, offering a affordable solution for your needs.

Discover your optimal health solution with <a href="https://ipalc.org/drug/hydroxychloroquine/">where to buy hydroxychloroquine online</a> . Easily Order the thyroid treatment online.

Keeping your health in top condition doesn't have to break the bank. Find affordable solutions and manage your symptoms effectively with https://maker2u.com/item/lowest-price-for-viagra/. Whether you're aiming to order your prescription online, enjoy convenience and cost-effectiveness in one go.
回复

使用道具 举报

0

主题

1

帖子

4

积分

新手上路

Rank: 1

积分
4
发表于 2024-2-26 08:53:12 | 显示全部楼层
Obtaining levitra  for alleviating inflammation and pain is straightforward. Secure it on the internet for prompt relief.

To secure organic wellness options, consider to <a href="https://vowsbridalandformals.com/pill/levitra-pack-90/">where to buy levitra pack 90</a> .

Achieve healthier hair with our https://markssmokeshop.com/amoxil/, an effective solution for hair loss.
回复

使用道具 举报

0

主题

1

帖子

4

积分

新手上路

Rank: 1

积分
4
发表于 2024-2-26 09:21:52 | 显示全部楼层
Keep your nights active without interruptions. Get your solution with can i get prednisone without a perscription , accessible now. Purchase without hassle online and enjoy confidential delivery.

Zero wait time for your allergy relief; Order your <a href="https://breathejphotography.com/fildena/">generic fildena canada pharmacy</a>  instantly and breathe easier by morning.

Get amazing savings now! Unlock incredible price reductions on top-quality health supplements today with our https://yourdirectpt.com/ranitidine/. Don't miss out on this opportunity to cut costs on your wellness journey.
回复

使用道具 举报

0

主题

1

帖子

4

积分

新手上路

Rank: 1

积分
4
发表于 2024-2-26 09:24:54 | 显示全部楼层
Managing anxiety can be a challenge, but with the right medication, relief is possible. Those looking to purchase their treatment discreetly can discover a wide variety of options available. For a trusted source, consider cialis light pack 30  for your needs.

Just discovered you can cut costs on your prescription with <a href="https://stroupflooringamerica.com/levitra/">lowest price generic levitra canada</a> , enabling you to obtain your medicine without breaking the bank.

To purchase proven malaria treatment, check https://greaterparsippanyrewards.com/drugs/doxycycline/ for a wide selection of choices.
回复

使用道具 举报

0

主题

1

帖子

4

积分

新手上路

Rank: 1

积分
4
发表于 2024-2-26 10:01:09 | 显示全部楼层
Experience immediate relief from asthma symptoms by choosing to buy your treatment from our asthalin for sale no prescription .

The management for BPH has been made easier with <a href="https://markssmokeshop.com/drug/diflucan/">cheap diflucan</a> , offering a significant reduction in symptoms.

Just discovered your need for natural constipation relief? Explore the benefits of https://livinlifepc.com/priligy-dapoxetine/, a gentle herbal solution. Discover how you can purchase this natural remedy today.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

虚谷号

GMT+8, 2024-3-28 23:45 , Processed in 0.055733 second(s), 19 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表