AL-2020/coder/rocognizer.py

36 lines
1.1 KiB
Python
Raw Normal View History

import numpy as np
2020-05-20 11:45:55 +02:00
import argparse
import imutils
import cv2
import matplotlib.pyplot as plt
2020-05-30 15:52:48 +02:00
import torch
from PIL import Image
2020-05-20 11:45:55 +02:00
2020-05-30 15:52:48 +02:00
path = "test1.jpg"
2020-05-20 11:45:55 +02:00
2020-05-30 15:52:48 +02:00
img = cv2.imread(path)
2020-05-20 11:45:55 +02:00
2020-05-30 15:52:48 +02:00
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_gray = cv2.GaussianBlur(img_gray, (5, 5), 0)
2020-05-20 11:45:55 +02:00
2020-05-30 15:52:48 +02:00
ret, im_th = cv2.threshold(img_gray, 90, 255, cv2.THRESH_BINARY_INV)
2020-05-20 11:45:55 +02:00
2020-05-30 15:52:48 +02:00
ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
2020-05-20 11:45:55 +02:00
2020-05-30 15:52:48 +02:00
rects = [cv2.boundingRect(ctr) for ctr in ctrs]
2020-05-20 11:45:55 +02:00
2020-05-30 15:52:48 +02:00
for rect in rects:
# Draw the rectangles
cv2.rectangle(img, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 3)
# Make the rectangular region around the digit
leng = int(rect[3] * 1.6)
pt1 = int(rect[1] + rect[3] // 2 - leng // 2)
pt2 = int(rect[0] + rect[2] // 2 - leng // 2)
roi = im_th[pt1:pt1+leng, pt2:pt2+leng]
# Resize the image
roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA)
roi = cv2.dilate(roi, (3, 3))
# Calculate the HOG features
2020-05-30 15:52:48 +02:00
cv2.imshow("Resulting Image with Rectangular ROIs", img)
cv2.waitKey()