def refine_gfimage(img, mask): #time-consuming-mask # Draw BBox using gf bboxes = findBBox(img, mask) if bboxes is None: return mask box_img = np.zeros((img.shape), dtype=np.uint8) #cv2.drawContours(box_img,[bboxes],-1,(255,255,255),1) for bbox in bboxes: bbox_ = resizeBBox(bbox, ratio=1.0) cv2.drawContours(box_img, [bbox_], 0, (255, 255, 255), 1) newmask = cv2.cvtColor(box_img, cv2.COLOR_BGR2GRAY) # #img_contours,contours,hierarchy = cv2.findContours(newmask, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) contours, hierarchy = cv2.findContours(newmask, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) img_contours = np.zeros((img.shape), dtype=np.uint8) if len(contours) == 0: return mask for contour in contours: random.shuffle(contour) cv2.drawContours(img_contours, [contour], 0, (255, 255, 255), 1) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) newmask = cv2.morphologyEx(img_contours[:, :, 0], cv2.MORPH_CLOSE, kernel) return newmask
def detect(self, origin, isdebug=False): start = time.time() # Default Size h, w, c = origin.shape size = 200.0 # Resize img = cv2.resize(origin, (int(w * size / h), int(size))) #showResult("img",img) for case in switch(AnalyzeImageQuality.dayornight(img)): if case('Day'): # Extract Good Features corners = refinedGoodFeatures(origin, img) mask = checkFeatures(img, corners, isdebug) closing = close(mask) refined_gfmask = refine_gfimage(img, closing) #showResult("refined_gfmask",refined_gfmask) finalmasks = mkfinalmasks(img, refined_gfmask, isday=True, isdebug=isdebug) break if case('Night'): finalmasks = mkfinalmasks(img, None, isday=isdebug) break for colrindex, fmask in enumerate(finalmasks): if (fmask > 0).sum() == 0: continue bboxes = mask2plates(img, fmask) # Resize if bboxes is not None: bboxes = resizeBBoxes(bboxes, h / size) rois = BBoxes2ROIs(origin, bboxes) for i, roi in enumerate(rois): confidence = self.licenplatevalidator.process( roi, mode=colrs[colrindex], isdebug=isdebug) print confidence if confidence > 0.7: #pts = self.licenplatevalidator.getRefinedROI() #bbox = refineBBox(bboxes[i],pts) bbox = resizeBBox(bboxes[i], ratio=0.9) print("total elapsed time: " + str(int((time.time() - start) * 1000) / 1000.0) + "s") return confidence, [bbox], [roi] ''' # Check Result if isdebug and bboxes is not None: drawBBox(origin,bboxes,debug=True) for i in range(len(rois)): showResult("cropped",rois[i]) ''' return 0.0, None, None