# 人工智能 ## 自定义模型识别 ```python from AI import CustomModel ``` ### 模型初始化 ```python CustomModel(labels,path) ``` ### 参数说明 | 参数 | 类型 | 说明 | | :--- | :--- | :--- | | labels | list | 标签列表,必填 | | path | str | 模型路径,必填 | ### 加载识别 ```python loadModel(img) ``` ### 参数说明 | 参数 | 类型 | 说明 | | :--- | :--- | :--- | | img | image.Image() | 要识别的Image对象,必填 | ### 样例 ```python from cocopiPython import cameraPython,Screen from AI import CustomModel Screen = Screen() # 画布初始化 camera = cameraPython() # 摄像头初始化 labels = ["loadModel", "unload", "left", "right", "forward", "stop"] # 标签 path = "/root/preset/model/autologistics" # 模型路径 CustomModel = CustomModel(labels,path) # 初始化模型 while True: canvas = camera.capture() # 获取摄像头图像 canvas = canvas.crop(48,8,224,224) # 裁剪图像为224*224 data = CustomModel.loadModel(canvas) # 加载模型进行识别 for i in data: canvas.draw_rectangle(i["X"],i["Y"],i["X"]+i["W"],i["Y"]+i["H"],color=(255,0,0),thickness=1) # 在屏幕绘制检测框 canvas.draw_string(i["X"],i["Y"]-20,(str(i["name"])+":"+str(i["score"])),color=(255,0,0)) # 在屏幕上绘制名称与置信度 Screen.show(canvas,48,8) # 显示画布 ``` 其中i["X"]表示识别结果的检测框起点X轴坐标,i["Y"]表示识别结果的检测框起点Y轴坐标,i["W"]表示识别结果的检测框宽度,i["H"]表示识别结果的检测框高度,i["name"]表示识别结果的名称,i["score"]表示识别结果的置信度,i["middleX"]表示识别结果的检测框中心点的X轴坐标,i["middleY"]表示识别结果的检测框中心点的Y轴坐标。 ## 边缘检测 ```python from AI import EdgeDetection ``` ### 模型初始化 ```python EdgeDetection() ``` ### 加载识别 ```python loadModel(img) ``` ### 参数说明 | 参数 | 类型 | 说明 | | :--- | :--- | :--- | | img | image.Image() | 要识别的Image对象,必填 | ### 样例 ```python from cocopiPython import cameraPython,Screen from AI import EdgeDetection Screen = Screen() # 画布初始化 camera = cameraPython() # 摄像头初始化 EdgeDetection = EdgeDetection() # 初始化 while True: img = camera.capture() # 获取摄像头数据 img = img.crop(48,8,224,224) # 裁剪尺寸为224*224 canvas = EdgeDetection.loadModel(img) # 加载模型 Screen.show(canvas,48,8) # 显示画布 ``` ## 人脸检测 ```python from AI import Facedetection ``` ### 模型初始化 ```python Facedetection() ``` ### 加载识别 ```python loadModel(img) ``` ### 参数说明 | 参数 | 类型 | 说明 | | :--- | :--- | :--- | | img | image.Image() | 要识别的Image对象,必填 | ### 样例 ```python from cocopiPython import cameraPython,Screen from AI import Facedetection Screen = Screen() # 画布初始化 camera = cameraPython() # 摄像头初始化 face = Facedetection() # 人脸检测初始化 while True: canvas = camera.capture() # 获取摄像头图像 canvas = canvas.crop(48,8,224,224) # 裁剪图像尺寸 data = face.loadModel(canvas) # 加载人脸检测模型 for i in data: canvas.draw_rectangle(i["X"],i["Y"],i["X"]+i["W"],i["Y"]+i["H"],color=(255,0,0), thickness=1) # 在屏幕上绘制人脸检测框 canvas.draw_string(5,5,str(len(data)), scale = 1, color = (255,255,255) , thickness = 1) # 在屏幕上显示人脸数量 Screen.show(canvas,48,8) # 显示画布 ``` ## 人脸识别 ```python from AI import FaceRecognition ``` ### 模型初始化 ```python FaceRecognition() ``` ### 加载模型 加载人脸检测模型,获取人脸信息 ```python loadmodel(img) ``` ### 参数说明 | 参数 | 类型 | 说明 | | :--- | :--- | :--- | | img | image.Image() | 要识别的Image对象,必填 | ### 添加人脸 录入人脸信息保存到本地 ```python add(name,feature,dataPath) ``` ### 参数说明 | 参数 | 类型 | 说明 | | :--- | :--- | :--- | | name | str | 识别到的人脸名称,必填 | | feature | list | 人脸数据,必填 | | dataPath | str | 保存路径,选填,默认为"/root/user/model/recorded_face_features.py" | ### 删除人脸 删除本地保存的最后一条人脸数据 ```python remove(name,dataPath) ``` ### 参数说明 | 参数 | 类型 | 说明 | | :--- | :--- | :--- | | name | str | 要删除的人脸名称,必填 | | dataPath | str | 保存路径,选填,默认为"/root/user/model/recorded_face_features.py" | ### 加载人脸信息 加载人脸采集时保存到本地的人脸信息数据 ```python loadface(DataPath) ``` ### 参数说明 | 参数 | 类型 | 说明 | | :--- | :--- | :--- | | dataPath | str | 保存路径,选填,默认为"/root/user/model/recorded_face_features.py" | ### 样例 #### 人脸采集 ```python import time from cocopiPython import cameraPython,Screen from AI import FaceRecognition from CocoPi import BUTTON # 按钮初始化 key_C = BUTTON(13) key_D = BUTTON(7) Screen = Screen() camera = cameraPython() FaceRecognition = FaceRecognition() People_list = ["小明","小红","小刚","小花"] FaceNum = 0 faceScore = 0 color = (255,0,0) while True: canvas = camera.capture() # 获取摄像头数据 canvas = canvas.crop(48,8,224,224) # 裁剪图片尺寸 data = FaceRecognition.loadmodel(canvas) # 加载人脸检测模型 for i in data: canvas.draw_rectangle(i["X"],i["Y"],i["X"]+i["W"],i["Y"]+i["H"],color=color,thickness=1) # 检测框 if key_C.is_pressed() and FaceNum < len(People_list): while not key_C.is_pressed()== False: time.sleep(0.01) FaceRecognition.add(People_list[len(FaceRecognition)],i["feature"]) # 录入人脸保存 canvas.draw_rectangle(0,0,320,20,color=(0,0,0),thickness=-1) canvas.draw_string(0,0,"已采集"+str(People_list[FaceNum])+"人脸数据",scale=1,color=(255,255,255),thickness=1) Screen.show(canvas,48,8) FaceNum = FaceNum + 1 time.sleep(1) if key_D.is_pressed() and FaceNum > 0: while not key_D.is_pressed()== False: time.sleep(0.01) FaceRecognition.remove(People_list[len(FaceRecognition)-1]) # 删除人脸 canvas.draw_rectangle(0,0,320,20,color=(0,0,0),thickness=-1) canvas.draw_string(0,0,"已删除"+str(People_list[FaceNum-1])+"人脸数据",scale=1,color=(255,255,255),thickness=1) Screen.show(canvas,48,8) FaceNum = FaceNum - 1 time.sleep(1) Screen.show(canvas,48,8) ``` #### 人脸识别 ```python from cocopiPython import cameraPython,Screen from AI import FaceRecognition Screen = Screen() camera = cameraPython() FaceRecognition = FaceRecognition() faceScore = 0 color = (255,0,0) while True: canvas = camera.capture() # 获取摄像头数据 canvas = canvas.crop(48,8,224,224) # 裁剪图片尺寸 data = FaceRecognition.loadmodel(canvas) # 加载人脸检测模型 FaceRecognition.loadface() for i in data: faceScore = round(i["recognizeScore"],2) canvas.draw_rectangle(i["X"],i["Y"],i["X"]+i["W"],i["Y"]+i["H"],color=color,thickness=1) # 检测框 canvas.draw_string(i["X"],i["Y"],str(i["name"])+":"+str(faceScore),scale=1,color=color,thickness=1) if faceScore > 80: color = (0,255,0) else: color = (255,0,0) Screen.show(canvas,48,8) ``` #### 人脸识别+人脸采集 ```python import time from cocopiPython import cameraPython,Screen from AI import FaceRecognition from CocoPi import BUTTON # 按钮初始化 key_C = BUTTON(13) key_D = BUTTON(7) Screen = Screen() camera = cameraPython() FaceRecognition = FaceRecognition() People_list = ["小明","小红","小刚","小花"] FaceNum = 0 faceScore = 0 color = (255,0,0) while True: canvas = camera.capture() # 获取摄像头图像 canvas = canvas.crop(48,8,224,224) # 裁剪图片尺寸 data = FaceRecognition.loadmodel(canvas) # 加载人脸检测模型 for i in data: canvas.draw_rectangle(i["X"],i["Y"],i["X"]+i["W"],i["Y"]+i["H"],color=color,thickness=1) # 检测框 if key_C.is_pressed() and FaceNum < len(People_list): while not key_C.is_pressed()== False: time.sleep(0.01) FaceRecognition.add(People_list[len(FaceRecognition)],i["feature"]) # 录入人脸保存 canvas.draw_rectangle(0,0,320,20,color=(0,0,0),thickness=-1) canvas.draw_string(0,0,"已采集"+str(People_list[FaceNum])+"人脸数据",scale=1,color=(255,255,255),thickness=1) Screen.show(canvas,48,8) FaceNum = FaceNum + 1 time.sleep(1) faceScore = round(i["recognizeScore"],2) if faceScore > 70: color = (0,255,0) canvas.draw_string(i["X"],i["Y"],str(i["name"])+":"+str(faceScore),scale=1,color=color,thickness=1) else: color = (255,0,0) if key_D.is_pressed() and FaceNum > 0: while not key_D.is_pressed()== False: time.sleep(0.01) FaceRecognition.remove(People_list[len(FaceRecognition)-1]) # 删除人脸 canvas.draw_rectangle(0,0,320,20,color=(0,0,0),thickness=-1) canvas.draw_string(0,0,"已删除"+str(People_list[FaceNum-1])+"人脸数据",scale=1,color=(255,255,255),thickness=1) Screen.show(canvas,48,8) FaceNum = FaceNum - 1 time.sleep(1) Screen.show(canvas,48,8) ``` ## 数字识别 ```python from AI import Digitalecognition ``` ### 模型初始化 ```python Digitalecognition() ``` ### 加载识别 ```python loadModel(img) ``` ### 参数说明 | 参数 | 类型 | 说明 | | :--- | :--- | :--- | | img | image.Image() | 要识别的Image对象,必填 | ### 样例 ```python from cocopiPython import cameraPython,Screen from AI import Digitalecognition Screen = Screen() # 画布初始化 camera = cameraPython() # 摄像头初始化 Digit = Digitalecognition() # 人脸检测初始化 while True: canvas = camera.capture() # 获取摄像头图像 canvas = canvas.crop(48,8,224,224) # 裁剪图像尺寸 data = Digit.loadModel(canvas) for i in data: canvas.draw_rectangle(i["X"],i["Y"],i["X"]+i["W"],i["Y"]+i["H"],color=(255,0,0),thickness=1) canvas.draw_string(i["X"],i["Y"]-20,(str(i["name"])+":"+str(i["score"])),color=(255,0,0)) Screen.show(canvas,48,8) ``` ## 物体检测 ```python from AI import ObjectDetection ``` ### 模型初始化 ```python ObjectDetection() ``` ### 加载识别 ```python loadModel(img) ``` ### 参数说明 | 参数 | 类型 | 说明 | | :--- | :--- | :--- | | img | image.Image() | 要识别的Image对象,必填 | ### 样例 ```python from cocopiPython import cameraPython,Screen from AI import ObjectDetection Screen = Screen() # 画布初始化 camera = cameraPython() # 摄像头初始化 object = ObjectDetection() # 物体检测初始化 while True: canvas = camera.capture() # 获取摄像头图像 canvas = canvas.crop(48,8,224,224) # 裁剪图像尺寸 data = object.loadModel(canvas) for i in data: canvas.draw_rectangle(i["X"],i["Y"],i["X"]+i["W"],i["Y"]+i["H"],color=(255,0,0),thickness=1) canvas.draw_string(i["X"],i["Y"]-20,(str(i["name"])+":"+str(i["score"])),color=(255,0,0)) Screen.show(canvas,48,8) ``` ## 猜拳识别 ```python from AI import RockPaperScissors ``` ### 模型初始化 ```python RockPaperScissors() ``` ### 加载识别 ```python loadModel(img) ``` ### 参数说明 | 参数 | 类型 | 说明 | | :--- | :--- | :--- | | img | image.Image() | 要识别的Image对象,必填 | ### 样例 ```python from cocopiPython import cameraPython,Screen from AI import RockPaperScissors Screen = Screen() camera = cameraPython() RockPaperScissors = RockPaperScissors() while True: canvas = camera.capture() canvas = canvas.crop(48,8,224,224) data = RockPaperScissors.loadModel(canvas) for i in data: canvas.draw_rectangle(i["X"],i["Y"],i["X"]+i["W"],i["Y"]+i["H"],color=(255,0,0),thickness=1) canvas.draw_string(i["X"],i["Y"]-10,(str(i["name"])+":"+str(i["score"])),color=(255,0,0)) Screen.show(canvas,48,8) ``` ## 自学习识别 ```python from AI import SelfLearning ``` ### 模型初始化 ```python SelfLearning(lables) ``` ### 参数说明 | 参数 | 类型 | 说明 | | :--- | :--- | :--- | | lables | 列表 | 要识别的物体名称列表,必填 | ### 添加类别 ```python addClass(img) ``` ### 参数说明 | 参数 | 类型 | 说明 | | :--- | :--- | :--- | | img | image.Image() | 要识别的Image对象,必填 | ### 训练 ```python trainSave(path) ``` ### 参数说明 | 参数 | 类型 | 说明 | | :--- | :--- | :--- | | path | string | 保存路径,选填,默认为“/root/user/model/module.bin” | ### 加载识别 ```python loadModel(img,path) ``` ### 参数说明 | 参数 | 类型 | 说明 | | :--- | :--- | :--- | | img | image.Image() | 要识别的Image对象,必填 | | path | string | 保存路径,选填,默认为“/root/user/model/module.bin” | ### 样例 #### 采集 ```python import time from cocopiPython import cameraPython,Screen from AI import SelfLearning from CocoPi import BUTTON key_C = BUTTON(13) key_D = BUTTON(7) Screen = Screen() camera = cameraPython() lables=(['鼠标','键盘','水杯']) SL = SelfLearning(lables) is_train = 0 count = 0 str_display = "按C键添加类别,按D键進行学习" while True: # 获取摄像头数据 canvas = camera.capture() canvas = canvas.crop(48,8,224,224) if key_C.is_pressed(): while not key_C.is_pressed()== False: time.sleep(0.01) if count < len(lables): # 采集数据添加到列表中 time.sleep(1) SL.addClass(canvas) count += 1 if key_D.is_pressed(): while not key_D.is_pressed()== False: time.sleep(0.01) if count >= len(lables) and is_train == 0: time.sleep(1) # 训练并保存,参数:path 为保存路径默认/root/user/model/module.bin SL.train() is_train = 1 str_display = "学习已完成" canvas.draw_string(0,0,str_display,scale=1,color=(255,0,0),thickness=1) canvas.draw_string(0,16,"采集次数:"+str(count),scale=1,color=(255,0,0),thickness=1) Screen.show(canvas,48,8) ``` #### 识别 ```python from cocopiPython import cameraPython,Screen from AI import SelfLearning Screen = Screen() camera = cameraPython() SL= SelfLearning(['鼠标','键盘','水杯']) SL.load() while True: # 获取摄像头数据 img = camera.capture() canvas = img.crop(48,8,224,224) data = SL.predict(canvas) canvas.draw_string(0,5,(str(data["name"])+":"+str(data["score"])),scale=1,color=(255,0,0),thickness=1) Screen.show(canvas,48,8) ``` ## 拼音识别 ```python from AI import PinyinRecognition ``` ### 模型初始化 ```python PinyinRecognition(keywords) ``` ### 参数说明 | 参数 | 类型 | 说明 | | :--- | :--- | :--- | | keywords | 列表 | 拼音参数,文字的拼音与音调,必填 | ### 获取结果 ```python getresult() ``` ### 样例 ```python from cocopiPython import Screen from AI import PinyinRecognition Screen = Screen() canvas = Screen.create() keywords = [["ni3 hao3",0.1],["hao3 de1",0.1],["wan3 shang4 hao3",0.1]] # 设置拼音参数 PinyinRecognition = PinyinRecognition(keywords) data ="" while True: canvas.clear() data = str(PinyinRecognition.getResult()) # 获取拼音识别结果 print("jieguo:"+str(data)) canvas.draw_string(0,0, "请对准麦克风说出指令", scale = 2, color = (255,0,0) , thickness = 1) if data != "None": canvas.draw_string(0, 30, "拼音识别结果:", scale=2, color=(255,0,0), thickness = 1) canvas.draw_string(30, 60, (data), scale=2, color=(255,255,255), thickness = 1) Screen.show(canvas) ``` ## 车牌识别 ```python from AI import PlateRecognition ``` ### 模型初始化 ```python PlateRecognition() ``` ### 加载识别 ```python loadModel(img) ``` ### 参数说明 | 参数 | 类型 | 说明 | | :--- | :--- | :--- | | img | image.Image() | 要识别的Image对象,必填 | ### 样例 ```python from cocopiPython import cameraPython,Screen from AI import PlateRecognition Screen = Screen() # 画布初始化 camera = cameraPython() # 摄像头初始化 PlateRecognition = PlateRecognition() # 车牌识别初始化 while True: canvas = camera.capture() # 获取摄像头图像 canvas = canvas.crop(48,8,224,224) # 裁剪图像尺寸 data = PlateRecognition.load(canvas) # 加载识别 for i in data: canvas.draw_rectangle(i["X"],i["Y"],i["X"]+i["W"],i["Y"]+i["H"],color=(255,0,0),thickness=1) # 检测框 canvas.draw_string(i["X"],i["Y"]-10,(str(i["name"])),color=(255,0,0)) # 名称 Screen.show(canvas,48,8) # 显示 ```