摄像头水色监控

摄像头水色监控
本文将利用摄像头监控水的颜色变化,利用的是图像识别+opencv的处理。而其中的图像识别部分可参见我的文章“煤等矿物的视觉识别”,链接如下 https://mp.weixin.qq.com/s/xYlBPypVgvLOg0a6GErb5Q,所以不再重复。而本篇文章可接在图像识别后面达到视频监控效果。
首先我们已经依靠文章“煤等矿物的视觉识别”训练出了水色图像识别模型。接着是摄像头的处理部分。
在这里插入图片描述
在这里插入图片描述
程序思维如下:
首先读取模型,建立标签
接着cv2读取摄像头
因为要对图像处理添加中文标注,所以需要随时保存摄像头图片,
接着读取图片转为PIL图片,设置字体,在(300,10)处添加文字
文字内容为标签的预测值,然后随时更新摄像头状态即可。
代码如下:
from keras.models import load_model
from keras.preprocessing.image import img_to_array,load_img
import numpy as np
import cv2
from PIL import Image
from PIL import ImageDraw,ImageFont

载入模型

model = load_model(‘rock_selector.h5’)
label = np.array([‘黄水’,‘清水’])

def image_change(image):
image = image.resize((224, 224))
image = img_to_array(image)
image = image / 255
image = np.expand_dims(image, 0)
return image

video = “http://admin:admin@192.168.137.165:8081/”

cap = cv2.VideoCapture(0)
while True:
_,frame = cap.read()
bg = “temp.png”
cv2.imwrite(bg,frame)
image = load_img(“temp.png”)
#plt.imshow(image)
image = image_change(image)
img=cv2.imread(“temp.png”)
img_PIL = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
myfont = ImageFont.truetype(r’C:/Windows/Fonts/simfang.ttf’, 40)
draw = ImageDraw.Draw(img_PIL)
draw.text((300, 10), label[model.predict_classes(image)][0], font=myfont, fill=(200, 100, 0))
img_OpenCV = cv2.cvtColor(np.asarray(img_PIL), cv2.COLOR_RGB2BGR)
cv2.imshow(‘frame’, img_OpenCV)
cv2.waitKey(1)

全部代码,包括模型在公众号回复“摄像头水色监控”获取
欢迎大家关注公众号“人工智能实战及常见代码分享”,
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI看世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值