图像处理=maixpy3

这篇博客介绍了如何在MaixPy环境下使用摄像头进行图像处理,包括启动摄像头、拍摄图片、显示图片、实时显示画面以及寻找特定颜色块。此外,还展示了如何利用神经网络进行图像分类(ResNet18)和人脸识别,以及进行边缘检测。代码示例涵盖了颜色阈值设置、模型加载和运行,以及结果的实时显示。
摘要由CSDN通过智能技术生成
一、相机使用

使用摄像头
启动摄像头,显示第第一帧画片

from maix import display,camera
display.show(camera.capture())

拍摄图片
启动摄像头拍摄一张图片,保存图片到/root/文件夹下,命名为tmp.jpg重启可以看到

from maix import camera, display
img = camera.capture()
img.save('/root/tmp.jpg')
display.show(img)

读取保存的图片并显示到屏幕上

from maix import image, display
img = image.Image()
img = img.open('/root/tmp.jpg')#读取 /root/tmp.jpg 的图片
display.show(img)#显示到屏幕上

屏幕实时显示摄像头画面

from maix import display, camera
while True:
    display.show(camera.capture())
二、图像处理

找色块

#寻找色块可以通过指定 HSV 阈值、RBG 值或者 LAB 值,进行颜色的确定,下面的例程代码可以通过修改中的函数参数进行修改寻找的颜色
#绿色	[(28,-36,-14,68,-5,15)]#HSV值域
#红色	[(20,22,-3,55,52,42)]
#黄色	[(35,-6,22,88,5,81)]
#蓝色	[(13, 11, -91, 54, 48, -28)]
#白色	[(41,6,-32,74,11,-12)]
#黑色	[(10,-3,-28,50,10,-4)

from maix import camera, image, display

while True:
    tmp = camera.capture()
    ma = tmp.find_blobs([(28,-36,-14,68,-5,15)]) # 传入需要寻找颜色的 HSV 值
    for i in ma:
        tmp.draw_rectangle(i["x"], i["y"], i["x"] + i["w"], i["y"] + i["h"], (255, 0, 0), 1)
    display.show(tmp)

获取颜色值

#述寻找颜色的 LAB 阈值,可以通过以下的方法进行识别。
from maix import camera, image, display

while True:
    img = camera.capture()
    ma = img.get_blob_color((110,110,20,20),1)
    img.draw_string(10, 10, str(ma), 0.5)
    img.draw_rectangle(110,110, 130, 130, (255, 0, 0), 1) 
    display.show(img)
三、神经网络使用

更多模型功能下载MaixHub
注意事项

  • 1、部署模型的代码请不要多次运行,这样会过度使用内存,运行网络的时候会内存溢出
  • 2、当运行代码的时候,屏幕长时间没有显示的时候,需要手动重启机器,再将重新运行一次代码

运行ResNet18分类网络

  • 部署Resnet18模型
    class Resnet:
    	m={
    		"param":"/home/model/resnet18_10000_awmm.param",
    		"bin":"home/model/resnet18_1000_awnn.bin"
    	}
    	options={
    		"model_type":"awnn",
    		"inputs":{
    			"input0":(224,224,3)
    		},
    		"outputs":{
    			"output0":(1,1,1000)
    		},
    		"first_layer_conv_no_pad":False,
    		"mean":[127.5,127.5,127.5],
    		"norm":[0.00784313725490196,0.00784313725490196,0.00784313725490196],
    	}
    	def __init__(self):
    		from maix import nn
    		self.model=nn.load(self.m,opt=self.options)
    	
    	def __del__(self):
    		del self.model
    
    global resnet
    resnet=Resnet()
    
  • 开始进行分类识别
    print(resnet.model)
    from maix import camera,nn,display
    from home.res.classes_label import labels
    whilw True:
    	img=camera.capture().resize(224,224)
    	tmp=img.tobytes()
    	out=resnet.model.forward(tmp,quantize=True)#为分类得分
    	out2=nn.F.softmax(out)#为分类概率
    	msg="{:.2f}:{}",format(out2.max(),labels[out.argmax()])
    	img.draw_string(0,0,str(msg),1,(255,0,0),1)
    	display.show(img)
    

人脸检测

  • 部署人脸检测模型
    class face:
    	labels=["person"]
    	anchors=[1.19,1.98,2.79,4.59,4.53,8.92,8.06,5.29,10.32,10.65]
    	m={"param":"/home/model/face/yolov2_face_awnn.param",
    		"bin":"/home/model/face/yolov2_face_awnn.bin"
    	}
    	options={
    		"model_type":"awnn",
    		"inputs":{
    			"input0":(224,224,3)
    		},
    		"outputs":{
    			"output0":(7,7,(1+4+1)*5)
    		},
    		"mean":[127.5,127.5,127.5],
    		"norm":[0.0078125,0.0078125,0.0078125],
    	}
    	def __init__(self):
    		from maix import nn
    		from maix.nn import decoder
    		self.model=nn.load(self.m,opt=self.options)
    			self.yolo=decoder.Yolo2(len(self.labels),
    									self.anchors,
    									net_in_size(224,224),
    									net_out_size=(7,7))
    		
    	def __del__(self):
    		del self.model
    		del self.yolo
    
    global Face
    Face=face()
    print(Face)
    
  • 运行网络
    	from maix import camera,display,nn,image
    	from maix.nn import decoder
    	labels=["person"]
    	while True:
    		img=camera.capture().resize(224,224)
    		out=Face.model.forward(img.tobytes(),quantize=True,layout="hwc")
    		boxes,probs=Face.yolo.run(out,nms=0.3,threshol0.5,imgh_size=(224,224))
    		if len(boxes):
    			for i,box in enumerate(boxes):
    				img.draw_rectangle(box[0],box[1],box[0]+box[2],box[1]+box[3],(255,0,0),1)
    			display.show(img)
    		else:
    			display.show(img)
    

边缘检测

from maix import nn,camera,display,image
import numpy as np
import time
model={
	"param":"/home/model/sobel_int8.param",
	"bin":"/home/model/sobel_int8.bin"
}
input_size=(224,224,3)
output_size=(222,222,3)

options={
	"model_type":"awnn",
	"inputs":{
		"input0":input_size
	},
	"outputs":{
		"output0":output_size
	},
	"mean":[127.5,127.5,127.5],
	"norm":[0.0078125,0.0078125,0.0078125],
}
print("-- load model:",model)
m=nn.load(model,opt=options)
print("-- load ok")

while True:
	img=camera.capture().resize(224,224)
	out = m.forward(img.tobytes(),quantize=True,layout="hwc")
	out=out.astype(np.flaot32).reshape(output_size)
	out=(np.ndarray.__abs__(out)*255/out.max()).astype(np.uint8)
	data=out.tobytes()
	img2=img.load(data,(222,222),mode="RGB")
	display.show(img2)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

栋哥爱做饭

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

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

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

打赏作者

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

抵扣说明:

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

余额充值