处理单个图像并提取文字
(self, img_path)
| 33 | return 0 |
| 34 | |
| 35 | def process_image(self, img_path): |
| 36 | """处理单个图像并提取文字""" |
| 37 | img = Image.open(img_path) |
| 38 | |
| 39 | if img.size != (1920, 1080): |
| 40 | return None |
| 41 | |
| 42 | # 裁剪图像 |
| 43 | cropped_img = img.crop(self.crop_box) |
| 44 | |
| 45 | # 图像预处理 |
| 46 | img_array = np.array(cropped_img) |
| 47 | mask = np.all(img_array > 245, axis=2) |
| 48 | img_array[mask] = [255, 255, 255] |
| 49 | img_array[~mask] = [0, 0, 0] |
| 50 | |
| 51 | # 转换为PIL图像 |
| 52 | processed_img = Image.fromarray(img_array) |
| 53 | |
| 54 | # 准备OCR |
| 55 | buffer = io.BytesIO() |
| 56 | processed_img.convert('RGB').save(buffer, format='PNG') |
| 57 | image_bytes = buffer.getvalue() |
| 58 | |
| 59 | # OCR识别 |
| 60 | text = self.ocr.classification(image_bytes) |
| 61 | return text.strip() if text else None |
| 62 | |
| 63 | def process_frames(self, input_folder, output_folder): |
| 64 | """处理文件夹中的所有帧并生成字幕""" |