This commit is contained in:
2025-09-03 13:52:24 +08:00
parent 8cb8e5f935
commit eb5cf715ec
5 changed files with 273 additions and 97 deletions

View File

@ -157,3 +157,77 @@ class OCRViolationDetector:
# 返回检测结果(是否有违禁词、所有违禁词列表、对应置信度列表)
return len(all_prohibited) > 0, all_prohibited, all_confidences
# def test_image(self, image_path: str, show_image: bool = True) -> tuple:
# """
# 对单张图片进行OCR违禁词检测并展示结果
#
# Args:
# image_path (str): 图片文件路径
# show_image (bool): 是否显示图片默认为True
#
# Returns:
# tuple: (是否有违禁词, 违禁词列表, 对应的置信度列表)
# """
# # 检查图片文件是否存在
# if not os.path.exists(image_path):
# self.logger.error(f"图片文件不存在: {image_path}")
# return False, [], []
#
# try:
# # 读取图片
# frame = cv2.imread(image_path)
# if frame is None:
# self.logger.error(f"无法读取图片: {image_path}")
# return False, [], []
#
# self.logger.info(f"开始处理图片: {image_path}")
#
# # 调用检测方法
# has_violation, violations, confidences = self.detect(frame)
#
# # 输出检测结果
# if has_violation:
# self.logger.info(f"在图片中检测到 {len(violations)} 个违禁词:")
# for word, conf in zip(violations, confidences):
# self.logger.info(f"- {word} (置信度: {conf:.4f})")
# else:
# self.logger.info("图片中未检测到违禁词")
# # 显示图片(如果需要)
# if show_image:
# # 调整图片大小以便于显示(如果太大)
# height, width = frame.shape[:2]
# max_size = 800
# if max(height, width) > max_size:
# scale = max_size / max(height, width)
# frame = cv2.resize(frame, None, fx=scale, fy=scale)
#
# cv2.imshow(f"OCR检测结果: {'发现违禁词' if has_violation else '未发现违禁词'}", frame)
# cv2.waitKey(0) # 等待用户按键
# cv2.destroyAllWindows()
#
# return has_violation, violations, confidences
#
# except Exception as e:
# self.logger.error(f"处理图片时发生错误: {str(e)}", exc_info=True)
# return False, [], []
#
#
# # 使用示例
# if __name__ == "__main__":
# # 配置参数
# forbidden_words_path = "forbidden_words.txt" # 违禁词文件路径
# test_image_path = r"D:\Git\bin\video\ocr\images\img_7.png" # 测试图片路径
# ocr_threshold = 0.6 # OCR置信度阈值
#
# # 创建检测器实例
# detector = OCRViolationDetector(
# forbidden_words_path=forbidden_words_path,
# ocr_confidence_threshold=ocr_threshold,
# log_level=logging.INFO,
# log_file="ocr_detection.log"
# )
#
# # 测试图片
# detector.test_image(test_image_path, show_image=True)