def ocrSegment(self, sharp): print("input image sharp shape:", sharp.shape) # Given a (hopefully) sharpened image, ocr the segment logger.debug(VisualRecord("sharp image ***", sharp, "End image")) img = Image.fromarray(sharp, mode='L') # logger.debug(VisualRecord("Tesseract Input", img, "End image")) # Read off the labels one at a time. Need a margin # since otherwise the cropping is too close for tesseract. with PyTessBaseAPI() as api: # api.Init(".","eng",OEM_DEFAULT) api.SetVariable("tessedit_char_whitelist", self.whiteList) api.SetImage(img) timg = api.GetThresholdedImage() logger.debug( VisualRecord("Tesseract's image ***", timg, "End image")) boxes = api.GetComponentImages(RIL.TEXTLINE, True) i = 0 granularityOfArray = "" yearOfArray = 0 for i, (im, box, _, _) in enumerate(boxes): margin = 5 api.SetRectangle(box['x'], box['y'], box['w'] + margin, box['h'] + margin) croppedSegment = sharp[box['y']:box['y'] + box['h'] + margin, box['x']:box['x'] + box['w'] + margin] ocrResult = api.GetUTF8Text() # Mean confidences conf = api.MeanTextConf() # print("confidences: ", api.AllWordConfidences()) print(ocrResult) # print (dir(api.GetBoxText(0))) print("==>", self.classifyEntry(ocrResult, i)) # Still need to split time units and aggregate # when necessary with date classOfLine = self.classifyEntry(ocrResult, i) if self.granularity in classOfLine: granularityOfArray = classOfLine.split(' ')[1] if self.year in classOfLine: yearOfArray = int(classOfLine.split(' ')[1]) # [classification of line, box info of text, box info, text, # coordinate which we use to find bar if relevant.] self.arrayDict[i] = [ classOfLine, api.GetBoxText(0), box, ocrResult, 0 ] # split, find, etc defined for this. # print(api.GetBoxText(0)) # Letter coordinates tailPrint = "\n" + ocrResult + "end image" logger.debug( VisualRecord("ocrResult", croppedSegment, tailPrint)) print(repr(box)) fixedDates = self.fixDates(granularityOfArray, yearOfArray) self.barHeight(img)
def logMatchesOnBGRImg(img_BGR, matches, title="Matches", text="", step=""): img = img_BGR.copy() for match in matches: cv2.rectangle(img, (match[0], match[1]), (match[2], match[3]), MATCH_COLOR, MATCH_WEIGHT) logging.getLogger("killfeed").debug( VisualRecord(title + step, img, text, fmt="png"))
def take_screenshot(): screen_name = Screenshooter.get_screen_file_name() save_screen_path = os.path.join(Screenshooter.__session_dir, screen_name) Logger.info("Screenshot capture to file " + screen_name) Browser.get_browser().get_driver().save_screenshot(save_screen_path) result_image = Image.open(save_screen_path) Logger.info(VisualRecord(screen_name, result_image))
def show_image(msg, frame): # open the logging file logger = logging.getLogger("visual_logging_example") fh = FileHandler("demo.html", mode="w") # set the logger attributes logger.setLevel(logging.DEBUG) logger.addHandler(fh) logger.debug(VisualRecord(msg, frame, fmt="png"))
def Update(self,NNState,distance,confidence,imageRead = None): # Logging και ενημέρωση των στοιχείων self.count +=1 self.NNState = NNState self.distance = distance self.confidence = confidence self.timeDif = self.curTime - self.lastTime if(self.LogInfo and imageRead is not None and self.count % 2 == 0): logText = f"Count :{self.count} State : {self.State} NN : {self.NNState} conf : {self.confidence:0.2f} dist : {self.distance:0.2f} timeElapsed : {self.timeDif:0.2f}" self.logger.info(VisualRecord(logText,imageRead,str(self.count)))
def vlog(image, title): """ Create an entry in the visual log. Args: image: Image to be stored in the visual log entry. title: Title for the visual log entry. Returns: None. """ logger.debug(VisualRecord(title, image, fmt='png'))
def logScaledTemplateMatchOnBGRImg( img_BGR, template, loc, scale, title="scaled template match (template, match)", text=""): result_img = img_BGR.copy() newH, newW = (math.ceil(template.shape[0] * scale), math.ceil(template.shape[1] * scale)) cv2.rectangle(result_img, (loc[0], loc[1]), (loc[0] + newW, loc[1] + newH), MATCH_COLOR, MATCH_WEIGHT) logger.debug(VisualRecord(title, [template, result_img], text, fmt="png"))
def logCroppedTemplateMatchOnBGRImg( img_BGR, template, loc, cropType, title="scaled template match (template, match)", text=""): result_img = img_BGR.copy() cropped_template = cropIcon(template, cropType) newH, newW = cropped_template.shape[:2] cv2.rectangle(result_img, (loc[0], loc[1]), (loc[0] + newW, loc[1] + newH), MATCH_COLOR, MATCH_WEIGHT) logger.debug(VisualRecord(title, [template, result_img], text, fmt="png"))
def getBestTemplateMatch(img, template): w, h = template.shape[::-1] if w > img.shape[1] or h > img.shape[0]: raise ValueError("template bigger than the img") result = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED) _minVal, _maxVal, minLoc, maxLoc = cv2.minMaxLoc(result, None) result_img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) cv2.rectangle(result_img, (maxLoc[0], maxLoc[1]), (maxLoc[0] + w, maxLoc[1] + h), (100, 100, 255), 3) text = "val = " + str(round(_maxVal, 3)) logger.debug( VisualRecord("Best template match (template, match)", [template, result_img], text, fmt="png")) return (maxLoc[0], maxLoc[1], maxLoc[0] + w, maxLoc[1] + h), _maxVal
def write_log(self, title, image): self.logger.debug(VisualRecord( (title + " = %d" % self.capture.count_frame), image, fmt="png"))
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^') cv_image = cv2.imread('vlogging/tests/lenna.jpg') pil_image = Image.open('vlogging/tests/lenna.jpg') import logging logger = logging.getLogger("demo") fh = FileHandler('test.html', mode="w") logger.setLevel(logging.DEBUG) logger.addHandler(fh) logger.debug( VisualRecord("Hello from OpenCV", cv_image, "This is openCV image", fmt="png", size=(50, 50))) logger.info( VisualRecord("Hello from PIL", pil_image, "This is PIL image", fmt="jpeg", size=(100, 300))) logger.info( VisualRecord("Hello from pylab", fig1, "This is PyLab graph", fmt="png",
def visualOdom(self): cnt = 0 list_img = [] still = True for frame in self.camera.capture_continuous(self.rawCapture, format="bgr", use_video_port=True): if rospy.is_shutdown(): break if cnt>=self.img_d_num and (self.img_debug or self.save_to_disk): print(cnt,self.img_d_num,self.img_debug) print("Debug finished") still=False break cnt+=1 try: start = time.time() image = frame.array img_log = image.copy() gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) smooth = cv2.GaussianBlur(gray, (21, 21), 0) division = cv2.divide(gray, smooth, scale=255) edges = cv2.Canny(division, 100, 150) lines = cv2.HoughLines(edges,1,np.pi/180,120) # print("len lines : ",len(lines)) rhos = np.asarray([]) thetas = np.asarray([]) rhosx = np.asarray([]) thetasx = np.asarray([]) rhosy = np.asarray([]) thetasy = np.asarray([]) for pd in lines: for rho,theta in pd: rhos = np.append(rhos,rho) thetas = np.append(thetas,theta) rhos[np.argwhere(thetas>2.8)] = - rhos[np.argwhere(thetas>2.8)] thetas[np.argwhere(thetas>2.8)] = thetas[np.argwhere(thetas>2.8)] - np.pi idx = thetas.argsort() rhos = rhos[idx] thetas = thetas [idx] grad = np.diff(thetas) id_max = np.argmax(grad) if grad[id_max] > 0.5: # print("2 lines") rhosx = rhos[:id_max+1] thetasx = thetas[:id_max+1] rhosy = rhos[id_max+1:] thetasy = thetas[id_max+1:] else: # print("1 line") rhosy = rhos thetasy = thetas rhox_mean = np.mean(rhosx) thetax_mean = np.mean(thetasx) rhoy_mean = np.mean(rhosy) thetay_mean = np.mean(thetasy) except Exception as e: print(e) self.rawCapture.truncate(0) continue if self.img_debug: if rhosx.size != 0: x2 = rhox_mean + 470* (-np.sin(thetax_mean)) cv2.circle(image, (int(rhox_mean), 0), 5, (255, 255, 255), 5) cv2.circle(image, (int(x2),470 ), 5, (255, 255, 255), 5) if rhosy.size != 0: y2 = rhoy_mean + 630* (-np.cos(thetay_mean)) cv2.circle(image, ( 0, int(rhoy_mean)), 5, (255, 255, 255), 5) cv2.circle(image, ( 630,int(y2)), 5, (255, 255, 255), 5) if rhosx.size != 0 and rhosy.size != 0: a,b = self.findIntersection(int(rhox_mean), 0, int(x2),470 , 0, int(rhoy_mean) , 630,int(y2)) cv2.circle(image, ( int(a),int(b)), 5, (255, 255, 255), 5) if self.save_to_disk or self.img_debug: list_img.append(img_log) msg = Float64MultiArray(data=[rhoy_mean,thetay_mean]) self.pub.publish(msg) end = time.time() self.rawCapture.truncate(0) # print(end - start) if self.img_debug: print("sending debig file") self.logger.debug(VisualRecord(("Detected edges using sigma"), list_img, fmt = "png")) self.scp.put("demo.html",remote_path='/home/kolmogorov/Documents/ROS/artista/remote_logs') print("debug sent") if self.save_to_disk: for cnt,image_name in enumerate(list_img): cv2.imwrite("/home/pi/log_img/img"+str(cnt)+".jpg", image_name) print("Images saved")
# USAGE # python visual_logging_example.py # import the necessary packages from logging import FileHandler from vlogging import VisualRecord import logging import cv2 # open the logging file logger = logging.getLogger("visual_logging_example") fh = FileHandler("demo.html", mode="w") # set the logger attributes logger.setLevel(logging.DEBUG) logger.addHandler(fh) # load our example image and convert it to grayscale image = cv2.imread("lex.jpg") image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # loop over some varying sigma sizes for s in xrange(3, 11, 2): # blur the image and detect edges blurred = cv2.GaussianBlur(image, (s, s), 0) edged = cv2.Canny(blurred, 75, 200) logger.debug( VisualRecord(("Detected edges using sigma = %d" % (s)), [blurred, edged], fmt="png"))
# Find edges edge_segmented_image = cv2.Canny(gray_masked_image, 150, 170) #cv2.imwrite('edge' + str(img_key) + '.png', edge_segmented_image) # Find contours : using RETR_EXTERNAL to fetch only external boundary _, contours, _ = cv2.findContours(edge_segmented_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Find the convex hull object for each contour hull_list = [] for i in range(len(contours)): hull = cv2.convexHull(contours[i]) hull_list.append(hull) contours_poly = [None] * len(contours) boundRect = [None] * len(contours) # print(type(boundRect)) # centers = [None] * len(contours) # radius = [None] * len(contours) for i, c in enumerate(contours): contours_poly[i] = cv2.approxPolyDP(c, 3, True) boundRect[i] = cv2.boundingRect(contours_poly[i]) # Add the timestamp to bounding box parameter list # print(type(boundRect[i])) # list(boundRect[i]) # boundRect[i].append(timestamp) #bb_param_lst_img.append(boundRect[i]) # Add the new bounding box to list of existing bounding boxes for the image logger.debug( VisualRecord("Input Image = %s" % (img_key), [ image_to_be_processed, masked_image, gray_masked_image, edge_segmented_image ], fmt="png"))
def logImg(img, title="", text=""): logging.getLogger("killfeed").debug( VisualRecord(title, img, text, fmt="png"))
hough_v_disp = du.hough_lines(v_disp) hough_u_disp = du.hough_lines(u_disp) #Binarization: #Projection and classification in the image space # v_disp2 = np.hstack((hough_v_disp, v_disp)) # u_disp2 = np.vstack((hough_u_disp,u_disp)) #%% filler = np.zeros((u_disp2.shape[0], v_disp2.shape[1]), dtype=np.uint8) logger.debug( VisualRecord("U_disparity", [filler, u_disp2, v_disp2, image], fmt="png")) # logger.debug(VisualRecord( # "V_disparity and Image depth map", [v_disp2, image], fmt="png")) #%% drawing = False # true if mouse is pressed mode = True # if True, draw rectangle. Press 'm' to toggle to curve ix, iy = -1, -1 # mouse callback function def draw_circle(event, x, y, flags, param): global ix, iy, drawing, mode if event == cv2.EVENT_LBUTTONDOWN: drawing = True
def log(self, name, image): self.logger.debug(VisualRecord(name, image, fmt='png'))
def log(self): logText = "log" self.logger.info(VisualRecord(logText,self.image,str(self.i)))
def logImg(img, title="", text=""): logger.debug(VisualRecord(title, img, text, fmt="png"))