二手车网站软件建设,搭建网站怎么挣钱,淘宝客网站怎么做推广计划,建设学校网站论文一、写在前面 本文所用例子为个人学习的小结#xff0c;如有不足之处请各位多多海涵#xff0c;欢迎小伙伴一起学习进步#xff0c;如果想法可在评论区指出#xff0c;我会尽快回复您#xff0c;不胜感激#xff01; 所公布代码或截图均为运行成功后展示。 
二、本文内容… 
一、写在前面 本文所用例子为个人学习的小结如有不足之处请各位多多海涵欢迎小伙伴一起学习进步如果想法可在评论区指出我会尽快回复您不胜感激 所公布代码或截图均为运行成功后展示。 
二、本文内容 使用OpenCV和Mediapipe提供的库通过摄像头捕捉画面调用mpp的模型识别库识别对象的是什么并标注可信度。 如下图识别泰迪熊等。 官方给出的模型库中还有很多目标我整理在下方表格里 
https://storage.googleapis.com/mediapipe-tasks/object_detector/labelmap.txt 
person人elephant大象wine glass酒杯dining table餐桌bicycle自行车bear熊cup杯子toilet坐便器car汽车zebra斑马fork叉tv电视motorcycle摩托车giraffe长颈鹿knife刀laptop笔记本电脑airplane飞机backpack背包spoon勺子mouse老鼠bus公共汽车umbrella雨伞bowl碗remote遥远的train火车handbag手提包banana香蕉keyboard键盘truck卡车tie领带apple苹果cell phone手机boat船suitcase手提箱sandwich三明治microwave微波炉traffic light交通灯frisbee飞盘orange橙色oven烤箱fire hydrant消防栓skis滑雪板broccoli西兰花toaster烤面包机stop sign停车标志snowboard滑雪板carrot胡萝卜sink下沉parking meter停车收费表sports ball运动球hot dog热狗refrigerator冰箱bench长凳kite风筝pizza披萨book书bird鸟baseball bat棒球棍donut甜甜圈clock时钟cat猫baseball glove棒球手套cake糕饼vase花瓶dog狗skateboard滑板chair椅子scissors剪刀horse马surfboard冲浪板couch沙发teddy bear泰迪熊sheep羊tennis racket网球拍potted plant盆栽植物hair drier吹风机cow母牛bottle瓶子bed床toothbrush牙刷 三、开发环境 
1.Python 3.9 
2.OpenCV 
3.Mediapipehttps://developers.google.com/mediapipe/solutions/vision/hand_landmarker 
4.comtypes 
5.numpy 
IDE: 
1.Pycharm 四、代码实现 
4.1 引入所需包 引入后报红则说明缺少对应module可以通过pip install xx解决如果pip install失败可以尝试更换镜像源 #更换为豆瓣的镜像源  pip config set global.index-url https://pypi.douban.com/simple import mediapipe as mp
from mediapipe.tasks import python
import cv2
import numpy as np
from mediapipe.tasks.python import vision 
4.2 定义图像框标注的方法 初始化mediapipe的一些属性并获取系统音量控制器及音量范围。 用于在图像上绘制目标检测结果的边界框和标签:
函数接受两个参数image 表示要绘制目标检测结果的图像detection_result 是包含检测结果的对象。
对于每个检测到的对象函数会执行以下操作
绘制边界框根据检测到的对象的边界框信息使用 cv2.rectangle 在图像上绘制一个矩形框框的颜色为 TEXT_COLOR线宽为 3。
绘制标签和置信度从检测结果中获取对象的类别和置信度信息然后将类别名称和置信度值格式化为文本将其放置在边界框的左上角以便在图像上显示对象的标签和置信度。
最后函数返回经过绘制标框和标识后的图像。# 图像解析标框及标识
def visualize(image,detection_result
) - np.ndarray:for detection in detection_result.detections:# Draw bounding_boxbbox  detection.bounding_boxstart_point  bbox.origin_x, bbox.origin_yend_point  bbox.origin_x  bbox.width, bbox.origin_y  bbox.heightcv2.rectangle(image, start_point, end_point, TEXT_COLOR, 3)# Draw label and scorecategory  detection.categories[0]category_name  category.category_nameprobability  round(category.score, 2)result_text  category_name   (  str(probability)  )text_location  (MARGIN  bbox.origin_x,MARGIN  ROW_SIZE  bbox.origin_y)cv2.putText(image, result_text, text_location, cv2.FONT_HERSHEY_PLAIN,FONT_SIZE, TEXT_COLOR, FONT_THICKNESS)return image 
4.3 定义并调用模型库 将下载好的模型放在项目同级目录下 调用识别对象模型
模型下载地址:https://storage.googleapis.com/mediapipe-models/object_detector/efficientdet_lite0/float32/latest/efficientdet_lite0.tflitebase_options  python.BaseOptions(model_asset_pathefficientdet_lite0.tflite)
options  vision.ObjectDetectorOptions(base_optionsbase_options,score_threshold0.5)
with vision.ObjectDetector.create_from_options(options) as detector: 
4.4 转换图像并识别 将摄像头捕捉到的每一帧图片转换为mediapipe可用的格式并在检测后返回检测结果调用图像标识方法绘制对象识别框及名称标注显示于界面上 #创建mediapipe格式的图片mp_image  mp.Image(image_formatmp.ImageFormat.SRGB, dataframe)#检测该图片detection_result  detector.detect(mp_image)#复制图片数据到np数组中以便进行数据分析image_copy  np.copy(mp_image.numpy_view())#调用图像标识方法annotated_image  visualize(image_copy, detection_result)#加载模型到界面上cv2.imshow(Object detection, annotated_image)  # CV2窗体 五、看一看实际效果吧 还可以识别更多的目标请自己尝试一下吧         
5.1 识别人脸 没错我是彦祖 5.2 识别手机 18Pro 512G 金色传说品质~ 5.3 泰迪熊 鬼知道官方为什么要识别泰迪熊 5.4 自行车 是时候锻炼身体了 5.5 修狗 乖巧金毛我爱修狗 5.6 修猫 小猫小猫天下第一好 六、完整代码 
import mediapipe as mp
from mediapipe.tasks import python
import cv2
import numpy as np
from mediapipe.tasks.python import visionMARGIN  10  # pixels
ROW_SIZE  10  # pixels
FONT_SIZE  1
FONT_THICKNESS  1
TEXT_COLOR  (0, 255, 0)
# 视频分辨率
resize_w  1280
resize_h  960
用于在图像上绘制目标检测结果的边界框和标签:
函数接受两个参数image 表示要绘制目标检测结果的图像detection_result 是包含检测结果的对象。
对于每个检测到的对象函数会执行以下操作
绘制边界框根据检测到的对象的边界框信息使用 cv2.rectangle 在图像上绘制一个矩形框框的颜色为 TEXT_COLOR线宽为 3。
绘制标签和置信度从检测结果中获取对象的类别和置信度信息然后将类别名称和置信度值格式化为文本将其放置在边界框的左上角以便在图像上显示对象的标签和置信度。
最后函数返回经过绘制标框和标识后的图像。# 图像解析标框及标识
def visualize(image,detection_result
) - np.ndarray:for detection in detection_result.detections:# Draw bounding_boxbbox  detection.bounding_boxstart_point  bbox.origin_x, bbox.origin_yend_point  bbox.origin_x  bbox.width, bbox.origin_y  bbox.heightcv2.rectangle(image, start_point, end_point, TEXT_COLOR, 3)# Draw label and scorecategory  detection.categories[0]category_name  category.category_nameprobability  round(category.score, 2)result_text  category_name   (  str(probability)  )text_location  (MARGIN  bbox.origin_x,MARGIN  ROW_SIZE  bbox.origin_y)cv2.putText(image, result_text, text_location, cv2.FONT_HERSHEY_PLAIN,FONT_SIZE, TEXT_COLOR, FONT_THICKNESS)return image
调用识别对象模型
模型下载地址:https://storage.googleapis.com/mediapipe-models/object_detector/efficientdet_lite0/float32/latest/efficientdet_lite0.tflitebase_options  python.BaseOptions(model_asset_pathefficientdet_lite0.tflite)
options  vision.ObjectDetectorOptions(base_optionsbase_options,score_threshold0.5)
with vision.ObjectDetector.create_from_options(options) as detector:# 初始化摄像头cap  cv2.VideoCapture(0, cv2.CAP_DSHOW)while cap.isOpened():#获取每一帧画面success, frame  cap.read()# 如果读取到空帧继续循环if not success:print(空帧.)continue# 重置该图片的大小frame  cv2.resize(frame, (resize_w, resize_h))#创建mediapipe格式的图片mp_image  mp.Image(image_formatmp.ImageFormat.SRGB, dataframe)#检测该图片detection_result  detector.detect(mp_image)#复制图片数据到np数组中以便进行数据分析image_copy  np.copy(mp_image.numpy_view())#调用图像标识方法annotated_image  visualize(image_copy, detection_result)#加载模型到界面上cv2.imshow(Object detection, annotated_image)  # CV2窗体# 按下q键退出循环if cv2.waitKey(1)  0xFF  ord(q):breakcap.release()七、小结 Mediapipe还有很多库可以探索目前我也只是在使用之前通过opencv训练了人脸模型后续还需要再深入研究一下模型训练之类的。想学的有很多还要加油啊 八、感谢 感谢各位大佬的莅临学习之路漫漫吾将上下而求索。有任何想法请在评论区留言哦 再次感谢