def show_layout(item): if isinstance(item, LTTextBox): self.outfp.write('<textbox id="%d" bbox="%s" />\n' % (item.index, bbox2str(item.bbox))) elif isinstance(item, LTTextGroup): self.outfp.write('<textgroup bbox="%s">\n' % bbox2str(item.bbox)) for child in item: show_layout(child) self.outfp.write('</textgroup>\n') return
def show_group(item): if isinstance(item, LTTextBox): self.outfp.write('<textbox id="%d" bbox="%s" />\n' % (item.index, bbox2str(item.bbox))) elif isinstance(item, LTTextGroup): self.outfp.write('<textgroup bbox="%s">\n' % bbox2str(item.bbox)) for child in item: show_group(child) self.outfp.write("</textgroup>\n") return
def render(item): if isinstance(item, LTPage): self.outfp.write( '<page id="%s" bbox="%s" rotate="%d">\n' % (item.pageid, bbox2str(item.bbox), item.rotate)) for child in item: render(child) self.outfp.write('</page>\n') elif isinstance(item, LTLine): self.outfp.write('<line linewidth="%d" bbox="%s" />\n' % (item.linewidth, bbox2str(item.bbox))) elif isinstance(item, LTRect): self.outfp.write('<rect linewidth="%d" bbox="%s" />\n' % (item.linewidth, bbox2str(item.bbox))) elif isinstance(item, LTPolygon): self.outfp.write( '<polygon linewidth="%d" bbox="%s" pts="%s"/>\n' % (item.linewidth, bbox2str(item.bbox), item.get_pts())) elif isinstance(item, LTFigure): self.outfp.write('<figure name="%s" bbox="%s">\n' % (item.name, bbox2str(item.bbox))) for child in item: render(child) self.outfp.write('</figure>\n') elif isinstance(item, LTTextLine): self.outfp.write('<textline bbox="%s">\n' % bbox2str(item.bbox)) for child in item: render(child) self.outfp.write('</textline>\n') elif isinstance(item, LTTextBox): self.outfp.write('<textbox id="%d" bbox="%s">\n' % (item.index, bbox2str(item.bbox))) for child in item: render(child) self.outfp.write('</textbox>\n') elif isinstance(item, LTChar): vertical = '' if item.is_vertical(): vertical = 'vertical="true" ' self.outfp.write('<text font="%s" %sbbox="%s" size="%.3f">' % (enc(item.font.fontname), vertical, bbox2str(item.bbox), item.get_size())) self.write(item.text) self.outfp.write('</text>\n') elif isinstance(item, LTText): self.outfp.write('<text>%s</text>\n' % item.text) elif isinstance(item, LTImage): if self.outdir: name = self.write_image(item) self.outfp.write( '<image src="%s" width="%d" height="%d" />\n' % (enc(name), item.width, item.height)) else: self.outfp.write('<image width="%d" height="%d" />\n' % (item.width, item.height)) else: assert 0, item return
def render(item): if isinstance(item, LTPage): self.outfp.write('<page id="%s" bbox="%s" rotate="%d">\n' % (item.pageid, bbox2str(item.bbox), item.rotate)) for child in item: render(child) if item.groups is not None: self.outfp.write('<layout>\n') for group in item.groups: show_group(group) self.outfp.write('</layout>\n') self.outfp.write('</page>\n') elif isinstance(item, LTLine): self.outfp.write('<line linewidth="%d" bbox="%s" />\n' % (item.linewidth, bbox2str(item.bbox))) elif isinstance(item, LTRect): self.outfp.write('<rect linewidth="%d" bbox="%s" />\n' % (item.linewidth, bbox2str(item.bbox))) elif isinstance(item, LTCurve): self.outfp.write('<curve linewidth="%d" bbox="%s" pts="%s"/>\n' % (item.linewidth, bbox2str(item.bbox), item.get_pts())) elif isinstance(item, LTFigure): self.outfp.write('<figure name="%s" bbox="%s">\n' % (item.name, bbox2str(item.bbox))) for child in item: render(child) self.outfp.write('</figure>\n') elif isinstance(item, LTTextLine): self.outfp.write('<textline bbox="%s">\n' % bbox2str(item.bbox)) for child in item: render(child) self.outfp.write('</textline>\n') elif isinstance(item, LTTextBox): wmode = '' if isinstance(item, LTTextBoxVertical): wmode = ' wmode="vertical"' self.outfp.write('<textbox id="%d" bbox="%s"%s>\n' % (item.index, bbox2str(item.bbox), wmode)) for child in item: render(child) self.outfp.write('</textbox>\n') elif isinstance(item, LTChar): self.outfp.write('<text font="%s" bbox="%s" size="%.3f">' % (enc(item.fontname), bbox2str(item.bbox), item.size)) self.write_text(item.get_text()) self.outfp.write('</text>\n') elif isinstance(item, LTText): self.outfp.write('<text>%s</text>\n' % item.get_text()) elif isinstance(item, LTImage): if self.imagewriter is not None: name = self.imagewriter.export_image(item) self.outfp.write('<image src="%s" width="%d" height="%d" />\n' % (enc(name), item.width, item.height)) else: self.outfp.write('<image width="%d" height="%d" />\n' % (item.width, item.height)) else: assert 0, item return
def __repr__(self): return "<%s(%s) %s matrix=%s>" % ( self.__class__.__name__, self.name, bbox2str(self.bbox), matrix2str(self.matrix), )
def __repr__(self): if self.debug: return ('<char matrix=%s font=%r fontsize=%.1f bbox=%s adv=%s text=%r>' % (matrix2str(self.matrix), self.font, self.fontsize, bbox2str(self.bbox), self.adv, self.text)) else: return '<char %r>' % self.text
def __renderChar(self, item): #if not self.__isLine: # return #font = enc(item.font.basefont) font = enc(item.fontname) #size = item.get_size() size = item.size fontChanged = False if (self.__font == None or (self.__font.fullName != font or self.__font.size != size)) or not self.__hasFont: # zmienil sie font lub nie jestesmy w obrebie tagu <span> dla fontu #if self.__font == None: # print "None", #else: # print self.__font.psname, if self.__hasFont: if self.__icu != None and self.__inWord: if not (self.__wordInd - 1) in self.__whites: self.__outfp.write("</span>") fontChanged = True self.__endSpecialTags(self.__font) self.__outfp.write("</span>") self.__font = self.__lib.findFont(self.__page, font).instantiate(size) #print self.__font.psname self.__outfp.write("<span style=\"") name = None if self.__fontMap != None: name = self.__fontMap.get(self.__font.name) if name == None: name = self.__font.name self.__outfp.write("font-family: " + self.__font.name) if self.__font.bold: self.__outfp.write("; font-weight: bold") if self.__font.italic: self.__outfp.write("; font-style: italic") #self.__outfp.write("; ps-name: " + font) self.__outfp.write("; font-size: " + str(self.__font.size)) self.__outfp.write("\">") self.__startSpecialTags(self.__font) self.__hasFont = True if self.__icu != None: if self.__ind in self.__divs or fontChanged: # TODO: I jezeli hasFont bylo False przy wywolaniu renderChar # i wypisalismy nowy font to wtedy nie moze byc srodek slowa (wiec __ind bedzie w self.__divs bo to 0) if not self.__wordInd in self.__whites: self.__outfp.write( "<span class=\"ocrx_word\" title=\"bbox " + bbox2str( changeCoords(self.__pagebbox, self.__divbboxes[ self.__wordInd])) + "\">") self.__wordInd += 1 self.__inWord = True self.__outfp.write(item.get_text().replace("<", "<").replace( "&", "&").encode("utf-8")) if self.__icu != None: self.__ind += 1 if self.__ind in self.__divs: if not (self.__wordInd - 1) in self.__whites: self.__outfp.write("</span>") self.__inWord = False
def __repr__(self): if self.debug: return ( '<char matrix=%s font=%r fontsize=%.1f bbox=%s adv=%s text=%r>' % (matrix2str(self.matrix), self.font, self.fontsize, bbox2str(self.bbox), self.adv, self.text)) else: return '<char %r>' % self.text
def __repr__(self): if self.debug: return ('<%s %s matrix=%s font=%r adv=%s text=%r>' % (self.__class__.__name__, bbox2str(self.bbox), matrix2str(self.matrix), self.fontname, self.adv, self.text)) else: return '<char %r>' % self.text
def render(item): if isinstance(item, LTPage): self.outfp.write('<page id="%s" bbox="%s" rotate="%d">\n' % (item.pageid, bbox2str(item.bbox), item.rotate)) for child in item: render(child) self.outfp.write('</page>\n') elif isinstance(item, LTLine): self.outfp.write('<line linewidth="%d" bbox="%s" />\n' % (item.linewidth, bbox2str(item.bbox))) elif isinstance(item, LTRect): self.outfp.write('<rect linewidth="%d" bbox="%s" />\n' % (item.linewidth, bbox2str(item.bbox))) elif isinstance(item, LTPolygon): self.outfp.write('<polygon linewidth="%d" bbox="%s" pts="%s"/>\n' % (item.linewidth, bbox2str(item.bbox), item.get_pts())) elif isinstance(item, LTFigure): self.outfp.write('<figure name="%s" bbox="%s">\n' % (item.name, bbox2str(item.bbox))) for child in item: render(child) self.outfp.write('</figure>\n') elif isinstance(item, LTTextLine): self.outfp.write('<textline bbox="%s">\n' % bbox2str(item.bbox)) for child in item: render(child) self.outfp.write('</textline>\n') elif isinstance(item, LTTextBox): self.outfp.write('<textbox id="%d" bbox="%s">\n' % (item.index, bbox2str(item.bbox))) for child in item: render(child) self.outfp.write('</textbox>\n') elif isinstance(item, LTChar): vertical = '' if item.is_vertical(): vertical = 'vertical="true" ' self.outfp.write('<text font="%s" %sbbox="%s" size="%.3f">' % (enc(item.font.fontname), vertical, bbox2str(item.bbox), item.get_size())) self.write(item.text) self.outfp.write('</text>\n') elif isinstance(item, LTText): self.outfp.write('<text>%s</text>\n' % item.text) elif isinstance(item, LTImage): if self.outdir: name = self.write_image(item) self.outfp.write('<image src="%s" width="%d" height="%d" />\n' % (enc(name), item.width, item.height)) else: self.outfp.write('<image width="%d" height="%d" />\n' % (item.width, item.height)) else: assert 0, item return
def receive_layout(self, ltpage): def render(item): if isinstance(item, LTPage): self.__num += 1 self.__outfp.write("<div class=\"ocr_page\"") self.__outfp.write(" title=\"") self.__outfp.write("pageno " + str(self.__num) + ";") self.__outfp.write(" bbox " + bbox2str(scaleBbox(normalize(ltpage.bbox)))) self.__outfp.write("\">") for child in item: #print child render(child) self.__outfp.write("</div>") elif isinstance(item, LTLine): pass elif isinstance(item, LTRect): pass elif isinstance(item, LTCurve): pass elif isinstance(item, LTFigure): pass elif isinstance(item, LTTextLine): self.__outfp.write("<span class=\"ocr_line\"") self.__outfp.write(" title=\"bbox " + bbox2str(changeCoords(self.__pagebbox, normalize(item.bbox))) + "\"") self.__outfp.write(">") #self.__isLine = True self.__hasFont = False self.__chars = [] for child in item: render(child) text = u"" for c in self.__chars: text += c.get_text() if self.__icu != None: # zob. metode HOCRExporter.__exportNode self.__divs = divideIntoWords(text, self.__icu) (self.__divbboxes, self.__whites) = generateDivBboxesDirect(self.__divs, self.__chars, self.__hasFont, self.__font, self.__lib, self.__page, enc) self.__divs = [0] + self.__divs self.__ind = 0 self.__wordInd = 0 self.__inWord = False for c in self.__chars: # (***) self.__renderChar(c) if self.__hasFont: self.__endSpecialTags(self.__font) self.__outfp.write("</span>") #self.__isLine = False self.__outfp.write("</span>") elif isinstance(item, LTTextBox): self.__outfp.write("<div class=\"ocr_carea\"") self.__outfp.write(" title=\"bbox " + bbox2str(changeCoords(self.__pagebbox, normalize(item.bbox))) + "\"") self.__outfp.write(">") for child in item: render(child) self.__outfp.write("</div>") elif isinstance(item, LTChar): self.__chars.append(item) elif isinstance(item, LTText): pass # TODO: NOTE ignorujemy tekst pusty (tu byly same spacje) #self.outfp.write('<text>%s</text>\n' % item.get_text()) elif isinstance(item, LTImage): pass elif isinstance(item, LTTextGroup): self.__outfp.write("<div class=\"ocr_carea\"") self.__outfp.write(" title=\"bbox " + bbox2str(changeCoords(self.__pagebbox, normalize(item.bbox))) + "\"") self.__outfp.write(">") for child in item: render(child) self.__outfp.write("</div>") else: assert 0, item return # ltpage to strona ktorej dziecmi sa elementy typu "textbox", # w ltpage.layout jest struktura z uzyciem elementow "textgroup" (ktora # zawiera jako potomkow takze elementy "textbox" bedace dziecmi strony) self.__pagebbox = normalize(ltpage.bbox) if ltpage.groups and not self.__ignore: # nie ignorujemy "textgroup" i # "textgroup" sa w zanalizowanej strukturze #print "ignore" self.__num += 1 self.__outfp.write("<div class=\"ocr_page\"") self.__outfp.write(" title=\"") self.__outfp.write("pageno " + str(self.__num) + ";") self.__outfp.write("bbox " + bbox2str(scaleBbox(normalize(ltpage.bbox)))) self.__outfp.write("\">") for lay in ltpage.groups: render(lay) self.__outfp.write("</div>") else: # ignorujemy "textgroup" lub ich nie ma # TODO: I sprawdzic: jezeli brak ltpage.layout to chyba rownoznaczne ze strona bez tekstu? render(ltpage) return
def __repr__(self): return ('<%s(%r) %s rotate=%r>' % (self.__class__.__name__, self.pageid, bbox2str(self.bbox), self.rotate))
def __repr__(self): return ('<%s %s matrix=%s font=%r adv=%s text=%r>' % (self.__class__.__name__, bbox2str(self.bbox), matrix2str(self.matrix), self.fontname, self.adv, self.get_text()))
def __repr__(self): return '<%s %s %r>' % (self.__class__.__name__, bbox2str(self.bbox), self.get_text())
def __repr__(self): return ('<page(%r) bbox=%s rotate=%r>' % (self.pageid, bbox2str(self.bbox), self.rotate))
def __repr__(self): return ('<textbox(%s) %s %r...>' % (self.index, bbox2str(self.bbox), self.get_text()[:20]))
def __repr__(self): return ('<container %s>' % bbox2str(self.bbox))
def __repr__(self): return ('<%s(%s) %s %r>' % (self.__class__.__name__, self.name, bbox2str(self.bbox), self.srcsize))
def __repr__(self): return ('<%s(%s) %s %r...>' % (self.__class__.__name__, self.index, bbox2str(self.bbox), self.text[:20]))
def __repr__(self): (w,h) = self.srcsize return ('<%s(%s) %s %dx%d>' % (self.__class__.__name__, self.name, bbox2str(self.bbox), w, h))
def __repr__(self): return ('<%s(%s) %s %r>' % (self.__class__.__name__, self.index, bbox2str(self.bbox), self.get_text()))
def scaled_bbox(self, item): return 'bbox="%s" %s' % ( bbox2str(item.bbox, scale=self.scale, roundCoords=self.roundCoords, yOffset=self._yoffset), bbox2dims(item.bbox, scale=self.scale, roundCoords=self.roundCoords, yOffset=self._yoffset))
def __repr__(self): return ('<%s(%r) %s rotate=%r>' % (self.__class__.__name__, self.pageid, bbox2str( self.bbox), self.rotate))
def __repr__(self): return ('<textline %s>' % bbox2str(self.bbox))
def begin_page(self, page, ctm): self.outfp.write('<page id="%s" bbox="%s" rotate="%d">' % (self.pageno, bbox2str(page.mediabox), page.rotate)) return
def __repr__(self): return ('<figure %r bbox=%s matrix=%s>' % (self.name, bbox2str(self.bbox), matrix2str(self.matrix)))
def render(item): if isinstance(item, LTPage): self.__num += 1 self.__outfp.write("<div class=\"ocr_page\"") self.__outfp.write(" title=\"") self.__outfp.write("pageno " + str(self.__num) + ";") self.__outfp.write(" bbox " + bbox2str(scaleBbox(normalize(ltpage.bbox)))) self.__outfp.write("\">") for child in item: #print child render(child) self.__outfp.write("</div>") elif isinstance(item, LTLine): pass elif isinstance(item, LTRect): pass elif isinstance(item, LTCurve): pass elif isinstance(item, LTFigure): pass elif isinstance(item, LTTextLine): self.__outfp.write("<span class=\"ocr_line\"") self.__outfp.write(" title=\"bbox " + bbox2str(changeCoords(self.__pagebbox, normalize(item.bbox))) + "\"") self.__outfp.write(">") #self.__isLine = True self.__hasFont = False self.__chars = [] for child in item: render(child) text = u"" for c in self.__chars: text += c.get_text() if self.__icu != None: # zob. metode HOCRExporter.__exportNode self.__divs = divideIntoWords(text, self.__icu) (self.__divbboxes, self.__whites) = generateDivBboxesDirect(self.__divs, self.__chars, self.__hasFont, self.__font, self.__lib, self.__page, enc) self.__divs = [0] + self.__divs self.__ind = 0 self.__wordInd = 0 self.__inWord = False for c in self.__chars: # (***) self.__renderChar(c) if self.__hasFont: self.__endSpecialTags(self.__font) self.__outfp.write("</span>") #self.__isLine = False self.__outfp.write("</span>") elif isinstance(item, LTTextBox): self.__outfp.write("<div class=\"ocr_carea\"") self.__outfp.write(" title=\"bbox " + bbox2str(changeCoords(self.__pagebbox, normalize(item.bbox))) + "\"") self.__outfp.write(">") for child in item: render(child) self.__outfp.write("</div>") elif isinstance(item, LTChar): self.__chars.append(item) elif isinstance(item, LTText): pass # TODO: NOTE ignorujemy tekst pusty (tu byly same spacje) #self.outfp.write('<text>%s</text>\n' % item.get_text()) elif isinstance(item, LTImage): pass elif isinstance(item, LTTextGroup): self.__outfp.write("<div class=\"ocr_carea\"") self.__outfp.write(" title=\"bbox " + bbox2str(changeCoords(self.__pagebbox, normalize(item.bbox))) + "\"") self.__outfp.write(">") for child in item: render(child) self.__outfp.write("</div>") else: assert 0, item return
def __repr__(self): return ('<item bbox=%s>' % bbox2str(self.bbox))
def tracking_selection(filename_in, filename_out_video, filename_out_csv, settings): # opening reader for the video v_in = cv2.VideoCapture(filename_in) n_frames = int(v_in.get(cv2.CAP_PROP_FRAME_COUNT)) fps = v_in.get(cv2.CAP_PROP_FPS) n_trackers = 10 ret, frame = v_in.read() # initial bounding boxes around the MOTGUI = MOTTrackingROI(frame, rois=None, name="Select Bounding Boxes around Tracked Objects") initial_roi = MOTGUI.getROIs() # initializing tracker, 10 trackers x nFrames tracks = [[None] * n_trackers for _ in range(n_frames)] trackers = [None] * n_trackers tracks[0] = initial_roi trackers = init_trackers(trackers, frame, initial_roi, settings['tracker']) frame_id = 1 # first one was used for initializaiton timeout = 0 while True: v_in.set(cv2.CAP_PROP_POS_FRAMES, frame_id) ret, frame = v_in.read() # end of the video or fail if ret == 0: break for i, tracker in enumerate(trackers): # tracker not initialized if tracker is None: continue ok, bbox = tracker.update(frame) if ok: tracks[frame_id][i] = bbox cv2.imshow("tracking", utils.draw_ROIs(frame, tracks[frame_id])) key = cv2.waitKey(timeout) # go backwards if key == ord('a'): frame_id = np.maximum(0, frame_id - 1) v_in.set(cv2.CAP_PROP_POS_FRAMES, frame_id) # go forward if key == ord('d'): frame_id = np.minimum(frame_id + 1, n_frames) v_in.set(cv2.CAP_PROP_POS_FRAMES, frame_id) if key == ord('s'): MOTGUI = MOTTrackingROI(frame, tracks[frame_id]) init_trackers(trackers, frame, MOTGUI.getROIs(), settings['tracker']) # play if key == ord('p'): if timeout == 0: timeout = 1 else: timeout = 0 # if the autoplay is active, add the frames automatically if timeout != 0: frame_id = frame_id + 1 if key == ord('q'): break cv2.destroyAllWindows() # exporting data v_in.release() v_in = cv2.VideoCapture(filename_in) n_frames = int(v_in.get(cv2.CAP_PROP_FRAME_COUNT)) fps = int(v_in.get(cv2.CAP_PROP_FPS)) v_in_width = int(v_in.get(cv2.CAP_PROP_FRAME_WIDTH)) v_in_height = int(v_in.get(cv2.CAP_PROP_FRAME_HEIGHT)) volumes = [[None] * n_frames for _ in range(n_trackers)] # saving the video with all tracklets visualised if settings['tracker_video_output']: v_out = cv2.VideoWriter(filename_out_video, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, (v_in_width, v_in_height)) for frame_id in range(n_frames): ret, frame = v_in.read() for t in range(n_trackers): bbox = tracks[frame_id][t] if bbox is None: continue volumes[t][frame_id] = utils.cut_frame(frame.copy(), bbox) if settings['tracker_video_output']: v_out.write(utils.draw_ROIs(frame, tracks[frame_id])) if settings['tracker_video_output']: v_out.release() # # saving results (cvs, volume) # # list of frames where each item is list of tracks to list of tracers where each item is a list of frames tracks_transposed = [list(i) for i in zip(*tracks)] for t in range(n_trackers): V = utils.resize_image_list(volumes[t]) # saving the volume if V.ravel().sum() > 0: # testing if the volume is not empty, if sum(V[:]) == 0, then there is nothing to save # save if settings['tracker_npyvolume_output']: frame_sum = V.reshape((-1, V.shape[-1])).sum(0) # filtering out the ignored frames V = V[..., np.logical_and(frame_sum > 0, np.isfinite(frame_sum))] filename_output_volume = filename_out_video.split('.')[:-1][0] + '_' + str(t) np.save(filename_output_volume, V) print('saved ' + filename_output_volume) filename_output_bbox_csv = filename_out_csv.split('.')[:-1][0] + '_' + str(t) + '.csv' tt = tracks_transposed[t] # saving only the non empty 4-element bounding boxes f = open(filename_output_bbox_csv, 'w') f.write(utils.bbox2str(tt)) f.close() print('saved ' + filename_output_bbox_csv)
def __repr__(self): return ('<%s %s matrix=%s font=%r adv=%s text=%r>' % (self.__class__.__name__, bbox2str(self.bbox), matrix2str( self.matrix), self.fontname, self.adv, self.get_text()))
def __repr__(self): return ('<%s(%s) %s matrix=%s>' % (self.__class__.__name__, self.name, bbox2str( self.bbox), matrix2str(self.matrix)))
def __repr__(self): return ('<%s %s>' % (self.__class__.__name__, bbox2str(self.bbox)))
def __repr__(self): return "<%s %s %r>" % (self.__class__.__name__, bbox2str(self.bbox), self.text)