From d9fcd74000b9e5e081a658aeae2e16764d7b9e45 Mon Sep 17 00:00:00 2001 From: raiots Date: Tue, 30 May 2023 21:55:41 +0800 Subject: [PATCH] feat: ocr --- ocr.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 单摄像头.py | 1 - 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 ocr.py diff --git a/ocr.py b/ocr.py new file mode 100644 index 0000000..b184de9 --- /dev/null +++ b/ocr.py @@ -0,0 +1,48 @@ +import cv2 +import pytesseract +import numpy as np +from pytesseract import Output + +img_source = cv2.imread('images/coffee.jpg') + + +def get_grayscale(image): + return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) + + +def thresholding(image): + return cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1] + + +def opening(image): + kernel = np.ones((5, 5), np.uint8) + return cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel) + + +def canny(image): + return cv2.Canny(image, 100, 200) + + +gray = get_grayscale(img_source) +thresh = thresholding(gray) +opening = opening(gray) +canny = canny(gray) + +for img in [img_source, gray, thresh, opening, canny]: + d = pytesseract.image_to_data(img, output_type=Output.DICT) + n_boxes = len(d['text']) + + # back to RGB + if len(img.shape) == 2: + img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) + + for i in range(n_boxes): + if int(d['conf'][i]) > 60: + (text, x, y, w, h) = (d['text'][i], d['left'][i], d['top'][i], d['width'][i], d['height'][i]) + # don't show empty text + if text and text.strip() != "": + img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) + img = cv2.putText(img, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 255, 0), 3) + + cv2.imshow('img', img) + cv2.waitKey(0) \ No newline at end of file diff --git a/单摄像头.py b/单摄像头.py index 2f29018..6afa707 100644 --- a/单摄像头.py +++ b/单摄像头.py @@ -5,7 +5,6 @@ @Environment : python 3.10 ''' - import serial import cv2 import numpy as np