ImageLabeler.cpp 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464
  1. /*
  2. * ImageLabeler.cpp
  3. *
  4. * Created on: Oct 4, 2011
  5. * Author: Gapchich Vladislav
  6. */
  7. #include "ImageLabeler.h"
  8. #include "functions.h"
  9. #include <QApplication>
  10. #include <QFrame>
  11. #include <QMenuBar>
  12. #include <QMenu>
  13. #include <QAction>
  14. #include <QBoxLayout>
  15. #include <QGridLayout>
  16. #include <QPixmap>
  17. #include <QLabel>
  18. #include <QScrollArea>
  19. #include <QPushButton>
  20. #include <QButtonGroup>
  21. #include <QListWidget>
  22. #include <QListWidgetItem>
  23. #include <QDesktopWidget>
  24. #include <QFileDialog>
  25. #include <QColorDialog>
  26. #include <QDir>
  27. #include <QMessageBox>
  28. #include <QListIterator>
  29. #include <QDomDocument>
  30. #include <QFile>
  31. #include <QKeyEvent>
  32. #include <QDebug>
  33. ImageLabeler::ImageLabeler(QWidget *aParent) :
  34. QMainWindow(aParent)
  35. {
  36. /*
  37. * Variables
  38. */
  39. list_images_ = new QList< Image >;
  40. main_label_ = -1;
  41. pure_data_ = 0;
  42. //label_ID_ = -1;
  43. /* options */
  44. auto_color_generation_ = 0;
  45. /* flags */
  46. interrupt_search_ = 0;
  47. unsaved_data_ = 0;
  48. /*
  49. * menu bar part begins
  50. */
  51. menu_bar_ = new QMenuBar(this);
  52. setMenuBar(menu_bar_);
  53. menu_file_ = new QMenu(menu_bar_);
  54. menu_file_->setTitle(tr("&File"));
  55. menu_pascal_ = new QMenu(menu_bar_);
  56. menu_pascal_->setTitle(tr("&Pascal"));
  57. menu_view_ = new QMenu(menu_bar_);
  58. menu_view_->setTitle(tr("&View"));
  59. menu_edit_ = new QMenu(menu_bar_);
  60. menu_edit_->setTitle(tr("&Edit"));
  61. menu_help_ = new QMenu(menu_bar_);
  62. menu_help_->setTitle(tr("&Help"));
  63. /* menu file */
  64. action_open_image_ = new QAction(this);
  65. action_open_image_->setText(tr("&Load image"));
  66. action_open_images_ = new QAction(this);
  67. action_open_images_->setText(tr("&Load images(recursively)"));
  68. action_open_labeled_image_ = new QAction(this);
  69. action_open_labeled_image_->setText(tr("&Load labeled image"));
  70. action_load_legend_ = new QAction(this);
  71. action_load_legend_->setText(tr("&Load legend"));
  72. action_save_labels_ = new QAction(this);
  73. action_save_labels_->setText(tr("&Save all info"));
  74. action_save_labels_->setEnabled(false);
  75. action_save_segmented_ = new QAction(this);
  76. action_save_segmented_->setText(tr("Save segmented &picture"));
  77. action_save_segmented_->setEnabled(false);
  78. action_save_legend_ = new QAction(this);
  79. action_save_legend_->setText(tr("Save &legend"));
  80. action_save_legend_->setEnabled(false);
  81. action_quit_ = new QAction(this);
  82. action_quit_->setText(tr("&Quit"));
  83. /* menu pascal */
  84. action_load_pascal_file_ = new QAction(this);
  85. action_load_pascal_file_->setText(tr("&Load pascal file"));
  86. action_load_pascal_poly_ = new QAction(this);
  87. action_load_pascal_poly_->setText(tr("&Load poly info"));
  88. /* menu view */
  89. action_view_normal_ = new QAction(this);
  90. action_view_normal_->setText(tr("&Normal"));
  91. action_view_normal_->setEnabled(false);
  92. action_view_segmented_ = new QAction(this);
  93. action_view_segmented_->setText(tr("&Segmented"));
  94. action_view_segmented_->setEnabled(false);
  95. /* menu edit */
  96. action_undo_ = new QAction(this);
  97. action_undo_->setText(tr("&Undo"));
  98. action_undo_->setEnabled(false);
  99. action_redo_ = new QAction(this);
  100. action_redo_->setText(tr("&Redo"));
  101. action_redo_->setEnabled(false);
  102. action_bound_box_tool_ = new QAction(this);
  103. action_bound_box_tool_->setText(tr("Bounding box tool"));
  104. action_bound_box_tool_->setEnabled(false);
  105. action_polygon_tool_ = new QAction(this);
  106. action_polygon_tool_->setText(tr("&Polygon tool"));
  107. action_polygon_tool_->setEnabled(false);
  108. action_tagging_tool_ = new QAction(this);
  109. action_tagging_tool_->setText(tr("&Tagging tool"));
  110. action_tagging_tool_->setEnabled(false);
  111. action_add_description_ = new QAction(this);
  112. action_add_description_->setText(tr("&Add image description"));
  113. action_add_description_->setEnabled(false);
  114. action_options_ = new QAction(this);
  115. action_options_->setText(tr("&Options"));
  116. /* menu help */
  117. action_help_content_ = new QAction(this);
  118. action_help_content_->setText(tr("&Help content"));
  119. action_help_content_->setEnabled(false);
  120. action_about_ = new QAction(this);
  121. action_about_->setText(tr("&About"));
  122. action_about_->setEnabled(false);
  123. /* ------------------ */
  124. menu_file_->addAction(action_open_image_);
  125. menu_file_->addAction(action_open_images_);
  126. menu_file_->addAction(action_open_labeled_image_);
  127. menu_file_->addAction(action_load_legend_);
  128. menu_file_->addAction(menu_pascal_->menuAction());
  129. menu_file_->addSeparator();
  130. menu_file_->addAction(action_save_segmented_);
  131. menu_file_->addAction(action_save_legend_);
  132. menu_file_->addAction(action_save_labels_);
  133. menu_file_->addSeparator();
  134. menu_file_->addAction(action_quit_);
  135. menu_pascal_->addAction(action_load_pascal_file_);
  136. menu_pascal_->addAction(action_load_pascal_poly_);
  137. menu_view_->addAction(action_view_normal_);
  138. menu_view_->addAction(action_view_segmented_);
  139. menu_edit_->addAction(action_undo_);
  140. menu_edit_->addAction(action_redo_);
  141. menu_edit_->addSeparator();
  142. menu_edit_->addAction(action_bound_box_tool_);
  143. menu_edit_->addAction(action_polygon_tool_);
  144. menu_edit_->addAction(action_tagging_tool_);
  145. menu_edit_->addSeparator();
  146. menu_edit_->addAction(action_add_description_);
  147. menu_edit_->addAction(action_options_);
  148. menu_help_->addAction(action_help_content_);
  149. menu_help_->addAction(action_about_);
  150. menu_bar_->addAction(menu_file_->menuAction());
  151. menu_bar_->addAction(menu_view_->menuAction());
  152. menu_bar_->addAction(menu_edit_->menuAction());
  153. menu_bar_->addAction(menu_help_->menuAction());
  154. /*
  155. * popup menu part begins
  156. */
  157. popup_area_list_ = new QMenu;
  158. action_delete_area_ = new QAction(this);
  159. action_delete_area_->setText(tr("&Delete area"));
  160. action_edit_area_ = new QAction(this);
  161. action_edit_area_->setText(tr("&Change area"));
  162. popup_area_list_->addAction(action_delete_area_);
  163. popup_area_list_->addAction(action_edit_area_);
  164. popup_label_list_ = new QMenu;
  165. action_set_color_ = new QAction(this);
  166. action_set_color_->setText(tr("Set &color"));
  167. action_toggle_priority_ = new QAction(this);
  168. action_toggle_priority_->setText(tr("Toggle &priority"));
  169. action_delete_label_ = new QAction(this);
  170. action_delete_label_->setText(tr("&Delete"));
  171. popup_label_list_->addAction(action_set_color_);
  172. popup_label_list_->addAction(action_toggle_priority_);
  173. popup_label_list_->addAction(action_delete_label_);
  174. popup_images_list_ = new QMenu;
  175. action_remove_image_ = new QAction(this);
  176. action_remove_image_->setText(tr("&remove image"));
  177. popup_images_list_->addAction(action_remove_image_);
  178. /*
  179. * widgets part begins
  180. */
  181. central_widget_ = new QWidget(this);
  182. setCentralWidget(central_widget_);
  183. frame_toolbox_ = new QFrame(central_widget_);
  184. frame_toolbox_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
  185. frame_toolbox_->setLineWidth(0);
  186. frame_toolbox_->setMidLineWidth(0);
  187. frame_center_ = new QFrame(central_widget_);
  188. //frame_image_->setFrameStyle(QFrame::Box | QFrame::Plain);
  189. //frame_image_->setLineWidth(1);
  190. frame_image_ = new QScrollArea(frame_center_);
  191. //frame_image_->setStyleSheet("QWidget {background: #888888;}");
  192. frame_image_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
  193. frame_image_->setLineWidth(0);
  194. frame_image_->setMidLineWidth(0);
  195. frame_image_->setWidgetResizable(false);
  196. frame_image_->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
  197. frame_image_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  198. frame_labelbox_ = new QFrame(central_widget_);
  199. frame_labelbox_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
  200. frame_labelbox_->setLineWidth(0);
  201. frame_labelbox_->setMidLineWidth(0);
  202. image_ = new QPixmap(500, 500);
  203. image_->fill(QColor(Qt::white));
  204. //image_holder_ = new ImageHolder(frame_image_);
  205. image_holder_ = new ImageHolder;//(frame_image_);
  206. image_holder_->setPixmap(*image_);
  207. //image_holder_->setStyleSheet("QLabel {background: #ffffff;}");
  208. image_holder_->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
  209. image_holder_->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
  210. image_holder_->setScaledContents(true);
  211. image_holder_->setFrameStyle(QFrame::Box | QFrame::Plain);
  212. image_holder_->setLineWidth(0);
  213. frame_image_->setWidget(image_holder_);
  214. list_label_ = new QListWidget(central_widget_);
  215. list_label_->setContextMenuPolicy(Qt::CustomContextMenu);
  216. list_areas_ = new QListWidget(central_widget_);
  217. list_areas_->setContextMenuPolicy(Qt::CustomContextMenu);
  218. list_images_widget_ = new QListWidget(central_widget_);
  219. list_images_widget_->setContextMenuPolicy(Qt::CustomContextMenu);
  220. // addLabel(0, false, "BACKGROUND");
  221. // list_label_->item(0)->setFlags(Qt::ItemIsSelectable);
  222. //QListWidgetItem *background = list_label_->item(0);
  223. //qDebug() << background;
  224. //background->setFlags(Qt::ItemIsSelectable);
  225. label_toolbox_ = new QLabel(tr("Tool box"), frame_toolbox_);
  226. label_list_areas_ = new QLabel(tr("Selected areas:"), central_widget_);
  227. label_list_images_ = new QLabel(tr("Loaded images:"), central_widget_);
  228. /* buttons */
  229. button_bound_box_tool_ = new QPushButton(frame_toolbox_);
  230. button_bound_box_tool_->setText(tr("bbox"));
  231. button_bound_box_tool_->setEnabled(false);
  232. button_bound_box_tool_->setCheckable(true);
  233. button_polygon_tool_ = new QPushButton(frame_toolbox_);
  234. button_polygon_tool_->setText(tr("poly tool"));
  235. button_polygon_tool_->setEnabled(false);
  236. button_polygon_tool_->setCheckable(true);
  237. button_tagging_tool_ = new QPushButton(frame_toolbox_);
  238. button_tagging_tool_->setText(tr("tagging"));
  239. button_tagging_tool_->setEnabled(false);
  240. button_clear_selection_tool_ = new QPushButton(frame_toolbox_);
  241. button_clear_selection_tool_->setText(tr("clear selection"));
  242. button_clear_selection_tool_->setEnabled(false);
  243. button_generate_colors_ = new QPushButton(frame_toolbox_);
  244. button_generate_colors_->setText(tr("generate label colors"));
  245. button_generate_colors_->setEnabled(false);
  246. button_delete_all_labels_ = new QPushButton(frame_toolbox_);
  247. button_delete_all_labels_->setText(tr("delete all labels"));
  248. button_delete_all_labels_->setEnabled(false);
  249. group_tools_ = new QButtonGroup;
  250. group_tools_->addButton(button_bound_box_tool_);
  251. group_tools_->addButton(button_polygon_tool_);
  252. button_confirm_selection_ = new QPushButton(central_widget_);
  253. button_confirm_selection_->setText(tr("Confirm selection"));
  254. button_confirm_selection_->setEnabled(false);
  255. button_add_label_ = new QPushButton(frame_labelbox_);
  256. button_add_label_->setText(tr("Add label"));
  257. button_add_label_->setEnabled(false);
  258. button_remove_label_ = new QPushButton(frame_labelbox_);
  259. button_remove_label_->setText(tr("Remove label"));
  260. button_remove_label_->setEnabled(false);
  261. button_prev_image_ = new QPushButton(central_widget_);
  262. button_prev_image_->setText("←");
  263. button_next_image_ = new QPushButton(central_widget_);
  264. button_next_image_->setText("→");
  265. /*
  266. * layouts part begins
  267. */
  268. layout_main_ = new QHBoxLayout(central_widget_);
  269. layout_left_ = new QVBoxLayout();
  270. layout_toolbox_ = new QVBoxLayout();
  271. layout_center_ = new QVBoxLayout();
  272. layout_frame_image_ = new QVBoxLayout();
  273. layout_image_widget_ = new QGridLayout();
  274. layout_center_buttons_ = new QHBoxLayout();
  275. layout_right_ = new QVBoxLayout();
  276. layout_labelbox_ = new QVBoxLayout();
  277. layout_labelbox_buttons_ = new QHBoxLayout();
  278. layout_main_->addLayout(layout_left_);
  279. layout_main_->addLayout(layout_center_);
  280. layout_main_->addLayout(layout_right_);
  281. /* making the center part stretchable */
  282. layout_main_->setStretch(1, 1);
  283. /* left part */
  284. layout_left_->addWidget(frame_toolbox_);
  285. frame_toolbox_->setLayout(layout_toolbox_);
  286. layout_toolbox_->addWidget(label_toolbox_);
  287. layout_toolbox_->addWidget(button_bound_box_tool_);
  288. layout_toolbox_->addWidget(button_polygon_tool_);
  289. layout_toolbox_->addSpacing(10);
  290. layout_toolbox_->addWidget(button_tagging_tool_);
  291. layout_toolbox_->addSpacing(10);
  292. layout_toolbox_->addWidget(button_clear_selection_tool_);
  293. layout_toolbox_->addWidget(button_delete_all_labels_);
  294. layout_toolbox_->addSpacing(10);
  295. layout_toolbox_->addWidget(button_generate_colors_);
  296. /* stretch is for making toolbox as small as it can be */
  297. layout_toolbox_->addSpacing(10);
  298. layout_left_->addWidget(label_list_images_);
  299. layout_left_->addWidget(list_images_widget_);
  300. list_images_widget_->setFixedWidth(200);
  301. layout_left_->addStretch(1);
  302. layout_left_->addWidget(button_confirm_selection_);
  303. /* central part */
  304. layout_center_->addWidget(frame_center_);
  305. frame_center_->setLayout(layout_frame_image_);
  306. //layout_frame_image_->addWidget(image_holder_);
  307. layout_frame_image_->setContentsMargins(0, 0, 0, 0);
  308. layout_frame_image_->addWidget(frame_image_);
  309. //frame_image_->setLayout(layout_image_widget_);
  310. // layout_image_widget_->setRowStretch(0, 1);
  311. // layout_image_widget_->setColumnStretch(0, 1);
  312. // layout_image_widget_->addWidget(image_holder_, 1, 1);
  313. // layout_image_widget_->setRowStretch(2, 1);
  314. // layout_image_widget_->setColumnStretch(2, 1);
  315. //layout_frame_image_->addStretch(1);
  316. layout_frame_image_->addLayout(layout_center_buttons_);
  317. layout_center_buttons_->addWidget(button_prev_image_);
  318. layout_center_buttons_->addWidget(button_next_image_);
  319. /* right part */
  320. layout_right_->addWidget(frame_labelbox_);
  321. frame_labelbox_->setFixedWidth(200);
  322. frame_labelbox_->setLayout(layout_labelbox_);
  323. layout_labelbox_->addLayout(layout_labelbox_buttons_);
  324. layout_labelbox_buttons_->addWidget(button_add_label_);
  325. layout_labelbox_buttons_->addWidget(button_remove_label_);
  326. layout_labelbox_->addWidget(list_label_);
  327. layout_labelbox_->addWidget(label_list_areas_);
  328. layout_labelbox_->addWidget(list_areas_);
  329. connect(
  330. action_quit_,
  331. SIGNAL(triggered()),
  332. this,
  333. SLOT(close())
  334. );
  335. connect(
  336. action_open_images_,
  337. SIGNAL(triggered()),
  338. this,
  339. SLOT(loadImages())
  340. );
  341. connect(
  342. action_open_image_,
  343. SIGNAL(triggered()),
  344. this,
  345. SLOT(loadImage())
  346. );
  347. connect(
  348. action_open_labeled_image_,
  349. SIGNAL(triggered()),
  350. this,
  351. SLOT(loadInfo())
  352. );
  353. connect(
  354. action_load_legend_,
  355. SIGNAL(triggered()),
  356. this,
  357. SLOT(loadLegendFromFile())
  358. );
  359. connect(
  360. action_load_pascal_file_,
  361. SIGNAL(triggered()),
  362. this,
  363. SLOT(loadPascalFile())
  364. );
  365. connect(
  366. action_load_pascal_poly_,
  367. SIGNAL(triggered()),
  368. this,
  369. SLOT(loadPascalPolys())
  370. );
  371. connect(
  372. action_save_legend_,
  373. SIGNAL(triggered()),
  374. this,
  375. SLOT(saveLegend())
  376. );
  377. connect(
  378. action_save_segmented_,
  379. SIGNAL(triggered()),
  380. this,
  381. SLOT(saveSegmentedPicture())
  382. );
  383. connect(
  384. action_save_labels_,
  385. SIGNAL(triggered()),
  386. this,
  387. SLOT(saveAllInfo())
  388. );
  389. connect(
  390. action_view_normal_,
  391. SIGNAL(triggered()),
  392. this,
  393. SLOT(viewNormal())
  394. );
  395. connect(
  396. action_view_segmented_,
  397. SIGNAL(triggered()),
  398. this,
  399. SLOT(viewSegmented())
  400. );
  401. connect(
  402. action_undo_,
  403. SIGNAL(triggered()),
  404. image_holder_,
  405. SLOT(undo())
  406. );
  407. connect(
  408. action_redo_,
  409. SIGNAL(triggered()),
  410. image_holder_,
  411. SLOT(redo())
  412. );
  413. connect(
  414. action_add_description_,
  415. SIGNAL(triggered()),
  416. &line_edit_form_,
  417. SLOT(setDescription())
  418. );
  419. connect(
  420. action_options_,
  421. SIGNAL(triggered()),
  422. &options_form_,
  423. SLOT(show())
  424. );
  425. connect(
  426. button_add_label_,
  427. SIGNAL(clicked()),
  428. this,
  429. SLOT(addLabel())
  430. );
  431. connect(
  432. button_remove_label_,
  433. SIGNAL(clicked()),
  434. this, SLOT(removeLabel())
  435. );
  436. connect(
  437. button_next_image_,
  438. SIGNAL(clicked()),
  439. this,
  440. SLOT(nextImage())
  441. );
  442. connect(
  443. button_prev_image_,
  444. SIGNAL(clicked()),
  445. this,
  446. SLOT(prevImage())
  447. );
  448. connect(
  449. button_bound_box_tool_,
  450. SIGNAL(toggled(bool)),
  451. this,
  452. SLOT(setBoundingBoxTool(bool))
  453. );
  454. connect(
  455. button_polygon_tool_,
  456. SIGNAL(toggled(bool)),
  457. this,
  458. SLOT(setPolygonTool(bool))
  459. );
  460. connect(
  461. button_tagging_tool_,
  462. SIGNAL(clicked()),
  463. &line_edit_form_,
  464. SLOT(setTags())
  465. );
  466. connect(
  467. button_clear_selection_tool_,
  468. SIGNAL(clicked()),
  469. this,
  470. SLOT(clearAllTool())
  471. );
  472. connect(
  473. button_generate_colors_,
  474. SIGNAL(clicked()),
  475. this,
  476. SLOT(generateColors())
  477. );
  478. connect(
  479. button_delete_all_labels_,
  480. SIGNAL(clicked()),
  481. this,
  482. SLOT(clearLabelList())
  483. );
  484. connect(
  485. button_confirm_selection_,
  486. SIGNAL(clicked()),
  487. this,
  488. SLOT(confirmSelection())
  489. );
  490. connect(
  491. list_label_,
  492. SIGNAL(itemChanged(QListWidgetItem *)),
  493. this,
  494. SLOT(editLabel(QListWidgetItem *))
  495. );
  496. connect(
  497. list_label_,
  498. SIGNAL(itemClicked(QListWidgetItem *)),
  499. this,
  500. SLOT(setLabelID(QListWidgetItem *))
  501. );
  502. connect(
  503. list_areas_,
  504. SIGNAL(itemDoubleClicked(QListWidgetItem *)),
  505. image_holder_,
  506. SLOT(focusOnArea(QListWidgetItem *))
  507. );
  508. connect(
  509. list_areas_,
  510. SIGNAL(customContextMenuRequested(const QPoint &)),
  511. this,
  512. SLOT(areaListPopupMenu(const QPoint &))
  513. );
  514. connect(
  515. list_areas_,
  516. SIGNAL(itemChanged(QListWidgetItem *)),
  517. this,
  518. SLOT(onAreaItemChange(QListWidgetItem *))
  519. );
  520. connect(
  521. list_label_,
  522. SIGNAL(customContextMenuRequested(const QPoint &)),
  523. this,
  524. SLOT(labelListPopupMenu(const QPoint &))
  525. );
  526. connect(
  527. list_images_widget_,
  528. SIGNAL(itemDoubleClicked(QListWidgetItem *)),
  529. this,
  530. SLOT(selectImage(QListWidgetItem *))
  531. );
  532. connect(
  533. list_images_widget_,
  534. SIGNAL(customContextMenuRequested(const QPoint &)),
  535. this,
  536. SLOT(imageListPopupMenu(const QPoint &))
  537. );
  538. connect(
  539. action_delete_area_,
  540. SIGNAL(triggered()),
  541. this,
  542. SLOT(deleteArea())
  543. );
  544. connect(
  545. action_edit_area_,
  546. SIGNAL(triggered()),
  547. this,
  548. SLOT(editArea())
  549. );
  550. connect(
  551. action_set_color_,
  552. SIGNAL(triggered()),
  553. this,
  554. SLOT(setLabelColor())
  555. );
  556. connect(
  557. action_toggle_priority_,
  558. SIGNAL(triggered()),
  559. this,
  560. SLOT(toggleLabelPriority())
  561. );
  562. connect(
  563. action_delete_label_,
  564. SIGNAL(triggered()),
  565. this,
  566. SLOT(removeLabel())
  567. );
  568. connect(
  569. action_remove_image_,
  570. SIGNAL(triggered()),
  571. this,
  572. SLOT(removeImage())
  573. );
  574. connect(
  575. image_holder_,
  576. SIGNAL(selectionStarted()),
  577. this,
  578. SLOT(onSelectionStarted())
  579. );
  580. connect(
  581. &line_edit_form_,
  582. SIGNAL(dataSet(QString)),
  583. this,
  584. SLOT(setDataFromForm(QString))
  585. );
  586. connect(
  587. image_holder_,
  588. SIGNAL(imageScaled()),
  589. this,
  590. SLOT(onImageScaled())
  591. );
  592. connect(
  593. image_holder_,
  594. SIGNAL(areaEdited()),
  595. this,
  596. SLOT(onAreaEdit())
  597. );
  598. connect(
  599. &options_form_,
  600. SIGNAL(optionsSet()),
  601. this,
  602. SLOT(onOptionsSet())
  603. );
  604. image_holder_->setBoundingBoxList(&list_bounding_box_);
  605. image_holder_->setPolygonList(&list_polygon_);
  606. image_holder_->setLabelColorList(&list_label_colors_);
  607. image_holder_->setMainLabelNum(&main_label_);
  608. image_holder_->setImage(image_);
  609. }
  610. ImageLabeler::~ImageLabeler()
  611. {
  612. delete action_quit_;
  613. delete action_open_labeled_image_;
  614. delete action_open_image_;
  615. delete action_open_images_;
  616. delete action_load_legend_;
  617. delete action_load_pascal_file_;
  618. delete action_load_pascal_poly_;
  619. delete action_save_legend_;
  620. delete action_save_segmented_;
  621. delete action_save_labels_;
  622. delete action_view_normal_;
  623. delete action_view_segmented_;
  624. delete action_undo_;
  625. delete action_redo_;
  626. delete action_bound_box_tool_;
  627. delete action_polygon_tool_;
  628. delete action_tagging_tool_;
  629. delete action_add_description_;
  630. delete action_options_;
  631. delete action_about_;
  632. delete action_help_content_;
  633. delete menu_pascal_;
  634. delete menu_file_;
  635. delete menu_view_;
  636. delete menu_edit_;
  637. delete menu_help_;
  638. delete menu_bar_;
  639. delete action_delete_area_;
  640. delete action_edit_area_;
  641. delete popup_area_list_;
  642. delete action_toggle_priority_;
  643. delete action_set_color_;
  644. delete action_delete_label_;
  645. delete popup_label_list_;
  646. delete action_remove_image_;
  647. delete popup_images_list_;
  648. delete image_;
  649. delete image_holder_;
  650. delete button_add_label_;
  651. delete button_remove_label_;
  652. delete button_bound_box_tool_;
  653. delete button_polygon_tool_;
  654. delete button_tagging_tool_;
  655. delete button_clear_selection_tool_;
  656. delete button_generate_colors_;
  657. delete button_delete_all_labels_;
  658. delete button_next_image_;
  659. delete button_prev_image_;
  660. delete button_confirm_selection_;
  661. delete label_list_areas_;
  662. delete label_toolbox_;
  663. delete label_list_images_;
  664. delete list_areas_;
  665. delete list_label_;
  666. delete list_images_widget_;
  667. delete layout_toolbox_;
  668. delete layout_right_;
  669. delete layout_center_buttons_;
  670. delete layout_frame_image_;
  671. delete layout_center_;
  672. delete layout_labelbox_buttons_;
  673. delete layout_labelbox_;
  674. delete layout_left_;
  675. delete layout_main_;
  676. delete frame_labelbox_;
  677. delete frame_toolbox_;
  678. delete frame_image_;
  679. delete frame_center_;
  680. delete central_widget_;
  681. //delete current_image_;
  682. if (pure_data_) {
  683. delete[] *pure_data_;
  684. delete pure_data_;
  685. }
  686. delete list_images_;
  687. }
  688. void
  689. ImageLabeler::addImage(Image *anImage)
  690. {
  691. QListWidgetItem *newItem = new QListWidgetItem;
  692. QString itemText = QString("%1: %2").
  693. arg(list_images_widget_->count()).
  694. arg(getFilenameFromPath(&(anImage->image_)));
  695. if (anImage->labeled_)
  696. itemText.append(" #labeled");
  697. if (anImage->pas_)
  698. itemText.append(" #pas");
  699. newItem->setText(itemText);
  700. list_images_widget_->addItem(newItem);
  701. list_images_->append(*anImage);
  702. }
  703. void
  704. ImageLabeler::addLabel()
  705. {
  706. QListWidgetItem *newItem = new QListWidgetItem;
  707. QString label;
  708. int itemNum = list_label_->count();
  709. label.append(QString("%1: ").arg(itemNum));
  710. if (itemNum) {
  711. newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  712. label.append("New label");
  713. }
  714. else {
  715. newItem->setFlags(
  716. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  717. Qt::ItemIsEnabled
  718. );
  719. label.append("BACKGROUND");
  720. }
  721. newItem->setText(label);
  722. QPixmap iconPix = QPixmap(20, 20);
  723. QColor color;
  724. if (0 != itemNum)
  725. color = Qt::white;
  726. else
  727. color = Qt::black;
  728. iconPix.fill(color);
  729. QIcon icon(iconPix);
  730. list_label_colors_.append(color.rgb());
  731. newItem->setIcon(icon);
  732. list_label_->addItem(newItem);
  733. list_label_->setItemSelected(newItem, true);
  734. label_ID_ = list_label_->count() - 1;
  735. unsaved_data_ = 1;
  736. }
  737. void
  738. ImageLabeler::addLabel(
  739. int aLabelID,
  740. bool isMain,
  741. QString aLabel
  742. )
  743. {
  744. QListWidgetItem *newItem = new QListWidgetItem;
  745. if (0 == aLabelID) {
  746. aLabel = QString("BACKGROUND");
  747. newItem->setFlags(
  748. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  749. Qt::ItemIsEnabled
  750. );
  751. }
  752. QString label;
  753. label.append(QString("%1: %2").
  754. arg(aLabelID).
  755. arg(aLabel)
  756. );
  757. /* TODO: check if there is another main label and make it common */
  758. if (isMain) {
  759. main_label_ = aLabelID;
  760. }
  761. QPixmap iconPix = QPixmap(20, 20);
  762. QColor color;
  763. if (0 != aLabelID)
  764. color = Qt::white;
  765. else
  766. color = Qt::black;
  767. iconPix.fill(color);
  768. QIcon icon(iconPix);
  769. list_label_colors_.append(color.rgb());
  770. newItem->setIcon(icon);
  771. newItem->setText(label);
  772. newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  773. list_label_->addItem(newItem);
  774. list_label_->setItemSelected(newItem, true);
  775. unsaved_data_ = 1;
  776. }
  777. void
  778. ImageLabeler::editLabel(QListWidgetItem *anItem)
  779. {
  780. QString label = anItem->text();
  781. int itemRow = list_label_->row(anItem);
  782. QString prefix = QString("%1: ").arg(itemRow);
  783. if (-1 != label.indexOf(prefix)) {
  784. return;
  785. /* NOTREACHED */
  786. }
  787. label.prepend(QString("%1: ").arg(itemRow));
  788. if (main_label_ == itemRow) {
  789. label.append(" #main");
  790. }
  791. list_label_->blockSignals(true);
  792. anItem->setText(label);
  793. list_label_->blockSignals(false);
  794. unsaved_data_ = 1;
  795. }
  796. void
  797. ImageLabeler::removeLabel()
  798. {
  799. if (0 == list_label_->count()) {
  800. return;
  801. /* NOTREACHED */
  802. }
  803. //QListWidgetItem *current = list_label_->currentItem();
  804. /* we need to keep BACKGROUND category */
  805. if (label_ID_ < 1) {
  806. return;
  807. /* NOTREACHED */
  808. }
  809. if (list_label_->count() <= label_ID_ ||
  810. list_label_colors_.count() <= label_ID_) {
  811. return;
  812. /* NOTREACHED */
  813. }
  814. list_label_->takeItem(label_ID_);
  815. list_label_colors_.takeAt(label_ID_);
  816. unsaved_data_ = 1;
  817. }
  818. void
  819. ImageLabeler::setLabelID(
  820. QListWidgetItem *anItem
  821. )
  822. {
  823. Q_UNUSED(anItem)
  824. if (!list_label_->count()) {
  825. return;
  826. /* NOTREACHED */
  827. }
  828. label_ID_ = list_label_->row(anItem);
  829. }
  830. void
  831. ImageLabeler::addBBoxArea(
  832. int anID,
  833. BoundingBox aBBox,
  834. int itemID
  835. )
  836. {
  837. QListWidgetItem *newItem = new QListWidgetItem;
  838. if (-1 == itemID)
  839. itemID = list_areas_->count();
  840. QString label;
  841. label.append(QString("%1: ").arg(itemID));
  842. label.append(QString("BBox #%1; ").arg(anID));
  843. label.append(QString("LabelID: %1; ").arg(aBBox.label_ID_));
  844. label.append(
  845. QString("data:%1;%2;%3;%4; ").
  846. arg(aBBox.rect.topLeft().x()).
  847. arg(aBBox.rect.topLeft().y()).
  848. arg(aBBox.rect.width()).
  849. arg(aBBox.rect.height())
  850. );
  851. newItem->setText(label);
  852. //newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  853. list_areas_->insertItem(itemID, newItem);
  854. list_areas_->setItemSelected(newItem, true);
  855. }
  856. void
  857. ImageLabeler::addPolyArea(
  858. int aPolyID,
  859. Polygon aPoly,
  860. int itemID
  861. )
  862. {
  863. QListWidgetItem *newItem = new QListWidgetItem;
  864. if (-1 == itemID)
  865. itemID = list_areas_->count();
  866. QString label;
  867. label.append(QString("%1: ").arg(itemID));
  868. label.append(QString("Poly #%1; ").arg(aPolyID));
  869. label.append(QString("LabelID: %1; ").arg(aPoly.label_ID_));
  870. label.append("points:");
  871. for (int i = 0; i < aPoly.poly.count(); i++) {
  872. label.append(
  873. QString("%1;%2;").
  874. arg(aPoly.poly.point(i).x()).
  875. arg(aPoly.poly.point(i).y())
  876. );
  877. }
  878. newItem->setText(label);
  879. //newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
  880. list_areas_->insertItem(itemID, newItem);
  881. list_areas_->setItemSelected(newItem, true);
  882. }
  883. void
  884. ImageLabeler::editArea()
  885. {
  886. bool oldState = list_areas_->blockSignals(true);
  887. if (!list_areas_->currentItem() || !list_areas_->count()) {
  888. return;
  889. /* NOTREACHED */
  890. }
  891. QListWidgetItem *current = list_areas_->currentItem();
  892. old_area_string_ = current->text();
  893. current->setFlags(current->flags() | Qt::ItemIsEditable);
  894. list_areas_->editItem(current);
  895. list_areas_->blockSignals(oldState);
  896. }
  897. void
  898. ImageLabeler::onAreaItemChange(QListWidgetItem *anItem)
  899. {
  900. list_areas_->blockSignals(true);
  901. QString areaString = anItem->text();
  902. int oldID = -1;
  903. if (-1 != areaString.indexOf("Poly")) {
  904. Polygon *poly = new Polygon;
  905. *poly = polyFromString(&areaString, &oldID);
  906. if (-1 < poly->label_ID_ && !poly->poly.isEmpty() &&
  907. -1 < oldID) {
  908. list_polygon_.takeAt(oldID);
  909. list_polygon_.insert(oldID, poly);
  910. }
  911. else
  912. anItem->setText(old_area_string_);
  913. }
  914. else if (-1 != areaString.indexOf("BBox")) {
  915. BoundingBox *bbox = new BoundingBox;
  916. *bbox = BBoxFromString(&areaString, &oldID);
  917. if (-1 < bbox->label_ID_ && -1 < oldID) {
  918. list_bounding_box_.takeAt(oldID);
  919. list_bounding_box_.insert(oldID, bbox);
  920. }
  921. else
  922. anItem->setText(old_area_string_);
  923. }
  924. else {
  925. showWarning(tr("record format is corrupted, try again"));
  926. anItem->setText(old_area_string_);
  927. }
  928. anItem->setFlags(anItem->flags() ^ Qt::ItemIsEditable);
  929. list_areas_->blockSignals(false);
  930. image_holder_->update();
  931. unsaved_data_ = 1;
  932. }
  933. void
  934. ImageLabeler::onAreaEdit()
  935. {
  936. if (!list_areas_->count() ||
  937. -1 == image_holder_->focusedSelection()) {
  938. //showWarning(tr("You haven't added any label"));
  939. return;
  940. /* NOTREACHED */
  941. }
  942. int figureID = image_holder_->focusedSelection();
  943. Figure figure = image_holder_->focusedSelectionType();
  944. for (int i = 0; i < list_areas_->count(); i++) {
  945. QListWidgetItem *item = list_areas_->item(i);
  946. QString text = item->text();
  947. if (RectFigure == figure && -1 != text.indexOf("BBox")) {
  948. bool ok = 0;
  949. int num = getNumFromString(&text, "BBox #", ";", &ok);
  950. if (ok && num == figureID) {
  951. list_areas_->takeItem(i);
  952. addBBoxArea(num, *list_bounding_box_.at(num), i);
  953. }
  954. }
  955. else if (PolyFigure == figure && -1 != text.indexOf("Poly")) {
  956. bool ok = 0;
  957. int num = getNumFromString(&text, "Poly #", ";", &ok);
  958. if (ok && num == figureID) {
  959. list_areas_->takeItem(i);
  960. addPolyArea(num, *list_polygon_.at(num), i);
  961. }
  962. }
  963. }
  964. unsaved_data_ = 1;
  965. }
  966. void
  967. ImageLabeler::deleteArea()
  968. {
  969. if (!list_areas_->currentItem() || !list_areas_->count()) {
  970. return;
  971. /* NOTREACHED */
  972. }
  973. QString text = list_areas_->currentItem()->text();
  974. bool ok = 0;
  975. //int labelID = getNumFromString(&text, "LabelID:", ";", &ok);
  976. // if (!ok) {
  977. // return;
  978. // /* NOTREACHED */
  979. // }
  980. /*
  981. * 0 - bbox
  982. * 1 - poly
  983. */
  984. QString shape;
  985. if (-1 != text.indexOf("BBox"))
  986. shape = QString("BBox");
  987. else if (-1 != text.indexOf("Poly"))
  988. shape = QString("Poly");
  989. else {
  990. return;
  991. /* NOTREACHED */
  992. }
  993. int areaNum = getNumFromString(&text, "#", ";", &ok);
  994. if (!ok) {
  995. return;
  996. /* NOTREACHED */
  997. }
  998. int currentItemRow = list_areas_->row(list_areas_->currentItem());
  999. /* changing all shapes(depends on current) which are next in the list */
  1000. for (int i = list_areas_->count() - 1; i > currentItemRow; i--) {
  1001. QListWidgetItem item = *(list_areas_->item(i));
  1002. QString newText = item.text();
  1003. if (-1 == newText.indexOf(shape))
  1004. continue;
  1005. int num = getNumFromString(&newText, "#", ";", &ok);
  1006. num--;
  1007. QString numString = QString("%1").arg(num);
  1008. int numPos = newText.indexOf("#") + 1;
  1009. newText.replace(numPos, numString.size(), numString);
  1010. list_areas_->takeItem(i);
  1011. list_areas_->insertItem(i, newText);
  1012. }
  1013. list_areas_->takeItem(currentItemRow);
  1014. if (shape == "BBox")
  1015. list_bounding_box_.removeAt(areaNum);
  1016. else
  1017. list_polygon_.removeAt(areaNum);
  1018. image_holder_->update();
  1019. unsaved_data_ = 1;
  1020. }
  1021. void
  1022. ImageLabeler::toggleLabelPriority()
  1023. {
  1024. if (!list_label_->count()) {
  1025. return;
  1026. /* NOTREACHED */
  1027. }
  1028. QListWidgetItem *item = list_label_->currentItem();
  1029. /* because we need to keep BACKGROUND category */
  1030. if (0 == list_label_->row(item)) {
  1031. return;
  1032. /* NOTREACHED */
  1033. }
  1034. int itemRow = list_label_->row(item);
  1035. QString text = item->text();
  1036. /* cleaning previous " #main" mark */
  1037. if (-1 != main_label_) {
  1038. QListWidgetItem *lastMain = list_label_->item(main_label_);
  1039. QString lastMainText = lastMain->text();
  1040. int mainPos = lastMainText.indexOf(" #main");
  1041. lastMainText = lastMainText.mid(0, mainPos);
  1042. list_label_->blockSignals(true);
  1043. lastMain->setText(lastMainText);
  1044. list_label_->blockSignals(false);
  1045. }
  1046. if (main_label_ == itemRow) {
  1047. int mainPos = text.indexOf(" #main");
  1048. text = text.mid(0, mainPos);
  1049. main_label_ = -1;
  1050. }
  1051. else {
  1052. text.append(" #main");
  1053. main_label_ = list_label_->row(item);
  1054. }
  1055. list_label_->blockSignals(true);
  1056. item->setText(text);
  1057. list_label_->blockSignals(false);
  1058. image_holder_->update();
  1059. unsaved_data_ = 1;
  1060. }
  1061. void
  1062. ImageLabeler::getImagesFromDir(const QDir &dir)
  1063. {
  1064. /* avoiding freezing during recursive search for files */
  1065. QApplication::processEvents();
  1066. if (interrupt_search_) {
  1067. clearAll();
  1068. return;
  1069. }
  1070. QStringList filenameFilter;
  1071. filenameFilter <<
  1072. "*.jpeg" <<
  1073. "*.jpg" <<
  1074. "*.gif" <<
  1075. "*.png" <<
  1076. "*.bmp" <<
  1077. "*.tiff" <<
  1078. "*.dat"
  1079. ;
  1080. QStringList listImages =
  1081. dir.entryList(filenameFilter, QDir::Files);
  1082. QStringList ignoredFiles;
  1083. foreach (QString file, listImages) {
  1084. if (ignoredFiles.contains(file) ||
  1085. file.contains("_segmented", Qt::CaseInsensitive) ||
  1086. file.contains(".dat", Qt::CaseInsensitive)) {
  1087. continue;
  1088. }
  1089. Image newImage;
  1090. newImage.image_ = dir.absoluteFilePath(file);
  1091. /* TODO: think about loading pascal files */
  1092. newImage.pas_ = 0;
  1093. QString labeled = alterFileName(file, "_labeled");
  1094. labeled = removePath(labeled);
  1095. labeled.append(".dat");
  1096. if (listImages.contains(labeled, Qt::CaseInsensitive))
  1097. newImage.labeled_ = 1;
  1098. else
  1099. {
  1100. newImage.labeled_ = 0;
  1101. }
  1102. addImage(&newImage);
  1103. }
  1104. QStringList listDir = dir.entryList(QDir::Dirs);
  1105. foreach (QString subdir, listDir) {
  1106. if ("." == subdir || ".." == subdir)
  1107. continue;
  1108. getImagesFromDir(QDir(dir.absoluteFilePath(subdir)));
  1109. }
  1110. }
  1111. void
  1112. ImageLabeler::nextImage()
  1113. {
  1114. if (list_images_->isEmpty()) {
  1115. return;
  1116. /* NOTREACHED */
  1117. }
  1118. if (askForUnsavedData()) {
  1119. return;
  1120. /* NOTREACHED */
  1121. }
  1122. if (list_images_widget_->count() - 1 == image_ID_) {
  1123. //current_image_ = list_images_->at(0).image_;
  1124. image_ID_ = 0;
  1125. }
  1126. else {
  1127. image_ID_ ++;
  1128. //current_image_ = list_images_->at(image_ID_).image_;
  1129. }
  1130. if (!selectImage(image_ID_)) {
  1131. showWarning(tr("Next image is not available"));
  1132. return;
  1133. /* NOTREACHED */
  1134. }
  1135. list_images_widget_->setCurrentRow(image_ID_);
  1136. if (current_image_.isEmpty()) {
  1137. return;
  1138. /* NOTREACHED */
  1139. }
  1140. QString winTitle;
  1141. winTitle.append("ImageLabeler - ");
  1142. winTitle.append(current_image_);
  1143. setWindowTitle(winTitle);
  1144. image_->load(current_image_);
  1145. image_holder_->resize(image_->size());
  1146. image_holder_->setPixmap(*image_);
  1147. list_bounding_box_.clear();
  1148. list_polygon_.clear();
  1149. list_areas_->clear();
  1150. image_holder_->clearAll();
  1151. segmented_image_.clear();
  1152. clearLabelColorList();
  1153. //setWindowTitle(tr("ImageLabeler"));
  1154. }
  1155. void
  1156. ImageLabeler::prevImage()
  1157. {
  1158. if (!list_images_widget_->count()) {
  1159. return;
  1160. /* NOTREACHED */
  1161. }
  1162. if (askForUnsavedData()) {
  1163. return;
  1164. /* NOTREACHED */
  1165. }
  1166. if (!image_ID_) {
  1167. image_ID_ = list_images_widget_->count() - 1;
  1168. //current_image_ = list_images_->at(image_ID_).image_;
  1169. }
  1170. else {
  1171. image_ID_--;
  1172. //current_image_ = list_images_->at(image_ID_).image_;
  1173. }
  1174. list_images_widget_->setCurrentRow(image_ID_);
  1175. if (!selectImage(image_ID_)) {
  1176. showWarning(tr("Next image is not available"));
  1177. return;
  1178. /* NOTREACHED */
  1179. }
  1180. QString winTitle;
  1181. winTitle.append("ImageLabeler - ");
  1182. winTitle.append(current_image_);
  1183. setWindowTitle(winTitle);
  1184. image_->load(current_image_);
  1185. image_holder_->resize(image_->size());
  1186. image_holder_->setPixmap(*image_);
  1187. list_bounding_box_.clear();
  1188. list_polygon_.clear();
  1189. list_areas_->clear();
  1190. image_holder_->clearAll();
  1191. segmented_image_.clear();
  1192. clearLabelColorList();
  1193. //setWindowTitle(tr("ImageLabeler"));
  1194. }
  1195. void
  1196. ImageLabeler::saveAllInfo()
  1197. {
  1198. if (!list_images_widget_->count()) {
  1199. showWarning("You have not opened any image yet");
  1200. return;
  1201. /* NOTREACHED */
  1202. }
  1203. /* ------------------------------------------------------------------------
  1204. * XML part
  1205. */
  1206. QDomDocument doc(tr("ImageLabeler"));
  1207. QDomElement root = doc.createElement(tr("pixelwise_labeling"));
  1208. doc.appendChild(root);
  1209. QDomElement image = doc.createElement(tr("image"));
  1210. root.appendChild(image);
  1211. QDomText pathToImage = doc.createTextNode(current_image_);
  1212. image.appendChild(pathToImage);
  1213. if (!segmented_image_.isEmpty()) {
  1214. QDomElement segmentedImage = doc.createElement(tr("segmented"));
  1215. root.appendChild(segmentedImage);
  1216. QDomText pathToSegmented = doc.createTextNode(segmented_image_);
  1217. segmentedImage.appendChild(pathToSegmented);
  1218. }
  1219. QDomElement description = doc.createElement(tr("description"));
  1220. root.appendChild(description);
  1221. QDomText descriptionText = doc.createTextNode(image_description_);
  1222. description.appendChild(descriptionText);
  1223. legendToXml(&doc, &root);
  1224. objectsToXml(&doc, &root);
  1225. setPureData();
  1226. QDomElement pureData = doc.createElement(tr("pure_data"));
  1227. QSize imageSize = image_->size();
  1228. QString pixelValues;
  1229. for (int i = 0; i < imageSize.height(); i++) {
  1230. for (int j = 0; j < imageSize.width(); j++) {
  1231. pixelValues.append(QString("%1").arg(pure_data_[i][j]));
  1232. //pixelValues.append(QString(""));
  1233. }
  1234. pixelValues.append("\n");
  1235. }
  1236. QDomText pureDataText = doc.createTextNode(pixelValues);
  1237. pureData.appendChild(pureDataText);
  1238. root.appendChild(pureData);
  1239. QString xml = doc.toString();
  1240. /* ------------------------------------------------------------------------
  1241. * XML part ends
  1242. */
  1243. QFileDialog fileDialog(0, tr("Save all info"));
  1244. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1245. fileDialog.setDefaultSuffix("dat");
  1246. fileDialog.setFileMode(QFileDialog::AnyFile);
  1247. QString dir = getDirFromPath(&current_image_);
  1248. /* altering the name of a new file */
  1249. QString newFileName = alterFileName(current_image_, "_labeled");
  1250. newFileName = removePath(newFileName);
  1251. fileDialog.selectFile(newFileName);
  1252. fileDialog.setDirectory(dir);
  1253. QString filename;
  1254. if (fileDialog.exec()) {
  1255. filename = fileDialog.selectedFiles().last();
  1256. }
  1257. else {
  1258. //showWarning(tr("Can not open file dialog"));
  1259. return;
  1260. /* NOTREACHED */
  1261. }
  1262. if (filename.isEmpty()) {
  1263. return;
  1264. /* NOTREACHED */
  1265. }
  1266. QFile file(filename);
  1267. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1268. showWarning(tr("Can not open file for writing"));
  1269. return;
  1270. /* NOTREACHED */
  1271. }
  1272. file.write(xml.toLocal8Bit());
  1273. file.close();
  1274. unsaved_data_ = 0;
  1275. }
  1276. void
  1277. ImageLabeler::saveSegmentedPicture()
  1278. {
  1279. if (list_bounding_box_.isEmpty() && list_polygon_.isEmpty()) {
  1280. return;
  1281. /* NOTREACHED */
  1282. }
  1283. setPureData();
  1284. // QString suffix = *current_image_;
  1285. // int dotPos = suffix.lastIndexOf(".");
  1286. // suffix.mid(dotPos + 1, suffix.size() - dotPos - 1);
  1287. QFileDialog fileDialog(0, tr("Save segmented picture"));
  1288. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1289. fileDialog.setDefaultSuffix("png");
  1290. fileDialog.setFileMode(QFileDialog::AnyFile);
  1291. QString dir = getDirFromPath(&current_image_);
  1292. /* altering the name of a new file */
  1293. QString newFileName = alterFileName(current_image_, "_segmented");
  1294. newFileName = removePath(newFileName);
  1295. fileDialog.selectFile(newFileName);
  1296. fileDialog.setDirectory(dir);
  1297. QString filename;
  1298. if (fileDialog.exec()) {
  1299. filename = fileDialog.selectedFiles().last();
  1300. }
  1301. else {
  1302. return;
  1303. /* NOTREACHED */
  1304. }
  1305. QSize imageSize = image_holder_->pixmap()->size();
  1306. QImage newImage(imageSize, QImage::Format_RGB32);
  1307. bool generateColorsFlag = auto_color_generation_;
  1308. bool flag = 0;
  1309. /* checking if all the colors are are different from white(default) */
  1310. if (!generateColorsFlag) {
  1311. for (int i = 1; i < list_label_->count(); i++) {
  1312. if (list_label_colors_.at(i) == 0xffffffff && !flag)
  1313. flag = 1;
  1314. else if (list_label_colors_.at(i) == 0xffffffff && flag) {
  1315. QMessageBox msgBox;
  1316. msgBox.setText(tr("There are few labels with default white color."));
  1317. msgBox.setInformativeText(tr("Do you want to generate all colors automatically? Otherwise you'll have to do it manually."));
  1318. msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  1319. msgBox.setDefaultButton(QMessageBox::Yes);
  1320. msgBox.setIcon(QMessageBox::Question);
  1321. int ret = msgBox.exec();
  1322. if (QMessageBox::Yes == ret) {
  1323. generateColorsFlag = 1;
  1324. break;
  1325. }
  1326. else {
  1327. return;
  1328. /* NOTREACHED */
  1329. }
  1330. }
  1331. }
  1332. }
  1333. /* generating colors for labels */
  1334. if (list_label_colors_.count() < list_label_->count() ||
  1335. generateColorsFlag) {
  1336. generateColors();
  1337. }
  1338. for (int i = 0; i < imageSize.height(); i++)
  1339. for (int j = 0; j < imageSize.width(); j++) {
  1340. newImage.setPixel(j, i, list_label_colors_.at(pure_data_[i][j]));
  1341. }
  1342. if (!newImage.save(filename, "png", 100)) {
  1343. showWarning(tr("An error occurred while saving the segmented image"));
  1344. return;
  1345. /* NOTREACHED */
  1346. }
  1347. segmented_image_ = filename;
  1348. action_view_segmented_->setEnabled(true);
  1349. }
  1350. void
  1351. ImageLabeler::saveLegend()
  1352. {
  1353. if (!list_label_->count()) {
  1354. showWarning("You have not added any label yet");
  1355. return;
  1356. /* NOTREACHED */
  1357. }
  1358. /* ------------------------------------------------------------------------
  1359. * XML part
  1360. */
  1361. QDomDocument doc(tr("ImageLabeler"));
  1362. QDomElement root = doc.createElement(tr("root"));
  1363. doc.appendChild(root);
  1364. legendToXml(&doc, &root);
  1365. QString xml = doc.toString();
  1366. /* ------------------------------------------------------------------------
  1367. * XML part ends
  1368. */
  1369. QFileDialog fileDialog(0, tr("Save legend"));
  1370. fileDialog.setAcceptMode(QFileDialog::AcceptSave);
  1371. fileDialog.setDefaultSuffix("dat");
  1372. fileDialog.setFileMode(QFileDialog::AnyFile);
  1373. QString dir = getDirFromPath(&current_image_);
  1374. /* altering the name of a new file */
  1375. QString newFileName = alterFileName(current_image_, "_legend");
  1376. newFileName = removePath(newFileName);
  1377. fileDialog.selectFile(newFileName);
  1378. fileDialog.setDirectory(dir);
  1379. QString filename;
  1380. if (fileDialog.exec()) {
  1381. filename = fileDialog.selectedFiles().last();
  1382. }
  1383. else {
  1384. //showWarning(tr("Can not open file dialog"));
  1385. return;
  1386. /* NOTREACHED */
  1387. }
  1388. if (filename.isEmpty()) {
  1389. return;
  1390. /* NOTREACHED */
  1391. }
  1392. QFile file(filename);
  1393. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  1394. showWarning(tr("Can not open file for writing"));
  1395. return;
  1396. /* NOTREACHED */
  1397. }
  1398. file.write(xml.toLocal8Bit());
  1399. file.close();
  1400. }
  1401. void
  1402. ImageLabeler::loadInfo()
  1403. {
  1404. if (askForUnsavedData()) {
  1405. return;
  1406. /* NOTREACHED */
  1407. }
  1408. QFileDialog fileDialog(0, tr("Load file with info"));
  1409. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1410. fileDialog.setDefaultSuffix("dat");
  1411. fileDialog.setFileMode(QFileDialog::AnyFile);
  1412. QString filename;
  1413. if (fileDialog.exec()) {
  1414. filename = fileDialog.selectedFiles().last();
  1415. }
  1416. else {
  1417. //showWarning(tr("Can not open file dialog"));
  1418. return;
  1419. /* NOTREACHED */
  1420. }
  1421. clearAllTool();
  1422. clearLabelList();
  1423. if (loadInfo(filename)) {
  1424. enableTools();
  1425. Image newImage;
  1426. newImage.image_ = current_image_;
  1427. newImage.labeled_ = 1;
  1428. newImage.pas_ = 0;
  1429. addImage(&newImage);
  1430. image_ID_ = list_images_widget_->count() - 1;
  1431. list_images_widget_->setCurrentRow(image_ID_);
  1432. }
  1433. unsaved_data_ = 0;
  1434. }
  1435. bool
  1436. ImageLabeler::loadInfo(QString filename)
  1437. {
  1438. QDomDocument doc("Image Labeler");
  1439. QFile file(filename);
  1440. if (!file.open(QIODevice::ReadOnly)) {
  1441. showWarning(tr("Can not open such file"));
  1442. return false;
  1443. /* NOTREACHED */
  1444. }
  1445. QString errMsg;
  1446. if (!doc.setContent(&file, &errMsg)) {
  1447. showWarning(errMsg);
  1448. file.close();
  1449. return false;
  1450. /* NOTREACHED */
  1451. }
  1452. file.close();
  1453. //clearAll();
  1454. //enableTools();
  1455. /* getting all info */
  1456. QDomElement elements = doc.documentElement();
  1457. QDomNode rootNode = elements.firstChild();
  1458. QString string;
  1459. while(!rootNode.isNull()) {
  1460. QDomElement element = rootNode.toElement();
  1461. if(!element.isNull()) {
  1462. /* path to the image */
  1463. if (element.tagName() == "image") {
  1464. string = element.text();
  1465. if (string.isEmpty()) {
  1466. showWarning(
  1467. tr(
  1468. "The file with data doesn't contain path to the image"
  1469. )
  1470. );
  1471. return false;
  1472. /* NOTREACHED */
  1473. }
  1474. if (!image_->load(string)) {
  1475. return false;
  1476. /* NOTREACHED */
  1477. }
  1478. // Image newImage;
  1479. // newImage.image_ = string;
  1480. // newImage.labeled_ = 1;
  1481. // addImage(&newImage);
  1482. current_image_ = string;
  1483. // image_ID_ = list_images_widget_->count() - 1;
  1484. // list_images_widget_->setCurrentRow(image_ID_);
  1485. QString winTitle;
  1486. winTitle.append("ImageLabeler - ");
  1487. winTitle.append(current_image_);
  1488. setWindowTitle(winTitle);
  1489. image_holder_->resize(image_->size());
  1490. image_holder_->setPixmap(*image_);
  1491. }
  1492. /* path to the segmented image */
  1493. if (element.tagName() == "segmented") {
  1494. string = element.text();
  1495. if (string.isEmpty()) {
  1496. continue;
  1497. }
  1498. segmented_image_ = string;
  1499. action_view_segmented_->setEnabled(true);
  1500. }
  1501. /* image description */
  1502. else if (element.tagName() == "description" &&
  1503. !element.text().isEmpty()) {
  1504. image_description_ = element.text();
  1505. }
  1506. /* legend */
  1507. else if (element.tagName() == "legend") {
  1508. loadLegendFromNode(&element);
  1509. }
  1510. /* objects */
  1511. else if (element.tagName() == "objects") {
  1512. QDomNode subNode = element.firstChild();
  1513. QDomElement subElement;
  1514. while(!subNode.isNull()) {
  1515. subElement = subNode.toElement();
  1516. if (subElement.isNull() || subElement.text().isEmpty()) {
  1517. subNode = subNode.nextSibling();
  1518. continue;
  1519. }
  1520. string = subElement.attribute("id");
  1521. bool ok = 1;
  1522. int id = string.toInt(&ok, 10);
  1523. if (!ok) {
  1524. qDebug() <<
  1525. "loadInfo: "
  1526. "poly id format is corrupted";
  1527. subNode = subNode.nextSibling();
  1528. continue;
  1529. }
  1530. string = subElement.text();
  1531. if (subElement.tagName() == "bbox") {
  1532. addBBoxFromData(&string, &id);
  1533. }
  1534. if (subElement.tagName() == "poly") {
  1535. addPolyFromData(&string, &id);
  1536. }
  1537. subNode = subNode.nextSibling();
  1538. }
  1539. }
  1540. }
  1541. rootNode = rootNode.nextSibling();
  1542. }
  1543. unsaved_data_ = 0;
  1544. return true;
  1545. }
  1546. void
  1547. ImageLabeler::loadPascalFile()
  1548. {
  1549. if (askForUnsavedData()) {
  1550. return;
  1551. /* NOTREACHED */
  1552. }
  1553. QFileDialog fileDialog(0, tr("Load pascal file"));
  1554. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1555. fileDialog.setDefaultSuffix("xml");
  1556. fileDialog.setFileMode(QFileDialog::AnyFile);
  1557. QStringList filters;
  1558. filters << "PASCAL xml data (*.xml)"
  1559. << "Any files (*)";
  1560. fileDialog.setNameFilters(filters);
  1561. QString filename;
  1562. if (fileDialog.exec()) {
  1563. filename = fileDialog.selectedFiles().last();
  1564. }
  1565. else {
  1566. //showWarning(tr("Can not open file dialog"));
  1567. return;
  1568. /* NOTREACHED */
  1569. }
  1570. clearAllTool();
  1571. clearLabelList();
  1572. if (loadPascalFile(filename)) {
  1573. enableTools();
  1574. Image newImage;
  1575. newImage.image_ = current_image_;
  1576. newImage.labeled_ = 1;
  1577. newImage.pas_ = 1;
  1578. addImage(&newImage);
  1579. image_ID_ = list_images_widget_->count() - 1;
  1580. list_images_widget_->setCurrentRow(image_ID_);
  1581. }
  1582. unsaved_data_ = 0;
  1583. }
  1584. bool
  1585. ImageLabeler::loadPascalFile(QString aFilename)
  1586. {
  1587. QDomDocument doc;
  1588. QFile file(aFilename);
  1589. if (!file.open(QIODevice::ReadOnly)) {
  1590. showWarning(tr("Can not open such file"));
  1591. return false;
  1592. /* NOTREACHED */
  1593. }
  1594. QString errMsg;
  1595. if (!doc.setContent(&file, &errMsg)) {
  1596. showWarning(errMsg);
  1597. file.close();
  1598. return false;
  1599. /* NOTREACHED */
  1600. }
  1601. file.close();
  1602. //clearAll();
  1603. //enableTools();
  1604. /* getting all info */
  1605. QDomElement elements = doc.documentElement();
  1606. QDomNode rootNode = elements.firstChild();
  1607. QString string;
  1608. QString path = getPathFromFilename(aFilename);
  1609. QString filename;
  1610. QStringList labels;
  1611. labels << "BACKGROUND";
  1612. int labelID;
  1613. while(!rootNode.isNull()) {
  1614. QDomElement element = rootNode.toElement();
  1615. if(!element.isNull()) {
  1616. /* folder */
  1617. if (element.tagName() == "folder") {
  1618. string = element.text();
  1619. if (!string.isEmpty()) {
  1620. path.append(string);
  1621. }
  1622. }
  1623. /* filename */
  1624. else if (element.tagName() == "filename") {
  1625. string = element.text();
  1626. if (!string.isEmpty()) {
  1627. filename = string;
  1628. }
  1629. }
  1630. /* object */
  1631. else if (element.tagName() == "object") {
  1632. QDomNode subNode = element.firstChild();
  1633. QDomElement subElement;
  1634. while(!subNode.isNull()) {
  1635. subElement = subNode.toElement();
  1636. if (subElement.isNull() || subElement.text().isEmpty()) {
  1637. subNode = subNode.nextSibling();
  1638. continue;
  1639. }
  1640. /* label */
  1641. if (subElement.tagName() == "name") {
  1642. string = subElement.text();
  1643. if (!string.isEmpty() &&
  1644. !labels.contains(string, Qt::CaseInsensitive))
  1645. {
  1646. addLabel(list_label_->count(), 0, string);
  1647. labelID = labels.count();
  1648. labels << string;
  1649. }
  1650. else if (labels.contains(string, Qt::CaseInsensitive)) {
  1651. for (int i = 0; i < labels.count(); i++) {
  1652. if (labels.at(i) == string)
  1653. labelID = i;
  1654. }
  1655. }
  1656. }
  1657. if (subElement.tagName() == "bndbox") {
  1658. /* 2 points */
  1659. QPoint topLeft;
  1660. QPoint bottomRight;
  1661. QDomNode bboxNode = subElement.firstChild();
  1662. QDomElement bboxElement;
  1663. while(!bboxNode.isNull()) {
  1664. bboxElement = bboxNode.toElement();
  1665. string.clear();
  1666. bool ok = 1;
  1667. if (bboxElement.tagName() == "xmin") {
  1668. string = bboxElement.text();
  1669. if (string.isEmpty())
  1670. ok = 0;
  1671. double xmin = string.toDouble(&ok);
  1672. if (ok)
  1673. topLeft.setX(qRound(xmin));
  1674. }
  1675. else if (bboxElement.tagName() == "ymin") {
  1676. string = bboxElement.text();
  1677. if (string.isEmpty())
  1678. ok = 0;
  1679. double ymin = string.toDouble(&ok);
  1680. if (ok)
  1681. topLeft.setY(qRound(ymin));
  1682. }
  1683. else if (bboxElement.tagName() == "xmax") {
  1684. string = bboxElement.text();
  1685. if (string.isEmpty())
  1686. ok = 0;
  1687. double xmax = string.toDouble(&ok);
  1688. if (ok)
  1689. bottomRight.setX(qRound(xmax));
  1690. }
  1691. else if (bboxElement.tagName() == "ymax") {
  1692. string = bboxElement.text();
  1693. if (string.isEmpty())
  1694. ok = 0;
  1695. double ymax = string.toDouble(&ok);
  1696. if (ok)
  1697. bottomRight.setY(qRound(ymax));
  1698. }
  1699. if (string.isEmpty() || !ok)
  1700. break;
  1701. bboxNode = bboxNode.nextSibling();
  1702. if (bboxNode.isNull()) {
  1703. BoundingBox *bbox = new BoundingBox;
  1704. bbox->rect.setTopLeft(topLeft);
  1705. bbox->rect.setBottomRight(bottomRight);
  1706. bbox->label_ID_ = labelID;
  1707. addBBox(bbox);
  1708. }
  1709. }
  1710. }
  1711. subNode = subNode.nextSibling();
  1712. }
  1713. }
  1714. }
  1715. rootNode = rootNode.nextSibling();
  1716. }
  1717. if (!image_->load(path + "/" + filename)) {
  1718. return false;
  1719. /* NOTREACHED */
  1720. }
  1721. current_image_ = path + filename;
  1722. QString winTitle;
  1723. winTitle.append("ImageLabeler - ");
  1724. winTitle.append(current_image_);
  1725. setWindowTitle(winTitle);
  1726. image_holder_->resize(image_->size());
  1727. image_holder_->setPixmap(*image_);
  1728. unsaved_data_ = 0;
  1729. return true;
  1730. }
  1731. void
  1732. ImageLabeler::loadPascalPolys()
  1733. {
  1734. if (current_image_.isEmpty()) {
  1735. return;
  1736. /* NOTREACHED */
  1737. }
  1738. QFileDialog fileDialog(0, tr("Load pascal polygons"));
  1739. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  1740. fileDialog.setDefaultSuffix("polygon");
  1741. fileDialog.setFileMode(QFileDialog::AnyFile);
  1742. QStringList filters;
  1743. filters << "PASCAL polygons (*.polygon)"
  1744. << "Any files (*)";
  1745. fileDialog.setNameFilters(filters);
  1746. QString filename;
  1747. if (fileDialog.exec()) {
  1748. filename = fileDialog.selectedFiles().last();
  1749. }
  1750. else {
  1751. //showWarning(tr("Can not open file dialog"));
  1752. return;
  1753. /* NOTREACHED */
  1754. }
  1755. //clearAllTool();
  1756. //clearLabelList();
  1757. if (!loadPascalPolys(filename)) {
  1758. showWarning(tr("File format is corrupted."));
  1759. }
  1760. unsaved_data_ = 0;
  1761. image_holder_->update();
  1762. }
  1763. bool
  1764. ImageLabeler::loadPascalPolys(QString aFilename)
  1765. {
  1766. QFile file(aFilename);
  1767. if (!file.open(QIODevice::ReadOnly)) {
  1768. showWarning(tr("Can not open such file"));
  1769. return false;
  1770. /* NOTREACHED */
  1771. }
  1772. QByteArray data = file.readAll();
  1773. file.close();
  1774. QString label;
  1775. int lastSpace = 0;
  1776. int pointCount = 0;
  1777. Polygon *poly = 0;
  1778. QPoint point;
  1779. bool evenFlag = 0;
  1780. //for (int i = 0; i < data.length(); i++) {
  1781. int i = 0;
  1782. while (i < data.length()) {
  1783. if (data.at(i) == ' ' && label.isEmpty() && !pointCount) {
  1784. label = QString(data.mid(lastSpace, i - lastSpace));
  1785. lastSpace = i + 1;
  1786. }
  1787. else if (data.at(i) == ' ' && !label.isEmpty() && !pointCount) {
  1788. QString num = QString(data.mid(lastSpace, i - lastSpace));
  1789. bool ok = 1;
  1790. pointCount = num.toInt(&ok, 10);
  1791. if (!ok) {
  1792. return false;
  1793. /* NOTREACHED */
  1794. }
  1795. poly = new Polygon;
  1796. poly->label_ID_ = -1;
  1797. lastSpace = i + 1;
  1798. }
  1799. else if (data.at(i) == ' ' &&
  1800. !label.isEmpty() &&
  1801. pointCount &&
  1802. !evenFlag)
  1803. {
  1804. evenFlag = 1;
  1805. QString num = QString(data.mid(lastSpace, i - lastSpace));
  1806. bool ok = 1;
  1807. int coor = qRound(num.toDouble(&ok));
  1808. if (!ok) {
  1809. return false;
  1810. /* NOTREACHED */
  1811. }
  1812. point.setX(coor);
  1813. lastSpace = i + 1;
  1814. }
  1815. else if (data.at(i) == ' ' &&
  1816. !label.isEmpty() &&
  1817. pointCount &&
  1818. evenFlag)
  1819. {
  1820. evenFlag = 0;
  1821. pointCount--;
  1822. QString num = QString(data.mid(lastSpace, i - lastSpace));
  1823. bool ok = 1;
  1824. int coor = qRound(num.toDouble(&ok));
  1825. if (!ok) {
  1826. return false;
  1827. /* NOTREACHED */
  1828. }
  1829. point.setY(coor);
  1830. lastSpace = i + 1;
  1831. poly->poly << point;
  1832. }
  1833. else if (data.at(i) == '\n') {
  1834. if (pointCount || !poly) {
  1835. return false;
  1836. /* NOTREACHED */
  1837. }
  1838. lastSpace = i + 1;
  1839. for (int j = 0; j < list_label_->count(); j++) {
  1840. if (list_label_->item(j)->text().contains(label, Qt::CaseInsensitive)) {
  1841. poly->label_ID_ = j;
  1842. }
  1843. }
  1844. if (-1 == poly->label_ID_) {
  1845. int labelID = list_label_->count();
  1846. addLabel(labelID, 0, label);
  1847. poly->label_ID_ = labelID;
  1848. }
  1849. addPoly(poly);
  1850. label.clear();
  1851. }
  1852. i++;
  1853. }
  1854. //clearAll();
  1855. //enableTools();
  1856. return true;
  1857. }
  1858. void
  1859. ImageLabeler::loadImage()
  1860. {
  1861. if (askForUnsavedData()) {
  1862. return;
  1863. /* NOTREACHED */
  1864. }
  1865. QFileDialog fileDialog(0, tr("Load image"));
  1866. fileDialog.setFileMode(QFileDialog::ExistingFile);
  1867. QStringList filters;
  1868. filters << "Image files (*.jpeg *.jpg *.gif *.png *.bmp *.tiff)"
  1869. << "Any files (*)";
  1870. fileDialog.setNameFilters(filters);
  1871. QString filename;
  1872. if (fileDialog.exec()) {
  1873. filename = fileDialog.selectedFiles().last();
  1874. }
  1875. else {
  1876. //showWarning(tr("Could not open file dialog"));
  1877. return;
  1878. /* NOTREACHED */
  1879. }
  1880. if (filename.isEmpty()) {
  1881. return;
  1882. /* NOTREACHED */
  1883. }
  1884. QString dirPath = getPathFromFilename(filename);
  1885. QDir dir(dirPath);
  1886. QStringList filter;
  1887. filter << "*.dat";
  1888. QStringList fileList = dir.entryList(filter, QDir::Files);
  1889. QString labeled = alterFileName(filename, "_labeled");
  1890. labeled = removePath(labeled);
  1891. labeled.append(".dat");
  1892. if (fileList.contains(labeled, Qt::CaseInsensitive)) {
  1893. labeled = dir.absoluteFilePath(labeled);
  1894. loadInfo(labeled);
  1895. Image *newImage = new Image;
  1896. newImage->image_ = filename;
  1897. newImage->labeled_ = 1;
  1898. newImage->pas_ = 0;
  1899. addImage(newImage);
  1900. enableTools();
  1901. return;
  1902. /* NOTREACHED */
  1903. }
  1904. clearAllTool();
  1905. QString winTitle;
  1906. winTitle.append("ImageLabeler - ");
  1907. winTitle.append(current_image_);
  1908. setWindowTitle(winTitle);
  1909. if (!image_->load(filename)) {
  1910. return;
  1911. /* NOTREACHED */
  1912. }
  1913. image_holder_->resize(image_->size());
  1914. image_holder_->setPixmap(*image_);
  1915. current_image_ = filename;
  1916. image_ID_ = list_images_widget_->count() - 1;
  1917. list_images_widget_->setCurrentRow(image_ID_);
  1918. Image *newImage = new Image;
  1919. newImage->image_ = filename;
  1920. newImage->labeled_ = 0;
  1921. newImage->pas_ = 0;
  1922. addImage(newImage);
  1923. if (!list_images_widget_->count()) {
  1924. return;
  1925. /* NOTREACHED */
  1926. }
  1927. enableTools();
  1928. }
  1929. void
  1930. ImageLabeler::loadImages()
  1931. {
  1932. if (askForUnsavedData()) {
  1933. return;
  1934. /* NOTREACHED */
  1935. }
  1936. QFileDialog fileDialog(0, tr("Load images"));
  1937. fileDialog.setFileMode(QFileDialog::Directory);
  1938. QString dirName("");
  1939. if (fileDialog.exec()) {
  1940. /* TODO: make it possible to select multiple folders */
  1941. dirName = fileDialog.selectedFiles().last();
  1942. }
  1943. else {
  1944. //showWarning(tr("Could not open file dialog"));
  1945. return;
  1946. /* NOTREACHED */
  1947. }
  1948. clearAllTool();
  1949. QWidget *searchInProgress = new QWidget(0);
  1950. searchInProgress->setWindowTitle(tr("Loading images"));
  1951. QLabel *info = new QLabel(searchInProgress);
  1952. info->setText(
  1953. tr("Program is looking for all image files in your directory recursively."));
  1954. QPushButton *cancel = new QPushButton(tr("Cancel"), searchInProgress);
  1955. connect(cancel, SIGNAL(clicked()), this, SLOT(interruptSearch()));
  1956. QVBoxLayout *layout = new QVBoxLayout(searchInProgress);
  1957. searchInProgress->setLayout(layout);
  1958. layout->addWidget(info);
  1959. layout->addWidget(cancel);
  1960. searchInProgress->adjustSize();
  1961. searchInProgress->move(QApplication::desktop()->screen()->rect().center() - rect().center());
  1962. searchInProgress->show();
  1963. getImagesFromDir(QDir(dirName));
  1964. cancel->disconnect();
  1965. searchInProgress->hide();
  1966. delete info;
  1967. delete cancel;
  1968. delete layout;
  1969. delete searchInProgress;
  1970. if (interrupt_search_) {
  1971. interrupt_search_ = 0;
  1972. return;
  1973. /* NOTREACHED */
  1974. }
  1975. if (!list_images_widget_->count()) {
  1976. showWarning(tr("The folder you selected contains no images"));
  1977. return;
  1978. /* NOTREACHED */
  1979. }
  1980. bool ret = 0;
  1981. if (list_images_->at(0).labeled_) {
  1982. QString labeled =
  1983. alterFileName(list_images_->at(0).image_, "_labeled");
  1984. labeled.append(".dat");
  1985. ret = loadInfo(labeled);
  1986. }
  1987. else
  1988. ret = image_->load(list_images_->at(0).image_);
  1989. if (!ret) {
  1990. return;
  1991. /* NOTREACHED */
  1992. }
  1993. current_image_ = list_images_->at(0).image_;
  1994. image_ID_ = 0;
  1995. list_images_widget_->setCurrentRow(image_ID_);
  1996. QString winTitle;
  1997. winTitle.append("ImageLabeler - ");
  1998. winTitle.append(current_image_);
  1999. setWindowTitle(winTitle);
  2000. image_holder_->resize(image_->size());
  2001. image_holder_->setPixmap(*image_);
  2002. enableTools();
  2003. }
  2004. void
  2005. ImageLabeler::loadLegendFromFile()
  2006. {
  2007. QFileDialog fileDialog(0, tr("Load legend"));
  2008. fileDialog.setFileMode(QFileDialog::AnyFile);
  2009. QString filename;
  2010. if (fileDialog.exec()) {
  2011. filename = fileDialog.selectedFiles().last();
  2012. }
  2013. else {
  2014. //showWarning(tr("Could not open file dialog"));
  2015. return;
  2016. /* NOTREACHED */
  2017. }
  2018. QDomDocument doc("Image Labeler");
  2019. QFile file(filename);
  2020. if (!file.open(QIODevice::ReadOnly)) {
  2021. showWarning(tr("Can not open such file"));
  2022. return;
  2023. /* NOTREACHED */
  2024. }
  2025. QString errMsg;
  2026. if (!doc.setContent(&file, &errMsg)) {
  2027. showWarning(errMsg);
  2028. file.close();
  2029. return;
  2030. /* NOTREACHED */
  2031. }
  2032. file.close();
  2033. list_label_->clear();
  2034. /* getting legend */
  2035. QDomElement elements = doc.documentElement();
  2036. QDomNode rootNode = elements.firstChild();
  2037. QString string;
  2038. while(!rootNode.isNull()) {
  2039. QDomElement element = rootNode.toElement();
  2040. if(!element.isNull()) {
  2041. if (element.tagName() == "legend") {
  2042. loadLegendFromNode(&element);
  2043. }
  2044. }
  2045. rootNode = rootNode.nextSibling();
  2046. }
  2047. }
  2048. void
  2049. ImageLabeler::loadLegendFromNode(QDomElement *anElement)
  2050. {
  2051. if (!anElement) {
  2052. return;
  2053. /* NOTREACHED */
  2054. }
  2055. QDomNode subNode = anElement->firstChild();
  2056. QDomElement subElement;
  2057. QString string;
  2058. int id = -1;
  2059. bool isMain;
  2060. uint color = 0xff000000;
  2061. while(!subNode.isNull()) {
  2062. subElement = subNode.toElement();
  2063. if (!subElement.isNull() && !subElement.text().isEmpty()) {
  2064. string = subElement.attribute("id");
  2065. bool ok = 0;
  2066. id = string.toInt(&ok, 10);
  2067. if (!ok) {
  2068. qDebug() <<
  2069. "loadLegendFromNode: "
  2070. "label id format is corrupted";
  2071. subNode = subNode.nextSibling();
  2072. continue;
  2073. }
  2074. string = subElement.attribute("isMain");
  2075. isMain = string.toInt(&ok, 2);
  2076. if (!ok) {
  2077. qDebug() <<
  2078. "loadLegendFromNode: "
  2079. "label isMain flag format is corrupted";
  2080. subNode = subNode.nextSibling();
  2081. continue;
  2082. }
  2083. string = subElement.attribute("color");
  2084. color = string.toUInt(&ok, 16);
  2085. if (!ok) {
  2086. qDebug() <<
  2087. "loadLegendFromNode: "
  2088. "label color format is corrupted";
  2089. subNode = subNode.nextSibling();
  2090. continue;
  2091. }
  2092. string = subElement.text();
  2093. addLabel(id, isMain, string);
  2094. setLabelColor(id, color);
  2095. }
  2096. subNode = subNode.nextSibling();
  2097. }
  2098. }
  2099. void
  2100. ImageLabeler::legendToXml(QDomDocument *aDoc, QDomElement *aRoot)
  2101. {
  2102. QDomElement legend = aDoc->createElement(tr("legend"));
  2103. aRoot->appendChild(legend);
  2104. /* storing all labels made by user */
  2105. int labelCount = list_label_->count();
  2106. for (int i = 0; i < labelCount; i++) {
  2107. QDomElement label = aDoc->createElement(tr("label"));
  2108. label.setAttribute("color", QString("%1").arg(list_label_colors_.at(i), 0, 16));
  2109. label.setAttribute("id", i);
  2110. QString priority;
  2111. if (main_label_ == i)
  2112. priority.append("1");
  2113. else
  2114. priority.append("0");
  2115. label.setAttribute("isMain", priority);
  2116. QString labelText = list_label_->item(i)->text();
  2117. /* removing the number prefix of label */
  2118. if (-1 != labelText.indexOf(QString("%1: ").arg(i))) {
  2119. labelText = labelText.mid(3, labelText.size() - 3);
  2120. }
  2121. QDomText labelName = aDoc->createTextNode(labelText);
  2122. label.appendChild(labelName);
  2123. legend.appendChild(label);
  2124. }
  2125. /* in case we have no labels */
  2126. if (0 == labelCount) {
  2127. QDomElement label = aDoc->createElement(tr("label"));
  2128. label.setAttribute(tr("id"), -1);
  2129. legend.appendChild(label);
  2130. }
  2131. }
  2132. void
  2133. ImageLabeler::objectsToXml(QDomDocument *aDoc, QDomElement *aRoot)
  2134. {
  2135. QDomElement objects = aDoc->createElement(tr("objects"));
  2136. aRoot->appendChild(objects);
  2137. /* rects first */
  2138. for (int i = 0; i < list_bounding_box_.size(); i++) {
  2139. QDomElement rectData = aDoc->createElement(tr("bbox"));
  2140. rectData.setAttribute("id", list_bounding_box_.at(i)->label_ID_);
  2141. QRect rect = list_bounding_box_.at(i)->rect.normalized();
  2142. QString rectDataString =
  2143. QString("%1;%2;%3;%4;").
  2144. arg(rect.x()).
  2145. arg(rect.y()).
  2146. arg(rect.width()).
  2147. arg(rect.height());
  2148. QDomText rectDataText = aDoc->createTextNode(rectDataString);
  2149. rectData.appendChild(rectDataText);
  2150. objects.appendChild(rectData);
  2151. }
  2152. /* polys next */
  2153. for (int i = 0; i < list_polygon_.size(); i++) {
  2154. QDomElement polyData = aDoc->createElement(tr("poly"));
  2155. polyData.setAttribute("id", list_polygon_.at(i)->label_ID_);
  2156. QPolygon poly = list_polygon_.at(i)->poly;
  2157. QString polyDataString;
  2158. for (int j = 0; j < poly.count(); j++)
  2159. polyDataString.append(
  2160. QString("%1;%2;").
  2161. arg(poly.point(j).x()).
  2162. arg(poly.point(j).y())
  2163. );
  2164. QDomText polyDataText = aDoc->createTextNode(polyDataString);
  2165. polyData.appendChild(polyDataText);
  2166. objects.appendChild(polyData);
  2167. }
  2168. }
  2169. void
  2170. ImageLabeler::generateColors()
  2171. {
  2172. int labelCount = list_label_->count();
  2173. if (!labelCount) {
  2174. showWarning(tr("you have not added any labels yet"));
  2175. return;
  2176. /* NOTREACHED */
  2177. }
  2178. int coeff = (0xff / labelCount) * 3;
  2179. //QList< uint > colors;
  2180. list_label_colors_.clear();
  2181. list_label_colors_.append(0);
  2182. uchar red = 0xff;
  2183. uchar green = 0xff;
  2184. uchar blue = 0xff;
  2185. uchar iterationColor = coeff;
  2186. uint color = 0xffffffff;
  2187. int j = 1;
  2188. for (int i = 1; i < labelCount; i++) {
  2189. if (6 == j) {
  2190. iterationColor += coeff;
  2191. j = 1;
  2192. }
  2193. if (5 == j) {
  2194. red = 0xff - iterationColor;
  2195. green = 0xff - iterationColor;
  2196. blue = 0xff;
  2197. }
  2198. else if (4 == j) {
  2199. red = 0xff - iterationColor;
  2200. green = 0xff;
  2201. blue = 0xff - iterationColor;
  2202. }
  2203. else if (3 == j) {
  2204. red = 0xff - iterationColor;
  2205. green = 0xff;
  2206. blue = 0xff;
  2207. }
  2208. else if (2 == j) {
  2209. red = 0xff;
  2210. green = 0xff - iterationColor;
  2211. blue = 0xff;
  2212. }
  2213. else if (1 == j){
  2214. red = 0xff;
  2215. green = 0xff;
  2216. blue = 0xff - iterationColor;
  2217. }
  2218. j++;
  2219. color = red + (green * 0x100) + (blue * 0x10000) + 0xff000000;
  2220. int itemNo = list_label_colors_.count();
  2221. QPixmap iconPix = QPixmap(20, 20);
  2222. iconPix.fill(color);
  2223. QIcon icon(iconPix);
  2224. list_label_->item(itemNo)->setIcon(icon);
  2225. list_label_colors_.append(color);
  2226. }
  2227. image_holder_->update();
  2228. }
  2229. void
  2230. ImageLabeler::addBBoxFromData(
  2231. QString *aBBoxData,
  2232. int *ID
  2233. )
  2234. {
  2235. BoundingBox *bbox = new BoundingBox;
  2236. *bbox = BBoxFromData(aBBoxData);
  2237. if (!bbox->rect.isValid() || !ID) {
  2238. return;
  2239. /* NOTREACHED */
  2240. }
  2241. bbox->label_ID_ = *ID;
  2242. list_bounding_box_.append(bbox);
  2243. addBBoxArea(list_bounding_box_.count() - 1, *bbox);
  2244. }
  2245. BoundingBox
  2246. ImageLabeler::BBoxFromData(
  2247. QString *aBBoxData
  2248. )
  2249. {
  2250. BoundingBox bbox;
  2251. QString buffer;
  2252. bbox.rect.setRect(-1, -1, -1, -1);
  2253. int startPos = 0;
  2254. bool ok = 1;
  2255. for (int i = 0; i < aBBoxData->size(); i++) {
  2256. if (';' != aBBoxData->at(i))
  2257. continue;
  2258. buffer = aBBoxData->mid(startPos, i - startPos);
  2259. int bboxData = buffer.toInt(&ok, 10);
  2260. if (!ok) {
  2261. qDebug() <<
  2262. "BBoxFromData: "
  2263. "bbox format is corrupted";
  2264. break;
  2265. }
  2266. if (-1 == bbox.rect.x()) {
  2267. bbox.rect.setX(bboxData);
  2268. bbox.rect.setWidth(-1);
  2269. }
  2270. else if (-1 == bbox.rect.y()) {
  2271. bbox.rect.setY(bboxData);
  2272. bbox.rect.setHeight(-1);
  2273. }
  2274. else if (-1 == bbox.rect.width()) {
  2275. bbox.rect.setWidth(bboxData);
  2276. }
  2277. else if (-1 == bbox.rect.height()) {
  2278. bbox.rect.setHeight(bboxData);
  2279. }
  2280. startPos = i + 1;
  2281. }
  2282. if (!bbox.rect.isValid()) {
  2283. qDebug() <<
  2284. "BBoxFromData: "
  2285. "bbox format is corrupted";
  2286. bbox.rect.setRect(-1, -1, -1, -1);
  2287. }
  2288. else if (!ok) {
  2289. bbox.rect.setRect(-1, -1, -1, -1);
  2290. }
  2291. return bbox;
  2292. }
  2293. BoundingBox
  2294. ImageLabeler::BBoxFromString(
  2295. QString *aString,
  2296. int *oldID
  2297. )
  2298. {
  2299. BoundingBox bbox;
  2300. bbox.label_ID_ = -1;
  2301. *oldID = -1;
  2302. if (!aString) {
  2303. return bbox;
  2304. /* NOTREACHED */
  2305. }
  2306. if (-1 == aString->indexOf("BBox")) {
  2307. return bbox;
  2308. /* NOTREACHED */
  2309. }
  2310. /* getting bbox id in the list(it cannot be changed) */
  2311. bool ok = 0;
  2312. int bboxID = getNumFromString(aString, "BBox #", ";", &ok);
  2313. if (!ok || bboxID <= -1) {
  2314. qDebug() <<
  2315. "BBoxFromString: bboxID is corrupted";
  2316. return bbox;
  2317. /* NOTREACHED */
  2318. }
  2319. /* getting new label id */
  2320. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  2321. if (!ok || labelID <= -1) {
  2322. showWarning(
  2323. tr("new LabelID is wrong, area can not be changed")
  2324. );
  2325. return bbox;
  2326. /* NOTREACHED */
  2327. }
  2328. /* getting new points */
  2329. int pointsPos = aString->indexOf("data:") + 5;
  2330. int pointsLen = aString->size() - pointsPos;
  2331. if (pointsLen <= 0) {
  2332. showWarning(
  2333. tr("new data is wrong, area can not be changed")
  2334. );
  2335. return bbox;
  2336. /* NOTREACHED */
  2337. }
  2338. QString pointsData = aString->mid(pointsPos, pointsLen);
  2339. bbox = BBoxFromData(&pointsData);
  2340. bbox.label_ID_ = labelID;
  2341. *oldID = bboxID;
  2342. return bbox;
  2343. }
  2344. void
  2345. ImageLabeler::addPoly(Polygon *poly)
  2346. {
  2347. if (poly->poly.isEmpty() || poly->label_ID_ < 0) {
  2348. return;
  2349. /* NOTREACHED */
  2350. }
  2351. list_polygon_.append(poly);
  2352. addPolyArea(list_polygon_.count() - 1, *poly);
  2353. }
  2354. void
  2355. ImageLabeler::addBBox(BoundingBox *bbox)
  2356. {
  2357. if (bbox->rect.isEmpty() || bbox->label_ID_ < 0) {
  2358. return;
  2359. /* NOTREACHED */
  2360. }
  2361. list_bounding_box_.append(bbox);
  2362. addBBoxArea(list_bounding_box_.count() - 1, *bbox);
  2363. }
  2364. void
  2365. ImageLabeler::addPolyFromData(
  2366. QString *aPolyData,
  2367. int *labelID
  2368. )
  2369. {
  2370. Polygon *poly = new Polygon;
  2371. *poly = polyFromData(aPolyData);
  2372. if (poly->poly.isEmpty() || !labelID) {
  2373. return;
  2374. /* NOTREACHED */
  2375. }
  2376. poly->label_ID_ = *labelID;
  2377. list_polygon_.append(poly);
  2378. addPolyArea(list_polygon_.count() - 1, *poly);
  2379. }
  2380. Polygon
  2381. ImageLabeler::polyFromData(
  2382. QString *aPolyData
  2383. )
  2384. {
  2385. Polygon poly;
  2386. poly.label_ID_ = -1;
  2387. QPoint point;
  2388. QString buffer;
  2389. int startPos = 0;
  2390. bool ok = 1;
  2391. bool evenFlag = 0;
  2392. for (int i = 0; i < aPolyData->size(); i++) {
  2393. if (';' != aPolyData->at(i))
  2394. continue;
  2395. buffer = aPolyData->mid(startPos, i - startPos);
  2396. int polyCoor = buffer.toInt(&ok, 10);
  2397. if (!ok) {
  2398. qDebug() <<
  2399. "polyFromData: "
  2400. "poly format is corrupted";
  2401. break;
  2402. }
  2403. if (!evenFlag) {
  2404. point.setX(polyCoor);
  2405. evenFlag = 1;
  2406. }
  2407. else {
  2408. point.setY(polyCoor);
  2409. poly.poly.append(point);
  2410. evenFlag = 0;
  2411. }
  2412. startPos = i + 1;
  2413. }
  2414. if (evenFlag) {
  2415. qDebug() <<
  2416. "polyFromData: "
  2417. "poly format is corrupted";
  2418. poly.poly.clear();
  2419. }
  2420. else if (!ok) {
  2421. poly.poly.clear();
  2422. }
  2423. return poly;
  2424. }
  2425. Polygon
  2426. ImageLabeler::polyFromString(
  2427. QString *aString,
  2428. int *oldID
  2429. )
  2430. {
  2431. Polygon poly;
  2432. poly.label_ID_ = -1;
  2433. *oldID = -1;
  2434. if (!aString) {
  2435. return poly;
  2436. /* NOTREACHED */
  2437. }
  2438. if (-1 == aString->indexOf("Poly")) {
  2439. return poly;
  2440. /* NOTREACHED */
  2441. }
  2442. /* getting poly id in the list(it cannot be changed) */
  2443. bool ok = 0;
  2444. int polyID = getNumFromString(aString, "Poly #", ";", &ok);
  2445. if (!ok || polyID <= -1) {
  2446. qDebug() <<
  2447. "polyFromString: poly ID is corrupted";
  2448. return poly;
  2449. /* NOTREACHED */
  2450. }
  2451. /* getting new label id */
  2452. int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
  2453. if (!ok || labelID <= -1) {
  2454. showWarning(
  2455. tr("new LabelID is wrong, area can not be changed")
  2456. );
  2457. return poly;
  2458. /* NOTREACHED */
  2459. }
  2460. /* getting new points */
  2461. int pointsPos = aString->indexOf("points:") + 7;
  2462. int pointsLen = aString->size() - pointsPos;
  2463. if (pointsLen <= 0) {
  2464. showWarning(
  2465. tr("new points data is wrong, area can not be changed")
  2466. );
  2467. return poly;
  2468. /* NOTREACHED */
  2469. }
  2470. QString pointsData = aString->mid(pointsPos, pointsLen);
  2471. poly = polyFromData(&pointsData);
  2472. poly.label_ID_ = labelID;
  2473. *oldID = polyID;
  2474. return poly;
  2475. }
  2476. void
  2477. ImageLabeler::showWarning(
  2478. const QString text
  2479. )
  2480. {
  2481. if (text.isEmpty()) {
  2482. return;
  2483. /* NOTREACHED */
  2484. }
  2485. QMessageBox msgBox;
  2486. msgBox.setText(text);
  2487. msgBox.setIcon(QMessageBox::Warning);
  2488. msgBox.exec();
  2489. }
  2490. bool
  2491. ImageLabeler::askForUnsavedData()
  2492. {
  2493. if ((!list_bounding_box_.isEmpty() ||
  2494. !list_polygon_.isEmpty()) &&
  2495. unsaved_data_)
  2496. {
  2497. QMessageBox msgBox;
  2498. msgBox.setText(tr("There is some unsaved data"));
  2499. msgBox.setInformativeText(tr("Do you want to save your progress?"));
  2500. msgBox.setStandardButtons(
  2501. QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  2502. msgBox.setDefaultButton(QMessageBox::Save);
  2503. msgBox.setIcon(QMessageBox::Question);
  2504. int ret = msgBox.exec();
  2505. if (QMessageBox::Save == ret)
  2506. saveAllInfo();
  2507. else if (QMessageBox::Cancel == ret)
  2508. return true;
  2509. }
  2510. return false;
  2511. }
  2512. void
  2513. ImageLabeler::setBoundingBoxTool(bool aButtonPressed)
  2514. {
  2515. if (aButtonPressed)
  2516. image_holder_->setTool(ImageHolder::BoundingBoxTool);
  2517. else {
  2518. image_holder_->setTool(ImageHolder::NoTool);
  2519. image_holder_->clearLast();
  2520. }
  2521. }
  2522. void
  2523. ImageLabeler::setPolygonTool(bool aButtonPressed)
  2524. {
  2525. if (aButtonPressed) {
  2526. image_holder_->setTool(ImageHolder::PolygonTool);
  2527. image_holder_->setFocus();
  2528. }
  2529. else {
  2530. image_holder_->setTool(ImageHolder::NoTool);
  2531. image_holder_->clearLast();
  2532. }
  2533. }
  2534. void
  2535. ImageLabeler::onSelectionStarted()
  2536. {
  2537. button_confirm_selection_->setEnabled(true);
  2538. }
  2539. void
  2540. ImageLabeler::onImageScaled()
  2541. {
  2542. }
  2543. void
  2544. ImageLabeler::confirmSelection()
  2545. {
  2546. if (!list_label_->count()) {
  2547. showWarning(tr("You haven't added any label"));
  2548. return;
  2549. /* NOTREACHED */
  2550. }
  2551. image_holder_->confirmSelection();
  2552. if (label_ID_ < 0)
  2553. label_ID_ = 0;
  2554. ImageHolder::Tool tool = image_holder_->tool();
  2555. switch (tool) {
  2556. case ImageHolder::BoundingBoxTool:
  2557. list_bounding_box_.last()->label_ID_ = label_ID_;
  2558. addBBoxArea(
  2559. list_bounding_box_.count() - 1,
  2560. *(list_bounding_box_.last())
  2561. );
  2562. break;
  2563. case ImageHolder::PolygonTool:
  2564. list_polygon_.last()->label_ID_ = label_ID_;
  2565. addPolyArea(
  2566. list_polygon_.count() - 1,
  2567. *(list_polygon_.last())
  2568. );
  2569. break;
  2570. default:
  2571. break;
  2572. }
  2573. button_confirm_selection_->setEnabled(false);
  2574. unsaved_data_ = 1;
  2575. }
  2576. void
  2577. ImageLabeler::clearAll()
  2578. {
  2579. clearLabelList();
  2580. list_areas_->clear();
  2581. list_bounding_box_.clear();
  2582. list_polygon_.clear();
  2583. list_images_->clear();
  2584. list_images_widget_->clear();
  2585. main_label_ = -1;
  2586. image_holder_->clearAll();
  2587. segmented_image_.clear();
  2588. action_view_normal_->setEnabled(false);
  2589. action_view_segmented_->setEnabled(false);
  2590. }
  2591. void
  2592. ImageLabeler::clearAllTool()
  2593. {
  2594. list_areas_->clear();
  2595. list_bounding_box_.clear();
  2596. list_polygon_.clear();
  2597. main_label_ = -1;
  2598. image_holder_->clearAll();
  2599. }
  2600. void ImageLabeler::clearLabelList()
  2601. {
  2602. list_label_->clear();
  2603. addLabel(0, false, "BACKGROUND");
  2604. list_label_->item(0)->setFlags(
  2605. Qt::ItemIsUserCheckable | Qt::ItemIsSelectable |
  2606. Qt::ItemIsEnabled
  2607. );
  2608. }
  2609. void ImageLabeler::clearLabelColorList()
  2610. {
  2611. list_label_colors_.clear();
  2612. list_label_colors_.append(0x0);
  2613. }
  2614. void
  2615. ImageLabeler::enableTools()
  2616. {
  2617. action_save_labels_->setEnabled(true);
  2618. action_save_segmented_->setEnabled(true);
  2619. action_save_legend_->setEnabled(true);
  2620. action_bound_box_tool_->setEnabled(true);
  2621. action_polygon_tool_->setEnabled(true);
  2622. action_add_description_->setEnabled(true);
  2623. action_undo_->setEnabled(true);
  2624. action_redo_->setEnabled(true);
  2625. button_bound_box_tool_->setEnabled(true);
  2626. button_polygon_tool_->setEnabled(true);
  2627. button_tagging_tool_->setEnabled(true);
  2628. button_add_label_->setEnabled(true);
  2629. button_remove_label_->setEnabled(true);
  2630. button_prev_image_->setEnabled(true);
  2631. button_next_image_->setEnabled(true);
  2632. button_clear_selection_tool_->setEnabled(true);
  2633. button_generate_colors_->setEnabled(true);
  2634. button_delete_all_labels_->setEnabled(true);
  2635. }
  2636. void
  2637. ImageLabeler::disableTools()
  2638. {
  2639. action_save_labels_->setEnabled(false);
  2640. action_save_segmented_->setEnabled(false);
  2641. action_save_legend_->setEnabled(false);
  2642. action_bound_box_tool_->setEnabled(false);
  2643. action_polygon_tool_->setEnabled(false);
  2644. button_tagging_tool_->setEnabled(false);
  2645. action_add_description_->setEnabled(false);
  2646. action_undo_->setEnabled(false);
  2647. action_redo_->setEnabled(false);
  2648. button_bound_box_tool_->setEnabled(false);
  2649. button_polygon_tool_->setEnabled(false);
  2650. button_add_label_->setEnabled(false);
  2651. button_remove_label_->setEnabled(false);
  2652. button_prev_image_->setEnabled(false);
  2653. button_next_image_->setEnabled(false);
  2654. button_clear_selection_tool_->setEnabled(false);
  2655. button_generate_colors_->setEnabled(false);
  2656. button_delete_all_labels_->setEnabled(false);
  2657. }
  2658. void
  2659. ImageLabeler::areaListPopupMenu(const QPoint &aPos)
  2660. {
  2661. QPoint globalPos = list_areas_->mapToGlobal(aPos);
  2662. QModelIndex index = list_areas_->indexAt(aPos);
  2663. if (-1 == index.row()) {
  2664. return;
  2665. /* NOTREACHED */
  2666. }
  2667. list_areas_->item(index.row())->setSelected(true);
  2668. popup_area_list_->exec(globalPos);
  2669. }
  2670. void
  2671. ImageLabeler::labelListPopupMenu(const QPoint &aPos)
  2672. {
  2673. QPoint globalPos = list_label_->mapToGlobal(aPos);
  2674. QModelIndex index = list_label_->indexAt(aPos);
  2675. if (-1 == index.row() || !index.row()) {
  2676. return;
  2677. /* NOTREACHED */
  2678. }
  2679. list_label_->item(index.row())->setSelected(true);
  2680. popup_label_list_->exec(globalPos);
  2681. }
  2682. void
  2683. ImageLabeler::setDataFromForm(QString aData)
  2684. {
  2685. if (ImageDescriptionPurpose == line_edit_form_.purpose()) {
  2686. image_description_ = aData;
  2687. setWindowTitle(image_description_);
  2688. }
  2689. else if (TaggingPurpose == line_edit_form_.purpose()) {
  2690. tags_ = aData;
  2691. }
  2692. }
  2693. void
  2694. ImageLabeler::setPureData()
  2695. {
  2696. /* initializing array */
  2697. if (pure_data_) {
  2698. delete[] *pure_data_;
  2699. delete pure_data_;
  2700. }
  2701. QSize imageSize = image_->size();
  2702. pure_data_ = new int *[imageSize.height()];
  2703. if (!pure_data_) {
  2704. return;
  2705. /* NOTREACHED */
  2706. }
  2707. for (int i = 0; i < imageSize.height(); i++) {
  2708. pure_data_[i] = new int[imageSize.width()];
  2709. if (!pure_data_[i]) {
  2710. return;
  2711. /* NOTREACHED */
  2712. }
  2713. }
  2714. int bboxCnt = list_bounding_box_.count();
  2715. int polyCnt = list_polygon_.count();
  2716. for (int i = 0; i < imageSize.height(); i++)
  2717. for (int j = 0; j < imageSize.width(); j++) {
  2718. pure_data_[i][j] = 0;
  2719. /* bboxes first */
  2720. for (int cnt = 0; cnt < bboxCnt; cnt++) {
  2721. BoundingBox *bbox = list_bounding_box_.at(cnt);
  2722. if (bbox->rect.contains(j, i)) {
  2723. pure_data_[i][j] = bbox->label_ID_;
  2724. }
  2725. }
  2726. /* polys next */
  2727. for (int cnt = 0; cnt < polyCnt; cnt++) {
  2728. Polygon *poly = list_polygon_.at(cnt);
  2729. if (poly->poly.containsPoint(QPoint(j, i), Qt::OddEvenFill)) {
  2730. pure_data_[i][j] = poly->label_ID_;
  2731. }
  2732. }
  2733. }
  2734. }
  2735. void
  2736. ImageLabeler::setLabelColor()
  2737. {
  2738. if (list_label_colors_.count() < list_label_->count()) {
  2739. generateColors();
  2740. }
  2741. QListWidgetItem *current = list_label_->currentItem();
  2742. int labelID = list_label_->row(current);
  2743. QColor defaultColor;
  2744. defaultColor.setRgb(list_label_colors_.at(labelID));
  2745. QColor newColor = QColorDialog::getColor(
  2746. defaultColor,
  2747. list_label_
  2748. );
  2749. if (!newColor.isValid()) {
  2750. return;
  2751. /* NOTREACHED */
  2752. }
  2753. list_label_colors_.takeAt(labelID);
  2754. list_label_colors_.insert(labelID, newColor.rgb());
  2755. QPixmap iconPix = QPixmap(20, 20);
  2756. iconPix.fill(newColor);
  2757. QIcon icon(iconPix);
  2758. current->setIcon(icon);
  2759. image_holder_->update();
  2760. }
  2761. void
  2762. ImageLabeler::setLabelColor(int anID, QColor aColor)
  2763. {
  2764. if (anID < 0) {
  2765. return;
  2766. /* NOTREACHED */
  2767. }
  2768. if (list_label_colors_.count() < list_label_->count()) {
  2769. generateColors();
  2770. }
  2771. QListWidgetItem *item = list_label_->item(anID);
  2772. list_label_colors_.takeAt(anID);
  2773. list_label_colors_.insert(anID, aColor.rgb());
  2774. QPixmap iconPix = QPixmap(20, 20);
  2775. iconPix.fill(aColor);
  2776. QIcon icon(iconPix);
  2777. item->setIcon(icon);
  2778. image_holder_->update();
  2779. }
  2780. void
  2781. ImageLabeler::onOptionsSet()
  2782. {
  2783. auto_color_generation_ = options_form_.autoColorGeneration();
  2784. }
  2785. void
  2786. ImageLabeler::viewNormal()
  2787. {
  2788. if (current_image_.isEmpty()) {
  2789. return;
  2790. /* NOTREACHED */
  2791. }
  2792. image_->load(current_image_);
  2793. image_holder_->setPixmap(*image_);
  2794. action_view_segmented_->setEnabled(true);
  2795. action_view_normal_->setEnabled(false);
  2796. }
  2797. void
  2798. ImageLabeler::viewSegmented()
  2799. {
  2800. if (segmented_image_.isEmpty()) {
  2801. return;
  2802. /* NOTREACHED */
  2803. }
  2804. image_->load(segmented_image_);
  2805. image_holder_->setPixmap(*image_);
  2806. action_view_segmented_->setEnabled(false);
  2807. action_view_normal_->setEnabled(true);
  2808. }
  2809. void
  2810. ImageLabeler::interruptSearch()
  2811. {
  2812. interrupt_search_ = 1;
  2813. }
  2814. void
  2815. ImageLabeler::selectImage(QListWidgetItem *anItem)
  2816. {
  2817. if (!anItem || list_images_widget_->row(anItem) < 0 ||
  2818. list_images_->isEmpty())
  2819. {
  2820. return;
  2821. /* NOTREACHED */
  2822. }
  2823. clearAllTool();
  2824. clearLabelList();
  2825. clearLabelColorList();
  2826. image_ID_ = list_images_widget_->row(anItem);
  2827. selectImage(image_ID_);
  2828. // if (list_images_->at(image_ID_).labeled_ &&
  2829. // !list_images_->at(image_ID_).pas_)
  2830. // {
  2831. // list_label_->clear();
  2832. // QString labeled =
  2833. // alterFileName(list_images_->at(image_ID_).image_, "_labeled");
  2834. // labeled.append(".dat");
  2835. // loadInfo(labeled);
  2836. // }
  2837. // else if (list_images_->at(image_ID_).labeled_ &&
  2838. // list_images_->at(image_ID_).pas_)
  2839. // {
  2840. // /* TODO: do the pascal file selecting */
  2841. // showWarning("this function doesn't work at the moment, sorry.");
  2842. // }
  2843. // else {
  2844. // current_image_ = list_images_->at(image_ID_).image_;
  2845. // image_->load(current_image_);
  2846. // image_holder_->setPixmap(*image_);
  2847. // image_holder_->resize(image_->size());
  2848. // }
  2849. }
  2850. bool
  2851. ImageLabeler::selectImage(int anImageID)
  2852. {
  2853. if (anImageID < 0 || list_images_->isEmpty() ||
  2854. list_images_->count() <= anImageID)
  2855. {
  2856. return false;
  2857. /* NOTREACHED */
  2858. }
  2859. if (list_images_->at(anImageID).labeled_ &&
  2860. !list_images_->at(anImageID).pas_)
  2861. {
  2862. list_label_->clear();
  2863. QString labeled =
  2864. alterFileName(list_images_->at(anImageID).image_, "_labeled");
  2865. labeled.append(".dat");
  2866. loadInfo(labeled);
  2867. }
  2868. else if (list_images_->at(anImageID).labeled_ &&
  2869. list_images_->at(anImageID).pas_)
  2870. {
  2871. /* TODO: do the pascal file selecting */
  2872. showWarning("this function doesn't work at the moment, sorry.");
  2873. }
  2874. else {
  2875. current_image_ = list_images_->at(anImageID).image_;
  2876. image_->load(current_image_);
  2877. image_holder_->setPixmap(*image_);
  2878. image_holder_->resize(image_->size());
  2879. }
  2880. return true;
  2881. }
  2882. void
  2883. ImageLabeler::imageListPopupMenu(const QPoint &aPos)
  2884. {
  2885. QPoint globalPos = list_images_widget_->mapToGlobal(aPos);
  2886. QModelIndex index = list_images_widget_->indexAt(aPos);
  2887. if (-1 == index.row()) {
  2888. return;
  2889. /* NOTREACHED */
  2890. }
  2891. list_images_widget_->item(index.row())->setSelected(true);
  2892. popup_images_list_->exec(globalPos);
  2893. }
  2894. void
  2895. ImageLabeler::removeImage()
  2896. {
  2897. int num = list_images_widget_->currentRow();
  2898. list_images_widget_->takeItem(num);
  2899. list_images_->takeAt(num);
  2900. for (int i = num ; i < list_images_widget_->count(); i++) {
  2901. QString newStr = getFilenameFromPath(&(list_images_->at(i).image_));
  2902. newStr.prepend(QString("%1: ").arg(i));
  2903. list_images_widget_->item(i)->setText(newStr);
  2904. }
  2905. }
  2906. void
  2907. ImageLabeler::resizeEvent (QResizeEvent *anEvent)
  2908. {
  2909. QWidget::resizeEvent(anEvent);
  2910. }
  2911. void
  2912. ImageLabeler::mousePressEvent(QMouseEvent *anEvent)
  2913. {
  2914. QWidget::mousePressEvent(anEvent);
  2915. }
  2916. void
  2917. ImageLabeler::keyPressEvent(QKeyEvent *anEvent)
  2918. {
  2919. keyboard_modifier_ = anEvent->modifiers();
  2920. if (Qt::Key_Left == anEvent->key() &&
  2921. Qt::ControlModifier == keyboard_modifier_) {
  2922. prevImage();
  2923. }
  2924. if (Qt::Key_Right == anEvent->key() &&
  2925. Qt::ControlModifier == keyboard_modifier_) {
  2926. nextImage();
  2927. }
  2928. if ((Qt::Key_Enter == anEvent->key() ||
  2929. Qt::Key_Return == anEvent->key()) &&
  2930. // Qt::NoModifier == anEvent->modifiers() &&
  2931. ImageHolder::NewSelection == image_holder_->state()) {
  2932. confirmSelection();
  2933. }
  2934. if ((Qt::Key_Enter == anEvent->key() ||
  2935. Qt::Key_Return == anEvent->key()) &&
  2936. // Qt::NoModifier == anEvent->modifiers() &&
  2937. -1 != image_holder_->focusedSelection()) {
  2938. image_holder_->clearFocusOnArea();
  2939. image_holder_->clearEdition();
  2940. image_holder_->update();
  2941. }
  2942. if (Qt::Key_Escape == anEvent->key()) {
  2943. image_holder_->clearLast();
  2944. image_holder_->clearFocusOnArea();
  2945. image_holder_->clearEdition();
  2946. image_holder_->update();
  2947. }
  2948. if (Qt::Key_Z == anEvent->key() &&
  2949. Qt::ControlModifier == anEvent->modifiers()) {
  2950. image_holder_->undo();
  2951. }
  2952. if (Qt::Key_Y == anEvent->key() &&
  2953. Qt::ControlModifier == anEvent->modifiers()) {
  2954. image_holder_->redo();
  2955. }
  2956. QWidget::keyPressEvent(anEvent);
  2957. }
  2958. void
  2959. ImageLabeler::keyReleaseEvent(QKeyEvent *anEvent)
  2960. {
  2961. keyboard_modifier_ = anEvent->modifiers();
  2962. }
  2963. void
  2964. ImageLabeler::wheelEvent(QWheelEvent *anEvent)
  2965. {
  2966. /* zoomin */
  2967. if (0 < anEvent->delta() &&
  2968. Qt::ControlModifier == keyboard_modifier_) {
  2969. image_holder_->scaleImage(ZoomIn, 1.1);
  2970. }
  2971. /* zoomout */
  2972. else if (anEvent->delta() < 0 &&
  2973. Qt::ControlModifier == keyboard_modifier_) {
  2974. image_holder_->scaleImage(ZoomOut, 1.1);
  2975. }
  2976. }
  2977. /*
  2978. *
  2979. */