見cv_imread函數和cv_imwrite函數
import cv2
import os
import matplotlib.pyplot as plt
from paddleocr import PaddleOCR, draw_ocr
import numpy as np
import urllib.parse # Add this import statementfrom txt_get import ImageTextExtractor# 初始化OCR,OCR會自動下載PP-OCRv3檢測器、識別器和角度分類器
ocr = PaddleOCR(use_angle_cls=True)# 指定輸出路徑和字體路徑
# out_path = './output_images' #這里不用out_path了,直接在detect_and_save_people里用output_dir
font = './doc/fonts/simfang.ttf'
font = r'C:\Windows\Fonts\simfang.ttf' # 修改為絕對路徑,并且確定字體存在def cv_imread(file_path):# 使用 np.fromfile 讀取文件字節流file_bytes = np.fromfile(file_path, dtype=np.uint8)# 使用 cv2.imdecode 解碼圖像img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)return imgdef cv_imwrite(file_path, img):"""使用 cv2.imencode 和 Python 內置的文件寫入支持中文文件名保存圖像"""ext = os.path.splitext(file_path)[1]result, encoded_img = cv2.imencode(ext, img)if result:with open(file_path, 'wb') as f:f.write(encoded_img.tobytes())else:raise Exception("圖像編碼失敗")def detect_and_save_people(image_path, output_dir):"""檢測圖像中的文本,如果包含"xxx"關鍵詞,則保存圖像到指定路徑。"""try:extractor = ImageTextExtractor()extracted_text = extractor.extract_text(image_path)if extracted_text:print(f"從圖像中提取到的文本:{extracted_text}")if "xxx" in extracted_text:print(f"檢測到“xxx”關鍵詞,保存圖片: {image_path}")# 創建輸出目錄 (如果不存在)if not os.path.exists(output_dir):os.makedirs(output_dir)# 構建輸出文件名filename = os.path.basename(image_path)output_path = os.path.join(output_dir, filename) # Use original filename without URL encoding# 復制圖片cv2_img = cv_imread(image_path)cv_imwrite(output_path, cv2_img) # Use cv_imwrite instead of cv2.imwriteelse:print(f"未檢測到“xxx”關鍵詞: {image_path}")else:print(f"未檢測到文本: {image_path}")except Exception as e:print(f"處理圖像 {image_path} 時發生錯誤: {e}")def process_directory(root_dir, output_dir):"""遞歸處理指定目錄下的所有圖像,檢測圖像中是否包含"xxx"關鍵詞,如果包含則保存圖像到指定路徑。"""for dirpath, dirnames, filenames in os.walk(root_dir):for filename in filenames:if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')): # 檢查文件是否是圖像image_path = os.path.join(dirpath, filename)detect_and_save_people(image_path, output_dir)if __name__ == '__main__':# 使用示例root_dir = 'xxx' # 替換為你的根目錄output_dir = 'xxx' # 替換為你想要保存圖像的目錄process_directory(root_dir, output_dir)