手机检测结果可视化进阶OpenCV绘制带置信度标签的红框教程1. 项目背景与需求在实际的手机检测项目中仅仅检测出手机位置是不够的。我们还需要清晰地展示检测结果让用户能够直观地看到每个检测到的手机位置以及对应的置信度。这就是为什么我们需要在检测框上添加标签信息。传统的检测框绘制往往只显示一个简单的矩形框但这样的展示方式信息量有限。通过OpenCV的高级绘图功能我们可以创建更加专业和美观的可视化效果包括不同颜色的检测框红色表示手机检测显示每个检测结果的置信度百分比添加背景色块提高文字可读性自定义标签样式和位置本教程将基于DAMO-YOLO手机检测系统教你如何使用OpenCV实现专业的检测结果可视化。2. 环境准备与基础代码2.1 安装所需库首先确保你已经安装了必要的Python库pip install opencv-python numpy pillow2.2 基础检测代码以下是手机检测的基础代码框架import cv2 import numpy as np from modelscope.pipelines import pipeline from modelscope.utils.constant import Tasks class PhoneDetector: def __init__(self): # 初始化DAMO-YOLO检测模型 self.detector pipeline( Tasks.domain_specific_object_detection, modeldamo/cv_tinynas_object-detection_damoyolo_phone ) def detect_phones(self, image_path): # 读取图像 image cv2.imread(image_path) # 执行检测 result self.detector(image_path) return image, result3. OpenCV高级绘图技巧3.1 基础矩形框绘制让我们从最简单的红色矩形框开始def draw_basic_boxes(image, detections): for detection in detections[boxes]: x1, y1, x2, y2 detection[:4].astype(int) confidence detection[4] # 绘制红色矩形框 cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255), 2) return image3.2 添加置信度标签简单的矩形框还不够我们需要添加置信度信息def draw_boxes_with_labels(image, detections): for detection in detections[boxes]: x1, y1, x2, y2 detection[:4].astype(int) confidence detection[4] # 绘制矩形框 cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255), 2) # 准备标签文本 label fphone: {confidence:.1%} # 设置文本样式 font cv2.FONT_HERSHEY_SIMPLEX font_scale 0.6 thickness 2 # 获取文本尺寸 (text_width, text_height), baseline cv2.getTextSize( label, font, font_scale, thickness ) # 绘制文本背景 cv2.rectangle( image, (x1, y1 - text_height - 5), (x1 text_width, y1), (0, 0, 255), -1 # 填充矩形 ) # 绘制文本 cv2.putText( image, label, (x1, y1 - 5), font, font_scale, (255, 255, 255), thickness ) return image4. 完整的高级可视化实现4.1 带圆角的矩形框让我们创建更美观的圆角矩形框def draw_rounded_rect(image, top_left, bottom_right, color, thickness, corner_radius): 绘制圆角矩形 x1, y1 top_left x2, y2 bottom_right # 绘制四个角 cv2.ellipse(image, (x1 corner_radius, y1 corner_radius), (corner_radius, corner_radius), 180, 0, 90, color, thickness) cv2.ellipse(image, (x2 - corner_radius, y1 corner_radius), (corner_radius, corner_radius), 270, 0, 90, color, thickness) cv2.ellipse(image, (x1 corner_radius, y2 - corner_radius), (corner_radius, corner_radius), 90, 0, 90, color, thickness) cv2.ellipse(image, (x2 - corner_radius, y2 - corner_radius), (corner_radius, corner_radius), 0, 0, 90, color, thickness) # 绘制四条边 cv2.line(image, (x1 corner_radius, y1), (x2 - corner_radius, y1), color, thickness) cv2.line(image, (x1, y1 corner_radius), (x1, y2 - corner_radius), color, thickness) cv2.line(image, (x1 corner_radius, y2), (x2 - corner_radius, y2), color, thickness) cv2.line(image, (x2, y1 corner_radius), (x2, y2 - corner_radius), color, thickness) return image4.2 完整的高级可视化函数def visualize_detections_advanced(image, detections, confidence_threshold0.5): 高级检测结果可视化 for detection in detections[boxes]: if detection[4] confidence_threshold: continue x1, y1, x2, y2 detection[:4].astype(int) confidence detection[4] # 根据置信度调整框的颜色 color_intensity int(confidence * 255) color (0, 0, color_intensity) # 从深蓝到亮红 # 绘制圆角矩形框 draw_rounded_rect(image, (x1, y1), (x2, y2), color, 3, 10) # 准备标签文本 label fPhone: {confidence:.1%} # 设置文本样式 font cv2.FONT_HERSHEY_DUPLEX font_scale 0.7 thickness 2 # 获取文本尺寸 (text_width, text_height), baseline cv2.getTextSize( label, font, font_scale, thickness ) # 标签位置框上方 label_y max(y1 - 10, text_height 5) # 绘制标签背景带圆角 bg_top_left (x1, label_y - text_height - 5) bg_bottom_right (x1 text_width 10, label_y 5) draw_rounded_rect(image, bg_top_left, bg_bottom_right, color, -1, 5) # 绘制文本 cv2.putText( image, label, (x1 5, label_y - 5), font, font_scale, (255, 255, 255), thickness ) # 在框的右下角添加小置信度标签 small_label f{confidence:.0%} cv2.putText( image, small_label, (x2 - 30, y2 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1 ) return image5. 实际应用示例5.1 完整的使用流程def main(): # 初始化检测器 detector PhoneDetector() # 检测手机 image_path test_image.jpg image, detections detector.detect_phones(image_path) # 可视化结果 result_image visualize_detections_advanced(image, detections) # 保存结果 cv2.imwrite(result_image.jpg, result_image) # 显示结果 cv2.imshow(Phone Detection Result, result_image) cv2.waitKey(0) cv2.destroyAllWindows() if __name__ __main__: main()5.2 批量处理示例如果你需要处理多张图片def process_batch_images(image_paths): detector PhoneDetector() for image_path in image_paths: try: image, detections detector.detect_phones(image_path) result_image visualize_detections_advanced(image, detections) # 保存结果 output_path fresults/{os.path.basename(image_path)} cv2.imwrite(output_path, result_image) print(fProcessed: {image_path}) except Exception as e: print(fError processing {image_path}: {str(e)})6. 效果优化与自定义6.1 自定义颜色方案你可以根据置信度创建颜色渐变效果def get_color_by_confidence(confidence): 根据置信度获取颜色从红到绿渐变 if confidence 0.5: # 红色到黄色渐变 r 255 g int(255 * (confidence * 2)) b 0 else: # 黄色到绿色渐变 r int(255 * (2 - confidence * 2)) g 255 b 0 return (b, g, r) # OpenCV使用BGR格式6.2 添加动画效果可选对于实时检测系统你可以添加简单的动画效果def draw_pulsing_box(image, box, confidence, frame_count): 绘制有脉动效果的检测框 x1, y1, x2, y2 box.astype(int) # 创建脉动效果大小轻微变化 pulse 1 0.1 * np.sin(frame_count * 0.1) center_x, center_y (x1 x2) // 2, (y1 y2) // 2 width, height (x2 - x1), (y2 - y1) new_x1 center_x - int(width * pulse / 2) new_y1 center_y - int(height * pulse / 2) new_x2 center_x int(width * pulse / 2) new_y2 center_y int(height * pulse / 2) # 绘制脉动框 color get_color_by_confidence(confidence) cv2.rectangle(image, (new_x1, new_y1), (new_x2, new_y2), color, 2) return image7. 总结通过本教程你学会了如何使用OpenCV创建专业的手机检测可视化效果。关键要点包括基础矩形框绘制使用cv2.rectangle绘制检测框文本标签添加使用cv2.putText显示置信度信息高级视觉效果圆角矩形、颜色渐变、背景色块等自定义样式根据需求调整颜色、大小、位置等参数这些技巧不仅适用于手机检测也可以应用到其他目标检测任务中。通过良好的可视化效果你可以让检测结果更加直观和易于理解。记得在实际项目中根据具体需求调整参数和样式找到最适合你应用场景的可视化方案。良好的可视化不仅能提升用户体验也能帮助调试和优化检测算法。获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。
手机检测结果可视化进阶:OpenCV绘制带置信度标签的红框教程
发布时间:2026/5/25 7:37:00
手机检测结果可视化进阶OpenCV绘制带置信度标签的红框教程1. 项目背景与需求在实际的手机检测项目中仅仅检测出手机位置是不够的。我们还需要清晰地展示检测结果让用户能够直观地看到每个检测到的手机位置以及对应的置信度。这就是为什么我们需要在检测框上添加标签信息。传统的检测框绘制往往只显示一个简单的矩形框但这样的展示方式信息量有限。通过OpenCV的高级绘图功能我们可以创建更加专业和美观的可视化效果包括不同颜色的检测框红色表示手机检测显示每个检测结果的置信度百分比添加背景色块提高文字可读性自定义标签样式和位置本教程将基于DAMO-YOLO手机检测系统教你如何使用OpenCV实现专业的检测结果可视化。2. 环境准备与基础代码2.1 安装所需库首先确保你已经安装了必要的Python库pip install opencv-python numpy pillow2.2 基础检测代码以下是手机检测的基础代码框架import cv2 import numpy as np from modelscope.pipelines import pipeline from modelscope.utils.constant import Tasks class PhoneDetector: def __init__(self): # 初始化DAMO-YOLO检测模型 self.detector pipeline( Tasks.domain_specific_object_detection, modeldamo/cv_tinynas_object-detection_damoyolo_phone ) def detect_phones(self, image_path): # 读取图像 image cv2.imread(image_path) # 执行检测 result self.detector(image_path) return image, result3. OpenCV高级绘图技巧3.1 基础矩形框绘制让我们从最简单的红色矩形框开始def draw_basic_boxes(image, detections): for detection in detections[boxes]: x1, y1, x2, y2 detection[:4].astype(int) confidence detection[4] # 绘制红色矩形框 cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255), 2) return image3.2 添加置信度标签简单的矩形框还不够我们需要添加置信度信息def draw_boxes_with_labels(image, detections): for detection in detections[boxes]: x1, y1, x2, y2 detection[:4].astype(int) confidence detection[4] # 绘制矩形框 cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255), 2) # 准备标签文本 label fphone: {confidence:.1%} # 设置文本样式 font cv2.FONT_HERSHEY_SIMPLEX font_scale 0.6 thickness 2 # 获取文本尺寸 (text_width, text_height), baseline cv2.getTextSize( label, font, font_scale, thickness ) # 绘制文本背景 cv2.rectangle( image, (x1, y1 - text_height - 5), (x1 text_width, y1), (0, 0, 255), -1 # 填充矩形 ) # 绘制文本 cv2.putText( image, label, (x1, y1 - 5), font, font_scale, (255, 255, 255), thickness ) return image4. 完整的高级可视化实现4.1 带圆角的矩形框让我们创建更美观的圆角矩形框def draw_rounded_rect(image, top_left, bottom_right, color, thickness, corner_radius): 绘制圆角矩形 x1, y1 top_left x2, y2 bottom_right # 绘制四个角 cv2.ellipse(image, (x1 corner_radius, y1 corner_radius), (corner_radius, corner_radius), 180, 0, 90, color, thickness) cv2.ellipse(image, (x2 - corner_radius, y1 corner_radius), (corner_radius, corner_radius), 270, 0, 90, color, thickness) cv2.ellipse(image, (x1 corner_radius, y2 - corner_radius), (corner_radius, corner_radius), 90, 0, 90, color, thickness) cv2.ellipse(image, (x2 - corner_radius, y2 - corner_radius), (corner_radius, corner_radius), 0, 0, 90, color, thickness) # 绘制四条边 cv2.line(image, (x1 corner_radius, y1), (x2 - corner_radius, y1), color, thickness) cv2.line(image, (x1, y1 corner_radius), (x1, y2 - corner_radius), color, thickness) cv2.line(image, (x1 corner_radius, y2), (x2 - corner_radius, y2), color, thickness) cv2.line(image, (x2, y1 corner_radius), (x2, y2 - corner_radius), color, thickness) return image4.2 完整的高级可视化函数def visualize_detections_advanced(image, detections, confidence_threshold0.5): 高级检测结果可视化 for detection in detections[boxes]: if detection[4] confidence_threshold: continue x1, y1, x2, y2 detection[:4].astype(int) confidence detection[4] # 根据置信度调整框的颜色 color_intensity int(confidence * 255) color (0, 0, color_intensity) # 从深蓝到亮红 # 绘制圆角矩形框 draw_rounded_rect(image, (x1, y1), (x2, y2), color, 3, 10) # 准备标签文本 label fPhone: {confidence:.1%} # 设置文本样式 font cv2.FONT_HERSHEY_DUPLEX font_scale 0.7 thickness 2 # 获取文本尺寸 (text_width, text_height), baseline cv2.getTextSize( label, font, font_scale, thickness ) # 标签位置框上方 label_y max(y1 - 10, text_height 5) # 绘制标签背景带圆角 bg_top_left (x1, label_y - text_height - 5) bg_bottom_right (x1 text_width 10, label_y 5) draw_rounded_rect(image, bg_top_left, bg_bottom_right, color, -1, 5) # 绘制文本 cv2.putText( image, label, (x1 5, label_y - 5), font, font_scale, (255, 255, 255), thickness ) # 在框的右下角添加小置信度标签 small_label f{confidence:.0%} cv2.putText( image, small_label, (x2 - 30, y2 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1 ) return image5. 实际应用示例5.1 完整的使用流程def main(): # 初始化检测器 detector PhoneDetector() # 检测手机 image_path test_image.jpg image, detections detector.detect_phones(image_path) # 可视化结果 result_image visualize_detections_advanced(image, detections) # 保存结果 cv2.imwrite(result_image.jpg, result_image) # 显示结果 cv2.imshow(Phone Detection Result, result_image) cv2.waitKey(0) cv2.destroyAllWindows() if __name__ __main__: main()5.2 批量处理示例如果你需要处理多张图片def process_batch_images(image_paths): detector PhoneDetector() for image_path in image_paths: try: image, detections detector.detect_phones(image_path) result_image visualize_detections_advanced(image, detections) # 保存结果 output_path fresults/{os.path.basename(image_path)} cv2.imwrite(output_path, result_image) print(fProcessed: {image_path}) except Exception as e: print(fError processing {image_path}: {str(e)})6. 效果优化与自定义6.1 自定义颜色方案你可以根据置信度创建颜色渐变效果def get_color_by_confidence(confidence): 根据置信度获取颜色从红到绿渐变 if confidence 0.5: # 红色到黄色渐变 r 255 g int(255 * (confidence * 2)) b 0 else: # 黄色到绿色渐变 r int(255 * (2 - confidence * 2)) g 255 b 0 return (b, g, r) # OpenCV使用BGR格式6.2 添加动画效果可选对于实时检测系统你可以添加简单的动画效果def draw_pulsing_box(image, box, confidence, frame_count): 绘制有脉动效果的检测框 x1, y1, x2, y2 box.astype(int) # 创建脉动效果大小轻微变化 pulse 1 0.1 * np.sin(frame_count * 0.1) center_x, center_y (x1 x2) // 2, (y1 y2) // 2 width, height (x2 - x1), (y2 - y1) new_x1 center_x - int(width * pulse / 2) new_y1 center_y - int(height * pulse / 2) new_x2 center_x int(width * pulse / 2) new_y2 center_y int(height * pulse / 2) # 绘制脉动框 color get_color_by_confidence(confidence) cv2.rectangle(image, (new_x1, new_y1), (new_x2, new_y2), color, 2) return image7. 总结通过本教程你学会了如何使用OpenCV创建专业的手机检测可视化效果。关键要点包括基础矩形框绘制使用cv2.rectangle绘制检测框文本标签添加使用cv2.putText显示置信度信息高级视觉效果圆角矩形、颜色渐变、背景色块等自定义样式根据需求调整颜色、大小、位置等参数这些技巧不仅适用于手机检测也可以应用到其他目标检测任务中。通过良好的可视化效果你可以让检测结果更加直观和易于理解。记得在实际项目中根据具体需求调整参数和样式找到最适合你应用场景的可视化方案。良好的可视化不仅能提升用户体验也能帮助调试和优化检测算法。获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。