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()
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) cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) roi = thresh[y:y + h, x:x + w] try: digit_class = int(ANN.predict(ann, roi)[0]) except: continue cv2.putText(img, "%d" % digit_class, (x, y - 1), font, 1, (0, 255, 0)) cv2.imshow("thresh", thresh) cv2.imshow("detected and classified digits", img) cv2.imwrite("detected_and_classified_digits.png", img) cv2.waitKey()
rectangles =[] for c in cntrs: r=x,y,w,h=cv2.boundingRect(c) a=cv2.contourArea(c) b=(img.shape[0]-2) *(img.shape[1]-3) is_inside = False for q in rectangles: if inside(r,q): is_inside=True break if not is_inside: if not a == b: rectangles.append(r) for r in rectangles: x,y,w,h=wrap_digit(r) cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2) roi=thbw[y:y+h,x:x+w] try: digit_class=int(ANN.predict(ann,roi.cope())[0]) except: continue cv2.putText(img,"%d" %digit_class,(x,y-1),font,1,(0,255,0)) cv2.imshow("thbw",thbw) cv2.imshow("countours",img) cv2.imwrite("sample.jpg",img) cv2.waitKey()
b = (img.shape[0] - 3) * (img.shape[1] - 3) is_inside = False for q in rectangles: if inside(r, q): is_inside = True break if not is_inside: if not a == b: rectangles.append(r) for r in rectangles: x, y, w, h = wrap_digit(r) x = int(x) y = int(y) w = int(w) h = int(h) cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) roi = thbw[y:y + h, x:x + h] try: digital_class = int(ANN.predict(ann, roi.copy())[0]) except: continue cv2.putText(img, "%d" % digital_class, (x, y - 1), font, 1, (0, 255, 0)) cv2.namedWindow("thbw", 0) cv2.resizeWindow("thbw", 640, 480) cv2.imshow("thbw", thbw) cv2.namedWindow("counters", 0) cv2.resizeWindow("counters", 640, 480) cv2.imshow("counters", img) cv2.imwrite("sample.jpg", img) cv2.waitKey()