import ddddocr
import cv2
from PIL import Image
det = ddddocr.DdddOcr(det=True)
det2 = ddddocr.DdddOcr(beta=True)
with open("download.png", 'rb') as f:
image = f.read()
bboxes = det.detection(image)
# print(bboxes)
im = cv2.imread("download.png")
all = []
for bbox in bboxes:
x1, y1, x2, y2 = bbox
# im = cv2.rectangle(im, (x1, y1), (x2, y2), color=(0, 0, 255), thickness=2)
# 裁剪出框选区域
cropped = im[y1:y2, x1:x2]
# 转换为 RGB 格式(OpenCV 是 BGR)
cropped_rgb = cv2.cvtColor(cropped, cv2.COLOR_BGR2RGB)
# 转换为 PIL 图像
pil_img = Image.fromarray(cropped_rgb)
result = det2.classification(pil_img)
tt = f'{result}: {[x1, y1, x2, y2]}'
all.append(tt)
print(all)
# cv2.imwrite("result.jpg", im)