代码拉取完成,页面将自动刷新
import cv2
import numpy as np
import digits_ann
OPENCV_MAJOR_VERSION = int(cv2.__version__.split('.')[0])
def inside(r1, r2):
x1, y1, w1, h1 = r1
x2, y2, w2, h2 = r2
return (x1 > x2) and (y1 > y2) and (x1+w1 < x2+w2) and \
(y1+h1 < y2+h2)
def wrap_digit(rect, img_w, img_h):
x, y, w, h = rect
x_center = x + w//2
y_center = y + h//2
if (h > w):
w = h
x = x_center - (w//2)
else:
h = w
y = y_center - (h//2)
padding = 5
x -= padding
y -= padding
w += 2 * padding
h += 2 * padding
if x < 0:
x = 0
elif x > img_w:
x = img_w
if y < 0:
y = 0
elif y > img_h:
y = img_h
if x+w > img_w:
w = img_w - x
if y+h > img_h:
h = img_h - y
return x, y, w, h
ann, test_data = digits_ann.train(
digits_ann.create_ann(60), 50000, 10)
img_path = "./digit_images/digits_0.jpg"
img = cv2.imread(img_path, cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.GaussianBlur(gray, (7, 7), 0, gray)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
erode_kernel = np.ones((2, 2), np.uint8)
thresh = cv2.erode(thresh, erode_kernel, thresh, iterations=2)
if OPENCV_MAJOR_VERSION >= 4:
# OpenCV 4 or a later version is being used.
contours, hier = cv2.findContours(thresh, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
else:
# OpenCV 3 or an earlier version is being used.
# cv2.findContours has an extra return value.
# The extra return value is the thresholded image, which is
# unchanged, so we can ignore it.
_, contours, hier = cv2.findContours(thresh, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
rectangles = []
img_h, img_w = img.shape[:2]
img_area = img_w * img_h
for c in contours:
a = cv2.contourArea(c)
if a >= 0.98 * img_area or a <= 0.0001 * img_area:
continue
r = cv2.boundingRect(c)
is_inside = False
for q in rectangles:
if inside(r, q):
is_inside = True
break
if not is_inside:
rectangles.append(r)
for r in rectangles:
x, y, w, h = wrap_digit(r, img_w, img_h)
roi = thresh[y:y+h, x:x+w]
digit_class = int(digits_ann.predict(ann, roi)[0])
cv2.rectangle(img, (x,y), (x+w, y+h), (255, 0, 0), 2)
cv2.putText(img, "%d" % digit_class, (x, y-5),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
cv2.imwrite("detected_and_classified_digits_thresh.png", thresh)
cv2.imwrite("detected_and_classified_digits.png", img)
cv2.imshow("thresh", thresh)
cv2.imshow("detected and classified digits", img)
cv2.waitKey()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。