Views
No views yet
1from rapidocr_onnxruntime import RapidOCR
2from huggingface_hub import hf_hub_download
3import cv2
4
5def draw_boxes_cv(image_path, ocr_result, out_path="boxed_cv.png"):
6 img = cv2.imread(image_path)
7
8 for line in ocr_result:
9 box = line[0]
10 xs = [p[0] for p in box]
11 ys = [p[1] for p in box]
12
13 pt1 = (int(min(xs)), int(min(ys)))
14 pt2 = (int(max(xs)), int(max(ys)))
15
16 cv2.rectangle(img, pt1, pt2, (0, 0, 255), 2)
17
18 cv2.imwrite(out_path, img)
19
20# Model paths
21det_path = "<path_to_model>/ppocrv5_det.onnx"
22rec_path = "<path_to_model>/ppocrv5_rec.onnx"
23dict_path = "<path_to_dictionary>" # E.g. hf_hub_download("monkt/paddleocr-onnx", "languages/chinese/dict.txt")
24
25# Initialize OCR model
26ocr = RapidOCR(
27 det_model_path=det_path,
28 rec_model_path=rec_path,
29 rec_keys_path=dict_path
30)
31
32result, elapsed = ocr("document.png")
33for res in result:
34 print(f"Word: {res[1]} with Probability: {res[2]}")
35
36draw_boxes_cv("document.png", result)