def Predict(self, ic, option=0): self.LoadData() self.CreateKNN() Target = ic original = Target.img.copy() process = Target.BinaryProcess() final, contours = Target.Draw() NewContours = [] for cnt in contours: [x, y, w, h] = cv2.boundingRect(cnt) cv2.rectangle(original, (x, y), (x + w, y + h), (0, 0, 255), 2) TargetPxs = process[y:y + h, x:x + w] TargetPxsSmall = cv2.resize(TargetPxs, (10, 10)) sample = TargetPxsSmall.reshape((1, 100)) sample = np.float32(sample) retval, results, neigh_resp, dists = self.model.findNearest(sample, k=3) if (results == 1): NewContours.append(cnt) pic = np.zeros((450, 600, 3)).astype(np.uint8) cv2.drawContours(pic, NewContours, -1, (255, 255, 255), 2) if (option == 1): ip.CvShow('Classification', pic) return pic, NewContours
def Predict(self, ic: ImgClass, contours): self.LoadData() self.CreateKNN() Target = ic original = Target.img.copy() process = Target.BinaryProcess() out = np.zeros(Target.img.shape, np.uint8) for cnt in contours: if cv2.contourArea(cnt) > 50: [x, y, w, h] = cv2.boundingRect(cnt) cv2.rectangle(original, (x, y), (x + w, y + h), (0, 0, 255), 2) TargetPxs = process[y:y + h, x:x + w] TargetPxsSmall = cv2.resize(TargetPxs, (10, 10)) sample = TargetPxsSmall.reshape((1, 100)) sample = np.float32(sample) retval, results, neigh_resp, dists = self.model.findNearest( sample, k=1) string = self.Code.deCode(results[0][0]) cv2.putText(out, string, (x, y + h), 0, 1, (0, 255, 0)) ip.CvShow('Out', out)
def __init__(self,ic:ImgClass): self.original=ic.img.copy() img = ic.img.copy() self.resize=cv2.resize(img,(800,600)) bp = ip.Binarization(self.resize,80) p,contours, heirs = cv2.findContours(bp,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) pic = np.zeros(self.resize.shape).astype(np.uint8) cv2.drawContours(pic,contours,-1,(0,0,255),2) ip.CvShow(pic)
def deBug(self, x): im = Image.open('0.jpg') img = ImageTk.PhotoImage(image=im) cvim = cv2.imread('5799.jpg') ip.CvShow('5799', cvim) return img print(self.CurrentFile)
def Url2ic(self): if self.url_src.get(): url = self.url_src.get() cap = cv2.VideoCapture(url) if (cap.isOpened()): ret, img = cap.read() if ret: self.ic = ImgClass('') self.ic.FromUrlSrc(img) self.classfy = Classification(self.ic, self) self.program = Mainsys(self.ic, self) ip.CvShow('N', img)
def __init__(self): r = requests.get(self.url) self.r = r if r.status_code == requests.codes.ok: # 以 BeautifulSoup 解析 HTML 程式碼 soup = BeautifulSoup(r.text, 'html.parser') # 以 CSS 的 class 抓出各類 #print(soup) imgs = soup.find_all('img') print(len(imgs)) for img in imgs: c = img.get('alt') print(c) urlsrc = img.get('src') print(urlsrc) cap = cv2.VideoCapture(urlsrc) if (cap.isOpened()): ret, img = cap.read() if ret: ip.CvShow('N', img) else: print('Wrong Request')
def Trainning(self): Main = self.Main Main.SetConsole('Train!') Target = self.ic original = Target.img.copy() process = Target.BinaryProcess() final, contours = Target.Draw() #area = ip.MappingCnt(contours) if os.path.isfile('CarLicenseSample.data') and os.path.isfile( 'CarLicenseRes.data'): print('LoadData') Main.SetConsole('LoadData!') samples = np.loadtxt('CarLicenseSample.data', np.float32) responses = np.loadtxt('CarLicenseRes.data', np.float32) else: print('NewData') Main.SetConsole('NewData!') samples = np.empty((0, 100)) responses = [] keys = [i for i in range(48, 50)] close = 0 for cnt in contours: original = Target.img.copy() [x, y, w, h] = cv2.boundingRect(cnt) cv2.rectangle(original, (x, y), (x + w, y + h), (0, 0, 255), 2) TargetPxs = process[y:y + h, x:x + w] TargetPxsSmall = cv2.resize(TargetPxs, (10, 10)) cv2.imshow('norm', original) while close == 0: key = cv2.waitKey(100) if key != -1: print(key) if cv2.getWindowProperty('norm', 0) == -1: print('Close') Main.SetConsole('Close!') close = 1 if key == 27: # (escape to quit) cv2.destroyAllWindows() close = 1 print('Esc') Main.SetConsole('Esc!') elif key == 32: print('Skip') Main.SetConsole('Skip!') elif key == ord('s'): ip.CvShow('ic.img', Target.img) Main.SetConsole('Show!') elif key in keys: responses = np.append(responses, int(chr(key))) sample = TargetPxsSmall.reshape((1, 100)) samples = np.append(samples, sample, 0) Main.SetConsole('Add Sample!') break if close == 1: return 0 cv2.destroyAllWindows() responses = np.array(responses, np.float32) responses = responses.reshape((responses.size, 1)) print('training complete') Main.SetConsole('訓練完成,進行存檔!') cv2.destroyAllWindows() np.savetxt('CarLicenseSample.data', samples) np.savetxt('CarLicenseRes.data', responses) return 1
def GetContours(self): if self.ic: pic, contours = self.ic.Draw() ip.CvShow(self.CurrentFile, pic)
def BinaryProcess(self): if self.ic: ip.CvShow(self.CurrentFile, self.ic.BinaryProcess())