Update native RC1 samples and supported device documentation
Browse files- README.md +31 -7
- config.json +1 -2
- dlstreamer_sample.py +152 -0
- export_and_quantize.sh +2 -3
- openvino_sample.py +127 -0
README.md
CHANGED
|
@@ -25,13 +25,15 @@ language:
|
|
| 25 |
| **Source Framework** | PaddlePaddle |
|
| 26 |
| **Supported Precisions** | FP32, FP16 |
|
| 27 |
| **Inference Engine** | OpenVINO |
|
| 28 |
-
| **Hardware** | CPU, GPU
|
| 29 |
| **Detected Class(es)** | Text regions + recognized text strings |
|
| 30 |
|
| 31 |
---
|
| 32 |
|
| 33 |
## Overview
|
| 34 |
|
|
|
|
|
|
|
| 35 |
OCR for Text is a Metro Analytics use case that detects and reads text in
|
| 36 |
images and video streams using the PaddleOCR PP-OCRv4 pipeline.
|
| 37 |
It composes two models:
|
|
@@ -62,9 +64,13 @@ includes a specialized plate detector.
|
|
| 62 |
|
| 63 |
## Prerequisites
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
- Python 3.11+
|
| 66 |
- [Install OpenVINO](https://docs.openvino.ai/2026/get-started/install-openvino.html) (latest version)
|
| 67 |
-
- [Install Intel DLStreamer](https://docs.openedgeplatform.intel.com/
|
| 68 |
|
| 69 |
Create and activate a Python virtual environment before running the scripts:
|
| 70 |
|
|
@@ -115,9 +121,18 @@ resize and a dilation step so a whole word is captured in one box instead of
|
|
| 115 |
fragments), then the recognizer reads each cropped region and CTC-decodes it
|
| 116 |
into a text string, which is drawn as a solid-background label directly over
|
| 117 |
its box so the highlighted region visibly shows what is written.
|
| 118 |
-
Change the `device` string to run on CPU
|
|
|
|
|
|
|
| 119 |
|
|
|
|
| 120 |
```python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
import cv2
|
| 122 |
import numpy as np
|
| 123 |
import openvino as ov
|
|
@@ -130,7 +145,7 @@ DET_SIZE = 960
|
|
| 130 |
|
| 131 |
core = ov.Core()
|
| 132 |
|
| 133 |
-
# Change device to "GPU"
|
| 134 |
det_compiled = core.compile_model(core.read_model(DET_MODEL), "CPU")
|
| 135 |
rec_compiled = core.compile_model(core.read_model(REC_MODEL), "CPU")
|
| 136 |
|
|
@@ -240,12 +255,12 @@ writer.release()
|
|
| 240 |
print(f"Total text regions across all frames: {total_regions}", flush=True)
|
| 241 |
print("Saved: output_openvino.mp4")
|
| 242 |
```
|
|
|
|
| 243 |
|
| 244 |
**Device targets:**
|
| 245 |
|
| 246 |
- `"CPU"` -- default, works on all Intel platforms.
|
| 247 |
- `"GPU"` -- Intel integrated or discrete GPU.
|
| 248 |
-
- `"NPU"` -- Intel NPU; PP-OCRv4 FP16 models are NPU-compatible.
|
| 249 |
|
| 250 |
> **Note:** Recognition accuracy depends heavily on font, angle, and image
|
| 251 |
> quality. Plain block-lettered signage (as in the sample video) decodes
|
|
@@ -277,7 +292,16 @@ over its box before writing the annotated output to `output_dlstreamer.mp4`.
|
|
| 277 |
> /opt/intel/dlstreamer/gstreamer/lib/python3/dist-packages:${PYTHONPATH:-}
|
| 278 |
> ```
|
| 279 |
|
|
|
|
|
|
|
|
|
|
| 280 |
```python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
import gi
|
| 282 |
|
| 283 |
gi.require_version("Gst", "1.0")
|
|
@@ -425,12 +449,12 @@ if writer:
|
|
| 425 |
writer.release()
|
| 426 |
print(f"Total text regions across all frames: {total_regions}", flush=True)
|
| 427 |
```
|
|
|
|
| 428 |
|
| 429 |
**Device targets:**
|
| 430 |
|
| 431 |
- `"CPU"` -- default for OpenVINO inference inside the appsink loop.
|
| 432 |
- `"GPU"` -- change `"CPU"` to `"GPU"` in `core.compile_model()`.
|
| 433 |
-
- `"NPU"` -- change `"CPU"` to `"NPU"` in `core.compile_model()`.
|
| 434 |
|
| 435 |
#### Expected Output
|
| 436 |
|
|
@@ -447,4 +471,4 @@ Licensed under the MIT License. See [LICENSE](LICENSE) for details.
|
|
| 447 |
- [PaddleOCR PP-OCRv4](https://github.com/PaddlePaddle/PaddleOCR)
|
| 448 |
- [PaddleOCR OpenVINO Deployment](https://github.com/PaddlePaddle/PaddleOCR/blob/main/deploy/paddle2onnx/readme.md)
|
| 449 |
- [OpenVINO Documentation](https://docs.openvino.ai/)
|
| 450 |
-
- [Intel DLStreamer](https://docs.openedgeplatform.intel.com/
|
|
|
|
| 25 |
| **Source Framework** | PaddlePaddle |
|
| 26 |
| **Supported Precisions** | FP32, FP16 |
|
| 27 |
| **Inference Engine** | OpenVINO |
|
| 28 |
+
| **Hardware** | CPU, GPU |
|
| 29 |
| **Detected Class(es)** | Text regions + recognized text strings |
|
| 30 |
|
| 31 |
---
|
| 32 |
|
| 33 |
## Overview
|
| 34 |
|
| 35 |
+
Supported devices are CPU and GPU only; NPU is not supported.
|
| 36 |
+
|
| 37 |
OCR for Text is a Metro Analytics use case that detects and reads text in
|
| 38 |
images and video streams using the PaddleOCR PP-OCRv4 pipeline.
|
| 39 |
It composes two models:
|
|
|
|
| 64 |
|
| 65 |
## Prerequisites
|
| 66 |
|
| 67 |
+
Target stack: Intel DLStreamer **2026.3.0 RC1** with the matching OpenVINO GenAI bundle **2026.5.0.0.dev20261030** (OpenVINO Runtime **2026.5.0.dev20261030**).
|
| 68 |
+
Use the [native RC1 setup](../README.md#native-rc1-runtime) before running these samples; this is a development stack, not a stable release.
|
| 69 |
+
See the [device validation matrix](../DEVICE_SUPPORT_MATRIX.md) for measured results and device support exclusions.
|
| 70 |
+
|
| 71 |
- Python 3.11+
|
| 72 |
- [Install OpenVINO](https://docs.openvino.ai/2026/get-started/install-openvino.html) (latest version)
|
| 73 |
+
- [Install Intel DLStreamer](https://docs.openedgeplatform.intel.com/dev/edge-ai-libraries/dlstreamer/get_started/install/install_guide_ubuntu.html) (latest version)
|
| 74 |
|
| 75 |
Create and activate a Python virtual environment before running the scripts:
|
| 76 |
|
|
|
|
| 121 |
fragments), then the recognizer reads each cropped region and CTC-decodes it
|
| 122 |
into a text string, which is drawn as a solid-background label directly over
|
| 123 |
its box so the highlighted region visibly shows what is written.
|
| 124 |
+
Change the `device` string to run on CPU or GPU.
|
| 125 |
+
|
| 126 |
+
The full sample is in [openvino_sample.py](openvino_sample.py); run it with `python3 openvino_sample.py` or copy the code below.
|
| 127 |
|
| 128 |
+
<!-- sample: openvino_sample.py -->
|
| 129 |
```python
|
| 130 |
+
# SPDX-License-Identifier: MIT
|
| 131 |
+
# Copyright (C) Intel Corporation
|
| 132 |
+
#
|
| 133 |
+
# OCR for Text: OpenVINO Runtime sample.
|
| 134 |
+
# See README.md for prerequisites and inputs. Run: python3 openvino_sample.py
|
| 135 |
+
|
| 136 |
import cv2
|
| 137 |
import numpy as np
|
| 138 |
import openvino as ov
|
|
|
|
| 145 |
|
| 146 |
core = ov.Core()
|
| 147 |
|
| 148 |
+
# Change device to "GPU" to run on integrated GPU.
|
| 149 |
det_compiled = core.compile_model(core.read_model(DET_MODEL), "CPU")
|
| 150 |
rec_compiled = core.compile_model(core.read_model(REC_MODEL), "CPU")
|
| 151 |
|
|
|
|
| 255 |
print(f"Total text regions across all frames: {total_regions}", flush=True)
|
| 256 |
print("Saved: output_openvino.mp4")
|
| 257 |
```
|
| 258 |
+
<!-- /sample -->
|
| 259 |
|
| 260 |
**Device targets:**
|
| 261 |
|
| 262 |
- `"CPU"` -- default, works on all Intel platforms.
|
| 263 |
- `"GPU"` -- Intel integrated or discrete GPU.
|
|
|
|
| 264 |
|
| 265 |
> **Note:** Recognition accuracy depends heavily on font, angle, and image
|
| 266 |
> quality. Plain block-lettered signage (as in the sample video) decodes
|
|
|
|
| 292 |
> /opt/intel/dlstreamer/gstreamer/lib/python3/dist-packages:${PYTHONPATH:-}
|
| 293 |
> ```
|
| 294 |
|
| 295 |
+
The full sample is in [dlstreamer_sample.py](dlstreamer_sample.py); run it with `python3 dlstreamer_sample.py` or copy the code below.
|
| 296 |
+
|
| 297 |
+
<!-- sample: dlstreamer_sample.py -->
|
| 298 |
```python
|
| 299 |
+
# SPDX-License-Identifier: MIT
|
| 300 |
+
# Copyright (C) Intel Corporation
|
| 301 |
+
#
|
| 302 |
+
# OCR for Text: DLStreamer pipeline sample.
|
| 303 |
+
# See README.md for prerequisites and inputs. Run: python3 dlstreamer_sample.py
|
| 304 |
+
|
| 305 |
import gi
|
| 306 |
|
| 307 |
gi.require_version("Gst", "1.0")
|
|
|
|
| 449 |
writer.release()
|
| 450 |
print(f"Total text regions across all frames: {total_regions}", flush=True)
|
| 451 |
```
|
| 452 |
+
<!-- /sample -->
|
| 453 |
|
| 454 |
**Device targets:**
|
| 455 |
|
| 456 |
- `"CPU"` -- default for OpenVINO inference inside the appsink loop.
|
| 457 |
- `"GPU"` -- change `"CPU"` to `"GPU"` in `core.compile_model()`.
|
|
|
|
| 458 |
|
| 459 |
#### Expected Output
|
| 460 |
|
|
|
|
| 471 |
- [PaddleOCR PP-OCRv4](https://github.com/PaddlePaddle/PaddleOCR)
|
| 472 |
- [PaddleOCR OpenVINO Deployment](https://github.com/PaddlePaddle/PaddleOCR/blob/main/deploy/paddle2onnx/readme.md)
|
| 473 |
- [OpenVINO Documentation](https://docs.openvino.ai/)
|
| 474 |
+
- [Intel DLStreamer](https://docs.openedgeplatform.intel.com/dev/edge-ai-libraries/dlstreamer/index.html)
|
config.json
CHANGED
|
@@ -12,8 +12,7 @@
|
|
| 12 |
"inference_engine": "OpenVINO",
|
| 13 |
"hardware": [
|
| 14 |
"CPU",
|
| 15 |
-
"GPU"
|
| 16 |
-
"NPU"
|
| 17 |
],
|
| 18 |
"detected_classes": "Text regions + recognized text strings",
|
| 19 |
"hf_repo_id": "Intel/ocr-text-recognition",
|
|
|
|
| 12 |
"inference_engine": "OpenVINO",
|
| 13 |
"hardware": [
|
| 14 |
"CPU",
|
| 15 |
+
"GPU"
|
|
|
|
| 16 |
],
|
| 17 |
"detected_classes": "Text regions + recognized text strings",
|
| 18 |
"hf_repo_id": "Intel/ocr-text-recognition",
|
dlstreamer_sample.py
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-License-Identifier: MIT
|
| 2 |
+
# Copyright (C) Intel Corporation
|
| 3 |
+
#
|
| 4 |
+
# OCR for Text: DLStreamer pipeline sample.
|
| 5 |
+
# See README.md for prerequisites and inputs. Run: python3 dlstreamer_sample.py
|
| 6 |
+
|
| 7 |
+
import gi
|
| 8 |
+
|
| 9 |
+
gi.require_version("Gst", "1.0")
|
| 10 |
+
from gi.repository import Gst
|
| 11 |
+
|
| 12 |
+
import numpy as np
|
| 13 |
+
import openvino as ov
|
| 14 |
+
|
| 15 |
+
Gst.init([])
|
| 16 |
+
|
| 17 |
+
# Import cv2 after Gst.init to avoid GStreamer re-initialization conflicts.
|
| 18 |
+
import cv2
|
| 19 |
+
|
| 20 |
+
INPUT_VIDEO = "test_video.mp4"
|
| 21 |
+
DET_MODEL = "ch_PP-OCRv4_det_infer/inference.xml"
|
| 22 |
+
REC_MODEL = "ch_PP-OCRv4_rec_server_infer/inference.xml"
|
| 23 |
+
DICT_FILE = "ppocr_keys_v1.txt"
|
| 24 |
+
DET_SIZE = 960
|
| 25 |
+
|
| 26 |
+
core = ov.Core()
|
| 27 |
+
det_compiled = core.compile_model(core.read_model(DET_MODEL), "CPU")
|
| 28 |
+
rec_compiled = core.compile_model(core.read_model(REC_MODEL), "CPU")
|
| 29 |
+
|
| 30 |
+
# CTC label map: index 0 is the blank symbol, followed by every character in
|
| 31 |
+
# the dictionary file, followed by a trailing space character.
|
| 32 |
+
chars = open(DICT_FILE, encoding="utf-8").read().splitlines()
|
| 33 |
+
dict_character = ["blank"] + chars + [" "]
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def detect_text_regions(frame, thresh=0.3, pad=4):
|
| 37 |
+
"""Return (x, y, w, h) boxes for words/lines of text in a frame.
|
| 38 |
+
|
| 39 |
+
Resizing preserves aspect ratio (letterboxed onto a square canvas) so
|
| 40 |
+
text isn't skewed, and dilating the detection map merges nearby
|
| 41 |
+
characters into one box per word instead of one per character.
|
| 42 |
+
"""
|
| 43 |
+
h0, w0 = frame.shape[:2]
|
| 44 |
+
scale = DET_SIZE / max(h0, w0)
|
| 45 |
+
resized = cv2.resize(frame, (int(w0 * scale), int(h0 * scale)))
|
| 46 |
+
canvas = np.zeros((DET_SIZE, DET_SIZE, 3), dtype=np.uint8)
|
| 47 |
+
canvas[:resized.shape[0], :resized.shape[1]] = resized
|
| 48 |
+
|
| 49 |
+
blob = canvas.astype(np.float32).transpose(2, 0, 1)[np.newaxis] / 255.0
|
| 50 |
+
det_map = det_compiled([blob])[det_compiled.output(0)][0, 0]
|
| 51 |
+
binary = (det_map > thresh).astype(np.uint8) * 255
|
| 52 |
+
dilated = cv2.dilate(binary, np.ones((9, 25), np.uint8))
|
| 53 |
+
contours, _ = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 54 |
+
|
| 55 |
+
boxes = []
|
| 56 |
+
for c in contours:
|
| 57 |
+
x, y, w, h = cv2.boundingRect(c)
|
| 58 |
+
if w < 10 or h < 5:
|
| 59 |
+
continue
|
| 60 |
+
x0, y0 = max(0, x / scale - pad), max(0, y / scale - pad)
|
| 61 |
+
x1, y1 = min(w0, (x + w) / scale + pad), min(h0, (y + h) / scale + pad)
|
| 62 |
+
boxes.append((int(x0), int(y0), int(x1 - x0), int(y1 - y0)))
|
| 63 |
+
return boxes
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def recognize_text(crop, rec_h=48, max_w=320):
|
| 67 |
+
"""Resize a cropped text region to the recognizer's input shape and
|
| 68 |
+
CTC-decode the predicted character sequence into a string."""
|
| 69 |
+
h, w = crop.shape[:2]
|
| 70 |
+
if h == 0 or w == 0:
|
| 71 |
+
return "", 0.0
|
| 72 |
+
resized_w = max(1, min(max_w, round(rec_h * w / h)))
|
| 73 |
+
blob = cv2.resize(crop, (resized_w, rec_h)).astype(np.float32) / 255.0
|
| 74 |
+
blob = ((blob - 0.5) / 0.5).transpose(2, 0, 1)[np.newaxis, ...]
|
| 75 |
+
|
| 76 |
+
preds = rec_compiled([blob])[rec_compiled.output(0)][0]
|
| 77 |
+
idx = np.argmax(preds, axis=1)
|
| 78 |
+
conf = np.max(preds, axis=1)
|
| 79 |
+
|
| 80 |
+
text, scores, prev = [], [], -1
|
| 81 |
+
for i, c in zip(idx, conf):
|
| 82 |
+
if i != 0 and i != prev:
|
| 83 |
+
text.append(dict_character[i])
|
| 84 |
+
scores.append(c)
|
| 85 |
+
prev = i
|
| 86 |
+
confidence = float(np.mean(scores)) if scores else 0.0
|
| 87 |
+
return "".join(text), confidence
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def annotate(frame, box, text, confidence):
|
| 91 |
+
"""Draw a bounding box and, if any text was recognized, a legible
|
| 92 |
+
label (solid background so it stays readable over any color) above it."""
|
| 93 |
+
x, y, w, h = box
|
| 94 |
+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
| 95 |
+
if not text:
|
| 96 |
+
return
|
| 97 |
+
label = f"{text} ({confidence:.2f})"
|
| 98 |
+
(tw, th), base = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 2)
|
| 99 |
+
top = max(0, y - th - base - 6)
|
| 100 |
+
cv2.rectangle(frame, (x, top), (x + tw + 6, top + th + base + 6), (0, 255, 0), -1)
|
| 101 |
+
cv2.putText(frame, label, (x + 3, top + th + 2),
|
| 102 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
pipeline_str = (
|
| 106 |
+
f"filesrc location={INPUT_VIDEO} ! decodebin3 ! videoconvert ! "
|
| 107 |
+
"video/x-raw,format=BGR ! "
|
| 108 |
+
"appsink name=sink emit-signals=false sync=false"
|
| 109 |
+
)
|
| 110 |
+
pipeline = Gst.parse_launch(pipeline_str)
|
| 111 |
+
sink = pipeline.get_by_name("sink")
|
| 112 |
+
pipeline.set_state(Gst.State.PLAYING)
|
| 113 |
+
|
| 114 |
+
writer = None
|
| 115 |
+
frame_idx = 0
|
| 116 |
+
total_regions = 0
|
| 117 |
+
|
| 118 |
+
while True:
|
| 119 |
+
sample = sink.emit("pull-sample")
|
| 120 |
+
if sample is None:
|
| 121 |
+
break
|
| 122 |
+
buf = sample.get_buffer()
|
| 123 |
+
caps = sample.get_caps().get_structure(0)
|
| 124 |
+
width = caps.get_value("width")
|
| 125 |
+
height = caps.get_value("height")
|
| 126 |
+
|
| 127 |
+
ok, mapinfo = buf.map(Gst.MapFlags.READ)
|
| 128 |
+
if not ok:
|
| 129 |
+
continue
|
| 130 |
+
frame = np.ndarray((height, width, 3), dtype=np.uint8,
|
| 131 |
+
buffer=mapinfo.data).copy()
|
| 132 |
+
buf.unmap(mapinfo)
|
| 133 |
+
frame_idx += 1
|
| 134 |
+
|
| 135 |
+
for box in detect_text_regions(frame):
|
| 136 |
+
x, y, w, h = box
|
| 137 |
+
text, confidence = recognize_text(frame[y:y + h, x:x + w])
|
| 138 |
+
annotate(frame, box, text, confidence)
|
| 139 |
+
total_regions += 1
|
| 140 |
+
print(f"Frame {frame_idx}: region=({x},{y},{w},{h}) text={text!r} "
|
| 141 |
+
f"confidence={confidence:.2f}", flush=True)
|
| 142 |
+
|
| 143 |
+
if writer is None:
|
| 144 |
+
writer = cv2.VideoWriter(
|
| 145 |
+
"output_dlstreamer.mp4", cv2.VideoWriter_fourcc(*"mp4v"),
|
| 146 |
+
30.0, (width, height))
|
| 147 |
+
writer.write(frame)
|
| 148 |
+
|
| 149 |
+
pipeline.set_state(Gst.State.NULL)
|
| 150 |
+
if writer:
|
| 151 |
+
writer.release()
|
| 152 |
+
print(f"Total text regions across all frames: {total_regions}", flush=True)
|
export_and_quantize.sh
CHANGED
|
@@ -8,9 +8,6 @@
|
|
| 8 |
|
| 9 |
set -euo pipefail
|
| 10 |
|
| 11 |
-
echo "--- Installing dependencies ---"
|
| 12 |
-
pip install -qU openvino
|
| 13 |
-
|
| 14 |
# Ask for approval before downloading models and sample files
|
| 15 |
echo ""
|
| 16 |
echo "This script will download:"
|
|
@@ -34,6 +31,8 @@ else
|
|
| 34 |
echo "WARNING: HuggingFace tracking ping failed (offline?); continuing." >&2
|
| 35 |
fi
|
| 36 |
echo ""
|
|
|
|
|
|
|
| 37 |
|
| 38 |
DET_URL="https://paddleocr.bj.bcebos.com/PP-OCRv4/chinese/ch_PP-OCRv4_det_infer.tar"
|
| 39 |
# The larger "server" recognition model is noticeably more accurate than the
|
|
|
|
| 8 |
|
| 9 |
set -euo pipefail
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
# Ask for approval before downloading models and sample files
|
| 12 |
echo ""
|
| 13 |
echo "This script will download:"
|
|
|
|
| 31 |
echo "WARNING: HuggingFace tracking ping failed (offline?); continuing." >&2
|
| 32 |
fi
|
| 33 |
echo ""
|
| 34 |
+
echo "--- Installing dependencies ---"
|
| 35 |
+
pip install -qU openvino
|
| 36 |
|
| 37 |
DET_URL="https://paddleocr.bj.bcebos.com/PP-OCRv4/chinese/ch_PP-OCRv4_det_infer.tar"
|
| 38 |
# The larger "server" recognition model is noticeably more accurate than the
|
openvino_sample.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-License-Identifier: MIT
|
| 2 |
+
# Copyright (C) Intel Corporation
|
| 3 |
+
#
|
| 4 |
+
# OCR for Text: OpenVINO Runtime sample.
|
| 5 |
+
# See README.md for prerequisites and inputs. Run: python3 openvino_sample.py
|
| 6 |
+
|
| 7 |
+
import cv2
|
| 8 |
+
import numpy as np
|
| 9 |
+
import openvino as ov
|
| 10 |
+
|
| 11 |
+
DET_MODEL = "ch_PP-OCRv4_det_infer/inference.xml"
|
| 12 |
+
REC_MODEL = "ch_PP-OCRv4_rec_server_infer/inference.xml"
|
| 13 |
+
DICT_FILE = "ppocr_keys_v1.txt"
|
| 14 |
+
INPUT_VIDEO = "test_video.mp4"
|
| 15 |
+
DET_SIZE = 960
|
| 16 |
+
|
| 17 |
+
core = ov.Core()
|
| 18 |
+
|
| 19 |
+
# Change device to "GPU" to run on integrated GPU.
|
| 20 |
+
det_compiled = core.compile_model(core.read_model(DET_MODEL), "CPU")
|
| 21 |
+
rec_compiled = core.compile_model(core.read_model(REC_MODEL), "CPU")
|
| 22 |
+
|
| 23 |
+
# CTC label map: index 0 is the blank symbol, followed by every character in
|
| 24 |
+
# the dictionary file, followed by a trailing space character.
|
| 25 |
+
chars = open(DICT_FILE, encoding="utf-8").read().splitlines()
|
| 26 |
+
dict_character = ["blank"] + chars + [" "]
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def detect_text_regions(frame, thresh=0.3, pad=4):
|
| 30 |
+
"""Return (x, y, w, h) boxes for words/lines of text in a frame.
|
| 31 |
+
|
| 32 |
+
Resizing preserves aspect ratio (letterboxed onto a square canvas) so
|
| 33 |
+
text isn't skewed, and dilating the detection map merges nearby
|
| 34 |
+
characters into one box per word instead of one per character.
|
| 35 |
+
"""
|
| 36 |
+
h0, w0 = frame.shape[:2]
|
| 37 |
+
scale = DET_SIZE / max(h0, w0)
|
| 38 |
+
resized = cv2.resize(frame, (int(w0 * scale), int(h0 * scale)))
|
| 39 |
+
canvas = np.zeros((DET_SIZE, DET_SIZE, 3), dtype=np.uint8)
|
| 40 |
+
canvas[:resized.shape[0], :resized.shape[1]] = resized
|
| 41 |
+
|
| 42 |
+
blob = canvas.astype(np.float32).transpose(2, 0, 1)[np.newaxis] / 255.0
|
| 43 |
+
det_map = det_compiled([blob])[det_compiled.output(0)][0, 0]
|
| 44 |
+
binary = (det_map > thresh).astype(np.uint8) * 255
|
| 45 |
+
dilated = cv2.dilate(binary, np.ones((9, 25), np.uint8))
|
| 46 |
+
contours, _ = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 47 |
+
|
| 48 |
+
boxes = []
|
| 49 |
+
for c in contours:
|
| 50 |
+
x, y, w, h = cv2.boundingRect(c)
|
| 51 |
+
if w < 10 or h < 5:
|
| 52 |
+
continue
|
| 53 |
+
x0, y0 = max(0, x / scale - pad), max(0, y / scale - pad)
|
| 54 |
+
x1, y1 = min(w0, (x + w) / scale + pad), min(h0, (y + h) / scale + pad)
|
| 55 |
+
boxes.append((int(x0), int(y0), int(x1 - x0), int(y1 - y0)))
|
| 56 |
+
return boxes
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def recognize_text(crop, rec_h=48, max_w=320):
|
| 60 |
+
"""Resize a cropped text region to the recognizer's input shape and
|
| 61 |
+
CTC-decode the predicted character sequence into a string."""
|
| 62 |
+
h, w = crop.shape[:2]
|
| 63 |
+
if h == 0 or w == 0:
|
| 64 |
+
return "", 0.0
|
| 65 |
+
resized_w = max(1, min(max_w, round(rec_h * w / h)))
|
| 66 |
+
blob = cv2.resize(crop, (resized_w, rec_h)).astype(np.float32) / 255.0
|
| 67 |
+
blob = ((blob - 0.5) / 0.5).transpose(2, 0, 1)[np.newaxis, ...]
|
| 68 |
+
|
| 69 |
+
preds = rec_compiled([blob])[rec_compiled.output(0)][0]
|
| 70 |
+
idx = np.argmax(preds, axis=1)
|
| 71 |
+
conf = np.max(preds, axis=1)
|
| 72 |
+
|
| 73 |
+
text, scores, prev = [], [], -1
|
| 74 |
+
for i, c in zip(idx, conf):
|
| 75 |
+
if i != 0 and i != prev:
|
| 76 |
+
text.append(dict_character[i])
|
| 77 |
+
scores.append(c)
|
| 78 |
+
prev = i
|
| 79 |
+
confidence = float(np.mean(scores)) if scores else 0.0
|
| 80 |
+
return "".join(text), confidence
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def annotate(frame, box, text, confidence):
|
| 84 |
+
"""Draw a bounding box and, if any text was recognized, a legible
|
| 85 |
+
label (solid background so it stays readable over any color) above it."""
|
| 86 |
+
x, y, w, h = box
|
| 87 |
+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
| 88 |
+
if not text:
|
| 89 |
+
return
|
| 90 |
+
label = f"{text} ({confidence:.2f})"
|
| 91 |
+
(tw, th), base = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 2)
|
| 92 |
+
top = max(0, y - th - base - 6)
|
| 93 |
+
cv2.rectangle(frame, (x, top), (x + tw + 6, top + th + base + 6), (0, 255, 0), -1)
|
| 94 |
+
cv2.putText(frame, label, (x + 3, top + th + 2),
|
| 95 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
cap = cv2.VideoCapture(INPUT_VIDEO)
|
| 99 |
+
fps = cap.get(cv2.CAP_PROP_FPS) or 30.0
|
| 100 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 101 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 102 |
+
writer = cv2.VideoWriter(
|
| 103 |
+
"output_openvino.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))
|
| 104 |
+
|
| 105 |
+
frame_idx = 0
|
| 106 |
+
total_regions = 0
|
| 107 |
+
|
| 108 |
+
while True:
|
| 109 |
+
ok, frame = cap.read()
|
| 110 |
+
if not ok:
|
| 111 |
+
break
|
| 112 |
+
frame_idx += 1
|
| 113 |
+
|
| 114 |
+
for box in detect_text_regions(frame):
|
| 115 |
+
x, y, w, h = box
|
| 116 |
+
text, confidence = recognize_text(frame[y:y + h, x:x + w])
|
| 117 |
+
annotate(frame, box, text, confidence)
|
| 118 |
+
total_regions += 1
|
| 119 |
+
print(f"Frame {frame_idx}: region=({x},{y},{w},{h}) text={text!r} "
|
| 120 |
+
f"confidence={confidence:.2f}", flush=True)
|
| 121 |
+
|
| 122 |
+
writer.write(frame)
|
| 123 |
+
|
| 124 |
+
cap.release()
|
| 125 |
+
writer.release()
|
| 126 |
+
print(f"Total text regions across all frames: {total_regions}", flush=True)
|
| 127 |
+
print("Saved: output_openvino.mp4")
|