ai.js 585 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475
  1. /*
  2. _ ___ ____ _
  3. / \ |_ _| | __ ) __ _ ___(_) ___
  4. / _ \ | | | _ \ / _` / __| |/ __|
  5. / ___ \ | | | |_) | (_| \__ \ | (__
  6. /_/ \_\___| |____/ \__,_|___/_|\___|
  7. */
  8. Blockly.Blocks['ai_v831_button_read_pressed_canvas'] = {
  9. init: function () {
  10. this.appendDummyInput()
  11. .appendField(Blockly.Msg.ai_basic_button_when)
  12. .appendField(new Blockly.FieldDropdown([
  13. ["A", "A"],
  14. ["B", "B"],
  15. ["C", "C"],
  16. ["D", "D"]
  17. ]), "button_type")
  18. .appendField(Blockly.Msg.ai_basic_pressed)
  19. this.setOutput(true, null);
  20. this.setColour("#d42b03");
  21. var thisBlock = this;
  22. this.setTooltip(function () {
  23. var mode = thisBlock.getFieldValue('button_type');
  24. var TOOLTIPS = {
  25. '0': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "A"),
  26. '1': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "B"),
  27. '2': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "C"),
  28. '3': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "D"),
  29. };
  30. return TOOLTIPS[mode];
  31. });
  32. this.setHelpUrl("");
  33. }
  34. };
  35. Blockly.Python['ai_v831_button_read_pressed_canvas'] = function (block) {
  36. var _type = block.getFieldValue('button_type');
  37. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  38. sys.path.append("/root/")`
  39. Blockly.Python.definitions_['v831_import_CocoPi_BUTTON'] = `from CocoPi import BUTTON`
  40. // Blockly.Python.definitions_['v831_import_CocoPi'] = `sys.path.append("/root/")`
  41. Blockly.Python.definitions_['import_define_x_v831_button'] = `key_A = BUTTON(14)
  42. key_B = BUTTON(8)
  43. key_C = BUTTON(13)
  44. key_D = BUTTON(7)
  45. `
  46. var code = `key_${_type}.is_pressed()`
  47. return [code, Blockly.Python.ORDER_CONDITIONAL];
  48. };
  49. Blockly.Blocks['ai_v831_button_read_pressed'] = {
  50. init: function () {
  51. this.appendDummyInput()
  52. .appendField(Blockly.Msg.TOUCH_CASE)
  53. .appendField(new Blockly.FieldDropdown([
  54. ["A", "A"],
  55. ["B", "B"],
  56. ["C", "C"],
  57. ["D", "D"]
  58. ]), "button_type")
  59. .appendField(Blockly.Msg.ai_basic_pressed)
  60. this.appendStatementInput("input")
  61. .setCheck(null)
  62. .appendField(Blockly.Msg.basic_motion_statement_exec);
  63. // this.setOutput(true, null);
  64. this.setNextStatement(true);
  65. this.setPreviousStatement(true);
  66. this.setColour("#d42b03");
  67. var thisBlock = this;
  68. this.setTooltip(function () {
  69. var mode = thisBlock.getFieldValue('button_type');
  70. var TOOLTIPS = {
  71. '0': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "A"),
  72. '1': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "B"),
  73. '2': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "C"),
  74. '3': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "D"),
  75. };
  76. return TOOLTIPS[mode];
  77. });
  78. this.setHelpUrl("");
  79. }
  80. };
  81. Blockly.Python['ai_v831_button_read_pressed'] = function (block) {
  82. var _type = block.getFieldValue('button_type');
  83. var statements_input = Blockly.Python.statementToCode(block, 'input');
  84. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  85. sys.path.append("/root/")`
  86. Blockly.Python.definitions_['v831_import_CocoPi_BUTTON'] = `from CocoPi import BUTTON`
  87. // Blockly.Python.definitions_['v831_import_CocoPi'] = `sys.path.append("/root/")`
  88. Blockly.Python.definitions_['import_define_x_v831_button'] = `key_A = BUTTON(14)
  89. key_B = BUTTON(8)
  90. key_C = BUTTON(13)
  91. key_D = BUTTON(7)
  92. `
  93. let allBlocks = block.workspace.getAllBlocks();
  94. let global = ""
  95. try {
  96. global = allBlocks[0].workspace.variableList.toString()
  97. }
  98. catch (e) {
  99. console.log(e)
  100. }
  101. Blockly.Python.addFunction("BUTTON" + _type, `def BUTTON${_type}():
  102. ${global ? 'global ' + global : ""}
  103. ${statements_input || " pass"}\n`, true)
  104. var code = `if key_${_type}.is_pressed():
  105. BUTTON${_type}()
  106. `
  107. return code;
  108. };
  109. Blockly.Blocks['ai_v831_button_read_released'] = {
  110. init: function () {
  111. this.appendDummyInput()
  112. .appendField(Blockly.Msg.TOUCH_CASE)
  113. .appendField(new Blockly.FieldDropdown([
  114. ["A", "A"],
  115. ["B", "B"],
  116. ["C", "C"],
  117. ["D", "D"]
  118. ]), "button_type")
  119. .appendField(Blockly.Msg.ai_basic_pressed_one)
  120. this.appendStatementInput("input")
  121. .setCheck(null)
  122. .appendField(Blockly.Msg.basic_motion_statement_exec);
  123. // this.setOutput(true, null);
  124. this.setNextStatement(true);
  125. this.setPreviousStatement(true);
  126. this.setColour("#d42b03");
  127. var thisBlock = this;
  128. this.setTooltip(function () {
  129. var mode = thisBlock.getFieldValue('button_type');
  130. var TOOLTIPS = {
  131. '0': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "A"),
  132. '1': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "B"),
  133. '2': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "C"),
  134. '3': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "D"),
  135. };
  136. return TOOLTIPS[mode];
  137. });
  138. this.setHelpUrl("");
  139. }
  140. };
  141. Blockly.Python['ai_v831_button_read_released'] = function (block) {
  142. var _type = block.getFieldValue('button_type');
  143. var statements_input = Blockly.Python.statementToCode(block, 'input');
  144. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  145. sys.path.append("/root/")`
  146. Blockly.Python.definitions_['v831_import_CocoPi_BUTTON'] = `from CocoPi import BUTTON`
  147. // Blockly.Python.definitions_['v831_import_CocoPi'] = `sys.path.append("/root/")`
  148. Blockly.Python.definitions_['import_define_x_v831_button'] = `key_A = BUTTON(14)
  149. key_B = BUTTON(8)
  150. key_C = BUTTON(13)
  151. key_D = BUTTON(7)
  152. `
  153. let allBlocks = block.workspace.getAllBlocks();
  154. let global = ""
  155. try {
  156. global = allBlocks[0].workspace.variableList.toString()
  157. }
  158. catch (e) {
  159. console.log(e)
  160. }
  161. Blockly.Python.addFunction("BUTTONisPressed" + _type, `def BUTTONisPressed${_type}():
  162. ${global ? 'global ' + global : ""}
  163. ${statements_input || " pass"}\n`, true)
  164. var code = `if key_${_type}.is_pressed():
  165. while not (key_${_type}.is_pressed() == False):
  166. time.sleep(0.1)
  167. BUTTONisPressed${_type}()
  168. `
  169. return code;
  170. };
  171. Blockly.Blocks['ai_v831_button_loosen'] = {
  172. init: function () {
  173. this.appendDummyInput()
  174. .appendField(Blockly.Msg.TOUCH_CASE)
  175. .appendField(new Blockly.FieldDropdown([
  176. ["A", "A"],
  177. ["B", "B"],
  178. ["C", "C"],
  179. ["D", "D"]
  180. ]), "button_type")
  181. .appendField(Blockly.Msg.ESP32RELEASED)
  182. this.appendStatementInput("input")
  183. .setCheck(null)
  184. .appendField(Blockly.Msg.basic_motion_statement_exec);
  185. // this.setOutput(true, null);
  186. this.setNextStatement(true);
  187. this.setPreviousStatement(true);
  188. this.setColour("#d42b03");
  189. var thisBlock = this;
  190. this.setTooltip(function () {
  191. var mode = thisBlock.getFieldValue('button_type');
  192. var TOOLTIPS = {
  193. '0': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "A"),
  194. '1': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "B"),
  195. '2': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "C"),
  196. '3': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "D"),
  197. };
  198. return TOOLTIPS[mode];
  199. });
  200. this.setHelpUrl("");
  201. }
  202. };
  203. Blockly.Python['ai_v831_button_loosen'] = function (block) {
  204. var _type = block.getFieldValue('button_type');
  205. var statements_input = Blockly.Python.statementToCode(block, 'input');
  206. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  207. sys.path.append("/root/")`
  208. Blockly.Python.definitions_['v831_import_CocoPi_BUTTON'] = `from CocoPi import BUTTON`
  209. // Blockly.Python.definitions_['v831_import_CocoPi'] = `sys.path.append("/root/")`
  210. Blockly.Python.definitions_['import_define_x_v831_button'] = `key_A = BUTTON(14)
  211. key_B = BUTTON(8)
  212. key_C = BUTTON(13)
  213. key_D = BUTTON(7)
  214. `
  215. let allBlocks = block.workspace.getAllBlocks();
  216. let global = ""
  217. try {
  218. global = allBlocks[0].workspace.variableList.toString()
  219. }
  220. catch (e) {
  221. console.log(e)
  222. }
  223. Blockly.Python.addFunction("NOBUTTONisPressed" + _type, `def NOBUTTONisPressed${_type}():
  224. ${global ? 'global ' + global : ""}
  225. ${statements_input || " pass"}\n`, true)
  226. var code = `if key_${_type}.is_pressed() == False:
  227. NOBUTTONisPressed${_type}()
  228. `
  229. return code;
  230. };
  231. Blockly.Blocks['ai_v831_button_del'] = {
  232. init: function () {
  233. this.appendDummyInput()
  234. .appendField(Blockly.Msg.ai_basic_button_when)
  235. .appendField(new Blockly.FieldDropdown([
  236. ["A", "A"],
  237. ["B", "B"],
  238. ["C", "C"],
  239. ["D", "D"]
  240. ]), "button_type_released")
  241. .appendField(Blockly.Msg.ai_basic_released)
  242. this.setPreviousStatement(true, null);
  243. this.setNextStatement(true, null);
  244. this.setColour("#d42b03");
  245. var thisBlock = this;
  246. this.setTooltip(function () {
  247. var mode = thisBlock.getFieldValue('button_type_released');
  248. var TOOLTIPS = {
  249. '0': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "A"),
  250. '1': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "B"),
  251. '2': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "C"),
  252. '3': Blockly.Msg.ai_k210_button_read_pressed_TOOLTIP.replace('%1', "D"),
  253. };
  254. return TOOLTIPS[mode];
  255. });
  256. this.setHelpUrl("");
  257. }
  258. };
  259. Blockly.Python['ai_v831_button_del'] = function (block) {
  260. var _type = block.getFieldValue('button_type_released');
  261. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  262. sys.path.append("/root/")`
  263. Blockly.Python.definitions_['v831_import_CocoPi_BUTTON'] = `from CocoPi import BUTTON`
  264. // Blockly.Python.definitions_['v831_import_CocoPi'] = `sys.path.append("/root/")`
  265. Blockly.Python.definitions_['import_define_x_v831_button'] = `key_A = BUTTON(14)
  266. key_B = BUTTON(8)
  267. key_C = BUTTON(13)
  268. key_D = BUTTON(7)
  269. `
  270. var code = `key_${_type}.button.release()
  271. del key_${_type}
  272. `
  273. return code;
  274. };
  275. Blockly.Blocks['ai_v831_led_light_up'] = {
  276. init: function () {
  277. this.appendDummyInput()
  278. .appendField(Blockly.Msg.ai_basic_led_set)
  279. .appendField(new Blockly.FieldDropdown([
  280. [Blockly.Msg.ai_basic_led_blue, "blue"],
  281. // [Blockly.Msg.ai_basic_led_red, "red"]
  282. ]), "type")
  283. .appendField(Blockly.Msg.ai_basic_led_on);
  284. this.setPreviousStatement(true, null);
  285. this.setNextStatement(true, null);
  286. this.setColour("#d42b03");
  287. var thisBlock = this;
  288. this.setTooltip(function () {
  289. var mode = thisBlock.getFieldValue('type');
  290. var TOOLTIPS = {
  291. 'blue': Blockly.Msg.ai_k210_led_light_up_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_blue),
  292. 'red': Blockly.Msg.ai_k210_led_light_up_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_red)
  293. };
  294. return TOOLTIPS[mode];
  295. });
  296. this.setHelpUrl("");
  297. }
  298. };
  299. Blockly.Python['ai_v831_led_light_up'] = function (block) {
  300. var dropdown_type = block.getFieldValue('type');
  301. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  302. sys.path.append("/root/")`
  303. Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
  304. Blockly.Python.definitions_['v831_import_time'] = `import time`
  305. Blockly.Python.definitions_['v831_import_CocoPi_LED'] = `from CocoPi import LED
  306. LED = LED()
  307. `
  308. // TODO: Assemble Python into code variable.
  309. var code = `LED.out(1)
  310. `;
  311. return code;
  312. };
  313. Blockly.Blocks['ai_v831_led_light_off'] = {
  314. init: function () {
  315. this.appendDummyInput()
  316. .appendField(Blockly.Msg.ai_basic_led_set)
  317. .appendField(new Blockly.FieldDropdown([
  318. [Blockly.Msg.ai_basic_led_blue, "blue"],
  319. // [Blockly.Msg.ai_basic_led_red, "red"]
  320. ]), "type")
  321. .appendField(Blockly.Msg.ai_basic_led_off);
  322. this.setPreviousStatement(true, null);
  323. this.setNextStatement(true, null);
  324. this.setColour("#d42b03");
  325. var thisBlock = this;
  326. this.setTooltip(function () {
  327. var mode = thisBlock.getFieldValue('type');
  328. var TOOLTIPS = {
  329. 'blue': Blockly.Msg.ai_k210_led_light_off_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_blue),
  330. 'red': Blockly.Msg.ai_k210_led_light_off_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_red)
  331. };
  332. return TOOLTIPS[mode];
  333. });
  334. this.setHelpUrl("");
  335. }
  336. };
  337. Blockly.Python['ai_v831_led_light_off'] = function (block) {
  338. var dropdown_type = block.getFieldValue('type');
  339. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  340. sys.path.append("/root/")`
  341. Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
  342. Blockly.Python.definitions_['v831_import_time'] = `import time`
  343. Blockly.Python.definitions_['v831_import_CocoPi_LED'] = `from CocoPi import LED
  344. LED = LED()
  345. `
  346. // TODO: Assemble Python into code variable.
  347. var code = `LED.out(0)
  348. `;
  349. return code;
  350. };
  351. Blockly.Blocks['ai_v831_rgb_light_up'] = {
  352. init: function () {
  353. this.appendDummyInput()
  354. .appendField(Blockly.Msg.ai_basic_rgb_set_on)
  355. this.appendValueInput("rgb_value")
  356. .setCheck(null)
  357. .appendField("");
  358. this.appendDummyInput()
  359. .appendField(Blockly.Msg.LED_BRTN).appendField(new Blockly.FieldNumber(50, 0, 255), "uart_bps");
  360. this.setPreviousStatement(true, null);
  361. this.setNextStatement(true, null);
  362. this.setColour("#d42b03");
  363. this.setTooltip(Blockly.Msg.ai_basic_rgb_set_on);
  364. this.setHelpUrl("");
  365. }
  366. };
  367. Blockly.Python['ai_v831_rgb_light_up'] = function (block) {
  368. var value_name = Blockly.Python.valueToCode(block, 'rgb_value', Blockly.Python.ORDER_ATOMIC);
  369. var content = block.getFieldValue('uart_bps');
  370. console.log(value_name.slice(1, -1))
  371. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  372. sys.path.append("/root/")`
  373. Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
  374. Blockly.Python.definitions_['v831_import_time'] = `import time`
  375. Blockly.Python.definitions_['v831_import_CocoPi_RGB'] = `from CocoPi import singleRgb
  376. singleRgb=singleRgb()`;
  377. var code = `singleRgb.setBrightness(${content})
  378. singleRgb.setColor(${value_name.slice(1, -1)})
  379. time.sleep(0.05)
  380. singleRgb.show()
  381. `;
  382. return code;
  383. };
  384. Blockly.Blocks['ai_v831_rgb_light_off'] = {
  385. init: function () {
  386. this.appendDummyInput()
  387. .appendField(Blockly.Msg.ai_basic_rgb_set_off)
  388. this.setPreviousStatement(true, null);
  389. this.setNextStatement(true, null);
  390. this.setColour("#d42b03");
  391. this.setTooltip(Blockly.Msg.ai_basic_rgb_set_off);
  392. this.setHelpUrl("");
  393. }
  394. };
  395. Blockly.Python['ai_v831_rgb_light_off'] = function (block) {
  396. var dropdown_type = block.getFieldValue('type');
  397. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  398. sys.path.append("/root/")`
  399. Blockly.Python.definitions_['v831_import_smbus2'] = `import smbus2`
  400. Blockly.Python.definitions_['v831_import_time'] = `import time`
  401. Blockly.Python.definitions_['v831_import_CocoPi_RGB'] = `from CocoPi import singleRgb
  402. singleRgb=singleRgb()`;
  403. // TODO: Assemble Python into code variable.
  404. var code = `singleRgb.setColor(0,0,0)
  405. time.sleep(0.05)
  406. singleRgb.show()
  407. `;
  408. return code;
  409. };
  410. Blockly.Blocks['ai_basic_light_up_led_analog'] = {
  411. init: function () {
  412. this.appendValueInput("brightness")
  413. .setCheck(null)
  414. .appendField(Blockly.Msg.ai_basic_led_analog_set)
  415. .appendField(new Blockly.FieldDropdown([
  416. [Blockly.Msg.ai_basic_led_analog_led1, "1"],
  417. [Blockly.Msg.ai_basic_led_analog_led2, "2"]
  418. ]), "type")
  419. .appendField(Blockly.Msg.ai_basic_led_analog_brightness_text);
  420. this.appendDummyInput()
  421. .appendField(Blockly.Msg.ai_basic_led_analog_range_text);
  422. this.setInputsInline(true);
  423. this.setPreviousStatement(true, null);
  424. this.setNextStatement(true, null);
  425. this.setColour("#d42b03");
  426. var thisBlock = this;
  427. this.setTooltip(function () {
  428. var mode = thisBlock.getFieldValue('type');
  429. var TOOLTIPS = {
  430. '1': Blockly.Msg.ai_basic_light_up_led_analog_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_analog_led1),
  431. '2': Blockly.Msg.ai_basic_light_up_led_analog_TOOLTIP.replace('%1', Blockly.Msg.ai_basic_led_analog_led2)
  432. };
  433. return TOOLTIPS[mode];
  434. });
  435. this.setHelpUrl("");
  436. }
  437. };
  438. Blockly.Python['ai_basic_light_up_led_analog'] = function (block) {
  439. var dropdown_type = block.getFieldValue('type');
  440. var value_brightness = Blockly.Python.valueToCode(block, 'brightness', Blockly.Python.ORDER_ATOMIC);
  441. Blockly.Python.definitions_['v831_import_time'] = `import time`
  442. Blockly.Python.definitions_['ai_basic_light_up_led_analog_def'] = '' +
  443. 'from machine import Timer,PWM\n' +
  444. '\n' +
  445. 'led_left_tim = Timer(Timer.TIMER2, Timer.CHANNEL0, mode=Timer.MODE_PWM)\n' +
  446. 'led_right_tim = Timer(Timer.TIMER2, Timer.CHANNEL1, mode=Timer.MODE_PWM)\n' +
  447. 'led_left_ctrl = PWM(led_left_tim, freq=500000, duty=50, pin=31)\n' +
  448. 'led_right_ctrl = PWM(led_right_tim, freq=500000, duty=50, pin=32)\n' +
  449. '\n' +
  450. 'def analog_light(which, brightness):\n' +
  451. ' if which == 1:\n' +
  452. ' led_left_ctrl.duty(brightness)\n' +
  453. ' if which == 2:\n' +
  454. ' led_right_ctrl.duty(brightness)\n' +
  455. '';
  456. // TODO: Assemble Python into code variable.
  457. var code = 'analog_light(' + dropdown_type + ',' + value_brightness + ')\n';
  458. return code;
  459. };
  460. /*
  461. _ ___ ___ ____
  462. / \ |_ _| |_ _|_ __ ___ __ _ __ _ ___ | _ \ _ __ ___ ___ ___ ___ ___
  463. / _ \ | | | || '_ ` _ \ / _` |/ _` |/ _ \ | |_) | '__/ _ \ / __/ _ \/ __/ __|
  464. / ___ \ | | | || | | | | | (_| | (_| | __/ | __/| | | (_) | (_| __/\__ \__ \
  465. /_/ \_\___| |___|_| |_| |_|\__,_|\__, |\___| |_| |_| \___/ \___\___||___/___/
  466. |___/
  467. */
  468. Blockly.Blocks["ai_lcd_screeninit"] = {
  469. init: function () {
  470. // this.appendDummyInput()
  471. // .appendField(new Blockly.FieldImage("blockly/media/screen_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  472. // this.appendDummyInput()
  473. // .appendField(Blockly.Msg.image_process_lcd_init);
  474. this.appendDummyInput().appendField(Blockly.Msg.image_process_lcd_direction)
  475. .appendField(new Blockly.FieldDropdown([
  476. [Blockly.Msg.image_process_lcd_landscape_screen, "False"],
  477. [Blockly.Msg.image_process_lcd_vertical_screen, "True"]
  478. ]), "isScreen")
  479. this.setInputsInline(false);
  480. this.setPreviousStatement(true);
  481. this.setNextStatement(true);
  482. this.setColour("#5cb2d6");
  483. this.setTooltip(Blockly.Msg.ai_lcd_screeninit_TOOLTIP);
  484. this.setHelpUrl('');
  485. }
  486. }
  487. Blockly.Python.ai_lcd_screeninit = function (block) {
  488. var isScreen = block.getFieldValue('isScreen');
  489. Blockly.Python.definitions_['v831_import_display'] = `from maix import display`;
  490. Blockly.Python.definitions_['v831_import_image'] = `from maix import image`;
  491. Blockly.Python.definitions_['v831_import_camera'] = `from maix import camera`;
  492. Blockly.Python.definitions_['v831_import_os'] = `import os`
  493. // Blockly.Python.addVariable("ScreenOrientation", `ScreenOrientation = ${isScreen}`, false)
  494. Blockly.Python.definitions_['v831_camera_set'] = `cameraSize = True
  495. def CAMERATYPE():
  496. global cameraSize
  497. try:
  498. if os.path.exists("/etc/cameraSize.cfg"):
  499. cameraSize = True
  500. else:
  501. cameraSize = False
  502. except:
  503. cameraSize = False
  504. CAMERATYPE()
  505. `
  506. Blockly.Python.definitions_['v831_camera_set_ScreenOrientation'] = `ScreenOrientation = ${isScreen}`
  507. var _code = ``;
  508. return _code;;
  509. }
  510. Blockly.Blocks['ai_lcd_rotation'] = {
  511. init: function () {
  512. this.appendDummyInput()
  513. .appendField(Blockly.Msg.LCD_CANVAS + Blockly.Msg.image_process_set_lcd_rotation)
  514. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  515. // .appendField()
  516. .appendField(new Blockly.FieldDropdown([
  517. ["0°", "0"],
  518. ["90°", "90"],
  519. ["180°", "180"],
  520. ["270°", "270"]
  521. ]), "DEGREE")
  522. .appendField(Blockly.Msg.CATEGORY_SHOW)
  523. this.setInputsInline(true);
  524. this.setPreviousStatement(true, null);
  525. this.setNextStatement(true, null);
  526. this.setColour("#5bb2d6");
  527. this.setTooltip(Blockly.Msg.ai_lcd_rotation_TOOLTIP);
  528. this.setHelpUrl('');
  529. }
  530. }
  531. Blockly.Python.ai_lcd_rotation = function (block) {
  532. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  533. var dropdown_name = block.getFieldValue('DEGREE');
  534. Blockly.Python.addFunction('v831_rotation_fun', `def screenShow(inputImg,rotationAngle):
  535. if rotationAngle==90 or rotationAngle == 270:
  536. screenCapture=inputImg.crop(0,0,240,320)
  537. else:
  538. screenCapture=inputImg.crop(0,0,320,240)
  539. canvas_screenShow = screenCapture.rotate(-rotationAngle,adjust=1)
  540. display.show(canvas_screenShow)
  541. `)
  542. let w = ''
  543. let h = ''
  544. if (dropdown_name == 90 || dropdown_name == 270) {
  545. w = 240
  546. h = 320
  547. } else {
  548. w = 320
  549. h = 240
  550. }
  551. var _code = `screenShow(canvas,${dropdown_name})
  552. `;
  553. return _code;;
  554. }
  555. Blockly.Blocks['ai_lcd_font'] = {
  556. init: function () {
  557. this.appendDummyInput().appendField(Blockly.Msg.image_lcd_set_font)
  558. this.appendValueInput("save_path")
  559. .setCheck(null)
  560. this.setInputsInline(true);
  561. this.setPreviousStatement(true, null);
  562. this.setNextStatement(true, null);
  563. this.setColour("#5bb2d6");
  564. this.setTooltip(Blockly.Msg.image_lcd_set_font);
  565. this.setHelpUrl('');
  566. }
  567. }
  568. Blockly.Python.ai_lcd_font = function (block) {
  569. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  570. var font_path = Blockly.Python.valueToCode(block, 'save_path', Blockly.Python.ORDER_ATOMIC);
  571. var _code = `image.load_freetype(${font_path})\n`;
  572. return _code;;
  573. }
  574. Blockly.Blocks['ai_lcd_filp'] = {
  575. init: function () {
  576. this.appendDummyInput()
  577. .appendField(Blockly.Msg.image_process_set_lcd_mirror_flip)
  578. .appendField(new Blockly.FieldCheckbox('TRUE'), 'TextCheckBox');
  579. this.setInputsInline(true);
  580. this.setPreviousStatement(true, null);
  581. this.setNextStatement(true, null);
  582. this.setColour("#5bb2d6");
  583. this.setTooltip(Blockly.Msg.ai_lcd_filp_TOOLTIP);
  584. this.setHelpUrl('');
  585. }
  586. }
  587. Blockly.Python.ai_lcd_filp = function (block) {
  588. var _tf = block.getFieldValue('TextCheckBox') == 'TRUE' ? "True" : "False";
  589. var _code = "lcd.mirror(" + _tf + ")\n";
  590. return _code;
  591. }
  592. Blockly.Blocks['ai_lcd_screen'] = {
  593. init: function () {
  594. this.appendDummyInput()
  595. .appendField(Blockly.Msg.image_process_set_filled_screen_color)
  596. .appendField(new Blockly.FieldDropdown([
  597. [Blockly.Msg.image_process_set_filled_screen_color_red, "RED"],
  598. [Blockly.Msg.image_process_set_filled_screen_color_green, "GREEN"],
  599. [Blockly.Msg.image_process_set_filled_screen_color_blue, "BLUE"],
  600. [Blockly.Msg.image_process_set_filled_screen_color_purple, "PURPLE"],
  601. [Blockly.Msg.image_process_set_filled_screen_color_cyan, "CYAN"],
  602. [Blockly.Msg.image_process_set_filled_screen_color_orange, "ORANGE"],
  603. [Blockly.Msg.image_process_set_filled_screen_color_yellow, "YELLOW"],
  604. [Blockly.Msg.image_process_set_filled_screen_color_black, "BLACK"],
  605. [Blockly.Msg.image_process_set_filled_screen_color_white, "WHITE"]
  606. ]), "COLOR");
  607. this.setInputsInline(true);
  608. this.setPreviousStatement(true, null);
  609. this.setNextStatement(true, null);
  610. this.setColour("#5bb2d6");
  611. var thisBlock = this;
  612. this.setTooltip(function () {
  613. var mode = thisBlock.getFieldValue('COLOR');
  614. var TOOLTIPS = {
  615. 'RED': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_red),
  616. 'GREEN': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_green),
  617. 'BLUE': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_blue),
  618. 'PURPLE': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_purple),
  619. 'CYAN': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_cyan),
  620. 'ORANGE': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_orange),
  621. 'YELLOW': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_yellow),
  622. 'BLACK': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_black),
  623. 'WHITE': Blockly.Msg.ai_lcd_screen_TOOLTIP.replace('%1', Blockly.Msg.image_process_set_filled_screen_color_white)
  624. };
  625. return TOOLTIPS[mode];
  626. });
  627. this.setHelpUrl('');
  628. }
  629. };
  630. Blockly.Python.ai_lcd_screen = function (block) {
  631. var colour_name = block.getFieldValue('COLOR');
  632. var _code = "lcd.clear(lcd." + colour_name + ")\n";
  633. return _code;
  634. }
  635. Blockly.Blocks['ai_lcd_fill_screen_with_rgb'] = {
  636. init: function () {
  637. this.appendDummyInput()
  638. .appendField(Blockly.Msg.image_process_text_let_canvas)
  639. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  640. .appendField(Blockly.Msg.image_process_text_let_canvas_filled_with_color);
  641. this.appendValueInput("rgb_value")
  642. .setCheck(null)
  643. .appendField("");
  644. this.setInputsInline(true);
  645. this.setPreviousStatement(true, null);
  646. this.setNextStatement(true, null);
  647. this.setColour("#5bb2d6");
  648. this.setTooltip(Blockly.Msg.ai_lcd_fill_screen_with_rgb_TOOLTIP);
  649. this.setHelpUrl("");
  650. }
  651. };
  652. Blockly.Python['ai_lcd_fill_screen_with_rgb'] = function (block) {
  653. var value_name = Blockly.Python.valueToCode(block, 'rgb_value', Blockly.Python.ORDER_ATOMIC);
  654. // TODO: Assemble Python into code variable.
  655. if (value_name.charAt(0) == '#') {
  656. var d = 0,
  657. e = 0,
  658. f = 0;
  659. try {
  660. 7 == value_name.length && (d = parseInt(value_name.substring(1, 3), 16),
  661. e = parseInt(value_name.substring(3, 5), 16),
  662. f = parseInt(value_name.substring(5, 7), 16))
  663. } catch (g) { }
  664. // TODO: Assemble Python into code variable.
  665. value_name = "(" + d + "," + e + "," + f + ")";
  666. }
  667. var code = `try:
  668. if ScreenOrientation:
  669. canvas = image.new(size = (240, 320), color = ${value_name}, mode = "RGB")
  670. else:
  671. canvas = image.new(size = (320, 240), color = ${value_name}, mode = "RGB")
  672. except:
  673. canvas = image.new(size = (320, 240), color = ${value_name}, mode = "RGB")
  674. `;
  675. return code;
  676. };
  677. Blockly.Blocks['ai_lcd_clear'] = {
  678. init: function () {
  679. this.appendDummyInput()
  680. .appendField(Blockly.Msg.image_process_clear_lcd);
  681. this.setInputsInline(true);
  682. this.setPreviousStatement(true, null);
  683. this.setNextStatement(true, null);
  684. this.setColour("#5bb2d6");
  685. this.setTooltip(Blockly.Msg.ai_lcd_clear_TOOLTIP);
  686. this.setHelpUrl('');
  687. }
  688. };
  689. Blockly.Python.ai_lcd_clear = function (block) {
  690. var _code = "lcd.clear(lcd.BLACK)\n";
  691. return _code;
  692. }
  693. Blockly.Blocks['ai_lcd_rgb_value_input'] = {
  694. init: function () {
  695. this.appendDummyInput()
  696. .appendField(Blockly.Msg.image_process_rgb_r);
  697. this.appendValueInput("rgb_value_r")
  698. .setCheck(null)
  699. .appendField("");
  700. this.appendDummyInput()
  701. .appendField(Blockly.Msg.image_process_rgb_g);
  702. this.appendValueInput("rgb_value_g")
  703. .setCheck(null)
  704. .appendField("");
  705. this.appendDummyInput()
  706. .appendField(Blockly.Msg.image_process_rgb_b);
  707. this.appendValueInput("rgb_value_b")
  708. .setCheck(null)
  709. .appendField("");
  710. this.setInputsInline(true);
  711. this.setOutput(true, null);
  712. this.setColour("#5bb2d6");
  713. this.setTooltip(Blockly.Msg.ai_lcd_rgb_value_input_TOOLTIP);
  714. this.setHelpUrl("");
  715. }
  716. };
  717. Blockly.Python['ai_lcd_rgb_value_input'] = function (block) {
  718. var value_rgb_value_r = Blockly.Python.valueToCode(block, 'rgb_value_r', Blockly.Python.ORDER_ATOMIC);
  719. var value_rgb_value_g = Blockly.Python.valueToCode(block, 'rgb_value_g', Blockly.Python.ORDER_ATOMIC);
  720. var value_rgb_value_b = Blockly.Python.valueToCode(block, 'rgb_value_b', Blockly.Python.ORDER_ATOMIC);
  721. // TODO: Assemble Python into code variable.
  722. var code = '' + value_rgb_value_r + ',' + value_rgb_value_g + ',' + value_rgb_value_b + '';
  723. // TODO: Change ORDER_NONE to the correct strength.
  724. return [code, Blockly.Python.ORDER_NONE];
  725. };
  726. Blockly.Blocks.ai_lcd_XY = {
  727. init: function () {
  728. var _input = this.appendDummyInput();
  729. this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
  730. this.setTooltip(Blockly.Msg.ai_lcd_XY_TOOLTIP);
  731. this.setColour("#5bb2d6");
  732. this.setOutput(!0, "Boolean");
  733. _input.appendField(Blockly.Msg.image_process_xy_x);
  734. this.appendValueInput("A").setCheck("Number");
  735. _input = this.appendDummyInput();
  736. _input.appendField(Blockly.Msg.image_process_xy_y)
  737. this.appendValueInput("B").setCheck("Number");
  738. this.setInputsInline(!0);
  739. }
  740. };
  741. Blockly.Python.ai_lcd_XY = function (block) {
  742. var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
  743. var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
  744. var code = "" + _x + ", " + _y + "";
  745. return [code, Blockly.Python.ORDER_ATOMIC];
  746. };
  747. Blockly.Blocks.ai_lcd_WH = {
  748. init: function () {
  749. var _input = this.appendDummyInput();
  750. this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
  751. this.setTooltip(Blockly.Msg.ai_lcd_WH_TOOLTIP);
  752. this.setColour("#5bb2d6");
  753. this.setOutput(!0, "Boolean");
  754. _input.appendField(Blockly.Msg.image_process_wh_width);
  755. this.appendValueInput("A").setCheck("Number");
  756. _input = this.appendDummyInput();
  757. _input.appendField(Blockly.Msg.image_process_wh_height)
  758. this.appendValueInput("B").setCheck("Number");
  759. this.setInputsInline(!0);
  760. }
  761. };
  762. Blockly.Python.ai_lcd_WH = function (block) {
  763. var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
  764. var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
  765. var code = "" + _x + ", " + _y + "";
  766. return [code, Blockly.Python.ORDER_ATOMIC];
  767. };
  768. Blockly.Blocks['ai_lcd_colortext'] = {
  769. init: function () {
  770. this.appendDummyInput()
  771. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_text.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  772. this.appendDummyInput()
  773. .appendField(Blockly.Msg.image_process_lcd_show_text);
  774. this.appendDummyInput()
  775. .appendField(Blockly.Msg.image_process_lcd_show_text_color)
  776. .appendField(new Blockly.FieldDropdown([
  777. [Blockly.Msg.image_process_lcd_show_text_color_white, "WHITE"],
  778. [Blockly.Msg.image_process_lcd_show_text_color_black, "BLACK"],
  779. [Blockly.Msg.image_process_lcd_show_text_color_red, "RED"],
  780. [Blockly.Msg.image_process_lcd_show_text_color_green, "GREEN"],
  781. [Blockly.Msg.image_process_lcd_show_text_color_blue, "BLUE"],
  782. [Blockly.Msg.image_process_lcd_show_text_color_purple, "PURPLE"],
  783. [Blockly.Msg.image_process_lcd_show_text_color_cyan, "CYAN"],
  784. [Blockly.Msg.image_process_lcd_show_text_color_orange, "ORANGE"],
  785. [Blockly.Msg.image_process_lcd_show_text_color_yellow, "YELLOW"]
  786. ]), "COLOR");
  787. this.appendDummyInput()
  788. .appendField(Blockly.Msg.image_process_lcd_show_text_bg_color)
  789. .appendField(new Blockly.FieldDropdown([
  790. [Blockly.Msg.image_process_lcd_show_text_color_black, "BLACK"],
  791. [Blockly.Msg.image_process_lcd_show_text_color_white, "WHITE"],
  792. [Blockly.Msg.image_process_lcd_show_text_color_red, "RED"],
  793. [Blockly.Msg.image_process_lcd_show_text_color_green, "GREEN"],
  794. [Blockly.Msg.image_process_lcd_show_text_color_blue, "BLUE"],
  795. [Blockly.Msg.image_process_lcd_show_text_color_purple, "PURPLE"],
  796. [Blockly.Msg.image_process_lcd_show_text_color_cyan, "CYAN"],
  797. [Blockly.Msg.image_process_lcd_show_text_color_orange, "ORANGE"],
  798. [Blockly.Msg.image_process_lcd_show_text_color_yellow, "YELLOW"]
  799. ]), "BCOLOR");
  800. this.appendValueInput("CONTENT")
  801. .setCheck("String")
  802. .appendField(Blockly.Msg.image_process_text_content);
  803. /* this.appendDummyInput()
  804. .appendField(Blockly.Msg.LCDDT); */
  805. this.appendValueInput("POSA")
  806. .setCheck("String")
  807. .appendField(Blockly.Msg.image_process_text_start_coord);
  808. this.setPreviousStatement(true, null);
  809. this.setNextStatement(true, null);
  810. this.setColour("#5bb2d6");
  811. this.setTooltip('');
  812. this.setHelpUrl('');
  813. }
  814. };
  815. Blockly.Python['ai_lcd_colortext'] = function (block) {
  816. var color = block.getFieldValue("COLOR");
  817. var bcolor = block.getFieldValue("BCOLOR");
  818. var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
  819. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  820. //var size = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
  821. var _code = "lcd_draw_string(lcd," + xy + ", " + content + ", color=" + color + ")\n"; //, scale=" + size + "
  822. // return [_code, Blockly.Python.ORDER_ATOMIC]
  823. return _code;
  824. };
  825. Blockly.Blocks["ai_lcd_createnonecarvas"] = {
  826. init: function () {
  827. this.appendDummyInput()
  828. .appendField(Blockly.Msg.image_process_create_blank_canvas)
  829. .appendField(new Blockly.FieldVariable("canvas"), "varitem");
  830. this.appendValueInput("wh")
  831. .setCheck(null)
  832. .appendField(Blockly.Msg.image_process_text_size);
  833. // this.appendDummyInput()
  834. // .appendField(Blockly.Msg.LCDDCC_F)
  835. // .appendField(new Blockly.FieldCheckbox("TRUE"), "check")
  836. // .appendField(Blockly.Msg.LCDDCC_I)
  837. // .appendField(new Blockly.FieldTextInput(""), "text");
  838. this.setInputsInline(true);
  839. this.setPreviousStatement(true, null);
  840. this.setNextStatement(true, null);
  841. this.setColour("#5cb2d6");
  842. this.setTooltip(Blockly.Msg.ai_lcd_createnonecarvas_TOOLTIP);
  843. this.setHelpUrl('');
  844. }
  845. }
  846. Blockly.Python.ai_lcd_createnonecarvas = function (block) {
  847. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  848. var value_wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
  849. var _code = variable_name + " = image.new(size = (" + value_wh + "))\n";
  850. return _code;
  851. }
  852. Blockly.Blocks["ai_lcd_fillcanvas"] = {
  853. init: function () {
  854. this.appendDummyInput()
  855. .appendField(Blockly.Msg.image_process_text_let_canvas)
  856. .appendField(new Blockly.FieldVariable("canvas"), "varitem");
  857. this.appendValueInput("color")
  858. .setCheck(null)
  859. .appendField(Blockly.Msg.image_process_text_let_canvas_filled_with_color);
  860. this.setInputsInline(true);
  861. this.setPreviousStatement(true, null);
  862. this.setNextStatement(true, null);
  863. this.setColour("#5cb2d6");
  864. this.setTooltip(Blockly.Msg.ai_lcd_fillcanvas_TOOLTIP);
  865. this.setHelpUrl('');
  866. }
  867. }
  868. Blockly.Python.ai_lcd_fillcanvas = function (block) {
  869. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  870. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  871. var _code = variable_name + '.draw_rectangle(0,0, ' + variable_name + '.width(),' + variable_name + '.height(), color=' + value_color + ', thickness=1, fill=True)\n';
  872. return _code;
  873. }
  874. Blockly.Blocks["ai_lcd_createcarvas"] = {
  875. init: function () {
  876. this.appendDummyInput()
  877. .appendField(Blockly.Msg.image_process_create_image_canvas)
  878. .appendField(new Blockly.FieldVariable("canvas"), "varitem");
  879. this.appendValueInput("path").setCheck(null).appendField(Blockly.Msg.image_process_create_image_canvas_path);
  880. this.setPreviousStatement(true, null);
  881. this.setNextStatement(true, null);
  882. // this.setInputsInline(true);
  883. this.setColour("#5cb2d6");
  884. this.setTooltip(Blockly.Msg.ai_lcd_createcarvas_TOOLTIP);
  885. this.setHelpUrl('');
  886. }
  887. }
  888. Blockly.Python.ai_lcd_createcarvas = function (block) {
  889. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  890. // var checkbox_name = block.getFieldValue('check') == 'TRUE' ? "True" : "False";
  891. var text_name = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  892. var _code = variable_name + " = image.open(" + text_name + ")\n";
  893. return _code;
  894. }
  895. Blockly.Blocks['ai_lcd_showcarvas_set_cord'] = {
  896. init: function () {
  897. this.appendDummyInput()
  898. .appendField(Blockly.Msg.image_process_text_set_canvas_setup)
  899. // .appendField(new Blockly.FieldVariable("canvas"), "varitem");
  900. this.appendValueInput("xy")
  901. .setCheck(null)
  902. .appendField(Blockly.Msg.image_process_text_start_coord_as);
  903. this.setInputsInline(true);
  904. this.setPreviousStatement(true, null);
  905. this.setNextStatement(true, null);
  906. this.setColour("#5cb2d6");
  907. this.setTooltip(Blockly.Msg.ai_lcd_showcarvas_set_cord_TOOLTIP);
  908. this.setHelpUrl("");
  909. }
  910. };
  911. Blockly.Python.ai_lcd_showcarvas_set_cord = function (block) {
  912. var value_name = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  913. // TODO: Assemble Python into code variable.
  914. var code = "_canvas_x, _canvas_y = " + value_name + "\n";
  915. return code;
  916. };
  917. Blockly.Blocks['ai_lcd_showcarvas_set_display'] = {
  918. init: function () {
  919. this.appendDummyInput()
  920. .appendField(Blockly.Msg.image_process_show_canvas)
  921. // .appendField(new Blockly.FieldVariable("canvas"), "varitem");
  922. this.setInputsInline(false);
  923. this.setPreviousStatement(true, null);
  924. this.setNextStatement(true, null);
  925. this.setColour("#5cb2d6");
  926. this.setTooltip(Blockly.Msg.ai_lcd_showcarvas_set_display_TOOLTIP);
  927. this.setHelpUrl("");
  928. }
  929. };
  930. Blockly.Python.ai_lcd_showcarvas_set_display = function (block) {
  931. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  932. Blockly.Python.definitions_['v831_import_mjpg'] = `from maix import mjpg`
  933. Blockly.Python.definitions_['v831_import_utils'] = `from maix import utils`
  934. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  935. Blockly.Python.definitions_['v831_import_time'] = `import time`
  936. Blockly.Python.definitions_['v831_import_display'] = `from maix import display`;
  937. Blockly.Python.definitions_['v831_import_image'] = `from maix import image`;
  938. Blockly.Python.definitions_['v831_import_camera'] = `from maix import camera`;
  939. Blockly.Python.addVariable(`_canvas_x`, `_canvas_x = 0`, true)
  940. Blockly.Python.addVariable(`_canvas_y`, `_canvas_y = 0`, true)
  941. Blockly.Python.definitions_['v831_camera_set'] = `cameraSize = True
  942. def CAMERATYPE():
  943. global cameraSize
  944. try:
  945. if os.path.exists("/etc/cameraSize.cfg"):
  946. cameraSize = True
  947. else:
  948. cameraSize = False
  949. except:
  950. cameraSize = False
  951. CAMERATYPE()
  952. `
  953. Blockly.Python.definitions_['import_image'] = `image.load_freetype("/root/preset/fonts/simhei.ttf")
  954. if cameraSize==True:
  955. camera.camera.config(size=(320,240))
  956. else:
  957. camera.camera.config(size=(240,320))
  958. try:
  959. if ScreenOrientation==True:
  960. canvas = image.new(size = (240, 320))
  961. else:
  962. canvas = image.new(size = (320, 240))
  963. except:
  964. canvas = image.new(size = (320, 240))
  965. ScreenOrientation = False
  966. `
  967. Blockly.Python.definitions_[`v831_display_show_canvas`] = `def v831_display_show_canvas(displayShow,_canvas_x,_canvas_y):
  968. global ScreenOrientation,cameraSize
  969. try:
  970. if ScreenOrientation:
  971. displayShowCanvas = image.new(size = (240, 320))
  972. displayShowCanvas.draw_rectangle(0,0,240,320, color=(0,0,0), thickness=-1)
  973. displayShowCanvas.draw_image(displayShow,_canvas_x,_canvas_y,alpha=1)
  974. displayShowVER = displayShowCanvas.crop(0,0,240,320)
  975. displayShowVER = displayShowVER.rotate(-90, adjust=1)
  976. display.show(displayShowVER)
  977. else:
  978. displayShowCanvas = image.new(size = (320, 240))
  979. displayShowCanvas.draw_rectangle(0,0,320,240, color=(0,0,0), thickness=-1)
  980. displayShowCanvas.draw_image(displayShow,_canvas_x,_canvas_y,alpha=1)
  981. display.show(displayShowCanvas)
  982. except:
  983. displayShowCanvas = image.new(size = (320, 240))
  984. displayShowCanvas.draw_rectangle(0,0,320,240, color=(0,0,0), thickness=-1)
  985. displayShowCanvas.draw_image(displayShow,_canvas_x,_canvas_y,alpha=1)
  986. display.show(displayShowCanvas)
  987. `
  988. var code = `v831_display_show_canvas(canvas,_canvas_x,_canvas_y)
  989. `;
  990. return code;
  991. };
  992. Blockly.Blocks['ai_lcd_showcarvas'] = {
  993. init: function () {
  994. this.appendDummyInput()
  995. .appendField(Blockly.Msg.image_process_show_canvas)
  996. .appendField(new Blockly.FieldVariable("canvas"), "varitem");
  997. this.appendValueInput("xy")
  998. .setCheck(null)
  999. .appendField(Blockly.Msg.image_process_text_start_coord);
  1000. this.setInputsInline(false);
  1001. this.setPreviousStatement(true, null);
  1002. this.setNextStatement(true, null);
  1003. this.setColour("#5cb2d6");
  1004. this.setTooltip("");
  1005. this.setHelpUrl("");
  1006. }
  1007. };
  1008. Blockly.Python.ai_lcd_showcarvas = function (block) {
  1009. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1010. var value_name = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  1011. // TODO: Assemble Python into code variable.
  1012. var code = "lcd.display(" + variable_name + ", oft=(" + value_name + "))\n";
  1013. return code;
  1014. };
  1015. Blockly.Blocks['ai_lcd_clearcanvas'] = {
  1016. init: function () {
  1017. this.appendDummyInput()
  1018. .appendField(Blockly.Msg.image_process_clear_canvas_1 + Blockly.Msg.image_process_clear_canvas_2)
  1019. this.setPreviousStatement(true, null);
  1020. this.setNextStatement(true, null);
  1021. this.setColour("#5bb2d6");
  1022. this.setTooltip(Blockly.Msg.ai_lcd_clearcanvas_TOOLTIP);
  1023. this.setHelpUrl('');
  1024. }
  1025. }
  1026. Blockly.Python.ai_lcd_clearcanvas = function (block) {
  1027. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1028. var _code = "canvas.clear()\n";
  1029. return _code;;
  1030. }
  1031. Blockly.Blocks['lcd_set_position'] = {
  1032. init: function () {
  1033. this.appendValueInput("POSX")
  1034. .setCheck("Number")
  1035. .appendField("X:");
  1036. this.appendValueInput("POSY")
  1037. .setCheck("Number")
  1038. .appendField("Y:");
  1039. this.setOutput(true, "String");
  1040. this.setInputsInline(true);
  1041. this.setColour("#5bb2d6");
  1042. this.setTooltip("");
  1043. this.setHelpUrl("");
  1044. }
  1045. };
  1046. Blockly.Python['lcd_set_position'] = function (block) {
  1047. // var reg = /^?[0-9]\d*$/
  1048. var pos_x = Blockly.Python.valueToCode(block, 'POSX', Blockly.Python.ORDER_ATOMIC);
  1049. var pos_y = Blockly.Python.valueToCode(block, 'POSY', Blockly.Python.ORDER_ATOMIC);
  1050. //var code = posConvert(pos_x, pos_y);
  1051. var code = pos_x + "," + pos_y;
  1052. return [code, Blockly.Python.ORDER_ATOMIC];
  1053. };
  1054. Blockly.Blocks['lcd_set_color'] = {
  1055. init: function () {
  1056. this.appendDummyInput()
  1057. .appendField(Blockly.Msg.COLOR)
  1058. .appendField(new Blockly.FieldColour("#ff0000"), "COLOR");
  1059. this.setOutput(true, "String");
  1060. this.setColour("#5bb2d6");
  1061. this.setTooltip("");
  1062. this.setHelpUrl("");
  1063. }
  1064. };
  1065. Blockly.Python['lcd_set_color'] = function (block) {
  1066. var code = block.getFieldValue('COLOR');
  1067. return [code, Blockly.Python.ORDER_ATOMIC];
  1068. };
  1069. Blockly.Blocks['lcd_set_width_height'] = {
  1070. init: function () {
  1071. this.appendValueInput("WIDTH")
  1072. .setCheck("Number")
  1073. .appendField(Blockly.Msg.OLCD_WIDTH);
  1074. this.appendValueInput("HEIGHT")
  1075. .setCheck("Number")
  1076. .appendField(Blockly.Msg.OLCD_HEIGHT);
  1077. this.setOutput(true, "String");
  1078. this.setInputsInline(true);
  1079. this.setColour("#5bb2d6");
  1080. this.setTooltip("");
  1081. this.setHelpUrl("");
  1082. }
  1083. };
  1084. Blockly.Python['lcd_set_width_height'] = function (block) {
  1085. var width = Blockly.Python.valueToCode(block, 'WIDTH', Blockly.Python.ORDER_ATOMIC);
  1086. var height = Blockly.Python.valueToCode(block, 'HEIGHT', Blockly.Python.ORDER_ATOMIC);
  1087. var code = width + ', ' + height;
  1088. return [code, Blockly.Python.ORDER_ATOMIC];
  1089. };
  1090. // ai_lcd_textcarvas_new_line
  1091. Blockly.Blocks['ai_lcd_textcarvas_new_line'] = {
  1092. init: function () {
  1093. this.appendDummyInput()
  1094. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_text.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  1095. this.appendDummyInput()
  1096. .appendField(Blockly.Msg.image_process_text_on_canvas + Blockly.Msg.image_process_text_on_draw + Blockly.Msg.image_process_draw_text)
  1097. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1098. // .appendField();
  1099. this.appendValueInput("POSA")
  1100. .setCheck("String")
  1101. .appendField(Blockly.Msg.image_process_text_start_coord);
  1102. this.appendValueInput("CONTENT")
  1103. .setCheck("String")
  1104. .appendField(Blockly.Msg.image_process_text_content);
  1105. this.appendValueInput("dispalyStr")
  1106. .setCheck("Number")
  1107. .appendField(Blockly.Msg.image_process_text_dispalyStr);
  1108. this.appendValueInput("buttonUp")
  1109. .setCheck("String")
  1110. .appendField(Blockly.Msg.image_process_text_buttonDown);
  1111. this.appendValueInput("buttonDown")
  1112. .setCheck("String")
  1113. .appendField(Blockly.Msg.image_process_text_buttonUp);
  1114. this.appendValueInput("COLOR")
  1115. .setCheck("String")
  1116. .appendField(Blockly.Msg.image_process_text_color);
  1117. this.appendValueInput("Scale")
  1118. .setCheck("Number")
  1119. .appendField(Blockly.Msg.image_process_draw_text_font_size);
  1120. this.appendValueInput("LineNum")
  1121. .setCheck("Number")
  1122. .appendField(Blockly.Msg.image_process_text_line_heigit);
  1123. // this.appendDummyInput()
  1124. // .appendField(Blockly.Msg.image_process_draw_text_if_monospace)
  1125. // .appendField(new Blockly.FieldCheckbox("FALSE"), "bool");
  1126. this.setInputsInline(false);
  1127. this.setPreviousStatement(true);
  1128. this.setNextStatement(true);
  1129. this.setColour("#5bb2d6");
  1130. this.setTooltip(Blockly.Msg.ai_lcd_textcarvas_TOOLTIP);
  1131. this.setHelpUrl('');
  1132. }
  1133. };
  1134. Blockly.Python.ai_lcd_textcarvas_new_line = function (block) {
  1135. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  1136. var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
  1137. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  1138. var scale = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
  1139. var LineNum = Blockly.Python.valueToCode(block, 'LineNum', Blockly.Python.ORDER_ATOMIC);
  1140. var dispalyStr = Blockly.Python.valueToCode(block, 'dispalyStr', Blockly.Python.ORDER_ATOMIC);
  1141. var buttonUp = Blockly.Python.valueToCode(block, 'buttonUp', Blockly.Python.ORDER_ATOMIC);
  1142. var buttonDown = Blockly.Python.valueToCode(block, 'buttonDown', Blockly.Python.ORDER_ATOMIC);
  1143. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1144. Blockly.Python.definitions_['v831_import_display'] = `from maix import display`;
  1145. Blockly.Python.definitions_['v831_import_image'] = `from maix import image`;
  1146. Blockly.Python.definitions_['v831_import_camera'] = `from maix import camera`;
  1147. Blockly.Python.definitions_['v831_import_os'] = `import os`
  1148. Blockly.Python.definitions_['v831_camera_set'] = `cameraSize = True
  1149. def CAMERATYPE():
  1150. global cameraSize
  1151. try:
  1152. if os.path.exists("/etc/cameraSize.cfg"):
  1153. cameraSize = True
  1154. else:
  1155. cameraSize = False
  1156. except:
  1157. cameraSize = False
  1158. CAMERATYPE()
  1159. `
  1160. Blockly.Python.definitions_['import_image'] = `image.load_freetype("/root/preset/fonts/simhei.ttf")
  1161. if cameraSize==True:
  1162. camera.camera.config(size=(320,240))
  1163. else:
  1164. camera.camera.config(size=(240,320))
  1165. try:
  1166. if ScreenOrientation==True:
  1167. canvas = image.new(size = (240, 320))
  1168. else:
  1169. canvas = image.new(size = (320, 240))
  1170. except:
  1171. canvas = image.new(size = (320, 240))
  1172. ScreenOrientation = False
  1173. `
  1174. var _code = ""
  1175. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  1176. sys.path.append("/root/")`
  1177. Blockly.Python.definitions_['v831_import_CocoPi_BUTTON'] = `from CocoPi import BUTTON`
  1178. // Blockly.Python.definitions_['v831_import_CocoPi'] = `sys.path.append("/root/")`
  1179. Blockly.Python.definitions_['import_define_x_v831_button'] = `key_A = BUTTON(14)
  1180. key_B = BUTTON(8)
  1181. key_C = BUTTON(13)
  1182. key_D = BUTTON(7)`
  1183. Blockly.Python.definitions_["ai_lcd_textcarvas_new_line"] = `BUTTONNUMBER = 0
  1184. def textCanvasNewLine(canvasName,posax,posay,text,Num,buttonUp,buttonDown,Scale,Color,LineNum):
  1185. global BUTTONNUMBER,ScreenOrientation
  1186. numText = len(text)//Num + 1
  1187. lineHeight = 240
  1188. if ScreenOrientation:
  1189. lineHeight = 320
  1190. if numText>(lineHeight//LineNum):
  1191. if buttonUp:
  1192. time.sleep(0.1)
  1193. if BUTTONNUMBER<int(numText-(lineHeight//LineNum))+ int(posay/LineNum):
  1194. BUTTONNUMBER = BUTTONNUMBER + 1
  1195. elif buttonDown:
  1196. time.sleep(0.1)
  1197. if BUTTONNUMBER>0:
  1198. BUTTONNUMBER = BUTTONNUMBER - 1
  1199. #BUTTONNUMBER = BUTTONNUMBER + (posay/LineNum)
  1200. for i in range(numText):
  1201. canvasName.draw_string(posax,posay+int(i*LineNum),text[int((i+BUTTONNUMBER)*Num):int((i+1+BUTTONNUMBER)*Num)], scale = Scale, color = Color, thickness = 1)
  1202. # canvasName.draw_string(posax,posay,str(text), scale = Scale, color = Color, thickness = 1)
  1203. `
  1204. if (color.charAt(0) == '#') {
  1205. var d = 0,
  1206. e = 0,
  1207. f = 0;
  1208. try {
  1209. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1210. e = parseInt(color.substring(3, 5), 16),
  1211. f = parseInt(color.substring(5, 7), 16))
  1212. } catch (g) { }
  1213. var bool = "False";
  1214. // var bool = block.getFieldValue('bool') == "TRUE" ? "True" : "False";
  1215. // var bool = Blockly.Python.valueToCode(block, 'POSC', Blockly.Python.ORDER_ATOMIC) == "TRUE" ? "True" : "False";
  1216. // color=(" + d + "," + e + "," + f + ")" + ", font_size=" + size + ")\n"
  1217. _code = `textCanvasNewLine(canvas,${xy},${content},${dispalyStr},${buttonUp},${buttonDown},${scale},(${d} ,${e}, ${f}),${LineNum})\n`
  1218. // _code = variable_name + '.draw_string(' + xy + ', ' + content + ', scale = ' + scale + ', color = (' + d + ',' + e + ',' + f + ') , thickness = 1)\n'; // #在红色画布上写下hello world
  1219. } else if (color.charAt(0) != '#') {
  1220. var d = 0,
  1221. e = 0,
  1222. f = 0;
  1223. try {
  1224. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1225. e = parseInt(color.substring(3, 5), 16),
  1226. f = parseInt(color.substring(5, 7), 16))
  1227. } catch (g) { }
  1228. _code = `textCanvasNewLine(canvas,${xy},${content},${dispalyStr},${buttonUp},${buttonDown},${scale},${color},${LineNum})\n`
  1229. // _code = variable_name + '.draw_string(' + xy + ', ' + content + ', scale = ' + scale + ', color = ' + color + ', thickness = 1)\n' + // #在红色画布上写下hello world
  1230. // '';
  1231. }
  1232. return _code;
  1233. }
  1234. Blockly.Blocks['ai_lcd_textcarvas'] = {
  1235. init: function () {
  1236. this.appendDummyInput()
  1237. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_text.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  1238. this.appendDummyInput()
  1239. .appendField(Blockly.Msg.image_process_text_on_canvas + Blockly.Msg.image_process_text_on_draw + Blockly.Msg.image_process_draw_text)
  1240. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1241. this.appendValueInput("POSA")
  1242. .setCheck("String")
  1243. .appendField(Blockly.Msg.image_process_text_start_coord);
  1244. this.appendValueInput("CONTENT")
  1245. .setCheck("String")
  1246. .appendField(Blockly.Msg.image_process_text_content);
  1247. this.appendValueInput("COLOR")
  1248. .setCheck("String")
  1249. .appendField(Blockly.Msg.image_process_text_color);
  1250. this.appendValueInput("Scale")
  1251. .setCheck("Number")
  1252. .appendField(Blockly.Msg.image_process_draw_text_font_size);
  1253. // this.appendDummyInput()
  1254. // .appendField(Blockly.Msg.image_process_draw_text_if_monospace)
  1255. // .appendField(new Blockly.FieldCheckbox("FALSE"), "bool");
  1256. this.setInputsInline(false);
  1257. this.setPreviousStatement(true);
  1258. this.setNextStatement(true);
  1259. this.setColour("#5bb2d6");
  1260. this.setTooltip(Blockly.Msg.ai_lcd_textcarvas_TOOLTIP);
  1261. this.setHelpUrl('');
  1262. }
  1263. };
  1264. Blockly.Python.ai_lcd_textcarvas = function (block) {
  1265. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  1266. Blockly.Python.definitions_['v831_import_display'] = `from maix import display`;
  1267. Blockly.Python.definitions_['v831_import_image'] = `from maix import image`;
  1268. Blockly.Python.definitions_['v831_import_camera'] = `from maix import camera`;
  1269. Blockly.Python.definitions_['v831_import_os'] = `import os`
  1270. Blockly.Python.definitions_['v831_camera_set'] = `cameraSize = True
  1271. def CAMERATYPE():
  1272. global cameraSize
  1273. try:
  1274. if os.path.exists("/etc/cameraSize.cfg"):
  1275. cameraSize = True
  1276. else:
  1277. cameraSize = False
  1278. except:
  1279. cameraSize = False
  1280. CAMERATYPE()
  1281. `
  1282. Blockly.Python.definitions_['import_image'] = `image.load_freetype("/root/preset/fonts/simhei.ttf")
  1283. if cameraSize==True:
  1284. camera.camera.config(size=(320,240))
  1285. else:
  1286. camera.camera.config(size=(240,320))
  1287. try:
  1288. if ScreenOrientation==True:
  1289. canvas = image.new(size = (240, 320))
  1290. else:
  1291. canvas = image.new(size = (320, 240))
  1292. except:
  1293. canvas = image.new(size = (320, 240))
  1294. ScreenOrientation = False
  1295. `
  1296. var _code = ""
  1297. if (color.charAt(0) == '#') {
  1298. var d = 0,
  1299. e = 0,
  1300. f = 0;
  1301. try {
  1302. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1303. e = parseInt(color.substring(3, 5), 16),
  1304. f = parseInt(color.substring(5, 7), 16))
  1305. } catch (g) { }
  1306. var bool = "False";
  1307. // var bool = block.getFieldValue('bool') == "TRUE" ? "True" : "False";
  1308. // var bool = Blockly.Python.valueToCode(block, 'POSC', Blockly.Python.ORDER_ATOMIC) == "TRUE" ? "True" : "False";
  1309. var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
  1310. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  1311. var scale = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
  1312. // color=(" + d + "," + e + "," + f + ")" + ", font_size=" + size + ")\n"
  1313. _code = 'canvas.draw_string(' + xy + ', ' + content + ', scale = ' + scale + ', color = (' + d + ',' + e + ',' + f + ') , thickness = 1)\n'; // #在红色画布上写下hello world
  1314. } else if (color.charAt(0) != '#') {
  1315. var d = 0,
  1316. e = 0,
  1317. f = 0;
  1318. try {
  1319. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1320. e = parseInt(color.substring(3, 5), 16),
  1321. f = parseInt(color.substring(5, 7), 16))
  1322. } catch (g) { }
  1323. var bool = "False";
  1324. // var bool = block.getFieldValue('bool') == "TRUE" ? "True" : "False";
  1325. // var bool = Blockly.Python.valueToCode(block, 'POSC', Blockly.Python.ORDER_ATOMIC) == "TRUE" ? "True" : "False";
  1326. var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
  1327. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  1328. var scale = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
  1329. _code = 'canvas.draw_string(' + xy + ', ' + content + ', scale = ' + scale + ', color = ' + color + ', thickness = 1)\n' + // #在红色画布上写下hello world
  1330. '';
  1331. }
  1332. return _code;
  1333. }
  1334. Blockly.Blocks['ai_lcd_characterscarvas'] = {
  1335. init: function () {
  1336. this.appendDummyInput()
  1337. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_hanzi_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  1338. this.appendDummyInput()
  1339. .appendField(Blockly.Msg.image_process_text_on_canvas)
  1340. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1341. .appendField(Blockly.Msg.image_process_text_on_draw + Blockly.Msg.image_process_draw_characters);
  1342. this.appendValueInput("POSA")
  1343. .setCheck("String")
  1344. .appendField(Blockly.Msg.image_process_text_start_coord);
  1345. this.appendValueInput("CONTENT")
  1346. .setCheck("String")
  1347. .appendField(Blockly.Msg.image_process_text_content);
  1348. this.appendValueInput("COLOR")
  1349. .setCheck("String")
  1350. .appendField(Blockly.Msg.image_process_text_color);
  1351. this.appendDummyInput()
  1352. .appendField(Blockly.Msg.image_process_draw_text_font_size + ":")
  1353. // .setCheck(null)
  1354. .appendField(new Blockly.FieldDropdown([
  1355. [Blockly.Msg.SMALL, "1"],
  1356. [Blockly.Msg.MIDDLE, "2"],
  1357. [Blockly.Msg.LARGE, "3"]
  1358. ]), "fonts");
  1359. // this.appendDummyInput()
  1360. // .appendField(Blockly.Msg.Set_Auto_Wrap + ":")
  1361. // // .setCheck(null)
  1362. // .appendField(new Blockly.FieldDropdown([
  1363. // [Blockly.Msg.LED_SCROLL_YES, "True"],
  1364. // [Blockly.Msg.LED_SCROLL_NO, "False"]
  1365. // ]), "wraps");
  1366. this.setInputsInline(false);
  1367. this.setPreviousStatement(true);
  1368. this.setNextStatement(true);
  1369. this.setColour("#5bb2d6");
  1370. this.setTooltip(Blockly.Msg.ai_lcd_characterscarvas_TOOLTIP);
  1371. this.setHelpUrl('');
  1372. }
  1373. };
  1374. Blockly.Python.ai_lcd_characterscarvas = function (block) {
  1375. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  1376. var _code = ''
  1377. if (color.charAt(0) == '#') {
  1378. var d = 0,
  1379. e = 0,
  1380. f = 0;
  1381. try {
  1382. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1383. e = parseInt(color.substring(3, 5), 16),
  1384. f = parseInt(color.substring(5, 7), 16))
  1385. } catch (g) { }
  1386. // var bool = Blockly.Python.valueToCode(block, 'POSC', Blockly.Python.ORDER_ATOMIC) == "TRUE" ? "True" : "False";
  1387. var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
  1388. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  1389. var size = block.getFieldValue('fonts');;
  1390. // var wraps = block.getFieldValue('wraps');
  1391. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1392. _code = "lcd_draw_string(" + variable_name + ", " + xy + ", " + content + ", color=(" + d + "," + e + "," + f + ")" + ", font_size=" + size + ")\n";
  1393. } else if (color.charAt(0) != '#') {
  1394. var d = 0,
  1395. e = 0,
  1396. f = 0;
  1397. try {
  1398. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1399. e = parseInt(color.substring(3, 5), 16),
  1400. f = parseInt(color.substring(5, 7), 16))
  1401. } catch (g) { }
  1402. // var bool = Blockly.Python.valueToCode(block, 'POSC', Blockly.Python.ORDER_ATOMIC) == "TRUE" ? "True" : "False";
  1403. var content = Blockly.Python.valueToCode(block, 'CONTENT', Blockly.Python.ORDER_ATOMIC);
  1404. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  1405. var size = block.getFieldValue('fonts');;
  1406. // var wraps = block.getFieldValue('wraps');
  1407. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1408. _code = "lcd_draw_string(" + variable_name + ", " + xy + ", " + content + ", color=" + color + ", font_size=" + size + ")\n";
  1409. }
  1410. return _code;
  1411. }
  1412. Blockly.Blocks['ai_lcd_linecarvas'] = {
  1413. init: function () {
  1414. this.appendDummyInput()
  1415. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_line.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  1416. this.appendDummyInput()
  1417. .appendField(Blockly.Msg.image_process_text_on_canvas + Blockly.Msg.image_process_text_on_draw + Blockly.Msg.image_process_draw_line)
  1418. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1419. // .appendField();
  1420. this.appendValueInput("POSA")
  1421. .setCheck("String")
  1422. .appendField(Blockly.Msg.image_process_text_start_coord);
  1423. this.appendValueInput("POSB")
  1424. .setCheck("String")
  1425. .appendField(Blockly.Msg.image_process_text_end_coord);
  1426. this.appendValueInput("COLOR")
  1427. .setCheck("String")
  1428. .appendField(Blockly.Msg.image_process_text_color);
  1429. this.appendValueInput("Scale")
  1430. .setCheck("Number")
  1431. .appendField(Blockly.Msg.image_process_text_thick);
  1432. this.setInputsInline(false);
  1433. this.setPreviousStatement(true);
  1434. this.setNextStatement(true);
  1435. this.setColour("#5bb2d6");
  1436. this.setTooltip(Blockly.Msg.ai_lcd_linecarvas_TOOLTIP);
  1437. this.setHelpUrl('');
  1438. }
  1439. };
  1440. Blockly.Python.ai_lcd_linecarvas = function (block) {
  1441. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  1442. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1443. Blockly.Python.definitions_['v831_import_display'] = `from maix import display`;
  1444. Blockly.Python.definitions_['v831_import_image'] = `from maix import image`;
  1445. Blockly.Python.definitions_['v831_import_camera'] = `from maix import camera`;
  1446. Blockly.Python.definitions_['v831_import_os'] = `import os`
  1447. Blockly.Python.definitions_['v831_camera_set'] = `cameraSize = True
  1448. def CAMERATYPE():
  1449. global cameraSize
  1450. try:
  1451. if os.path.exists("/etc/cameraSize.cfg"):
  1452. cameraSize = True
  1453. else:
  1454. cameraSize = False
  1455. except:
  1456. cameraSize = False
  1457. CAMERATYPE()
  1458. `
  1459. Blockly.Python.definitions_['import_image'] = `image.load_freetype("/root/preset/fonts/simhei.ttf")
  1460. if cameraSize==True:
  1461. camera.camera.config(size=(320,240))
  1462. else:
  1463. camera.camera.config(size=(240,320))
  1464. try:
  1465. if ScreenOrientation==True:
  1466. canvas = image.new(size = (240, 320))
  1467. else:
  1468. canvas = image.new(size = (320, 240))
  1469. except:
  1470. canvas = image.new(size = (320, 240))
  1471. ScreenOrientation = False
  1472. `
  1473. if (color.charAt(0) == '#') {
  1474. var d = 0,
  1475. e = 0,
  1476. f = 0;
  1477. try {
  1478. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1479. e = parseInt(color.substring(3, 5), 16),
  1480. f = parseInt(color.substring(5, 7), 16))
  1481. } catch (g) { }
  1482. var size = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
  1483. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  1484. var xy2 = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
  1485. var _code = "canvas.draw_line(" + xy + ", " + xy2 + ", color=(" + d + "," + e + "," + f + "), thickness=" + size + ")\n";
  1486. } else if (color.charAt(0) != '#') {
  1487. var d = 0,
  1488. e = 0,
  1489. f = 0;
  1490. try {
  1491. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1492. e = parseInt(color.substring(3, 5), 16),
  1493. f = parseInt(color.substring(5, 7), 16))
  1494. } catch (g) { }
  1495. var size = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
  1496. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  1497. var xy2 = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
  1498. var _code = "canvas.draw_line(" + xy + ", " + xy2 + ", color=" + color + ", thickness=" + size + ")\n";
  1499. }
  1500. return _code;
  1501. }
  1502. Blockly.Blocks['ai_lcd_draw_rectangle'] = {
  1503. init: function () {
  1504. // this.appendDummyInput()
  1505. // .appendField(
  1506. // new Blockly.FieldImage("./../blockly/media/line.png", 25, 25, "15"));
  1507. this.appendDummyInput()
  1508. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_rectangle.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  1509. this.appendDummyInput()
  1510. .appendField(Blockly.Msg.image_process_text_on_canvas + Blockly.Msg.image_process_text_on_draw)
  1511. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1512. // .appendField()
  1513. .appendField(new Blockly.FieldDropdown([
  1514. [Blockly.Msg.image_process_text_filled, "fillrect"],
  1515. [Blockly.Msg.image_process_text_stroked, "rect"]
  1516. ]), "rect_type")
  1517. .appendField(Blockly.Msg.image_process_draw_rectangle_text);
  1518. this.appendValueInput("POSA")
  1519. .setCheck("String")
  1520. .appendField(Blockly.Msg.image_process_text_start_coord);
  1521. this.appendValueInput("POSB")
  1522. .setCheck("String")
  1523. .appendField(Blockly.Msg.image_process_text_size);
  1524. this.appendValueInput("COLOR")
  1525. .setCheck("String")
  1526. .appendField(Blockly.Msg.image_process_text_color);
  1527. // this.appendValueInput("Scale")
  1528. // .setCheck("Number")
  1529. // .appendField(Blockly.Msg.image_process_text_thick);
  1530. this.setInputsInline(false);
  1531. this.setPreviousStatement(true);
  1532. this.setNextStatement(true);
  1533. this.setColour("#5bb2d6");
  1534. var thisBlock = this;
  1535. this.setTooltip(function () {
  1536. var mode = thisBlock.getFieldValue('rect_type');
  1537. var TOOLTIPS = {
  1538. 'fillrect': Blockly.Msg.ai_lcd_ractanglecarvas_TOOLTIP.replace('%1', Blockly.Msg.image_process_text_filled),
  1539. 'rect': Blockly.Msg.ai_lcd_ractanglecarvas_TOOLTIP.replace('%1', Blockly.Msg.image_process_text_stroked)
  1540. };
  1541. return TOOLTIPS[mode];
  1542. });
  1543. this.setHelpUrl('');
  1544. }
  1545. };
  1546. Blockly.Python.ai_lcd_draw_rectangle = function (block) {
  1547. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  1548. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1549. var type = block.getFieldValue('rect_type') == "fillrect" ? "-1" : "1";
  1550. var th = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
  1551. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  1552. var size = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
  1553. Blockly.Python.definitions_['v831_import_display'] = `from maix import display`;
  1554. Blockly.Python.definitions_['v831_import_image'] = `from maix import image`;
  1555. Blockly.Python.definitions_['v831_import_camera'] = `from maix import camera`;
  1556. Blockly.Python.definitions_['v831_import_os'] = `import os`
  1557. Blockly.Python.definitions_['v831_camera_set'] = `cameraSize = True
  1558. def CAMERATYPE():
  1559. global cameraSize
  1560. try:
  1561. if os.path.exists("/etc/cameraSize.cfg"):
  1562. cameraSize = True
  1563. else:
  1564. cameraSize = False
  1565. except:
  1566. cameraSize = False
  1567. CAMERATYPE()
  1568. `
  1569. Blockly.Python.definitions_['import_image'] = `image.load_freetype("/root/preset/fonts/simhei.ttf")
  1570. if cameraSize==True:
  1571. camera.camera.config(size=(320,240))
  1572. else:
  1573. camera.camera.config(size=(240,320))
  1574. try:
  1575. if ScreenOrientation==True:
  1576. canvas = image.new(size = (240, 320))
  1577. else:
  1578. canvas = image.new(size = (320, 240))
  1579. except:
  1580. canvas = image.new(size = (320, 240))
  1581. ScreenOrientation = False
  1582. `
  1583. // console.log(xy.split(','),size.split(','))
  1584. let a = xy.split(',')
  1585. let b = size.split(',')
  1586. // let w = Number(a[0])+Number(b[0])
  1587. // let h = Number(a[1])+Number(b[1])
  1588. // let wh = `${w},${h}`
  1589. // let ai_lcd_fill_screen_with_rgb = text.includes('ai_lcd_fill_screen_with_rgb')
  1590. var _code = ''
  1591. if (color.charAt(0) == '#') {
  1592. var d = 0,
  1593. e = 0,
  1594. f = 0;
  1595. try {
  1596. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1597. e = parseInt(color.substring(3, 5), 16),
  1598. f = parseInt(color.substring(5, 7), 16))
  1599. } catch (g) { }
  1600. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1601. // var type = block.getFieldValue('rect_type') == "fillrect" ? "-1" : "1";
  1602. // var th = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
  1603. // var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  1604. // var size = Blockly.Python.valueToCode(block, 'POSB', Blockly.Python.ORDER_ATOMIC);
  1605. _code = `canvas.draw_rectangle(${xy}, ${a[0]}+${b[0]},${a[1]}+${b[1]}, color=(${d},${e},${f}), thickness=${type})
  1606. `
  1607. } else if (color.charAt(0) != '#') {
  1608. _code = `canvas.draw_rectangle(${xy}, ${a[0]}+${b[0]},${a[1]}+${b[1]}, color=${color}, thickness=${type})
  1609. `
  1610. }
  1611. return _code;
  1612. }
  1613. Blockly.Blocks['ai_lcd_drawcirclecarvas'] = {
  1614. init: function () {
  1615. this.appendDummyInput()
  1616. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_circle.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  1617. this.appendDummyInput()
  1618. .appendField(Blockly.Msg.image_process_text_on_canvas + Blockly.Msg.image_process_text_on_draw)
  1619. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1620. // .appendField()
  1621. .appendField(new Blockly.FieldDropdown([
  1622. [Blockly.Msg.image_process_text_filled, "fillrect"],
  1623. [Blockly.Msg.image_process_text_stroked, "rect"]
  1624. ]), "rect_type")
  1625. .appendField(Blockly.Msg.image_process_draw_circle_text);
  1626. this.appendValueInput("POSA")
  1627. .setCheck("String")
  1628. .appendField(Blockly.Msg.image_process_draw_circle_text_start);
  1629. this.appendValueInput("Scale")
  1630. .setCheck("Number")
  1631. .appendField(Blockly.Msg.image_process_text_radius);
  1632. this.appendValueInput("COLOR")
  1633. .setCheck("String")
  1634. .appendField(Blockly.Msg.image_process_text_color);
  1635. // this.appendValueInput("Scale_0")
  1636. // .setCheck("Number")
  1637. // .appendField(Blockly.Msg.image_process_text_thick);
  1638. this.setInputsInline(false);
  1639. this.setPreviousStatement(true);
  1640. this.setNextStatement(true);
  1641. this.setColour("#5bb2d6");
  1642. var thisBlock = this;
  1643. this.setTooltip(function () {
  1644. var mode = thisBlock.getFieldValue('rect_type');
  1645. var TOOLTIPS = {
  1646. 'fillrect': Blockly.Msg.ai_lcd_drawcirclecarvas_TOOLTIP.replace('%1', Blockly.Msg.image_process_text_filled),
  1647. 'rect': Blockly.Msg.ai_lcd_drawcirclecarvas_TOOLTIP.replace('%1', Blockly.Msg.image_process_text_stroked)
  1648. };
  1649. return TOOLTIPS[mode];
  1650. });
  1651. this.setHelpUrl('');
  1652. }
  1653. };
  1654. Blockly.Python.ai_lcd_drawcirclecarvas = function (block) {
  1655. var color = Blockly.Python.valueToCode(block, 'COLOR', Blockly.Python.ORDER_ATOMIC);
  1656. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1657. Blockly.Python.definitions_['v831_import_display'] = `from maix import display`;
  1658. Blockly.Python.definitions_['v831_import_image'] = `from maix import image`;
  1659. Blockly.Python.definitions_['v831_import_camera'] = `from maix import camera`;
  1660. Blockly.Python.definitions_['v831_import_os'] = `import os`
  1661. Blockly.Python.definitions_['v831_camera_set'] = `cameraSize = True
  1662. def CAMERATYPE():
  1663. global cameraSize
  1664. try:
  1665. if os.path.exists("/etc/cameraSize.cfg"):
  1666. cameraSize = True
  1667. else:
  1668. cameraSize = False
  1669. except:
  1670. cameraSize = False
  1671. CAMERATYPE()
  1672. `
  1673. Blockly.Python.definitions_['import_image'] = `image.load_freetype("/root/preset/fonts/simhei.ttf")
  1674. if cameraSize==True:
  1675. camera.camera.config(size=(320,240))
  1676. else:
  1677. camera.camera.config(size=(240,320))
  1678. try:
  1679. if ScreenOrientation==True:
  1680. canvas = image.new(size = (240, 320))
  1681. else:
  1682. canvas = image.new(size = (320, 240))
  1683. except:
  1684. canvas = image.new(size = (320, 240))
  1685. ScreenOrientation = False
  1686. `
  1687. if (color.charAt(0) == '#') {
  1688. var d = 0,
  1689. e = 0,
  1690. f = 0;
  1691. try {
  1692. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1693. e = parseInt(color.substring(3, 5), 16),
  1694. f = parseInt(color.substring(5, 7), 16))
  1695. } catch (g) { }
  1696. var type = block.getFieldValue('rect_type') == "fillrect" ? "-1" : "1";
  1697. var th = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
  1698. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  1699. var th0 = Blockly.Python.valueToCode(block, 'Scale_0', Blockly.Python.ORDER_ATOMIC);
  1700. var _code = "canvas.draw_circle(" + xy + ", " + th + ", color=(" + d + ", " + e + ", " + f + "), thickness=" + type + ")\n";
  1701. } else if (color.charAt(0) != '#') {
  1702. var d = 0,
  1703. e = 0,
  1704. f = 0;
  1705. try {
  1706. 7 == color.length && (d = parseInt(color.substring(1, 3), 16),
  1707. e = parseInt(color.substring(3, 5), 16),
  1708. f = parseInt(color.substring(5, 7), 16))
  1709. } catch (g) { }
  1710. var type = block.getFieldValue('rect_type') == "fillrect" ? "-1" : "1";
  1711. var th = Blockly.Python.valueToCode(block, 'Scale', Blockly.Python.ORDER_ATOMIC);
  1712. var xy = Blockly.Python.valueToCode(block, 'POSA', Blockly.Python.ORDER_ATOMIC);
  1713. var th0 = Blockly.Python.valueToCode(block, 'Scale_0', Blockly.Python.ORDER_ATOMIC);
  1714. var _code = "canvas.draw_circle(" + xy + ", " + th + ", color=" + color + ", thickness=" + type + ")\n";
  1715. }
  1716. return _code;
  1717. }
  1718. Blockly.Blocks['ai_lcd_draw_image_on_canvas'] = {
  1719. init: function () {
  1720. this.appendDummyInput()
  1721. .appendField(new Blockly.FieldImage("blockly/media/lcd_draw_image.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  1722. this.appendDummyInput()
  1723. .appendField(Blockly.Msg.image_process_text_on_canvas + Blockly.Msg.image_process_draw_image_title);
  1724. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1725. // .appendField();
  1726. this.appendValueInput("image_path")
  1727. .setCheck(null)
  1728. .appendField(Blockly.Msg.image_process_draw_sensor_image_path);
  1729. this.appendValueInput("scale_y")
  1730. .setCheck(null)
  1731. .appendField(Blockly.Msg.OLCD_COORDINATE);
  1732. this.appendValueInput("alpha")
  1733. .setCheck(null)
  1734. .appendField(Blockly.Msg.image_process_draw_image_alpha);
  1735. this.setInputsInline(false);
  1736. this.setPreviousStatement(true, null);
  1737. this.setNextStatement(true, null);
  1738. this.setColour("#5bb2d6");
  1739. this.setTooltip(Blockly.Msg.ai_lcd_draw_image_on_canvas_TOOLTIP);
  1740. this.setHelpUrl("");
  1741. }
  1742. };
  1743. Blockly.Python['ai_lcd_draw_image_on_canvas'] = function (block) {
  1744. var value_image_path = Blockly.Python.valueToCode(block, 'image_path', Blockly.Python.ORDER_ATOMIC);
  1745. var value_scale_y = Blockly.Python.valueToCode(block, 'scale_y', Blockly.Python.ORDER_ATOMIC);
  1746. var alpha = Blockly.Python.valueToCode(block, 'alpha', Blockly.Python.ORDER_ATOMIC);
  1747. Blockly.Python.definitions_['v831_import_display'] = `from maix import display`;
  1748. Blockly.Python.definitions_['v831_import_image'] = `from maix import image`;
  1749. Blockly.Python.definitions_['v831_import_camera'] = `from maix import camera`;
  1750. Blockly.Python.definitions_['v831_import_os'] = `import os`
  1751. Blockly.Python.definitions_['v831_camera_set'] = `cameraSize = True
  1752. def CAMERATYPE():
  1753. global cameraSize
  1754. try:
  1755. if os.path.exists("/etc/cameraSize.cfg"):
  1756. cameraSize = True
  1757. else:
  1758. cameraSize = False
  1759. except:
  1760. cameraSize = False
  1761. CAMERATYPE()
  1762. `
  1763. Blockly.Python.definitions_['import_image'] = `image.load_freetype("/root/preset/fonts/simhei.ttf")
  1764. if cameraSize==True:
  1765. camera.camera.config(size=(320,240))
  1766. else:
  1767. camera.camera.config(size=(240,320))
  1768. try:
  1769. if ScreenOrientation==True:
  1770. canvas = image.new(size = (240, 320))
  1771. else:
  1772. canvas = image.new(size = (320, 240))
  1773. except:
  1774. canvas = image.new(size = (320, 240))
  1775. ScreenOrientation = False
  1776. `
  1777. // let alpha = Blockly.Python.valueToCode(block, 'alpha', Blockly.Python.ORDER_ATOMIC)
  1778. // TODO: Assemble Python into code variable.
  1779. var code = 'canvas.draw_image(' + value_image_path + ',' + value_scale_y + ',alpha=' + alpha + ')\n';
  1780. return code;
  1781. };
  1782. Blockly.Blocks['v831_img_save'] = {
  1783. init: function () {
  1784. this.appendValueInput("save_path")
  1785. .setCheck(null)
  1786. .appendField(Blockly.Msg.image_process_text_let_canvas)
  1787. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1788. .appendField(Blockly.Msg.image_process_img_save)
  1789. // .appendField(new Blockly.FieldDropdown([
  1790. // [Blockly.Msg.display_draw_vertical, "0", "img"],
  1791. // [Blockly.Msg.display_draw_horizontal, "1", "img1"]
  1792. // ]), "invert");
  1793. this.setInputsInline(false);
  1794. this.setPreviousStatement(true, null);
  1795. this.setNextStatement(true, null);
  1796. this.setColour("#f0983e");
  1797. this.setTooltip(Blockly.Msg.ai_img_save_TOOLTIP);
  1798. this.setHelpUrl("");
  1799. }
  1800. };
  1801. Blockly.Python['v831_img_save'] = function (block) {
  1802. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1803. var save_path_location = Blockly.Python.valueToCode(block, 'save_path', Blockly.Python.ORDER_ATOMIC);
  1804. // let invert = block.getFieldValue('invert');
  1805. // console.log(document.getElementById("img").value, invert)
  1806. var code = 'canvas.save("/root/user/img/' + save_path_location.slice(1, -1) + '.jpg")\n';
  1807. if (save_path_location[0] != '"') {
  1808. code = 'canvas.save(' + save_path_location + ')\n';
  1809. }
  1810. return code;
  1811. };
  1812. Blockly.Blocks.v831_img_text = {
  1813. init: function () {
  1814. this.appendValueInput("save_path")
  1815. .setCheck(null)
  1816. .appendField(Blockly.Msg.read_img_path_to)
  1817. this.setOutput(true, null);
  1818. this.setColour("#f0983e");
  1819. this.setTooltip(Blockly.Msg.read_img_path_to);
  1820. this.setHelpUrl("");
  1821. }
  1822. };
  1823. Blockly.Python.v831_img_text = function (block) {
  1824. var save_path_location = Blockly.Python.valueToCode(block, 'save_path', Blockly.Python.ORDER_ATOMIC);
  1825. var code = 'image.open(' + save_path_location + ')';
  1826. return [code, Blockly.Python.ORDER_NONE];
  1827. }
  1828. Blockly.Blocks['v831_img_open'] = {
  1829. init: function () {
  1830. this.appendValueInput("save_path")
  1831. .setCheck(null)
  1832. .appendField(Blockly.Msg.read_img_path_to)
  1833. // .appendField(Blockly.Msg.image_process_img_save);
  1834. // this.appendDummyInput()
  1835. // .appendField(Blockly.Msg.image_process_img_save_quality)
  1836. // .appendField(new Blockly.FieldNumber(90, 0, 100), "compression")
  1837. // .appendField("%");
  1838. // this.setInputsInline(false);
  1839. this.setNextStatement(true, null);
  1840. this.setPreviousStatement(true, null);
  1841. this.setColour("#f0983e");
  1842. this.setTooltip(Blockly.Msg.read_img_path_to);
  1843. this.setHelpUrl("");
  1844. }
  1845. };
  1846. Blockly.Python['v831_img_open'] = function (block) {
  1847. var save_path_location = Blockly.Python.valueToCode(block, 'save_path', Blockly.Python.ORDER_ATOMIC);
  1848. var code = 'canvas = image.open(' + save_path_location + ')\n';
  1849. return code;
  1850. };
  1851. // 翻转
  1852. Blockly.Blocks['v831_lcd_invert'] = {
  1853. init: function () {
  1854. this.appendDummyInput()
  1855. .appendField(Blockly.Msg.image_process_text_let_canvas + Blockly.Msg.image_process_text_image_canvas)
  1856. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1857. // .appendField()
  1858. .appendField(Blockly.Msg.image_process_pixtovec_text_first)
  1859. .appendField(new Blockly.FieldDropdown([
  1860. [Blockly.Msg.display_draw_vertical, "0"],
  1861. [Blockly.Msg.display_draw_horizontal, "1"]
  1862. ]), "invert")
  1863. .appendField(Blockly.Msg.MAIXDUINO_DISPLAY_ORIENTATION_ROTATE);
  1864. this.setNextStatement(true, null);
  1865. this.setPreviousStatement(true, null);
  1866. this.setColour("#f0983e");
  1867. // this.setTooltip(Blockly.Msg.ai_lcd_invert_color_TOOLTIP);
  1868. this.setHelpUrl("");
  1869. }
  1870. };
  1871. Blockly.Python['v831_lcd_invert'] = function (block) {
  1872. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1873. let invert = block.getFieldValue('invert');
  1874. // TODO: Assemble Python into code variable.
  1875. var code = `canvas = canvas.flip(${invert})\n`
  1876. // TODO: Change ORDER_NONE to the correct strength.
  1877. return code;
  1878. };
  1879. // 旋转
  1880. Blockly.Blocks['v831_lcd_rotating'] = {
  1881. init: function () {
  1882. this.appendValueInput('spend')
  1883. .setCheck(null)
  1884. .appendField(Blockly.Msg.image_process_text_let_canvas + Blockly.Msg.image_process_text_image_canvas)
  1885. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1886. // .appendField()
  1887. .appendField(Blockly.Msg.image_process_pixtovec_text_first)
  1888. .appendField(new Blockly.FieldDropdown([
  1889. [Blockly.Msg.x_motor_cw_ai, "0"],
  1890. [Blockly.Msg.x_motor_acw_ai, "1"]
  1891. ]), "rotating")
  1892. .appendField(Blockly.Msg.x_motor_set_motor_turns_ai);
  1893. this.appendDummyInput().appendField(Blockly.Msg.robot_dog_ag);
  1894. this.setNextStatement(true, null);
  1895. this.setPreviousStatement(true, null);
  1896. this.setColour("#f0983e");
  1897. this.setTooltip(Blockly.Msg.ai_lcd_invert_color_TOOLTIP);
  1898. this.setHelpUrl("");
  1899. }
  1900. };
  1901. Blockly.Python['v831_lcd_rotating'] = function (block) {
  1902. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1903. let rotating = block.getFieldValue('rotating');
  1904. let spend = (rotating != 0 ? '+' : '-') + Blockly.Python.valueToCode(block, 'spend', Blockly.Python.ORDER_ATOMIC);
  1905. // TODO: Assemble Python into code variable.
  1906. // Blockly.Python.definitions_['lcd_invert_init'] = `def lcdRotation(inputImg,rotationAngle):
  1907. // from maix import image
  1908. // imgRotationAim = image.new(size = (320, 240),color=(0,0,0))
  1909. // imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
  1910. // return imgRotationAim.draw_image(imageRotationBuffer.rotate(rotationAngle, adjust=1),0,0,alpha=1)
  1911. // `
  1912. // console.log(rotating,spend)
  1913. var code = `canvas = canvas.rotate(${spend}, adjust=1)\n`
  1914. // variable_name+'.rotate('+ spend +', adjust=1)';
  1915. // TODO: Change ORDER_NONE to the correct strength.
  1916. return code;
  1917. };
  1918. Blockly.Blocks['ai_lcd_invert_color'] = {
  1919. init: function () {
  1920. this.appendDummyInput()
  1921. .appendField(Blockly.Msg.image_process_text_let_canvas + Blockly.Msg.image_process_color_invert)
  1922. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1923. // .appendField();
  1924. this.setOutput(true, null);
  1925. this.setColour("#f0983e");
  1926. this.setTooltip(Blockly.Msg.ai_lcd_invert_color_TOOLTIP);
  1927. this.setHelpUrl("");
  1928. }
  1929. };
  1930. Blockly.Python['ai_lcd_invert_color'] = function (block) {
  1931. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1932. // TODO: Assemble Python into code variable.
  1933. var code = 'canvas.invert()';
  1934. // TODO: Change ORDER_NONE to the correct strength.
  1935. return [code, Blockly.Python.ORDER_NONE];
  1936. };
  1937. Blockly.Blocks['v831_lcd_resizecanvas'] = {
  1938. init: function () {
  1939. this.appendDummyInput()
  1940. .appendField(Blockly.Msg.image_process_text_let_canvas + Blockly.Msg.image_process_text_image_canvas)
  1941. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1942. // .appendField()
  1943. .appendField(Blockly.Msg.image_process_resize_to);
  1944. this.appendValueInput("wh")
  1945. .setCheck(null);
  1946. this.setInputsInline(true);
  1947. this.setNextStatement(true, null);
  1948. this.setPreviousStatement(true, null);
  1949. this.setColour("#f0983e");
  1950. this.setTooltip(Blockly.Msg.ai_lcd_resizecanvas_TOOLTIP);
  1951. this.setHelpUrl("");
  1952. }
  1953. };
  1954. Blockly.Python['v831_lcd_resizecanvas'] = function (block) {
  1955. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1956. var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
  1957. // TODO: Assemble Python into code variable.
  1958. // Blockly.Python.definitions_['lcd_invert_init'] = `CANVASINVERT = image.new(size = (240, 320), color = (0,0,0), mode = 'RGB')
  1959. // `
  1960. // var code = `CANVASINVERT.draw_image(${variable_name}.crop(0, 0, 240, 320).resize(${wh}),0,0,alpha=1)`
  1961. // variable_name + '.resize(' + wh + ')';
  1962. // TODO: Change ORDER_NONE to the correct strength.
  1963. var code = `canvas = canvas.resize(${wh}, padding = 0)\n`
  1964. return code;
  1965. };
  1966. Blockly.Blocks['ai_lcd_color_scheme'] = {
  1967. init: function () {
  1968. this.appendDummyInput()
  1969. .appendField(Blockly.Msg.image_process_text_let_canvas)
  1970. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  1971. .appendField(Blockly.Msg.image_process_color_scheme_convert)
  1972. .appendField(new Blockly.FieldDropdown([
  1973. [Blockly.Msg.image_process_color_scheme_bw, "to_grayscale(1)"],
  1974. [Blockly.Msg.image_process_color_scheme_rgb, "to_rgb565(1)"]
  1975. ]), "color_scheme");
  1976. this.setOutput(true, null);
  1977. this.setColour("#f0983e");
  1978. var thisBlock = this;
  1979. this.setTooltip(function () {
  1980. var mode = thisBlock.getFieldValue('color_scheme');
  1981. var TOOLTIPS = {
  1982. 'to_grayscale(1)': Blockly.Msg.ai_lcd_color_scheme_TOOLTIP.replace('%1', Blockly.Msg.image_process_color_scheme_bw),
  1983. 'to_rgb565(1)': Blockly.Msg.ai_lcd_color_scheme_TOOLTIP.replace('%1', Blockly.Msg.image_process_color_scheme_rgb)
  1984. };
  1985. return TOOLTIPS[mode];
  1986. });
  1987. this.setHelpUrl("");
  1988. }
  1989. };
  1990. Blockly.Python['ai_lcd_color_scheme'] = function (block) {
  1991. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  1992. var dropdown_vari = block.getFieldValue('color_scheme');
  1993. // TODO: Assemble Python into code variable.
  1994. var code = variable_name + '.' + dropdown_vari;
  1995. // TODO: Change ORDER_NONE to the correct strength.
  1996. return [code, Blockly.Python.ORDER_NONE];
  1997. };
  1998. Blockly.Blocks['ai_lcd_ai_pix'] = {
  1999. init: function () {
  2000. this.appendDummyInput()
  2001. .appendField(Blockly.Msg.image_process_text_let_canvas)
  2002. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  2003. .appendField(Blockly.Msg.image_process_pixtovec_text_first)
  2004. .appendField(new Blockly.FieldDropdown([
  2005. [Blockly.Msg.image_process_pixtovec_v2p, ".ai_to_pix()"],
  2006. [Blockly.Msg.image_process_pixtovec_p2v, ".pix_to_ai()"]
  2007. ]), "ai_pix_type")
  2008. .appendField(Blockly.Msg.image_process_pixtovec_conversion);
  2009. this.setInputsInline(false);
  2010. this.setPreviousStatement(true, null);
  2011. this.setNextStatement(true, null);
  2012. this.setColour("#f0983e");
  2013. this.setTooltip("");
  2014. this.setHelpUrl("");
  2015. }
  2016. };
  2017. Blockly.Python['ai_lcd_ai_pix'] = function (block) {
  2018. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2019. var dropdown_vari = block.getFieldValue('ai_pix_type');
  2020. // TODO: Assemble Python into code variable.
  2021. var code = 'a = ' + variable_name + dropdown_vari + '\n';
  2022. // TODO: Change ORDER_NONE to the correct strength.
  2023. return code;
  2024. };
  2025. Blockly.Blocks['v831_lcd_img_cut'] = {
  2026. init: function () {
  2027. this.appendDummyInput()
  2028. .appendField(Blockly.Msg.image_process_text_let_canvas + Blockly.Msg.image_process_text_image_canvas + Blockly.Msg.image_process_imgcut_cutting)
  2029. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  2030. // .appendField()
  2031. // .appendField();
  2032. this.appendValueInput("xy")
  2033. .setCheck(null)
  2034. .appendField(Blockly.Msg.image_process_imgcut_start_coord);
  2035. this.appendValueInput("wh")
  2036. .setCheck(null)
  2037. .appendField(Blockly.Msg.image_process_imgcut_size);
  2038. this.setInputsInline(false);
  2039. this.setNextStatement(true, null);
  2040. this.setPreviousStatement(true, null)
  2041. this.setColour("#f0983e");
  2042. this.setTooltip(Blockly.Msg.ai_lcd_img_cut_TOOLTIP);
  2043. this.setHelpUrl("");
  2044. }
  2045. };
  2046. Blockly.Python['v831_lcd_img_cut'] = function (block) {
  2047. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2048. var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  2049. var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
  2050. // TODO: Assemble Python into code variable.
  2051. // TODO: Change ORDER_NONE to the correct strength.
  2052. var code = `canvas = canvas.crop(${xy},${wh})\n`;
  2053. return code;
  2054. };
  2055. Blockly.Blocks['ai_vision_get_statistics'] = {
  2056. init: function () {
  2057. this.appendDummyInput()
  2058. .appendField(Blockly.Msg.image_process_get_statistics_to_canvas)
  2059. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  2060. .appendField(Blockly.Msg.image_process_get_statistics_get_analysis);
  2061. this.appendValueInput("xy")
  2062. .setCheck(null)
  2063. .appendField(Blockly.Msg.image_process_get_statistics_start_coord);
  2064. this.appendValueInput("wh")
  2065. .setCheck(null)
  2066. .appendField(Blockly.Msg.image_process_get_statistics_size);
  2067. this.appendValueInput("bins")
  2068. .setCheck(null)
  2069. .appendField(Blockly.Msg.image_process_get_statistics_boxes);
  2070. this.setInputsInline(false);
  2071. this.setOutput(true, null);
  2072. this.setColour("#f0983e");
  2073. this.setTooltip(Blockly.Msg.ai_vision_get_statistics_TOOLTIP);
  2074. this.setHelpUrl("");
  2075. }
  2076. };
  2077. Blockly.Python['ai_vision_get_statistics'] = function (block) {
  2078. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2079. var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  2080. var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
  2081. var bins = Blockly.Python.valueToCode(block, 'bins', Blockly.Python.ORDER_ATOMIC);
  2082. // TODO: Assemble Python into code variable.
  2083. var code = variable_name + '.get_statistics(bins=' + bins + ',roi=(' + xy + ',' + wh + '))';
  2084. // TODO: Change ORDER_NONE to the correct strength.
  2085. return [code, Blockly.Python.ORDER_NONE];
  2086. };
  2087. Blockly.Blocks['ai_vision_get_histogram'] = {
  2088. init: function () {
  2089. this.appendDummyInput()
  2090. .appendField(Blockly.Msg.ai_vision_get_histogram_get)
  2091. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  2092. .appendField(Blockly.Msg.ai_vision_get_histogram_compute);
  2093. this.appendDummyInput()
  2094. .appendField(Blockly.Msg.ai_vision_get_histogram_region);
  2095. this.appendValueInput("xy")
  2096. .setCheck(null)
  2097. .appendField(Blockly.Msg.ai_vision_get_histogram_region_xy);
  2098. this.appendValueInput("wh")
  2099. .setCheck(null)
  2100. .appendField(Blockly.Msg.ai_vision_get_histogram_region_wh);
  2101. this.setInputsInline(false);
  2102. this.setOutput(true, null);
  2103. this.setColour("#f0983e");
  2104. this.setTooltip(Blockly.Msg.ai_vision_get_histogram_TOOLTIP);
  2105. this.setHelpUrl("");
  2106. }
  2107. };
  2108. Blockly.Python['ai_vision_get_histogram'] = function (block) {
  2109. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2110. var value_xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  2111. var value_wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
  2112. // TODO: Assemble Python into code variable.
  2113. var code = variable_name + '.get_histogram(roi=(' + value_xy + ',' + value_wh + '))';
  2114. // TODO: Change ORDER_NONE to the correct strength.
  2115. return [code, Blockly.Python.ORDER_NONE];
  2116. };
  2117. Blockly.Blocks['ai_vision_get_histogram_percentile'] = {
  2118. init: function () {
  2119. this.appendDummyInput()
  2120. .appendField(Blockly.Msg.ai_vision_get_histogram_details_1)
  2121. .appendField(new Blockly.FieldVariable("hist"), "varitem")
  2122. .appendField(Blockly.Msg.ai_vision_get_histogram_details_get);
  2123. this.appendValueInput("perc")
  2124. .setCheck(null);
  2125. this.setInputsInline(true);
  2126. this.setOutput(true, null);
  2127. this.setColour("#f0983e");
  2128. this.setTooltip(Blockly.Msg.ai_vision_get_histogram_percentile_TOOLTIP);
  2129. this.setHelpUrl("https://docs.openmv.io/library/omv.image.html#id3");
  2130. }
  2131. };
  2132. Blockly.Python['ai_vision_get_histogram_percentile'] = function (block) {
  2133. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2134. var value_perc = Blockly.Python.valueToCode(block, 'perc', Blockly.Python.ORDER_ATOMIC);
  2135. // TODO: Assemble Python into code variable.
  2136. var code = variable_name + '.get_percentile(' + value_perc + ')';
  2137. // TODO: Change ORDER_NONE to the correct strength.
  2138. return [code, Blockly.Python.ORDER_NONE];
  2139. };
  2140. Blockly.Blocks['ai_vision_get_histogram_percentile_detail'] = {
  2141. init: function () {
  2142. this.appendDummyInput()
  2143. .appendField(Blockly.Msg.ai_vision_get_histogram_percentile_1)
  2144. .appendField(new Blockly.FieldVariable("percentile"), "varitem");
  2145. this.appendDummyInput()
  2146. .appendField(Blockly.Msg.ai_vision_get_histogram_percentile_2)
  2147. .appendField(new Blockly.FieldDropdown([
  2148. [Blockly.Msg.ai_vision_get_histogram_percentile_3, "value()"],
  2149. [Blockly.Msg.ai_vision_get_histogram_percentile_4, "l_value()"],
  2150. [Blockly.Msg.ai_vision_get_histogram_percentile_5, "a_value()"],
  2151. [Blockly.Msg.ai_vision_get_histogram_percentile_6, "b_value()"]
  2152. ]), "option");
  2153. this.setInputsInline(false);
  2154. this.setOutput(true, null);
  2155. this.setColour("#f0983e");
  2156. var thisBlock = this;
  2157. this.setTooltip(function () {
  2158. var mode = thisBlock.getFieldValue('option');
  2159. var TOOLTIPS = {
  2160. 'value()': Blockly.Msg.ai_vision_get_histogram_percentile_detail_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_get_histogram_percentile_3),
  2161. 'l_value()': Blockly.Msg.ai_vision_get_histogram_percentile_detail_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_get_histogram_percentile_4),
  2162. 'a_value()': Blockly.Msg.ai_vision_get_histogram_percentile_detail_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_get_histogram_percentile_5),
  2163. 'b_value()': Blockly.Msg.ai_vision_get_histogram_percentile_detail_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_get_histogram_percentile_6)
  2164. };
  2165. return TOOLTIPS[mode];
  2166. });
  2167. this.setHelpUrl("https://docs.openmv.io/library/omv.image.html#id3");
  2168. }
  2169. };
  2170. Blockly.Python['ai_vision_get_histogram_percentile_detail'] = function (block) {
  2171. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2172. var dropdown_option = block.getFieldValue('option');
  2173. var code = variable_name + '.' + dropdown_option;
  2174. // TODO: Change ORDER_NONE to the correct strength.
  2175. return [code, Blockly.Python.ORDER_NONE];
  2176. };
  2177. Blockly.Blocks['ai_vision_lab_rgb_conversion'] = {
  2178. init: function () {
  2179. this.appendDummyInput()
  2180. .appendField(Blockly.Msg.image_process_lab_rgb_conversion_exec)
  2181. .appendField(new Blockly.FieldDropdown([
  2182. [Blockly.Msg.image_process_lab_rgb_conversion_torgb, "lab_to_rgb"],
  2183. [Blockly.Msg.image_process_lab_rgb_conversion_tolab, "rgb_to_lab"]
  2184. ]), "rgblab");
  2185. this.appendValueInput("color_tuple")
  2186. .setCheck(null)
  2187. .appendField(Blockly.Msg.image_process_lab_rgb_conversion_tuple);
  2188. this.setOutput(true, null);
  2189. this.setColour("#f0983e");
  2190. var thisBlock = this;
  2191. this.setTooltip(function () {
  2192. var mode = thisBlock.getFieldValue('rgblab');
  2193. var TOOLTIPS = {
  2194. 'lab_to_rgb': Blockly.Msg.ai_vision_lab_rgb_conversion_TOOLTIP.replace('%1', Blockly.Msg.image_process_lab_rgb_conversion_torgb),
  2195. 'rgb_to_lab': Blockly.Msg.ai_vision_lab_rgb_conversion_TOOLTIP.replace('%1', Blockly.Msg.image_process_lab_rgb_conversion_tolab)
  2196. };
  2197. return TOOLTIPS[mode];
  2198. });
  2199. this.setHelpUrl("");
  2200. }
  2201. };
  2202. Blockly.Python['ai_vision_lab_rgb_conversion'] = function (block) {
  2203. var type = block.getFieldValue('rgblab');
  2204. var tuple = Blockly.Python.valueToCode(block, 'color_tuple', Blockly.Python.ORDER_ATOMIC);
  2205. // TODO: Assemble Python into code variable.
  2206. var code = 'image.' + type + '(' + tuple + ')';
  2207. // TODO: Change ORDER_NONE to the correct strength.
  2208. return [code, Blockly.Python.ORDER_NONE];
  2209. };
  2210. Blockly.Blocks['ai_vision_rgb_grayscale_conversion'] = {
  2211. init: function () {
  2212. this.appendDummyInput()
  2213. .appendField(Blockly.Msg.image_process_rgb_grayscale_conversion_exec)
  2214. .appendField(new Blockly.FieldDropdown([
  2215. [Blockly.Msg.image_process_rgb_grayscale_conversion_torgb, "grayscale_to_rgb"],
  2216. [Blockly.Msg.image_process_rgb_grayscale_conversion_togray, "rgb_to_grayscale"]
  2217. ]), "rgb_g");
  2218. this.appendValueInput("color_tuple")
  2219. .setCheck(null)
  2220. .appendField(Blockly.Msg.image_process_rgb_grayscale_conversion_set);
  2221. this.setOutput(true, null);
  2222. this.setColour("#f0983e");
  2223. var thisBlock = this;
  2224. this.setTooltip(function () {
  2225. var mode = thisBlock.getFieldValue('rgb_g');
  2226. var TOOLTIPS = {
  2227. 'grayscale_to_rgb': Blockly.Msg.ai_vision_rgb_grayscale_conversion_TOOLTIP.replace('%1', Blockly.Msg.image_process_rgb_grayscale_conversion_torgb),
  2228. 'rgb_to_grayscale': Blockly.Msg.ai_vision_rgb_grayscale_conversion_TOOLTIP.replace('%1', Blockly.Msg.image_process_rgb_grayscale_conversion_togray)
  2229. };
  2230. return TOOLTIPS[mode];
  2231. });
  2232. this.setHelpUrl("");
  2233. }
  2234. };
  2235. Blockly.Python['ai_vision_rgb_grayscale_conversion'] = function (block) {
  2236. var type = block.getFieldValue('rgb_g');
  2237. var tuple = Blockly.Python.valueToCode(block, 'color_tuple', Blockly.Python.ORDER_ATOMIC);
  2238. Blockly.Python.addVariable('rgb_tuple', "rgb_tuple = None", true);
  2239. // TODO: Assemble Python into code variable.
  2240. var code = 'image.' + type + '(' + tuple + ')';
  2241. // TODO: Change ORDER_NONE to the correct strength.
  2242. return [code, Blockly.Python.ORDER_NONE];
  2243. };
  2244. /*
  2245. _ ___ ____
  2246. / \ |_ _| / ___|__ _ _ __ ___ ___ _ __ __ _
  2247. / _ \ | | | | / _` | '_ ` _ \ / _ \ '__/ _` |
  2248. / ___ \ | | | |__| (_| | | | | | | __/ | | (_| |
  2249. /_/ \_\___| \____\__,_|_| |_| |_|\___|_| \__,_|
  2250. */
  2251. Blockly.Blocks.ai_camera_wh = {
  2252. init: function () {
  2253. var _input = this.appendDummyInput();
  2254. this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
  2255. this.setColour("#60c1bb");
  2256. this.setOutput(!0, "Boolean");
  2257. _input.appendField(Blockly.Msg.W);
  2258. this.appendValueInput("A").setCheck("Number");
  2259. _input = this.appendDummyInput();
  2260. _input.appendField(Blockly.Msg.H)
  2261. this.appendValueInput("B").setCheck("Number");
  2262. this.setInputsInline(!0);
  2263. }
  2264. };
  2265. Blockly.Python.ai_camera_wh = function (block) {
  2266. var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
  2267. var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
  2268. var code = "" + _x + ", " + _y + "";
  2269. return [code, Blockly.Python.ORDER_ATOMIC];
  2270. };
  2271. Blockly.Blocks['ai_camera_init'] = {
  2272. init: function () {
  2273. this.appendDummyInput()
  2274. .appendField(new Blockly.FieldImage("blockly/media/camera_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  2275. this.appendDummyInput()
  2276. .appendField(Blockly.Msg.ai_camera_camera_init);
  2277. // this.appendDummyInput()
  2278. // .appendField(Blockly.Msg.ai_camera_camera_set_size)
  2279. // .appendField(new Blockly.FieldDropdown([
  2280. // ["QVGA (320*240)", "QVGA"],
  2281. // ["QQVGA (160*120)", "QQVGA"]
  2282. // ]), "DEGREE");
  2283. // this.appendDummyInput()
  2284. // .appendField(Blockly.Msg.ai_camera_camera_color_mode)
  2285. // .appendField(new Blockly.FieldDropdown([
  2286. // [Blockly.Msg.ai_camera_camera_color_mode_rgb, "RGB565"],
  2287. // [Blockly.Msg.ai_camera_camera_color_mode_bw, "GRAYSCALE"]
  2288. // ]), "DEGREE1");
  2289. this.setPreviousStatement(true, null);
  2290. this.setNextStatement(true, null);
  2291. this.setColour("#60c1bb");
  2292. this.setTooltip(Blockly.Msg.ai_camera_init_TOOLTIP);
  2293. this.setHelpUrl('');
  2294. }
  2295. };
  2296. Blockly.Python.ai_camera_init = function (block) {
  2297. Blockly.Python.definitions_['v831_import_camera'] = `from maix import camera`
  2298. Blockly.Python.definitions_['v831_import_os'] = `import os`
  2299. Blockly.Python.definitions_['v831_camera_set'] = `cameraSize = True
  2300. def CAMERATYPE():
  2301. global cameraSize
  2302. try:
  2303. if os.path.exists("/etc/cameraSize.cfg"):
  2304. cameraSize = True
  2305. else:
  2306. cameraSize = False
  2307. except:
  2308. cameraSize = False
  2309. CAMERATYPE()
  2310. `
  2311. Blockly.Python.addVariable("SETVFLIP", "SETVFLIP = False", true)
  2312. Blockly.Python.addVariable("SETHMIRROT", "SETHMIRROT = False", true)
  2313. var _code = ``;
  2314. return _code;
  2315. }
  2316. Blockly.Blocks['ai_camera_windows'] = {
  2317. init: function () {
  2318. this.appendDummyInput()
  2319. .appendField(Blockly.Msg.ai_camera_windowing_resize);
  2320. this.appendDummyInput()
  2321. .appendField(Blockly.Msg.ai_camera_windowing_w)
  2322. .appendField(new Blockly.FieldNumber(240), "window_w")
  2323. .appendField(Blockly.Msg.ai_camera_windowing_h)
  2324. .appendField(new Blockly.FieldNumber(320), "window_h")
  2325. // .appendField(Blockly.Msg.ai_camera_windowing_unit);
  2326. this.setPreviousStatement(true, null);
  2327. this.setNextStatement(true, null);
  2328. this.setColour("#60c1bb");
  2329. this.setTooltip(Blockly.Msg.ai_camera_windows_TOOLTIP);
  2330. this.setHelpUrl("");
  2331. }
  2332. };
  2333. Blockly.Python['ai_camera_windows'] = function (block) {
  2334. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2335. var w = block.getFieldValue('window_w');
  2336. var h = block.getFieldValue('window_h');
  2337. // var code = 'sensor.set_windowing((' + w + ',' + h + '))\n';
  2338. var code = 'camera.camera.config(size=(' + w + ',' + h + '))\n';
  2339. return code;
  2340. };
  2341. Blockly.Blocks['ai_camera_fit_to_screen_size'] = {
  2342. init: function () {
  2343. this.appendDummyInput()
  2344. .appendField(Blockly.Msg.ai_camera_camera_fit_set + Blockly.Msg.ai_camera_camera_fit_to)
  2345. .appendField(new Blockly.FieldDropdown([
  2346. ["320*240", "320"],
  2347. ["224*224", "224"]
  2348. ]), "size");
  2349. this.setInputsInline(true);
  2350. this.setPreviousStatement(true, null);
  2351. this.setNextStatement(true, null);
  2352. this.setColour("#60c1bb");
  2353. this.setTooltip(Blockly.Msg.ai_camera_fit_to_screen_size_TOOLTIP);
  2354. this.setHelpUrl("");
  2355. }
  2356. };
  2357. Blockly.Python['ai_camera_fit_to_screen_size'] = function (block) {
  2358. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2359. var dropdown_size = block.getFieldValue('size');
  2360. Blockly.Python.definitions_['v831_import_camera'] = `from maix import camera`
  2361. Blockly.Python.definitions_['v831_import_os'] = `import os`
  2362. Blockly.Python.definitions_['v831_import_image'] = `from maix import image`;
  2363. Blockly.Python.definitions_['v831_camera_set'] = `cameraSize = True
  2364. def CAMERATYPE():
  2365. global cameraSize
  2366. try:
  2367. if os.path.exists("/etc/cameraSize.cfg"):
  2368. cameraSize = True
  2369. else:
  2370. cameraSize = False
  2371. except:
  2372. cameraSize = False
  2373. CAMERATYPE()
  2374. `
  2375. Blockly.Python.definitions_['lcd_invert_init'] = `def lcdRotation(inputImg):
  2376. global SETVFLIP,SETHMIRROT,cameraSize,ScreenOrientation
  2377. imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
  2378. try:
  2379. if ScreenOrientation:
  2380. imgRotationAim = image.new(size = (240, 320))
  2381. rotationAngle = 180
  2382. else:
  2383. imgRotationAim = image.new(size = (320, 240))
  2384. rotationAngle = 90
  2385. except:
  2386. imgRotationAim = image.new(size = (320, 240))
  2387. rotationAngle = 90
  2388. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  2389. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  2390. return GETROTATION
  2391. `
  2392. Blockly.Python.definitions_['lcd_invert_init_new'] = `def lcdRotationNew(inputImg):
  2393. global SETVFLIP,SETHMIRROT,cameraSize,ScreenOrientation
  2394. imageRotationBuffer = inputImg.crop(0, 0, 320, 240)
  2395. try:
  2396. if ScreenOrientation:
  2397. imgRotationAim = image.new(size = (240, 320))
  2398. rotationAngle = 90
  2399. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  2400. else:
  2401. imgRotationAim = image.new(size = (320, 240))
  2402. GETROTATION = imageRotationBuffer
  2403. except:
  2404. imgRotationAim = image.new(size = (320, 240))
  2405. GETROTATION = imageRotationBuffer
  2406. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  2407. return GETROTATION
  2408. `
  2409. Blockly.Python.definitions_['lcd_invert_rotate_camera'] = `def getLcdRotation(cameraCapture):
  2410. global cameraSize
  2411. if cameraSize:
  2412. return lcdRotationNew(cameraCapture)
  2413. else:
  2414. return lcdRotation(cameraCapture)
  2415. `
  2416. // #从摄像头中获取一张图像
  2417. var code = `canvas = getLcdRotation(camera.capture())\n`;
  2418. // TODO: Assemble Python into code variable.
  2419. if (dropdown_size == "224") {
  2420. code = `canvas = getLcdRotation(camera.capture()).crop(0,0,224,224)\n`
  2421. }
  2422. return code;
  2423. };
  2424. Blockly.Blocks['ai_camera_snapshot'] = {
  2425. init: function () {
  2426. this.appendDummyInput()
  2427. .appendField(Blockly.Msg.ai_camera_snapshot);
  2428. this.setOutput(false, null);
  2429. this.setNextStatement(true, null);
  2430. this.setPreviousStatement(true, null);
  2431. this.setColour("#60c1bb");
  2432. this.setTooltip(Blockly.Msg.ai_camera_snapshot_TOOLTIP);
  2433. this.setHelpUrl('');
  2434. }
  2435. };
  2436. Blockly.Python.ai_camera_snapshot = function (block) {
  2437. Blockly.Python.definitions_['v831_import_camera'] = `from maix import camera`
  2438. Blockly.Python.definitions_['v831_import_os'] = `import os`
  2439. Blockly.Python.definitions_['v831_import_image'] = `from maix import image`;
  2440. Blockly.Python.definitions_['v831_camera_set'] = `cameraSize = True
  2441. def CAMERATYPE():
  2442. global cameraSize
  2443. try:
  2444. if os.path.exists("/etc/cameraSize.cfg"):
  2445. cameraSize = True
  2446. else:
  2447. cameraSize = False
  2448. except:
  2449. cameraSize = False
  2450. CAMERATYPE()
  2451. `
  2452. Blockly.Python.definitions_['lcd_invert_init_new'] = `def lcdRotationNew(inputImg):
  2453. global SETVFLIP,SETHMIRROT,cameraSize,ScreenOrientation
  2454. imageRotationBuffer = inputImg.crop(0, 0, 320, 240)
  2455. if ScreenOrientation:
  2456. imgRotationAim = image.new(size = (240, 320))
  2457. rotationAngle = 90
  2458. GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
  2459. else:
  2460. imgRotationAim = image.new(size = (320, 240))
  2461. GETROTATION = imageRotationBuffer
  2462. GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
  2463. return GETROTATION
  2464. `
  2465. Blockly.Python.addVariable("ScreenOrientation", `ScreenOrientation = False`, true)
  2466. // #从摄像头中获取一张图像
  2467. var _code = `canvas = lcdRotationNew(camera.capture())\n`;
  2468. return _code;
  2469. }
  2470. // 图传
  2471. Blockly.Blocks['v831_camera_graph_transmission'] = {
  2472. init: function () {
  2473. this.appendDummyInput()
  2474. .appendField(Blockly.Msg.v831_transmission_setup_show);
  2475. // this.appendDummyInput()
  2476. // .appendField(Blockly.Msg.v831_transmission_init_setup_source)
  2477. // .appendField(new Blockly.FieldVariable("img_graphTransmission"), "varitem");
  2478. this.setPreviousStatement(true, null);
  2479. this.setNextStatement(true, null);
  2480. this.setColour("#60c1bb");
  2481. this.setTooltip(Blockly.Msg.v831_transmission_setup_show);
  2482. this.setHelpUrl('');
  2483. }
  2484. };
  2485. Blockly.Python.v831_camera_graph_transmission = function (block) {
  2486. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2487. Blockly.Python.definitions_['v831_import_mjpg'] = `from maix import mjpg`
  2488. Blockly.Python.definitions_['v831_import_utils'] = `from maix import utils`
  2489. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  2490. Blockly.Python.definitions_['v831_import_time'] = `import time`
  2491. // #从摄像头中获取一张图像
  2492. var _code = `jpg = utils.rgb2jpg(canvas.convert("RGB").tobytes(), canvas.width, canvas.height)
  2493. img_base64 = base64.b64encode(jpg)
  2494. img_base64_str = "data:image/jpg;base64," + str(img_base64)[2:-1]
  2495. time.sleep(0.1)
  2496. try:
  2497. print(img_base64_str)
  2498. pass
  2499. except Exception as e:
  2500. print(str(e))
  2501. pass
  2502. `;
  2503. return _code
  2504. }
  2505. Blockly.Blocks['v831_camera_internet_transmission'] = {
  2506. init: function () {
  2507. this.appendDummyInput()
  2508. .appendField(Blockly.Msg.v831_internet_transmission_setup_show);
  2509. // this.appendDummyInput()
  2510. // .appendField(Blockly.Msg.v831_transmission_init_setup_source)
  2511. // .appendField(new Blockly.FieldVariable("img_graphTransmission"), "varitem");
  2512. this.setPreviousStatement(true, null);
  2513. this.setNextStatement(true, null);
  2514. this.setColour("#60c1bb");
  2515. this.setTooltip(Blockly.Msg.v831_transmission_setup_show);
  2516. this.setHelpUrl('');
  2517. }
  2518. };
  2519. Blockly.Python.v831_camera_internet_transmission = function (block) {
  2520. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2521. Blockly.Python.definitions_['v831_import_mjpg'] = `from maix import mjpg`
  2522. Blockly.Python.definitions_['v831_import_utils'] = `from maix import utils`
  2523. Blockly.Python.definitions_['v831_import_queue'] = `queue = mjpg.Queue(maxsize=8)
  2524. mjpg.MjpgServerThread("0.0.0.0", 18811, mjpg.BytesImageHandlerFactory(q=queue)).start()`
  2525. // #从摄像头中获取一张图像
  2526. var _code = `jpg = utils.rgb2jpg(canvas.convert("RGB").tobytes(), 320, 240)
  2527. queue.put(mjpg.BytesImage(jpg))
  2528. `;
  2529. return _code
  2530. }
  2531. Blockly.Blocks['ai_camera_lens_corr'] = {
  2532. init: function () {
  2533. this.appendValueInput("lens_corr")
  2534. .setCheck(null)
  2535. .appendField(Blockly.Msg.ai_camera_set_lens_corr);
  2536. this.setInputsInline(true);
  2537. this.setOutput(true, null);
  2538. this.setColour("#60c1bb");
  2539. this.setTooltip(Blockly.Msg.ai_camera_lens_corr_TOOLTIP);
  2540. this.setHelpUrl("");
  2541. }
  2542. };
  2543. Blockly.Python['ai_camera_lens_corr'] = function (block) {
  2544. var value_lens_corr = Blockly.Python.valueToCode(block, 'lens_corr', Blockly.Python.ORDER_ATOMIC);
  2545. // TODO: Assemble Python into code variable.
  2546. var code = value_lens_corr + '.lens_corr(strength = 1.8, zoom = 1.0)';
  2547. // TODO: Change ORDER_NONE to the correct strength.
  2548. return [code, Blockly.Python.ORDER_NONE];
  2549. };
  2550. Blockly.Blocks['ai_camera_image_property_change'] = {
  2551. init: function () {
  2552. this.appendDummyInput()
  2553. .appendField(Blockly.Msg.ai_camera_bcs_set_text_2)
  2554. .appendField(new Blockly.FieldDropdown([
  2555. [Blockly.Msg.ai_camera_bcs_vflip, "set_vflip"],
  2556. [Blockly.Msg.ai_camera_bcs_hmirror, "set_hmirror"],
  2557. // [Blockly.Msg.ai_camera_bcs_wb, "set_auto_whitebal"],
  2558. // [Blockly.Msg.ai_camera_bcs_g, "set_auto_gain"],
  2559. // [Blockly.Msg.ai_camera_bcs_e, "set_auto_exposure"]
  2560. ]), "image_property")
  2561. .appendField(Blockly.Msg.ai_camera_bcs_as_2)
  2562. .appendField(new Blockly.FieldDropdown([
  2563. [Blockly.Msg.ai_camera_enable + Blockly.Msg.ai_camera_bcs_status, "True"],
  2564. [Blockly.Msg.ai_camera_disable + Blockly.Msg.ai_camera_bcs_status, "False"]
  2565. ]), "DEGREE");
  2566. this.setPreviousStatement(true, null);
  2567. this.setNextStatement(true, null);
  2568. this.setColour("#60c1bb");
  2569. var thisBlock = this;
  2570. this.setTooltip(function () {
  2571. var mode = thisBlock.getFieldValue('image_property');
  2572. var TOOLTIPS = {
  2573. 'set_vflip': Blockly.Msg.ai_camera_image_property_change_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_vflip),
  2574. 'set_hmirror': Blockly.Msg.ai_camera_image_property_change_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_hmirror),
  2575. 'set_auto_whitebal': Blockly.Msg.ai_camera_image_property_change_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_wb),
  2576. 'set_auto_gain': Blockly.Msg.ai_camera_image_property_change_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_g),
  2577. 'set_auto_exposure': Blockly.Msg.ai_camera_image_property_change_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_e)
  2578. };
  2579. return TOOLTIPS[mode];
  2580. });
  2581. this.setHelpUrl('');
  2582. }
  2583. };
  2584. Blockly.Python.ai_camera_image_property_change = function (block) {
  2585. var degee = block.getFieldValue('DEGREE');
  2586. var dropdown_image_property = block.getFieldValue('image_property');
  2587. var _code = ``;
  2588. if (dropdown_image_property == "set_vflip") {
  2589. _code = `SETVFLIP = ${degee}
  2590. `
  2591. } else if (dropdown_image_property == "set_hmirror") {
  2592. _code = `SETHMIRROT = ${degee}
  2593. `
  2594. }
  2595. return _code;
  2596. }
  2597. Blockly.Blocks['ai_camera_vflip'] = {
  2598. init: function () {
  2599. this.appendDummyInput()
  2600. .appendField(Blockly.Msg.ai_camera_set_vflip)
  2601. .appendField(new Blockly.FieldDropdown([
  2602. [Blockly.Msg.ai_camera_enable, "True"],
  2603. [Blockly.Msg.ai_camera_disable, "False"]
  2604. ]), "DEGREE")
  2605. this.setPreviousStatement(true, null);
  2606. this.setNextStatement(true, null);
  2607. this.setColour("#60c1bb");
  2608. this.setTooltip('');
  2609. this.setHelpUrl('');
  2610. }
  2611. };
  2612. Blockly.Python.ai_camera_vflip = function (block) {
  2613. var degee = block.getFieldValue('DEGREE');
  2614. var _code = "sensor.set_vflip(" + degee + ")\n";
  2615. return _code;
  2616. }
  2617. Blockly.Blocks['ai_camera_hmirror'] = {
  2618. init: function () {
  2619. this.appendDummyInput()
  2620. .appendField(Blockly.Msg.ai_camera_set_hmirror)
  2621. .appendField(new Blockly.FieldDropdown([
  2622. [Blockly.Msg.ai_camera_enable, "True"],
  2623. [Blockly.Msg.ai_camera_disable, "False"]
  2624. ]), "DEGREE")
  2625. this.setPreviousStatement(true, null);
  2626. this.setNextStatement(true, null);
  2627. this.setColour("#60c1bb");
  2628. this.setTooltip('');
  2629. this.setHelpUrl('');
  2630. }
  2631. };
  2632. Blockly.Python.ai_camera_hmirror = function (block) {
  2633. var degee = block.getFieldValue('DEGREE');
  2634. var _code = "sensor.set_hmirror(" + degee + ")\n"
  2635. return _code;
  2636. }
  2637. Blockly.Blocks['ai_camera_auto_whitebal'] = {
  2638. init: function () {
  2639. this.appendDummyInput()
  2640. .appendField(Blockly.Msg.ai_camera_set_auto_whitebal)
  2641. .appendField(new Blockly.FieldDropdown([
  2642. [Blockly.Msg.ai_camera_enable, "True"],
  2643. [Blockly.Msg.ai_camera_disable, "False"]
  2644. ]), "DEGREE")
  2645. this.setPreviousStatement(true, null);
  2646. this.setNextStatement(true, null);
  2647. this.setColour("#60c1bb");
  2648. this.setTooltip('');
  2649. this.setHelpUrl('');
  2650. }
  2651. };
  2652. Blockly.Python.ai_camera_auto_whitebal = function (block) {
  2653. var degee = block.getFieldValue('DEGREE');
  2654. var _code = "sensor.set_auto_whitebal(" + degee + ")\n"
  2655. return _code;
  2656. }
  2657. Blockly.Blocks['ai_camera_auto_gain'] = {
  2658. init: function () {
  2659. this.appendDummyInput()
  2660. .appendField(Blockly.Msg.ai_camera_set_auto_gain)
  2661. .appendField(new Blockly.FieldDropdown([
  2662. [Blockly.Msg.ai_camera_enable, "True"],
  2663. [Blockly.Msg.ai_camera_disable, "False"]
  2664. ]), "DEGREE")
  2665. this.setPreviousStatement(true, null);
  2666. this.setNextStatement(true, null);
  2667. this.setColour("#60c1bb");
  2668. this.setTooltip('');
  2669. this.setHelpUrl('');
  2670. }
  2671. };
  2672. Blockly.Python.ai_camera_auto_gain = function (block) {
  2673. var degee = block.getFieldValue('DEGREE');
  2674. var _code = "sensor.set_auto_gain(" + degee + ")\n"
  2675. return _code;
  2676. }
  2677. Blockly.Blocks['ai_camera_auto_exposure'] = {
  2678. init: function () {
  2679. this.appendDummyInput()
  2680. .appendField(Blockly.Msg.ai_camera_set_auto_exposure)
  2681. .appendField(new Blockly.FieldDropdown([
  2682. [Blockly.Msg.ai_camera_enable, "True"],
  2683. [Blockly.Msg.ai_camera_disable, "False"]
  2684. ]), "DEGREE")
  2685. this.setPreviousStatement(true, null);
  2686. this.setNextStatement(true, null);
  2687. this.setColour("#60c1bb");
  2688. this.setTooltip('');
  2689. this.setHelpUrl('');
  2690. }
  2691. };
  2692. Blockly.Python.ai_camera_auto_exposure = function (block) {
  2693. var degee = block.getFieldValue('DEGREE');
  2694. var _code = "sensor.set_auto_exposure(" + degee + ")\n"
  2695. return _code;
  2696. }
  2697. Blockly.Blocks['ai_camera_bcs_operation'] = {
  2698. init: function () {
  2699. this.appendDummyInput()
  2700. .appendField(Blockly.Msg.ai_camera_bcs_set_text)
  2701. .appendField(new Blockly.FieldDropdown([
  2702. [Blockly.Msg.ai_camera_bcs_contrast, "contrast"],
  2703. [Blockly.Msg.ai_camera_bcs_brightness, "brightness"],
  2704. [Blockly.Msg.ai_camera_bcs_saturation, "saturation"]
  2705. ]), "image_property")
  2706. .appendField(Blockly.Msg.ai_camera_bcs_as);
  2707. this.appendValueInput("A").setCheck("Number");
  2708. this.appendDummyInput()
  2709. .appendField(Blockly.Msg.ai_camera_set_range);
  2710. this.setPreviousStatement(true, null);
  2711. this.setNextStatement(true, null);
  2712. this.setInputsInline(!0);
  2713. this.setColour("#60c1bb");
  2714. var thisBlock = this;
  2715. this.setTooltip(function () {
  2716. var mode = thisBlock.getFieldValue('image_property');
  2717. var TOOLTIPS = {
  2718. 'contrast': Blockly.Msg.ai_camera_bcs_operation_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_contrast),
  2719. 'brightness': Blockly.Msg.ai_camera_bcs_operation_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_brightness),
  2720. 'saturation': Blockly.Msg.ai_camera_bcs_operation_TOOLTIP.replace('%1', Blockly.Msg.ai_camera_bcs_saturation)
  2721. };
  2722. return TOOLTIPS[mode];
  2723. });
  2724. this.setHelpUrl('');
  2725. }
  2726. };
  2727. Blockly.Python.ai_camera_bcs_operation = function (block) {
  2728. var dropdown_image_property = block.getFieldValue('image_property');
  2729. var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
  2730. var _code = "sensor.set_" + dropdown_image_property + "(" + _x + ")\n"
  2731. return _code;
  2732. }
  2733. Blockly.Blocks['ai_camera_contrast'] = {
  2734. init: function () {
  2735. this.appendDummyInput()
  2736. .appendField(Blockly.Msg.ai_camera_set_contrast_to_value);
  2737. this.appendValueInput("A").setCheck("Number");
  2738. this.appendDummyInput()
  2739. .appendField(Blockly.Msg.ai_camera_set_range);
  2740. this.setPreviousStatement(true, null);
  2741. this.setNextStatement(true, null);
  2742. this.setInputsInline(!0);
  2743. this.setColour("#60c1bb");
  2744. this.setTooltip('');
  2745. this.setHelpUrl('');
  2746. }
  2747. };
  2748. Blockly.Python.ai_camera_contrast = function (block) {
  2749. var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
  2750. var _code = "sensor.set_contrast(" + _x + ")\n"
  2751. return _code;
  2752. }
  2753. Blockly.Blocks['ai_camera_brightness'] = {
  2754. init: function () {
  2755. this.appendDummyInput()
  2756. .appendField(Blockly.Msg.ai_camera_set_brightness_to_value);
  2757. this.appendValueInput("A").setCheck("Number");
  2758. this.appendDummyInput()
  2759. .appendField(Blockly.Msg.ai_camera_set_range);
  2760. this.setPreviousStatement(true, null);
  2761. this.setNextStatement(true, null);
  2762. this.setInputsInline(!0);
  2763. this.setColour("#60c1bb");
  2764. this.setTooltip('');
  2765. this.setHelpUrl('');
  2766. }
  2767. };
  2768. Blockly.Python.ai_camera_brightness = function (block) {
  2769. var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
  2770. var _code = "sensor.set_brightness(" + _x + ")\n"
  2771. return _code;
  2772. }
  2773. Blockly.Blocks['ai_camera_saturation'] = {
  2774. init: function () {
  2775. this.appendDummyInput()
  2776. .appendField(Blockly.Msg.ai_camera_set_saturation_value);
  2777. this.appendValueInput("A").setCheck("Number");
  2778. this.appendDummyInput()
  2779. .appendField(Blockly.Msg.ai_camera_set_range);
  2780. this.setPreviousStatement(true, null);
  2781. this.setNextStatement(true, null);
  2782. this.setInputsInline(!0);
  2783. this.setColour("#60c1bb");
  2784. this.setTooltip('');
  2785. this.setHelpUrl('');
  2786. }
  2787. };
  2788. Blockly.Python.ai_camera_saturation = function (block) {
  2789. var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
  2790. var _code = "sensor.set_saturation(" + _x + ")\n"
  2791. return _code;
  2792. }
  2793. /*
  2794. _ ___ __ ___ _
  2795. / \ |_ _| \ \ / (_)___(_) ___ _ __
  2796. / _ \ | | \ \ / /| / __| |/ _ \| '_ \
  2797. / ___ \ | | \ V / | \__ \ | (_) | | | |
  2798. /_/ \_\___| \_/ |_|___/_|\___/|_| |_|
  2799. */
  2800. Blockly.Blocks.ai_lcd_XY_vision = {
  2801. init: function () {
  2802. var _input = this.appendDummyInput();
  2803. this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
  2804. this.setColour("#f0983e");
  2805. this.setOutput(!0, "Boolean");
  2806. _input.appendField(Blockly.Msg.X);
  2807. this.appendValueInput("A").setCheck("Number");
  2808. _input = this.appendDummyInput();
  2809. _input.appendField(Blockly.Msg.Y)
  2810. this.appendValueInput("B").setCheck("Number");
  2811. this.setInputsInline(!0);
  2812. }
  2813. };
  2814. Blockly.Python.ai_lcd_XY_vision = function (block) {
  2815. var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
  2816. var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
  2817. var code = "" + _x + ", " + _y + "";
  2818. return [code, Blockly.Python.ORDER_ATOMIC];
  2819. };
  2820. Blockly.Blocks.ai_lcd_WH_vision = {
  2821. init: function () {
  2822. var _input = this.appendDummyInput();
  2823. this.setHelpUrl(Blockly.Msg.LOGIC_OPERATION_HELPURL);
  2824. this.setColour("#f0983e");
  2825. this.setOutput(!0, "Boolean");
  2826. _input.appendField(Blockly.Msg.W);
  2827. this.appendValueInput("A").setCheck("Number");
  2828. _input = this.appendDummyInput();
  2829. _input.appendField(Blockly.Msg.H)
  2830. this.appendValueInput("B").setCheck("Number");
  2831. this.setInputsInline(!0);
  2832. }
  2833. };
  2834. Blockly.Python.ai_lcd_WH_vision = function (block) {
  2835. var _x = Blockly.Python.valueToCode(block, "A", Blockly.Python.ORDER_ATOMIC);
  2836. var _y = Blockly.Python.valueToCode(block, "B", Blockly.Python.ORDER_ATOMIC);
  2837. var code = "" + _x + ", " + _y + "";
  2838. return [code, Blockly.Python.ORDER_ATOMIC];
  2839. };
  2840. Blockly.Blocks['ai_canvas_area_tuple'] = {
  2841. init: function () {
  2842. this.appendDummyInput()
  2843. .appendField(Blockly.Msg.ai_vision_canvas_area_tuple);
  2844. this.appendValueInput("xy")
  2845. .setCheck(null)
  2846. .appendField(Blockly.Msg.ai_vision_canvas_area_tuple_xy);
  2847. this.appendValueInput("wh")
  2848. .setCheck(null)
  2849. .appendField(Blockly.Msg.ai_vision_canvas_area_tuple_wh);
  2850. this.setInputsInline(false);
  2851. this.setOutput(true, null);
  2852. this.setColour("#f0983e");
  2853. this.setTooltip(Blockly.Msg.ai_vision_canvas_area_tuple_TOOLTIP);
  2854. this.setHelpUrl("");
  2855. }
  2856. };
  2857. Blockly.Python['ai_canvas_area_tuple'] = function (block) {
  2858. var value_coordinate = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  2859. var value_size = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
  2860. // TODO: Assemble Python into code variable.
  2861. var code = value_coordinate + ',' + value_size;
  2862. // TODO: Change ORDER_NONE to the correct strength.
  2863. return [code, Blockly.Python.ORDER_NONE];
  2864. };
  2865. Blockly.Blocks['ai_lane_tracking_run'] = {
  2866. init: function () {
  2867. this.appendDummyInput()
  2868. .appendField(new Blockly.FieldImage("blockly/media/lane_tracking_header.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
  2869. this.appendDummyInput()
  2870. .appendField(Blockly.Msg.ai_vision_from_canvas + Blockly.Msg.ai_vision_lane_tracking_run)
  2871. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  2872. // .appendField();
  2873. this.appendValueInput("rgb_value")
  2874. .setCheck(null)
  2875. .appendField(Blockly.Msg.ai_vision_lane_tracking_setup_threshold);
  2876. this.setPreviousStatement(true, null);
  2877. this.setNextStatement(true, null);
  2878. this.setColour("#f0983e");
  2879. this.setTooltip(Blockly.Msg.ai_vision_lane_tracking_setup_threshold);
  2880. this.setHelpUrl("");
  2881. }
  2882. };
  2883. Blockly.Python['ai_lane_tracking_run'] = function (block) {
  2884. var value_color = Blockly.Python.valueToCode(block, 'rgb_value', Blockly.Python.ORDER_ATOMIC)
  2885. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2886. var code = '' +
  2887. 'line = canvas.find_line()\n' +
  2888. 'canvas.draw_line(line["rect"][0], line["rect"][1], line["rect"][2],line["rect"][3], color=' + value_color + ', thickness=1)\n' +
  2889. 'canvas.draw_line(line["rect"][2], line["rect"][3], line["rect"][4],line["rect"][5], color=' + value_color + ', thickness=1)\n' +
  2890. 'canvas.draw_line(line["rect"][4], line["rect"][5], line["rect"][6],line["rect"][7], color=' + value_color + ', thickness=1)\n' +
  2891. 'canvas.draw_line(line["rect"][6], line["rect"][7], line["rect"][0],line["rect"][1], color=' + value_color + ', thickness=1)\n' +
  2892. 'canvas.draw_circle(line["cx"], line["cy"], 4,color=' + value_color + ', thickness=1)\n';
  2893. return code;
  2894. };
  2895. Blockly.Blocks['ai_vision_find_lines'] = {
  2896. init: function () {
  2897. this.appendDummyInput()
  2898. .appendField(new Blockly.FieldImage("blockly/media/ai_vision_find_line.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
  2899. this.appendDummyInput()
  2900. .appendField(Blockly.Msg.ai_vision_from_canvas)
  2901. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  2902. .appendField(Blockly.Msg.ai_vision_find_line);
  2903. this.appendValueInput("xy")
  2904. .setCheck(null)
  2905. .appendField(Blockly.Msg.ai_vision_find_line_start_coord);
  2906. this.appendValueInput("wh")
  2907. .setCheck(null)
  2908. .appendField(Blockly.Msg.ai_vision_find_line_size);
  2909. /*this.appendValueInput("threshold")
  2910. .setCheck(null)
  2911. .appendField(Blockly.Msg.ai_vision_find_line_hoffman_threshold);
  2912. this.appendValueInput("theta_margin")
  2913. .setCheck(null)
  2914. .appendField(Blockly.Msg.ai_vision_find_line_merge_theta);
  2915. this.appendValueInput("rho_margin")
  2916. .setCheck(null)
  2917. .appendField(Blockly.Msg.ai_vision_find_line_merge_rho);*/
  2918. this.setOutput(true, null);
  2919. this.setColour("#f0983e");
  2920. this.setTooltip(Blockly.Msg.ai_vision_find_lines_TOOLTIP);
  2921. this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
  2922. }
  2923. };
  2924. Blockly.Python['ai_vision_find_lines'] = function (block) {
  2925. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2926. //var threshold = Blockly.Python.valueToCode(block, 'threshold', Blockly.Python.ORDER_ATOMIC);
  2927. //var theta_margin = Blockly.Python.valueToCode(block, 'theta_margin', Blockly.Python.ORDER_ATOMIC);
  2928. //var rho_margin = Blockly.Python.valueToCode(block, 'rho_margin', Blockly.Python.ORDER_ATOMIC);
  2929. var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  2930. var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
  2931. // TODO: Assemble Python into code variable.
  2932. var code = variable_name + '.find_lines(roi=(' + xy + ',' + wh + '), threshold = 1000, theta_margin = 25, rho_margin = 25)';
  2933. // TODO: Change ORDER_NONE to the correct strength.
  2934. return [code, Blockly.Python.ORDER_NONE];
  2935. };
  2936. Blockly.Blocks['ai_vision_find_rects'] = {
  2937. init: function () {
  2938. this.appendDummyInput()
  2939. .appendField(new Blockly.FieldImage("blockly/media/ai_vision_find_rect.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
  2940. this.appendDummyInput()
  2941. .appendField(Blockly.Msg.ai_vision_from_canvas)
  2942. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  2943. .appendField(Blockly.Msg.ai_vision_find_rect);
  2944. this.appendValueInput("xy")
  2945. .setCheck(null)
  2946. .appendField(Blockly.Msg.ai_vision_find_rect_start_coord);
  2947. this.appendValueInput("wh")
  2948. .setCheck(null)
  2949. .appendField(Blockly.Msg.ai_vision_find_rect_region_size);
  2950. /*this.appendValueInput("threshold")
  2951. .setCheck(null)
  2952. .appendField(Blockly.Msg.ai_vision_find_rect_bounding_threshold);*/
  2953. this.setOutput(true, null);
  2954. this.setColour("#f0983e");
  2955. this.setTooltip(Blockly.Msg.ai_vision_find_rects_TOOLTIP);
  2956. this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
  2957. }
  2958. };
  2959. Blockly.Python['ai_vision_find_rects'] = function (block) {
  2960. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2961. var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  2962. var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
  2963. // var threshold = Blockly.Python.valueToCode(block, 'threshold', Blockly.Python.ORDER_ATOMIC);
  2964. // TODO: Assemble Python into code variable.
  2965. var code = variable_name + '.find_rects(roi=(' + xy + ',' + wh + '), threshold = 10000)';
  2966. // TODO: Change ORDER_NONE to the correct strength.
  2967. return [code, Blockly.Python.ORDER_NONE];
  2968. };
  2969. Blockly.Blocks['ai_vision_find_lines'] = {
  2970. init: function () {
  2971. this.appendDummyInput()
  2972. .appendField(new Blockly.FieldImage("blockly/media/ai_vision_find_color.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
  2973. this.appendDummyInput()
  2974. .appendField(Blockly.Msg.ai_vision_from_canvas + Blockly.Msg.ai_vision_find_blobs_color_region)
  2975. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  2976. // .appendField();
  2977. this.appendValueInput("color")
  2978. .setCheck(null)
  2979. .appendField(Blockly.Msg.ai_vision_lane_tracking_setup_threshold);
  2980. this.appendValueInput("xy")
  2981. .setCheck(null)
  2982. .appendField(Blockly.Msg.ai_vision_find_blobs_start_coord);
  2983. this.appendValueInput("wh")
  2984. .setCheck(null)
  2985. .appendField(Blockly.Msg.ai_vision_find_blobs_size);
  2986. this.setInputsInline(false);
  2987. this.setOutput(true, null);
  2988. this.setColour("#f0983e");
  2989. this.setTooltip(Blockly.Msg.ai_vision_find_blobs_TOOLTIP);
  2990. this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
  2991. }
  2992. };
  2993. Blockly.Python['ai_vision_find_lines'] = function (block) {
  2994. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  2995. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  2996. var value_xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  2997. var value_wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
  2998. Blockly.Python.definitions_[''] = `
  2999. def v831_find_blob_fun(lane_trackingimg,region1,COLOR):
  3000. IMGBLOB = lane_trackingimg.crop(region1[0],region1[1],region1[2],region1[3])
  3001. blobsCANVAS = IMGBLOB.find_blobs(COLOR, merge=True)
  3002. return blobsCANVAS
  3003. `;
  3004. var code = `v831_find_blob_fun(canvas,(${value_xy},${value_wh}),${value_color})
  3005. `;
  3006. return [code, Blockly.Python.ORDER_NONE];
  3007. };
  3008. Blockly.Blocks['ai_vision_find_blobs'] = {
  3009. init: function () {
  3010. this.appendDummyInput()
  3011. .appendField(new Blockly.FieldImage("blockly/media/ai_vision_find_color.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
  3012. this.appendDummyInput()
  3013. .appendField(Blockly.Msg.ai_vision_from_canvas + Blockly.Msg.ai_vision_find_lines_color_region)
  3014. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  3015. // .appendField();
  3016. this.appendValueInput("color")
  3017. .setCheck(null)
  3018. .appendField(Blockly.Msg.ai_vision_lane_tracking_setup_threshold);
  3019. this.appendValueInput("xy")
  3020. .setCheck(null)
  3021. .appendField(Blockly.Msg.ai_vision_find_blobs_start_coord);
  3022. this.appendValueInput("wh")
  3023. .setCheck(null)
  3024. .appendField(Blockly.Msg.ai_vision_find_blobs_size);
  3025. this.setInputsInline(false);
  3026. this.setOutput(true, null);
  3027. this.setColour("#f0983e");
  3028. this.setTooltip(Blockly.Msg.ai_vision_find_blobs_TOOLTIP);
  3029. this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
  3030. }
  3031. };
  3032. Blockly.Python['ai_vision_find_blobs'] = function (block) {
  3033. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  3034. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  3035. var value_xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  3036. var value_wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
  3037. Blockly.Python.addVariable('lane_trackingline', "lane_trackingline1 = {}", true);
  3038. Blockly.Python.definitions_['ai_vision_find_blobs_color'] = `_ld_color_detection_threshold = ${value_color}
  3039. def v831_lane_tracking_setup_one(lane_trackingimg,region1):
  3040. global lane_trackingline
  3041. lane_trackingcanvas = lane_trackingimg.crop(region1[0],region1[1],region1[2],region1[3])
  3042. lane_trackingline = lane_trackingcanvas.find_line()
  3043. lane_trackingcanvas.draw_circle(lane_trackingline["cx"], lane_trackingline["cy"], 2,color=(0,255,0), thickness=1)
  3044. lane_trackingimg.draw_image(lane_trackingcanvas,region1[0],region1[1])
  3045. lane_trackingimg.draw_rectangle(region1[0],region1[1],region1[0]+region1[2],region1[1]+region1[3], color=(255,0,0), thickness=1)
  3046. lane_trackingline["cx"] = lane_trackingline["cx"] + region1[0]
  3047. lane_trackingline["cy"] = lane_trackingline["cy"] + region1[1]
  3048. return lane_trackingline
  3049. `;
  3050. var code = `v831_lane_tracking_setup_one(canvas,(${value_xy},${value_wh}))
  3051. `;
  3052. return [code, Blockly.Python.ORDER_NONE];
  3053. };
  3054. //
  3055. Blockly.Blocks['ai_vision_find_blobs_result'] = {
  3056. init: function () {
  3057. this.appendDummyInput()
  3058. .appendField(Blockly.Msg.OLCD_LINE_START)
  3059. .appendField(new Blockly.FieldVariable("i"), "varitem")
  3060. .appendField(Blockly.Msg.ai_vision_find_lines_color_region_result)
  3061. .appendField(new Blockly.FieldDropdown([
  3062. [Blockly.Msg.ai_models_object_model_x_center, "cx"],
  3063. [Blockly.Msg.ai_models_object_model_y_center, "cy"]
  3064. ]), "value");
  3065. // this.setInputsInline(false);
  3066. this.setOutput(true, null);
  3067. this.setColour("#f0983e");
  3068. this.setTooltip(Blockly.Msg.ai_vision_find_lines_color_region_result);
  3069. }
  3070. };
  3071. Blockly.Python['ai_vision_find_blobs_result'] = function (block) {
  3072. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  3073. var property = block.getFieldValue('value');
  3074. var code = `${variable_name}.get("${property}")`;
  3075. return [code, Blockly.Python.ORDER_ATOMIC];
  3076. };
  3077. Blockly.Blocks['ai_lane_tracking_setup'] = {
  3078. init: function () {
  3079. this.appendDummyInput()
  3080. .appendField(new Blockly.FieldImage("blockly/media/lane_tracking_header.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
  3081. this.appendDummyInput().appendField(Blockly.Msg.ai_vision_from_canvas + Blockly.Msg.ai_vision_lane_tracking_setup)
  3082. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  3083. // .appendField();
  3084. this.appendValueInput("color")
  3085. .setCheck(null)
  3086. .appendField(Blockly.Msg.ai_vision_lane_tracking_setup_threshold);
  3087. this.appendValueInput("lsize")
  3088. .setCheck(null)
  3089. .appendField(Blockly.Msg.ai_vision_lane_tracking_setup_l);
  3090. this.appendValueInput("rsize")
  3091. .setCheck(null)
  3092. .appendField(Blockly.Msg.ai_vision_lane_tracking_setup_r);
  3093. // this.appendValueInput("axis")
  3094. // .setCheck(null)
  3095. // .appendField(Blockly.Msg.ai_vision_lane_tracking_setup_axis);
  3096. this.setPreviousStatement(true, null);
  3097. this.setNextStatement(true, null);
  3098. this.setColour("#f0983e");
  3099. this.setTooltip(Blockly.Msg.ai_vision_lane_tracking_setup_TOOLTIP);
  3100. this.setHelpUrl("");
  3101. }
  3102. };
  3103. Blockly.Python['ai_lane_tracking_setup'] = function (block) {
  3104. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  3105. var value_color = Blockly.Python.valueToCode(block, 'color', Blockly.Python.ORDER_ATOMIC);
  3106. var value_lsize = Blockly.Python.valueToCode(block, 'lsize', Blockly.Python.ORDER_ATOMIC);
  3107. var value_rsize = Blockly.Python.valueToCode(block, 'rsize', Blockly.Python.ORDER_ATOMIC);
  3108. var value_axis = Blockly.Python.valueToCode(block, 'axis', Blockly.Python.ORDER_ATOMIC);
  3109. console.log(value_lsize[0])
  3110. Blockly.Python.addVariable('lane_trackingline1', "lane_trackingline1 = {}", true);
  3111. Blockly.Python.addVariable('lane_trackingline2', "lane_trackingline2 = {}", true);
  3112. Blockly.Python.definitions_['ai_lane_tracking_setup'] = `_ld_color_detection_threshold = ${value_color}
  3113. def v831_lane_tracking_setup(lane_trackingimg,region1,region2):
  3114. global lane_trackingline1,lane_trackingline2
  3115. lane_trackingcanvas1 = lane_trackingimg.crop(region1[0],region1[1],region1[2],region1[3])
  3116. lane_trackingcanvas2 = lane_trackingimg.crop(region2[0],region2[1],region2[2],region2[3])
  3117. lane_trackingline1 = lane_trackingcanvas1.find_line()
  3118. lane_trackingline2 = lane_trackingcanvas2.find_line()
  3119. lane_trackingcanvas1.draw_circle(lane_trackingline1["cx"], lane_trackingline1["cy"], 2,color=(0,255,0), thickness=1)
  3120. lane_trackingcanvas2.draw_circle(lane_trackingline2["cx"], lane_trackingline2["cy"], 2,color=(0,255,0), thickness=1)
  3121. lane_trackingimg.draw_image(lane_trackingcanvas1,region1[0],region1[1])
  3122. lane_trackingimg.draw_image(lane_trackingcanvas2,region2[0],region2[1])
  3123. lane_trackingimg.draw_rectangle(region1[0],region1[1],region1[0]+region1[2],region1[1]+region1[3], color=(255,0,0), thickness=1)
  3124. lane_trackingimg.draw_rectangle(region2[0],region2[1],region2[0]+region2[2],region2[1]+region2[3], color=(255,0,0), thickness=1)
  3125. `
  3126. let code = `v831_lane_tracking_setup(canvas,${value_lsize},${value_rsize})
  3127. `
  3128. return code
  3129. }
  3130. Blockly.Blocks['ai_lane_tracking_get'] = {
  3131. init: function () {
  3132. this.appendDummyInput()
  3133. .appendField(Blockly.Msg.ai_vision_lane_tracking_run_get)
  3134. .appendField(new Blockly.FieldDropdown([
  3135. [Blockly.Msg.ai_vision_lane_tracking_run_ld, "left_check"],
  3136. [Blockly.Msg.ai_vision_lane_tracking_run_lv, "left_value"],
  3137. [Blockly.Msg.ai_vision_lane_tracking_run_rd, "right_check"],
  3138. [Blockly.Msg.ai_vision_lane_tracking_run_rv, "right_value"]
  3139. ]), "return");
  3140. this.setOutput(true, null);
  3141. this.setColour("#f0983e");
  3142. this.setTooltip(Blockly.Msg.ai_vision_lane_tracking_run_get_TOOLTIP);
  3143. this.setHelpUrl("");
  3144. }
  3145. };
  3146. Blockly.Python['ai_lane_tracking_get'] = function (block) {
  3147. var dropdown_return = block.getFieldValue('return');
  3148. // TODO: Assemble Python into code variable.
  3149. var code = '_ld_return["' + dropdown_return + '"]';
  3150. // TODO: Change ORDER_NONE to the correct strength.
  3151. return [code, Blockly.Python.ORDER_NONE];
  3152. };
  3153. Blockly.Blocks['ai_vision_color_analyze'] = {
  3154. init: function () {
  3155. this.appendDummyInput()
  3156. .appendField(new Blockly.FieldImage("blockly/media/ai_vision_analyze_color.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
  3157. this.appendDummyInput()
  3158. .appendField(Blockly.Msg.ai_vision_get_canvas + Blockly.Msg.ai_vision_color_analyze_get_result)
  3159. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  3160. // .appendField();
  3161. this.appendValueInput("xy")
  3162. .setCheck(null)
  3163. .appendField(Blockly.Msg.ai_vision_color_analyze_start_coord);
  3164. this.appendValueInput("wh")
  3165. .setCheck(null)
  3166. .appendField(Blockly.Msg.ai_vision_color_analyze_size);
  3167. this.setOutput(true, null);
  3168. this.setColour("#f0983e");
  3169. this.setTooltip(Blockly.Msg.ai_vision_color_analyze_TOOLTIP);
  3170. this.setHelpUrl("");
  3171. }
  3172. };
  3173. Blockly.Python['ai_vision_color_analyze'] = function (block) {
  3174. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  3175. var xy = Blockly.Python.valueToCode(block, 'xy', Blockly.Python.ORDER_ATOMIC);
  3176. var wh = Blockly.Python.valueToCode(block, 'wh', Blockly.Python.ORDER_ATOMIC);
  3177. // TODO: Assemble Python into code variable.
  3178. var code = 'canvas.get_blob_color((' + xy + ', ' + wh + '), 0, 0)';
  3179. // TODO: Change ORDER_NONE to the correct strength.
  3180. return [code, Blockly.Python.ORDER_NONE];
  3181. };
  3182. Blockly.Blocks['ai_vision_get_blobs'] = {
  3183. init: function () {
  3184. this.appendDummyInput()
  3185. .appendField(Blockly.Msg.ai_vision_from)
  3186. .appendField(new Blockly.FieldVariable("i"), "varitem")
  3187. .appendField(Blockly.Msg.ai_vision_get_blobs_result)
  3188. .appendField(new Blockly.FieldDropdown([
  3189. [Blockly.Msg.ai_vision_detection_box_x, "x"],
  3190. [Blockly.Msg.ai_vision_detection_box_y, "y"],
  3191. [Blockly.Msg.ai_models_face_model_ws, "w"],
  3192. [Blockly.Msg.ai_models_face_model_hs, "h"],
  3193. // [Blockly.Msg.ai_vision_detection_box_pixels, "pixels()"],
  3194. [Blockly.Msg.ai_vision_detection_box_cx, "cx"],
  3195. [Blockly.Msg.ai_vision_detection_box_cy, "cy"]
  3196. // [Blockly.Msg.ai_vision_detection_box_degree, "rotation()"],
  3197. // [Blockly.Msg.ai_vision_detection_box_total, "count()"]
  3198. ]), "blob_type");
  3199. this.setInputsInline(false);
  3200. this.setOutput(true, null);
  3201. this.setColour("#f0983e");
  3202. this.setTooltip(Blockly.Msg.ai_vision_get_blobs_TOOLTIP);
  3203. this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
  3204. }
  3205. };
  3206. Blockly.Python['ai_vision_get_blobs'] = function (block) {
  3207. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  3208. var type = block.getFieldValue('blob_type');
  3209. // TODO: Assemble Python into code variable.
  3210. var code = `${variable_name}["${type}"]`;
  3211. if (type == "cx") {
  3212. code = `int((${variable_name}["w"] + ${variable_name}["x"] + ${variable_name}["x"])/2)`
  3213. } else if (type == "cy") {
  3214. code = `int((${variable_name}["h"] + ${variable_name}["y"] + ${variable_name}["y"])/2)`
  3215. }
  3216. // TODO: Change ORDER_NONE to the correct strength.
  3217. return [code, Blockly.Python.ORDER_NONE];
  3218. };
  3219. Blockly.Blocks['ai_vision_get_threshold_default'] = {
  3220. init: function () {
  3221. this.appendDummyInput()
  3222. .appendField(Blockly.Msg.ai_vision_detection_color_threshold_set)
  3223. .appendField(new Blockly.FieldDropdown([
  3224. [Blockly.Msg.ai_vision_detection_color_threshold_set_black, "black"],
  3225. [Blockly.Msg.ai_vision_detection_color_threshold_set_white, "white"],
  3226. [Blockly.Msg.ai_vision_detection_color_threshold_set_red, "red"],
  3227. [Blockly.Msg.ai_vision_detection_color_threshold_set_green, "green"],
  3228. [Blockly.Msg.ai_vision_detection_color_threshold_set_blue, "blue"],
  3229. [Blockly.Msg.ai_vision_detection_color_threshold_set_orange, "orange"]
  3230. ]), "color");
  3231. this.setOutput(true, null);
  3232. this.setColour("#f0983e");
  3233. var thisBlock = this;
  3234. this.setTooltip(function () {
  3235. var mode = thisBlock.getFieldValue('color');
  3236. var TOOLTIPS = {
  3237. 'black': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_black),
  3238. 'white': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_white),
  3239. 'red': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_red),
  3240. 'green': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_green),
  3241. 'blue': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_blue),
  3242. 'orange': Blockly.Msg.ai_vision_get_threshold_default_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_detection_color_threshold_set_orange)
  3243. };
  3244. return TOOLTIPS[mode];
  3245. });
  3246. this.setHelpUrl("");
  3247. }
  3248. };
  3249. Blockly.Python['ai_vision_get_threshold_default'] = function (block) {
  3250. var dropdown_color = block.getFieldValue('color');
  3251. // TODO: Assemble Python into code variable.
  3252. if (dropdown_color == "black") {
  3253. var code = '[(0,40)]';
  3254. } else if (dropdown_color == "white") {
  3255. var code = '[(64,100)]';
  3256. } else if (dropdown_color == "red") {
  3257. var code = '[(45, 65, 40, 80, 40, 60)]';
  3258. } else if (dropdown_color == "green") {
  3259. var code = '[(45, 65, -50, -30, 0, 40)]';
  3260. } else if (dropdown_color == "blue") {
  3261. var code = '[(45, 65, -20, 30, -60, -20)]';
  3262. } else if (dropdown_color == "orange") {
  3263. var code = '[(77, 55, 19, 61, 14, 108)]';
  3264. }
  3265. // TODO: Change ORDER_NONE to the correct strength.
  3266. return [code, Blockly.Python.ORDER_NONE];
  3267. };
  3268. Blockly.Blocks['ai_vision_find_qrcode'] = {
  3269. init: function () {
  3270. this.appendDummyInput()
  3271. .appendField(new Blockly.FieldImage("blockly/media/ai_vision_qrcode_.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  3272. this.appendDummyInput()
  3273. .appendField(Blockly.Msg.image_process_text_recognize_qrcode)
  3274. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  3275. // .appendField();
  3276. this.setOutput(false, null);
  3277. this.setNextStatement(true, null);
  3278. this.setPreviousStatement(true, null);
  3279. this.setColour("#f0983e");
  3280. this.setTooltip(Blockly.Msg.ai_vision_find_qrcode_TOOLTIP);
  3281. this.setHelpUrl("");
  3282. }
  3283. };
  3284. Blockly.Python['ai_vision_find_qrcode'] = function (block) {
  3285. // TODO: Assemble Python into code variable.
  3286. Blockly.Python.addVariable('findQrcode', "findQrcode = []", true);
  3287. var code = 'findQrcode = canvas.find_qrcodes() \n';
  3288. // TODO: Change ORDER_NONE to the correct strength.
  3289. return code;
  3290. };
  3291. Blockly.Blocks['ai_vision_find_qrcode_result'] = {
  3292. init: function () {
  3293. this.appendDummyInput()
  3294. .appendField(Blockly.Msg.image_process_draw_qrcode_show)
  3295. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  3296. // .appendField();
  3297. this.setOutput(false, null);
  3298. this.setNextStatement(true, null);
  3299. this.setPreviousStatement(true, null);
  3300. this.setColour("#f0983e");
  3301. this.setTooltip(Blockly.Msg.ai_vision_find_qrcode_TOOLTIP);
  3302. this.setHelpUrl("");
  3303. }
  3304. };
  3305. Blockly.Python['ai_vision_find_qrcode_result'] = function (block) {
  3306. let code = `for i in findQrcode:
  3307. canvas.draw_string((i["x"]),(i["y"]), (i["payload"]), scale = 1, color = (255,0,0) , thickness = 1)
  3308. canvas.draw_rectangle((i["x"]),(i["y"]), (i["x"])+(i["w"]),(i["y"])+ (i["h"]), color=(255,0,0), thickness=1)
  3309. `
  3310. // TODO: Change ORDER_NONE to the correct strength.
  3311. return code;
  3312. };
  3313. Blockly.Blocks['ai_vision_qrcode_get_info'] = {
  3314. init: function () {
  3315. this.appendDummyInput()
  3316. .appendField(Blockly.Msg.ai_vision_qrcode_get_result + Blockly.Msg.ai_vision_pattern_detection_content)
  3317. this.setInputsInline(false);
  3318. this.setOutput(true, null);
  3319. this.setColour("#f0983e");
  3320. this.setTooltip(Blockly.Msg.ai_vision_qrcode_get_result + Blockly.Msg.ai_vision_pattern_detection_content);
  3321. this.setHelpUrl("");
  3322. }
  3323. };
  3324. Blockly.Python['ai_vision_qrcode_get_info'] = function (block) {
  3325. Blockly.Python.addFunction("getQRcodeResult", `def getQRcodeResult():
  3326. global findQrcode
  3327. qrCodeList = []
  3328. for i in findQrcode:
  3329. qrCodeList.append({i["payload"]})
  3330. return qrCodeList
  3331. `)
  3332. let code = "getQRcodeResult()"
  3333. return [code, Blockly.Python.ORDER_NONE];
  3334. };
  3335. Blockly.Blocks['ai_vision_find_apriltag'] = {
  3336. init: function () {
  3337. this.appendDummyInput()
  3338. .appendField(new Blockly.FieldImage("blockly/media/ai_vision_apriltag_.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  3339. this.appendDummyInput()
  3340. .appendField(Blockly.Msg.ai_vision_get_canvas + Blockly.Msg.ai_vision_apriltag_detected_result)
  3341. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  3342. // .appendField();
  3343. // this.setOutput(true, null);
  3344. this.setNextStatement(true, null);
  3345. this.setPreviousStatement(true, null);
  3346. this.setColour("#f0983e");
  3347. this.setTooltip(Blockly.Msg.ai_vision_find_apriltag_TOOLTIP);
  3348. this.setHelpUrl("");
  3349. }
  3350. };
  3351. Blockly.Python['ai_vision_find_apriltag'] = function (block) {
  3352. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  3353. // TODO: Assemble Python into code variable.
  3354. var code = 'findApriltags = canvas.find_apriltags(families = 16,fx = (6/5.76)*320,fy = (6/3.24)*240,cx = 160,cy = 120)';
  3355. // TODO: Change ORDER_NONE to the correct strength.
  3356. return code;
  3357. };
  3358. Blockly.Blocks['ai_vision_apriltag_get_info'] = {
  3359. init: function () {
  3360. this.appendDummyInput()
  3361. .appendField(Blockly.Msg.ai_vision_apriltag_get_result)
  3362. this.setInputsInline(false);
  3363. // this.setOutput(true, null);
  3364. this.setNextStatement(true, null);
  3365. this.setPreviousStatement(true, null);
  3366. this.setColour("#f0983e");
  3367. var thisBlock = this;
  3368. this.setTooltip(function () {
  3369. var mode = thisBlock.getFieldValue('get_info');
  3370. var TOOLTIPS = {
  3371. 'payload': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
  3372. 'id': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
  3373. 'family': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_type_family),
  3374. 'x': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_x),
  3375. 'y': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_y),
  3376. 'w': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_w),
  3377. 'h': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_h)
  3378. };
  3379. return TOOLTIPS[mode];
  3380. });
  3381. this.setHelpUrl("");
  3382. }
  3383. };
  3384. Blockly.Python['ai_vision_apriltag_get_info'] = function (block) {
  3385. // TODO: Assemble Python into code variable.
  3386. var code = `for i in findApriltags:
  3387. canvas.draw_string((i["x"]),(i["y"]), (i["id"]), scale = 1, color = (255,0,0) , thickness = 1)
  3388. canvas.draw_rectangle((i["x"]),(i["y"]), (i["x"])+(i["w"]),(i["y"])+ (i["h"]), color=(255,0,0), thickness=1)
  3389. `;
  3390. // TODO: Change ORDER_NONE to the correct strength.
  3391. return code;
  3392. };
  3393. Blockly.Blocks['ai_vision_find_datamatrices'] = {
  3394. init: function () {
  3395. this.appendDummyInput()
  3396. .appendField(Blockly.Msg.ai_vision_get_canvas + Blockly.Msg.ai_vision_apriltag_detected_result);
  3397. this.setOutput(true, null);
  3398. this.setColour("#f0983e");
  3399. this.setTooltip(Blockly.Msg.ai_vision_find_datamatrices_TOOLTIP);
  3400. this.setHelpUrl("");
  3401. }
  3402. };
  3403. Blockly.Python['ai_vision_find_datamatrices'] = function (block) {
  3404. Blockly.Python.addFunction("getApriltagResult", `def getApriltagResult():
  3405. global findApriltags
  3406. if len(findApriltags):
  3407. return findApriltags[0]["id"]
  3408. else:
  3409. return ""
  3410. )`)
  3411. var code = `getApriltagResult()`;
  3412. // TODO: Change ORDER_NONE to the correct strength.
  3413. return [code, Blockly.Python.ORDER_NONE];
  3414. };
  3415. Blockly.Blocks['ai_vision_datamatrices_get_info'] = {
  3416. init: function () {
  3417. this.appendDummyInput()
  3418. .appendField(Blockly.Msg.ai_vision_from)
  3419. .appendField(new Blockly.FieldVariable("i"), "varitem")
  3420. .appendField(Blockly.Msg.ai_vision_data_matrices_get_result)
  3421. .appendField(new Blockly.FieldDropdown([
  3422. [Blockly.Msg.ai_vision_pattern_detection_content, "payload"],
  3423. [Blockly.Msg.ai_vision_pattern_detection_rows, "rows"],
  3424. [Blockly.Msg.ai_vision_pattern_detection_cols, "columns"],
  3425. [Blockly.Msg.ai_vision_pattern_detection_can_tolerate_count, "capacity"],
  3426. [Blockly.Msg.ai_vision_pattern_detection_can_not_tolerate_count, "padding"],
  3427. [Blockly.Msg.ai_vision_pattern_detection_x, "x"],
  3428. [Blockly.Msg.ai_vision_pattern_detection_y, "y"],
  3429. [Blockly.Msg.ai_vision_pattern_detection_w, "w"],
  3430. [Blockly.Msg.ai_vision_pattern_detection_h, "h"]
  3431. ]), "get_info");
  3432. this.setInputsInline(false);
  3433. this.setOutput(true, null);
  3434. this.setColour("#f0983e");
  3435. var thisBlock = this;
  3436. this.setTooltip(function () {
  3437. var mode = thisBlock.getFieldValue('get_info');
  3438. var TOOLTIPS = {
  3439. 'payload': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
  3440. 'rows': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_rows),
  3441. 'columns': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_cols),
  3442. 'capacity': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_can_tolerate_count),
  3443. 'padding': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_can_not_tolerate_count),
  3444. 'x': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_x),
  3445. 'y': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_y),
  3446. 'w': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_w),
  3447. 'h': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_h)
  3448. };
  3449. return TOOLTIPS[mode];
  3450. });
  3451. this.setHelpUrl("");
  3452. }
  3453. };
  3454. Blockly.Python['ai_vision_datamatrices_get_info'] = function (block) {
  3455. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  3456. var dropdown_name = block.getFieldValue('get_info');
  3457. // TODO: Assemble Python into code variable.
  3458. var code = variable_name + '.' + dropdown_name + '()';
  3459. // TODO: Change ORDER_NONE to the correct strength.
  3460. return [code, Blockly.Python.ORDER_NONE];
  3461. };
  3462. Blockly.Blocks['ai_vision_find_barcodes'] = {
  3463. init: function () {
  3464. this.appendDummyInput()
  3465. .appendField(new Blockly.FieldImage("blockly/media/ai_vision_barcode_.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  3466. this.appendDummyInput()
  3467. .appendField(Blockly.Msg.ai_vision_barcode_detected_result)
  3468. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  3469. // .appendField();
  3470. this.setNextStatement(true);
  3471. this.setPreviousStatement(true);
  3472. this.setColour("#f0983e");
  3473. this.setTooltip(Blockly.Msg.ai_vision_find_barcodes_TOOLTIP);
  3474. this.setHelpUrl("");
  3475. }
  3476. };
  3477. Blockly.Python['ai_vision_find_barcodes'] = function (block) {
  3478. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  3479. // TODO: Assemble Python into code variable.
  3480. Blockly.Python.addVariable('findBarcodes', "findBarcodes = []", true);
  3481. var code = 'findBarcodes = canvas.find_barcodes() \n';
  3482. // TODO: Change ORDER_NONE to the correct strength.
  3483. return code;
  3484. };
  3485. Blockly.Blocks['ai_vision_barcodes_get_info'] = {
  3486. init: function () {
  3487. this.appendDummyInput()
  3488. .appendField("屏幕显示条形码")
  3489. this.setInputsInline(false);
  3490. this.setOutput(false, null);
  3491. this.setNextStatement(true);
  3492. this.setPreviousStatement(true);
  3493. this.setColour("#f0983e");
  3494. var thisBlock = this;
  3495. this.setTooltip(function () {
  3496. var mode = thisBlock.getFieldValue('get_info');
  3497. var TOOLTIPS = {
  3498. 'payload': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
  3499. 'corners': Blockly.Msg.ai_models_face_model_rect.replace('%1', Blockly.Msg.ai_models_face_model_rect),
  3500. 'x': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_x),
  3501. 'y': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_y),
  3502. 'w': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_w),
  3503. 'h': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_h)
  3504. };
  3505. return TOOLTIPS[mode];
  3506. });
  3507. this.setHelpUrl("");
  3508. }
  3509. };
  3510. Blockly.Python['ai_vision_barcodes_get_info'] = function (block) {
  3511. var dropdown_name = block.getFieldValue('get_info');
  3512. // TODO: Assemble Python into code variable.
  3513. let code = `for i in findBarcodes:
  3514. canvas.draw_string((i["corners"][0][0]),(i["corners"][0][1]), (i["payload"]), scale = 1, color = (255,0,0) , thickness = 1)
  3515. x1,y1 = i['corners'][0] #访问字典的列表
  3516. x2,y2 = i['corners'][1]
  3517. x3,y3 = i['corners'][2]
  3518. x4,y4 = i['corners'][3]
  3519. canvas.draw_line(x1, y1, x2, y2, color = (0, 255, 0), thickness = 3)
  3520. canvas.draw_line(x2, y2, x3, y3, color = (0, 255, 0), thickness = 3)
  3521. canvas.draw_line(x3, y3, x4, y4, color = (0, 255, 0), thickness = 3)
  3522. canvas.draw_line(x4, y4, x1, y1, color = (0, 255, 0), thickness = 3)
  3523. `
  3524. return code
  3525. };
  3526. Blockly.Blocks['ai_vision_barcodes_get_info_result'] = {
  3527. init: function () {
  3528. this.appendDummyInput()
  3529. .appendField(Blockly.Msg.uav_bar_code)
  3530. this.setInputsInline(false);
  3531. this.setOutput(true, null);
  3532. this.setColour("#f0983e");
  3533. this.setTooltip(Blockly.Msg.uav_bar_code);
  3534. }
  3535. };
  3536. Blockly.Python['ai_vision_barcodes_get_info_result'] = function (block) {
  3537. Blockly.Python.addFunction("getBarcodesResult", `def getBarcodesResult():
  3538. global findBarcodes
  3539. if len(findBarcodes)>0:
  3540. return findBarcodes[0]["payload"]
  3541. else:
  3542. return ""
  3543. `)
  3544. var code = `getBarcodesResult()`;
  3545. return [code, Blockly.Python.ORDER_NONE];
  3546. };
  3547. Blockly.Blocks['ai_vision_get_circles'] = {
  3548. init: function () {
  3549. this.appendDummyInput()
  3550. .appendField(Blockly.Msg.ai_vision_from)
  3551. .appendField(new Blockly.FieldVariable("i"), "varitem")
  3552. .appendField(Blockly.Msg.ai_vision_find_circle_detected_result)
  3553. .appendField(new Blockly.FieldDropdown([
  3554. [Blockly.Msg.ai_vision_find_circle_detected_x, "x()"],
  3555. [Blockly.Msg.ai_vision_find_circle_detected_y, "y()"],
  3556. [Blockly.Msg.ai_vision_find_circle_detected_radius, "r()"]
  3557. ]), "blob_type");
  3558. this.setInputsInline(false);
  3559. this.setOutput(true, null);
  3560. this.setColour("#f0983e");
  3561. this.setTooltip(Blockly.Msg.ai_vision_get_circles_TOOLTIP);
  3562. this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
  3563. }
  3564. };
  3565. Blockly.Python['ai_vision_get_circles'] = function (block) {
  3566. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  3567. var type = block.getFieldValue('blob_type');
  3568. var code = variable_name + '.' + type;
  3569. return [code, Blockly.Python.ORDER_NONE];
  3570. };
  3571. Blockly.Blocks['ai_vision_get_rects'] = {
  3572. init: function () {
  3573. this.appendDummyInput()
  3574. .appendField(Blockly.Msg.ai_vision_from)
  3575. .appendField(new Blockly.FieldVariable("i"), "varitem")
  3576. .appendField(Blockly.Msg.ai_vision_find_rect_detected_result)
  3577. .appendField(new Blockly.FieldDropdown([
  3578. [Blockly.Msg.ai_vision_find_rect_detected_x, "x()"],
  3579. [Blockly.Msg.ai_vision_find_rect_detected_y, "y()"],
  3580. [Blockly.Msg.ai_vision_find_rect_detected_w, "w()"],
  3581. [Blockly.Msg.ai_vision_find_rect_detected_h, "h()"],
  3582. [Blockly.Msg.ai_vision_find_rect_detected_tuple, "rect()"],
  3583. [Blockly.Msg.ai_vision_find_rect_detected_corners, "corners()"]
  3584. ]), "blob_type");
  3585. this.setInputsInline(false);
  3586. this.setOutput(true, null);
  3587. this.setColour("#f0983e");
  3588. this.setTooltip(Blockly.Msg.ai_vision_get_rects_TOOLTIP);
  3589. this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
  3590. }
  3591. };
  3592. Blockly.Python['ai_vision_get_rects'] = function (block) {
  3593. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  3594. var type = block.getFieldValue('blob_type');
  3595. // TODO: Assemble Python into code variable.
  3596. var code = variable_name + '.' + type;
  3597. // TODO: Change ORDER_NONE to the correct strength.
  3598. return [code, Blockly.Python.ORDER_NONE];
  3599. };
  3600. Blockly.Blocks['ai_vision_get_lines'] = {
  3601. init: function () {
  3602. this.appendDummyInput()
  3603. .appendField(Blockly.Msg.ai_vision_from)
  3604. .appendField(new Blockly.FieldVariable("i"), "varitem")
  3605. .appendField(Blockly.Msg.ai_vision_find_line_detected_result)
  3606. .appendField(new Blockly.FieldDropdown([
  3607. [Blockly.Msg.ai_vision_find_line_detected_start_x, "x1()"],
  3608. [Blockly.Msg.ai_vision_find_line_detected_start_y, "y1()"],
  3609. [Blockly.Msg.ai_vision_find_line_detected_end_x, "x2()"],
  3610. [Blockly.Msg.ai_vision_find_line_detected_end_y, "y2()"],
  3611. [Blockly.Msg.ai_vision_find_line_detected_length, "length()"],
  3612. [Blockly.Msg.ai_vision_find_line_detected_angle, "theta()"]
  3613. ]), "blob_type");
  3614. this.setInputsInline(false);
  3615. this.setOutput(true, null);
  3616. this.setColour("#f0983e");
  3617. this.setTooltip(Blockly.Msg.ai_vision_get_lines_TOOLTIP);
  3618. this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
  3619. }
  3620. };
  3621. Blockly.Python['ai_vision_get_lines'] = function (block) {
  3622. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  3623. var type = block.getFieldValue('blob_type');
  3624. // TODO: Assemble Python into code variable.
  3625. var code = variable_name + '.' + type;
  3626. // TODO: Change ORDER_NONE to the correct strength.
  3627. return [code, Blockly.Python.ORDER_NONE];
  3628. };
  3629. /*
  3630. _ ___ __ __ _ _
  3631. / \ |_ _| | \/ (_) ___ _ __ ___ _ __ | |__ ___ _ __ ___
  3632. / _ \ | | | |\/| | |/ __| '__/ _ \| '_ \| '_ \ / _ \| '_ \ / _ \
  3633. / ___ \ | | | | | | | (__| | | (_) | |_) | | | | (_) | | | | __/
  3634. /_/ \_\___| |_| |_|_|\___|_| \___/| .__/|_| |_|\___/|_| |_|\___|
  3635. |_|
  3636. */
  3637. Blockly.Blocks['ai_mphone_init'] = {
  3638. init: function () {
  3639. this.appendDummyInput()
  3640. .appendField(new Blockly.FieldImage("blockly/media/microphone_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  3641. this.appendDummyInput()
  3642. .appendField(Blockly.Msg.ai_microphone_init);
  3643. this.setPreviousStatement(true);
  3644. this.setNextStatement(true);
  3645. this.setColour("#5fcd8e");
  3646. this.setTooltip(Blockly.Msg.ai_mphone_init_TOOLTIP);
  3647. this.setHelpUrl("");
  3648. }
  3649. };
  3650. Blockly.Python.ai_mphone_init = function (block) {
  3651. Blockly.Python.definitions_.import_all_mphone = "" +
  3652. "from Maix import GPIO, I2S, FFT\n" +
  3653. "from board import board_info\n" +
  3654. "from fpioa_manager import fm\n" +
  3655. "\n" +
  3656. "sample_rate = 38640\n" +
  3657. "sample_points = 1024\n" +
  3658. "fft_points = 512\n" +
  3659. "hist_x_num = 50\n" +
  3660. "\n" +
  3661. "fm.register(20,fm.fpioa.I2S0_IN_D0, force=True)\n" +
  3662. "fm.register(19,fm.fpioa.I2S0_WS, force=True)\n" +
  3663. "fm.register(18,fm.fpioa.I2S0_SCLK, force=True)\n" +
  3664. "\n" +
  3665. "_recorder_rx = I2S(I2S.DEVICE_0)\n" +
  3666. "_recorder_rx.channel_config(_recorder_rx.CHANNEL_0, _recorder_rx.RECEIVER, align_mode = I2S.STANDARD_MODE)\n" +
  3667. "_recorder_rx.set_sample_rate(sample_rate)\n" +
  3668. "def _microphone_read_average(lst): \n" +
  3669. " return int((sum(lst)/len(lst))*100)\n" +
  3670. "";
  3671. // Blockly.Python.addVariable('sample_rate', "sample_rate = 38640", true);
  3672. // Blockly.Python.addVariable('sample_points', "sample_points = 1024", true);
  3673. // Blockly.Python.addVariable('fft_points', "fft_points = 512", true);
  3674. // Blockly.Python.addVariable('hist_x_num', "hist_x_num = 50", true);
  3675. // Blockly.Python.addVariable('_recorder_rx', "_recorder_rx = None", true);
  3676. // Blockly.Python.addVariable('fft_res', "fft_res = None", true);
  3677. // Blockly.Python.addVariable('fft_amp', "fft_amp = None", true);
  3678. Blockly.Python.addVariable('read_all_channel', "read_all_channel = [0, 0, 0, 0, 0, 0, 0, 0]", true);
  3679. // Blockly.Python.addVariable('_recorder_rx', "", true);
  3680. var _code = "" +
  3681. "";
  3682. return _code;
  3683. }
  3684. Blockly.Blocks['mphone_audio_spectrum_init'] = {
  3685. init: function () {
  3686. this.appendDummyInput()
  3687. .appendField(new Blockly.FieldImage("blockly/media/ai_audio_spectrum.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  3688. this.appendDummyInput()
  3689. .appendField(Blockly.Msg.ai_microphone_start_spectrum_analyze);
  3690. this.setPreviousStatement(true, null);
  3691. this.setNextStatement(true, null);
  3692. this.setColour("#5fcd8e");
  3693. this.setTooltip(Blockly.Msg.mphone_audio_spectrum_init_TOOLTIP);
  3694. this.setHelpUrl("");
  3695. }
  3696. };
  3697. Blockly.Python['mphone_audio_spectrum_init'] = function (block) {
  3698. // TODO: Assemble Python into code variable.
  3699. Blockly.Python.definitions_['v831_import_time'] = `import time`
  3700. var code = "" +
  3701. "time.sleep_ms(10)\n" +
  3702. "_audio = _recorder_rx.record(sample_points)\n" +
  3703. "fft_res = FFT.run(_audio.to_bytes(),fft_points)\n" +
  3704. "fft_amp = FFT.amplitude(fft_res)\n\n" +
  3705. "read_all_channel[0] = int(_microphone_read_average(fft_amp[0:63]))\n" +
  3706. "read_all_channel[1] = int(_microphone_read_average(fft_amp[64:127]))\n" +
  3707. "read_all_channel[2] = int(_microphone_read_average(fft_amp[127:191]))\n" +
  3708. "read_all_channel[3] = int(_microphone_read_average(fft_amp[192:255]))\n" +
  3709. "read_all_channel[4] = int(_microphone_read_average(fft_amp[256:319]))\n" +
  3710. "read_all_channel[5] = int(_microphone_read_average(fft_amp[320:383]))\n" +
  3711. "read_all_channel[6] = int(_microphone_read_average(fft_amp[384:447]))\n" +
  3712. "read_all_channel[7] = int(_microphone_read_average(fft_amp[448:514]))\n" +
  3713. "";
  3714. return code;
  3715. };
  3716. Blockly.Blocks['ai_mphone_read_sound_sensitivity'] = {
  3717. init: function () {
  3718. this.appendDummyInput()
  3719. .appendField(Blockly.Msg.ai_microphone_get_spectrum_analyze_result)
  3720. .appendField(new Blockly.FieldDropdown([
  3721. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_1, "0"],
  3722. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_2, "1"],
  3723. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_3, "2"],
  3724. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_4, "3"],
  3725. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_5, "4"],
  3726. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_6, "5"],
  3727. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_7, "6"],
  3728. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_8, "7"]
  3729. ]), "level");
  3730. this.setOutput(true, null);
  3731. this.setColour("#5fcd8e");
  3732. this.setTooltip(Blockly.Msg.ai_mphone_read_sound_sensitivity_TOOLTIP);
  3733. this.setHelpUrl("");
  3734. }
  3735. };
  3736. Blockly.Python['ai_mphone_read_sound_sensitivity'] = function (block) {
  3737. var dropdown_level = block.getFieldValue('level');
  3738. // TODO: Assemble Python into code variable.
  3739. var code = 'read_all_channel[' + dropdown_level + ']';
  3740. // TODO: Change ORDER_NONE to the correct strength.
  3741. return [code, Blockly.Python.ORDER_NONE];
  3742. };
  3743. Blockly.Blocks['ai_mphone_Record_audio_init'] = {
  3744. init: function () {
  3745. this.appendDummyInput()
  3746. .appendField(new Blockly.FieldImage("blockly/media/microphone_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  3747. this.appendDummyInput()
  3748. .appendField(Blockly.Msg.ai_mphone_Record_audio_init);
  3749. this.setPreviousStatement(true);
  3750. this.setNextStatement(true);
  3751. this.setColour("#5fcd8e");
  3752. this.setTooltip(Blockly.Msg.ai_mphone_Record_audio_init_TOOLTIP);
  3753. this.setHelpUrl("");
  3754. }
  3755. };
  3756. Blockly.Python.ai_mphone_Record_audio_init = function (block) {
  3757. Blockly.Python.definitions_.import_ai_mphone_Record_audio_init = "" +
  3758. "from Maix import GPIO, I2S\n" +
  3759. "from fpioa_manager import fm\n" +
  3760. 'import audio\n' +
  3761. "\n" +
  3762. '# user setting\n' +
  3763. "sample_rate = 16000\n" +
  3764. "# default seting\n" +
  3765. "sample_points = 2048\n" +
  3766. "wav_ch = 2\n" +
  3767. "\n" +
  3768. 'fm.register(8, fm.fpioa.GPIO0, force=True)\n' +
  3769. 'wifi_en = GPIO(GPIO.GPIO0, GPIO.OUT)\n' +
  3770. 'wifi_en.value(0)\n' +
  3771. "\n" +
  3772. "fm.register(20,fm.fpioa.I2S0_IN_D0, force=True)\n" +
  3773. "fm.register(19,fm.fpioa.I2S0_WS, force=True)\n" +
  3774. "fm.register(18,fm.fpioa.I2S0_SCLK, force=True)\n" +
  3775. "\n" +
  3776. "rx = I2S(I2S.DEVICE_0)\n" +
  3777. "rx.channel_config(rx.CHANNEL_0, rx.RECEIVER, align_mode=I2S.STANDARD_MODE)\n" +
  3778. "rx.set_sample_rate(sample_rate)\n" +
  3779. "print(rx)\n" +
  3780. "";
  3781. var _code = "" +
  3782. "";
  3783. return _code;
  3784. }
  3785. Blockly.Blocks['ai_mphone_record_init'] = {
  3786. init: function () {
  3787. this.appendDummyInput()
  3788. .appendField(Blockly.Msg.ai_microphone_init);
  3789. this.setPreviousStatement(true);
  3790. this.setNextStatement(true, null);
  3791. this.setColour("#5fcd8e");
  3792. this.setTooltip(Blockly.Msg.ai_microphone_init);
  3793. this.setHelpUrl("");
  3794. }
  3795. }
  3796. Blockly.Python['ai_mphone_record_init'] = function (block) {
  3797. Blockly.Python.definitions_['v831_import_pyaudio'] = `import pyaudio`
  3798. Blockly.Python.definitions_['v831_import_wave'] = `import wave`
  3799. Blockly.Python.definitions_['v831_import_os'] = `import os`
  3800. let code = ''
  3801. return code;
  3802. }
  3803. Blockly.Blocks['ai_mphone_record_setting'] = {
  3804. init: function () {
  3805. this.appendValueInput("record_time").setCheck(null)
  3806. .appendField(Blockly.Msg.ai_mphone_Record_audio_start_time)
  3807. this.appendValueInput("path")
  3808. .setCheck(null)
  3809. .appendField(Blockly.Msg.ai_mphone_Record_audio_start_path);
  3810. this.appendDummyInput()
  3811. .appendField(Blockly.Msg.ai_mphone_Record_audio_start);
  3812. this.setPreviousStatement(true);
  3813. this.setNextStatement(true, null);
  3814. this.setInputsInline(false);
  3815. this.setColour("#5fcd8e");
  3816. this.setTooltip(Blockly.Msg.ai_mphone_Record_audio_start);
  3817. this.setHelpUrl("");
  3818. }
  3819. }
  3820. Blockly.Python['ai_mphone_record_setting'] = function (block) {
  3821. var value_path = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  3822. var record_time = Blockly.Python.valueToCode(block, 'record_time', Blockly.Python.ORDER_ATOMIC);
  3823. Blockly.Python.definitions_['v831_import_pyaudio'] = `import pyaudio`
  3824. Blockly.Python.definitions_['v831_import_wave'] = `import wave`
  3825. Blockly.Python.definitions_['v831_import_os'] = `import os`
  3826. let code = `if(os.path.exists(${value_path})):
  3827. os.system("rm ${value_path.slice(1, value_path.length - 1)}")
  3828. CHUNK = 1024
  3829. FORMAT = pyaudio.paInt16
  3830. CHANNELS = 1
  3831. RATE = 16000
  3832. RECORD_SECONDS = ${Number(record_time) + 1}
  3833. WAVE_OUTPUT_FILENAME = ${value_path}
  3834. p = pyaudio.PyAudio()
  3835. stream = p.open(format=FORMAT,channels=CHANNELS,rate=RATE,input=True,frames_per_buffer=CHUNK)
  3836. print("* recording")
  3837. frames = []
  3838. for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
  3839. data = stream.read(CHUNK,exception_on_overflow = False)
  3840. frames.append(data)
  3841. print("* done recording")
  3842. stream.stop_stream()
  3843. stream.close()
  3844. p.terminate()
  3845. wf = wave.open(WAVE_OUTPUT_FILENAME, "wb")
  3846. wf.setnchannels(CHANNELS)
  3847. wf.setsampwidth(p.get_sample_size(FORMAT))
  3848. wf.setframerate(RATE)
  3849. wf.writeframes(b"".join(frames))
  3850. wf.close()
  3851. `
  3852. return code;
  3853. }
  3854. Blockly.Blocks['ai_mphone_Record_audio_start'] = {
  3855. init: function () {
  3856. this.appendDummyInput()
  3857. .appendField(Blockly.Msg.ai_mphone_Record_audio_start);
  3858. this.setPreviousStatement(true);
  3859. this.setNextStatement(true, null);
  3860. this.setColour("#5fcd8e");
  3861. this.setTooltip(Blockly.Msg.ai_mphone_Record_audio_start_TOOLTIP);
  3862. this.setHelpUrl("");
  3863. }
  3864. };
  3865. Blockly.Python['ai_mphone_Record_audio_start'] = function (block) {
  3866. var code = `
  3867. print("* recording")
  3868. frames = []
  3869. for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
  3870. data = stream.read(CHUNK,exception_on_overflow = False)
  3871. frames.append(data)
  3872. print("* done recording")
  3873. stream.stop_stream()
  3874. stream.close()
  3875. p.terminate()
  3876. wf = wave.open(WAVE_OUTPUT_FILENAME, "wb")
  3877. wf.setnchannels(CHANNELS)
  3878. wf.setsampwidth(p.get_sample_size(FORMAT))
  3879. wf.setframerate(RATE)
  3880. wf.writeframes(b"".join(frames))
  3881. wf.close()
  3882. `;
  3883. return code;
  3884. };
  3885. Blockly.Blocks['ai_audio_start_recording'] = {
  3886. init: function () {
  3887. this.appendDummyInput()
  3888. .appendField(Blockly.Msg.ai_audio_play_music_file);
  3889. this.appendValueInput("path")
  3890. .setCheck(null)
  3891. .appendField(Blockly.Msg.ai_audio_play_music_file_set_file_path);
  3892. this.setPreviousStatement(true, null);
  3893. this.setNextStatement(true, null);
  3894. this.setColour("#5fcd8e");
  3895. this.setTooltip(Blockly.Msg.ai_audio_stop_recording_TOOLTIP);
  3896. this.setHelpUrl("");
  3897. }
  3898. };
  3899. Blockly.Python['ai_audio_start_recording'] = function (block) {
  3900. // TODO: Assemble Python into code variable.
  3901. Blockly.Python.definitions_['v831_import_pyaudio'] = `import pyaudio`
  3902. Blockly.Python.definitions_['v831_import_wave'] = `import wave`
  3903. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  3904. sys.path.append("/root/")`
  3905. var code = `
  3906. CHUNK = 1024
  3907. wf = wave.open(r"test.wav", "rb")#(sys.argv[1], "rb"
  3908. p = pyaudio.PyAudio()
  3909. stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
  3910. channels=wf.getnchannels(),
  3911. rate=wf.getframerate(),
  3912. output=True)
  3913. data = wf.readframes(CHUNK)
  3914. while len(data) > 0:
  3915. stream.write(data)
  3916. data = wf.readframes(CHUNK)
  3917. stream.stop_stream()
  3918. stream.close()
  3919. p.terminate()
  3920. `;
  3921. return code;
  3922. };
  3923. /*
  3924. _ ___ _ _ _
  3925. / \ |_ _| / \ _ _ __| (_) ___
  3926. / _ \ | | / _ \| | | |/ _` | |/ _ \
  3927. / ___ \ | | / ___ \ |_| | (_| | | (_) |
  3928. /_/ \_\___| /_/ \_\__,_|\__,_|_|\___/
  3929. */
  3930. Blockly.Blocks['ai_audio_play_init'] = {
  3931. init: function () {
  3932. this.appendDummyInput()
  3933. .appendField(Blockly.Msg.AUDIO_S);
  3934. this.setPreviousStatement(true, null);
  3935. this.setNextStatement(true, null);
  3936. this.setColour("#603ea0");
  3937. this.setTooltip("");
  3938. this.setHelpUrl("");
  3939. }
  3940. };
  3941. Blockly.Python['ai_audio_play_init'] = function (block) {
  3942. Blockly.Python.definitions_['v831_import_os'] = `import os`
  3943. Blockly.Python.definitions_['v831_import_pyaudio'] = `import pyaudio`
  3944. Blockly.Python.definitions_['v831_import_wave'] = `import wave`
  3945. Blockly.Python.definitions_['v831_import_time'] = `import time`
  3946. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  3947. sys.path.append("/root/")`
  3948. Blockly.Python.addVariable("VOICESTATE", "VOICESTATE = 0", true)
  3949. Blockly.Python.addVariable("VOICEDATA", "", true)
  3950. Blockly.Python.addVariable("VOICESTREAM", 'VOICESTREAM = ""', true)
  3951. Blockly.Python.addVariable("VOICEPYAUDIO", "VOICEPYAUDIO = pyaudio.PyAudio()", true)
  3952. Blockly.Python.addVariable("CHUNK", "CHUNK = 1024", true)
  3953. Blockly.Python.addVariable("VOICEPPATH", "", true)
  3954. Blockly.Python.addVariable("VOICEPLAYSTATE", 'VOICEPLAYSTATE = True', true)
  3955. Blockly.Python.addVariable("VOICEWF", 'VOICEWF = ""', true)
  3956. var _code = ``;
  3957. return _code;
  3958. };
  3959. Blockly.Blocks['ai_audio_play_volume'] = {
  3960. init: function () {
  3961. this.appendDummyInput()
  3962. .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol)
  3963. .appendField(new Blockly.FieldNumber(25, 0, 100), "play_vol")
  3964. .appendField("(0~100)");
  3965. this.setPreviousStatement(true, null);
  3966. this.setNextStatement(true, null);
  3967. this.setColour("#603ea0");
  3968. this.setTooltip("");
  3969. this.setHelpUrl("");
  3970. }
  3971. };
  3972. Blockly.Python['ai_audio_play_volume'] = function (block) {
  3973. var number_play_vol = block.getFieldValue('play_vol');
  3974. Blockly.Python.definitions_['systemVoice_numberMap'] = `def voice_numberMap(value):
  3975. valueScaled = float(value - 0) / float(100)
  3976. return int(valueScaled * 31)
  3977. `
  3978. var _code = `VOICENUMP = str(voice_numberMap(${number_play_vol}))
  3979. time.sleep(0.01)
  3980. SYSTEMVOICE = "amixer cset numid=8,iface=MIXER,name=\\"LINEOUT volume\\" "+ VOICENUMP+""
  3981. `;
  3982. return _code;
  3983. };
  3984. Blockly.Blocks['ai_audio_play_song'] = {
  3985. init: function () {
  3986. this.appendValueInput("path")
  3987. .setCheck(null)
  3988. .appendField(Blockly.Msg.ai_audio_play_music_file_set_file_path)
  3989. this.appendDummyInput()
  3990. .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol)
  3991. .appendField(new Blockly.FieldNumber(25, 0, 100), "play_vol")
  3992. .appendField("(0~100)");
  3993. this.appendDummyInput()
  3994. .appendField(Blockly.Msg.ai_audio_start_play);
  3995. this.setPreviousStatement(true, null);
  3996. this.setNextStatement(true, null);
  3997. this.setColour("#603ea0");
  3998. this.setTooltip(Blockly.Msg.ai_audio_start_play);
  3999. this.setHelpUrl("");
  4000. }
  4001. };
  4002. Blockly.Python['ai_audio_play_song'] = function (block) {
  4003. var path_to_video = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  4004. var number_play_vol = block.getFieldValue('play_vol');
  4005. Blockly.Python.definitions_['v831_import_os'] = `import os`
  4006. Blockly.Python.definitions_['v831_import_pyaudio'] = `import pyaudio`
  4007. Blockly.Python.definitions_['v831_import_wave'] = `import wave`
  4008. Blockly.Python.definitions_['v831_import_time'] = `import time`
  4009. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  4010. sys.path.append("/root/")`
  4011. Blockly.Python.addVariable("CHUNK", "CHUNK = 1024", true)
  4012. Blockly.Python.definitions_['systemVoice_numberMap'] = `def voice_numberMap(value):
  4013. valueScaled = float(value - 0) / float(100)
  4014. return int(valueScaled * 31)
  4015. VOICENUMP = str(voice_numberMap(${number_play_vol}))
  4016. time.sleep(0.01)
  4017. SYSTEMVOICE = "amixer cset numid=8,iface=MIXER,name=\\"LINEOUT volume\\" "+ VOICENUMP+""
  4018. os.system(SYSTEMVOICE)
  4019. `
  4020. // Blockly.Python.addVariable("VOICEPPATH","",false)
  4021. var _code = `
  4022. wf = wave.open(${path_to_video}, "rb")#(sys.argv[1], "rb"
  4023. p = pyaudio.PyAudio()
  4024. stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),channels=wf.getnchannels(),rate=wf.getframerate(),output=True)
  4025. data = wf.readframes(CHUNK)
  4026. while len(data) > 0:
  4027. stream.write(data)
  4028. data = wf.readframes(CHUNK)
  4029. stream.stop_stream()
  4030. stream.close()
  4031. p.terminate()
  4032. `;
  4033. return _code;
  4034. };
  4035. Blockly.Blocks['ai_audio_play_start'] = {
  4036. init: function () {
  4037. this.appendDummyInput()
  4038. .appendField(Blockly.Msg.ai_audio_start_play);
  4039. this.setPreviousStatement(true, null);
  4040. this.setNextStatement(true, null);
  4041. this.setColour("#603ea0");
  4042. this.setTooltip(Blockly.Msg.ai_audio_start_play);
  4043. this.setHelpUrl("");
  4044. }
  4045. }
  4046. Blockly.Python['ai_audio_play_start'] = function (block) {
  4047. Blockly.Python.definitions_["VoicePlayState"] = `def VoicePlayState():
  4048. global VOICESTATE,VOICEDATA,CHUNK,VOICESTREAM,VOICEPYAUDIO,VOICEWF,VOICEPPATH
  4049. if VOICESTATE == 0:
  4050. VOICEWF = wave.open(VOICEPPATH, "rb")#(sys.argv[1], "rb"
  4051. VOICEPYAUDIO = pyaudio.PyAudio()
  4052. VOICESTREAM = VOICEPYAUDIO.open(format=VOICEPYAUDIO.get_format_from_width(VOICEWF.getsampwidth()),channels=VOICEWF.getnchannels(),rate=VOICEWF.getframerate(),output=True)
  4053. VOICEDATA = VOICEWF.readframes(CHUNK)
  4054. VOICESTATE = 1
  4055. return True
  4056. else:
  4057. if len(VOICEDATA) > 0:
  4058. try:
  4059. VOICESTREAM.write(VOICEDATA)
  4060. VOICEDATA = VOICEWF.readframes(CHUNK)
  4061. return True
  4062. except:
  4063. VOICESTATE = 0
  4064. else:
  4065. VOICESTREAM.stop_stream()
  4066. VOICESTREAM.close()
  4067. VOICEPYAUDIO.terminate()
  4068. VOICESTATE = 0
  4069. return False
  4070. `
  4071. let code = `VOICEPLAYSTATE = VoicePlayState()
  4072. `
  4073. return code;
  4074. }
  4075. Blockly.Blocks['ai_audio_play_finish'] = {
  4076. init: function () {
  4077. this.appendDummyInput()
  4078. .appendField(Blockly.Msg.ai_audio_end_play);
  4079. this.setPreviousStatement(true, null);
  4080. this.setNextStatement(true, null);
  4081. this.setColour("#603ea0");
  4082. this.setTooltip("");
  4083. this.setHelpUrl("");
  4084. }
  4085. };
  4086. Blockly.Python['ai_audio_play_finish'] = function (block) {
  4087. // TODO: Assemble Python into code variable.
  4088. var code = `try:
  4089. VOICESTREAM.stop_stream()
  4090. VOICESTREAM.close()
  4091. VOICEPYAUDIO.terminate()
  4092. VOICESTATE = 0
  4093. except:
  4094. pass
  4095. `;
  4096. return code;
  4097. };
  4098. Blockly.Blocks['ai_audio_get_play_status'] = {
  4099. init: function () {
  4100. this.appendDummyInput()
  4101. .appendField(Blockly.Msg.ai_audio_get_play_status);
  4102. this.setOutput(true, null);
  4103. this.setColour("#603ea0");
  4104. this.setTooltip(Blockly.Msg.ai_audio_play_state_speaker_TOOLTIP);
  4105. this.setHelpUrl("");
  4106. }
  4107. };
  4108. Blockly.Python['ai_audio_get_play_status'] = function (block) {
  4109. // TODO: Assemble Python into code variable.
  4110. var code = 'VOICEPLAYSTATE';
  4111. // TODO: Change ORDER_NONE to the correct strength.
  4112. return [code, Blockly.Python.ORDER_NONE];
  4113. };
  4114. // 在线语音识别
  4115. Blockly.Blocks['ai_online_voice_init'] = {
  4116. init: function () {
  4117. this.appendDummyInput()
  4118. .appendField(Blockly.Msg.online_voice_init); //
  4119. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  4120. this.appendValueInput("B").setCheck(null).appendField('API Secret');
  4121. this.appendValueInput("C").setCheck(null).appendField('API Key'); //Blockly.Msg.online_voice_init_path
  4122. this.setPreviousStatement(true, null);
  4123. this.setNextStatement(true, null);
  4124. this.setColour("#0000ff");
  4125. this.setTooltip(Blockly.Msg.online_voice_init);
  4126. this.setHelpUrl("");
  4127. }
  4128. };
  4129. Blockly.Python['ai_online_voice_init'] = function (block) {
  4130. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  4131. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  4132. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  4133. // TODO: Assemble Python into code variable.
  4134. Blockly.Python.definitions_['v831_import_time'] = `import time`
  4135. Blockly.Python.definitions_['v831_import_websocket'] = `import websocket`
  4136. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  4137. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  4138. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  4139. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  4140. Blockly.Python.definitions_['v831_import_json'] = `import json`
  4141. Blockly.Python.definitions_['v831_import_urlencode'] = `from urllib.parse import urlencode`
  4142. Blockly.Python.definitions_['v831_import_ssl'] = `import ssl`
  4143. Blockly.Python.definitions_['v831_import_format_date_time'] = `from wsgiref.handlers import format_date_time`
  4144. Blockly.Python.definitions_['v831_import_from_time_mktime'] = `from time import mktime`
  4145. Blockly.Python.definitions_['v831_import_thread_as_thread'] = `import _thread as thread`
  4146. Blockly.Python.definitions_.ai_online_voice_init = `
  4147. STATUS_FIRST_FRAME = 0 # 第一帧的标识
  4148. STATUS_CONTINUE_FRAME = 1 # 中间帧标识
  4149. STATUS_LAST_FRAME = 2 # 最后一帧的标识
  4150. class Ws_Param_voice(object):
  4151. # 初始化
  4152. def __init__(self, APPID, APIKey, APISecret, AudioFile):
  4153. self.APPID_voice = APPID
  4154. self.APIKey_voice = APIKey
  4155. self.APISecret_voice = APISecret
  4156. self.AudioFile_voice = AudioFile
  4157. # 公共参数(common)
  4158. self.CommonArgs = {"app_id": self.APPID_voice}
  4159. # 业务参数(business),更多个性化参数可在官网查看
  4160. self.BusinessArgs = {"domain": "iat", "language": "zh_cn", "accent": "mandarin", "vinfo":1,"vad_eos":10000}
  4161. # 生成url
  4162. def create_url_voice(self):
  4163. url = "wss://ws-api.xfyun.cn/v2/iat"
  4164. # 生成RFC1123格式的时间戳
  4165. now = datetime.now()
  4166. date = format_date_time(mktime(now.timetuple()))
  4167. # 拼接字符串
  4168. signature_origin = "host: " + "ws-api.xfyun.cn" + "\\n"
  4169. signature_origin += "date: " + date + "\\n"
  4170. signature_origin += "GET " + "/v2/iat " + "HTTP/1.1"
  4171. # 进行hmac-sha256进行加密
  4172. signature_sha = hmac.new(self.APISecret_voice .encode("utf-8"), signature_origin.encode("utf-8"),
  4173. digestmod=hashlib.sha256).digest()
  4174. signature_sha = base64.b64encode(signature_sha).decode(encoding="utf-8")
  4175. authorization_origin = "api_key=\\"%s\\", algorithm=\\"%s\\", headers=\\"%s\\", signature=\\"%s\\"" % (self.APIKey_voice , "hmac-sha256", "host date request-line", signature_sha)
  4176. authorization = base64.b64encode(authorization_origin.encode("utf-8")).decode(encoding="utf-8")
  4177. # 将请求的鉴权参数组合为字典
  4178. v = {
  4179. "authorization": authorization,
  4180. "date": date,
  4181. "host": "ws-api.xfyun.cn"
  4182. }
  4183. # 拼接鉴权参数,生成url
  4184. url = url + "?" + urlencode(v)
  4185. # print("date: ",date)
  4186. # print("v: ",v)
  4187. # 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释,比对相同参数时生成的url与自己代码生成的url是否一致
  4188. # print('websocket url :', url)
  4189. return url
  4190. # 收到websocket消息的处理
  4191. def on_message_voice(ws, message):
  4192. global asr_result,txt_result
  4193. try:
  4194. code = json.loads(message)["code"]
  4195. sid = json.loads(message)["sid"]
  4196. if code != 0:
  4197. errMsg = json.loads(message)["message"]
  4198. print("sid:%s call error:%s code is:%s" % (sid, errMsg, code))
  4199. else:
  4200. data = json.loads(message)["data"]["result"]["ws"]
  4201. # print(json.loads(message))
  4202. result = ""
  4203. for i in data:
  4204. for w in i["cw"]:
  4205. result += w["w"]
  4206. print("sid:%s call success!,data is:%s" % (sid, json.dumps(data, ensure_ascii=False)))
  4207. print("json.dumps(data, ensure_ascii=False)",eval(json.dumps(data, ensure_ascii=False)))
  4208. asr_result.append(eval(json.dumps(data, ensure_ascii=False)))
  4209. txt_result.append(eval(json.dumps(data, ensure_ascii=False)[1]))
  4210. except Exception as e:
  4211. print("receive msg,but parse exception:", e)
  4212. # 收到websocket错误的处理
  4213. def on_error_voice(ws, error):
  4214. print("### error:", error)
  4215. # 收到websocket关闭的处理
  4216. def on_close_voice(ws):
  4217. print("### closed ###")
  4218. # 收到websocket连接建立的处理
  4219. def on_open_voice(ws):
  4220. def run_voice(*args):
  4221. global wsParam_voice
  4222. frameSize = 8000 # 每一帧的音频大小
  4223. intervel = 0.04 # 发送音频间隔(单位:s)
  4224. status = STATUS_FIRST_FRAME # 音频的状态信息,标识音频是第一帧,还是中间帧、最后一帧
  4225. with open(wsParam_voice.AudioFile_voice, "rb") as fp:
  4226. while True:
  4227. buf = fp.read(frameSize)
  4228. # 文件结束
  4229. if not buf:
  4230. status = STATUS_LAST_FRAME
  4231. # 第一帧处理
  4232. # 发送第一帧音频,带business 参数
  4233. # appid 必须带上,只需第一帧发送
  4234. if status == STATUS_FIRST_FRAME:
  4235. d = {"common": wsParam_voice.CommonArgs,
  4236. "business": wsParam_voice.BusinessArgs,
  4237. "data": {"status": 0, "format": "audio/L16;rate=16000",
  4238. "audio": str(base64.b64encode(buf), "utf-8"),
  4239. "encoding": "raw"}}
  4240. d = json.dumps(d)
  4241. ws.send(d)
  4242. status = STATUS_CONTINUE_FRAME
  4243. # 中间帧处理
  4244. elif status == STATUS_CONTINUE_FRAME:
  4245. d = {"data": {"status": 1, "format": "audio/L16;rate=16000",
  4246. "audio": str(base64.b64encode(buf), "utf-8"),
  4247. "encoding": "raw"}}
  4248. ws.send(json.dumps(d))
  4249. # 最后一帧处理
  4250. elif status == STATUS_LAST_FRAME:
  4251. d = {"data": {"status": 2, "format": "audio/L16;rate=16000",
  4252. "audio": str(base64.b64encode(buf), "utf-8"),
  4253. "encoding": "raw"}}
  4254. ws.send(json.dumps(d))
  4255. time.sleep(1)
  4256. break
  4257. # 模拟音频采样间隔
  4258. time.sleep(intervel)
  4259. ws.close()
  4260. thread.start_new_thread(run_voice, ())
  4261. asr_result=[]
  4262. txt_result=[]
  4263. APPId_voice = ${APPId}
  4264. APISecret_voice = ${APISecret}
  4265. APIKey_voice = ${APIKey}
  4266. `
  4267. var code = '';
  4268. return code;
  4269. };
  4270. // 在线语音识别加载
  4271. Blockly.Blocks['ai_online_voice_loading'] = {
  4272. init: function () {
  4273. this.appendDummyInput()
  4274. .appendField(Blockly.Msg.online_voice_loading); //
  4275. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  4276. this.appendValueInput("B").setCheck(null).appendField('API Secret');
  4277. this.appendValueInput("C").setCheck(null).appendField('API Key');
  4278. this.appendValueInput("path").setCheck(null).appendField(Blockly.Msg.online_voice_init_path);
  4279. this.setPreviousStatement(true, null);
  4280. this.setNextStatement(true, null);
  4281. this.setColour("#0000ff");
  4282. this.setTooltip(Blockly.Msg.online_voice_loading);
  4283. this.setHelpUrl("");
  4284. }
  4285. };
  4286. Blockly.Python['ai_online_voice_loading'] = function (block) {
  4287. var AudioFile = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  4288. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  4289. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  4290. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  4291. // TODO: Assemble Python into code variable.
  4292. Blockly.Python.definitions_['v831_import_time'] = `import time`
  4293. Blockly.Python.definitions_['v831_import_websocket'] = `import websocket`
  4294. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  4295. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  4296. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  4297. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  4298. Blockly.Python.definitions_['v831_import_json'] = `import json`
  4299. Blockly.Python.definitions_['v831_import_urlencode'] = `from urllib.parse import urlencode`
  4300. Blockly.Python.definitions_['v831_import_ssl'] = `import ssl`
  4301. Blockly.Python.definitions_['v831_import_format_date_time'] = `from wsgiref.handlers import format_date_time`
  4302. Blockly.Python.definitions_['v831_import_from_time_mktime'] = `from time import mktime`
  4303. Blockly.Python.definitions_['v831_import_thread_as_thread'] = `import _thread as thread`
  4304. Blockly.Python.definitions_.ai_online_voice_init = `
  4305. STATUS_FIRST_FRAME = 0 # 第一帧的标识
  4306. STATUS_CONTINUE_FRAME = 1 # 中间帧标识
  4307. STATUS_LAST_FRAME = 2 # 最后一帧的标识
  4308. class Ws_Param_voice(object):
  4309. # 初始化
  4310. def __init__(self, APPID, APIKey, APISecret, AudioFile):
  4311. self.APPID_voice = APPID
  4312. self.APIKey_voice = APIKey
  4313. self.APISecret_voice = APISecret
  4314. self.AudioFile_voice = AudioFile
  4315. # 公共参数(common)
  4316. self.CommonArgs = {"app_id": self.APPID_voice}
  4317. # 业务参数(business),更多个性化参数可在官网查看
  4318. self.BusinessArgs = {"domain": "iat", "language": "zh_cn", "accent": "mandarin", "vinfo":1,"vad_eos":10000}
  4319. # 生成url
  4320. def create_url_voice(self):
  4321. url = "wss://ws-api.xfyun.cn/v2/iat"
  4322. # 生成RFC1123格式的时间戳
  4323. now = datetime.now()
  4324. date = format_date_time(mktime(now.timetuple()))
  4325. # 拼接字符串
  4326. signature_origin = "host: " + "ws-api.xfyun.cn" + "\\n"
  4327. signature_origin += "date: " + date + "\\n"
  4328. signature_origin += "GET " + "/v2/iat " + "HTTP/1.1"
  4329. # 进行hmac-sha256进行加密
  4330. signature_sha = hmac.new(self.APISecret_voice .encode("utf-8"), signature_origin.encode("utf-8"),
  4331. digestmod=hashlib.sha256).digest()
  4332. signature_sha = base64.b64encode(signature_sha).decode(encoding="utf-8")
  4333. authorization_origin = "api_key=\\"%s\\", algorithm=\\"%s\\", headers=\\"%s\\", signature=\\"%s\\"" % (self.APIKey_voice , "hmac-sha256", "host date request-line", signature_sha)
  4334. authorization = base64.b64encode(authorization_origin.encode("utf-8")).decode(encoding="utf-8")
  4335. # 将请求的鉴权参数组合为字典
  4336. v = {
  4337. "authorization": authorization,
  4338. "date": date,
  4339. "host": "ws-api.xfyun.cn"
  4340. }
  4341. # 拼接鉴权参数,生成url
  4342. url = url + "?" + urlencode(v)
  4343. # print("date: ",date)
  4344. # print("v: ",v)
  4345. # 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释,比对相同参数时生成的url与自己代码生成的url是否一致
  4346. # print('websocket url :', url)
  4347. return url
  4348. # 收到websocket消息的处理
  4349. def on_message_voice(ws, message):
  4350. global asr_result,txt_result
  4351. try:
  4352. code = json.loads(message)["code"]
  4353. sid = json.loads(message)["sid"]
  4354. if code != 0:
  4355. errMsg = json.loads(message)["message"]
  4356. print("sid:%s call error:%s code is:%s" % (sid, errMsg, code))
  4357. else:
  4358. data = json.loads(message)["data"]["result"]["ws"]
  4359. # print(json.loads(message))
  4360. result = ""
  4361. for i in data:
  4362. for w in i["cw"]:
  4363. result += w["w"]
  4364. print("sid:%s call success!,data is:%s" % (sid, json.dumps(data, ensure_ascii=False)))
  4365. print("json.dumps(data, ensure_ascii=False)",eval(json.dumps(data, ensure_ascii=False)))
  4366. asr_result.append(eval(json.dumps(data, ensure_ascii=False)))
  4367. txt_result.append(eval(json.dumps(data, ensure_ascii=False)[1]))
  4368. except Exception as e:
  4369. print("receive msg,but parse exception:", e)
  4370. # 收到websocket错误的处理
  4371. def on_error_voice(ws, error):
  4372. print("### error:", error)
  4373. # 收到websocket关闭的处理
  4374. def on_close_voice(ws):
  4375. print("### closed ###")
  4376. # 收到websocket连接建立的处理
  4377. def on_open_voice(ws):
  4378. def run_voice(*args):
  4379. global wsParam_voice
  4380. frameSize = 8000 # 每一帧的音频大小
  4381. intervel = 0.04 # 发送音频间隔(单位:s)
  4382. status = STATUS_FIRST_FRAME # 音频的状态信息,标识音频是第一帧,还是中间帧、最后一帧
  4383. with open(wsParam_voice.AudioFile_voice, "rb") as fp:
  4384. while True:
  4385. buf = fp.read(frameSize)
  4386. # 文件结束
  4387. if not buf:
  4388. status = STATUS_LAST_FRAME
  4389. # 第一帧处理
  4390. # 发送第一帧音频,带business 参数
  4391. # appid 必须带上,只需第一帧发送
  4392. if status == STATUS_FIRST_FRAME:
  4393. d = {"common": wsParam_voice.CommonArgs,
  4394. "business": wsParam_voice.BusinessArgs,
  4395. "data": {"status": 0, "format": "audio/L16;rate=16000",
  4396. "audio": str(base64.b64encode(buf), "utf-8"),
  4397. "encoding": "raw"}}
  4398. d = json.dumps(d)
  4399. ws.send(d)
  4400. status = STATUS_CONTINUE_FRAME
  4401. # 中间帧处理
  4402. elif status == STATUS_CONTINUE_FRAME:
  4403. d = {"data": {"status": 1, "format": "audio/L16;rate=16000",
  4404. "audio": str(base64.b64encode(buf), "utf-8"),
  4405. "encoding": "raw"}}
  4406. ws.send(json.dumps(d))
  4407. # 最后一帧处理
  4408. elif status == STATUS_LAST_FRAME:
  4409. d = {"data": {"status": 2, "format": "audio/L16;rate=16000",
  4410. "audio": str(base64.b64encode(buf), "utf-8"),
  4411. "encoding": "raw"}}
  4412. ws.send(json.dumps(d))
  4413. time.sleep(1)
  4414. break
  4415. # 模拟音频采样间隔
  4416. time.sleep(intervel)
  4417. ws.close()
  4418. thread.start_new_thread(run_voice, ())
  4419. asr_result=[]
  4420. txt_result=[]
  4421. APPId_voice = ${APPId}
  4422. APISecret_voice = ${APISecret}
  4423. APIKey_voice = ${APIKey}
  4424. `
  4425. Blockly.Python.definitions_['get_online_voice_data'] = `asr_string=""
  4426. def getVoiceData():
  4427. global wsParam_voice,asr_string,txt_result,asr_result
  4428. # 测试时候在此处正确填写相关信息即可运行
  4429. asr_string = ""
  4430. txt_result = []
  4431. asr_result = []
  4432. time1 = datetime.now()
  4433. wsParam_voice = Ws_Param_voice(APPID=APPId_voice, APISecret=APISecret_voice,
  4434. APIKey=APIKey_voice,
  4435. AudioFile=${AudioFile})
  4436. websocket.enableTrace(False)
  4437. wsUrl = wsParam_voice.create_url_voice()
  4438. ws = websocket.WebSocketApp(wsUrl, on_message=on_message_voice, on_error=on_error_voice)
  4439. ws.on_open = on_open_voice
  4440. ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
  4441. time2 = datetime.now()
  4442. for i in range(len(asr_result)):
  4443. for j in range(len(asr_result[i])):
  4444. txt_result.append(asr_result[i][j]["cw"][0]["w"])
  4445. print("txt_result:", txt_result)
  4446. for i in range(len(txt_result)):
  4447. asr_string=asr_string+txt_result[i]
  4448. print("asr_string:", asr_string)
  4449. return asr_string
  4450. `
  4451. let code = `VoiceResult = getVoiceData()
  4452. `;
  4453. return code;
  4454. };
  4455. Blockly.Blocks['ai_online_voice_result'] = {
  4456. init: function () {
  4457. this.appendDummyInput()
  4458. .appendField(Blockly.Msg.online_voice_result); //
  4459. this.setOutput(true, null);
  4460. this.setColour("#0000ff");
  4461. this.setTooltip(Blockly.Msg.online_voice_result);
  4462. this.setHelpUrl("");
  4463. }
  4464. };
  4465. Blockly.Python['ai_online_voice_result'] = function (block) {
  4466. let code = `VoiceResult`;
  4467. return [code, Blockly.Python.ORDER_ATOMIC];
  4468. };
  4469. // 在线人脸识别
  4470. Blockly.Blocks['ai_online_ident_init'] = {
  4471. init: function () {
  4472. this.appendDummyInput()
  4473. .appendField(Blockly.Msg.online_face_init); //
  4474. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  4475. this.appendValueInput("B").setCheck(null).appendField('API Secret');
  4476. this.appendValueInput("C").setCheck(null).appendField('API Key');
  4477. this.setPreviousStatement(true, null);
  4478. this.setNextStatement(true, null);
  4479. this.setColour("#0000ff");
  4480. this.setTooltip(Blockly.Msg.online_face_init);
  4481. this.setHelpUrl("");
  4482. }
  4483. };
  4484. Blockly.Python['ai_online_ident_init'] = function (block) {
  4485. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  4486. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  4487. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  4488. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  4489. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  4490. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  4491. Blockly.Python.definitions_['v831_import_from_time_mktime'] = `from time import mktime`
  4492. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  4493. Blockly.Python.definitions_['v831_import_format_date_time'] = `from wsgiref.handlers import format_date_time`
  4494. Blockly.Python.definitions_['v831_import_urlencode'] = `from urllib.parse import urlencode`
  4495. Blockly.Python.definitions_['v831_import_os'] = `import os`
  4496. Blockly.Python.definitions_['v831_import_traceback'] = `import traceback`
  4497. Blockly.Python.definitions_['v831_import_json'] = `import json`
  4498. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  4499. Blockly.Python.definitions_.ai_online_ident_init = `
  4500. class AssembleHeaderException(Exception):
  4501. def __init__(self, msg):
  4502. self.message = msg
  4503. class Url:
  4504. def __init__(this, host, path, schema):
  4505. this.host = host
  4506. this.path = path
  4507. this.schema = schema
  4508. pass
  4509. # 进行sha256加密和base64编码
  4510. def sha256base64(data):
  4511. sha256 = hashlib.sha256()
  4512. sha256.update(data)
  4513. digest = base64.b64encode(sha256.digest()).decode(encoding="utf-8")
  4514. return digest
  4515. def parse_url_face(requset_url):
  4516. stidx = requset_url.index("://")
  4517. host = requset_url[stidx + 3:]
  4518. schema = requset_url[:stidx + 3]
  4519. edidx = host.index("/")
  4520. if edidx <= 0:
  4521. raise AssembleHeaderException("invalid request url:" + requset_url)
  4522. path = host[edidx:]
  4523. host = host[:edidx]
  4524. u = Url(host, path, schema)
  4525. return u
  4526. def assemble_ws_auth_url_face(requset_url, method="GET", api_key="", api_secret=""):
  4527. u = parse_url_face(requset_url)
  4528. host = u.host
  4529. path = u.path
  4530. now = datetime.now()
  4531. date = format_date_time(mktime(now.timetuple()))
  4532. print(date)
  4533. # date = "Thu, 12 Dec 2019 01:57:27 GMT"
  4534. signature_origin = "host: {}\\ndate: {}\\n{} {} HTTP/1.1".format(host, date, method, path)
  4535. print(signature_origin)
  4536. signature_sha = hmac.new(api_secret.encode("utf-8"), signature_origin.encode("utf-8"),
  4537. digestmod=hashlib.sha256).digest()
  4538. signature_sha = base64.b64encode(signature_sha).decode(encoding="utf-8")
  4539. authorization_origin = "api_key=\\"%s\\", algorithm=\\"%s\\", headers=\\"%s\\", signature=\\"%s\\"" % (
  4540. api_key, "hmac-sha256", "host date request-line", signature_sha)
  4541. authorization = base64.b64encode(authorization_origin.encode("utf-8")).decode(encoding="utf-8")
  4542. print(authorization_origin)
  4543. values = {
  4544. "host": host,
  4545. "date": date,
  4546. "authorization": authorization
  4547. }
  4548. return requset_url + "?" + urlencode(values)
  4549. def gen_body_face(appid, img_path, server_id):
  4550. with open(img_path, "rb") as f:
  4551. img_data = f.read()
  4552. body = {
  4553. "header": {
  4554. "app_id": appid,
  4555. "status": 3
  4556. },
  4557. "parameter": {
  4558. server_id: {
  4559. "service_kind": "face_detect",
  4560. #"detect_points": "1", #检测特征点
  4561. #"detect_property": "1", #检测人脸属性
  4562. "face_detect_result": {
  4563. "encoding": "utf8",
  4564. "compress": "raw",
  4565. "format": "json"
  4566. }
  4567. }
  4568. },
  4569. "payload": {
  4570. "input1": {
  4571. "encoding": "jpg",
  4572. "status": 3,
  4573. "image": str(base64.b64encode(img_data), "utf-8")
  4574. }
  4575. }
  4576. }
  4577. return json.dumps(body)
  4578. APPId_face = ${APPId}
  4579. APISecret_face = ${APISecret}
  4580. APIKey_face = ${APIKey}
  4581. `;
  4582. let code = ''
  4583. return code;
  4584. };
  4585. // 在线人脸识别加载
  4586. Blockly.Blocks['ai_online_ident_loading'] = {
  4587. init: function () {
  4588. this.appendDummyInput()
  4589. .appendField(Blockly.Msg.online_face_loading); //
  4590. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  4591. this.appendValueInput("B").setCheck(null).appendField('API Secret');
  4592. this.appendValueInput("C").setCheck(null).appendField('API Key');
  4593. this.appendValueInput("path")
  4594. .setCheck(null)
  4595. .appendField(Blockly.Msg.online_face_init_path); //
  4596. this.setPreviousStatement(true, null);
  4597. this.setNextStatement(true, null);
  4598. this.setColour("#0000ff");
  4599. this.setTooltip(Blockly.Msg.online_face_loading);
  4600. this.setHelpUrl("");
  4601. }
  4602. };
  4603. Blockly.Python['ai_online_ident_loading'] = function (block) {
  4604. var path = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  4605. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  4606. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  4607. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  4608. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  4609. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  4610. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  4611. Blockly.Python.definitions_['v831_import_from_time_mktime'] = `from time import mktime`
  4612. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  4613. Blockly.Python.definitions_['v831_import_format_date_time'] = `from wsgiref.handlers import format_date_time`
  4614. Blockly.Python.definitions_['v831_import_urlencode'] = `from urllib.parse import urlencode`
  4615. Blockly.Python.definitions_['v831_import_os'] = `import os`
  4616. Blockly.Python.definitions_['v831_import_traceback'] = `import traceback`
  4617. Blockly.Python.definitions_['v831_import_json'] = `import json`
  4618. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  4619. Blockly.Python.definitions_.ai_online_ident_init = `
  4620. class AssembleHeaderException(Exception):
  4621. def __init__(self, msg):
  4622. self.message = msg
  4623. class Url:
  4624. def __init__(this, host, path, schema):
  4625. this.host = host
  4626. this.path = path
  4627. this.schema = schema
  4628. pass
  4629. # 进行sha256加密和base64编码
  4630. def sha256base64(data):
  4631. sha256 = hashlib.sha256()
  4632. sha256.update(data)
  4633. digest = base64.b64encode(sha256.digest()).decode(encoding="utf-8")
  4634. return digest
  4635. def parse_url_face(requset_url):
  4636. stidx = requset_url.index("://")
  4637. host = requset_url[stidx + 3:]
  4638. schema = requset_url[:stidx + 3]
  4639. edidx = host.index("/")
  4640. if edidx <= 0:
  4641. raise AssembleHeaderException("invalid request url:" + requset_url)
  4642. path = host[edidx:]
  4643. host = host[:edidx]
  4644. u = Url(host, path, schema)
  4645. return u
  4646. def assemble_ws_auth_url_face(requset_url, method="GET", api_key="", api_secret=""):
  4647. u = parse_url_face(requset_url)
  4648. host = u.host
  4649. path = u.path
  4650. now = datetime.now()
  4651. date = format_date_time(mktime(now.timetuple()))
  4652. print(date)
  4653. # date = "Thu, 12 Dec 2019 01:57:27 GMT"
  4654. signature_origin = "host: {}\\ndate: {}\\n{} {} HTTP/1.1".format(host, date, method, path)
  4655. print(signature_origin)
  4656. signature_sha = hmac.new(api_secret.encode("utf-8"), signature_origin.encode("utf-8"),
  4657. digestmod=hashlib.sha256).digest()
  4658. signature_sha = base64.b64encode(signature_sha).decode(encoding="utf-8")
  4659. authorization_origin = "api_key=\\"%s\\", algorithm=\\"%s\\", headers=\\"%s\\", signature=\\"%s\\"" % (
  4660. api_key, "hmac-sha256", "host date request-line", signature_sha)
  4661. authorization = base64.b64encode(authorization_origin.encode("utf-8")).decode(encoding="utf-8")
  4662. print(authorization_origin)
  4663. values = {
  4664. "host": host,
  4665. "date": date,
  4666. "authorization": authorization
  4667. }
  4668. return requset_url + "?" + urlencode(values)
  4669. def gen_body_face(appid, img_path, server_id):
  4670. with open(img_path, "rb") as f:
  4671. img_data = f.read()
  4672. body = {
  4673. "header": {
  4674. "app_id": appid,
  4675. "status": 3
  4676. },
  4677. "parameter": {
  4678. server_id: {
  4679. "service_kind": "face_detect",
  4680. #"detect_points": "1", #检测特征点
  4681. #"detect_property": "1", #检测人脸属性
  4682. "face_detect_result": {
  4683. "encoding": "utf8",
  4684. "compress": "raw",
  4685. "format": "json"
  4686. }
  4687. }
  4688. },
  4689. "payload": {
  4690. "input1": {
  4691. "encoding": "jpg",
  4692. "status": 3,
  4693. "image": str(base64.b64encode(img_data), "utf-8")
  4694. }
  4695. }
  4696. }
  4697. return json.dumps(body)
  4698. APPId_face = ${APPId}
  4699. APISecret_face = ${APISecret}
  4700. APIKey_face = ${APIKey}
  4701. `;
  4702. Blockly.Python.definitions_.online_ident = `
  4703. ONLINE_IDENT_DATA = ""
  4704. def run_face(appid, apikey, apisecret, img_path, server_id="s67c9c78c"):
  4705. url = "http://api.xf-yun.com/v1/private/{}".format(server_id)
  4706. request_url = assemble_ws_auth_url_face(url, "POST", apikey, apisecret)
  4707. headers = {"content-type": "application/json", "host": "api.xf-yun.com", "app_id": appid}
  4708. print(request_url)
  4709. response = requests.post(request_url, data=gen_body_face(appid, img_path, server_id), headers=headers)
  4710. resp_data = json.loads(response.content.decode("utf-8"))
  4711. print(resp_data)
  4712. picRes=base64.b64decode(resp_data["payload"]["face_detect_result"]["text"]).decode()
  4713. print(picRes)
  4714. with open("/root/pic0.txt","w") as f:
  4715. f.write(picRes)
  4716. return eval(picRes)
  4717. `
  4718. let code = `
  4719. if __name__ == "__main__":
  4720. img_path_face = ${path}
  4721. ONLINE_IDENT_DATA = run_face(appid=APPId_face,apisecret=APISecret_face,apikey=APIKey_face,img_path=img_path_face,)
  4722. `;
  4723. return code;
  4724. };
  4725. // 在线人脸识别结果
  4726. Blockly.Blocks['ai_online_ident_result'] = {
  4727. init: function () {
  4728. this.appendDummyInput()
  4729. .appendField(Blockly.Msg.online_face_result); //
  4730. this.setOutput(true, null);
  4731. this.setColour("#0000ff");
  4732. this.setTooltip(Blockly.Msg.online_face_result);
  4733. this.setHelpUrl("");
  4734. }
  4735. };
  4736. Blockly.Python['ai_online_ident_result'] = function (block) {
  4737. let code = `ONLINE_IDENT_DATA["face_num"]`;
  4738. return [code, Blockly.Python.ORDER_ATOMIC];
  4739. };
  4740. Blockly.Blocks['ai_online_ident_result_people'] = {
  4741. init: function () {
  4742. this.appendDummyInput()
  4743. .appendField(Blockly.Msg.serialcomm_write_item_first)
  4744. .appendField(new Blockly.FieldVariable("i"), "varitem")
  4745. .appendField(Blockly.Msg.face_information)
  4746. .appendField(new Blockly.FieldDropdown([
  4747. [Blockly.Msg.ai_models_object_model_x, "x"],
  4748. [Blockly.Msg.ai_models_object_model_y, "y"],
  4749. [Blockly.Msg.ai_models_object_model_w, "w"],
  4750. [Blockly.Msg.ai_models_object_model_h, "h"],
  4751. // ['人脸属性信息', "property"],
  4752. [Blockly.Msg.ai_models_object_model_confidence, "score"]
  4753. ]), "TYPE");
  4754. this.setOutput(true, null);
  4755. this.setColour("#0000ff");
  4756. this.setTooltip('');
  4757. this.setHelpUrl("");
  4758. }
  4759. }
  4760. Blockly.Python['ai_online_ident_result_people'] = function (block) {
  4761. var type = block.getFieldValue('TYPE');
  4762. var i = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  4763. let code = '';
  4764. if (type == "w") {
  4765. code = `ONLINE_IDENT_DATA.get('face_'+str(${i})).get('${type}') + ONLINE_IDENT_DATA.get('face_'+str(${i})).get('x')`
  4766. } else if (type == "h") {
  4767. code = `ONLINE_IDENT_DATA.get('face_'+str(${i})).get('${type}') + ONLINE_IDENT_DATA.get('face_'+str(${i})).get('y')`
  4768. } else {
  4769. code = `ONLINE_IDENT_DATA.get('face_'+str(${i})).get('${type}')`
  4770. }
  4771. return [code, Blockly.Python.ORDER_ATOMIC];
  4772. };
  4773. Blockly.Blocks['ai_audio_init'] = {
  4774. init: function () {
  4775. this.appendDummyInput()
  4776. .appendField(new Blockly.FieldImage("blockly/media/speaker_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  4777. this.appendDummyInput()
  4778. .appendField(Blockly.Msg.Text_to_speech_initialization); //
  4779. this.appendValueInput("A")
  4780. .setCheck(null)
  4781. .appendField('APP ID');
  4782. this.appendValueInput("B")
  4783. .setCheck(null)
  4784. .appendField('API Secret');
  4785. this.appendValueInput("C")
  4786. .setCheck(null)
  4787. .appendField('API Key');
  4788. this.setPreviousStatement(true, null);
  4789. this.setNextStatement(true, null);
  4790. this.setColour("#0000ff");
  4791. this.setTooltip(Blockly.Msg.Text_to_speech_initialization);
  4792. this.setHelpUrl("");
  4793. }
  4794. };
  4795. // 在线文本转语音初始化
  4796. Blockly.Python.ai_audio_init = function (block) {
  4797. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  4798. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  4799. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  4800. Blockly.Python.definitions_['v831_import_time'] = `import time`
  4801. Blockly.Python.definitions_['v831_import_os'] = `import os`
  4802. Blockly.Python.definitions_['v831_import_json'] = `import json`
  4803. Blockly.Python.definitions_['v831_import_wave'] = `import wave`
  4804. Blockly.Python.definitions_['v831_import_urlencode'] = `from urllib.parse import urlencode`
  4805. Blockly.Python.definitions_['v831_import_format_date_time'] = `from wsgiref.handlers import format_date_time`
  4806. Blockly.Python.definitions_['v831_import_from_time_mktime'] = `from time import mktime`
  4807. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  4808. // Blockly.Python.definitions_['v831_import_datetime'] = `import datetime`
  4809. Blockly.Python.definitions_['v831_import_websocket'] = `import websocket`
  4810. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  4811. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  4812. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  4813. Blockly.Python.definitions_['v831_import_ssl'] = `import ssl`
  4814. Blockly.Python.definitions_['v831_import_thread_as_thread'] = `import _thread as thread`
  4815. Blockly.Python.definitions_.ai_audio_init_function = `
  4816. STATUS_FIRST_FRAME = 0 # 第一帧的标识
  4817. STATUS_CONTINUE_FRAME = 1 # 中间帧标识
  4818. STATUS_LAST_FRAME = 2 # 最后一帧的标识
  4819. class Ws_Param_Audio(object):
  4820. # 初始化
  4821. def __init__(self, APPID, APIKey, APISecret, Text):
  4822. self.APPID = APPID
  4823. self.APIKey = APIKey
  4824. self.APISecret = APISecret
  4825. self.Text = Text
  4826. # 公共参数(common)
  4827. self.CommonArgs = {"app_id": self.APPID}
  4828. # 业务参数(business),更多个性化参数可在官网查看
  4829. self.BusinessArgs = {"aue": "raw", "auf": "audio/L16;rate=16000", "vcn": "xiaoyan", "tte": "utf8"}
  4830. self.Data = {"status": 2, "text": str(base64.b64encode(self.Text.encode("utf-8")), "UTF8")}
  4831. #使用小语种须使用以下方式,此处的unicode指的是 utf16小端的编码方式,即"UTF-16LE"”
  4832. #self.Data = {"status": 2, "text": str(base64.b64encode(self.Text.encode('utf-16')), "UTF8")}
  4833. # 生成url
  4834. def create_url(self):
  4835. url = "wss://tts-api.xfyun.cn/v2/tts"
  4836. # 生成RFC1123格式的时间戳
  4837. now = datetime.now()
  4838. date = format_date_time(mktime(now.timetuple()))
  4839. # 拼接字符串
  4840. signature_origin = "host: " + "ws-api.xfyun.cn" + "\\n"
  4841. signature_origin += "date: " + date + "\\n"
  4842. signature_origin += "GET " + "/v2/tts " + "HTTP/1.1"
  4843. # 进行hmac-sha256进行加密
  4844. signature_sha = hmac.new(self.APISecret.encode("utf-8"), signature_origin.encode("utf-8"),digestmod=hashlib.sha256).digest()
  4845. signature_sha = base64.b64encode(signature_sha).decode(encoding="utf-8")
  4846. authorization_origin = "api_key=\\"%s\\", algorithm=\\"%s\\", headers=\\"%s\\", signature=\\"%s\\"" % (self.APIKey, "hmac-sha256", "host date request-line", signature_sha)
  4847. authorization = base64.b64encode(authorization_origin.encode("utf-8")).decode(encoding="utf-8")
  4848. # 将请求的鉴权参数组合为字典
  4849. v = {
  4850. "authorization": authorization,
  4851. "date": date,
  4852. "host": "ws-api.xfyun.cn"
  4853. }
  4854. # 拼接鉴权参数,生成url
  4855. url = url + "?" + urlencode(v)
  4856. # print("date: ",date)
  4857. # print("v: ",v)
  4858. # 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释,比对相同参数时生成的url与自己代码生成的url是否一致
  4859. # print('websocket url :', url)
  4860. return url
  4861. APPId_Audio = ${APPId}
  4862. APISecret_Audio = ${APISecret}
  4863. APIKey_Audio = ${APIKey}
  4864. `;
  4865. var _code = "";
  4866. return _code;
  4867. }
  4868. Blockly.Blocks['ai_audio_play_state'] = {
  4869. init: function () {
  4870. this.appendDummyInput()
  4871. .appendField(Blockly.Msg.text_conversion_voice); //
  4872. this.appendValueInput("A")
  4873. .setCheck(null)
  4874. .appendField('APP ID');
  4875. this.appendValueInput("B")
  4876. .setCheck(null)
  4877. .appendField('API Secret');
  4878. this.appendValueInput("C")
  4879. .setCheck(null)
  4880. .appendField('API Key');
  4881. this.appendValueInput("NAME")
  4882. .setCheck(null)
  4883. .appendField(Blockly.Msg.Content_of_text); //
  4884. this.appendValueInput("path")
  4885. .setCheck(null)
  4886. .appendField(Blockly.Msg.Voice_path); //
  4887. this.setPreviousStatement(true, null);
  4888. this.setNextStatement(true, null);
  4889. this.setColour("#0000ff");
  4890. this.setTooltip(Blockly.Msg.text_conversion_voice);
  4891. this.setHelpUrl("");
  4892. }
  4893. };
  4894. Blockly.Python['ai_audio_play_state'] = function (block) {
  4895. // TODO: Assemble Python into code variable.
  4896. let text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC)
  4897. let url = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC)
  4898. let path = `${url.slice(1, url.length - 1)}`
  4899. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  4900. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  4901. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  4902. Blockly.Python.definitions_['v831_import_time'] = `import time`
  4903. Blockly.Python.definitions_['v831_import_os'] = `import os`
  4904. Blockly.Python.definitions_['v831_import_json'] = `import json`
  4905. Blockly.Python.definitions_['v831_import_wave'] = `import wave`
  4906. Blockly.Python.definitions_['v831_import_urlencode'] = `from urllib.parse import urlencode`
  4907. Blockly.Python.definitions_['v831_import_format_date_time'] = `from wsgiref.handlers import format_date_time`
  4908. Blockly.Python.definitions_['v831_import_from_time_mktime'] = `from time import mktime`
  4909. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  4910. // Blockly.Python.definitions_['v831_import_datetime'] = `import datetime`
  4911. Blockly.Python.definitions_['v831_import_websocket'] = `import websocket`
  4912. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  4913. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  4914. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  4915. Blockly.Python.definitions_['v831_import_ssl'] = `import ssl`
  4916. Blockly.Python.definitions_['v831_import_thread_as_thread'] = `import _thread as thread`
  4917. Blockly.Python.definitions_.ai_audio_init_function = `
  4918. STATUS_FIRST_FRAME = 0 # 第一帧的标识
  4919. STATUS_CONTINUE_FRAME = 1 # 中间帧标识
  4920. STATUS_LAST_FRAME = 2 # 最后一帧的标识
  4921. class Ws_Param_Audio(object):
  4922. # 初始化
  4923. def __init__(self, APPID, APIKey, APISecret, Text):
  4924. self.APPID = APPID
  4925. self.APIKey = APIKey
  4926. self.APISecret = APISecret
  4927. self.Text = Text
  4928. # 公共参数(common)
  4929. self.CommonArgs = {"app_id": self.APPID}
  4930. # 业务参数(business),更多个性化参数可在官网查看
  4931. self.BusinessArgs = {"aue": "raw", "auf": "audio/L16;rate=16000", "vcn": "xiaoyan", "tte": "utf8"}
  4932. self.Data = {"status": 2, "text": str(base64.b64encode(self.Text.encode("utf-8")), "UTF8")}
  4933. #使用小语种须使用以下方式,此处的unicode指的是 utf16小端的编码方式,即"UTF-16LE"”
  4934. #self.Data = {"status": 2, "text": str(base64.b64encode(self.Text.encode('utf-16')), "UTF8")}
  4935. # 生成url
  4936. def create_url(self):
  4937. url = "wss://tts-api.xfyun.cn/v2/tts"
  4938. # 生成RFC1123格式的时间戳
  4939. now = datetime.now()
  4940. date = format_date_time(mktime(now.timetuple()))
  4941. # 拼接字符串
  4942. signature_origin = "host: " + "ws-api.xfyun.cn" + "\\n"
  4943. signature_origin += "date: " + date + "\\n"
  4944. signature_origin += "GET " + "/v2/tts " + "HTTP/1.1"
  4945. # 进行hmac-sha256进行加密
  4946. signature_sha = hmac.new(self.APISecret.encode("utf-8"), signature_origin.encode("utf-8"),digestmod=hashlib.sha256).digest()
  4947. signature_sha = base64.b64encode(signature_sha).decode(encoding="utf-8")
  4948. authorization_origin = "api_key=\\"%s\\", algorithm=\\"%s\\", headers=\\"%s\\", signature=\\"%s\\"" % (self.APIKey, "hmac-sha256", "host date request-line", signature_sha)
  4949. authorization = base64.b64encode(authorization_origin.encode("utf-8")).decode(encoding="utf-8")
  4950. # 将请求的鉴权参数组合为字典
  4951. v = {
  4952. "authorization": authorization,
  4953. "date": date,
  4954. "host": "ws-api.xfyun.cn"
  4955. }
  4956. # 拼接鉴权参数,生成url
  4957. url = url + "?" + urlencode(v)
  4958. # print("date: ",date)
  4959. # print("v: ",v)
  4960. # 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释,比对相同参数时生成的url与自己代码生成的url是否一致
  4961. # print('websocket url :', url)
  4962. return url
  4963. APPId_Audio = ${APPId}
  4964. APISecret_Audio = ${APISecret}
  4965. APIKey_Audio = ${APIKey}
  4966. `;
  4967. Blockly.Python.definitions_.on_message = `
  4968. def on_message(ws, message):
  4969. try:
  4970. message =json.loads(message)
  4971. code = message["code"]
  4972. sid = message["sid"]
  4973. audio = message["data"]["audio"]
  4974. audio = base64.b64decode(audio)
  4975. status = message["data"]["status"]
  4976. #print(message)
  4977. if status == 2:
  4978. print("ws is closed")
  4979. ws.close()
  4980. if code != 0:
  4981. errMsg = message["message"]
  4982. print("sid:%s call error:%s code is:%s" % (sid, errMsg, code))
  4983. else:
  4984. pcm_path = "${path}"
  4985. with open(pcm_path, "ab") as f:
  4986. f.write(audio)
  4987. print("tts")
  4988. with open(pcm_path, "rb") as pcmfile:
  4989. pcmdata = pcmfile.read()
  4990. with wave.open(pcm_path + ".wav", "wb") as wavfile:
  4991. wavfile.setparams((1, 2, 16000, 0, "NONE", "NONE"))
  4992. wavfile.writeframes(pcmdata)
  4993. except Exception as e:
  4994. print("receive msg,but parse exception:", e)
  4995. # 收到websocket错误的处理
  4996. def on_error(ws, error):
  4997. print("### error:", error)
  4998. # 收到websocket关闭的处理
  4999. def on_close(ws):
  5000. print("### closed ###")
  5001. # 收到websocket连接建立的处理
  5002. def on_open(ws):
  5003. def run(*args):
  5004. global wsParam_Audio
  5005. d = {"common": wsParam_Audio.CommonArgs,"business": wsParam_Audio.BusinessArgs,"data": wsParam_Audio.Data,}
  5006. d = json.dumps(d)
  5007. print("------>开始发送文本数据")
  5008. ws.send(d)
  5009. if os.path.exists(${url}):
  5010. os.remove(${url})
  5011. thread.start_new_thread(run, ())
  5012. `
  5013. Blockly.Python.addVariable('wsParam_Audio', 'wsParam_Audio = ""', true);
  5014. let code = `if __name__ == "__main__":
  5015. #with open('word.txt', 'r',encoding="UTF-8")as f:#txt= f.read()
  5016. txt_Audio=${text}
  5017. if os.path.exists(${url}):
  5018. os.remove(${url})
  5019. # 测试时候在此处正确填写相关信息即可运行
  5020. wsParam_Audio = Ws_Param_Audio(APPID=APPId_Audio, APISecret=APISecret_Audio,APIKey=APIKey_Audio,Text=txt_Audio)
  5021. websocket.enableTrace(False)
  5022. wsUrl = wsParam_Audio.create_url()
  5023. ws = websocket.WebSocketApp(wsUrl, on_message=on_message, on_error=on_error, on_close=on_close)
  5024. ws.on_open = on_open
  5025. ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
  5026. `
  5027. // TODO: Change ORDER_NONE to the correct strength.
  5028. return code
  5029. };
  5030. // 在线物体识别
  5031. Blockly.Blocks['ai_online_object_init'] = {
  5032. init: function () {
  5033. this.appendDummyInput()
  5034. .appendField(Blockly.Msg.online_object_init); //
  5035. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  5036. // this.appendValueInput("B").setCheck(null).appendField('APISecret');
  5037. this.appendValueInput("C").setCheck(null).appendField('API Key'); //Blockly.Msg.online_voice_init_path
  5038. this.setPreviousStatement(true, null);
  5039. this.setNextStatement(true, null);
  5040. this.setColour("#0000ff");
  5041. this.setTooltip(Blockly.Msg.online_object_init);
  5042. this.setHelpUrl("");
  5043. }
  5044. };
  5045. Blockly.Python['ai_online_object_init'] = function (block) {
  5046. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  5047. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  5048. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  5049. // TODO: Assemble Python into code variable.
  5050. Blockly.Python.definitions_['v831_import_time'] = `import time`
  5051. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  5052. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  5053. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  5054. Blockly.Python.definitions_.ai_online_object_init = `
  5055. API_KEY = ${APIKey}
  5056. APPID = ${APPId}
  5057. def objectGetHeader(APPID, image_url, API_KEY):
  5058. with open(image_url, "rb") as f:
  5059. img_data = f.read()
  5060. curTime = str(int(time.time()))
  5061. paramBase64 = base64.b64encode(img_data)
  5062. tmp = str(paramBase64, "utf-8")
  5063. m2 = hashlib.md5()
  5064. m2.update((API_KEY + curTime + tmp).encode("utf-8"))
  5065. checkSum = m2.hexdigest()
  5066. header = {
  5067. 'X-CurTime': curTime,
  5068. 'X-Param': paramBase64,
  5069. 'X-Appid': APPID,
  5070. 'X-CheckSum': checkSum,
  5071. }
  5072. return header
  5073. `
  5074. var code = '';
  5075. return code;
  5076. };
  5077. Blockly.Blocks['ai_online_object_loading'] = {
  5078. init: function () {
  5079. this.appendDummyInput()
  5080. .appendField(Blockly.Msg.online_object_load); //
  5081. this.appendValueInput("path").setCheck(null).appendField(Blockly.Msg.online_face_init_path);
  5082. this.setPreviousStatement(true, null);
  5083. this.setNextStatement(true, null);
  5084. this.setColour("#0000ff");
  5085. this.setTooltip(Blockly.Msg.online_object_load);
  5086. this.setHelpUrl("");
  5087. }
  5088. }
  5089. Blockly.Python.ai_online_object_loading = function (block) {
  5090. var AudioFile = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  5091. let code = `ImageUrl = ${AudioFile}
  5092. OBJECTRESULT = requests.post("http://tupapi.xfyun.cn/v1/currency", headers=objectGetHeader(APPID, ImageUrl,API_KEY))
  5093. `
  5094. return code
  5095. }
  5096. Blockly.Blocks['ai_online_object_result'] = {
  5097. init: function () {
  5098. this.appendDummyInput()
  5099. .appendField(Blockly.Msg.online_object_result); //
  5100. this.setOutput(true, null);
  5101. this.setColour("#0000ff");
  5102. this.setTooltip(Blockly.Msg.online_object_result);
  5103. this.setHelpUrl("");
  5104. }
  5105. };
  5106. Blockly.Python['ai_online_object_result'] = function (block) {
  5107. let code = `OBJECTRESULT.content`;
  5108. return [code, Blockly.Python.ORDER_ATOMIC];
  5109. };
  5110. // 在线车牌识别
  5111. Blockly.Blocks['ai_online_card_init'] = {
  5112. init: function () {
  5113. this.appendDummyInput()
  5114. .appendField(Blockly.Msg.online_card_init); //
  5115. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  5116. this.appendValueInput("B").setCheck(null).appendField('API Secret');
  5117. this.appendValueInput("C").setCheck(null).appendField('API Key'); //Blockly.Msg.online_voice_init_path
  5118. this.setPreviousStatement(true, null);
  5119. this.setNextStatement(true, null);
  5120. this.setColour("#0000ff");
  5121. this.setTooltip(Blockly.Msg.online_card_init);
  5122. this.setHelpUrl("");
  5123. }
  5124. };
  5125. Blockly.Python['ai_online_card_init'] = function (block) {
  5126. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  5127. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  5128. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  5129. // TODO: Assemble Python into code variable.
  5130. Blockly.Python.definitions_['v831_import_urlencode'] = `from urllib.parse import urlencode`
  5131. Blockly.Python.definitions_['v831_import_format_date_time'] = `from wsgiref.handlers import format_date_time`
  5132. Blockly.Python.definitions_['v831_import_from_time_mktime'] = `from time import mktime`
  5133. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  5134. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  5135. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  5136. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  5137. Blockly.Python.definitions_['v831_import_traceback'] = `import traceback`
  5138. Blockly.Python.definitions_['v831_import_json'] = `import json`
  5139. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  5140. Blockly.Python.definitions_.ai_online_card_init = `
  5141. APPId_Card = ${APPId}
  5142. APISecret_Card = ${APISecret}
  5143. APIKey_Card = ${APIKey}
  5144. class AssembleHeaderException_Card(Exception):
  5145. def __init__(self, msg):
  5146. self.message = msg
  5147. class Url_Card:
  5148. def __init__(this, host, path, schema):
  5149. this.host = host
  5150. this.path = path
  5151. this.schema = schema
  5152. pass
  5153. # 进行sha256加密和base64编码
  5154. def sha256base64_Card(data):
  5155. sha256 = hashlib.sha256()
  5156. sha256.update(data)
  5157. digest = base64.b64encode(sha256.digest()).decode(encoding="utf-8")
  5158. return digest
  5159. def parse_url_Card(requset_url):
  5160. stidx = requset_url.index("://")
  5161. host = requset_url[stidx + 3:]
  5162. schema = requset_url[:stidx + 3]
  5163. edidx = host.index("/")
  5164. if edidx <= 0:
  5165. raise AssembleHeaderException_Card("invalid request url:" + requset_url)
  5166. path = host[edidx:]
  5167. host = host[:edidx]
  5168. u = Url_Card(host, path, schema)
  5169. return u
  5170. def assemble_ws_auth_url_Card(requset_url, method="GET", api_key="", api_secret=""):
  5171. u = parse_url_Card(requset_url)
  5172. host = u.host
  5173. path = u.path
  5174. now = datetime.now()
  5175. date = format_date_time(mktime(now.timetuple()))
  5176. print(date)
  5177. # date = "Thu, 12 Dec 2019 01:57:27 GMT"
  5178. signature_origin = "host: {}\\ndate: {}\\n{} {} HTTP/1.1".format(host, date, method, path)
  5179. print(signature_origin)
  5180. signature_sha = hmac.new(api_secret.encode("utf-8"), signature_origin.encode("utf-8"),
  5181. digestmod=hashlib.sha256).digest()
  5182. signature_sha = base64.b64encode(signature_sha).decode(encoding="utf-8")
  5183. authorization_origin = "api_key=\\"%s\\", algorithm=\\"%s\\", headers=\\"%s\\", signature=\\"%s\\"" % (
  5184. api_key, "hmac-sha256", "host date request-line", signature_sha)
  5185. authorization = base64.b64encode(authorization_origin.encode("utf-8")).decode(encoding="utf-8")
  5186. print(authorization_origin)
  5187. values = {
  5188. "host": host,
  5189. "date": date,
  5190. "authorization": authorization
  5191. }
  5192. return requset_url + "?" + urlencode(values)
  5193. def gen_body_Card(appid, img_path, server_id):
  5194. with open(img_path, "rb") as f:
  5195. img_data = f.read()
  5196. body = {
  5197. "header": {
  5198. "app_id": appid,
  5199. "status": 3
  5200. },
  5201. "parameter": {
  5202. server_id: {
  5203. "carLicenseRes": {
  5204. "encoding": "utf8",
  5205. "compress": "raw",
  5206. "format": "json"
  5207. }
  5208. }
  5209. },
  5210. "payload": {
  5211. "carImgBase64Str": {
  5212. "encoding": "jpg",
  5213. "status": 3,
  5214. "image": str(base64.b64encode(img_data), "utf-8")
  5215. }
  5216. }
  5217. }
  5218. return json.dumps(body)
  5219. `
  5220. var code = '';
  5221. return code;
  5222. };
  5223. Blockly.Blocks['ai_online_card_loading'] = {
  5224. init: function () {
  5225. this.appendDummyInput()
  5226. .appendField(Blockly.Msg.online_card_load); //
  5227. this.appendValueInput("path").setCheck(null).appendField(Blockly.Msg.online_face_init_path);
  5228. this.setPreviousStatement(true, null);
  5229. this.setNextStatement(true, null);
  5230. this.setColour("#0000ff");
  5231. this.setTooltip(Blockly.Msg.online_card_load);
  5232. this.setHelpUrl("");
  5233. }
  5234. }
  5235. Blockly.Python.ai_online_card_loading = function (block) {
  5236. var AudioFile = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  5237. Blockly.Python.definitions_.ai_online_card_load = `def run_Card(appid, apikey, apisecret, img_path, server_id="jd_ocr_car"):
  5238. url = "http://api.xf-yun.com/v1/private/{}".format(server_id)
  5239. request_url = assemble_ws_auth_url_Card(url, "POST", apikey, apisecret)
  5240. headers = {"content-type": "application/json", "host": "api.xf-yun.com", "app_id": appid}
  5241. #print(request_url)
  5242. response = requests.post(request_url, data=gen_body_Card(appid, img_path, server_id), headers=headers)
  5243. resp_data = json.loads(response.content.decode("utf-8"))
  5244. print(resp_data)
  5245. resultCard = base64.b64decode(resp_data["payload"]["carLicenseRes"]["text"]).decode("utf-8")
  5246. return json.loads(resultCard)
  5247. CARDRESULT = ""
  5248. `
  5249. let code = `if __name__ == "__main__":
  5250. img_path_Card = ${AudioFile}
  5251. CARDRESULT = run_Card(appid=APPId_Card,apisecret=APISecret_Card,apikey=APIKey_Card,img_path=img_path_Card,)
  5252. `
  5253. return code
  5254. }
  5255. Blockly.Blocks['ai_online_card_result'] = {
  5256. init: function () {
  5257. this.appendDummyInput()
  5258. .appendField(Blockly.Msg.online_card_result); //
  5259. this.setOutput(true, null);
  5260. this.setColour("#0000ff");
  5261. this.setTooltip(Blockly.Msg.online_card_result);
  5262. this.setHelpUrl("");
  5263. }
  5264. };
  5265. Blockly.Python['ai_online_card_result'] = function (block) {
  5266. let code = `CARDRESULT`;
  5267. return [code, Blockly.Python.ORDER_ATOMIC];
  5268. };
  5269. // 机器翻译
  5270. Blockly.Blocks['ai_online_machine_translation_init'] = {
  5271. init: function () {
  5272. this.appendDummyInput()
  5273. .appendField(Blockly.Msg.machine_translation_init); //
  5274. this.appendDummyInput()
  5275. .appendField(Blockly.Msg.BLYNK_GET_DATANAME)
  5276. .appendField(new Blockly.FieldDropdown([
  5277. [Blockly.Msg.ai_speech_recognition_setup_set_simplified_chinese, "cn"],
  5278. [Blockly.Msg.ai_speech_recognition_setup_set_english, "en"],
  5279. ]), "from")
  5280. .appendField(Blockly.Msg.machine_translation_to)
  5281. .appendField(new Blockly.FieldDropdown([
  5282. [Blockly.Msg.ai_speech_recognition_setup_set_english, "en"],
  5283. [Blockly.Msg.ai_speech_recognition_setup_set_simplified_chinese, "cn"],
  5284. ]), "to");
  5285. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  5286. this.appendValueInput("B").setCheck(null).appendField('API Secret');
  5287. this.appendValueInput("C").setCheck(null).appendField('API Key');
  5288. this.setPreviousStatement(true, null);
  5289. this.setNextStatement(true, null);
  5290. this.setColour("#0000ff");
  5291. this.setTooltip(Blockly.Msg.machine_translation_init);
  5292. this.setHelpUrl("");
  5293. }
  5294. };
  5295. Blockly.Python['ai_online_machine_translation_init'] = function (block) {
  5296. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  5297. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  5298. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  5299. let FROM = block.getFieldValue('from')
  5300. let TO = block.getFieldValue('to')
  5301. // TODO: Assemble Python into code variable.
  5302. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  5303. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  5304. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  5305. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  5306. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  5307. Blockly.Python.definitions_['v831_import_json'] = `import json`
  5308. Blockly.Python.definitions_.ai_online_machine_translation_init = `
  5309. APPId_machine = ${APPId}
  5310. APISecret_machine = ${APISecret}
  5311. APIKey_machine = ${APIKey}
  5312. TRANSLATIONTEXT = ""
  5313. class get_result_machine(object):
  5314. def __init__(self,host,TRANSLATIONTEXT):
  5315. # 应用ID(到控制台获取)
  5316. self.APPID_machine = ${APPId}
  5317. # 接口APISercet(到控制台机器翻译服务页面获取)
  5318. self.Secret_machine = ${APISecret}
  5319. # 接口APIKey(到控制台机器翻译服务页面获取)
  5320. self.APIKey_machine= ${APIKey}
  5321. # 以下为POST请求
  5322. self.Host = "itrans.xfyun.cn"
  5323. self.RequestUri = "/v2/its"
  5324. # 设置url
  5325. # print(host)
  5326. self.url="https://"+host+self.RequestUri
  5327. self.HttpMethod = "POST"
  5328. self.Algorithm = "hmac-sha256"
  5329. self.HttpProto = "HTTP/1.1"
  5330. # 设置当前时间
  5331. curTime_utc = datetime.utcnow()
  5332. self.Date = self.httpdate_machine(curTime_utc)
  5333. # 设置业务参数
  5334. # 语种列表参数值请参照接口文档:https://www.xfyun.cn/doc/nlp/xftrans/API.html
  5335. self.Text=TRANSLATIONTEXT
  5336. self.BusinessArgs={
  5337. "from": "${FROM}",
  5338. "to": "${TO}",
  5339. }
  5340. def hashlib_256_machine(self, res):
  5341. m = hashlib.sha256(bytes(res.encode(encoding="utf-8"))).digest()
  5342. result = "SHA-256=" + base64.b64encode(m).decode(encoding="utf-8")
  5343. return result
  5344. def httpdate_machine(self, dt):
  5345. """
  5346. Return a string representation of a date according to RFC 1123
  5347. (HTTP/1.1).
  5348. The supplied date must be in UTC.
  5349. """
  5350. weekday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dt.weekday()]
  5351. month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  5352. "Oct", "Nov", "Dec"][dt.month - 1]
  5353. return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (weekday, dt.day, month,
  5354. dt.year, dt.hour, dt.minute, dt.second)
  5355. def generateSignature_machine(self, digest):
  5356. signatureStr = "host: " + self.Host + "\\n"
  5357. signatureStr += "date: " + self.Date + "\\n"
  5358. signatureStr += self.HttpMethod + " " + self.RequestUri \\
  5359. + " " + self.HttpProto + "\\n"
  5360. signatureStr += "digest: " + digest
  5361. signature = hmac.new(bytes(self.Secret_machine.encode(encoding="utf-8")),
  5362. bytes(signatureStr.encode(encoding="utf-8")),
  5363. digestmod=hashlib.sha256).digest()
  5364. result = base64.b64encode(signature)
  5365. return result.decode(encoding="utf-8")
  5366. def init_header_machine(self, data):
  5367. digest = self.hashlib_256_machine(data)
  5368. #print(digest)
  5369. sign = self.generateSignature_machine(digest)
  5370. authHeader = "api_key=\\""+self.APIKey_machine+"\\", algorithm=\\""+self.Algorithm+"\\", headers=\\"host date request-line digest\\", signature=\\""+sign+"\\""
  5371. #print(authHeader)
  5372. headers = {
  5373. "Content-Type": "application/json",
  5374. "Accept": "application/json",
  5375. "Method": "POST",
  5376. "Host": self.Host,
  5377. "Date": self.Date,
  5378. "Digest": digest,
  5379. "Authorization": authHeader
  5380. }
  5381. return headers
  5382. def get_body_machine(self):
  5383. content = str(base64.b64encode(self.Text.encode("utf-8")), "utf-8")
  5384. postdata = {
  5385. "common": {"app_id": self.APPID_machine},
  5386. "business": self.BusinessArgs,
  5387. "data": {
  5388. "text": content,
  5389. }
  5390. }
  5391. body = json.dumps(postdata)
  5392. #print(body)
  5393. return body
  5394. def call_url_machine(self):
  5395. if self.APPID_machine == "" or self.APIKey_machine == "" or self.Secret_machine == "":
  5396. print("Appid 或APIKey 或APISecret 为空!请打开demo代码,填写相关信息。")
  5397. else:
  5398. code = 0
  5399. body=self.get_body_machine()
  5400. headers=self.init_header_machine(body)
  5401. #print(self.url)
  5402. response = requests.post(self.url, data=body, headers=headers,timeout=8)
  5403. status_code = response.status_code
  5404. #print(response.content)
  5405. if status_code!=200:
  5406. # 鉴权失败
  5407. print("Http请求失败,状态码:" + str(status_code) + ",错误信息:" + response.text)
  5408. print("请根据错误信息检查代码,接口文档:https://www.xfyun.cn/doc/nlp/xftrans/API.html")
  5409. else:
  5410. # 鉴权成功
  5411. respData = json.loads(response.text)
  5412. #print('result',respData["data"]['result']['trans_result']['dst'])
  5413. # return
  5414. # 以下仅用于调试
  5415. code = str(respData["code"])
  5416. if code=="0":
  5417. return respData["data"]["result"]["trans_result"]["dst"]
  5418. else:
  5419. print("请前往https://www.xfyun.cn/document/error-code?code=" + code + "查询解决办法")
  5420. `
  5421. var code = '';
  5422. return code;
  5423. };
  5424. Blockly.Blocks['ai_online_machine_translation_loading'] = {
  5425. init: function () {
  5426. this.appendDummyInput()
  5427. .appendField(Blockly.Msg.machine_translation_load); //
  5428. this.appendDummyInput()
  5429. .appendField(Blockly.Msg.BLYNK_GET_DATANAME)
  5430. .appendField(new Blockly.FieldDropdown([
  5431. [Blockly.Msg.ai_speech_recognition_setup_set_simplified_chinese, "cn"],
  5432. [Blockly.Msg.ai_speech_recognition_setup_set_english, "en"],
  5433. ]), "from")
  5434. .appendField(Blockly.Msg.machine_translation_to)
  5435. .appendField(new Blockly.FieldDropdown([
  5436. [Blockly.Msg.ai_speech_recognition_setup_set_english, "en"],
  5437. [Blockly.Msg.ai_speech_recognition_setup_set_simplified_chinese, "cn"],
  5438. ]), "to");
  5439. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  5440. this.appendValueInput("B").setCheck(null).appendField('API Secret');
  5441. this.appendValueInput("C").setCheck(null).appendField('API Key');
  5442. this.appendValueInput("NAME")
  5443. .setCheck(null)
  5444. .appendField(Blockly.Msg.Content_of_text); //
  5445. this.setPreviousStatement(true, null);
  5446. this.setNextStatement(true, null);
  5447. this.setColour("#0000ff");
  5448. this.setTooltip(Blockly.Msg.machine_translation_load);
  5449. this.setHelpUrl("");
  5450. }
  5451. }
  5452. Blockly.Python.ai_online_machine_translation_loading = function (block) {
  5453. let text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC)
  5454. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  5455. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  5456. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  5457. let FROM = block.getFieldValue('from')
  5458. let TO = block.getFieldValue('to')
  5459. // TODO: Assemble Python into code variable.
  5460. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  5461. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  5462. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  5463. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  5464. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  5465. Blockly.Python.definitions_['v831_import_json'] = `import json`
  5466. Blockly.Python.definitions_.ai_online_machine_translation_init = `
  5467. APPId_machine = ${APPId}
  5468. APISecret_machine = ${APISecret}
  5469. APIKey_machine = ${APIKey}
  5470. TRANSLATIONTEXT = ""
  5471. class get_result_machine(object):
  5472. def __init__(self,host,TRANSLATIONTEXT):
  5473. # 应用ID(到控制台获取)
  5474. self.APPID_machine = ${APPId}
  5475. # 接口APISercet(到控制台机器翻译服务页面获取)
  5476. self.Secret_machine = ${APISecret}
  5477. # 接口APIKey(到控制台机器翻译服务页面获取)
  5478. self.APIKey_machine= ${APIKey}
  5479. # 以下为POST请求
  5480. self.Host = "itrans.xfyun.cn"
  5481. self.RequestUri = "/v2/its"
  5482. # 设置url
  5483. # print(host)
  5484. self.url="https://"+host+self.RequestUri
  5485. self.HttpMethod = "POST"
  5486. self.Algorithm = "hmac-sha256"
  5487. self.HttpProto = "HTTP/1.1"
  5488. # 设置当前时间
  5489. curTime_utc = datetime.utcnow()
  5490. self.Date = self.httpdate_machine(curTime_utc)
  5491. # 设置业务参数
  5492. # 语种列表参数值请参照接口文档:https://www.xfyun.cn/doc/nlp/xftrans/API.html
  5493. self.Text=TRANSLATIONTEXT
  5494. self.BusinessArgs={
  5495. "from": "${FROM}",
  5496. "to": "${TO}",
  5497. }
  5498. def hashlib_256_machine(self, res):
  5499. m = hashlib.sha256(bytes(res.encode(encoding="utf-8"))).digest()
  5500. result = "SHA-256=" + base64.b64encode(m).decode(encoding="utf-8")
  5501. return result
  5502. def httpdate_machine(self, dt):
  5503. """
  5504. Return a string representation of a date according to RFC 1123
  5505. (HTTP/1.1).
  5506. The supplied date must be in UTC.
  5507. """
  5508. weekday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dt.weekday()]
  5509. month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  5510. "Oct", "Nov", "Dec"][dt.month - 1]
  5511. return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (weekday, dt.day, month,
  5512. dt.year, dt.hour, dt.minute, dt.second)
  5513. def generateSignature_machine(self, digest):
  5514. signatureStr = "host: " + self.Host + "\\n"
  5515. signatureStr += "date: " + self.Date + "\\n"
  5516. signatureStr += self.HttpMethod + " " + self.RequestUri \\
  5517. + " " + self.HttpProto + "\\n"
  5518. signatureStr += "digest: " + digest
  5519. signature = hmac.new(bytes(self.Secret_machine.encode(encoding="utf-8")),
  5520. bytes(signatureStr.encode(encoding="utf-8")),
  5521. digestmod=hashlib.sha256).digest()
  5522. result = base64.b64encode(signature)
  5523. return result.decode(encoding="utf-8")
  5524. def init_header_machine(self, data):
  5525. digest = self.hashlib_256_machine(data)
  5526. #print(digest)
  5527. sign = self.generateSignature_machine(digest)
  5528. authHeader = "api_key=\\""+self.APIKey_machine+"\\", algorithm=\\""+self.Algorithm+"\\", headers=\\"host date request-line digest\\", signature=\\""+sign+"\\""
  5529. #print(authHeader)
  5530. headers = {
  5531. "Content-Type": "application/json",
  5532. "Accept": "application/json",
  5533. "Method": "POST",
  5534. "Host": self.Host,
  5535. "Date": self.Date,
  5536. "Digest": digest,
  5537. "Authorization": authHeader
  5538. }
  5539. return headers
  5540. def get_body_machine(self):
  5541. content = str(base64.b64encode(self.Text.encode("utf-8")), "utf-8")
  5542. postdata = {
  5543. "common": {"app_id": self.APPID_machine},
  5544. "business": self.BusinessArgs,
  5545. "data": {
  5546. "text": content,
  5547. }
  5548. }
  5549. body = json.dumps(postdata)
  5550. #print(body)
  5551. return body
  5552. def call_url_machine(self):
  5553. if self.APPID_machine == "" or self.APIKey_machine == "" or self.Secret_machine == "":
  5554. print("Appid 或APIKey 或APISecret 为空!请打开demo代码,填写相关信息。")
  5555. else:
  5556. code = 0
  5557. body=self.get_body_machine()
  5558. headers=self.init_header_machine(body)
  5559. #print(self.url)
  5560. response = requests.post(self.url, data=body, headers=headers,timeout=8)
  5561. status_code = response.status_code
  5562. #print(response.content)
  5563. if status_code!=200:
  5564. # 鉴权失败
  5565. print("Http请求失败,状态码:" + str(status_code) + ",错误信息:" + response.text)
  5566. print("请根据错误信息检查代码,接口文档:https://www.xfyun.cn/doc/nlp/xftrans/API.html")
  5567. else:
  5568. # 鉴权成功
  5569. respData = json.loads(response.text)
  5570. #print('result',respData["data"]['result']['trans_result']['dst'])
  5571. # return
  5572. # 以下仅用于调试
  5573. code = str(respData["code"])
  5574. if code=="0":
  5575. return respData["data"]["result"]["trans_result"]["dst"]
  5576. else:
  5577. print("请前往https://www.xfyun.cn/document/error-code?code=" + code + "查询解决办法")
  5578. `
  5579. let code = `TRANSLATIONTEXT = ${text}
  5580. machinehost = "itrans.xfyun.cn"
  5581. #初始化类
  5582. gClass=get_result_machine(host=machinehost,TRANSLATIONTEXT=TRANSLATIONTEXT)
  5583. TRANSLATIONRESULT = gClass.call_url_machine()
  5584. `
  5585. return code
  5586. }
  5587. Blockly.Blocks['ai_online_machine_translation_result'] = {
  5588. init: function () {
  5589. this.appendDummyInput()
  5590. .appendField(Blockly.Msg.machine_translation_result); //
  5591. this.setOutput(true, null);
  5592. this.setColour("#0000ff");
  5593. this.setTooltip(Blockly.Msg.machine_translation_result);
  5594. this.setHelpUrl("");
  5595. }
  5596. };
  5597. Blockly.Python['ai_online_machine_translation_result'] = function (block) {
  5598. let code = `TRANSLATIONRESULT`;
  5599. return [code, Blockly.Python.ORDER_ATOMIC];
  5600. };
  5601. // 手写文字识别
  5602. Blockly.Blocks['ai_online_handwritten_text_init'] = {
  5603. init: function () {
  5604. this.appendDummyInput()
  5605. .appendField(Blockly.Msg.handwritten_text_init); //
  5606. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  5607. // this.appendValueInput("B").setCheck(null).appendField('API Secret');
  5608. this.appendValueInput("C").setCheck(null).appendField('API Key');
  5609. this.setPreviousStatement(true, null);
  5610. this.setNextStatement(true, null);
  5611. this.setColour("#0000ff");
  5612. this.setTooltip(Blockly.Msg.handwritten_text_init);
  5613. this.setHelpUrl("");
  5614. }
  5615. };
  5616. Blockly.Python['ai_online_handwritten_text_init'] = function (block) {
  5617. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  5618. // var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  5619. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  5620. // TODO: Assemble Python into code variable.
  5621. Blockly.Python.definitions_['v831_import_time'] = `import time`
  5622. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  5623. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  5624. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  5625. Blockly.Python.definitions_['v831_import_json'] = `import json`
  5626. Blockly.Python.definitions_['v831_import_from_urllib_parse'] = `from urllib import parse`
  5627. Blockly.Python.definitions_.ai_online_handwritten_text_init = `
  5628. URL_handwritten_text = "http://webapi.xfyun.cn/v1/service/v1/ocr/handwriting"
  5629. APPId_handwritten_text = ${APPId}
  5630. APIKey_handwritten_text = ${APIKey}
  5631. def textGetHeader_handwritten_text(language, location):
  5632. curTime = str(int(time.time()))
  5633. param = "{\\"language\\":\\""+language+"\\",\\"location\\":\\""+location+"\\"}"
  5634. paramBase64 = base64.b64encode(param.encode("utf-8"))
  5635. m2 = hashlib.md5()
  5636. str1 = APIKey_handwritten_text + curTime + str(paramBase64, "utf-8")
  5637. m2.update(str1.encode("utf-8"))
  5638. checkSum = m2.hexdigest()
  5639. # 组装http请求头
  5640. header = {
  5641. "X-CurTime": curTime,
  5642. "X-Param": paramBase64,
  5643. "X-Appid": APPId_handwritten_text,
  5644. "X-CheckSum": checkSum,
  5645. "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
  5646. }
  5647. return header
  5648. def getBody_handwritten_text(filepath):
  5649. with open(filepath, "rb") as f:
  5650. imgfile = f.read()
  5651. data = {"image": str(base64.b64encode(imgfile), "utf-8")}
  5652. return data
  5653. `
  5654. var code = '';
  5655. return code;
  5656. };
  5657. Blockly.Blocks['ai_online_handwritten_text_loading'] = {
  5658. init: function () {
  5659. this.appendDummyInput()
  5660. .appendField(Blockly.Msg.handwritten_text_load); //
  5661. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  5662. // this.appendValueInput("B").setCheck(null).appendField('API Secret');
  5663. this.appendValueInput("C").setCheck(null).appendField('API Key');
  5664. this.appendValueInput("path")
  5665. .setCheck(null)
  5666. .appendField(Blockly.Msg.online_face_init_path); //
  5667. this.setPreviousStatement(true, null);
  5668. this.setNextStatement(true, null);
  5669. this.setColour("#0000ff");
  5670. this.setTooltip(Blockly.Msg.handwritten_text_load);
  5671. this.setHelpUrl("");
  5672. }
  5673. }
  5674. Blockly.Python.ai_online_handwritten_text_loading = function (block) {
  5675. let text = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC)
  5676. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  5677. // var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  5678. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  5679. // TODO: Assemble Python into code variable.
  5680. Blockly.Python.definitions_['v831_import_time'] = `import time`
  5681. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  5682. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  5683. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  5684. Blockly.Python.definitions_['v831_import_json'] = `import json`
  5685. Blockly.Python.definitions_['v831_import_from_urllib_parse'] = `from urllib import parse`
  5686. Blockly.Python.definitions_.ai_online_handwritten_text_init = `
  5687. URL_handwritten_text = "http://webapi.xfyun.cn/v1/service/v1/ocr/handwriting"
  5688. APPId_handwritten_text = ${APPId}
  5689. APIKey_handwritten_text = ${APIKey}
  5690. def textGetHeader_handwritten_text(language, location):
  5691. curTime = str(int(time.time()))
  5692. param = "{\\"language\\":\\""+language+"\\",\\"location\\":\\""+location+"\\"}"
  5693. paramBase64 = base64.b64encode(param.encode("utf-8"))
  5694. m2 = hashlib.md5()
  5695. str1 = APIKey_handwritten_text + curTime + str(paramBase64, "utf-8")
  5696. m2.update(str1.encode("utf-8"))
  5697. checkSum = m2.hexdigest()
  5698. # 组装http请求头
  5699. header = {
  5700. "X-CurTime": curTime,
  5701. "X-Param": paramBase64,
  5702. "X-Appid": APPId_handwritten_text,
  5703. "X-CheckSum": checkSum,
  5704. "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
  5705. }
  5706. return header
  5707. def getBody_handwritten_text(filepath):
  5708. with open(filepath, "rb") as f:
  5709. imgfile = f.read()
  5710. data = {"image": str(base64.b64encode(imgfile), "utf-8")}
  5711. return data
  5712. `
  5713. let code = `# 语种设置
  5714. textLanguage = "cn|en"
  5715. # 是否返回文本位置信息
  5716. textLocation = "false"
  5717. # 图片上传接口地址
  5718. picFilePath_handwritten_text = ${text}
  5719. HandwrittenTextData = requests.post(URL_handwritten_text, headers=textGetHeader_handwritten_text(textLanguage, textLocation), data=getBody_handwritten_text(picFilePath_handwritten_text))
  5720. `
  5721. return code
  5722. }
  5723. Blockly.Blocks['ai_online_handwritten_text_result'] = {
  5724. init: function () {
  5725. this.appendDummyInput()
  5726. .appendField(Blockly.Msg.handwritten_text_result); //
  5727. this.setOutput(true, null);
  5728. this.setColour("#0000ff");
  5729. this.setTooltip(Blockly.Msg.handwritten_text_result);
  5730. this.setHelpUrl("");
  5731. }
  5732. };
  5733. Blockly.Python['ai_online_handwritten_text_result'] = function (block) {
  5734. Blockly.Python.definitions_.ai_online_handwritten_text_result = `def handwrittenTextDatas_handwritten_text(r):
  5735. my_json_data = r.content.decode("utf8")
  5736. # print(type(my_json))
  5737. loadsData = json.loads(my_json_data)
  5738. # s = json.dumps(data, indent=4, sort_keys=True)
  5739. print("data",my_json_data)
  5740. deal_data = ""
  5741. if loadsData["code"] == "0" :
  5742. t = loadsData["data"]["block"][0]["line"]
  5743. for i in t:
  5744. print(i)
  5745. for j in i["word"]:
  5746. print(j["content"])
  5747. deal_data = deal_data + j["content"] + " "
  5748. #print('deal_data',deal_data)
  5749. return deal_data
  5750. `
  5751. let code = `handwrittenTextDatas_handwritten_text(HandwrittenTextData)`;
  5752. return [code, Blockly.Python.ORDER_ATOMIC];
  5753. };
  5754. // 公式识别
  5755. Blockly.Blocks['ai_online_formula_init'] = {
  5756. init: function () {
  5757. this.appendDummyInput()
  5758. .appendField(Blockly.Msg.formula_init); //
  5759. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  5760. this.appendValueInput("B").setCheck(null).appendField('API Secret');
  5761. this.appendValueInput("C").setCheck(null).appendField('API Key');
  5762. this.setPreviousStatement(true, null);
  5763. this.setNextStatement(true, null);
  5764. this.setColour("#0000ff");
  5765. this.setTooltip(Blockly.Msg.formula_init);
  5766. this.setHelpUrl("");
  5767. }
  5768. };
  5769. Blockly.Python['ai_online_formula_init'] = function (block) {
  5770. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  5771. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  5772. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  5773. // TODO: Assemble Python into code variable.
  5774. Blockly.Python.definitions_['v831_import_time'] = `import time`
  5775. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  5776. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  5777. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  5778. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  5779. Blockly.Python.definitions_['v831_import_json'] = `import json`
  5780. Blockly.Python.definitions_['v831_import_from_urllib_parse'] = `from urllib import parse`
  5781. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  5782. Blockly.Python.definitions_.ai_online_formula_init = `class get_result_formula(object):
  5783. def __init__(self,host,path):
  5784. # 应用ID(到控制台获取)
  5785. self.APPID_formula = ${APPId}
  5786. # 接口APISercet(到控制台公式识别服务页面获取)
  5787. self.Secret_formula = ${APISecret}
  5788. # 接口APIKey(到控制台公式识别服务页面获取)
  5789. self.APIKey_formula = ${APIKey}
  5790. # 以下为POST请求
  5791. self.Host_formula = host
  5792. self.RequestUri_formula = "/v2/itr"
  5793. # 设置url
  5794. # print(host)
  5795. self.url_formula="https://"+host+self.RequestUri_formula
  5796. self.HttpMethod_formula = "POST"
  5797. self.Algorithm_formula = "hmac-sha256"
  5798. self.HttpProto_formula = "HTTP/1.1"
  5799. # 设置当前时间
  5800. curTime_utc = datetime.utcnow()
  5801. self.Date_formula = self.httpdate_formula(curTime_utc)
  5802. #设置测试图片文件
  5803. self.AudioPath_formula = path
  5804. self.BusinessArgs_formula={
  5805. "ent": "teach-photo-print",
  5806. "aue": "raw",
  5807. }
  5808. def imgRead_formula(self, path):
  5809. with open(path, "rb") as fo:
  5810. return fo.read()
  5811. def hashlib_256_formula(self, res):
  5812. m = hashlib.sha256(bytes(res.encode(encoding="utf-8"))).digest()
  5813. result = "SHA-256=" + base64.b64encode(m).decode(encoding="utf-8")
  5814. return result
  5815. def httpdate_formula(self, dt):
  5816. """
  5817. Return a string representation of a date according to RFC 1123
  5818. (HTTP/1.1).
  5819. The supplied date must be in UTC.
  5820. """
  5821. weekday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dt.weekday()]
  5822. month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  5823. "Oct", "Nov", "Dec"][dt.month - 1]
  5824. return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (weekday, dt.day, month,
  5825. dt.year, dt.hour, dt.minute, dt.second)
  5826. def generateSignature_formula(self, digest):
  5827. signatureStr = "host: " + self.Host_formula + "\\n"
  5828. signatureStr += "date: " + self.Date_formula + "\\n"
  5829. signatureStr += self.HttpMethod_formula + " " + self.RequestUri_formula \\
  5830. + " " + self.HttpProto_formula + "\\n"
  5831. signatureStr += "digest: " + digest
  5832. signature = hmac.new(bytes(self.Secret_formula.encode(encoding="utf-8")),
  5833. bytes(signatureStr.encode(encoding="utf-8")),
  5834. digestmod=hashlib.sha256).digest()
  5835. result = base64.b64encode(signature)
  5836. return result.decode(encoding="utf-8")
  5837. def init_header_formula(self, data):
  5838. digest = self.hashlib_256_formula(data)
  5839. #print(digest)
  5840. sign = self.generateSignature_formula(digest)
  5841. authHeader = "api_key=\\""+self.APIKey_formula+"\\", algorithm=\\""+self.Algorithm_formula+"\\", headers=\\"host date request-line digest\\", signature=\\""+sign+"\\""
  5842. #print(authHeader)
  5843. headers = {
  5844. "Content-Type": "application/json",
  5845. "Accept": "application/json",
  5846. "Method": "POST",
  5847. "Host": self.Host_formula,
  5848. "Date": self.Date_formula,
  5849. "Digest": digest,
  5850. "Authorization": authHeader
  5851. }
  5852. return headers
  5853. def get_body_formula(self):
  5854. audioData = self.imgRead_formula(self.AudioPath_formula)
  5855. content = base64.b64encode(audioData).decode(encoding="utf-8")
  5856. postdata = {
  5857. "common": {"app_id": self.APPID_formula},
  5858. "business": self.BusinessArgs_formula,
  5859. "data": {
  5860. "image": content,
  5861. }
  5862. }
  5863. body = json.dumps(postdata)
  5864. #print(body)
  5865. return body
  5866. def call_url_formula(self):
  5867. if self.APPID_formula == "" or self.APIKey_formula == "" or self.Secret_formula == "":
  5868. print("Appid 或APIKey 或APISecret 为空!请打开demo代码,填写相关信息。")
  5869. else:
  5870. code = 0
  5871. body=self.get_body_formula()
  5872. headers=self.init_header_formula(body)
  5873. #print(self.url_formula)
  5874. response = requests.post(self.url_formula, data=body, headers=headers,timeout=8)
  5875. status_code = response.status_code
  5876. #print(response.content)
  5877. if status_code!=200:
  5878. # 鉴权失败
  5879. print("Http请求失败,状态码:" + str(status_code) + ",错误信息:" + response.text)
  5880. print("请根据错误信息检查代码,接口文档:https://www.xfyun.cn/doc/words/formula-discern/API.html")
  5881. else:
  5882. # 鉴权成功
  5883. respData = json.loads(response.text)
  5884. dataCotent = ""
  5885. try:
  5886. for i in respData["data"]["region"]:
  5887. dataCotent += i["recog"]["content"]
  5888. except:
  5889. dataCotent = "Failure"
  5890. return dataCotent
  5891. FORMULARESULT = ""
  5892. `
  5893. var code = '';
  5894. return code;
  5895. };
  5896. Blockly.Blocks['ai_online_formula_loading'] = {
  5897. init: function () {
  5898. this.appendDummyInput()
  5899. .appendField(Blockly.Msg.formula_load); //
  5900. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  5901. this.appendValueInput("B").setCheck(null).appendField('API Secret');
  5902. this.appendValueInput("C").setCheck(null).appendField('API Key');
  5903. this.appendValueInput("path")
  5904. .setCheck(null)
  5905. .appendField(Blockly.Msg.online_face_init_path); //
  5906. this.setPreviousStatement(true, null);
  5907. this.setNextStatement(true, null);
  5908. this.setColour("#0000ff");
  5909. this.setTooltip(Blockly.Msg.formula_load);
  5910. this.setHelpUrl("");
  5911. }
  5912. }
  5913. Blockly.Python.ai_online_formula_loading = function (block) {
  5914. let text = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC)
  5915. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  5916. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  5917. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  5918. // TODO: Assemble Python into code variable.
  5919. Blockly.Python.definitions_['v831_import_time'] = `import time`
  5920. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  5921. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  5922. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  5923. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  5924. Blockly.Python.definitions_['v831_import_json'] = `import json`
  5925. Blockly.Python.definitions_['v831_import_from_urllib_parse'] = `from urllib import parse`
  5926. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  5927. Blockly.Python.definitions_.ai_online_formula_init = `class get_result_formula(object):
  5928. def __init__(self,host,path):
  5929. # 应用ID(到控制台获取)
  5930. self.APPID_formula = ${APPId}
  5931. # 接口APISercet(到控制台公式识别服务页面获取)
  5932. self.Secret_formula = ${APISecret}
  5933. # 接口APIKey(到控制台公式识别服务页面获取)
  5934. self.APIKey_formula = ${APIKey}
  5935. # 以下为POST请求
  5936. self.Host_formula = host
  5937. self.RequestUri_formula = "/v2/itr"
  5938. # 设置url
  5939. # print(host)
  5940. self.url_formula="https://"+host+self.RequestUri_formula
  5941. self.HttpMethod_formula = "POST"
  5942. self.Algorithm_formula = "hmac-sha256"
  5943. self.HttpProto_formula = "HTTP/1.1"
  5944. # 设置当前时间
  5945. curTime_utc = datetime.utcnow()
  5946. self.Date_formula = self.httpdate_formula(curTime_utc)
  5947. #设置测试图片文件
  5948. self.AudioPath_formula = path
  5949. self.BusinessArgs_formula={
  5950. "ent": "teach-photo-print",
  5951. "aue": "raw",
  5952. }
  5953. def imgRead_formula(self, path):
  5954. with open(path, "rb") as fo:
  5955. return fo.read()
  5956. def hashlib_256_formula(self, res):
  5957. m = hashlib.sha256(bytes(res.encode(encoding="utf-8"))).digest()
  5958. result = "SHA-256=" + base64.b64encode(m).decode(encoding="utf-8")
  5959. return result
  5960. def httpdate_formula(self, dt):
  5961. """
  5962. Return a string representation of a date according to RFC 1123
  5963. (HTTP/1.1).
  5964. The supplied date must be in UTC.
  5965. """
  5966. weekday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dt.weekday()]
  5967. month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  5968. "Oct", "Nov", "Dec"][dt.month - 1]
  5969. return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (weekday, dt.day, month,
  5970. dt.year, dt.hour, dt.minute, dt.second)
  5971. def generateSignature_formula(self, digest):
  5972. signatureStr = "host: " + self.Host_formula + "\\n"
  5973. signatureStr += "date: " + self.Date_formula + "\\n"
  5974. signatureStr += self.HttpMethod_formula + " " + self.RequestUri_formula \\
  5975. + " " + self.HttpProto_formula + "\\n"
  5976. signatureStr += "digest: " + digest
  5977. signature = hmac.new(bytes(self.Secret_formula.encode(encoding="utf-8")),
  5978. bytes(signatureStr.encode(encoding="utf-8")),
  5979. digestmod=hashlib.sha256).digest()
  5980. result = base64.b64encode(signature)
  5981. return result.decode(encoding="utf-8")
  5982. def init_header_formula(self, data):
  5983. digest = self.hashlib_256_formula(data)
  5984. #print(digest)
  5985. sign = self.generateSignature_formula(digest)
  5986. authHeader = "api_key=\\""+self.APIKey_formula+"\\", algorithm=\\""+self.Algorithm_formula+"\\", headers=\\"host date request-line digest\\", signature=\\""+sign+"\\""
  5987. #print(authHeader)
  5988. headers = {
  5989. "Content-Type": "application/json",
  5990. "Accept": "application/json",
  5991. "Method": "POST",
  5992. "Host": self.Host_formula,
  5993. "Date": self.Date_formula,
  5994. "Digest": digest,
  5995. "Authorization": authHeader
  5996. }
  5997. return headers
  5998. def get_body_formula(self):
  5999. audioData = self.imgRead_formula(self.AudioPath_formula)
  6000. content = base64.b64encode(audioData).decode(encoding="utf-8")
  6001. postdata = {
  6002. "common": {"app_id": self.APPID_formula},
  6003. "business": self.BusinessArgs_formula,
  6004. "data": {
  6005. "image": content,
  6006. }
  6007. }
  6008. body = json.dumps(postdata)
  6009. #print(body)
  6010. return body
  6011. def call_url_formula(self):
  6012. if self.APPID_formula == "" or self.APIKey_formula == "" or self.Secret_formula == "":
  6013. print("Appid 或APIKey 或APISecret 为空!请打开demo代码,填写相关信息。")
  6014. else:
  6015. code = 0
  6016. body=self.get_body_formula()
  6017. headers=self.init_header_formula(body)
  6018. #print(self.url_formula)
  6019. response = requests.post(self.url_formula, data=body, headers=headers,timeout=8)
  6020. status_code = response.status_code
  6021. #print(response.content)
  6022. if status_code!=200:
  6023. # 鉴权失败
  6024. print("Http请求失败,状态码:" + str(status_code) + ",错误信息:" + response.text)
  6025. print("请根据错误信息检查代码,接口文档:https://www.xfyun.cn/doc/words/formula-discern/API.html")
  6026. else:
  6027. # 鉴权成功
  6028. respData = json.loads(response.text)
  6029. dataCotent = ""
  6030. try:
  6031. for i in respData["data"]["region"]:
  6032. dataCotent += i["recog"]["content"]
  6033. except:
  6034. dataCotent = "Failure"
  6035. return dataCotent
  6036. FORMULARESULT = ""
  6037. `
  6038. let code = `
  6039. if __name__ == "__main__":
  6040. formulaUrlPath_formula = ${text}
  6041. ##示例: host="rest-api.xfyun.cn"域名形式
  6042. host = "rest-api.xfyun.cn"
  6043. #初始化类
  6044. gClass=get_result_formula(host,path=formulaUrlPath_formula)
  6045. FORMULARESULT = gClass.call_url_formula()
  6046. `
  6047. return code
  6048. }
  6049. Blockly.Blocks['ai_online_formula_result'] = {
  6050. init: function () {
  6051. this.appendDummyInput()
  6052. .appendField(Blockly.Msg.formula_result); //
  6053. this.setOutput(true, null);
  6054. this.setColour("#0000ff");
  6055. this.setTooltip(Blockly.Msg.formula_result);
  6056. this.setHelpUrl("");
  6057. }
  6058. };
  6059. Blockly.Python['ai_online_formula_result'] = function (block) {
  6060. let code = `FORMULARESULT`;
  6061. return [code, Blockly.Python.ORDER_ATOMIC];
  6062. };
  6063. // 身份证识别
  6064. Blockly.Blocks['ai_online_ID_card_init'] = {
  6065. init: function () {
  6066. this.appendDummyInput()
  6067. .appendField('身份证识别初始化'); //
  6068. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  6069. this.appendValueInput("B").setCheck(null).appendField('API Secret');
  6070. this.appendValueInput("C").setCheck(null).appendField('API Key');
  6071. this.setPreviousStatement(true, null);
  6072. this.setNextStatement(true, null);
  6073. this.setColour("#0000ff");
  6074. this.setTooltip(Blockly.Msg.handwritten_text_init);
  6075. this.setHelpUrl("");
  6076. }
  6077. };
  6078. Blockly.Python['ai_online_ID_card_init'] = function (block) {
  6079. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  6080. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  6081. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  6082. // TODO: Assemble Python into code variable.
  6083. Blockly.Python.definitions_['v831_import_time'] = `import time`
  6084. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  6085. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  6086. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  6087. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  6088. Blockly.Python.definitions_['v831_import_json'] = `import json`
  6089. Blockly.Python.definitions_['v831_import_from_time_mktime'] = `from time import mktime`
  6090. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  6091. Blockly.Python.definitions_['v831_import_from_urllib_parse'] = `from urllib import parse`
  6092. Blockly.Python.definitions_['v831_import_format_date_time'] = `from wsgiref.handlers import format_date_time`
  6093. Blockly.Python.definitions_['v831_import_urlencode'] = `from urllib.parse import urlencode`
  6094. Blockly.Python.definitions_.ai_online_formula_init = `class AssembleHeaderException(Exception):
  6095. def __init__(self, msg):
  6096. self.message = msg
  6097. class Url:
  6098. def __init__(this, host, path, schema):
  6099. this.host = host
  6100. this.path = path
  6101. this.schema = schema
  6102. pass
  6103. # calculate sha256 and encode to base64
  6104. def sha256base64(data):
  6105. sha256 = hashlib.sha256()
  6106. sha256.update(data)
  6107. digest = base64.b64encode(sha256.digest()).decode(encoding="utf-8")
  6108. return digest
  6109. def parse_url(requset_url):
  6110. stidx = requset_url.index("://")
  6111. host = requset_url[stidx + 3:]
  6112. schema = requset_url[:stidx + 3]
  6113. edidx = host.index("/")
  6114. if edidx <= 0:
  6115. raise AssembleHeaderException("invalid request url:" + requset_url)
  6116. path = host[edidx:]
  6117. host = host[:edidx]
  6118. u = Url(host, path, schema)
  6119. return u
  6120. # build websocket auth request url
  6121. def assemble_ws_auth_url(requset_url, method="POST", api_key="", api_secret=""):
  6122. u = parse_url(requset_url)
  6123. host = u.host
  6124. path = u.path
  6125. now = datetime.now()
  6126. date = format_date_time(mktime(now.timetuple()))
  6127. #print(date)
  6128. # date = "Thu, 12 Dec 2019 01:57:27 GMT"
  6129. signature_origin = "host: {}\\ndate: {}\\n{} {} HTTP/1.1".format(host, date, method, path)
  6130. #print(signature_origin)
  6131. signature_sha = hmac.new(api_secret.encode("utf-8"), signature_origin.encode("utf-8"),
  6132. digestmod=hashlib.sha256).digest()
  6133. signature_sha = base64.b64encode(signature_sha).decode(encoding="utf-8")
  6134. authorization_origin = "api_key=\\"%s\\", algorithm=\\"%s\\", headers=\\"%s\\", signature=\\"%s\\"" % (
  6135. api_key, "hmac-sha256", "host date request-line", signature_sha)
  6136. authorization = base64.b64encode(authorization_origin.encode("utf-8")).decode(encoding="utf-8")
  6137. #print(authorization_origin)
  6138. values = {
  6139. "host": host,
  6140. "date": date,
  6141. "authorization": authorization
  6142. }
  6143. return requset_url + "?" + urlencode(values)
  6144. # 控制台获取以下信息
  6145. APPId = ${APPId}
  6146. APISecret = ${APISecret}
  6147. APIKey = ${APIKey}
  6148. `
  6149. var code = '';
  6150. return code;
  6151. };
  6152. Blockly.Blocks['ai_online_ID_card_loading'] = {
  6153. init: function () {
  6154. this.appendDummyInput()
  6155. .appendField('身份证识别加载'); //
  6156. // this.appendDummyInput()
  6157. // .appendField('设置识别语种')
  6158. // .appendField(new Blockly.FieldDropdown([
  6159. // ["en", "en"],
  6160. // ["cn|en", "cn|en"],
  6161. // ]), "language");
  6162. this.appendValueInput("path")
  6163. .setCheck(null)
  6164. .appendField(Blockly.Msg.online_face_init_path); //
  6165. this.setPreviousStatement(true, null);
  6166. this.setNextStatement(true, null);
  6167. this.setColour("#0000ff");
  6168. this.setTooltip(Blockly.Msg.handwritten_text_load);
  6169. this.setHelpUrl("");
  6170. }
  6171. }
  6172. Blockly.Python.ai_online_ID_card_loading = function (block) {
  6173. let text = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC)
  6174. // let language = Blockly.Python.valueToCode(block, 'language', Blockly.Python.ORDER_ATOMIC);
  6175. let code = `with open(${text}, "rb") as f:
  6176. imageBytes = f.read()
  6177. url = 'http://api.xf-yun.com/v1/private/s5ccecfce'
  6178. body = {
  6179. "header": {
  6180. "app_id": APPId,
  6181. "status": 3,
  6182. },
  6183. "parameter": {
  6184. "s5ccecfce": {
  6185. "template_list": "id_card",
  6186. "result": {
  6187. "encoding": "utf8",
  6188. "compress": "raw",
  6189. "format": "json"
  6190. }
  6191. }
  6192. },
  6193. "payload": {
  6194. "s5ccecfce_data_1": {
  6195. "encoding": "jpg",
  6196. "image": str(base64.b64encode(imageBytes), 'UTF-8'),
  6197. "status": 3
  6198. }
  6199. }
  6200. }
  6201. request_Id_card_url = assemble_ws_auth_url(url, "POST", APIKey, APISecret)
  6202. headers = {'content-type': "application/json", 'host': 'api.xf-yun.com', 'app_id': APPId}
  6203. #print(request_url)
  6204. Id_card_response = requests.post(request_Id_card_url, data=json.dumps(body), headers=headers)
  6205. IDCardtempResult = json.loads(Id_card_response.content.decode())
  6206. IDCardfinalResult = base64.b64decode(IDCard['payload']['result']['text']).decode()
  6207. IDCardfinalResult = IDCardfinalResult.replace(" ", "").replace("\n", "").replace("\t", "").strip()
  6208. `
  6209. return code
  6210. }
  6211. Blockly.Blocks['ai_online_ID_card_result'] = {
  6212. init: function () {
  6213. this.appendDummyInput()
  6214. .appendField('身份证识别结果'); //
  6215. this.setOutput(true, null);
  6216. this.setColour("#0000ff");
  6217. this.setTooltip(Blockly.Msg.handwritten_text_result);
  6218. this.setHelpUrl("");
  6219. }
  6220. };
  6221. Blockly.Python['ai_online_ID_card_result'] = function (block) {
  6222. let code = `IDCardfinalResult`;
  6223. return [code, Blockly.Python.ORDER_ATOMIC];
  6224. };
  6225. // 情绪识别
  6226. Blockly.Blocks['ai_online_emotion_init'] = {
  6227. init: function () {
  6228. this.appendDummyInput()
  6229. .appendField(Blockly.Msg.emotion_init);
  6230. // this.appendValueInput("path")
  6231. // .setCheck(null).appendField(Blockly.Msg.set_dictionary)
  6232. // .appendField(new Blockly.FieldVariable("emotion_recognize_result"), "varitem");
  6233. // this.appendDummyInput().appendField(Blockly.Msg.dictionary_contain);
  6234. this.setInputsInline(false)
  6235. this.setPreviousStatement(true, null);
  6236. this.setNextStatement(true, null);; //
  6237. this.setColour("#0000ff");
  6238. this.setTooltip(Blockly.Msg.emotion_init);
  6239. this.setHelpUrl("");
  6240. }
  6241. };
  6242. Blockly.Python['ai_online_emotion_init'] = function (block) {
  6243. // let = []
  6244. // let text = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC)
  6245. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  6246. // TODO: Assemble Python into code variable.
  6247. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  6248. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  6249. Blockly.Python.definitions_['v831_import_json'] = `import json`
  6250. Blockly.Python.addVariable("EMOTION_SEND_REQUEST", "EMOTION_SEND_REQUEST = {}", true);
  6251. Blockly.Python.definitions_['emotion_init'] = `def get_img_base64str(single_image_path):
  6252. with open(single_image_path, "rb") as fp:
  6253. imgbase64 = base64.b64encode(fp.read())
  6254. return imgbase64.decode()
  6255. url = "https://ai-api.cocorobo.hk/face"
  6256. `
  6257. // ${variable_name} =${text}
  6258. var code = ``;
  6259. return code;
  6260. };
  6261. Blockly.Blocks['ai_online_emotion_loading'] = {
  6262. init: function () {
  6263. this.appendDummyInput()
  6264. .appendField(Blockly.Msg.emotion_load); //
  6265. this.appendValueInput("path")
  6266. .setCheck(null)
  6267. .appendField(Blockly.Msg.online_face_init_path); //
  6268. this.setPreviousStatement(true, null);
  6269. this.setNextStatement(true, null);
  6270. this.setColour("#0000ff");
  6271. this.setTooltip(Blockly.Msg.emotion_load);
  6272. this.setHelpUrl("");
  6273. }
  6274. }
  6275. Blockly.Python.ai_online_emotion_loading = function (block) {
  6276. let text = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC)
  6277. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  6278. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  6279. Blockly.Python.definitions_['v831_import_json'] = `import json`
  6280. Blockly.Python.addVariable("EMOTION_SEND_REQUEST", "EMOTION_SEND_REQUEST = {}", true);
  6281. Blockly.Python.definitions_['emotion_init'] = `def get_img_base64str(single_image_path):
  6282. with open(single_image_path, "rb") as fp:
  6283. imgbase64 = base64.b64encode(fp.read())
  6284. return imgbase64.decode()
  6285. url = "https://ai-api.cocorobo.cn/face"
  6286. `
  6287. // let language = Blockly.Python.valueToCode(block, 'language', Blockly.Python.ORDER_ATOMIC);
  6288. let code = `imageEmotionURLbase64 = str(get_img_base64str(${text}))
  6289. # print(imageURLbase64)
  6290. data = "{\\"image\\":\\"" + str(imageEmotionURLbase64) + "\\"}"
  6291. try:
  6292. EMOTION_SEND_REQUEST = requests.post(url, data = data , headers = { "Ocp-Apim-Subscription-Key": "6baf787532ef4eec8bc93c88518b8916", "Content-type": "application/json" }, timeout = 10000)
  6293. EMOTION_SEND_REQUEST = json.loads(EMOTION_SEND_REQUEST.content)["data"]["result"]["face_list"][0]
  6294. # print(str(_COCOCLOUD_SEND_REQUEST.status_code)+", "+str(_COCOCLOUD_SEND_REQUEST.content))
  6295. except BaseException as e:
  6296. EMOTION_SEND_REQUEST = "no result"
  6297. pass
  6298. `
  6299. return code
  6300. }
  6301. Blockly.Blocks['ai_online_emotion_result'] = {
  6302. init: function () {
  6303. this.appendDummyInput()
  6304. .appendField(Blockly.Msg.emotion_result)
  6305. .appendField(new Blockly.FieldDropdown([
  6306. [Blockly.Msg.emotion_result_age, "age"],
  6307. [Blockly.Msg.emotion_result_emotion, "emotion"],
  6308. [Blockly.Msg.emotion_result_gender, "gender"],
  6309. [Blockly.Msg.emotion_result_expression, "expression"]
  6310. ]), "emotion_type");
  6311. this.setOutput(true, null);
  6312. this.setColour("#0000ff");
  6313. this.setTooltip(Blockly.Msg.emotion_result);
  6314. this.setHelpUrl("");
  6315. }
  6316. };
  6317. Blockly.Python['ai_online_emotion_result'] = function (block) {
  6318. var type = block.getFieldValue('emotion_type');
  6319. let code = ""
  6320. // console.log(type)
  6321. if (type == "age") {
  6322. code = `EMOTION_SEND_REQUEST["age"]`
  6323. } else {
  6324. code = `EMOTION_SEND_REQUEST["${type}"]["type"]`
  6325. }
  6326. return [code, Blockly.Python.ORDER_ATOMIC];
  6327. };
  6328. // 手势识别
  6329. Blockly.Blocks['ai_online_gesture_init'] = {
  6330. init: function () {
  6331. this.appendDummyInput()
  6332. .appendField(Blockly.Msg.gesture_init); //
  6333. this.setPreviousStatement(true, null);
  6334. this.setNextStatement(true, null);
  6335. this.setColour("#0000ff");
  6336. this.setTooltip(Blockly.Msg.gesture_init);
  6337. this.setHelpUrl("");
  6338. }
  6339. };
  6340. Blockly.Python['ai_online_gesture_init'] = function (block) {
  6341. // let = []
  6342. // TODO: Assemble Python into code variable.
  6343. Blockly.Python.definitions_['v831_import_fromPIL_Image'] = `from PIL import Image`
  6344. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  6345. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  6346. Blockly.Python.definitions_['v831_import_json'] = `import json`
  6347. Blockly.Python.definitions_['emotion_init'] = `
  6348. def getImgData(single_image_path):
  6349. imgGesture = Image.open(single_image_path).convert("RGB")
  6350. imgGesture.save(single_image_path.split(".")[0] + ".png", "png")
  6351. with open(single_image_path.split(".")[0] + ".png", "rb") as fp:
  6352. return fp.read()
  6353. url = "https://ai-api.cocorobo.hk/gesture"
  6354. `
  6355. var code = '';
  6356. return code;
  6357. };
  6358. Blockly.Blocks['ai_online_gesture_loading'] = {
  6359. init: function () {
  6360. this.appendDummyInput()
  6361. .appendField(Blockly.Msg.gesture_load);
  6362. this.appendValueInput("path")
  6363. .setCheck(null)
  6364. .appendField(Blockly.Msg.online_face_init_path); //
  6365. this.setPreviousStatement(true, null);
  6366. this.setNextStatement(true, null);
  6367. this.setColour("#0000ff");
  6368. this.setTooltip(Blockly.Msg.gesture_load);
  6369. this.setHelpUrl("");
  6370. }
  6371. }
  6372. Blockly.Python.ai_online_gesture_loading = function (block) {
  6373. let text = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC)
  6374. Blockly.Python.definitions_['v831_import_fromPIL_Image'] = `from PIL import Image`
  6375. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  6376. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  6377. Blockly.Python.definitions_['v831_import_json'] = `import json`
  6378. Blockly.Python.definitions_['emotion_init'] = `
  6379. def getImgData(single_image_path):
  6380. imgGesture = Image.open(single_image_path).convert("RGB")
  6381. imgGesture.save(single_image_path.split(".")[0] + ".png", "png")
  6382. with open(single_image_path.split(".")[0] + ".png", "rb") as fp:
  6383. return fp.read()
  6384. url = "https://ai-api.cocorobo.cn/gesture"
  6385. `
  6386. let code = `try:
  6387. payload={}
  6388. files=[("image",("filename.png", getImgData(${text}),"image/png"))]
  6389. headers = {}
  6390. GESTURE_SEND_REQUEST = requests.post(url, headers=headers, data=payload, files=files, timeout = 10000)
  6391. GESTURE_SEND_REQUEST = json.loads(GESTURE_SEND_REQUEST.content)["data"]["result"][0]["classname"]
  6392. # print(str(_COCOCLOUD_SEND_REQUEST.status_code)+", "+str(_COCOCLOUD_SEND_REQUEST.content))
  6393. except BaseException as e:
  6394. GESTURE_SEND_REQUEST = "no result"
  6395. pass
  6396. `
  6397. return code
  6398. }
  6399. Blockly.Blocks['ai_online_gesture_result'] = {
  6400. init: function () {
  6401. this.appendDummyInput()
  6402. .appendField(Blockly.Msg.gesture_result);
  6403. this.setOutput(true, null);
  6404. this.setColour("#0000ff");
  6405. this.setTooltip(Blockly.Msg.gesture_result);
  6406. this.setHelpUrl("");
  6407. }
  6408. };
  6409. Blockly.Python['ai_online_gesture_result'] = function (block) {
  6410. let code = `GESTURE_SEND_REQUEST`
  6411. return [code, Blockly.Python.ORDER_ATOMIC];
  6412. };
  6413. Blockly.Blocks['ai_audio_set'] = {
  6414. init: function () {
  6415. this.appendValueInput("Num")
  6416. .setCheck(null)
  6417. .appendField(Blockly.Msg.ai_audio_set_music_vol);
  6418. this.setInputsInline(true);
  6419. this.setPreviousStatement(true, null);
  6420. this.setNextStatement(true, null);
  6421. this.setColour("#603ea0");
  6422. this.setTooltip("");
  6423. this.setHelpUrl("");
  6424. }
  6425. };
  6426. Blockly.Python.ai_audio_set = function (block) {
  6427. var _num = Blockly.Python.valueToCode(block, 'Num', Blockly.Python.ORDER_ATOMIC);
  6428. var _code = "_player.volume(" + _num + ")\n"
  6429. return _code;
  6430. }
  6431. var SPEECH_RECOGNITION_BLOCK_COLOR = "230";
  6432. Blockly.Blocks['speech_recognition_new_setup'] = {
  6433. init: function () {
  6434. this.appendDummyInput()
  6435. .appendField(new Blockly.FieldImage("blockly/media/speech_recognition_header_new.png", 70, 70, { alt: "*", flipRtl: "FALSE" }));
  6436. this.appendDummyInput()
  6437. .appendField(Blockly.Msg.ai_speech_recognition_setup);
  6438. this.setPreviousStatement(true, null);
  6439. this.setNextStatement(true, null);
  6440. this.setColour("#ee783a");
  6441. this.setTooltip(Blockly.Msg.speech_recognition_new_setup_TOOLTIP);
  6442. this.setHelpUrl("");
  6443. }
  6444. };
  6445. Blockly.Python['speech_recognition_new_setup'] = function (block) {
  6446. Blockly.Python.definitions_["v831_import_coco_mfcc"] = `import _coco_mfcc`
  6447. Blockly.Python.definitions_["v831_import_time"] = `import time`
  6448. Blockly.Python.definitions_["v831_coco_mfcc_init"] = `mfcc = _coco_mfcc.MFCC(is_reply=False)`;
  6449. Blockly.Python.addVariable("RecordState", "RecordState = 0", true);
  6450. Blockly.Python.addVariable("recordFrequency", "recordFrequency = 0", true);
  6451. Blockly.Python.addVariable("recordResultData", `recordResultData = None`, true);
  6452. let code = ""
  6453. return code;
  6454. };
  6455. Blockly.Blocks['speech_recognition_clear'] = {
  6456. init: function () {
  6457. // this.appendDummyInput()
  6458. // .appendField(new Blockly.FieldImage("blockly/media/speech_recognition_header_new.png", 70, 70, { alt: "*", flipRtl: "FALSE" }));
  6459. this.appendDummyInput()
  6460. .appendField(Blockly.Msg.speech_recognition_Clean_up_the_last_study_result);
  6461. this.setPreviousStatement(true, null);
  6462. this.setNextStatement(true, null);
  6463. this.setColour("#ee783a");
  6464. this.setTooltip(Blockly.Msg.speech_recognition_Clean_up_the_last_study_result);
  6465. this.setHelpUrl("");
  6466. }
  6467. };
  6468. Blockly.Python['speech_recognition_clear'] = function (block) {
  6469. Blockly.Python.definitions_["v831_import_coco_mfcc"] = `import _coco_mfcc`
  6470. Blockly.Python.definitions_["v831_coco_mfcc_init"] = `mfcc = _coco_mfcc.MFCC(is_reply=False)`;
  6471. var code = `mfcc.clear()
  6472. `;
  6473. return code;
  6474. };
  6475. Blockly.Blocks['speech_recognition_record_start_result'] = {
  6476. init: function () {
  6477. this.appendDummyInput()
  6478. .appendField(new Blockly.FieldImage("blockly/media/speech_recognition_header_recording_new.png", 130, 60, { alt: "*", flipRtl: "FALSE" }));
  6479. this.appendDummyInput()
  6480. .appendField(Blockly.Msg.ai_speech_recognition_start_recording);
  6481. this.appendDummyInput()
  6482. .appendField(Blockly.Msg.ai_speech_recognition_start_recording_clip_text)
  6483. .appendField(new Blockly.FieldNumber(2, 1, 6, 1), "clip")
  6484. // .appendField(Blockly.Msg.ai_speech_recognition_start_recording_take_note);
  6485. this.appendDummyInput()
  6486. .appendField(Blockly.Msg.image_process_interface)
  6487. .appendField(new Blockly.FieldVariable("recordCanvas"), "varitem");
  6488. this.setPreviousStatement(true, null);
  6489. this.setNextStatement(true, null);
  6490. this.setColour("#ee783a");
  6491. this.setTooltip(Blockly.Msg.ai_speech_recognition_start_recording);
  6492. this.setHelpUrl("");
  6493. }
  6494. };
  6495. Blockly.Python['speech_recognition_record_start_result'] = function (block) {
  6496. var number_take = block.getFieldValue('clip');
  6497. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  6498. Blockly.Python.definitions_["v831_import_coco_mfcc"] = `import _coco_mfcc`
  6499. Blockly.Python.definitions_["v831_import_time"] = `import time`
  6500. Blockly.Python.definitions_["v831_coco_mfcc_init"] = `mfcc = _coco_mfcc.MFCC(is_reply=False)`;
  6501. Blockly.Python.addVariable("RecordState", "RecordState = 0", true);
  6502. Blockly.Python.addVariable("recordFrequency", "recordFrequency = 0", true);
  6503. Blockly.Python.addVariable("recordResultData", `recordResultData = None`, true);
  6504. Blockly.Python.addVariable('number_take', `number_take = ${number_take}`, true)
  6505. Blockly.Python.definitions_["v831_mfcc_clear"] = `mfcc.clear()
  6506. `
  6507. Blockly.Python.definitions_['v831_import_display'] = `from maix import display`;
  6508. Blockly.Python.definitions_['v831_import_image'] = `from maix import image`;
  6509. Blockly.Python.definitions_['v831_import_camera'] = `from maix import camera`;
  6510. Blockly.Python.definitions_['v831_import_os'] = `import os`
  6511. Blockly.Python.definitions_['v831_camera_set'] = `cameraSize = True
  6512. def CAMERATYPE():
  6513. global cameraSize
  6514. try:
  6515. if os.path.exists("/etc/cameraSize.cfg"):
  6516. cameraSize = True
  6517. else:
  6518. cameraSize = False
  6519. except:
  6520. cameraSize = False
  6521. CAMERATYPE()
  6522. `
  6523. Blockly.Python.addVariable("ScreenOrientation", `ScreenOrientation = False`, true)
  6524. Blockly.Python.addVariable(variable_name, `${variable_name} = image.new(size = (320, 240))`, true)
  6525. Blockly.Python.definitions_['import_image'] = `image.load_freetype("/root/preset/fonts/simhei.ttf")
  6526. if cameraSize==True:
  6527. camera.camera.config(size=(320,240))
  6528. else:
  6529. camera.camera.config(size=(240,320))
  6530. `
  6531. let idenSuccess = "录制成功";
  6532. let speek = "说第";
  6533. let SpeechFragment = "语音片段";
  6534. if (localStorage.getItem('handPyLanguage') == "zh-hant") {
  6535. idenSuccess = "錄製成功";
  6536. speek = "說第 ";
  6537. SpeechFragment = "語音片段";
  6538. } else if (localStorage.getItem('handPyLanguage') == "en") {
  6539. idenSuccess = "Recorded successfully";
  6540. speek = "Speek ";
  6541. SpeechFragment = "Speech fragment";
  6542. }
  6543. var code = `if RecordState == 0:
  6544. mfcc.recording(recordFrequency)
  6545. recordFrequency = recordFrequency + 1
  6546. time.sleep(1)
  6547. RecordState = 1
  6548. if RecordState == 1:
  6549. recordData = mfcc.state()
  6550. if recordData == mfcc._mfcc_result:
  6551. ${variable_name}.draw_string(0,80,"${idenSuccess}", scale = 2, color = (0,204,204) , thickness = 1)
  6552. time.sleep(1)
  6553. if recordFrequency < number_take:
  6554. RecordState = 0
  6555. else:
  6556. ${variable_name}.draw_string(0,80, "${speek} "+str(recordFrequency)+" ${SpeechFragment}", scale = 2, color = (0,204,204) , thickness = 1)
  6557. `
  6558. return code
  6559. };
  6560. Blockly.Blocks['speech_recognition_new_start_recognition'] = {
  6561. init: function () {
  6562. this.appendDummyInput()
  6563. .appendField(new Blockly.FieldImage("blockly/media/speech_recognition_header_recognition_new.png", 130, 60, { alt: "*", flipRtl: "FALSE" }));
  6564. this.appendDummyInput()
  6565. .appendField(Blockly.Msg.ai_speech_recognition_start_recognizing);
  6566. this.setPreviousStatement(true, null);
  6567. this.setNextStatement(true, null);
  6568. this.setColour("#ee783a");
  6569. this.setTooltip(Blockly.Msg.speech_recognition_new_start_recognition_TOOLTIP);
  6570. this.setHelpUrl("");
  6571. }
  6572. };
  6573. Blockly.Python['speech_recognition_new_start_recognition'] = function (block) {
  6574. Blockly.Python.definitions_["v831_import_coco_mfcc"] = `import _coco_mfcc`
  6575. Blockly.Python.definitions_["v831_import_time"] = `import time`
  6576. Blockly.Python.definitions_["v831_coco_mfcc_init"] = `mfcc = _coco_mfcc.MFCC(is_reply=False)`;
  6577. Blockly.Python.addVariable("RecordState", "RecordState = 0", true);
  6578. Blockly.Python.addVariable("recordFrequency", "recordFrequency = 0", true);
  6579. Blockly.Python.addVariable("recordResultData", `recordResultData = None`, true);
  6580. var code = `if RecordState == 0:
  6581. mfcc.recognize()
  6582. recordResultData = None
  6583. time.sleep(1)
  6584. RecordState = 1
  6585. `
  6586. return code
  6587. };
  6588. Blockly.Blocks['speech_recognition_new_recognition_get_any_result'] = {
  6589. init: function () {
  6590. this.appendDummyInput()
  6591. .appendField(Blockly.Msg.ai_speech_recognition_any_result_text);
  6592. this.appendStatementInput("NAME")
  6593. .setCheck(null)
  6594. .appendField(Blockly.Msg.ai_speech_recognition_any_result_text_do);
  6595. this.setPreviousStatement(true, null);
  6596. this.setNextStatement(true, null);
  6597. this.setColour("#ee783a");
  6598. this.setTooltip(Blockly.Msg.speech_recognition_new_recognition_get_any_result_TOOLTIP);
  6599. this.setHelpUrl("");
  6600. }
  6601. };
  6602. Blockly.Python['speech_recognition_new_recognition_get_any_result'] = function (block) {
  6603. var statements_name = Blockly.Python.statementToCode(block, 'NAME');
  6604. // TODO: Assemble Python into code variable.
  6605. var code = `if RecordState == 1:
  6606. recordData = mfcc.state()
  6607. if recordData == mfcc._mfcc_result:
  6608. recordResultData = mfcc.result()
  6609. time.sleep(1)
  6610. RecordState = 0
  6611. ${statements_name}
  6612. `;
  6613. return code;
  6614. };
  6615. Blockly.Blocks['speech_recognition_new_recognition_get_result'] = {
  6616. init: function () {
  6617. this.appendDummyInput()
  6618. .appendField(Blockly.Msg.ai_speech_recognition_get_result)
  6619. .appendField(new Blockly.FieldNumber(1, 1, 6, 1), "index")
  6620. .appendField(Blockly.Msg.ai_speech_recognition_get_result_after);
  6621. this.setOutput(true, null);
  6622. this.setColour("#ee783a");
  6623. this.setTooltip(Blockly.Msg.speech_recognition_new_recognition_get_result_TOOLTIP);
  6624. this.setHelpUrl("");
  6625. }
  6626. };
  6627. Blockly.Python['speech_recognition_new_recognition_get_result'] = function (block) {
  6628. var number_index = block.getFieldValue('index');
  6629. // TODO: Assemble Python into code variable.
  6630. var code = `recordResultData == ${number_index - 1}`;
  6631. // TODO: Change ORDER_NONE to the correct strength.
  6632. return [code, Blockly.Python.ORDER_NONE];
  6633. };
  6634. /*
  6635. _ ___ ____ _
  6636. / \ |_ _| / ___| _ __ ___ __ _| | _____ _ __
  6637. / _ \ | | \___ \| '_ \ / _ \/ _` | |/ / _ \ '__|
  6638. / ___ \ _ | | _ ___) | |_) | __/ (_| | < __/ |
  6639. /_/ \_(_)___(_) |____/| .__/ \___|\__,_|_|\_\___|_|
  6640. |_|
  6641. */
  6642. Blockly.Blocks['ai_audio_init_speaker'] = {
  6643. init: function () {
  6644. this.appendDummyInput()
  6645. .appendField(new Blockly.FieldImage("blockly/media/speaker_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  6646. this.appendDummyInput()
  6647. .appendField(Blockly.Msg.ai_audio_init);
  6648. this.setColour("#5f7fc1");
  6649. this.setTooltip(Blockly.Msg.ai_audio_init_speaker_TOOLTIP);
  6650. this.setHelpUrl("");
  6651. this.setPreviousStatement(true);
  6652. this.setNextStatement(true);
  6653. }
  6654. };
  6655. Blockly.Python.ai_audio_init_speaker = function (block) {
  6656. Blockly.Python.definitions_.import_Maix = "from Maix import I2S, GPIO";
  6657. Blockly.Python.definitions_.import_fpioa_manager = "from fpioa_manager import *";
  6658. Blockly.Python.definitions_.import_audio = "import audio";
  6659. var _code = "";
  6660. return _code;
  6661. }
  6662. Blockly.Blocks['ai_audio_play_song_speaker'] = {
  6663. init: function () {
  6664. this.appendDummyInput()
  6665. .appendField(Blockly.Msg.ai_audio_play_music_file);
  6666. // this.appendDummyInput()
  6667. // .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol)
  6668. // .appendField(new Blockly.FieldNumber(95, 0, 100, 1), "play_vol");
  6669. this.appendValueInput("volume")
  6670. .setCheck(null)
  6671. .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol);
  6672. this.appendValueInput("path")
  6673. .setCheck(null)
  6674. .appendField(Blockly.Msg.ai_audio_play_music_file_set_file_path);
  6675. this.setPreviousStatement(true, null);
  6676. this.setNextStatement(true, null);
  6677. this.setColour("#5f7fc1");
  6678. this.setTooltip(Blockly.Msg.ai_audio_play_song_speaker_TOOLTIP);
  6679. this.setHelpUrl("");
  6680. }
  6681. };
  6682. Blockly.Python['ai_audio_play_song_speaker'] = function (block) {
  6683. // var number_play_vol = block.getFieldValue('play_vol');
  6684. var number_play_vol = Blockly.Python.valueToCode(block, 'volume', Blockly.Python.ORDER_ATOMIC);
  6685. var value_name = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  6686. var _in = "" +
  6687. "_audio_rx = I2S(I2S.DEVICE_1)\n" +
  6688. "\n" +
  6689. "def _speaker_play_song(path, volume):\n" +
  6690. " global _audio_play_state,_player\n" +
  6691. " _player = audio.Audio(path = path)\n" +
  6692. " _player.volume(volume)\n" +
  6693. " try:\n" +
  6694. " _wav_info = _player.play_process(_audio_rx)\n" +
  6695. " _audio_rx.channel_config(_audio_rx.CHANNEL_1, I2S.TRANSMITTER, resolution = I2S.RESOLUTION_16_BIT, align_mode = I2S.STANDARD_MODE)\n" +
  6696. " _audio_rx.set_sample_rate(_wav_info[1])\n" +
  6697. " _audio_play_state = 0\n" +
  6698. "\n" +
  6699. " fm.register(34,fm.fpioa.I2S1_OUT_D1)\n" +
  6700. " fm.register(35,fm.fpioa.I2S1_SCLK)\n" +
  6701. " fm.register(33,fm.fpioa.I2S1_WS)\n" +
  6702. "\n" +
  6703. " while True:\n" +
  6704. " _audio_play_state = _player.play()\n" +
  6705. " if (_audio_play_state) == True:\n" +
  6706. " print(\"Playing.\")\n" +
  6707. " elif (_audio_play_state) == False:\n" +
  6708. " print(\"Done.\")\n" +
  6709. " break\n" +
  6710. "\n" +
  6711. " except:\n" +
  6712. " pass\n" +
  6713. "\n";
  6714. // Blockly.Python.definitions_.import_audio = _in;
  6715. Blockly.Python.addVariable('_audio_play_state', _in, true);
  6716. // Blockly.Python.addSetup("ai_audio_read", _in);
  6717. var _code = "_speaker_play_song(path=" + value_name + ", volume=" + number_play_vol + ")\n";
  6718. return _code;
  6719. };
  6720. Blockly.Blocks['ai_audio_play_finish_speaker'] = {
  6721. init: function () {
  6722. this.appendDummyInput()
  6723. .appendField(Blockly.Msg.ai_audio_end_play);
  6724. this.setPreviousStatement(true, null);
  6725. this.setNextStatement(true, null);
  6726. this.setColour("#5f7fc1");
  6727. this.setTooltip(Blockly.Msg.ai_audio_play_finish_speaker_TOOLTIP);
  6728. this.setHelpUrl("");
  6729. }
  6730. };
  6731. Blockly.Python['ai_audio_play_finish_speaker'] = function (block) {
  6732. // TODO: Assemble Python into code variable.
  6733. var code = '_player.finish()\n';
  6734. return code;
  6735. };
  6736. Blockly.Blocks['ai_audio_set_speaker'] = {
  6737. init: function () {
  6738. this.appendValueInput("Num")
  6739. .setCheck(null)
  6740. .appendField(Blockly.Msg.ai_audio_set_music_vol);
  6741. this.setInputsInline(true);
  6742. this.setPreviousStatement(true, null);
  6743. this.setNextStatement(true, null);
  6744. this.setColour("#5f7fc1");
  6745. this.setTooltip(Blockly.Msg.ai_audio_set_speaker_TOOLTIP);
  6746. this.setHelpUrl("");
  6747. }
  6748. };
  6749. Blockly.Python.ai_audio_set_speaker = function (block) {
  6750. var _num = Blockly.Python.valueToCode(block, 'Num', Blockly.Python.ORDER_ATOMIC);
  6751. var _code = "_player.volume(" + _num + ")\n"
  6752. return _code;
  6753. }
  6754. Blockly.Blocks['ai_audio_files_list'] = {
  6755. init: function () {
  6756. this.appendDummyInput()
  6757. .appendField(Blockly.Msg.ai_audio_files_list);
  6758. this.appendValueInput("path")
  6759. .setCheck(null)
  6760. .appendField(Blockly.Msg.ai_audio_files_list_set_file_path);
  6761. this.setOutput(true, null);
  6762. this.setColour("#5f7fc1");
  6763. this.setTooltip(Blockly.Msg.ai_audio_files_list_TOOLTIP);
  6764. this.setHelpUrl("");
  6765. }
  6766. };
  6767. Blockly.Python.ai_audio_files_list = function (block) {
  6768. var value_name = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  6769. Blockly.Python.definitions_['v831_import_os'] = `import os`
  6770. Blockly.Python.definitions_.import_ai_audio_files_list = '' +
  6771. 'def getRawFileList(path):\n' +
  6772. ' files = []\n' +
  6773. ' for f in os.listdir(path):\n' +
  6774. ' if (not f.endswith("~") or not f == "") and f.split(".")[1] == "wav":\n' +
  6775. ' files.append(path + "/" + f)\n' +
  6776. ' return files\n' +
  6777. '\n';
  6778. var code = 'getRawFileList(' + value_name + ')';
  6779. return [code, Blockly.Python.ORDER_NONE];
  6780. };
  6781. Blockly.Blocks['ai_audio_play_song_list_loop_speaker'] = {
  6782. init: function () {
  6783. this.appendDummyInput()
  6784. .appendField(Blockly.Msg.ai_audio_play_music_file_list_loop);
  6785. // this.appendDummyInput()
  6786. // .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol)
  6787. // .appendField(new Blockly.FieldNumber(95, 0, 100, 1), "play_vol");
  6788. this.appendValueInput("volume")
  6789. .setCheck(null)
  6790. .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol);
  6791. // this.appendValueInput("path")
  6792. // .setCheck(null)
  6793. // .appendField(Blockly.Msg.ai_audio_play_music_file_set_file_path);
  6794. this.appendValueInput("my_list")
  6795. .setCheck(null)
  6796. .appendField(Blockly.Msg.ai_audio_play_music_file_set_files_list);
  6797. this.appendValueInput("firstIndex")
  6798. .setCheck(null)
  6799. .appendField(Blockly.Msg.ai_audio_play_music_file_set_first_index);
  6800. this.setPreviousStatement(true, null);
  6801. this.setNextStatement(true, null);
  6802. this.setColour("#5f7fc1");
  6803. this.setTooltip(Blockly.Msg.ai_audio_play_song_speaker_list_loop_TOOLTIP);
  6804. this.setHelpUrl("");
  6805. }
  6806. };
  6807. Blockly.Python['ai_audio_play_song_list_loop_speaker'] = function (block) {
  6808. // var number_play_vol = block.getFieldValue('play_vol');
  6809. var number_play_vol = Blockly.Python.valueToCode(block, 'volume', Blockly.Python.ORDER_ATOMIC);
  6810. // var value_name = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  6811. var my_list = Blockly.Python.valueToCode(block, 'my_list', Blockly.Python.ORDER_ATOMIC);
  6812. var firstIndex = Blockly.Python.valueToCode(block, 'firstIndex', Blockly.Python.ORDER_ATOMIC);
  6813. var _in = "" +
  6814. "_audio_rx = I2S(I2S.DEVICE_1)\n" +
  6815. "direction = 0\n" +
  6816. "firstIndex = " + firstIndex + "\n" +
  6817. "\n" +
  6818. "# 上一首\n" +
  6819. "def previous_song(index,my_list):\n" +
  6820. " global direction\n" +
  6821. " index = index - 1\n" +
  6822. " direction = 1\n" +
  6823. " if index == -1:\n" +
  6824. " index = len(my_list) - 1\n" +
  6825. " return index\n" +
  6826. "\n" +
  6827. "# 切换上一首\n" +
  6828. "def change_previous_song(index):\n" +
  6829. " global _audio_play_state,direction,firstIndex,_player\n" +
  6830. " _player.finish()\n" +
  6831. " _audio_play_state = 0\n" +
  6832. " firstIndex = index\n" +
  6833. " direction = 1\n" +
  6834. "\n" +
  6835. "# 下一首\n" +
  6836. "def next_song(index,my_list):\n" +
  6837. " global direction\n" +
  6838. " index = index + 1\n" +
  6839. " direction = 2\n" +
  6840. " if index == len(my_list):\n" +
  6841. " index = 0\n" +
  6842. " return index\n" +
  6843. "\n" +
  6844. "# 切换下一首\n" +
  6845. "def change_next_song(index):\n" +
  6846. " global _audio_play_state,direction,firstIndex,_player\n" +
  6847. " _player.finish()\n" +
  6848. " _audio_play_state = 0\n" +
  6849. " firstIndex = index\n" +
  6850. " direction = 2\n" +
  6851. "\n" +
  6852. "def _speaker_play_song_list_loop(volume, my_list):\n" +
  6853. " global _audio_play_state,_player,firstIndex,play_again,direction\n" +
  6854. " _player = audio.Audio(path = my_list[firstIndex])\n" +
  6855. " _player.volume(volume)\n" +
  6856. " try:\n" +
  6857. // " firstIndex = index\n" +
  6858. " _wav_info = _player.play_process(_audio_rx)\n" +
  6859. " _audio_rx.channel_config(_audio_rx.CHANNEL_1, I2S.TRANSMITTER, resolution = I2S.RESOLUTION_16_BIT, align_mode = I2S.STANDARD_MODE)\n" +
  6860. " _audio_rx.set_sample_rate(_wav_info[1])\n" +
  6861. " _audio_play_state = 0\n" +
  6862. "\n" +
  6863. " fm.register(34,fm.fpioa.I2S1_OUT_D1)\n" +
  6864. " fm.register(35,fm.fpioa.I2S1_SCLK)\n" +
  6865. " fm.register(33,fm.fpioa.I2S1_WS)\n" +
  6866. "\n" +
  6867. " button_a_state = False\n" +
  6868. " button_b_state = False\n" +
  6869. " while True:\n" +
  6870. " _audio_play_state = _player.play()\n" +
  6871. " if (_audio_play_state) == True:\n" +
  6872. " #print(\"Playing.\")\n" +
  6873. " if (_gp_side_a.value() == 1) and button_a_state == False:\n" +
  6874. " index = previous_song(firstIndex,my_list)\n" +
  6875. " button_a_state = True\n" +
  6876. " elif (_gp_side_a.value() == 0) and button_a_state == True:\n" +
  6877. " button_a_state = False\n" +
  6878. " change_previous_song(index)\n" +
  6879. " break\n" +
  6880. " if (_gp_side_b.value() == 1) and button_b_state == False:\n" +
  6881. " index = next_song(firstIndex,my_list)\n" +
  6882. " button_b_state = True\n" +
  6883. " elif (_gp_side_b.value() == 0) and button_b_state == True:\n" +
  6884. " button_b_state = False\n" +
  6885. " change_next_song(index)\n" +
  6886. " break\n" +
  6887. "\n" +
  6888. " elif (_audio_play_state) == False:\n" +
  6889. " #print(\"Done.\")\n" +
  6890. " index = firstIndex + 1\n" +
  6891. " if index == len(my_list):\n" +
  6892. " index = 0\n" +
  6893. " firstIndex = index\n" +
  6894. " break\n" +
  6895. "\n" +
  6896. " except:\n" +
  6897. " play_again = True\n" +
  6898. " if direction == 0:\n" +
  6899. " direction = 3\n" +
  6900. " return\n" +
  6901. "\n";
  6902. // Blockly.Python.definitions_.import_audio = _in;
  6903. Blockly.Python.addVariable('_audio_play_list_loop_state', _in, true);
  6904. Blockly.Python.definitions_['import_define_x_ai_button'] = '' +
  6905. 'from fpioa_manager import *\n' +
  6906. 'from Maix import FPIOA, GPIO\n' +
  6907. '\n' +
  6908. '_gp_side_buttons = [9, 10, 11]\n' +
  6909. '\n' +
  6910. 'FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)\n' +
  6911. 'FPIOA().set_function(_gp_side_buttons[1],FPIOA.GPIO1)\n' +
  6912. 'FPIOA().set_function(_gp_side_buttons[2],FPIOA.GPIO2)\n' +
  6913. '\n' +
  6914. '_gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)\n' +
  6915. '_gp_side_b = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)\n' +
  6916. '_gp_side_c = GPIO(GPIO.GPIO2,GPIO.IN,GPIO.PULL_UP)\n' +
  6917. '\n';
  6918. // Blockly.Python.addSetup("ai_audio_read", _in);
  6919. var _code = "_speaker_play_song_list_loop(volume=" + number_play_vol + ", my_list=" + my_list + ")\n";
  6920. return _code;
  6921. };
  6922. Blockly.Blocks['ai_audio_play_song_list_random_speaker'] = {
  6923. init: function () {
  6924. this.appendDummyInput()
  6925. .appendField(Blockly.Msg.ai_audio_play_music_file_list_random);
  6926. // this.appendDummyInput()
  6927. // .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol)
  6928. // .appendField(new Blockly.FieldNumber(95, 0, 100, 1), "play_vol");
  6929. this.appendValueInput("volume")
  6930. .setCheck(null)
  6931. .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol);
  6932. // this.appendValueInput("path")
  6933. // .setCheck(null)
  6934. // .appendField(Blockly.Msg.ai_audio_play_music_file_set_file_path);
  6935. this.appendValueInput("my_list")
  6936. .setCheck(null)
  6937. .appendField(Blockly.Msg.ai_audio_play_music_file_set_files_list);
  6938. this.appendValueInput("firstIndex")
  6939. .setCheck(null)
  6940. .appendField(Blockly.Msg.ai_audio_play_music_file_set_first_index);
  6941. this.setPreviousStatement(true, null);
  6942. this.setNextStatement(true, null);
  6943. this.setColour("#5f7fc1");
  6944. this.setTooltip(Blockly.Msg.ai_audio_play_music_file_list_random_TOOLTIP);
  6945. this.setHelpUrl("");
  6946. }
  6947. };
  6948. Blockly.Python['ai_audio_play_song_list_random_speaker'] = function (block) {
  6949. // var number_play_vol = block.getFieldValue('play_vol');
  6950. var number_play_vol = Blockly.Python.valueToCode(block, 'volume', Blockly.Python.ORDER_ATOMIC);
  6951. // var value_name = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  6952. var my_list = Blockly.Python.valueToCode(block, 'my_list', Blockly.Python.ORDER_ATOMIC);
  6953. var firstIndex = Blockly.Python.valueToCode(block, 'firstIndex', Blockly.Python.ORDER_ATOMIC);
  6954. Blockly.Python.definitions_['v831_import_time'] = `import time`
  6955. Blockly.Python.definitions_['v831_import_random'] = `import random`
  6956. var _in = "" +
  6957. "random.seed(time.ticks_cpu())\n" +
  6958. "\n" +
  6959. "_audio_rx = I2S(I2S.DEVICE_1)\n" +
  6960. "firstIndex = " + firstIndex + "\n" +
  6961. "\n" +
  6962. "# 随机一首\n" +
  6963. "def random_song(index,my_list):\n" +
  6964. " index = random.randrange(0, len(my_list), 1)\n" +
  6965. " return index\n" +
  6966. "\n" +
  6967. "# 切换歌曲\n" +
  6968. "def change_random_song(index):\n" +
  6969. " global _audio_play_state,firstIndex,_player\n" +
  6970. " _player.finish()\n" +
  6971. " _audio_play_state = 0\n" +
  6972. " firstIndex = index\n" +
  6973. "\n" +
  6974. "def _speaker_play_song_list_random(volume, my_list):\n" +
  6975. " global _audio_play_state,_player,firstIndex,play_again\n" +
  6976. " _player = audio.Audio(path = my_list[firstIndex])\n" +
  6977. " _player.volume(volume)\n" +
  6978. " try:\n" +
  6979. // " firstIndex = index\n" +
  6980. " _wav_info = _player.play_process(_audio_rx)\n" +
  6981. " _audio_rx.channel_config(_audio_rx.CHANNEL_1, I2S.TRANSMITTER, resolution = I2S.RESOLUTION_16_BIT, align_mode = I2S.STANDARD_MODE)\n" +
  6982. " _audio_rx.set_sample_rate(_wav_info[1])\n" +
  6983. " _audio_play_state = 0\n" +
  6984. "\n" +
  6985. " fm.register(34,fm.fpioa.I2S1_OUT_D1)\n" +
  6986. " fm.register(35,fm.fpioa.I2S1_SCLK)\n" +
  6987. " fm.register(33,fm.fpioa.I2S1_WS)\n" +
  6988. "\n" +
  6989. " button_a_state = False\n" +
  6990. " while True:\n" +
  6991. " _audio_play_state = _player.play()\n" +
  6992. " if (_audio_play_state) == True:\n" +
  6993. " #print(\"Playing.\")\n" +
  6994. " if (_gp_side_a.value() == 1) and button_a_state == False:\n" +
  6995. " index = random_song(firstIndex,my_list)\n" +
  6996. " button_a_state = True\n" +
  6997. " elif (_gp_side_a.value() == 0) and button_a_state == True:\n" +
  6998. " button_a_state = False\n" +
  6999. " change_random_song(index)\n" +
  7000. " break\n" +
  7001. "\n" +
  7002. " elif (_audio_play_state) == False:\n" +
  7003. " #print(\"Done.\")\n" +
  7004. " index = random_song(firstIndex,my_list)\n" +
  7005. " firstIndex = index\n" +
  7006. " break\n" +
  7007. "\n" +
  7008. " except:\n" +
  7009. " play_again = True\n" +
  7010. " return\n" +
  7011. "\n";
  7012. // Blockly.Python.definitions_.import_audio = _in;
  7013. Blockly.Python.addVariable('_audio_play_list_random_state', _in, true);
  7014. Blockly.Python.definitions_['import_define_x_ai_button'] = '' +
  7015. 'from fpioa_manager import *\n' +
  7016. 'from Maix import FPIOA, GPIO\n' +
  7017. '\n' +
  7018. '_gp_side_buttons = [9, 10, 11]\n' +
  7019. '\n' +
  7020. 'FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)\n' +
  7021. 'FPIOA().set_function(_gp_side_buttons[1],FPIOA.GPIO1)\n' +
  7022. 'FPIOA().set_function(_gp_side_buttons[2],FPIOA.GPIO2)\n' +
  7023. '\n' +
  7024. '_gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)\n' +
  7025. '_gp_side_b = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)\n' +
  7026. '_gp_side_c = GPIO(GPIO.GPIO2,GPIO.IN,GPIO.PULL_UP)\n' +
  7027. '\n';
  7028. // Blockly.Python.addSetup("ai_audio_read", _in);
  7029. var _code = "_speaker_play_song_list_random(volume=" + number_play_vol + ", my_list=" + my_list + ")\n";
  7030. return _code;
  7031. };
  7032. /*
  7033. _ ___ __ ___ _
  7034. / \ |_ _| \ \ / (_) __| | ___ ___
  7035. / _ \ | | \ \ / /| |/ _` |/ _ \/ _ \
  7036. / ___ \ | | \ V / | | (_| | __/ (_) |
  7037. /_/ \_\___| \_/ |_|\__,_|\___|\___/
  7038. */
  7039. Blockly.Blocks['ai_video_record_setup'] = {
  7040. init: function () {
  7041. this.appendDummyInput()
  7042. .appendField(new Blockly.FieldImage("blockly/media/video_record_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  7043. this.appendDummyInput()
  7044. .appendField(Blockly.Msg.ai_video_start_recording);
  7045. this.appendValueInput("save_path")
  7046. .setCheck(null)
  7047. .appendField(Blockly.Msg.ai_video_start_recording_file_path);
  7048. this.appendValueInput("w")
  7049. .setCheck(null)
  7050. .appendField(Blockly.Msg.ai_video_start_recording_width);
  7051. this.appendValueInput("h")
  7052. .setCheck(null)
  7053. .appendField(Blockly.Msg.ai_video_start_recording_height);
  7054. this.setPreviousStatement(true, null);
  7055. this.setNextStatement(true, null);
  7056. this.setColour("#60c1bb");
  7057. this.setTooltip(Blockly.Msg.ai_video_record_setup_TOOLTIP);
  7058. this.setHelpUrl("");
  7059. }
  7060. };
  7061. Blockly.Python['ai_video_record_setup'] = function (block) {
  7062. var value_save_path = Blockly.Python.valueToCode(block, 'save_path', Blockly.Python.ORDER_ATOMIC);
  7063. var value_w = Blockly.Python.valueToCode(block, 'w', Blockly.Python.ORDER_ATOMIC);
  7064. var value_h = Blockly.Python.valueToCode(block, 'h', Blockly.Python.ORDER_ATOMIC);
  7065. Blockly.Python.definitions_.import_video = "import video";
  7066. Blockly.Python.definitions_.import_Maix = "from Maix import GPIO";
  7067. Blockly.Python.addVariable('record_vid', 'record_vid = None', true);
  7068. // TODO: Assemble Python into code variable.
  7069. var code = "_record_vid = video.open(" + value_save_path + ", record=1, width=" + value_w + ", height=" + value_h + ", interval=20000, quality=80)\n";
  7070. return code;
  7071. };
  7072. Blockly.Blocks['ai_video_get'] = {
  7073. init: function () {
  7074. this.appendDummyInput()
  7075. .appendField(Blockly.Msg.ai_video_to_canvas)
  7076. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  7077. .appendField(Blockly.Msg.ai_video_record);
  7078. this.setInputsInline(false);
  7079. this.setPreviousStatement(true, null);
  7080. this.setNextStatement(true, null);
  7081. this.setColour("#60c1bb");
  7082. this.setTooltip(Blockly.Msg.ai_video_get_TOOLTIP);
  7083. this.setHelpUrl("");
  7084. }
  7085. };
  7086. Blockly.Python.ai_video_get = function (block) {
  7087. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  7088. var _code = "_record_vid.record(" + variable_name + ")\n"
  7089. return _code;
  7090. }
  7091. Blockly.Blocks['ai_video_stop'] = {
  7092. init: function () {
  7093. this.appendDummyInput()
  7094. .appendField(Blockly.Msg.ai_video_stop_recording);
  7095. this.setPreviousStatement(true);
  7096. this.setNextStatement(true);
  7097. this.setColour("#60c1bb");
  7098. this.setTooltip(Blockly.Msg.ai_video_stop_TOOLTIP);
  7099. this.setHelpUrl("");
  7100. }
  7101. };
  7102. Blockly.Python.ai_video_stop = function (block) {
  7103. var _code = "_record_vid.record_finish()\n"
  7104. return _code;
  7105. }
  7106. Blockly.Blocks['ai_video_set_init'] = {
  7107. init: function () {
  7108. this.appendDummyInput()
  7109. .appendField(new Blockly.FieldImage("blockly/media/video_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  7110. this.appendDummyInput()
  7111. .appendField(Blockly.Msg.ai_video_set_play_init);
  7112. this.setPreviousStatement(true, null);
  7113. this.setNextStatement(true, null);
  7114. this.setColour("#4d9994");
  7115. this.setTooltip(Blockly.Msg.ai_video_set_play_init);
  7116. this.setHelpUrl("");
  7117. }
  7118. }
  7119. Blockly.Python['ai_video_set_init'] = function (block) {
  7120. Blockly.Python.definitions_['v831_import_os'] = `import os`
  7121. Blockly.Python.definitions_['v831_import_pyaudio'] = `import pyaudio`
  7122. Blockly.Python.definitions_['v831_import_av'] = `import av`
  7123. Blockly.Python.definitions_['v831_import_from_maix_display'] = `from maix import display`
  7124. Blockly.Python.definitions_['v831_import_from_maix_camera'] = `from maix import camera`
  7125. Blockly.Python.definitions_['v831_import_from_maix_image'] = `from maix import image`
  7126. // Blockly.Python.addVariable("VIDEOPLAY",`VIDEOPLAY = False`,true)
  7127. let code = ``
  7128. return code;
  7129. }
  7130. Blockly.Blocks['ai_video_set'] = {
  7131. init: function () {
  7132. // this.appendDummyInput()
  7133. // .appendField(Blockly.Msg.ai_video_play_video);
  7134. this.appendValueInput("path")
  7135. .setCheck(null)
  7136. .appendField(Blockly.Msg.ai_video_play_video_file_path);
  7137. // this.appendDummyInput()
  7138. // .appendField(Blockly.Msg.ai_video_play_video_2);
  7139. this.setPreviousStatement(true, null);
  7140. this.setNextStatement(true, null);
  7141. this.setColour("#4d9994");
  7142. this.setTooltip(Blockly.Msg.ai_video_play_video);
  7143. this.setHelpUrl("https://cn.maixpy.sipeed.com/maixpy/zh/api_reference/media/video.html#1-avi");
  7144. }
  7145. };
  7146. Blockly.Python['ai_video_set'] = function (block) {
  7147. // Blockly.Python.definitions_.import_Maix = "from maix import display, camera, image";
  7148. var value_path = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  7149. var code = `pathToVideo = ${value_path}
  7150. containerVideo = av.open(pathToVideo)
  7151. streamVideo = containerVideo.streams.video[0]
  7152. `
  7153. return code;
  7154. };
  7155. Blockly.Blocks['ai_video_set_stats'] = {
  7156. init: function () {
  7157. this.appendDummyInput()
  7158. .appendField(Blockly.Msg.ai_video_set_play_stats);
  7159. // this.appendDummyInput()
  7160. // .appendField(Blockly.Msg.ai_video_play_video_2);
  7161. this.setOutput(true, null)
  7162. this.setColour("#4d9994");
  7163. this.setTooltip(Blockly.Msg.ai_video_set_play_stats);
  7164. this.setHelpUrl("https://cn.maixpy.sipeed.com/maixpy/zh/api_reference/media/video.html#1-avi");
  7165. }
  7166. };
  7167. Blockly.Python['ai_video_set_stats'] = function (block) {
  7168. var code = `containerVideo.decode(video=0)`
  7169. return [code, Blockly.Python.ORDER_NONE];
  7170. };
  7171. Blockly.Blocks['ai_video_set_play'] = {
  7172. init: function () {
  7173. this.appendDummyInput().appendField(Blockly.Msg.ai_models_from)
  7174. .appendField(new Blockly.FieldVariable("i"), "varitem")
  7175. .appendField(Blockly.Msg.ai_video_set_play);
  7176. this.setPreviousStatement(true, null);
  7177. this.setNextStatement(true, null);
  7178. this.setColour("#4d9994");
  7179. this.setTooltip(Blockly.Msg.ai_video_set_play);
  7180. this.setHelpUrl("");
  7181. }
  7182. }
  7183. Blockly.Python['ai_video_set_play'] = function (block) {
  7184. var value = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  7185. let code = `if "Video" in repr(${value}):
  7186. VideoImg = image.load(bytes(${value}.to_rgb().planes[0]), (streamVideo.width, streamVideo.height))
  7187. display.show(VideoImg)
  7188. `
  7189. return code;
  7190. }
  7191. Blockly.Blocks['ai_video_stop_play'] = {
  7192. init: function () {
  7193. this.appendDummyInput()
  7194. .appendField(Blockly.Msg.ai_video_stop_video_playing);
  7195. this.setPreviousStatement(true, null);
  7196. this.setNextStatement(true, null);
  7197. this.setColour("#4d9994");
  7198. this.setTooltip(Blockly.Msg.ai_video_stop_play_TOOLTIP);
  7199. this.setHelpUrl("");
  7200. }
  7201. };
  7202. Blockly.Python['ai_video_stop_play'] = function (block) {
  7203. // TODO: Assemble Python into code variable.
  7204. var code = `break
  7205. `;
  7206. return code;
  7207. };
  7208. Blockly.Blocks['ai_video_record_init'] = {
  7209. init: function () {
  7210. this.appendDummyInput()
  7211. .appendField(new Blockly.FieldImage("blockly/media/video_record_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  7212. this.appendDummyInput()
  7213. .appendField(Blockly.Msg.ai_video_start_recording);
  7214. this.appendValueInput("save_path")
  7215. .setCheck(null)
  7216. .appendField(Blockly.Msg.ai_video_start_recording_file_path);
  7217. this.appendValueInput("w")
  7218. .setCheck(null)
  7219. .appendField(Blockly.Msg.ai_video_start_recording_width);
  7220. this.appendValueInput("h")
  7221. .setCheck(null)
  7222. .appendField(Blockly.Msg.ai_video_start_recording_height);
  7223. this.setPreviousStatement(true, null);
  7224. this.setNextStatement(true, null);
  7225. this.setColour("#4d9994");
  7226. this.setTooltip(Blockly.Msg.ai_video_record_setup_TOOLTIP);
  7227. // this.setHelpUrl("https://cn.maixpy.sipeed.com/maixpy/zh/api_reference/media/video.html#1-avi");
  7228. }
  7229. };
  7230. Blockly.Python['ai_video_record_init'] = function (block) {
  7231. Blockly.Python.definitions_['v831_import_os'] = `import os`
  7232. Blockly.Python.definitions_['v831_import_pyaudio'] = `import pyaudio`
  7233. Blockly.Python.definitions_['v831_import_av'] = `import av`
  7234. Blockly.Python.definitions_['v831_import_from_maix_display'] = `from maix import display`
  7235. Blockly.Python.definitions_['v831_import_from_maix_camera'] = `from maix import camera`
  7236. Blockly.Python.definitions_['v831_import_from_maix_image'] = `from maix import image`
  7237. var value_path = Blockly.Python.valueToCode(block, 'save_path', Blockly.Python.ORDER_ATOMIC);
  7238. var w = Blockly.Python.valueToCode(block, 'w', Blockly.Python.ORDER_ATOMIC);
  7239. var h = Blockly.Python.valueToCode(block, 'h', Blockly.Python.ORDER_ATOMIC);
  7240. var code = `path_to_video = ${value_path}
  7241. fps = 10
  7242. container = av.open(path_to_video, mode='w')
  7243. stream = container.add_stream('h264', rate=fps) # h264 or mpeg4
  7244. stream.width = ${w}
  7245. stream.height = ${h}
  7246. stream.pix_fmt = 'yuv420p'
  7247. `
  7248. return code;
  7249. };
  7250. Blockly.Blocks['ai_video_record_get'] = {
  7251. init: function () {
  7252. this.appendDummyInput()
  7253. .appendField(Blockly.Msg.ai_video_to_canvas)
  7254. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  7255. .appendField(Blockly.Msg.ai_video_record);
  7256. this.setInputsInline(false);
  7257. this.setPreviousStatement(true, null);
  7258. this.setNextStatement(true, null);
  7259. this.setColour("#4d9994");
  7260. this.setTooltip(Blockly.Msg.ai_video_get_TOOLTIP);
  7261. this.setHelpUrl("");
  7262. }
  7263. };
  7264. Blockly.Python.ai_video_record_get = function (block) {
  7265. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  7266. var _code = `frame = av.VideoFrame(${variable_name}.width, ${variable_name}.height, 'rgb24')
  7267. frame.planes[0].update(${variable_name}.tobytes())
  7268. for packet in stream.encode(frame):
  7269. container.mux(packet)
  7270. `
  7271. return _code;
  7272. }
  7273. Blockly.Blocks['ai_video_record_stop'] = {
  7274. init: function () {
  7275. this.appendDummyInput()
  7276. .appendField(Blockly.Msg.ai_video_stop_recording);
  7277. this.setPreviousStatement(true);
  7278. this.setNextStatement(true);
  7279. this.setColour("#4d9994");
  7280. this.setTooltip(Blockly.Msg.ai_video_stop_TOOLTIP);
  7281. this.setHelpUrl("");
  7282. }
  7283. };
  7284. Blockly.Python.ai_video_record_stop = function (block) {
  7285. var _code = `
  7286. #Flush stream
  7287. for packet in stream.encode():
  7288. container.mux(packet)
  7289. #Close the file
  7290. container.close()
  7291. `
  7292. return _code;
  7293. }
  7294. // 视频录制
  7295. Blockly.Blocks['ai_video_record'] = {
  7296. init: function () {
  7297. this.appendDummyInput()
  7298. .appendField(new Blockly.FieldImage("blockly/media/video_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  7299. this.appendDummyInput()
  7300. .appendField(Blockly.Msg.ai_video_recording_and_save);
  7301. this.appendValueInput("path")
  7302. .setCheck(null)
  7303. .appendField(Blockly.Msg.VIDEO_SET_P);
  7304. this.appendDummyInput()
  7305. .appendField(Blockly.Msg.ai_video_play_video_2);
  7306. this.setPreviousStatement(true, null);
  7307. this.setNextStatement(true, null);
  7308. this.setColour("#4d9994");
  7309. this.setTooltip(Blockly.Msg.ai_video_set_TOOLTIP);
  7310. this.setHelpUrl("https://cn.maixpy.sipeed.com/maixpy/zh/api_reference/media/video.html#1-avi");
  7311. }
  7312. };
  7313. Blockly.Python['ai_video_record'] = function (block) {
  7314. var value_path = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  7315. Blockly.Python.definitions_['v831_import_time'] = `import time`
  7316. Blockly.Python.definitions_['v831_import_pyaudio'] = `import pyaudio`
  7317. Blockly.Python.definitions_['v831_import_av'] = `import av`
  7318. Blockly.Python.definitions_['v831_import_os'] = `import os`
  7319. Blockly.Python.definitions_['v831_import_from_maix_display'] = `import display`
  7320. Blockly.Python.definitions_['v831_import_from_maix_camera'] = `import camera`
  7321. Blockly.Python.definitions_['v831_import_from_maix_image'] = `import image`
  7322. var code = `path_to_video = ${value_path};
  7323. duration, fps = 4, 10
  7324. total_frames = duration * fps
  7325. container = av.open(path_to_video, mode='w')
  7326. stream = container.add_stream('h264', rate=fps) # h264 or mpeg4
  7327. stream.width = 320
  7328. stream.height = 240
  7329. stream.pix_fmt = 'yuv420p'
  7330. for frame_i in range(total_frames):
  7331. img = image.new(size=(stream.width, stream.height), mode='RGB')
  7332. img.draw_string(0, 0, str(time.time()), 2)
  7333. frame = av.VideoFrame(img.width, img.height, 'rgb24')
  7334. frame.planes[0].update(img.tobytes())
  7335. for packet in stream.encode(frame):
  7336. container.mux(packet)
  7337. #Flush stream
  7338. for packet in stream.encode():
  7339. container.mux(packet)
  7340. #Close the file
  7341. container.close()
  7342. #Play the video
  7343. container = av.open(path_to_video)
  7344. stream = container.streams.video[0]
  7345. for frame in container.decode(video=0):
  7346. if 'Video' in repr(frame):
  7347. img = image.load(bytes(frame.to_rgb().planes[0]), (stream.width, stream.height))
  7348. display.show(img)`
  7349. return code;
  7350. };
  7351. Blockly.Blocks['ai_video_status'] = {
  7352. init: function () {
  7353. this.appendDummyInput()
  7354. .appendField(Blockly.Msg.ai_video_get_video_playing_status);
  7355. this.setOutput(true, null);
  7356. this.setColour("#4d9994");
  7357. this.setTooltip(Blockly.Msg.ai_video_status_TOOLTIP);
  7358. this.setHelpUrl("");
  7359. }
  7360. };
  7361. Blockly.Python.ai_video_status = function (block) {
  7362. var code = "_video_play.play()";
  7363. return [code, Blockly.Python.ORDER_NONE];
  7364. }
  7365. /*
  7366. _ ___ __ __ _ _
  7367. / \ |_ _| | \/ | ___ __| | ___| |___
  7368. / _ \ | | | |\/| |/ _ \ / _` |/ _ \ / __|
  7369. / ___ \ | | | | | | (_) | (_| | __/ \__ \
  7370. /_/ \_\___| |_| |_|\___/ \__,_|\___|_|___/
  7371. */
  7372. Blockly.Blocks['ai_model_allinit'] = {
  7373. init: function () {
  7374. // let types = getLocalStorage("isQiehuan");
  7375. // if (!types) {
  7376. // saveLocalStorage("isQiehuan", 0);
  7377. // }
  7378. this.appendDummyInput()
  7379. .appendField(Blockly.Msg.ai_models_init_title)
  7380. .appendField(new Blockly.FieldDropdown([
  7381. [Blockly.Msg.ai_models_init_hand_written_model, "Single Digit Recognition"],
  7382. [Blockly.Msg.ai_models_init_object_model, "Common Object Recognition"],
  7383. [Blockly.Msg.ai_models_init_face_model, "Human Face Recognition"]
  7384. ]), "DEGREE");
  7385. this.appendDummyInput().appendField(Blockly.Msg.ai_model_allinit_language).appendField(new Blockly.FieldDropdown([
  7386. [Blockly.Msg.ai_speech_recognition_setup_set_english, "English"],
  7387. [Blockly.Msg.ai_speech_recognition_setup_set_simplified_chinese, "Simplified Chinese"],
  7388. [Blockly.Msg.ai_speech_recognition_setup_set_traditional_chinese, "Traditional Chinese"]
  7389. ]), "interface")
  7390. // this.appendDummyInput()
  7391. // .appendField(Blockly.Msg.ai_camera_camera_fit_set1)
  7392. // .appendField(new Blockly.FieldVariable("img_mnist"), "camera_varitem");
  7393. // this.appendDummyInput()
  7394. // .appendField(Blockly.Msg.ai_models_init_source)
  7395. // .appendField(new Blockly.FieldVariable("img_mnist"), "varitem");
  7396. this.setPreviousStatement(true, null);
  7397. this.setNextStatement(true, null);
  7398. this.setColour("#ee783a");
  7399. var thisBlock = this;
  7400. this.setTooltip(function () {
  7401. var mode = thisBlock.getFieldValue('DEGREE');
  7402. var TOOLTIPS = {
  7403. 'Single Digit Recognition': Blockly.Msg.ai_model_allinit_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_hand_written_model),
  7404. 'Common Object Recognition': Blockly.Msg.ai_model_allinit_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_object_model),
  7405. 'Human Face Recognition': Blockly.Msg.ai_model_allinit_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_face_model)
  7406. };
  7407. return TOOLTIPS[mode];
  7408. });
  7409. this.setHelpUrl('');
  7410. }
  7411. };
  7412. Blockly.Python.ai_model_allinit = function (block) {
  7413. Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
  7414. var degee = block.getFieldValue('DEGREE');
  7415. var _code = "";
  7416. if (degee == "Single Digit Recognition") {
  7417. Blockly.Python.addVariable('task_mnist', "task_mnist = kpu.load(\"/sd/preset/models/mnist.kmodel\")\n", true);
  7418. } else if (degee == "Common Object Recognition") {
  7419. var language = localStorage.getItem("handPyLanguage");
  7420. Blockly.Python.addVariable('task_objectrecognition', 'task_objectrecognition = None', true);
  7421. if (language == "en") {
  7422. _code = '' +
  7423. 'classes_objectrecognition = [\'aeroplane\', \'bicycle\', \'bird\', \'boat\', \'bottle\', \'bus\', \'car\', \'cat\', \'chair\', \'cow\', \'diningtable\', \'dog\', \'horse\', \'motorbike\', \'person\', \'pottedplant\', \'sheep\', \'sofa\', \'train\', \'tvmonitor\']\n' +
  7424. 'task_objectrecognition = kpu.load("/sd/preset/models/tinyyolo_v2_20class.kmodel")\n' +
  7425. 'object_anchor = (1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52)\n' +
  7426. 'a = kpu.init_yolo2(task_objectrecognition, 0.5, 0.3, 5, object_anchor)\n' +
  7427. '';
  7428. }
  7429. else if (language == "zh-hans") {
  7430. _code = '' +
  7431. 'classes_objectrecognition = [\'飞机\', \'自行车\', \'鸟\', \'船\', \'瓶子\', \'公共汽车\', \'汽车\', \'猫\', \'椅子\', \'牛\', \'餐桌\', \'狗\', \'马\', \'摩托车\', \'人\', \'盆栽\', \'羊\', \'沙发\', \'火车\', \'电视监视器\']\n' +
  7432. 'task_objectrecognition = kpu.load("/sd/preset/models/tinyyolo_v2_20class.kmodel")\n' +
  7433. 'object_anchor = (1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52)\n' +
  7434. 'a = kpu.init_yolo2(task_objectrecognition, 0.5, 0.3, 5, object_anchor)\n' +
  7435. '';
  7436. }
  7437. else {
  7438. _code = '' +
  7439. 'classes_objectrecognition = [\'飛機\', \'自行車\', \'鳥\', \'船\', \'瓶子\', \'公共汽車\', \'汽車\', \'貓\', \'椅子\', \'牛\', \'餐桌\', \'狗\', \'馬\', \'摩托車\', \'人\', \'盆栽\', \'羊\', \'沙發\', \'火車\', \'電視監視器\']\n' +
  7440. 'task_objectrecognition = kpu.load("/sd/preset/models/tinyyolo_v2_20class.kmodel")\n' +
  7441. 'object_anchor = (1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52)\n' +
  7442. 'a = kpu.init_yolo2(task_objectrecognition, 0.5, 0.3, 5, object_anchor)\n' +
  7443. '';
  7444. }
  7445. } else {
  7446. Blockly.Python.addVariable('task_facerecognition', 'task_facerecognition = None', true);
  7447. _code = "" +
  7448. "task_facerecognition = kpu.load(\"/sd/preset/models/preset/face-recognition.kmodel\")\n" +
  7449. "anchor_face = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025)\n" +
  7450. "a = kpu.init_yolo2(task_facerecognition, 0.5, 0.3, 5, anchor_face)\n" +
  7451. "\n";
  7452. }
  7453. return _code;
  7454. }
  7455. // 边缘检测
  7456. Blockly.Blocks['ai_model_edge_init'] = {
  7457. init: function () {
  7458. // let types = getLocalStorage("isQiehuan");
  7459. // if (!types) {
  7460. // saveLocalStorage("isQiehuan", 0);
  7461. // }
  7462. this.appendDummyInput()
  7463. .appendField(Blockly.Msg.ai_models_init_title)
  7464. .appendField(Blockly.Msg.ai_models_edge_detection_model);
  7465. this.setPreviousStatement(true, null);
  7466. this.setNextStatement(true, null);
  7467. this.setColour("#ee783a");
  7468. this.setTooltip(Blockly.Msg.ai_models_init_title + Blockly.Msg.ai_models_edge_detection_model);
  7469. this.setHelpUrl('');
  7470. }
  7471. };
  7472. Blockly.Python.ai_model_edge_init = function (block) {
  7473. Blockly.Python.definitions_['v831_import_numpy_as_np'] = `import numpy as np`
  7474. Blockly.Python.definitions_.import_KPU = `
  7475. class Edge:
  7476. model = {
  7477. "param": "/root/preset/model/sobel_int8.param",
  7478. "bin": "/root/preset/model/sobel_int8.bin"
  7479. }
  7480. input_size = (224, 224, 3)
  7481. output_size = (222, 222, 3)
  7482. options = {
  7483. "model_type": "awnn",
  7484. "inputs": {
  7485. "input0": input_size
  7486. },
  7487. "outputs": {
  7488. "output0": output_size
  7489. },
  7490. "mean": [127.5, 127.5, 127.5],
  7491. "norm": [0.0078125, 0.0078125, 0.0078125],
  7492. }
  7493. def __init__(self):
  7494. from maix import nn
  7495. print("-- load model:", self.model)
  7496. self.model = nn.load(self.model, opt=self.options)
  7497. print("-- load ok")
  7498. def __del__(self):
  7499. del self.model
  7500. m = Edge()`;
  7501. // var degee = block.getFieldValue('DEGREE');
  7502. var _code = "";
  7503. return _code;
  7504. }
  7505. // 加载边缘检测模型
  7506. Blockly.Blocks['ai_model_edge_loading'] = {
  7507. init: function () {
  7508. // let types = getLocalStorage("isQiehuan");
  7509. // if (!types) {
  7510. // saveLocalStorage("isQiehuan", 0);
  7511. // }
  7512. this.appendDummyInput()
  7513. .appendField(Blockly.Msg.ai_models_load_edge_detection_model);
  7514. // this.appendDummyInput()
  7515. // .appendField(Blockly.Msg.ai_models_init_source)
  7516. // .appendField(new Blockly.FieldVariable("img_edgedetection"), "varitem");
  7517. this.setPreviousStatement(true, null);
  7518. this.setNextStatement(true, null);
  7519. this.setColour("#ee783a");
  7520. // var thisBlock = this;
  7521. this.setTooltip(Blockly.Msg.ai_models_load_edge_detection_model);
  7522. this.setHelpUrl('');
  7523. }
  7524. };
  7525. Blockly.Python.ai_model_edge_loading = function (block) {
  7526. // var degee = block.getFieldValue('varitem');
  7527. Blockly.Python.definitions_['v831_import_numpy_as_np'] = `import numpy as np`
  7528. Blockly.Python.definitions_.import_KPU = `
  7529. class Edge:
  7530. model = {
  7531. "param": "/root/preset/model/sobel_int8.param",
  7532. "bin": "/root/preset/model/sobel_int8.bin"
  7533. }
  7534. input_size = (224, 224, 3)
  7535. output_size = (222, 222, 3)
  7536. options = {
  7537. "model_type": "awnn",
  7538. "inputs": {
  7539. "input0": input_size
  7540. },
  7541. "outputs": {
  7542. "output0": output_size
  7543. },
  7544. "mean": [127.5, 127.5, 127.5],
  7545. "norm": [0.0078125, 0.0078125, 0.0078125],
  7546. }
  7547. def __init__(self):
  7548. from maix import nn
  7549. print("-- load model:", self.model)
  7550. self.model = nn.load(self.model, opt=self.options)
  7551. print("-- load ok")
  7552. def __del__(self):
  7553. del self.model
  7554. m = Edge()`;
  7555. // ${degee} = camera.capture().resize(224,224)
  7556. var _code = `_canvas_x, _canvas_y = 48,8
  7557. canvas = canvas.resize(224, 224, padding = 0)
  7558. out = m.model.forward(canvas, quantize=True, layout="hwc")
  7559. out = out.astype(np.float32).reshape(m.output_size)
  7560. out = (np.ndarray.__abs__(out) * 255 / out.max()).astype(np.uint8)
  7561. data = out.tobytes()
  7562. canvas = canvas.load(data,(222, 222), mode="RGB")
  7563. `;
  7564. return _code;
  7565. }
  7566. // 获取边缘测试模型结果
  7567. Blockly.Blocks['ai_model_edge_result'] = {
  7568. init: function () {
  7569. this.appendDummyInput()
  7570. .appendField(Blockly.Msg.ai_models_get_edge_detection_result);
  7571. this.setOutput(true, null);
  7572. // this.setNextStatement(true, null);
  7573. this.setColour("#ee783a");
  7574. // var thisBlock = this;
  7575. this.setTooltip(Blockly.Msg.ai_models_get_edge_detection_result);
  7576. this.setHelpUrl('');
  7577. }
  7578. };
  7579. Blockly.Python.ai_model_edge_result = function (block) {
  7580. var degee = block.getFieldValue('varitem');
  7581. var _code = "edgeModel";
  7582. return [_code, Blockly.Python.ORDER_ATOMIC];
  7583. }
  7584. Blockly.Blocks['ai_model_allload'] = {
  7585. init: function () {
  7586. this.appendDummyInput()
  7587. .appendField(Blockly.Msg.ai_models_load_title)
  7588. .appendField(new Blockly.FieldDropdown([
  7589. [Blockly.Msg.ai_models_init_hand_written_model, "Single Digit Recognition"],
  7590. [Blockly.Msg.ai_models_init_object_model, "Common Object Recognition"],
  7591. [Blockly.Msg.ai_models_init_face_model, "Human Face Recognition"]
  7592. ]), "DEGREE");
  7593. // this.appendDummyInput()
  7594. // .appendField(Blockly.Msg.ai_camera_camera_fit_set1)
  7595. // .appendField(new Blockly.FieldVariable("img_mnist"), "camera_varitem");
  7596. this.appendDummyInput()
  7597. .appendField(Blockly.Msg.ai_models_init_source)
  7598. .appendField(new Blockly.FieldVariable("img_mnist"), "varitem");
  7599. this.setPreviousStatement(true, null);
  7600. this.setNextStatement(true, null);
  7601. this.setColour("#ee783a");
  7602. var thisBlock = this;
  7603. this.setTooltip(function () {
  7604. var mode = thisBlock.getFieldValue('DEGREE');
  7605. var TOOLTIPS = {
  7606. 'Single Digit Recognition': Blockly.Msg.ai_model_allload_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_hand_written_model),
  7607. 'Common Object Recognition': Blockly.Msg.ai_model_allload_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_object_model),
  7608. 'Human Face Recognition': Blockly.Msg.ai_model_allload_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_face_model)
  7609. };
  7610. return TOOLTIPS[mode];
  7611. });
  7612. this.setHelpUrl('');
  7613. }
  7614. };
  7615. Blockly.Python.ai_model_allload = function (block) {
  7616. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  7617. // var camera_variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('camera_varitem'), Blockly.Variables.NAME_TYPE);
  7618. Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
  7619. var degee = block.getFieldValue('DEGREE');
  7620. var _code = ""
  7621. if (degee == "Single Digit Recognition") {
  7622. // Blockly.Python.addVariable('img_mnist_resized', '', true);
  7623. _code += "img_mnist1=" + variable_name + ".to_grayscale(1)\n";
  7624. _code += "img_mnist_resized=img_mnist1.resize(28,28)\n";
  7625. _code += "a=img_mnist_resized.invert()\n";
  7626. _code += "a=img_mnist_resized.strech_char(1)\n";
  7627. _code += "a=img_mnist_resized.pix_to_ai()\n";
  7628. _code += "fmap_mnist=kpu.forward(task_mnist, img_mnist_resized)\n";
  7629. _code += "plist_mnist=fmap_mnist[:]\n";
  7630. _code += "pmax_mnist=max(plist_mnist)\n";
  7631. _code += "max_index_mnist=plist_mnist.index(pmax_mnist)\n";
  7632. } else if (degee == "Common Object Recognition") {
  7633. // Blockly.Python.addVariable('code_objectrecognition', '', true);
  7634. _code += "code_objectrecognition = kpu.run_yolo2(task_objectrecognition, " + variable_name + ")\n";
  7635. _code += variable_name + ".ai_to_pix()\n";
  7636. } else {
  7637. // Blockly.Python.addVariable('code_facerecognition', '', true);
  7638. _code += "code_facerecognition = kpu.run_yolo2(task_facerecognition, " + variable_name + ")\n";
  7639. _code += variable_name + ".ai_to_pix()\n";
  7640. }
  7641. return _code;
  7642. }
  7643. Blockly.Blocks['ai_model_index_init'] = {
  7644. init: function () {
  7645. this.appendValueInput("class_input")
  7646. .setCheck(null)
  7647. .appendField(Blockly.Msg.ai_models_init_title)
  7648. // .appendField("DEGREE");
  7649. this.setPreviousStatement(true, null);
  7650. this.setNextStatement(true, null);
  7651. this.setColour("#ee783a");
  7652. this.setTooltip(Blockly.Msg.ai_models_init_title);
  7653. this.setHelpUrl('');
  7654. }
  7655. };
  7656. Blockly.Python.ai_model_index_init = function (block) {
  7657. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  7658. Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
  7659. console.log('value_class_input', value_class_input)
  7660. var _code = value_class_input;
  7661. // Blockly.Python.addVariable('task_mnist', "task_mnist = kpu.load(\"/sd/preset/models/mnist.kmodel\")\n", true);
  7662. return _code;
  7663. }
  7664. // 手写数字识别
  7665. Blockly.Blocks['ai_model_look_written_model'] = {
  7666. init: function () {
  7667. // this.appendDummyInput()
  7668. // .appendField(Blockly.Msg.ai_models_init_title)
  7669. // .appendField(new Blockly.FieldDropdown([
  7670. // [Blockly.Msg.ai_models_init_hand_written_model, "Human Face Recognition"]
  7671. // ]), "DEGREE");
  7672. this.appendDummyInput()
  7673. .appendField(Blockly.Msg.ai_models_init_hand_written_model);
  7674. this.setOutput(true, null);
  7675. this.setColour("#ee783a");
  7676. this.setTooltip(Blockly.Msg.ai_models_init_title + Blockly.Msg.ai_models_init_hand_written_model);
  7677. this.setHelpUrl("");
  7678. }
  7679. };
  7680. Blockly.Python.ai_model_look_written_model = function (block) {
  7681. // Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
  7682. var _code = "task_mnist = kpu.load(\"/sd/preset/models/mnist.kmodel\")\n";
  7683. return [_code, Blockly.Python.ORDER_ATOMIC];
  7684. }
  7685. Blockly.Blocks['ai_model_look_written_model_old'] = {
  7686. init: function () {
  7687. this.appendDummyInput()
  7688. .appendField(Blockly.Msg.ai_models_init_title)
  7689. .appendField(new Blockly.FieldDropdown([
  7690. [Blockly.Msg.ai_models_init_hand_written_model, "Human Face Recognition"]
  7691. ]), "DEGREE");
  7692. // this.appendDummyInput()
  7693. // .appendField(Blockly.Msg.ai_models_init_hand_written_model);
  7694. this.setPreviousStatement(true, null);
  7695. this.setNextStatement(true, null);
  7696. this.setColour("#ee783a");
  7697. this.setTooltip(Blockly.Msg.ai_models_init_title + Blockly.Msg.ai_models_init_hand_written_model);
  7698. this.setHelpUrl("");
  7699. }
  7700. };
  7701. Blockly.Python.ai_model_look_written_model_old = function (block) {
  7702. Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
  7703. var _code = "task_mnist = kpu.load(\"/sd/preset/models/mnist.kmodel\")\n";
  7704. return _code;
  7705. }
  7706. Blockly.Blocks['ai_model_index_load'] = {
  7707. init: function () {
  7708. this.appendDummyInput()
  7709. .appendField(Blockly.Msg.ai_models_load_title)
  7710. .appendField(new Blockly.FieldDropdown([
  7711. [Blockly.Msg.ai_models_init_hand_written_model, "Single Digit Recognition"]
  7712. ]), "DEGREE");
  7713. this.appendDummyInput()
  7714. .appendField(Blockly.Msg.ai_models_init_source)
  7715. .appendField(new Blockly.FieldVariable("img_mnist"), "varitem");
  7716. this.setPreviousStatement(true, null);
  7717. this.setNextStatement(true, null);
  7718. this.setColour("#ee783a");
  7719. var thisBlock = this;
  7720. this.setTooltip(function () {
  7721. var mode = thisBlock.getFieldValue('DEGREE');
  7722. var TOOLTIPS = {
  7723. 'Single Digit Recognition': Blockly.Msg.ai_model_allload_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_hand_written_model)
  7724. };
  7725. return TOOLTIPS[mode];
  7726. });
  7727. this.setHelpUrl('');
  7728. }
  7729. };
  7730. Blockly.Python.ai_model_index_load = function (block) {
  7731. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  7732. // var camera_variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('camera_varitem'), Blockly.Variables.NAME_TYPE);
  7733. Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
  7734. var degee = block.getFieldValue('DEGREE');
  7735. var _code = ""
  7736. if (degee == "Single Digit Recognition") {
  7737. // Blockly.Python.addVariable('img_mnist_resized', '', true);
  7738. _code += "img_mnist1=" + variable_name + ".to_grayscale(1)\n";
  7739. _code += "img_mnist_resized=img_mnist1.resize(28,28)\n";
  7740. _code += "a=img_mnist_resized.invert()\n";
  7741. _code += "a=img_mnist_resized.strech_char(1)\n";
  7742. _code += "a=img_mnist_resized.pix_to_ai()\n";
  7743. _code += "fmap_mnist=kpu.forward(task_mnist, img_mnist_resized)\n";
  7744. _code += "plist_mnist=fmap_mnist[:]\n";
  7745. _code += "pmax_mnist=max(plist_mnist)\n";
  7746. _code += "max_index_mnist=plist_mnist.index(pmax_mnist)\n";
  7747. }
  7748. return _code;
  7749. }
  7750. Blockly.Blocks['ai_model_index_mnist'] = {
  7751. init: function () {
  7752. this.appendDummyInput()
  7753. .appendField(new Blockly.FieldImage("blockly/media/digit_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  7754. this.appendDummyInput()
  7755. .appendField(Blockly.Msg.ai_models_hand_written_model_result);
  7756. this.appendDummyInput()
  7757. .appendField(new Blockly.FieldDropdown([
  7758. [Blockly.Msg.ai_models_hand_written_model_number, "c"],
  7759. [Blockly.Msg.ai_models_hand_written_model_confidence, "confidence"]
  7760. ]), "value");
  7761. this.setOutput(true, null);
  7762. this.setColour("#ee783a");
  7763. this.setTooltip(Blockly.Msg.ai_model_index_mnist_TOOLTIP);
  7764. this.setHelpUrl('');
  7765. }
  7766. };
  7767. Blockly.Python.ai_model_index_mnist = function (block) {
  7768. var value = block.getFieldValue('value');
  7769. var _code = "max_index_mnist";
  7770. if (value == "confidence") {
  7771. _code = "int(pmax_mnist*100)";
  7772. }
  7773. return [_code, Blockly.Python.ORDER_ATOMIC];
  7774. }
  7775. Blockly.Blocks['ai_model_digital_init'] = {
  7776. init: function () {
  7777. this.appendDummyInput()
  7778. .appendField(Blockly.Msg.ai_models_init_title)
  7779. .appendField(Blockly.Msg.ai_models_init_hand_written_model);
  7780. // this.appendDummyInput()
  7781. // .appendField(Blockly.Msg.ai_models_init_hand_written_model);
  7782. this.setPreviousStatement(true, null);
  7783. this.setNextStatement(true, null);
  7784. this.setColour("#ee783a");
  7785. this.setTooltip(Blockly.Msg.ai_models_init_title + Blockly.Msg.ai_models_init_hand_written_model);
  7786. this.setHelpUrl("");
  7787. }
  7788. };
  7789. Blockly.Python.ai_model_digital_init = function (block) {
  7790. // Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
  7791. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  7792. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  7793. var _code = `
  7794. class Number_recognition:
  7795. mdsc_path = "/root/preset/model/Number.mud"
  7796. labels = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
  7797. anchors = [1.0, 5.0, 1.35, 5.42, 0.49, 2.55, 0.86, 3.75, 0.65, 4.38]
  7798. def __init__(self):
  7799. self.model = nn.load(self.mdsc_path)
  7800. self.decoder = decoder.Yolo2(len(self.labels) , self.anchors , net_in_size = (224, 224) ,net_out_size = (7,7))
  7801. def __del__(self):
  7802. del self.model
  7803. del self.decoder
  7804. def cal_fps(self ,start , end):
  7805. one_second = 1
  7806. one_flash = end - start
  7807. fps = one_second / one_flash
  7808. return fps
  7809. number_recognition = Number_recognition()
  7810. `;
  7811. return _code;
  7812. }
  7813. Blockly.Blocks['ai_model_digital_init_loding'] = {
  7814. init: function () {
  7815. this.appendDummyInput()
  7816. .appendField(Blockly.Msg.ai_models_init_hand_written_model);
  7817. // this.appendDummyInput()
  7818. // .appendField(Blockly.Msg.ai_models_init_source)
  7819. // .appendField(new Blockly.FieldVariable("img_mnist"), "varitem");
  7820. this.setPreviousStatement(true, null);
  7821. this.setNextStatement(true, null);
  7822. this.setColour("#ee783a");
  7823. var thisBlock = this;
  7824. this.setTooltip(function () {
  7825. var mode = thisBlock.getFieldValue('DEGREE');
  7826. var TOOLTIPS = {
  7827. 'Single Digit Recognition': Blockly.Msg.ai_model_allload_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_hand_written_model)
  7828. };
  7829. return TOOLTIPS[mode];
  7830. });
  7831. this.setHelpUrl('');
  7832. }
  7833. };
  7834. Blockly.Python.ai_model_digital_init_loding = function (block) {
  7835. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  7836. // var camera_variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('camera_varitem'), Blockly.Variables.NAME_TYPE);
  7837. Blockly.Python.definitions_.import_KPU = ``;
  7838. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  7839. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  7840. Blockly.Python.definitions_['v831_import_from_Number_recognition'] = `
  7841. class Number_recognition:
  7842. mdsc_path = "/root/preset/model/Number.mud"
  7843. labels = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
  7844. anchors = [1.0, 5.0, 1.35, 5.42, 0.49, 2.55, 0.86, 3.75, 0.65, 4.38]
  7845. def __init__(self):
  7846. self.model = nn.load(self.mdsc_path)
  7847. self.decoder = decoder.Yolo2(len(self.labels) , self.anchors , net_in_size = (224, 224) ,net_out_size = (7,7))
  7848. self.NumberRecognition = []
  7849. def __del__(self):
  7850. del self.model
  7851. del self.decoder
  7852. def cal_fps(self ,start , end):
  7853. one_second = 1
  7854. one_flash = end - start
  7855. fps = one_second / one_flash
  7856. return fps
  7857. def digitalShowCanvas(self,canvas):
  7858. out = number_recognition.model.forward(canvas, quantize=1, layout = "hwc")
  7859. BOXES, PROBS = number_recognition.decoder.run(out, nms=0.5, threshold=0.3, img_size=(224,224))
  7860. if len(BOXES):
  7861. for BOXESI, BOXOBJ in enumerate(BOXES):
  7862. canvas.draw_string(0,0, (str(number_recognition.labels[PROBS[BOXESI][0]]) + str(str(":") + str(round(PROBS[BOXESI][1][PROBS[BOXESI][0]]*100, 2)))), scale = 1, color = (255,0,0) , thickness = 1)
  7863. canvas.draw_rectangle(BOXOBJ[0],BOXOBJ[1], BOXOBJ[0]+BOXOBJ[2],BOXOBJ[1]+ BOXOBJ[3], color=(255,0,0), thickness=1)
  7864. BOXES[BOXESI].append(PROBS[BOXESI])
  7865. #self.NumberRecognition = BOXES
  7866. else:
  7867. BOXES = []
  7868. return BOXES
  7869. number_recognition = Number_recognition()
  7870. `;
  7871. var _code = `_canvas_x, _canvas_y = 48,8
  7872. canvas = canvas.crop(48,8,224,224)
  7873. BOXES = number_recognition.digitalShowCanvas(canvas)
  7874. `
  7875. return _code;
  7876. }
  7877. Blockly.Blocks['ai_model_digital_new'] = {
  7878. init: function () {
  7879. this.appendDummyInput()
  7880. .appendField(Blockly.Msg.ai_model_digital_new_text)
  7881. this.appendStatementInput("input")
  7882. .setCheck(null)
  7883. .appendField(Blockly.Msg.basic_motion_statement_exec);
  7884. this.updateShape_();
  7885. this.setMutator(new Blockly.Mutator(['ai_model_false_create_with_item']));
  7886. this.setColour("#ee783a");
  7887. this.setPreviousStatement(true, null);
  7888. this.setNextStatement(true, null);
  7889. this.setTooltip("");
  7890. this.setHelpUrl("");
  7891. this.itemCount_ = 0;
  7892. },
  7893. mutationToDom: function () {
  7894. if (!this.itemCount_)
  7895. return null;
  7896. var container = document.createElement('mutation');
  7897. this.itemCount_ && container.setAttribute('aielse', 1);
  7898. return container;
  7899. },
  7900. domToMutation: function (a) {
  7901. this.itemCount_ = parseInt(a.getAttribute('aielse'), 10);
  7902. this.updateShape_();
  7903. },
  7904. decompose: function (a) {
  7905. var b = a.newBlock('ai_model_false_create_with_container');
  7906. b.initSvg();
  7907. var c = b.nextConnection
  7908. this.itemCount_ && (a = a.newBlock("ai_model_false_create_with_item"),
  7909. a.initSvg(),
  7910. c.connect(a.previousConnection));
  7911. console.log("this.elseCount_", a)
  7912. return b
  7913. },
  7914. compose: function (a) {
  7915. var b = a.nextConnection.targetBlock();
  7916. this.itemCount_ = 0;
  7917. var e = null;
  7918. if (b && b.type === "ai_model_false_create_with_item") {
  7919. this.itemCount_++;
  7920. e = b.statementConnection_;
  7921. b = b.nextConnection && b.nextConnection.targetBlock()
  7922. }
  7923. this.updateShape_()
  7924. Blockly.Mutator.reconnect(e, this, "AIELSE")
  7925. },
  7926. saveConnections: function (a) {
  7927. a = a.nextConnection.targetBlock();
  7928. if (a && a.type === "ai_model_false_create_with_item") {
  7929. e = this.getInput("AIELSE");
  7930. a.statementConnection_ = e && e.connection.targetConnection;
  7931. a = a.nextConnection && a.nextConnection.targetBlock()
  7932. }
  7933. },
  7934. updateShape_: function () {
  7935. this.getInput("AIELSE") && this.removeInput("AIELSE");
  7936. console.log("this.itemCount_", this.itemCount_)
  7937. this.itemCount_ && this.appendStatementInput("AIELSE").appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSE)
  7938. },
  7939. };
  7940. Blockly.Blocks['ai_model_false_create_with_container'] = {
  7941. init: function () {
  7942. this.setColour("#ee783a");
  7943. this.appendDummyInput()
  7944. .appendField(Blockly.Msg.CONTROLS_REPEAT_INPUT_DO);
  7945. this.setNextStatement(true)
  7946. this.setTooltip('');
  7947. this.contextMenu = false;
  7948. }
  7949. };
  7950. Blockly.Blocks['ai_model_false_create_with_item'] = {
  7951. init: function () {
  7952. this.setColour("#ee783a");
  7953. this.appendDummyInput().appendField(Blockly.Msg.CONTROLS_IF_ELSE_TITLE_ELSE);
  7954. this.setPreviousStatement(!0);
  7955. this.setTooltip(Blockly.Msg.CONTROLS_IF_ELSE_TOOLTIP);
  7956. this.contextMenu = !1
  7957. }
  7958. };
  7959. Blockly.Python['ai_model_digital_new'] = function (block) {
  7960. var statements_input = Blockly.Python.statementToCode(block, 'input');
  7961. var statements_input1 = Blockly.Python.statementToCode(block, 'AIELSE');
  7962. let allBlocks = block.workspace.getAllBlocks();
  7963. let global = ""
  7964. try {
  7965. global = allBlocks[0].workspace.variableList.toString()
  7966. }
  7967. catch (e) {
  7968. console.log(e)
  7969. }
  7970. console.log(statements_input1)
  7971. Blockly.Python.addFunction("digitalStatements", `def digitalStatements(BOXESI):
  7972. ${global != "" ? `global ${global}` : "pass"}
  7973. ${statements_input}
  7974. `)
  7975. var code = `if (len(BOXES)):
  7976. for BOXESI in BOXES:
  7977. digitalStatements(BOXESI)
  7978. else:
  7979. ${statements_input1 != "" ? statements_input1 : " pass"}
  7980. `
  7981. return code;
  7982. };
  7983. Blockly.Blocks['ai_model_digital_result'] = {
  7984. init: function () {
  7985. this.appendDummyInput()
  7986. .appendField(new Blockly.FieldImage("blockly/media/digit_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  7987. this.appendDummyInput()
  7988. // .appendField(Blockly.Msg.ai_models_from)
  7989. // .appendField(new Blockly.FieldVariable("i"), "varitem")
  7990. .appendField(Blockly.Msg.ai_models_hand_written_model_result);
  7991. this.appendDummyInput()
  7992. .appendField(new Blockly.FieldDropdown([
  7993. [Blockly.Msg.ai_models_hand_written_model_number, "c"],
  7994. [Blockly.Msg.ai_models_hand_written_model_confidence, "confidence"],
  7995. [Blockly.Msg.ai_models_object_model_x, "X"],
  7996. [Blockly.Msg.ai_models_object_model_y, "Y"],
  7997. // [Blockly.Msg.ai_models_object_model_w, "W"],
  7998. // [Blockly.Msg.ai_models_object_model_h, "H"],
  7999. [Blockly.Msg.ai_models_face_model_ws, "2"],
  8000. [Blockly.Msg.ai_models_face_model_hs, "3"],
  8001. [Blockly.Msg.ai_models_object_model_x_center, "CX"],
  8002. [Blockly.Msg.ai_models_object_model_y_center, "CY"]
  8003. ]), "value");
  8004. this.setOutput(true, null);
  8005. this.setColour("#ee783a");
  8006. this.setTooltip(Blockly.Msg.ai_models_hand_written_model_result);
  8007. this.setHelpUrl('');
  8008. }
  8009. };
  8010. Blockly.Python.ai_model_digital_result = function (block) {
  8011. var value = "BOXESI"
  8012. var type = block.getFieldValue('value');
  8013. // var _code = `"{}:{:.2f}%".format(number_recognition.labels[probs[${value}][0]], probs[${value}][1][probs[${value}][0]]*100)`;
  8014. if (type == "confidence") {
  8015. _code = `${value}[4][1][${value}[4][0]]*100`;
  8016. }
  8017. else if (type == "X") {
  8018. _code = `${value}[0]`;
  8019. }
  8020. else if (type == "Y") {
  8021. _code = `${value}[1]`;
  8022. }
  8023. else if (type == "W") {
  8024. _code = `int(${value}[0] + ${value}[2])`;
  8025. }
  8026. else if (type == "H") {
  8027. _code = `int(${value}[1] + ${value}[3])`;
  8028. }
  8029. else if (type == "c") {
  8030. _code = `number_recognition.labels[${value}[4][0]]`;
  8031. } else if (type == "CX") {
  8032. _code = `int((${value}[0] +${value}[0] + ${value}[2])/2)`;
  8033. } else if (type == "CY") {
  8034. _code = `int((${value}[1] + ${value}[1] + ${value}[3])/2)`;
  8035. }
  8036. else {
  8037. _code = `${value}[${type}]`;
  8038. }
  8039. return [_code, Blockly.Python.ORDER_ATOMIC];
  8040. }
  8041. Blockly.Blocks['ai_model_digital'] = {
  8042. init: function () {
  8043. this.appendDummyInput()
  8044. .appendField(Blockly.Msg.ai_model_digital);
  8045. // this.appendDummyInput()
  8046. // .appendField(new Blockly.FieldDropdown([
  8047. // [Blockly.Msg.ai_models_hand_written_model_number, "digitalResult"],
  8048. // [Blockly.Msg.ai_models_hand_written_model_confidence, "digitalConfidence"],
  8049. // ["检测框", "digitalCheckout"]
  8050. // ]), "digital");
  8051. this.setInputsInline(false);
  8052. this.setPreviousStatement(true, null);
  8053. this.setNextStatement(true, null);
  8054. this.setColour("#ee783a");
  8055. this.setTooltip();
  8056. this.setHelpUrl("");
  8057. }
  8058. };
  8059. Blockly.Python.ai_model_digital = function (block) {
  8060. Blockly.Python.addVariable("getDigitalResult", `getDigitalResult = []`)
  8061. Blockly.Python.addFunction("digitalShowCanvas", `def digitalShowCanvas():
  8062. global BOXES,getDigitalResult
  8063. getDigitalResult = []
  8064. if len(BOXES):
  8065. for BOXESI, BOXOBJ in enumerate(BOXES):
  8066. canvas.draw_string(0,0, (str(number_recognition.labels[PROBS[BOXESI][0]]) + str(str(":") + str(round(PROBS[BOXESI][1][PROBS[BOXESI][0]]*100, 2)))), scale = 1, color = (255,0,0) , thickness = 1)
  8067. canvas.draw_rectangle(BOXOBJ[0],BOXOBJ[1], BOXOBJ[0]+BOXOBJ[2],BOXOBJ[1]+ BOXOBJ[3], color=(255,0,0), thickness=1)
  8068. getDigitalResult.append({"digitalResult":number_recognition.labels[PROBS[BOXESI][0]],"digitalConfidence":round(PROBS[BOXESI][1][PROBS[BOXESI][0]]*100, 2)})
  8069. `)
  8070. var _code = "digitalShowCanvas()\n";
  8071. return _code;
  8072. }
  8073. Blockly.Blocks['ai_model_digital_attribute'] = {
  8074. init: function () {
  8075. this.appendDummyInput()
  8076. .appendField(Blockly.Msg.ai_model_digital_attribute);
  8077. this.setOutput(false, null);
  8078. this.setNextStatement(true, null);
  8079. this.setPreviousStatement(true, null);
  8080. this.setColour("#ee783a");
  8081. this.setTooltip(Blockly.Msg.ai_model_digital_attribute);
  8082. this.setHelpUrl("");
  8083. }
  8084. };
  8085. Blockly.Python['ai_model_digital_attribute'] = function (block) {
  8086. Blockly.Python.addFunction("", `def ai_model_digital_attribute():`)
  8087. var code = 'len(BOXES)';
  8088. return code;
  8089. };
  8090. Blockly.Blocks['ai_model_object_init'] = {
  8091. init: function () {
  8092. this.appendDummyInput()
  8093. .appendField(Blockly.Msg.ai_models_init_title)
  8094. .appendField(Blockly.Msg.ai_models_init_object_model);
  8095. // this.appendDummyInput().appendField(Blockly.Msg.ai_model_allinit_language).appendField(new Blockly.FieldDropdown([
  8096. // [Blockly.Msg.ai_speech_recognition_setup_set_english, "English"],
  8097. // [Blockly.Msg.ai_speech_recognition_setup_set_simplified_chinese, "Simplified Chinese"],
  8098. // [Blockly.Msg.ai_speech_recognition_setup_set_traditional_chinese, "Traditional Chinese"]
  8099. // ]), "interface")
  8100. // this.appendValueInput("class_input")
  8101. // .setCheck(null)
  8102. // .appendField(Blockly.Msg.ai_models_customized_init_setup_name);
  8103. this.setPreviousStatement(true, null);
  8104. this.setNextStatement(true, null);
  8105. this.setColour("#ee783a");
  8106. var thisBlock = this;
  8107. this.setTooltip(function () {
  8108. var mode = thisBlock.getFieldValue('DEGREE');
  8109. var TOOLTIPS = {
  8110. 'Common Object Recognition': Blockly.Msg.ai_model_allinit_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_object_model_title)
  8111. };
  8112. return TOOLTIPS[mode];
  8113. });
  8114. this.setHelpUrl('');
  8115. }
  8116. };
  8117. Blockly.Python.ai_model_object_init = function (block) {
  8118. var language = localStorage.getItem("handPyLanguage");
  8119. let a = ''
  8120. if (language == "en") {
  8121. a = '["aeroplane","bicycle","bird","boat","bottle","bus","car","cat","chair","cow","diningtable","dog","horse","motorbike","person","pottedplant","sheep","sofa","train","tvmonitor"]'
  8122. } else if (language == "zh-hans") {
  8123. a = '["飞机", "自行车", "鸟", "船", "瓶子", "公共汽车", "汽车", "猫", "椅子", "牛", "餐桌", "狗", "马", "摩托车", "人", "盆栽", "羊", "沙发", "火车", "电视监视器"]'
  8124. } else {
  8125. a = '["飛機", "自行車", "鳥", "船", "瓶子", "公共汽車", "汽車", "貓", "椅子", "牛", "餐桌", "狗", "馬", "摩托車", "人", "盆栽", "羊", "沙發", "火車", "電視監視器"]'
  8126. }
  8127. console.log(language)
  8128. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  8129. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  8130. Blockly.Python.definitions_.import_object = `
  8131. model = {
  8132. "param": "/root/preset/model/yolo2_20class_awnn.param",
  8133. "bin": "/root/preset/model/yolo2_20class_awnn.bin"
  8134. }
  8135. options = {
  8136. "model_type": "awnn",
  8137. "inputs": {
  8138. "input0": (224, 224, 3)
  8139. },
  8140. "outputs": {
  8141. "output0": (7, 7, (1+4+20)*5)
  8142. },
  8143. "mean": [127.5, 127.5, 127.5],
  8144. "norm": [0.0078125, 0.0078125, 0.0078125],
  8145. }
  8146. labels = ${a}
  8147. anchors = [5.4, 5.38, 1.65, 2.09, 0.8, 1.83, 2.45, 4.14, 0.46, 0.8]
  8148. m = nn.load(model, opt=options)
  8149. yolo2_decoder = decoder.Yolo2(len(labels), anchors, net_in_size=(options["inputs"]["input0"][0], options["inputs"]["input0"][1]), net_out_size=(7, 7))
  8150. `;
  8151. var _code = "";
  8152. return _code;
  8153. }
  8154. Blockly.Blocks['ai_model_object_load'] = {
  8155. init: function () {
  8156. this.appendDummyInput()
  8157. .appendField(Blockly.Msg.ai_models_load_title)
  8158. .appendField(Blockly.Msg.ai_models_init_object_model);
  8159. // this.appendDummyInput()
  8160. // .appendField(Blockly.Msg.ai_models_init_source)
  8161. // .appendField(new Blockly.FieldVariable("img_objectrecognition"), "varitem");
  8162. this.setPreviousStatement(true, null);
  8163. this.setNextStatement(true, null);
  8164. this.setColour("#ee783a");
  8165. var thisBlock = this;
  8166. this.setTooltip(function () {
  8167. var mode = thisBlock.getFieldValue('DEGREE');
  8168. var TOOLTIPS = {
  8169. 'Common Object Recognition': Blockly.Msg.ai_model_allload_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_object_model)
  8170. };
  8171. return TOOLTIPS[mode];
  8172. });
  8173. this.setHelpUrl('');
  8174. }
  8175. };
  8176. Blockly.Python.ai_model_object_load = function (block) {
  8177. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  8178. // var degee = block.getFieldValue('DEGREE');
  8179. var language = localStorage.getItem("handPyLanguage");
  8180. let a = ''
  8181. if (language == "en") {
  8182. a = '["aeroplane","bicycle","bird","boat","bottle","bus","car","cat","chair","cow","diningtable","dog","horse","motorbike","person","pottedplant","sheep","sofa","train","tvmonitor"]'
  8183. } else if (language == "zh-hans") {
  8184. a = '["飞机", "自行车", "鸟", "船", "瓶子", "公共汽车", "汽车", "猫", "椅子", "牛", "餐桌", "狗", "马", "摩托车", "人", "盆栽", "羊", "沙发", "火车", "电视监视器"]'
  8185. } else {
  8186. a = '["飛機", "自行車", "鳥", "船", "瓶子", "公共汽車", "汽車", "貓", "椅子", "牛", "餐桌", "狗", "馬", "摩托車", "人", "盆栽", "羊", "沙發", "火車", "電視監視器"]'
  8187. }
  8188. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  8189. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  8190. Blockly.Python.definitions_.import_object = `class ObjectRecognition:
  8191. def __init__(self):
  8192. model = {
  8193. "param": "/root/preset/model/yolo2_20class_awnn.param",
  8194. "bin": "/root/preset/model/yolo2_20class_awnn.bin"
  8195. }
  8196. options = {
  8197. "model_type": "awnn",
  8198. "inputs": {
  8199. "input0": (224, 224, 3)
  8200. },
  8201. "outputs": {
  8202. "output0": (7, 7, (1+4+20)*5)
  8203. },
  8204. "mean": [127.5, 127.5, 127.5],
  8205. "norm": [0.0078125, 0.0078125, 0.0078125],
  8206. }
  8207. self.labels = ${a}
  8208. anchors = [5.4, 5.38, 1.65, 2.09, 0.8, 1.83, 2.45, 4.14, 0.46, 0.8]
  8209. self.m = nn.load(model, opt=options)
  8210. self.yolo2_decoder = decoder.Yolo2(len(self.labels), anchors, net_in_size=(224,224), net_out_size=(7, 7))
  8211. self.OBJECTS = {
  8212. "OBJECT_START_X": [],
  8213. "OBJECT_START_Y": [],
  8214. "OBJECT_CONFIDENCE": [],
  8215. "OBJECT_WIDTH": [],
  8216. "OBJECT_HEIGHT": [],
  8217. "OBJECT_CENTER_X": [],
  8218. "OBJECT_CENTER_Y": [],
  8219. "OBJECT_RESULT": [],
  8220. }
  8221. def objectRecognize(self, canvas):
  8222. self.OBJECTS = {
  8223. "OBJECT_START_X": [],
  8224. "OBJECT_START_Y": [],
  8225. "OBJECT_CONFIDENCE": [],
  8226. "OBJECT_WIDTH": [],
  8227. "OBJECT_HEIGHT": [],
  8228. "OBJECT_CENTER_X": [],
  8229. "OBJECT_CENTER_Y": [],
  8230. "OBJECT_RESULT": [],
  8231. }
  8232. out = self.m.forward(canvas.tobytes(), quantize=True, layout="hwc")
  8233. BOXES, PROBS = self.yolo2_decoder.run(out, nms=0.3, threshold=0.3, img_size=(224,224))
  8234. if len(BOXES):
  8235. for BOXESI, BOXOBJ in enumerate(BOXES):
  8236. canvas.draw_string(BOXOBJ[0],(BOXOBJ[1]), ("".join([str(x) for x in [str(self.labels[PROBS[BOXESI][0]]), ",", round(PROBS[BOXESI][1][PROBS[BOXESI][0]]*100, 2)]])), scale = 1, color = (51,102,255) , thickness = 1)
  8237. canvas.draw_rectangle(BOXOBJ[0],BOXOBJ[1], BOXOBJ[0]+BOXOBJ[2],BOXOBJ[1]+ BOXOBJ[3], color=(255,0,0), thickness=1)
  8238. BOXES[BOXESI].append(PROBS[BOXESI])
  8239. else:
  8240. BOXES = []
  8241. return BOXES
  8242. ObjectRecognition = ObjectRecognition()
  8243. `;
  8244. var _code = `_canvas_x, _canvas_y = 48,8
  8245. canvas = canvas.crop(48,8,224,224)
  8246. BOXES = ObjectRecognition.objectRecognize(canvas)
  8247. `
  8248. return _code;
  8249. }
  8250. // ai_model_look_new
  8251. Blockly.Blocks['ai_model_look_new'] = {
  8252. init: function () {
  8253. this.appendDummyInput()
  8254. .appendField(Blockly.Msg.ai_models_object_model_if)
  8255. this.appendStatementInput("input")
  8256. .setCheck(null)
  8257. .appendField(Blockly.Msg.basic_motion_statement_exec);
  8258. this.updateShape_();
  8259. this.setMutator(new Blockly.Mutator(['ai_model_false_create_with_item']));
  8260. this.setColour("#ee783a");
  8261. this.setPreviousStatement(true, null);
  8262. this.setNextStatement(true, null);
  8263. this.setTooltip("");
  8264. this.setHelpUrl("");
  8265. this.itemCount_ = 0;
  8266. },
  8267. mutationToDom: function () {
  8268. if (!this.itemCount_)
  8269. return null;
  8270. var container = document.createElement('mutation');
  8271. this.itemCount_ && container.setAttribute('aielse', 1);
  8272. return container;
  8273. },
  8274. domToMutation: function (a) {
  8275. this.itemCount_ = parseInt(a.getAttribute('aielse'), 10);
  8276. this.updateShape_();
  8277. },
  8278. decompose: function (a) {
  8279. var b = a.newBlock('ai_model_false_create_with_container');
  8280. b.initSvg();
  8281. var c = b.nextConnection
  8282. this.itemCount_ && (a = a.newBlock("ai_model_false_create_with_item"),
  8283. a.initSvg(),
  8284. c.connect(a.previousConnection));
  8285. console.log("this.elseCount_", a)
  8286. return b
  8287. },
  8288. compose: function (a) {
  8289. var b = a.nextConnection.targetBlock();
  8290. this.itemCount_ = 0;
  8291. var e = null;
  8292. if (b && b.type === "ai_model_false_create_with_item") {
  8293. this.itemCount_++;
  8294. e = b.statementConnection_;
  8295. b = b.nextConnection && b.nextConnection.targetBlock()
  8296. }
  8297. this.updateShape_()
  8298. Blockly.Mutator.reconnect(e, this, "AIELSE")
  8299. },
  8300. saveConnections: function (a) {
  8301. a = a.nextConnection.targetBlock();
  8302. if (a && a.type === "ai_model_false_create_with_item") {
  8303. e = this.getInput("AIELSE");
  8304. a.statementConnection_ = e && e.connection.targetConnection;
  8305. a = a.nextConnection && a.nextConnection.targetBlock()
  8306. }
  8307. },
  8308. updateShape_: function () {
  8309. this.getInput("AIELSE") && this.removeInput("AIELSE");
  8310. console.log("this.itemCount_", this.itemCount_)
  8311. this.itemCount_ && this.appendStatementInput("AIELSE").appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSE)
  8312. },
  8313. };
  8314. Blockly.Python.ai_model_look_new = function (block) {
  8315. var statements_input = Blockly.Python.statementToCode(block, 'input');
  8316. var statements_input1 = Blockly.Python.statementToCode(block, 'AIELSE');
  8317. let allBlocks = block.workspace.getAllBlocks();
  8318. let global = ""
  8319. try {
  8320. global = allBlocks[0].workspace.variableList.toString()
  8321. }
  8322. catch (e) {
  8323. console.log(e)
  8324. }
  8325. Blockly.Python.addFunction("modelStatements", `def modelStatements(BOXESI):
  8326. ${global != "" ? `global ${global}` : "pass"}
  8327. ${statements_input}
  8328. `)
  8329. var code = `if len(BOXES):
  8330. for BOXESI in BOXES:
  8331. modelStatements(BOXESI)
  8332. else:
  8333. ${statements_input1 != "" ? statements_input1 : " pass"}
  8334. `
  8335. return code;
  8336. }
  8337. Blockly.Blocks['ai_model_get'] = {
  8338. init: function () {
  8339. this.appendDummyInput()
  8340. .appendField(new Blockly.FieldImage("blockly/media/object_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  8341. this.appendDummyInput()
  8342. .appendField(Blockly.Msg.ai_models_from)
  8343. .appendField(Blockly.Msg.ai_models_object_model_get)
  8344. .appendField(new Blockly.FieldDropdown([
  8345. [Blockly.Msg.ai_models_object_model_name, "Object"],
  8346. [Blockly.Msg.ai_models_object_model_x, "X"],
  8347. [Blockly.Msg.ai_models_object_model_y, "Y"],
  8348. // [Blockly.Msg.ai_models_object_model_w, "W"],
  8349. // [Blockly.Msg.ai_models_object_model_h, "H"],
  8350. // [Blockly.Msg.ai_models_object_model_x_center, "CX"],
  8351. // [Blockly.Msg.ai_models_object_model_y_center, "CY"],
  8352. // [Blockly.Msg.ai_models_object_model_count, "Number"],
  8353. // [Blockly.Msg.ai_models_object_model_index, "Serial"],
  8354. [Blockly.Msg.ai_models_object_model_confidence, "Confidence"],
  8355. [Blockly.Msg.ai_models_face_model_ws, "2"],
  8356. [Blockly.Msg.ai_models_face_model_hs, "3"],
  8357. [Blockly.Msg.ai_models_object_model_x_center, "CX"],
  8358. [Blockly.Msg.ai_models_object_model_y_center, "CY"]
  8359. ]), "TYPE");
  8360. this.setInputsInline(false);
  8361. this.setOutput(true, null);
  8362. this.setColour("#ee783a");
  8363. var thisBlock = this;
  8364. this.setTooltip(function () {
  8365. var mode = thisBlock.getFieldValue('TYPE');
  8366. var TOOLTIPS = {
  8367. 'Object': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_name),
  8368. 'X': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_x),
  8369. 'Y': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_y),
  8370. 'W': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_w),
  8371. 'H': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_h),
  8372. // 'CX': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_x_center),
  8373. // 'CY': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_y_center),
  8374. // 'Number': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_count),
  8375. // 'Serial': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_index),
  8376. // 'Confidence': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_confidence)
  8377. };
  8378. return TOOLTIPS[mode];
  8379. });
  8380. this.setHelpUrl("");
  8381. }
  8382. };
  8383. Blockly.Python.ai_model_get = function (block) {
  8384. var type = block.getFieldValue('TYPE');
  8385. var variable_name = "BOXESI";
  8386. var _code = "";
  8387. if (type == "Object") {
  8388. _code = `labels[${variable_name}[4][0]]`
  8389. // _code = `"{}:{:.2f}%".format(labels[probs[${variable_name}][0]], probs[${variable_name}][1][probs[${variable_name}][0]]*100)`
  8390. } else if (type == "X") {
  8391. _code = '' + variable_name + '[0]';
  8392. } else if (type == "Y") {
  8393. _code = '' + variable_name + '[1]';
  8394. } else if (type == "W") {
  8395. _code = 'int(' + variable_name + '[0]+' + '' + variable_name + '[2])';
  8396. } else if (type == "H") {
  8397. _code = 'int(' + variable_name + '[1]+' + '' + variable_name + '[3])';
  8398. } else if (type == "Confidence") {
  8399. _code = `${variable_name}[4][1][${variable_name}[4][0]]*100`;
  8400. } else if (type == "CX") {
  8401. _code = `int((${variable_name}[0] +${variable_name}[0] + ${variable_name}[2])/2)`;
  8402. } else if (type == "CY") {
  8403. _code = `int((${variable_name}[1] + ${variable_name}[1] + ${variable_name}[3])/2)`;
  8404. }
  8405. else {
  8406. _code = `${variable_name}[${type}]`;
  8407. }
  8408. return [_code, Blockly.Python.ORDER_ATOMIC];
  8409. }
  8410. Blockly.Blocks['ai_model_look'] = {
  8411. init: function () {
  8412. this.appendDummyInput()
  8413. .appendField(Blockly.Msg.ai_models_object_model_if);
  8414. this.setInputsInline(false);
  8415. this.setOutput(true, null);
  8416. this.setColour("#ee783a");
  8417. this.setTooltip(Blockly.Msg.ai_model_look_TOOLTIP);
  8418. this.setHelpUrl("");
  8419. }
  8420. };
  8421. Blockly.Python.ai_model_look = function (block) {
  8422. var _code = "len(boxes)";
  8423. return [_code, Blockly.Python.ORDER_ATOMIC];
  8424. }
  8425. Blockly.Blocks['ai_model_look_attribute'] = {
  8426. init: function () {
  8427. this.appendDummyInput()
  8428. .appendField(Blockly.Msg.ai_model_look_attribute);
  8429. this.setNextStatement(true, null);
  8430. this.setPreviousStatement(true, null);
  8431. this.setColour("#ee783a");
  8432. this.setTooltip(Blockly.Msg.ai_model_look_attribute);
  8433. this.setHelpUrl("");
  8434. }
  8435. };
  8436. Blockly.Python['ai_model_look_attribute'] = function (block) {
  8437. Blockly.Python.addFunction("objectRecognize", `def objectRecognize():
  8438. global BOXES
  8439. if len(BOXES):
  8440. for BOXESI, BOXOBJ in enumerate(BOXES):
  8441. canvas.draw_string(BOXOBJ[0],(BOXOBJ[1]), ("".join([str(x) for x in [str(labels[PROBS[BOXESI][0]]), ",", round(PROBS[BOXESI][1][PROBS[BOXESI][0]]*100, 2)]])), scale = 1, color = (51,102,255) , thickness = 1)
  8442. canvas.draw_rectangle(BOXOBJ[0],BOXOBJ[1], BOXOBJ[0]+BOXOBJ[2],BOXOBJ[1]+ BOXOBJ[3], color=(255,0,0), thickness=1)
  8443. `)
  8444. var code = 'objectRecognize()\n';
  8445. return code;
  8446. };
  8447. // 猜拳识别
  8448. Blockly.Blocks['ai_model_Guesswork_init'] = {
  8449. init: function () {
  8450. this.appendDummyInput()
  8451. .appendField(Blockly.Msg.ai_models_init_title)
  8452. .appendField(Blockly.Msg.ai_models_init_guesswork_model);
  8453. // this.appendDummyInput().appendField(Blockly.Msg.ai_model_allinit_language).appendField(new Blockly.FieldDropdown([
  8454. // [Blockly.Msg.ai_speech_recognition_setup_set_english, "English"],
  8455. // [Blockly.Msg.ai_speech_recognition_setup_set_simplified_chinese, "Simplified Chinese"],
  8456. // [Blockly.Msg.ai_speech_recognition_setup_set_traditional_chinese, "Traditional Chinese"]
  8457. // ]), "interface")
  8458. // this.appendValueInput("class_input")
  8459. // .setCheck(null)
  8460. // .appendField(Blockly.Msg.ai_models_customized_init_setup_name);
  8461. this.setPreviousStatement(true, null);
  8462. this.setNextStatement(true, null);
  8463. this.setColour("#ee783a");
  8464. var thisBlock = this;
  8465. this.setTooltip(function () {
  8466. var mode = thisBlock.getFieldValue('DEGREE');
  8467. var TOOLTIPS = {
  8468. 'Common Object Recognition': Blockly.Msg.ai_model_allinit_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_object_model_title)
  8469. };
  8470. return TOOLTIPS[mode];
  8471. });
  8472. this.setHelpUrl('');
  8473. }
  8474. };
  8475. Blockly.Python.ai_model_Guesswork_init = function (block) {
  8476. var language = localStorage.getItem("handPyLanguage");
  8477. console.log(language)
  8478. let a = ''
  8479. if (language == "en") {
  8480. a = '["Scissors", "Stone" ,"Paper"]'
  8481. } else if (language == "zh-hans") {
  8482. a = '["剪刀", "石头" ,"布"]'
  8483. } else {
  8484. a = '["剪刀", "石頭" ,"布"]'
  8485. }
  8486. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  8487. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  8488. Blockly.Python.definitions_.import_Guesswork_init = `class Mora:
  8489. mud_path = "/root/preset/model/mora_int8.mud"
  8490. labels = ${a}
  8491. anchors = [3.23, 3.25, 1.47, 1.55, 5.09, 5.33, 4.03, 4.28, 2.12, 2.56]
  8492. def __init__(self) -> None:
  8493. from maix import nn
  8494. self.model = nn.load(self.mud_path)
  8495. from maix.nn import decoder
  8496. self.decoder = decoder.Yolo2(len(self.labels) , self.anchors , net_in_size = (224, 224) ,net_out_size = (7,7))
  8497. def __del__(self):
  8498. del self.model
  8499. del self.decoder
  8500. def cal_fps(self ,start , end):
  8501. one_second = 1
  8502. one_flash = end - start
  8503. fps = one_second / one_flash
  8504. return fps
  8505. def draw_rectangle_with_title(self ,img, box, disp_str , fps ):
  8506. img.draw_rectangle(box[0], box[1], box[0] + box[2], box[1] + box[3],color=(255, 0, 0), thickness=2)
  8507. img.draw_string(box[0], box[1] ,disp_str, scale=1,color=(222, 0, 3), thickness=2)
  8508. #img.draw_string(0, 0 ,"FPS :"+str(fps), scale=2 ,color=(0, 0, 255), thickness=2)
  8509. def process(self,input):
  8510. t = time()
  8511. out = self.model.forward(input, quantize=1, layout = "hwc")
  8512. boxes, probs = self.decoder.run(out, nms=0.5, threshold=0.3, img_size=(224,224))
  8513. for i, box in enumerate(boxes):
  8514. class_id = probs[i][0]
  8515. prob = probs[i][1][class_id]
  8516. disp_str = "{}:{:.2f}%".format(self.labels[class_id], prob*100)
  8517. fps = self.cal_fps(t, time())
  8518. self.draw_rectangle_with_title(input, box, disp_str, fps)
  8519. Mora = Mora()
  8520. `;
  8521. var _code = "";
  8522. return _code;
  8523. }
  8524. Blockly.Blocks['ai_model_Guesswork_load'] = {
  8525. init: function () {
  8526. this.appendDummyInput()
  8527. .appendField(Blockly.Msg.ai_models_load_title)
  8528. .appendField(Blockly.Msg.ai_models_init_guesswork_model);
  8529. // this.appendDummyInput()
  8530. // .appendField(Blockly.Msg.ai_models_init_source)
  8531. // .appendField(new Blockly.FieldVariable("img_guessworkrecognition"), "varitem");
  8532. this.setPreviousStatement(true, null);
  8533. this.setNextStatement(true, null);
  8534. this.setColour("#ee783a");
  8535. var thisBlock = this;
  8536. this.setTooltip(function () {
  8537. var mode = thisBlock.getFieldValue('DEGREE');
  8538. var TOOLTIPS = {
  8539. 'Common Object Recognition': Blockly.Msg.ai_model_allload_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_object_model)
  8540. };
  8541. return TOOLTIPS[mode];
  8542. });
  8543. this.setHelpUrl('');
  8544. }
  8545. };
  8546. Blockly.Python.ai_model_Guesswork_load = function (block) {
  8547. var language = localStorage.getItem("handPyLanguage");
  8548. let a = ''
  8549. if (language == "en") {
  8550. a = '["Scissors", "Stone" ,"Paper"]'
  8551. } else if (language == "zh-hans") {
  8552. a = '["剪刀", "石头" ,"布"]'
  8553. } else {
  8554. a = '["剪刀", "石頭" ,"布"]'
  8555. }
  8556. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  8557. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  8558. Blockly.Python.definitions_.import_Guesswork_init = `class Mora:
  8559. mud_path = "/root/preset/model/mora_int8.mud"
  8560. labels = ${a}
  8561. anchors = [3.23, 3.25, 1.47, 1.55, 5.09, 5.33, 4.03, 4.28, 2.12, 2.56]
  8562. def __init__(self) -> None:
  8563. self.model = nn.load(self.mud_path)
  8564. self.decoder = decoder.Yolo2(len(self.labels) , self.anchors , net_in_size = (224, 224) ,net_out_size = (7,7))
  8565. self.MORAS = {
  8566. "MORA_START_X": [],
  8567. "MORA_START_Y": [],
  8568. "MORA_CONFIDENCE": [],
  8569. "MORA_WIDTH": [],
  8570. "MORA_HEIGHT": [],
  8571. "MORA_CENTER_X": [],
  8572. "MORA_CENTER_Y": [],
  8573. "MORA_RESULT": [],
  8574. }
  8575. def __del__(self):
  8576. del self.model
  8577. del self.decoder
  8578. def cal_fps(self ,start , end):
  8579. one_second = 1
  8580. one_flash = end - start
  8581. fps = one_second / one_flash
  8582. return fps
  8583. def draw_rectangle_with_title(self ,img, box, disp_str ):
  8584. img.draw_rectangle(box[0], box[1], box[0] + box[2], box[1] + box[3],color=(255, 0, 0), thickness=2)
  8585. img.draw_string(box[0], box[1] ,disp_str, scale=1,color=(222, 0, 3), thickness=2)
  8586. def process(self,input):
  8587. out = self.model.forward(input, quantize=1, layout = "hwc")
  8588. boxes, probs = self.decoder.run(out, nms=0.5, threshold=0.3, img_size=(224,224))
  8589. for i, box in enumerate(boxes):
  8590. class_id = probs[i][0]
  8591. prob = probs[i][1][class_id]
  8592. disp_str = "{}:{:.2f}%".format(self.labels[class_id], prob*100)
  8593. self.draw_rectangle_with_title(input, box, disp_str)
  8594. boxes[i].append(probs[i])
  8595. return boxes
  8596. Mora = Mora()
  8597. `;
  8598. var _code = `_canvas_x, _canvas_y = 48,8
  8599. canvas = canvas.crop(48,8,224,224)
  8600. BOXES = Mora.process(canvas)
  8601. `
  8602. return _code;
  8603. }
  8604. // ai_model_Guesswork_look_new
  8605. Blockly.Blocks['ai_model_Guesswork_look_new'] = {
  8606. init: function () {
  8607. this.appendDummyInput()
  8608. .appendField(Blockly.Msg.ai_model_guesswork_look_attribute)
  8609. this.appendStatementInput("input")
  8610. .setCheck(null)
  8611. .appendField(Blockly.Msg.basic_motion_statement_exec);
  8612. this.updateShape_();
  8613. this.setMutator(new Blockly.Mutator(['ai_model_false_create_with_item']));
  8614. this.setColour("#ee783a");
  8615. this.setPreviousStatement(true, null);
  8616. this.setNextStatement(true, null);
  8617. this.setTooltip("");
  8618. this.setHelpUrl("");
  8619. this.itemCount_ = 0;
  8620. },
  8621. mutationToDom: function () {
  8622. if (!this.itemCount_)
  8623. return null;
  8624. var container = document.createElement('mutation');
  8625. this.itemCount_ && container.setAttribute('aielse', 1);
  8626. return container;
  8627. },
  8628. domToMutation: function (a) {
  8629. this.itemCount_ = parseInt(a.getAttribute('aielse'), 10);
  8630. this.updateShape_();
  8631. },
  8632. decompose: function (a) {
  8633. var b = a.newBlock('ai_model_false_create_with_container');
  8634. b.initSvg();
  8635. var c = b.nextConnection
  8636. this.itemCount_ && (a = a.newBlock("ai_model_false_create_with_item"),
  8637. a.initSvg(),
  8638. c.connect(a.previousConnection));
  8639. console.log("this.elseCount_", a)
  8640. return b
  8641. },
  8642. compose: function (a) {
  8643. var b = a.nextConnection.targetBlock();
  8644. this.itemCount_ = 0;
  8645. var e = null;
  8646. if (b && b.type === "ai_model_false_create_with_item") {
  8647. this.itemCount_++;
  8648. e = b.statementConnection_;
  8649. b = b.nextConnection && b.nextConnection.targetBlock()
  8650. }
  8651. this.updateShape_()
  8652. Blockly.Mutator.reconnect(e, this, "AIELSE")
  8653. },
  8654. saveConnections: function (a) {
  8655. a = a.nextConnection.targetBlock();
  8656. if (a && a.type === "ai_model_false_create_with_item") {
  8657. e = this.getInput("AIELSE");
  8658. a.statementConnection_ = e && e.connection.targetConnection;
  8659. a = a.nextConnection && a.nextConnection.targetBlock()
  8660. }
  8661. },
  8662. updateShape_: function () {
  8663. this.getInput("AIELSE") && this.removeInput("AIELSE");
  8664. console.log("this.itemCount_", this.itemCount_)
  8665. this.itemCount_ && this.appendStatementInput("AIELSE").appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSE)
  8666. },
  8667. };
  8668. Blockly.Python.ai_model_Guesswork_look_new = function (block) {
  8669. var statements_input = Blockly.Python.statementToCode(block, 'input');
  8670. var statements_input1 = Blockly.Python.statementToCode(block, 'AIELSE');
  8671. let allBlocks = block.workspace.getAllBlocks();
  8672. let global = ""
  8673. try {
  8674. global = allBlocks[0].workspace.variableList.toString()
  8675. }
  8676. catch (e) {
  8677. console.log(e)
  8678. }
  8679. Blockly.Python.addFunction("GuessworkStatements", `def GuessworkStatements(BOXESI):
  8680. ${global != "" ? `global ${global}` : "pass"}
  8681. ${statements_input}
  8682. `)
  8683. var code = `if len(BOXES) > 0:
  8684. for BOXESI in BOXES:
  8685. GuessworkStatements(BOXESI)
  8686. else:
  8687. ${statements_input1 != "" ? statements_input1 : " pass"}
  8688. `
  8689. return code;
  8690. }
  8691. Blockly.Blocks['ai_model_Guesswork_get'] = {
  8692. init: function () {
  8693. this.appendDummyInput()
  8694. .appendField(new Blockly.FieldImage("blockly/media/object_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  8695. this.appendDummyInput()
  8696. .appendField(Blockly.Msg.ai_models_from)
  8697. .appendField(Blockly.Msg.ai_models_guesswork_model_get)
  8698. .appendField(new Blockly.FieldDropdown([
  8699. [Blockly.Msg.ai_models_object_model_name, "Object"],
  8700. [Blockly.Msg.ai_models_object_model_x, "X"],
  8701. [Blockly.Msg.ai_models_object_model_y, "Y"],
  8702. // [Blockly.Msg.ai_models_object_model_w, "W"],
  8703. // [Blockly.Msg.ai_models_object_model_h, "H"],
  8704. // [Blockly.Msg.ai_models_object_model_x_center, "CX"],
  8705. // [Blockly.Msg.ai_models_object_model_y_center, "CY"],
  8706. // [Blockly.Msg.ai_models_object_model_count, "Number"],
  8707. // [Blockly.Msg.ai_models_object_model_index, "Serial"],
  8708. [Blockly.Msg.ai_models_object_model_confidence, "Confidence"],
  8709. [Blockly.Msg.ai_models_face_model_ws, "2"],
  8710. [Blockly.Msg.ai_models_face_model_hs, "3"],
  8711. [Blockly.Msg.ai_models_object_model_x_center, "CX"],
  8712. [Blockly.Msg.ai_models_object_model_y_center, "CY"]
  8713. ]), "TYPE");
  8714. this.setInputsInline(false);
  8715. this.setOutput(true, null);
  8716. this.setColour("#ee783a");
  8717. var thisBlock = this;
  8718. this.setTooltip(Blockly.Msg.ai_models_guesswork_model_get);
  8719. this.setHelpUrl("");
  8720. }
  8721. };
  8722. Blockly.Python.ai_model_Guesswork_get = function (block) {
  8723. var type = block.getFieldValue('TYPE');
  8724. var variable_name = "BOXESI";
  8725. var _code = "";
  8726. if (type == "Object") {
  8727. _code = `Mora.labels[${variable_name}[4][0]]`
  8728. // _code = `"{}:{:.2f}%".format(labels[probs[${variable_name}][0]], probs[${variable_name}][1][probs[${variable_name}][0]]*100)`
  8729. } else if (type == "X") {
  8730. _code = '' + variable_name + '[0]';
  8731. } else if (type == "Y") {
  8732. _code = '' + variable_name + '[1]';
  8733. } else if (type == "W") {
  8734. _code = 'int(' + variable_name + '[0]+' + '' + variable_name + '[2])';
  8735. } else if (type == "H") {
  8736. _code = 'int(' + variable_name + '[1]+' + '' + variable_name + '[3])';
  8737. } else if (type == "Confidence") {
  8738. _code = `${variable_name}[4][1][${variable_name}[4][0]]*100`;
  8739. } else if (type == "CX") {
  8740. _code = `int((${variable_name}[0] +${variable_name}[0] + ${variable_name}[2])/2)`;
  8741. } else if (type == "CY") {
  8742. _code = `int((${variable_name}[1] + ${variable_name}[1] + ${variable_name}[3])/2)`;
  8743. }
  8744. else {
  8745. _code = `${variable_name}[${type}]`;
  8746. }
  8747. return [_code, Blockly.Python.ORDER_ATOMIC];
  8748. }
  8749. Blockly.Blocks['ai_model_Guesswork_look'] = {
  8750. init: function () {
  8751. this.appendDummyInput()
  8752. .appendField(Blockly.Msg.ai_models_guesswork_model_if);
  8753. this.setInputsInline(false);
  8754. this.setNextStatement(true, null);
  8755. this.setPreviousStatement(true, null);
  8756. this.setColour("#ee783a");
  8757. this.setTooltip(Blockly.Msg.ai_models_guesswork_model_if);
  8758. this.setHelpUrl("");
  8759. }
  8760. };
  8761. Blockly.Python.ai_model_Guesswork_look = function (block) {
  8762. Blockly.Python.addFunction("guessworkShowRecognize", `def guessworkShowRecognize():
  8763. global BOXES
  8764. if len(BOXES):
  8765. for BOXESI, BOXOBJ in enumerate(BOXES):
  8766. canvas.draw_string(10,0, (str(str(Mora.labels[PROBS[BOXESI][0]])) + str(str(":") + str(round(PROBS[BOXESI][1][PROBS[BOXESI][0]]*100, 2)))), scale = 1, color = (255,0,0) , thickness = 1)
  8767. canvas.draw_rectangle(BOXOBJ[0],BOXOBJ[1], BOXOBJ[0]+BOXOBJ[2],BOXOBJ[1]+ BOXOBJ[3], color=(255,0,0), thickness=1)
  8768. `)
  8769. var _code = "guessworkShowRecognize()\n";
  8770. return _code;
  8771. }
  8772. Blockly.Blocks['ai_model_Guesswork_look_attribute'] = {
  8773. init: function () {
  8774. this.appendDummyInput()
  8775. .appendField(Blockly.Msg.ai_model_guesswork_look_attribute);
  8776. this.setOutput(true, null);
  8777. this.setColour("#ee783a");
  8778. this.setTooltip(Blockly.Msg.ai_model_guesswork_look_attribute);
  8779. this.setHelpUrl("");
  8780. }
  8781. };
  8782. Blockly.Python['ai_model_Guesswork_look_attribute'] = function (block) {
  8783. var code = 'len(BOXES)';
  8784. return [code, Blockly.Python.ORDER_NONE];
  8785. };
  8786. Blockly.Blocks['ai_model_face_recognition_load'] = {
  8787. init: function () {
  8788. this.appendDummyInput()
  8789. .appendField(Blockly.Msg.ai_models_load_title)
  8790. .appendField(Blockly.Msg.ai_models_init_face_model);
  8791. // this.appendDummyInput()
  8792. // .appendField(Blockly.Msg.ai_models_init_source)
  8793. // .appendField(new Blockly.FieldVariable("img_facedetection"), "varitem");
  8794. this.setPreviousStatement(true, null);
  8795. this.setNextStatement(true, null);
  8796. this.setColour("#ee783a");
  8797. var thisBlock = this;
  8798. this.setTooltip(function () {
  8799. var mode = thisBlock.getFieldValue('DEGREE');
  8800. var TOOLTIPS = {
  8801. 'Human Face Recognition': Blockly.Msg.ai_model_allload_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_face_model)
  8802. };
  8803. return TOOLTIPS[mode];
  8804. });
  8805. this.setHelpUrl('');
  8806. }
  8807. };
  8808. Blockly.Python.ai_model_face_recognition_load = function (block) {
  8809. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  8810. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  8811. Blockly.Python.definitions_.import_face_recognition = `class YoloFaceRecognize:
  8812. labels = ["person"]
  8813. anchors = [1.19, 1.98, 2.79, 4.59, 4.53, 8.92, 8.06, 5.29, 10.32, 10.65]
  8814. m = {
  8815. "param": "/root/preset/model/yolo2_face_awnn.param",
  8816. "bin": "/root/preset/model/yolo2_face_awnn.bin"
  8817. }
  8818. options = {
  8819. "model_type": "awnn",
  8820. "inputs": {
  8821. "input0": (224, 224, 3)
  8822. },
  8823. "outputs": {
  8824. "output0": (7, 7, (1+4+len(labels))*5)
  8825. },
  8826. "mean": [127.5, 127.5, 127.5],
  8827. "norm": [0.0078125, 0.0078125, 0.0078125],
  8828. }
  8829. def __init__(self):
  8830. self.model = nn.load(self.m, opt=self.options)
  8831. self.decoder = decoder.Yolo2(len(self.labels), self.anchors, net_in_size=(224, 224), net_out_size=(7, 7))
  8832. self.FACES = {
  8833. "FACES_START_X": [],
  8834. "FACES_START_Y": [],
  8835. "FACENUM": 0,
  8836. "FACES_CONFIDENCE": [],
  8837. "FACES_WIDTH": [],
  8838. "FACES_HEIGHT": [],
  8839. "FACES_CENTER_X": [],
  8840. "FACES_CENTER_Y": []
  8841. }
  8842. def __del__(self):
  8843. del self.model
  8844. del self.decoder
  8845. def faceShowDetection(self,canvas):
  8846. out = YoloFaceRecognize.model.forward(canvas.tobytes(), quantize=True, layout="hwc")
  8847. BOXES, PROBS = YoloFaceRecognize.decoder.run(out, nms=0.3, threshold=0.5, img_size=(224, 224))
  8848. if len(BOXES):
  8849. for BOXESI, BOXOBJ in enumerate(BOXES):
  8850. canvas.draw_rectangle(BOXOBJ[0],BOXOBJ[1], BOXOBJ[0]+BOXOBJ[2],BOXOBJ[1]+ BOXOBJ[3], color=(255,0,0), thickness=1)
  8851. BOXES[BOXESI].append(PROBS[BOXESI])
  8852. def returnFaces(self):
  8853. return self.FACES
  8854. YoloFaceRecognize = YoloFaceRecognize()
  8855. `;
  8856. var _code = `_canvas_x, _canvas_y = 48,8
  8857. canvas = canvas.crop(48,8,224,224)
  8858. BOXES = YoloFaceRecognize.faceShowDetection(canvas)\n`
  8859. return _code;
  8860. }
  8861. Blockly.Blocks['ai_model_xy'] = {
  8862. init: function () {
  8863. this.appendDummyInput()
  8864. .appendField(new Blockly.FieldImage("blockly/media/face_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  8865. this.appendDummyInput()
  8866. .appendField(Blockly.Msg.ai_models_from)
  8867. .appendField(Blockly.Msg.ai_models_face_model_get)
  8868. .appendField(new Blockly.FieldDropdown([
  8869. [Blockly.Msg.ai_models_object_model_name, "Object"],
  8870. [Blockly.Msg.ai_models_face_model_x, "X"],
  8871. [Blockly.Msg.ai_models_face_model_y, "Y"],
  8872. // [Blockly.Msg.ai_models_face_model_w, "W"],
  8873. // [Blockly.Msg.ai_models_face_model_h, "H"],
  8874. // [Blockly.Msg.ai_models_face_model_x_center, "CX"],
  8875. // [Blockly.Msg.ai_models_face_model_y_center, "CY"],
  8876. [Blockly.Msg.ai_models_face_model_count, "Number"],
  8877. // [Blockly.Msg.ai_models_face_model_index, "Serial"],
  8878. [Blockly.Msg.ai_models_face_model_confidence, "Confidence"],
  8879. [Blockly.Msg.ai_models_face_model_ws, "2"],
  8880. [Blockly.Msg.ai_models_face_model_hs, "3"],
  8881. [Blockly.Msg.ai_models_object_model_x_center, "CX"],
  8882. [Blockly.Msg.ai_models_object_model_y_center, "CY"]
  8883. ]), "TYPE");
  8884. this.setInputsInline(false);
  8885. this.setOutput(true, null);
  8886. this.setColour("#ee783a");
  8887. var thisBlock = this;
  8888. this.setTooltip(function () {
  8889. var mode = thisBlock.getFieldValue('TYPE');
  8890. var TOOLTIPS = {
  8891. 'X': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x),
  8892. 'Y': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y),
  8893. 'W': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_w),
  8894. 'H': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_h),
  8895. // 'CX': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x_center),
  8896. // 'CY': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y_center),
  8897. 'Number': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_count),
  8898. // 'Serial': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_index),
  8899. // 'Confidence': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_confidence)
  8900. };
  8901. return TOOLTIPS[mode];
  8902. });
  8903. this.setHelpUrl("");
  8904. }
  8905. };
  8906. Blockly.Python.ai_model_xy = function (block) {
  8907. var type = block.getFieldValue('TYPE');
  8908. var variable_name = "BOXESI";
  8909. var _code = "";
  8910. if (type == "X") {
  8911. _code = '' + variable_name + '[0]';
  8912. } else if (type == "Y") {
  8913. _code = '' + variable_name + '[1]';
  8914. } else if (type == "W") {
  8915. _code = 'int(' + variable_name + '[0]+' + '' + variable_name + '[2])';
  8916. } else if (type == "H") {
  8917. _code = 'int(' + variable_name + '[1]+' + '' + variable_name + '[3])';
  8918. } else if (type == "Confidence") {
  8919. _code = `${variable_name}[4][1][${variable_name}[4][0]]*100`;
  8920. } else if (type == "Object") {
  8921. _code = `"Face"`;
  8922. } else if (type == "CX") {
  8923. _code = `int((${variable_name}[0] +${variable_name}[0] + ${variable_name}[2])/2)`;
  8924. } else if (type == "CY") {
  8925. _code = `int((${variable_name}[1] + ${variable_name}[1] + ${variable_name}[3])/2)`;
  8926. } else if (type == "Number") {
  8927. _code = `len(boxes)`;
  8928. }
  8929. else {
  8930. _code = `${variable_name}[${type}]`;
  8931. }
  8932. return [_code, Blockly.Python.ORDER_ATOMIC];
  8933. };
  8934. Blockly.Blocks['ai_model_face_new'] = {
  8935. init: function () {
  8936. this.appendDummyInput()
  8937. .appendField(Blockly.Msg.ai_model_face_model_list)
  8938. this.appendStatementInput("input")
  8939. .setCheck(null)
  8940. .appendField(Blockly.Msg.basic_motion_statement_exec);
  8941. this.updateShape_();
  8942. this.setMutator(new Blockly.Mutator(['ai_model_false_create_with_item']));
  8943. this.setColour("#ee783a");
  8944. this.setPreviousStatement(true, null);
  8945. this.setNextStatement(true, null);
  8946. this.setTooltip("");
  8947. this.setHelpUrl("");
  8948. this.itemCount_ = 0;
  8949. },
  8950. mutationToDom: function () {
  8951. if (!this.itemCount_)
  8952. return null;
  8953. var container = document.createElement('mutation');
  8954. this.itemCount_ && container.setAttribute('aielse', 1);
  8955. return container;
  8956. },
  8957. domToMutation: function (a) {
  8958. this.itemCount_ = parseInt(a.getAttribute('aielse'), 10);
  8959. this.updateShape_();
  8960. },
  8961. decompose: function (a) {
  8962. var b = a.newBlock('ai_model_false_create_with_container');
  8963. b.initSvg();
  8964. var c = b.nextConnection
  8965. this.itemCount_ && (a = a.newBlock("ai_model_false_create_with_item"),
  8966. a.initSvg(),
  8967. c.connect(a.previousConnection));
  8968. console.log("this.elseCount_", a)
  8969. return b
  8970. },
  8971. compose: function (a) {
  8972. var b = a.nextConnection.targetBlock();
  8973. this.itemCount_ = 0;
  8974. var e = null;
  8975. if (b && b.type === "ai_model_false_create_with_item") {
  8976. this.itemCount_++;
  8977. e = b.statementConnection_;
  8978. b = b.nextConnection && b.nextConnection.targetBlock()
  8979. }
  8980. this.updateShape_()
  8981. Blockly.Mutator.reconnect(e, this, "AIELSE")
  8982. },
  8983. saveConnections: function (a) {
  8984. a = a.nextConnection.targetBlock();
  8985. if (a && a.type === "ai_model_false_create_with_item") {
  8986. e = this.getInput("AIELSE");
  8987. a.statementConnection_ = e && e.connection.targetConnection;
  8988. a = a.nextConnection && a.nextConnection.targetBlock()
  8989. }
  8990. },
  8991. updateShape_: function () {
  8992. this.getInput("AIELSE") && this.removeInput("AIELSE");
  8993. console.log("this.itemCount_", this.itemCount_)
  8994. this.itemCount_ && this.appendStatementInput("AIELSE").appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSE)
  8995. },
  8996. };
  8997. Blockly.Python.ai_model_face_new = function (block) {
  8998. var statements_input = Blockly.Python.statementToCode(block, 'input');
  8999. var statements_input1 = Blockly.Python.statementToCode(block, 'AIELSE');
  9000. let allBlocks = block.workspace.getAllBlocks();
  9001. let global = ""
  9002. try {
  9003. global = allBlocks[0].workspace.variableList.toString()
  9004. }
  9005. catch (e) {
  9006. console.log(e)
  9007. }
  9008. Blockly.Python.addFunction("GuessworkStatements", `def GuessworkStatements(BOXESI):
  9009. ${global != "" ? `global ${global}` : "pass"}
  9010. ${statements_input}
  9011. `)
  9012. var code = `if len(BOXES) > 0:
  9013. for BOXESI in BOXES:
  9014. GuessworkStatements(BOXESI)
  9015. else:
  9016. ${statements_input1 != "" ? statements_input1 : " pass"}
  9017. `
  9018. return code;
  9019. }
  9020. Blockly.Blocks['ai_model_face'] = {
  9021. init: function () {
  9022. this.appendDummyInput()
  9023. .appendField(Blockly.Msg.ai_models_face_model_if);
  9024. this.setInputsInline(false);
  9025. this.setNextStatement(true, null);
  9026. this.setPreviousStatement(true, null);
  9027. this.setColour("#ee783a");
  9028. this.setTooltip(Blockly.Msg.ai_models_face_model_if);
  9029. this.setHelpUrl("");
  9030. }
  9031. };
  9032. Blockly.Python.ai_model_face = function (block) {
  9033. Blockly.Python.addFunction("faceShowDetection", `def faceShowDetection():
  9034. global BOXES
  9035. if len(BOXES):
  9036. for BOXESI, BOXOBJ in enumerate(BOXES):
  9037. canvas.draw_rectangle(BOXOBJ[0],BOXOBJ[1], BOXOBJ[0]+BOXOBJ[2],BOXOBJ[1]+ BOXOBJ[3], color=(255,0,0), thickness=1)
  9038. `)
  9039. var _code = "faceShowDetection()\n";
  9040. return _code;
  9041. }
  9042. Blockly.Blocks['ai_model_face_attribute'] = {
  9043. init: function () {
  9044. this.appendDummyInput()
  9045. .appendField(Blockly.Msg.ai_model_face_model_list);
  9046. this.setOutput(true, null);
  9047. this.setColour("#ee783a");
  9048. this.setTooltip(Blockly.Msg.ai_model_face_model_list);
  9049. this.setHelpUrl("");
  9050. }
  9051. };
  9052. Blockly.Python['ai_model_face_attribute'] = function (block) {
  9053. var code = 'boxes';
  9054. return [code, Blockly.Python.ORDER_NONE];
  9055. };
  9056. // 车牌识别
  9057. Blockly.Blocks['ai_model_card_recognition_init'] = {
  9058. init: function () {
  9059. // this.appendDummyInput()
  9060. // .appendField(new Blockly.FieldImage("blockly/media/header_customized_model.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
  9061. this.appendDummyInput()
  9062. .appendField(Blockly.Msg.ai_models_init_title)
  9063. .appendField(Blockly.Msg.ai_models_init_card_model);
  9064. this.setInputsInline(false);
  9065. this.setPreviousStatement(true, null);
  9066. this.setNextStatement(true, null);
  9067. this.setColour("#ee783a");
  9068. this.setTooltip(Blockly.Msg.ai_model_customized_init_setup_definition_TOOLTIP);
  9069. this.setHelpUrl("");
  9070. }
  9071. };
  9072. Blockly.Python['ai_model_card_recognition_init'] = function (block) {
  9073. // TODO: Assemble Python into code variable.
  9074. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  9075. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  9076. Blockly.Python.addVariable('CARDDATABOEX', `CARDDATABOEX = ""`, true)
  9077. Blockly.Python.definitions_['v831_card_recognition_init'] = `class LPR:
  9078. loc_model_path = "/root/preset/model/loc.mud"
  9079. reg_model_path = "/root/preset/model/reg.mud"
  9080. chars = ["皖", "沪", "津", "渝", "冀", "晋", "蒙", "辽", "吉", "黑",
  9081. "苏", "浙", "京", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤",
  9082. "桂", "琼", "川", "贵", "云", "藏", "陕", "甘", "青", "宁",
  9083. "新", "警", "学", "A", "B", "C", "D", "E", "F", "G",
  9084. "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S",
  9085. "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3",
  9086. "4", "5", "6", "7", "8", "9", "-"]
  9087. variances = [0.1, 0.2]
  9088. steps = [8, 16, 32]
  9089. min_sizes = [12, 24, 48, 96, 192, 320]
  9090. card_input_img = None
  9091. def __init__(self) -> None:
  9092. from maix import nn
  9093. self.loc_model = nn.load(self.loc_model_path, opt=None)
  9094. self.reg_model = nn.load(self.reg_model_path, opt=None)
  9095. from maix.nn import decoder
  9096. self.loc_decoder = decoder.license_plate_location(
  9097. [224, 224], self.steps, self.min_sizes, self.variances)
  9098. self.reg_decoder = decoder.CTC((1, 68, 18))
  9099. def __del__(self):
  9100. del self.loc_model
  9101. del self.loc_decoder
  9102. def cal_fps(self, start, end):
  9103. one_second = 1
  9104. one_flash = end - start
  9105. fps = one_second / one_flash
  9106. return fps
  9107. def draw_fps(self, img, fps):
  9108. img.draw_string(0, 0, "FPS :"+str(fps), scale=1,
  9109. color=(255, 0, 255), thickness=1)
  9110. def draw_string(self, img, x, y, string, color):
  9111. img.draw_string(x, y, string, color=color)
  9112. def draw_paste(self, src, dst):
  9113. src.paste(dst, 0, 0)
  9114. def draw_rectangle(self, img, box):
  9115. img.draw_rectangle(box[0], box[1], box[2], box[3],
  9116. color=(230, 230, 250), thickness=2)
  9117. def draw_point(self, img, landmark):
  9118. for i in range(4):
  9119. x = landmark[2 * i]
  9120. y = landmark[2 * i + 1]
  9121. img.draw_rectangle(x-2, y-2, x+2, y+2,
  9122. color=(193, 255, 193), thickness=-1)
  9123. def process(self, input):
  9124. global CARDDATABOEX
  9125. self.card_input_img = input
  9126. # retinaface decoder only support chw layout
  9127. loc_out = self.loc_model.forward(input, quantize=1, layout="chw")
  9128. boxes, landmarks = self.loc_decoder.run(loc_out, nms=0.2, score_thresh=0.7, outputs_shape=[
  9129. [1, 4, 2058], [1, 2, 2058], [1, 8, 2058]])
  9130. if len(boxes):
  9131. # print(boxes,landmarks)
  9132. for boxesi, box in enumerate(boxes):
  9133. boxes[boxesi].append(landmarks[boxesi])
  9134. CARDDATABOEX = boxes
  9135. def get_card_data(self, landmark):
  9136. # landmark = i[4][:6]
  9137. reg_in = self.card_input_img.crop_affine(landmark, 94, 24)
  9138. reg_out = self.reg_model.forward(reg_in, quantize=1, layout="chw")
  9139. LP_number = self.reg_decoder.run(reg_out)
  9140. string_LP = ""
  9141. for id in LP_number:
  9142. string_LP += self.chars[id]
  9143. return string_LP
  9144. LPRCARD = LPR()
  9145. `;
  9146. var code = '';
  9147. return code;
  9148. };
  9149. // 车牌识别
  9150. Blockly.Blocks['ai_model_card_recognition_load'] = {
  9151. init: function () {
  9152. this.appendDummyInput()
  9153. .appendField(Blockly.Msg.ai_models_load_title)
  9154. .appendField(Blockly.Msg.ai_models_init_card_model);
  9155. // this.appendDummyInput()
  9156. // .appendField(Blockly.Msg.ai_models_customized_init_setup_source)
  9157. // .appendField(new Blockly.FieldVariable("img_card_recognition"), "varitem");
  9158. this.setInputsInline(false);
  9159. this.setPreviousStatement(true, null);
  9160. this.setNextStatement(true, null);
  9161. this.setColour("#ee783a");
  9162. this.setTooltip(Blockly.Msg.ai_model_customized_init_setup_definition_TOOLTIP);
  9163. this.setHelpUrl("");
  9164. }
  9165. };
  9166. Blockly.Python['ai_model_card_recognition_load'] = function (block) {
  9167. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  9168. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  9169. Blockly.Python.addVariable('CARDDATABOEX', `CARDDATABOEX = ""`, true)
  9170. Blockly.Python.definitions_['v831_card_recognition_init'] = `class LPR:
  9171. loc_model_path = "/root/preset/model/loc.mud"
  9172. reg_model_path = "/root/preset/model/reg.mud"
  9173. chars = ["皖", "沪", "津", "渝", "冀", "晋", "蒙", "辽", "吉", "黑",
  9174. "苏", "浙", "京", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤",
  9175. "桂", "琼", "川", "贵", "云", "藏", "陕", "甘", "青", "宁",
  9176. "新", "警", "学", "A", "B", "C", "D", "E", "F", "G",
  9177. "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S",
  9178. "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3",
  9179. "4", "5", "6", "7", "8", "9", "-"]
  9180. variances = [0.1, 0.2]
  9181. steps = [8, 16, 32]
  9182. min_sizes = [12, 24, 48, 96, 192, 320]
  9183. card_input_img = None
  9184. def __init__(self) -> None:
  9185. self.loc_model = nn.load(self.loc_model_path, opt=None)
  9186. self.reg_model = nn.load(self.reg_model_path, opt=None)
  9187. self.loc_decoder = decoder.license_plate_location(
  9188. [224, 224], self.steps, self.min_sizes, self.variances)
  9189. self.reg_decoder = decoder.CTC((1, 68, 18))
  9190. self.LPRS = {
  9191. "LPR_START_X":[],
  9192. "LPR_START_Y":[],
  9193. "LPR_CENTER_X":[],
  9194. "LPR_CENTER_Y":[],
  9195. "LPR_WIDTH":[],
  9196. "LPR_HEIGHT":[],
  9197. "LPR_CONFIDENCE":[],
  9198. "LPR_RESULT":[],
  9199. }
  9200. def __del__(self):
  9201. del self.loc_model
  9202. del self.loc_decoder
  9203. def cal_fps(self, start, end):
  9204. one_second = 1
  9205. one_flash = end - start
  9206. fps = one_second / one_flash
  9207. return fps
  9208. def draw_fps(self, img, fps):
  9209. img.draw_string(0, 0, "FPS :"+str(fps), scale=1,
  9210. color=(255, 0, 255), thickness=1)
  9211. def draw_string(self, img, x, y, string, color):
  9212. img.draw_string(x, y, string, color=color)
  9213. def draw_paste(self, src, dst):
  9214. src.paste(dst, 0, 0)
  9215. def draw_rectangle(self, img, box):
  9216. img.draw_rectangle(box[0], box[1], box[2], box[3],
  9217. color=(230, 230, 250), thickness=2)
  9218. def draw_point(self, img, landmark):
  9219. for i in range(4):
  9220. x = landmark[2 * i]
  9221. y = landmark[2 * i + 1]
  9222. img.draw_rectangle(x-2, y-2, x+2, y+2,
  9223. color=(193, 255, 193), thickness=-1)
  9224. def process(self, input):
  9225. self.card_input_img = input
  9226. # retinaface decoder only support chw layout
  9227. loc_out = self.loc_model.forward(input, quantize=1, layout="chw")
  9228. boxes, landmarks = self.loc_decoder.run(loc_out, nms=0.2, score_thresh=0.7, outputs_shape=[[1, 4, 2058], [1, 2, 2058], [1, 8, 2058]])
  9229. self.LPRS = {
  9230. "LPR_START_X":[],
  9231. "LPR_START_Y":[],
  9232. "LPR_CENTER_X":[],
  9233. "LPR_CENTER_Y":[],
  9234. "LPR_WIDTH":[],
  9235. "LPR_HEIGHT":[],
  9236. "LPR_CONFIDENCE":[],
  9237. "LPR_RESULT":[],
  9238. }
  9239. for i,box in enumerate(boxes):
  9240. landmark = landmarks[i][:6]
  9241. reg_in = input.crop_affine(landmark , 94 , 24)
  9242. reg_out = self.reg_model.forward(reg_in , quantize=1, layout = "chw")
  9243. LP_number = self.reg_decoder.run(reg_out)
  9244. string_LP = ''
  9245. for id in LP_number:
  9246. string_LP += self.chars[id]
  9247. # string_LP += str(id)
  9248. input.draw_string(box[0]+5, box[1]+5, string_LP, color=(225,0,0))
  9249. input.draw_rectangle(box[0], box[1], box[2], box[3],color=(250,0,0), thickness=1)
  9250. boxes[i].append(landmarks[i])
  9251. return boxes
  9252. def get_card_data(self, landmark):
  9253. # landmark = i[4][:6]
  9254. reg_in = self.card_input_img.crop_affine(landmark, 94, 24)
  9255. reg_out = self.reg_model.forward(reg_in, quantize=1, layout="chw")
  9256. LP_number = self.reg_decoder.run(reg_out)
  9257. string_LP = ""
  9258. for id in LP_number:
  9259. string_LP += self.chars[id]
  9260. return string_LP
  9261. LPRCARD = LPR()
  9262. `;
  9263. // TODO: Assemble Python into code variable.
  9264. var code = `_canvas_x, _canvas_y = 48,8
  9265. canvas = canvas.crop(48,8,224,224)
  9266. BOXES = LPRCARD.process(canvas)
  9267. `;
  9268. return code;
  9269. };
  9270. // 车牌识别
  9271. Blockly.Blocks['ai_model_card_xy_new'] = {
  9272. init: function () {
  9273. this.appendDummyInput()
  9274. .appendField(Blockly.Msg.ai_models_card_attribute)
  9275. this.appendStatementInput("input")
  9276. .setCheck(null)
  9277. .appendField(Blockly.Msg.basic_motion_statement_exec);
  9278. this.updateShape_();
  9279. this.setMutator(new Blockly.Mutator(['ai_model_false_create_with_item']));
  9280. this.setColour("#ee783a");
  9281. this.setPreviousStatement(true, null);
  9282. this.setNextStatement(true, null);
  9283. this.setTooltip("");
  9284. this.setHelpUrl("");
  9285. this.itemCount_ = 0;
  9286. },
  9287. mutationToDom: function () {
  9288. if (!this.itemCount_)
  9289. return null;
  9290. var container = document.createElement('mutation');
  9291. this.itemCount_ && container.setAttribute('aielse', 1);
  9292. return container;
  9293. },
  9294. domToMutation: function (a) {
  9295. this.itemCount_ = parseInt(a.getAttribute('aielse'), 10);
  9296. this.updateShape_();
  9297. },
  9298. decompose: function (a) {
  9299. var b = a.newBlock('ai_model_false_create_with_container');
  9300. b.initSvg();
  9301. var c = b.nextConnection
  9302. this.itemCount_ && (a = a.newBlock("ai_model_false_create_with_item"),
  9303. a.initSvg(),
  9304. c.connect(a.previousConnection));
  9305. console.log("this.elseCount_", a)
  9306. return b
  9307. },
  9308. compose: function (a) {
  9309. var b = a.nextConnection.targetBlock();
  9310. this.itemCount_ = 0;
  9311. var e = null;
  9312. if (b && b.type === "ai_model_false_create_with_item") {
  9313. this.itemCount_++;
  9314. e = b.statementConnection_;
  9315. b = b.nextConnection && b.nextConnection.targetBlock()
  9316. }
  9317. this.updateShape_()
  9318. Blockly.Mutator.reconnect(e, this, "AIELSE")
  9319. },
  9320. saveConnections: function (a) {
  9321. a = a.nextConnection.targetBlock();
  9322. if (a && a.type === "ai_model_false_create_with_item") {
  9323. e = this.getInput("AIELSE");
  9324. a.statementConnection_ = e && e.connection.targetConnection;
  9325. a = a.nextConnection && a.nextConnection.targetBlock()
  9326. }
  9327. },
  9328. updateShape_: function () {
  9329. this.getInput("AIELSE") && this.removeInput("AIELSE");
  9330. console.log("this.itemCount_", this.itemCount_)
  9331. this.itemCount_ && this.appendStatementInput("AIELSE").appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSE)
  9332. },
  9333. };
  9334. Blockly.Python['ai_model_card_xy_new'] = function (block) {
  9335. var statements_input = Blockly.Python.statementToCode(block, 'input');
  9336. var statements_input1 = Blockly.Python.statementToCode(block, 'AIELSE');
  9337. let allBlocks = block.workspace.getAllBlocks();
  9338. let global = ""
  9339. try {
  9340. global = allBlocks[0].workspace.variableList.toString()
  9341. }
  9342. catch (e) {
  9343. console.log(e)
  9344. }
  9345. Blockly.Python.addFunction("cardStatements", `def cardStatements(BOXESI):
  9346. ${global != "" ? `global ${global}` : "pass"}
  9347. ${statements_input}
  9348. `)
  9349. var code = `if len(BOXES) > 0:
  9350. for BOXESI in BOXES:
  9351. cardStatements(BOXESI)
  9352. else:
  9353. ${statements_input1 != "" ? statements_input1 : " pass"}
  9354. `
  9355. return code;
  9356. };
  9357. Blockly.Blocks['ai_model_card_get'] = {
  9358. init: function () {
  9359. this.appendDummyInput()
  9360. .appendField(new Blockly.FieldImage("blockly/media/object_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  9361. this.appendDummyInput()
  9362. .appendField(Blockly.Msg.ai_models_from)
  9363. .appendField(Blockly.Msg.ai_models_card_model_get)
  9364. .appendField(new Blockly.FieldDropdown([
  9365. [Blockly.Msg.ai_models_init_card_result, "Object"],
  9366. [Blockly.Msg.ai_models_object_model_x, "X"],
  9367. [Blockly.Msg.ai_models_object_model_y, "Y"],
  9368. // [Blockly.Msg.ai_models_object_model_w, "W"],
  9369. // [Blockly.Msg.ai_models_object_model_h, "H"],
  9370. // [Blockly.Msg.ai_models_object_model_x_center, "CX"],
  9371. // [Blockly.Msg.ai_models_object_model_y_center, "CY"],
  9372. // [Blockly.Msg.ai_models_object_model_count, "Number"],
  9373. // [Blockly.Msg.ai_models_object_model_index, "Serial"],
  9374. // [Blockly.Msg.ai_models_object_model_confidence, "Confidence"],
  9375. [Blockly.Msg.ai_models_face_model_ws, "2"],
  9376. [Blockly.Msg.ai_models_face_model_hs, "3"],
  9377. [Blockly.Msg.ai_models_object_model_x_center, "CX"],
  9378. [Blockly.Msg.ai_models_object_model_y_center, "CY"]
  9379. ]), "TYPE");
  9380. this.setInputsInline(false);
  9381. this.setOutput(true, null);
  9382. this.setColour("#ee783a");
  9383. var thisBlock = this;
  9384. this.setTooltip(Blockly.Msg.ai_models_guesswork_model_get);
  9385. this.setHelpUrl("");
  9386. }
  9387. };
  9388. Blockly.Python.ai_model_card_get = function (block) {
  9389. var type = block.getFieldValue('TYPE');
  9390. var variable_name = "BOXESI";
  9391. var _code = "";
  9392. if (type == "Object") {
  9393. _code = `LPRCARD.get_card_data(${variable_name}[4][:6])`
  9394. // _code = `"{}:{:.2f}%".format(labels[probs[${variable_name}][0]], probs[${variable_name}][1][probs[${variable_name}][0]]*100)`
  9395. } else if (type == "X") {
  9396. _code = '' + variable_name + '[0]';
  9397. } else if (type == "Y") {
  9398. _code = '' + variable_name + '[1]';
  9399. } else if (type == "W") {
  9400. _code = 'int(' + variable_name + '[2])';
  9401. } else if (type == "H") {
  9402. _code = 'int(' + variable_name + '[3])';
  9403. } else if (type == "Confidence") {
  9404. _code = `${variable_name}[4][1][${variable_name}[4][0]]*100`;
  9405. } else if (type == "CX") {
  9406. _code = `int((${variable_name}[0] + ${variable_name}[2])/2)`;
  9407. } else if (type == "CY") {
  9408. _code = `int((${variable_name}[1] + ${variable_name}[3])/2)`;
  9409. }
  9410. else {
  9411. _code = `${variable_name}[${type}]-${variable_name}[${type - 2}]`;
  9412. }
  9413. // }
  9414. return [_code, Blockly.Python.ORDER_ATOMIC];
  9415. }
  9416. //
  9417. // 车牌识别
  9418. Blockly.Blocks['ai_model_card_xy'] = {
  9419. init: function () {
  9420. this.appendDummyInput()
  9421. .appendField(Blockly.Msg.ai_models_card_if_true);
  9422. this.setInputsInline(false);
  9423. this.setNextStatement(true, null);
  9424. this.setPreviousStatement(true, null);
  9425. this.setColour("#ee783a");
  9426. this.setTooltip(Blockly.Msg.ai_models_card_if_true);
  9427. this.setHelpUrl("");
  9428. }
  9429. };
  9430. Blockly.Python['ai_model_card_xy'] = function (block) {
  9431. Blockly.Python.addFunction("cardShowRecognize", `def cardShowRecognize():
  9432. global CARDDATABOEX
  9433. if len(CARDDATABOEX):
  9434. for CARDDATABOEXI in CARDDATABOEX:
  9435. canvas.draw_rectangle(CARDDATABOEXI[0],CARDDATABOEXI[1], CARDDATABOEXI[0]+CARDDATABOEXI[2]-CARDDATABOEXI[0],CARDDATABOEXI[1]+ CARDDATABOEXI[3]-CARDDATABOEXI[1], color=(255,0,0), thickness=1)
  9436. canvas.draw_string(CARDDATABOEXI[0],CARDDATABOEXI[1], (str(LPRCARD.get_card_data(CARDDATABOEXI[4][:6]))), scale = 1, color = (255,255,255) , thickness = 1)
  9437. `)
  9438. var code = 'cardShowRecognize()\n';
  9439. return code;
  9440. };
  9441. Blockly.Blocks['ai_model_card_result'] = {
  9442. init: function () {
  9443. this.appendDummyInput()
  9444. .appendField(Blockly.Msg.ai_models_card_attribute);
  9445. this.setInputsInline(false);
  9446. this.setOutput(true, null);
  9447. this.setColour("#ee783a");
  9448. this.setTooltip(Blockly.Msg.ai_models_card_attribute);
  9449. this.setHelpUrl("");
  9450. }
  9451. };
  9452. Blockly.Python['ai_model_card_result'] = function (block) {
  9453. var code = 'CARDDATABOEX';
  9454. return [code, Blockly.Python.ORDER_ATOMIC];
  9455. };
  9456. Blockly.Blocks['ai_model_face_recognition_init_new'] = {
  9457. init: function () {
  9458. this.appendDummyInput().appendField(Blockly.Msg.ai_models_init_title)
  9459. .appendField('人脸识别模型');
  9460. this.setPreviousStatement(true, null);
  9461. this.setNextStatement(true, null);
  9462. this.setColour("#ee783a");
  9463. this.setTooltip(Blockly.Msg.ai_models_init_title + Blockly.Msg.ai_models_init_face_model);
  9464. this.setHelpUrl("");
  9465. }
  9466. };
  9467. Blockly.Python.ai_model_face_recognition_init_new = function (block) {
  9468. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  9469. Blockly.Python.definitions_['v831_import_FaceRecognize'] = `from maix.nn.app.face import FaceRecognize`
  9470. Blockly.Python.definitions_.import_face_recognition_init_new = `
  9471. class Face_recognize:
  9472. score_threshold = 70 #识别分数阈值
  9473. input_size = (224, 224, 3) #输入图片尺寸
  9474. input_size_fe = (128, 128, 3) #输入人脸数据
  9475. feature_len = 256 #人脸数据宽度
  9476. steps = [8, 16, 32] #
  9477. channel_num = 0 #通道数量
  9478. users = [] #初始化用户列表
  9479. threshold = 0.5 #人脸阈值
  9480. nms = 0.3
  9481. max_face_num = 3 #输出的画面中的人脸的最大个数
  9482. names = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] #人脸标签定义
  9483. model = {
  9484. "param": "/root/preset/model/model_int8.param",
  9485. "bin": "/root/preset/model/model_int8.bin"
  9486. }
  9487. model_fe = {
  9488. "param": "/root/preset/model/fe_res18_117.param",
  9489. "bin": "/root/preset/model/fe_res18_117.bin"
  9490. }
  9491. def __init__(self):
  9492. for i in range(len(self.steps)):
  9493. self.channel_num += self.input_size[1] / self.steps[i] * (self.input_size[0] / self.steps[i]) * 2
  9494. self.channel_num = int(self.channel_num) #统计通道数量
  9495. self.options = { #准备人脸输出参数
  9496. "model_type": "awnn",
  9497. "inputs": {
  9498. "input0": self.input_size
  9499. },
  9500. "outputs": {
  9501. "output0": (1, 4, self.channel_num) ,
  9502. "431": (1, 2, self.channel_num) ,
  9503. "output2": (1, 10, self.channel_num)
  9504. },
  9505. "mean": [127.5, 127.5, 127.5],
  9506. "norm": [0.0078125, 0.0078125, 0.0078125],
  9507. }
  9508. self.options_fe = { #准备特征提取参数
  9509. "model_type": "awnn",
  9510. "inputs": {
  9511. "inputs_blob": self.input_size_fe
  9512. },
  9513. "outputs": {
  9514. "FC_blob": (1, 1, self.feature_len)
  9515. },
  9516. "mean": [127.5, 127.5, 127.5],
  9517. "norm": [0.0078125, 0.0078125, 0.0078125],
  9518. }
  9519. print("-- load model:", self.model)
  9520. self.m = nn.load(self.model, opt=self.options)
  9521. print("-- load ok")
  9522. print("-- load model:", self.model_fe)
  9523. self.m_fe = nn.load(self.model_fe, opt=self.options_fe)
  9524. print("-- load ok")
  9525. self.face_recognizer = FaceRecognize(self.m, self.m_fe, self.feature_len, self.input_size, self.threshold, self.nms, self.max_face_num)
  9526. def map_face(self,box,points): #将224*224空间的位置转换到240*240或320*240空间内
  9527. from maix import display # print(box,points)
  9528. if display.width() == display.height():
  9529. def tran(x):
  9530. return int(x/224*display.width())
  9531. box = list(map(tran, box))
  9532. def tran_p(p):
  9533. return list(map(tran, p))
  9534. points = list(map(tran_p, points))
  9535. else:
  9536. # 168x224(320x240) > 224x224(240x240) > 320x240
  9537. s = (224*display.height()/display.width()) # 168x224
  9538. w, h, c = display.width()/224, display.height()/224, 224/s
  9539. t, d = c*h, (224 - s) // 2 # d = 224 - s // 2 == 28
  9540. box[0], box[1], box[2], box[3] = int(box[0]*w), int((box[1]-28)*t), int(box[2]*w), int((box[3])*t)
  9541. def tran_p(p):
  9542. return [int(p[0]*w), int((p[1]-d)*t)] # 224 - 168 / 2 = 28 so 168 / (old_h - 28) = 240 / new_h
  9543. points = list(map(tran_p, points))
  9544. # print(box,points)
  9545. return box,points
  9546. def recognize(self, feature): #进行人脸匹配
  9547. def _compare(user): #定义映射函数
  9548. return self.face_recognizer.compare(user, feature) #推测匹配分数 score相关分数
  9549. face_score_l = list(map(_compare,self.users)) #映射特征数据在记录中的比对分数
  9550. return max(enumerate(face_score_l), key=lambda x: x[-1]) #提取出人脸分数最大值和最大值所在的位置
  9551. def __del__(self):
  9552. del self.face_recognizer
  9553. del self.m_fe
  9554. del self.m
  9555. face_recognizer = Face_recognize()
  9556. `;
  9557. // var degee = block.getFieldValue('DEGREE');
  9558. var _code = "";
  9559. // Blockly.Python.addVariable('task_facerecognition', 'task_facerecognition = None', true);
  9560. return [_code, Blockly.Python.ORDER_ATOMIC];
  9561. }
  9562. Blockly.Blocks['ai_model_face_recognition_load_new'] = {
  9563. init: function () {
  9564. this.appendDummyInput()
  9565. .appendField(Blockly.Msg.ai_models_load_title)
  9566. .appendField(Blockly.Msg.ai_models_init_face_model);
  9567. this.appendDummyInput()
  9568. .appendField(Blockly.Msg.ai_models_init_source)
  9569. .appendField(new Blockly.FieldVariable("img_facerecognition"), "varitem");
  9570. this.setPreviousStatement(true, null);
  9571. this.setNextStatement(true, null);
  9572. this.setColour("#ee783a");
  9573. var thisBlock = this;
  9574. this.setTooltip(function () {
  9575. var mode = thisBlock.getFieldValue('DEGREE');
  9576. var TOOLTIPS = {
  9577. 'Human Face Recognition': Blockly.Msg.ai_model_allload_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_face_model)
  9578. };
  9579. return TOOLTIPS[mode];
  9580. });
  9581. this.setHelpUrl('');
  9582. }
  9583. };
  9584. Blockly.Python.ai_model_face_recognition_load_new = function (block) {
  9585. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  9586. // var camera_variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('camera_varitem'), Blockly.Variables.NAME_TYPE);
  9587. // Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
  9588. // var degee = block.getFieldValue('DEGREE');
  9589. var _code = `facesRecognitionLoadNew = face_recognizer.face_recognizer.get_faces(AI_img.tobytes(),False) #提取人脸特征信息
  9590. `;
  9591. // Blockly.Python.addVariable('code_facerecognition', '', true);
  9592. // _code += "code_facerecognition = kpu.run_yolo2(task_facerecognition, " + variable_name + ")\n";
  9593. // _code += variable_name + ".ai_to_pix()\n";
  9594. return _code;
  9595. }
  9596. Blockly.Blocks['ai_model_xy_new'] = {
  9597. init: function () {
  9598. this.appendDummyInput()
  9599. .appendField(new Blockly.FieldImage("blockly/media/face_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  9600. this.appendDummyInput()
  9601. .appendField(Blockly.Msg.ai_models_from)
  9602. .appendField(new Blockly.FieldVariable("i"), "varitem")
  9603. .appendField(Blockly.Msg.ai_models_face_model_get)
  9604. .appendField(new Blockly.FieldDropdown([
  9605. [Blockly.Msg.ai_models_face_model_x, "X"],
  9606. [Blockly.Msg.ai_models_face_model_y, "Y"],
  9607. [Blockly.Msg.ai_models_face_model_w, "W"],
  9608. [Blockly.Msg.ai_models_face_model_h, "H"],
  9609. [Blockly.Msg.ai_models_face_model_x_center, "CX"],
  9610. [Blockly.Msg.ai_models_face_model_y_center, "CY"],
  9611. [Blockly.Msg.ai_models_face_model_count, "Number"],
  9612. [Blockly.Msg.ai_models_face_model_index, "Serial"],
  9613. [Blockly.Msg.ai_models_face_model_confidence, "Confidence"]
  9614. ]), "TYPE");
  9615. this.setInputsInline(false);
  9616. this.setOutput(true, null);
  9617. this.setColour("#ee783a");
  9618. var thisBlock = this;
  9619. this.setTooltip(function () {
  9620. var mode = thisBlock.getFieldValue('TYPE');
  9621. var TOOLTIPS = {
  9622. 'X': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x),
  9623. 'Y': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y),
  9624. 'W': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_w),
  9625. 'H': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_h),
  9626. 'CX': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x_center),
  9627. 'CY': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y_center),
  9628. 'Number': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_count),
  9629. 'Serial': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_index),
  9630. 'Confidence': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_confidence)
  9631. };
  9632. return TOOLTIPS[mode];
  9633. });
  9634. this.setHelpUrl("");
  9635. }
  9636. };
  9637. Blockly.Python.ai_model_xy_new = function (block) {
  9638. var type = block.getFieldValue('TYPE');
  9639. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  9640. var _code = "";
  9641. if (type == "X") {
  9642. _code = variable_name + "[0]";
  9643. } else if (type == "Y") {
  9644. _code = variable_name + "[1]";
  9645. } else if (type == "W") {
  9646. _code = variable_name + "[0] + " + variable_name + "[2]";
  9647. } else if (type == "H") {
  9648. _code = variable_name + "[1] + " + variable_name + "[3]";
  9649. }
  9650. // else if (type == "Number") {
  9651. // _code = variable_name + ".objnum()";
  9652. // } else if (type == "Serial") {
  9653. // _code = variable_name + ".index()";
  9654. // } else if (type == "Confidence") {
  9655. // _code = variable_name + ".value()";
  9656. // } else if (type == "CX") {
  9657. // _code = variable_name + ".x()+(" + variable_name + ".w()/2)";
  9658. // } else if (type == "CY") {
  9659. // _code = variable_name + ".y()+(" + variable_name + ".h()/2)";
  9660. // }
  9661. return [_code, Blockly.Python.ORDER_ATOMIC];
  9662. };
  9663. Blockly.Blocks['ai_model_face_attribute_new'] = {
  9664. init: function () {
  9665. this.appendDummyInput()
  9666. .appendField(Blockly.Msg.ai_model_face_attribute);
  9667. this.setOutput(true, null);
  9668. this.setColour("#ee783a");
  9669. this.setTooltip(Blockly.Msg.ai_model_face_attribute_TOOLTIP);
  9670. this.setHelpUrl("");
  9671. }
  9672. };
  9673. Blockly.Python['ai_model_face_attribute_new'] = function (block) {
  9674. var code = 'facesRecognitionLoadNew';
  9675. return [code, Blockly.Python.ORDER_NONE];
  9676. };
  9677. Blockly.Blocks['face_recognition_load'] = {
  9678. init: function () {
  9679. this.appendDummyInput()
  9680. .appendField(Blockly.Msg.ai_model_face_name);
  9681. this.appendDummyInput()
  9682. .appendField(Blockly.Msg.v831_face_recognition_load);
  9683. // this.appendDummyInput()
  9684. // .appendField(Blockly.Msg.ai_models_init_source)
  9685. // .appendField(new Blockly.FieldVariable("img_recognized_face"), "varitem");
  9686. this.appendValueInput("class_input")
  9687. .setCheck(null)
  9688. .appendField(Blockly.Msg.LISTS_CREATE_WITH_CONTAINER_TITLE_ADD + ":")
  9689. // .appendField(new Blockly.FieldVariable("people_name_list"), "people_name");
  9690. this.setInputsInline(false);
  9691. this.setPreviousStatement(true, null);
  9692. this.setNextStatement(true, null);
  9693. this.setColour("#ee783a");
  9694. this.setTooltip(Blockly.Msg.v831_face_recognition_load_title);
  9695. this.setHelpUrl("");
  9696. }
  9697. };
  9698. Blockly.Python['face_recognition_load'] = function (block) {
  9699. // var face_list = Blockly.Python.variableDB_.getName(block.getFieldValue('people_name'), Blockly.Variables.NAME_TYPE);
  9700. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  9701. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  9702. Blockly.Python.definitions_['v831_import_from_maix_nn_face'] = `from maix.nn.app import face`
  9703. Blockly.Python.definitions_['v831_import_from_maix_nn_FaceRecognize'] = `from maix.nn.app.face import FaceRecognize`
  9704. Blockly.Python.addVariable('NUMBERPERSON', 'NUMBERPERSON = 0', true);
  9705. Blockly.Python.definitions_.import_Face_Recognizer = `class Face_Recognizer:
  9706. max_face_num = 4
  9707. detect_threshold = 0.5
  9708. detect_nms = 0.3
  9709. FEATURES = []
  9710. def __init__(self, threshold = 0.5, nms = 0.3, max_face_num = 1):
  9711. model = "/root/preset/model/retinaface.mud"
  9712. model_fe = "/root/preset/model/fe_resnet.mud"
  9713. self.input_size = (224, 224, 3)
  9714. input_size_fe = (128, 128, 3)
  9715. self.feature_len = 256
  9716. self.features = []
  9717. m = nn.load(model)
  9718. m_fe = nn.load(model_fe)
  9719. self.isLoadRecoredFace = False
  9720. self.CLASSNAMEFACELIST = ${value_class_input}
  9721. self.recognizer = FaceRecognize(m, m_fe, self.feature_len, self.input_size, threshold, nms, max_face_num)
  9722. self.FACERECOGNIZES = {
  9723. }
  9724. self.FACESRECOGNITONRESULT = []
  9725. def get_faces(self, img, std_img = False):
  9726. faces = self.recognizer.get_faces(img, std_img)
  9727. return faces
  9728. def __len__(self):
  9729. return len(self.features)
  9730. def add_user(self, name, feature):
  9731. self.features.append([name, feature])
  9732. return True
  9733. def remove_user(self, name_del):
  9734. rm = None
  9735. for name, feature in self.features:
  9736. if name_del == name:
  9737. rm = [name, feature]
  9738. if rm:
  9739. self.features.remove(rm)
  9740. return True
  9741. return False
  9742. def recognize(self, feature):
  9743. max_score = 0
  9744. uid = -1
  9745. for i, user in enumerate(self.features):
  9746. score = self.recognizer.compare(user[1], feature)
  9747. if score > max_score:
  9748. max_score = score
  9749. uid = i
  9750. if uid >= 0:
  9751. return self.features[uid][0], max_score
  9752. return None, 0
  9753. def get_input_size(self):
  9754. return self.input_size
  9755. def get_feature_len(self):
  9756. return self.feature_len
  9757. def darw_info(self, img, box, points, disp_str, bg_color=(255, 0, 0, 255), font_color=(255, 255, 255, 255), font_size=32):
  9758. font_wh = image.get_string_size(disp_str)
  9759. for p in points:
  9760. img.draw_rectangle(p[0] - 1, p[1] -1, p[0] + 1, p[1] + 1, color=bg_color)
  9761. img.draw_rectangle(box[0], box[1], box[0] + box[2], box[1] + box[3], color=bg_color, thickness=2)
  9762. if disp_str:
  9763. img.draw_rectangle(box[0], box[1] - font_wh[1], box[0] + font_wh[0], box[1], color=bg_color, thickness = -1)
  9764. img.draw_string(box[0], box[1] - font_wh[1], disp_str, color=font_color)
  9765. def map_face(self,box,points,IMAGEFACESHOW):
  9766. # print(box,points)
  9767. for p in points:
  9768. IMAGEFACESHOW.draw_rectangle(p[0] - 1, p[1] -1, p[0] + 1, p[1] + 1, color=(255,200,255))
  9769. #return box,points
  9770. def darw_title(self, img, dis_size ,key_l = None, key_r =None):
  9771. if key_C:
  9772. key_l = "| "+ key_l
  9773. img.draw_string( 1, 2 ,key_l , scale = 1, color = (255, 255, 255), thickness = 2)
  9774. if key_D:
  9775. key_r = key_r+" |"
  9776. w = int(dis_size[0] - 4 - image.get_string_size(key_r)[0] * 1)
  9777. img.draw_string( w, 2 ,key_r , scale = 1, color = (255, 255, 255), thickness = 2)
  9778. def recognizeShowMessage(self, canvas):
  9779. self.FACESRECOGNITONRESULT = FACERECGNIZER.get_faces(canvas)
  9780. if len(self.FACESRECOGNITONRESULT):
  9781. for FACESRECOGNITONRESULTI in self.FACESRECOGNITONRESULT:
  9782. if self.isLoadRecoredFace:
  9783. if int(FACERECGNIZER.recognize(FACESRECOGNITONRESULTI[3])[1])>85:
  9784. canvas.draw_rectangle(FACESRECOGNITONRESULTI[1][0],FACESRECOGNITONRESULTI[1][1], FACESRECOGNITONRESULTI[1][0]+FACESRECOGNITONRESULTI[1][2],FACESRECOGNITONRESULTI[1][1]+ FACESRECOGNITONRESULTI[1][3], color=(51,204,0), thickness=1)
  9785. canvas.draw_string(40,0, ("".join([str(x) for x in [FACERECGNIZER.recognize(FACESRECOGNITONRESULTI[3])[0], " : ", round(FACERECGNIZER.recognize(FACESRECOGNITONRESULTI[3])[1], 2)]])), scale = 1, color = (51,204,0) , thickness = 1)
  9786. else:
  9787. canvas.draw_rectangle(FACESRECOGNITONRESULTI[1][0],FACESRECOGNITONRESULTI[1][1], FACESRECOGNITONRESULTI[1][0]+FACESRECOGNITONRESULTI[1][2],FACESRECOGNITONRESULTI[1][1]+ FACESRECOGNITONRESULTI[1][3], color=(204,0,0), thickness=1)
  9788. canvas.draw_string(40,0, ("".join([str(x) for x in [FACERECGNIZER.recognize(FACESRECOGNITONRESULTI[3])[0], " : ", round(FACERECGNIZER.recognize(FACESRECOGNITONRESULTI[3])[1], 2)]])), scale = 1, color = (204,0,0) , thickness = 1)
  9789. else:
  9790. FACERECGNIZER.map_face(FACESRECOGNITONRESULTI[1],FACESRECOGNITONRESULTI[2],canvas)
  9791. canvas.draw_rectangle(FACESRECOGNITONRESULTI[1][0],FACESRECOGNITONRESULTI[1][1], FACESRECOGNITONRESULTI[1][0]+FACESRECOGNITONRESULTI[1][2],FACESRECOGNITONRESULTI[1][1]+ FACESRECOGNITONRESULTI[1][3], color=(255,0,0), thickness=1)
  9792. FACERECGNIZER = Face_Recognizer(0.5, 0.3, max_face_num = 4)
  9793. `
  9794. var code = `_canvas_x, _canvas_y = 48,8
  9795. canvas = canvas.crop(48, 8,224, 224)
  9796. FACERECGNIZER.recognizeShowMessage(canvas)
  9797. `;
  9798. return code;
  9799. };
  9800. Blockly.Blocks['face_recognition_if_face_new'] = {
  9801. init: function () {
  9802. this.appendDummyInput()
  9803. .appendField(Blockly.Msg.ai_model_face_attribute_new)
  9804. this.appendStatementInput("input")
  9805. .setCheck(null)
  9806. .appendField(Blockly.Msg.basic_motion_statement_exec);
  9807. this.updateShape_();
  9808. this.setMutator(new Blockly.Mutator(['ai_model_false_create_with_item']));
  9809. this.setColour("#ee783a");
  9810. this.setPreviousStatement(true, null);
  9811. this.setNextStatement(true, null);
  9812. this.setTooltip("");
  9813. this.setHelpUrl("");
  9814. this.itemCount_ = 0;
  9815. },
  9816. mutationToDom: function () {
  9817. if (!this.itemCount_)
  9818. return null;
  9819. var container = document.createElement('mutation');
  9820. this.itemCount_ && container.setAttribute('aielse', 1);
  9821. return container;
  9822. },
  9823. domToMutation: function (a) {
  9824. this.itemCount_ = parseInt(a.getAttribute('aielse'), 10);
  9825. this.updateShape_();
  9826. },
  9827. decompose: function (a) {
  9828. var b = a.newBlock('ai_model_false_create_with_container');
  9829. b.initSvg();
  9830. var c = b.nextConnection
  9831. this.itemCount_ && (a = a.newBlock("ai_model_false_create_with_item"),
  9832. a.initSvg(),
  9833. c.connect(a.previousConnection));
  9834. console.log("this.elseCount_",a)
  9835. return b
  9836. },
  9837. compose: function (a) {
  9838. var b = a.nextConnection.targetBlock();
  9839. this.itemCount_ = 0;
  9840. var e = null;
  9841. if (b && b.type === "ai_model_false_create_with_item") {
  9842. this.itemCount_++;
  9843. e = b.statementConnection_;
  9844. b = b.nextConnection && b.nextConnection.targetBlock()
  9845. }
  9846. this.updateShape_()
  9847. Blockly.Mutator.reconnect(e, this, "AIELSE")
  9848. },
  9849. saveConnections: function (a) {
  9850. a = a.nextConnection.targetBlock();
  9851. if (a && a.type === "ai_model_false_create_with_item") {
  9852. e = this.getInput("AIELSE");
  9853. a.statementConnection_ = e && e.connection.targetConnection;
  9854. a = a.nextConnection && a.nextConnection.targetBlock()
  9855. }
  9856. },
  9857. updateShape_: function () {
  9858. this.getInput("AIELSE") && this.removeInput("AIELSE");
  9859. console.log("this.itemCount_",this.itemCount_)
  9860. this.itemCount_ && this.appendStatementInput("AIELSE").appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSE)
  9861. },
  9862. };
  9863. Blockly.Python['face_recognition_if_face_new'] = function (block) {
  9864. var statements_input = Blockly.Python.statementToCode(block, 'input');
  9865. var statements_input1 = Blockly.Python.statementToCode(block, 'AIELSE');
  9866. let allBlocks = block.workspace.getAllBlocks();
  9867. let global = ""
  9868. try {
  9869. global = allBlocks[0].workspace.variableList.toString()
  9870. }
  9871. catch (e) {
  9872. console.log(e)
  9873. }
  9874. Blockly.Python.addFunction("recognitionStatements", `def recognitionStatements(FACESRECOGNITONRESULTI):
  9875. ${global != "" ? `global ${global}` : "pass"}
  9876. ${statements_input}
  9877. `)
  9878. var code = `if len(FACERECGNIZER.FACESRECOGNITONRESULT) > 0:
  9879. for BOXESI in FACERECGNIZER.FACESRECOGNITONRESULT:
  9880. recognitionStatements(BOXESI)
  9881. else:
  9882. ${statements_input1 != "" ? statements_input1 : " pass"}
  9883. `
  9884. return code;
  9885. };
  9886. Blockly.Blocks['face_recognition_if_face'] = {
  9887. init: function () {
  9888. this.appendDummyInput()
  9889. .appendField(Blockly.Msg.ai_models_face_model_if_new);
  9890. this.setInputsInline(false);
  9891. this.setNextStatement(true, null);
  9892. this.setPreviousStatement(true, null);
  9893. this.setColour("#ee783a");
  9894. this.setTooltip(Blockly.Msg.ai_models_face_model_if_new);
  9895. this.setHelpUrl("");
  9896. }
  9897. };
  9898. Blockly.Python['face_recognition_if_face'] = function (block) {
  9899. var code = ``;
  9900. return code;
  9901. };
  9902. Blockly.Blocks['face_recognition_face_result'] = {
  9903. init: function () {
  9904. this.appendDummyInput()
  9905. .appendField(Blockly.Msg.ai_model_face_attribute_new);
  9906. this.setInputsInline(false);
  9907. this.setOutput(true, null);
  9908. this.setColour("#ee783a");
  9909. this.setTooltip(Blockly.Msg.ai_model_face_attribute_new);
  9910. this.setHelpUrl("");
  9911. }
  9912. };
  9913. Blockly.Python['face_recognition_face_result'] = function (block) {
  9914. var code = `FACESRECOGNITONRESULT`;
  9915. return [code, Blockly.Python.ORDER_ATOMIC];
  9916. };
  9917. Blockly.Blocks['face_recognition_detection_rectangle'] = {
  9918. init: function () {
  9919. this.appendDummyInput()
  9920. .appendField(Blockly.Msg.ai_models_face_model_get)
  9921. .appendField(new Blockly.FieldDropdown([
  9922. // [Blockly.Msg.face_information, "rect"],
  9923. [Blockly.Msg.ai_models_face_model_x, "X"],
  9924. [Blockly.Msg.ai_models_face_model_y, "Y"],
  9925. [Blockly.Msg.ai_models_face_model_confidence_1, "Confidence"],
  9926. [Blockly.Msg.ai_models_face_model_ws, "2"],
  9927. [Blockly.Msg.ai_models_face_model_hs, "3"],
  9928. [Blockly.Msg.ai_models_object_model_x_center, "CX"],
  9929. [Blockly.Msg.ai_models_object_model_y_center, "CY"],
  9930. [Blockly.Msg.v831_model_face_name, "0"],
  9931. [Blockly.Msg.v831_models_face_model_confidence, "1"],
  9932. ]), "TYPE");
  9933. this.setInputsInline(false);
  9934. this.setOutput(true, null);
  9935. this.setColour("#ee783a");
  9936. var thisBlock = this;
  9937. this.setTooltip(function () {
  9938. var mode = thisBlock.getFieldValue('TYPE');
  9939. var TOOLTIPS = {
  9940. 'rect': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_rect),
  9941. 'X': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x),
  9942. 'Y': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y),
  9943. 'W': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_w),
  9944. 'H': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_h),
  9945. 'CX': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x_center),
  9946. 'CY': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y_center),
  9947. 'Number': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_count),
  9948. 'Serial': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_index),
  9949. 'Confidence': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_confidence)
  9950. };
  9951. return TOOLTIPS[mode];
  9952. });
  9953. this.setHelpUrl("");
  9954. }
  9955. };
  9956. Blockly.Python.face_recognition_detection_rectangle = function (block) {
  9957. var type = block.getFieldValue('TYPE');
  9958. var _code = "";
  9959. if (type == "rect") {
  9960. _code = "FACESRECOGNITONRESULTI[3]";
  9961. } else if (type == "X") {
  9962. _code = "FACESRECOGNITONRESULTI[1][0]";
  9963. } else if (type == "Y") {
  9964. _code = "FACESRECOGNITONRESULTI[1][1]";
  9965. } else if (type == "W") {
  9966. _code = `int(FACESRECOGNITONRESULTI[1][2]+FACESRECOGNITONRESULTI[1][0])`;
  9967. } else if (type == "H") {
  9968. _code = `int(FACESRECOGNITONRESULTI[1][3]+FACESRECOGNITONRESULTI[1][1])`;
  9969. } else if (type == "Number") {
  9970. _code = "FACESRECOGNITONRESULTI.objnum()";
  9971. } else if (type == "Serial") {
  9972. _code = "FACESRECOGNITONRESULTI.index()";
  9973. } else if (type == "Confidence") {
  9974. _code = "FACESRECOGNITONRESULTI[0]";
  9975. } else if (type == "CX") {
  9976. _code = `int((FACESRECOGNITONRESULTI[1][2]+FACESRECOGNITONRESULTI[1][0]+FACESRECOGNITONRESULTI[1][0])/2)`;
  9977. } else if (type == "CY") {
  9978. _code = `int((FACESRECOGNITONRESULTI[1][3]+FACESRECOGNITONRESULTI[1][1]+FACESRECOGNITONRESULTI[1][1])/2)`;
  9979. } else if (type == "0" || type == "1") {
  9980. _code = `FACERECGNIZER.recognize(FACESRECOGNITONRESULTI[3])[${type}]`
  9981. }
  9982. else {
  9983. _code = `FACESRECOGNITONRESULTI[1][${type}]`;
  9984. }
  9985. return [_code, Blockly.Python.ORDER_ATOMIC];
  9986. };
  9987. Blockly.Blocks['face_recognition_map_face'] = {
  9988. init: function () {
  9989. this.appendDummyInput()
  9990. .appendField(Blockly.Msg.face_recognition_map_face_canvas)
  9991. .appendField(new Blockly.FieldVariable("img_recognized_face"), "varitem")
  9992. .appendField(Blockly.Msg.face_recognition_map_face_show)
  9993. .appendField(Blockly.Msg.face_recognition_map_face_key_point);
  9994. // this.setInputsInline(false);
  9995. this.setPreviousStatement(true, null);
  9996. this.setNextStatement(true, null);
  9997. this.setColour("#ee783a");
  9998. this.setTooltip(Blockly.Msg.v831_setting_face_data_save_title);
  9999. this.setHelpUrl("");
  10000. }
  10001. };
  10002. Blockly.Python['face_recognition_map_face'] = function (block) {
  10003. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  10004. // var variable1 = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem1'), Blockly.Variables.NAME_TYPE);
  10005. var code = `FACERECGNIZER.map_face(FACESRECOGNITONRESULTI[1],FACESRECOGNITONRESULTI[2],${variable_name})
  10006. `;
  10007. return code;
  10008. };
  10009. Blockly.Blocks['face_recognition_add'] = {
  10010. init: function () {
  10011. this.appendDummyInput()
  10012. .appendField(Blockly.Msg.CocoRobo_EVENT_ACTION_OPT1)
  10013. .appendField(new Blockly.FieldDropdown([
  10014. ["A", "A"],
  10015. ["B", "B"],
  10016. ["C", "C"],
  10017. ["D", "D"]
  10018. ]), "button_type")
  10019. .appendField(Blockly.Msg.ai_model_face_gather_1);
  10020. this.setInputsInline(false);
  10021. this.setPreviousStatement(true, null);
  10022. this.setNextStatement(true, null);
  10023. this.setColour("#ee783a");
  10024. this.setTooltip(Blockly.Msg.v831_setting_face_data_save_title);
  10025. this.setHelpUrl("");
  10026. }
  10027. };
  10028. Blockly.Python['face_recognition_add'] = function (block) {
  10029. var _type = block.getFieldValue('button_type');
  10030. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  10031. sys.path.append("/root/")`
  10032. Blockly.Python.definitions_['v831_import_CocoPi_BUTTON'] = `from CocoPi import BUTTON`
  10033. // Blockly.Python.definitions_['v831_import_CocoPi'] = `sys.path.append("/root/")`
  10034. Blockly.Python.definitions_['import_define_x_v831_button'] = `key_A = BUTTON(14)
  10035. key_B = BUTTON(8)
  10036. key_C = BUTTON(13)
  10037. key_D = BUTTON(7)
  10038. `
  10039. // var code = `key_${_type}.is_pressed()`
  10040. Blockly.Python.definitions_['v831_import_os'] = `import os`
  10041. Blockly.Python.definitions_['v831_import_json'] = `import json`
  10042. Blockly.Python.addVariable('ai_model_face_gather', `
  10043. def _CREATE_TEXT_FILE_WITH_CONTENT(_path, _data, _sep):
  10044. f = open(_path, "a")
  10045. f.write(_data + _sep)
  10046. f.close()
  10047. `, true);
  10048. var language = localStorage.getItem("handPyLanguage");
  10049. let saveFace = ""
  10050. if (language == "en") {
  10051. saveFace = "Save Face"
  10052. } else if (language == "zh-hant") {
  10053. saveFace = "儲存人臉"
  10054. } else {
  10055. saveFace = "保存人脸"
  10056. }
  10057. var code = `if key_${_type}.is_pressed():
  10058. while not (key_${_type}.is_pressed() == False):
  10059. time.sleep(0.1)
  10060. if NUMBERPERSON < len(FACERECGNIZER.CLASSNAMEFACELIST):
  10061. NUMBERPERSON = NUMBERPERSON + 1
  10062. FACERECGNIZER.add_user(FACERECGNIZER.CLASSNAMEFACELIST[len(FACERECGNIZER)], FACESRECOGNITONRESULTI[3])
  10063. FEATURES = FACERECGNIZER.features
  10064. try:
  10065. os.remove("/root/user/model/recorded_face_features.py")
  10066. except:
  10067. pass
  10068. try:
  10069. _CREATE_TEXT_FILE_WITH_CONTENT("/root/user/model/recorded_face_features.py", json.dumps(FACERECGNIZER.features), "\\r\\n")
  10070. except:
  10071. pass
  10072. canvas.draw_string(0,0, str("${saveFace}") + str(NUMBERPERSON), scale = 1, color = (51,204,0) , thickness = 1)
  10073. v831_display_show_canvas(canvas,_canvas_x,_canvas_y)
  10074. time.sleep(1000 / 1000)
  10075. `;
  10076. return code;
  10077. };
  10078. Blockly.Blocks['face_recognition_remove'] = {
  10079. init: function () {
  10080. this.appendDummyInput()
  10081. .appendField(Blockly.Msg.CocoRobo_EVENT_ACTION_OPT1)
  10082. .appendField(new Blockly.FieldDropdown([
  10083. ["A", "A"],
  10084. ["B", "B"],
  10085. ["C", "C"],
  10086. ["D", "D"]
  10087. ]), "button_type")
  10088. .appendField(Blockly.Msg.v831_setting_face_data_remove)
  10089. // this.appendStatementInput("input")
  10090. // .setCheck(null)
  10091. // .appendField(Blockly.Msg.basic_motion_statement_exec);
  10092. // .appendField(new Blockly.FieldVariable("save_face_data"), "varitem");
  10093. this.setInputsInline(false);
  10094. this.setPreviousStatement(true, null);
  10095. this.setNextStatement(true, null);
  10096. this.setColour("#ee783a");
  10097. this.setTooltip(Blockly.Msg.v831_setting_face_data_remove_title);
  10098. this.setHelpUrl("");
  10099. }
  10100. };
  10101. Blockly.Python['face_recognition_remove'] = function (block) {
  10102. var _type = block.getFieldValue('button_type');
  10103. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  10104. sys.path.append("/root/")`
  10105. Blockly.Python.definitions_['v831_import_CocoPi_BUTTON'] = `from CocoPi import BUTTON`
  10106. // Blockly.Python.definitions_['v831_import_CocoPi'] = `sys.path.append("/root/")`
  10107. Blockly.Python.definitions_['import_define_x_v831_button'] = `key_A = BUTTON(14)
  10108. key_B = BUTTON(8)
  10109. key_C = BUTTON(13)
  10110. key_D = BUTTON(7)
  10111. `
  10112. let language = localStorage.getItem("handPyLanguage");
  10113. let removeFace = ""
  10114. let faceData = ""
  10115. if (language == "en") {
  10116. removeFace = "Remove Face"
  10117. faceData = "Data"
  10118. } else if (language == "zh-hant") {
  10119. removeFace = "刪除人臉"
  10120. faceData = "數據"
  10121. } else {
  10122. removeFace = "删除人脸"
  10123. faceData = "数据"
  10124. }
  10125. var code = `if key_${_type}.is_pressed() and NUMBERPERSON > 0:
  10126. while not (key_${_type}.is_pressed() == False):
  10127. time.sleep(0.1)
  10128. canvas.draw_rectangle(0,0, 0+320,0+ 20, color=(0,0,0), thickness=-1)
  10129. canvas.draw_string(40,0, ("".join([str(x) for x in ["${removeFace}", NUMBERPERSON, "${faceData}"]])), scale = 1, color = (255,255,255) , thickness = 1)
  10130. v831_display_show_canvas(canvas,_canvas_x,_canvas_y)
  10131. time.sleep(1000 / 1000)
  10132. NUMBERPERSON = NUMBERPERSON - 1
  10133. FACERECGNIZER.remove_user(FACERECGNIZER.CLASSNAMEFACELIST[len(FACERECGNIZER)-1])
  10134. FEATURES = FACERECGNIZER.features
  10135. `;
  10136. return code;
  10137. };
  10138. Blockly.Blocks['face_recognition_data_load'] = {
  10139. init: function () {
  10140. this.appendDummyInput()
  10141. .appendField(Blockly.Msg.ai_model_face_data_load);
  10142. this.setPreviousStatement(true, null);
  10143. this.setNextStatement(true, null);
  10144. this.setColour("#ee783a");
  10145. this.setTooltip(Blockly.Msg.ai_model_face_data_load);
  10146. this.setHelpUrl("");
  10147. }
  10148. };
  10149. Blockly.Python['face_recognition_data_load'] = function (block) {
  10150. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  10151. Blockly.Python.definitions_['v831_import_json'] = `import json`
  10152. Blockly.Python.definitions_['load_face_data'] = `isLoadRecoredFace = False
  10153. try:
  10154. with open("/root/user/model/recorded_face_features.py", "r") as file:
  10155. FACERECGNIZER.features = json.loads(file.read())
  10156. FACERECGNIZER.isLoadRecoredFace = True
  10157. except:
  10158. FACERECGNIZER.isLoadRecoredFace = False
  10159. `
  10160. var code = ``;
  10161. return code;
  10162. };
  10163. Blockly.Blocks['face_recognition_data_results'] = {
  10164. init: function () {
  10165. this.appendDummyInput()
  10166. .appendField(Blockly.Msg.ai_models_from)
  10167. .appendField(new Blockly.FieldVariable("img_face_Recognizer"), "varitem")
  10168. .appendField(Blockly.Msg.WIFIEASYMODE_COCOCLOUD_TYPE)
  10169. .appendField(new Blockly.FieldDropdown([
  10170. [Blockly.Msg.v831_model_face_name, "0"],
  10171. [Blockly.Msg.ai_models_face_model_confidence, "1"],
  10172. ]), "TYPE");
  10173. this.setInputsInline(false);
  10174. this.setOutput(true, null);
  10175. this.setColour("#ee783a");
  10176. this.setTooltip(Blockly.Msg.v831_face_recognition_result_title);
  10177. this.setHelpUrl("");
  10178. }
  10179. };
  10180. Blockly.Python['face_recognition_data_results'] = function (block) {
  10181. var type = block.getFieldValue('TYPE');
  10182. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  10183. var code = `FACERECGNIZER.recognize(${variable_name})[${type}]
  10184. `;
  10185. return [code, Blockly.Python.ORDER_ATOMIC];
  10186. };
  10187. // 自学习
  10188. Blockly.Blocks['ai_model_self_learning_init'] = {
  10189. init: function () {
  10190. this.appendDummyInput()
  10191. .appendField(Blockly.Msg.ai_model_self_learning_init);
  10192. this.appendValueInput("class_input")
  10193. .setCheck(null)
  10194. .appendField(Blockly.Msg.ai_models_customized_init_setup_name);
  10195. this.setInputsInline(false);
  10196. this.setPreviousStatement(true, null);
  10197. this.setNextStatement(true, null);
  10198. this.setColour("#ee783a");
  10199. this.setTooltip(Blockly.Msg.ai_model_self_learning_init);
  10200. this.setHelpUrl("");
  10201. }
  10202. };
  10203. Blockly.Python['ai_model_self_learning_init'] = function (block) {
  10204. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  10205. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  10206. Blockly.Python.definitions_.import_self_learning_init = `
  10207. CLASSNUM = ${JSON.parse(value_class_input).length}
  10208. SAMPLENUM = ${JSON.parse(value_class_input).length * 5}
  10209. CLASSNAMELIST = ${value_class_input}
  10210. `
  10211. var code = ``;
  10212. return code;
  10213. };
  10214. Blockly.Blocks['ai_model_self_learning_load'] = {
  10215. init: function () {
  10216. this.appendDummyInput()
  10217. .appendField(Blockly.Msg.ai_model_self_learning_load);
  10218. this.appendValueInput("class_input")
  10219. .setCheck(null)
  10220. .appendField(Blockly.Msg.ai_models_customized_init_setup_name);
  10221. this.setInputsInline(false);
  10222. this.setPreviousStatement(true, null);
  10223. this.setNextStatement(true, null);
  10224. this.setColour("#ee783a");
  10225. this.setTooltip(Blockly.Msg.ai_model_self_learning_load);
  10226. this.setHelpUrl("");
  10227. }
  10228. };
  10229. Blockly.Python['ai_model_self_learning_load'] = function (block) {
  10230. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  10231. Blockly.Python.definitions_['v831_import_Classifier'] = `from maix.nn.app.classifier import Classifier`
  10232. Blockly.Python.addVariable("SELFLEARNCOUNT", `SELFLEARNCOUNT = 0`, true)
  10233. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  10234. Blockly.Python.definitions_.import_self_learning_init = `
  10235. CLASSNUM = ${JSON.parse(value_class_input).length}
  10236. SAMPLENUM = ${JSON.parse(value_class_input).length * 5}
  10237. CLASSNAMELIST = ${value_class_input}
  10238. `
  10239. Blockly.Python.definitions_.self_learing_load = `class Self_learn:
  10240. model = {
  10241. "param": "/root/preset/model/resnet18_1000_awnn.param",
  10242. "bin": "/root/preset/model/resnet18_1000_awnn.bin"
  10243. }
  10244. options = {
  10245. "model_type": "awnn",
  10246. "inputs": {
  10247. "input0": (224, 224, 3)
  10248. },
  10249. "outputs": {
  10250. "190": (1, 1, 512)
  10251. },
  10252. "mean": [127.5, 127.5, 127.5],
  10253. "norm": [0.0176, 0.0176, 0.0176],
  10254. }
  10255. class_num = CLASSNUM #学习类别
  10256. sample_num = SAMPLENUM #学习类别总数量
  10257. curr_class = 0
  10258. curr_sample = 0
  10259. def __init__(self):
  10260. self.m = nn.load(self.model, opt=self.options)
  10261. self.classifier = Classifier(self.m, self.class_num, self.sample_num, 512, 224, 224)
  10262. SELFLEARN = Self_learn()
  10263. `
  10264. var code = '';
  10265. return code;
  10266. };
  10267. Blockly.Blocks['ai_model_self_learning_recognize_load'] = {
  10268. init: function () {
  10269. this.appendDummyInput()
  10270. .appendField(Blockly.Msg.ai_model_self_learning_recognize_load);
  10271. this.appendValueInput("class_input")
  10272. .setCheck(null)
  10273. .appendField(Blockly.Msg.ai_models_customized_init_setup_name);
  10274. this.setInputsInline(false);
  10275. this.setPreviousStatement(true, null);
  10276. this.setNextStatement(true, null);
  10277. this.setColour("#ee783a");
  10278. this.setTooltip(Blockly.Msg.ai_model_self_learning_recognize_load);
  10279. this.setHelpUrl("");
  10280. }
  10281. };
  10282. Blockly.Python['ai_model_self_learning_recognize_load'] = function (block) {
  10283. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  10284. Blockly.Python.definitions_['v831_import_Classifier'] = `from maix.nn.app.classifier import Classifier`
  10285. Blockly.Python.definitions_['v831_from_nn_import_load'] = `from maix.nn.app.classifier import load`
  10286. Blockly.Python.definitions_['v831_import_os'] = `import os`
  10287. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  10288. Blockly.Python.addVariable("SELFLEARNCOUNT", `SELFLEARNCOUNT = 0`, true)
  10289. Blockly.Python.definitions_.import_self_learning_init = `
  10290. CLASSNUM = ${JSON.parse(value_class_input).length}
  10291. SAMPLENUM = ${JSON.parse(value_class_input).length * 5}
  10292. CLASSNAMELIST = ${value_class_input}
  10293. `
  10294. Blockly.Python.definitions_.self_learing_load = `class Self_learn:
  10295. model = {
  10296. "param": "/root/preset/model/resnet18_1000_awnn.param",
  10297. "bin": "/root/preset/model/resnet18_1000_awnn.bin"
  10298. }
  10299. options = {
  10300. "model_type": "awnn",
  10301. "inputs": {
  10302. "input0": (224, 224, 3)
  10303. },
  10304. "outputs": {
  10305. "190": (1, 1, 512)
  10306. },
  10307. "mean": [127.5, 127.5, 127.5],
  10308. "norm": [0.0176, 0.0176, 0.0176],
  10309. }
  10310. class_num = CLASSNUM #学习类别
  10311. sample_num = SAMPLENUM #学习类别总数量
  10312. curr_class = 0
  10313. curr_sample = 0
  10314. def __init__(self):
  10315. if os.path.isfile("/root/module.bin"):
  10316. self.m = nn.load(self.model, opt=self.options)
  10317. self.classifier = load(self.m,"/root/module.bin")
  10318. else:
  10319. self.m = nn.load(self.model, opt=self.options)
  10320. self.classifier = Classifier(self.m, self.class_num, self.sample_num, 512, 224, 224)
  10321. print("not have model!")
  10322. SELFLEARN = Self_learn()
  10323. `
  10324. var code = '';
  10325. return code;
  10326. };
  10327. Blockly.Blocks['ai_model_self_learning_add_class'] = {
  10328. init: function () {
  10329. this.appendDummyInput()
  10330. .appendField(Blockly.Msg.CocoRobo_EVENT_ACTION_OPT1)
  10331. .appendField(new Blockly.FieldDropdown([
  10332. ["A", "A"],
  10333. ["B", "B"],
  10334. ["C", "C"],
  10335. ["D", "D"]
  10336. ]), "button_type")
  10337. .appendField(Blockly.Msg.ai_model_self_learning_add_class_category + Blockly.Msg.ai_model_self_learning_make_data_set);
  10338. this.setInputsInline(false);
  10339. this.setPreviousStatement(true, null);
  10340. this.setNextStatement(true, null);
  10341. this.setColour("#ee783a");
  10342. this.setTooltip(Blockly.Msg.ai_model_self_learning_add_class_category + Blockly.Msg.ai_model_self_learning_make_data_set);
  10343. this.setHelpUrl("");
  10344. }
  10345. };
  10346. Blockly.Python['ai_model_self_learning_add_class'] = function (block) {
  10347. var _type = block.getFieldValue('button_type');
  10348. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  10349. sys.path.append("/root/")`
  10350. Blockly.Python.definitions_['v831_import_CocoPi_BUTTON'] = `from CocoPi import BUTTON`
  10351. // Blockly.Python.definitions_['v831_import_CocoPi'] = `sys.path.append("/root/")`
  10352. Blockly.Python.definitions_['import_define_x_v831_button'] = `key_A = BUTTON(14)
  10353. key_B = BUTTON(8)
  10354. key_C = BUTTON(13)
  10355. key_D = BUTTON(7)
  10356. `
  10357. var code = `_canvas_x, _canvas_y = 48,8
  10358. canvas = canvas.crop(48, 8, 224, 224)
  10359. if key_${_type}.is_pressed() and SELFLEARNCOUNT < SELFLEARN.class_num:
  10360. while not (key_${_type}.is_pressed() == False):
  10361. time.sleep(0.1)
  10362. SELFLEARN.classifier.add_class_img(canvas)
  10363. for i in range(5):
  10364. SELFLEARN.classifier.add_sample_img(canvas)
  10365. SELFLEARNCOUNT = SELFLEARNCOUNT + 1
  10366. `;
  10367. return code;
  10368. };
  10369. Blockly.Blocks['ai_model_self_learning_make_data_set'] = {
  10370. init: function () {
  10371. this.appendDummyInput()
  10372. .appendField(Blockly.Msg.ai_model_self_learning_make_data_set);
  10373. this.setInputsInline(false);
  10374. this.setPreviousStatement(true, null);
  10375. this.setNextStatement(true, null);
  10376. this.setColour("#ee783a");
  10377. this.setTooltip(Blockly.Msg.ai_model_self_learning_make_data_set);
  10378. this.setHelpUrl("");
  10379. }
  10380. };
  10381. Blockly.Python['ai_model_self_learning_make_data_set'] = function (block) {
  10382. var code = `for i in range(5):
  10383. SELFLEARN.classifier.add_sample_img(canvas)
  10384. `;
  10385. return code;
  10386. };
  10387. Blockly.Blocks['ai_model_self_learning_picture'] = {
  10388. init: function () {
  10389. this.appendDummyInput()
  10390. .appendField(Blockly.Msg.CocoRobo_EVENT_ACTION_OPT1)
  10391. .appendField(new Blockly.FieldDropdown([
  10392. ["A", "A"],
  10393. ["B", "B"],
  10394. ["C", "C"],
  10395. ["D", "D"]
  10396. ]), "button_type")
  10397. .appendField(Blockly.Msg.ai_model_self_learning_picture + Blockly.Msg.ai_model_self_learning_save_modal);
  10398. this.setInputsInline(false);
  10399. this.setPreviousStatement(true, null);
  10400. this.setNextStatement(true, null);
  10401. this.setColour("#ee783a");
  10402. this.setTooltip(Blockly.Msg.ai_model_self_learning_picture + Blockly.Msg.ai_model_self_learning_save_modal);
  10403. this.setHelpUrl("");
  10404. }
  10405. };
  10406. Blockly.Python['ai_model_self_learning_picture'] = function (block) {
  10407. var _type = block.getFieldValue('button_type');
  10408. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  10409. sys.path.append("/root/")`
  10410. Blockly.Python.definitions_['v831_import_CocoPi_BUTTON'] = `from CocoPi import BUTTON`
  10411. // Blockly.Python.definitions_['v831_import_CocoPi'] = `sys.path.append("/root/")`
  10412. Blockly.Python.definitions_['import_define_x_v831_button'] = `key_A = BUTTON(14)
  10413. key_B = BUTTON(8)
  10414. key_C = BUTTON(13)
  10415. key_D = BUTTON(7)
  10416. `
  10417. var code = `if key_${_type}.is_pressed() and SELFLEARNCOUNT >= SELFLEARN.class_num:
  10418. while not (key_${_type}.is_pressed() == False):
  10419. time.sleep(0.1)
  10420. SELFLEARN.classifier.train()
  10421. SELFLEARN.classifier.save("/root/module.bin")
  10422. `;
  10423. return code;
  10424. }; Blockly.Blocks['ai_model_self_learning_save_modal'] = {
  10425. init: function () {
  10426. this.appendDummyInput()
  10427. .appendField(Blockly.Msg.ai_model_self_learning_save_modal);
  10428. this.setInputsInline(false);
  10429. this.setPreviousStatement(true, null);
  10430. this.setNextStatement(true, null);
  10431. this.setColour("#ee783a");
  10432. this.setTooltip(Blockly.Msg.ai_model_self_learning_save_modal);
  10433. this.setHelpUrl("");
  10434. }
  10435. };
  10436. Blockly.Python['ai_model_self_learning_save_modal'] = function (block) {
  10437. var code = `SELFLEARN.classifier.save("/root/module.bin")
  10438. `;
  10439. return code;
  10440. };
  10441. Blockly.Blocks['ai_model_self_learning_class_verification'] = {
  10442. init: function () {
  10443. this.appendDummyInput()
  10444. .appendField(Blockly.Msg.ai_model_self_learning_class_verification);
  10445. this.setInputsInline(false);
  10446. this.setPreviousStatement(true, null);
  10447. this.setNextStatement(true, null);
  10448. this.setColour("#ee783a");
  10449. this.setTooltip(Blockly.Msg.ai_model_self_learning_class_verification);
  10450. this.setHelpUrl("");
  10451. }
  10452. };
  10453. Blockly.Python['ai_model_self_learning_class_verification'] = function (block) {
  10454. var code = `_canvas_x, _canvas_y = 48,8
  10455. canvas = canvas.crop(48, 8, 224, 224)
  10456. SELFLEARNidx, SELFLEARNdistance = SELFLEARN.classifier.predict(canvas)
  10457. `;
  10458. return code;
  10459. };
  10460. Blockly.Blocks['ai_model_self_learning_result'] = {
  10461. init: function () {
  10462. this.appendDummyInput()
  10463. .appendField(Blockly.Msg.ai_model_self_learning_result)
  10464. .appendField(new Blockly.FieldDropdown([
  10465. [Blockly.Msg.ai_model_self_learning_result_name, "X"],
  10466. [Blockly.Msg.ai_models_object_model_confidence, "Y"]
  10467. ]), "TYPE");;
  10468. this.setOutput(true, null);
  10469. this.setColour("#ee783a");
  10470. this.setTooltip(Blockly.Msg.ai_model_self_learning_result);
  10471. this.setHelpUrl("");
  10472. }
  10473. };
  10474. Blockly.Python['ai_model_self_learning_result'] = function (block) {
  10475. var type = block.getFieldValue('TYPE');
  10476. var code = '';
  10477. if (type == 'X') {
  10478. code = `CLASSNAMELIST[SELFLEARNidx]`
  10479. } else {
  10480. code = `100-SELFLEARNdistance`
  10481. }
  10482. return [code, Blockly.Python.ORDER_ATOMIC];
  10483. };
  10484. Blockly.Blocks['ai_model_customized_init_setup_definition'] = {
  10485. init: function () {
  10486. this.appendDummyInput()
  10487. .appendField(new Blockly.FieldImage("blockly/media/header_customized_model.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
  10488. this.appendDummyInput()
  10489. .appendField(Blockly.Msg.ai_model_customized_init_setup_definition);
  10490. this.setInputsInline(false);
  10491. this.setPreviousStatement(true, null);
  10492. this.setNextStatement(true, null);
  10493. this.setColour("#ee783a");
  10494. this.setTooltip(Blockly.Msg.ai_model_customized_init_setup_definition_TOOLTIP);
  10495. this.setHelpUrl("");
  10496. }
  10497. };
  10498. Blockly.Python['ai_model_customized_init_setup_definition'] = function (block) {
  10499. // TODO: Assemble Python into code variable.
  10500. Blockly.Python.includes_.import_amxisd = '' +
  10501. 'print("Clearing Cached Variables...", end="")\n' +
  10502. 'for name in dir(): \n' +
  10503. ' if not name.startswith(\'_\'): \n' +
  10504. ' del globals()[name]\n' +
  10505. 'print(" Done")\n' +
  10506. 'import KPU as kpu\n' +
  10507. 'kpu.memtest()\n' +
  10508. 'from Maix import utils\n' +
  10509. 'import gc\n' +
  10510. 'gc.enable()\n' +
  10511. 'utils.gc_heap_size()\n' +
  10512. '\n' +
  10513. '################# Done Init #################\n' +
  10514. '';
  10515. var code = '';
  10516. return code;
  10517. };
  10518. // 初始化serialcomm_read_setup_title_text_1
  10519. Blockly.Blocks['ai_model_index_custom_initialization_init_1'] = {
  10520. init: function () {
  10521. this.appendValueInput("class_input")
  10522. .setCheck(null)
  10523. .appendField(Blockly.Msg.serialcomm_read_setup_title_text_1)
  10524. // .appendField("DEGREE");
  10525. this.setPreviousStatement(true, null);
  10526. this.setNextStatement(true, null);
  10527. this.setColour("#ee783a");
  10528. this.setTooltip(Blockly.Msg.serialcomm_read_setup_title_text_1);
  10529. this.setHelpUrl('');
  10530. }
  10531. };
  10532. Blockly.Python.ai_model_index_custom_initialization_init_1 = function (block) {
  10533. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  10534. Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
  10535. var _code = value_class_input;
  10536. return _code;
  10537. }
  10538. // 自定义人脸识别
  10539. Blockly.Blocks['ai_model_face_model'] = {
  10540. init: function () {
  10541. this.appendDummyInput()
  10542. .appendField(Blockly.Msg.ai_model_face_setup);
  10543. this.setInputsInline(false);
  10544. this.setOutput(true, null);
  10545. this.setColour("#ee783a");
  10546. this.setTooltip(Blockly.Msg.ai_model_face_setup);
  10547. this.setHelpUrl("");
  10548. }
  10549. };
  10550. Blockly.Python.ai_model_face_model = function (block) {
  10551. Blockly.Python.definitions_['v831_import_time'] = `import time`
  10552. Blockly.Python.definitions_['v831_import_gc'] = `import gc`
  10553. Blockly.Python.includes_.import_amxisd = '' +
  10554. 'for name in dir(): \n' +
  10555. ' if not name.startswith(\'_\'): \n' +
  10556. ' del globals()[name]\n' +
  10557. 'gc.enable()\n' +
  10558. '\n' +
  10559. 'try:\n' +
  10560. ' from cocorobo import firmware_info\n' +
  10561. 'except BaseException as e:\n' +
  10562. ' print(str(e))\n' +
  10563. ' pass\n' +
  10564. '\n' +
  10565. 'if int(str(firmware_info.ai()).split("-")[0]) < 2021:\n' +
  10566. ' from Maix import utils\n' +
  10567. ' utils.gc_heap_size(0xAF000)\n' +
  10568. 'else:\n' +
  10569. ' import utime\n' +
  10570. ' clock = None\n' +
  10571. ' ACCURACY = 85\n' +
  10572. '\n' +
  10573. 'task_fd = None\n' +
  10574. 'task_ld = None\n' +
  10575. 'task_fe = None\n' +
  10576. '\n' +
  10577. '';
  10578. var code = `
  10579. from fpioa_manager import *
  10580. from Maix import FPIOA, GPIO
  10581. _gp_side_buttons = [9, 10, 11]
  10582. FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)
  10583. FPIOA().set_function(_gp_side_buttons[1],FPIOA.GPIO1)
  10584. FPIOA().set_function(_gp_side_buttons[2],FPIOA.GPIO2)
  10585. _gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)
  10586. _gp_side_b = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)
  10587. _gp_side_c = GPIO(GPIO.GPIO2,GPIO.IN,GPIO.PULL_UP)
  10588. side_button_state = False
  10589. def run_face_compare(record_ftrs, feature):
  10590. scores = []
  10591. for j in range(len(record_ftrs)):
  10592. score = kpu.face_compare(record_ftrs[j], feature)
  10593. scores.append(score)
  10594. max_score = 0
  10595. # if int(str(firmware_info.ai()).split("-")[0]) < 2021:
  10596. # index = -1
  10597. # else:
  10598. # index = 0
  10599. index = -1
  10600. for k in range(len(scores)):
  10601. if max_score < scores[k]:
  10602. max_score = scores[k]
  10603. index = k
  10604. return [scores, max_score, index]
  10605. # print(scores)
  10606. `;
  10607. // var code = '';
  10608. return [code, Blockly.Python.ORDER_ATOMIC];
  10609. }
  10610. // 设置人脸识别名称
  10611. Blockly.Blocks['ai_model_face_setup'] = {
  10612. init: function () {
  10613. // this.appendDummyInput()
  10614. // .appendField(Blockly.Msg.ai_model_face_setup);
  10615. this.appendValueInput("class_input")
  10616. .setCheck(null)
  10617. .appendField(Blockly.Msg.ai_model_face_name);
  10618. this.setPreviousStatement(true, null);
  10619. this.setNextStatement(true, null);
  10620. this.setColour("#ee783a");
  10621. this.setTooltip(Blockly.Msg.ai_model_face_name.replace(":", ""));
  10622. this.setHelpUrl("");
  10623. }
  10624. };
  10625. Blockly.Python['ai_model_face_setup'] = function (block) {
  10626. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  10627. var code = `
  10628. img_lcd = image.Image()
  10629. # img_face = image.Image(size=(128, 128))
  10630. collectNum = 0
  10631. record_ftrs = []
  10632. result = []
  10633. res_index = -1
  10634. # a = img_face.pix_to_ai()
  10635. names = ` + value_class_input + `
  10636. `;
  10637. return code;
  10638. };
  10639. Blockly.Blocks['ai_face_detection_model'] = {
  10640. init: function () {
  10641. this.appendDummyInput()
  10642. .appendField(Blockly.Msg.ai_face_detection_model);
  10643. this.setPreviousStatement(true, null);
  10644. this.setNextStatement(true, null);
  10645. this.setColour("#ee783a");
  10646. this.setTooltip(Blockly.Msg.ai_face_detection_model_TOOLTIP);
  10647. this.setHelpUrl("");
  10648. }
  10649. };
  10650. Blockly.Python['ai_face_detection_model'] = function (block) {
  10651. var code = '\n' +
  10652. 'if int(str(firmware_info.ai()).split("-")[0]) < 2021:\n' +
  10653. ' task_fd = kpu.load("/sd/preset/models/face_reocgnition/FD.emodel") # 人脸检测模型\n' +
  10654. 'else:\n' +
  10655. ' task_fd = kpu.load("/sd/preset/models/face/v2/FaceDetection.emodel") # 0x300000)\n' +
  10656. 'anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025)\n' +
  10657. 'a = kpu.init_yolo2(task_fd, 0.5, 0.3, 5, anchor)\n' +
  10658. '\n' +
  10659. '';
  10660. return code;
  10661. };
  10662. Blockly.Blocks['ai_face_point_detection_model'] = {
  10663. init: function () {
  10664. this.appendDummyInput()
  10665. .appendField(Blockly.Msg.ai_face_point_detection_model);
  10666. this.setPreviousStatement(true, null);
  10667. this.setNextStatement(true, null);
  10668. this.setColour("#ee783a");
  10669. this.setTooltip(Blockly.Msg.ai_face_point_detection_model_TOOLTIP);
  10670. this.setHelpUrl("");
  10671. }
  10672. };
  10673. Blockly.Python['ai_face_point_detection_model'] = function (block) {
  10674. Blockly.Python.definitions_.import_ai_face_point_detection_model = `
  10675. def run_five_points_inference(canvas, canvas_face, detected_face_rect):
  10676. a = canvas.draw_rectangle(detected_face_rect)
  10677. # Create face image from detection
  10678. face_cut = canvas.cut(detected_face_rect[0], detected_face_rect[1], detected_face_rect[2], detected_face_rect[3])
  10679. # Convert created face image to a dimension of 128x128
  10680. face_cut_128 = face_cut.resize(128, 128)
  10681. # 将正脸图像转为kpu格式, 便于后面的关键点模型的使用
  10682. a = face_cut_128.pix_to_ai()
  10683. # Get landmark for 5 points of the detected face
  10684. # 运行人脸5点关键点检测模型, 获取当前识别人脸的关键点数据
  10685. fmap = kpu.forward(task_ld, face_cut_128)
  10686. # print("fmap", fmap)
  10687. # 获取关键点预测结果
  10688. plist = fmap[:]
  10689. # print("plist", plist)
  10690. # Get position of left eye, then draw it on the canvas.
  10691. le = (i.x()+int(plist[0]*i.w() - 10), i.y()+int(plist[1]*i.h()))
  10692. a = canvas.draw_circle(le[0], le[1], 4)
  10693. # Get position of Right eye, then draw it on the canvas.
  10694. re = (i.x()+int(plist[2]*i.w()), i.y()+int(plist[3]*i.h()))
  10695. a = canvas.draw_circle(re[0], re[1], 4)
  10696. # Get position of Nose, then draw it on the canvas.
  10697. nose = (i.x()+int(plist[4]*i.w()), i.y()+int(plist[5]*i.h()))
  10698. a = canvas.draw_circle(nose[0], nose[1], 4)
  10699. # Get position of Left cheek, then draw it on the canvas.
  10700. lm = (i.x()+int(plist[6]*i.w()), i.y()+int(plist[7]*i.h()))
  10701. a = canvas.draw_circle(lm[0], lm[1], 4)
  10702. # Get position of Right cheek, then draw it on the canvas.
  10703. rm = (i.x()+int(plist[8]*i.w()), i.y()+int(plist[9]*i.h()))
  10704. a = canvas.draw_circle(rm[0], rm[1], 4)
  10705. # Align face to standard position
  10706. src_point = [le, re, nose, lm, rm]
  10707. '''
  10708. get_affine_transform() 函数, 根据获得的5点坐标与标准正脸坐标获取仿射变换矩阵
  10709. - 计算3个二维点对之间的仿射变换矩阵H(2行x3列),自由度为6。
  10710. - 仿射变换的原理就是根据源图像中一个三角形的顶点坐标和应用仿射变换之后的
  10711. - 目标图像中一个三角形的顶点坐标计算出一个变换矩阵,然后将这个矩阵应用到整个源图像。
  10712. src_point 为输入源图像中的三角形顶点坐标
  10713. dst_point 为输出目标图像中的三角形顶点坐标
  10714. Ref: https://zhuanlan.zhihu.com/p/142260846
  10715. '''
  10716. T = image.get_affine_transform(src_point, dst_point)
  10717. '''
  10718. warp_affine_ai() 函数, 对原始图片人脸图片进行仿射变换,变换为正脸图像
  10719. '''
  10720. a = image.warp_affine_ai(canvas, canvas_face, T)
  10721. '''
  10722. 将正脸图像转为kpu格式
  10723. '''
  10724. a = canvas_face.ai_to_pix()
  10725. # Recycle face_cut_128 in order to reduce memory usage
  10726. del(face_cut_128)
  10727. `;
  10728. var code = '\n' +
  10729. 'if int(str(firmware_info.ai()).split("-")[0]) < 2021:\n' +
  10730. ' task_ld = kpu.load("/sd/preset/models/face_reocgnition/KP_chwise.emodel") # 人脸5点关键点检测模型\n' +
  10731. 'else:\n' +
  10732. ' task_ld = kpu.load("/sd/preset/models/face/v2/FaceLandmarkDetection.emodel")# 0x400000)\n' +
  10733. 'dst_point = [(44, 59), (84, 59), (64, 82), (47, 105),(81, 105)]\n' +
  10734. '\n' +
  10735. '';
  10736. return code;
  10737. };
  10738. Blockly.Blocks['ai_face_Vitter_eigenvalue_model'] = {
  10739. init: function () {
  10740. this.appendDummyInput()
  10741. .appendField(Blockly.Msg.ai_face_Vitter_eigenvalue_model);
  10742. this.setPreviousStatement(true, null);
  10743. this.setNextStatement(true, null);
  10744. this.setColour("#ee783a");
  10745. this.setTooltip(Blockly.Msg.ai_face_Vitter_eigenvalue_model_TOOLTIP);
  10746. this.setHelpUrl("");
  10747. }
  10748. };
  10749. Blockly.Python['ai_face_Vitter_eigenvalue_model'] = function (block) {
  10750. Blockly.Python.definitions_.import_ai_face_Vitter_eigenvalue_model = `
  10751. def run_fmap_calculation(canvas_face):
  10752. global fmap
  10753. # Calculate face feature vector from 'img_face', 计算正脸图片的196维特征值
  10754. fmap = kpu.forward(task_fe, canvas_face)
  10755. # 获取计算结果
  10756. feature = kpu.face_encode(fmap[:])
  10757. return feature
  10758. `;
  10759. var code = '\n' +
  10760. 'if int(str(firmware_info.ai()).split("-")[0]) < 2021:\n' +
  10761. ' task_fe = kpu.load("/sd/preset/models/face_reocgnition/face_recognition_v4_2020_09_07.emodel") # 人脸196维特征值模型\n' +
  10762. ' kpu.set_outputs(task_fe, 0,1,1, 512)\n' +
  10763. 'else:\n' +
  10764. ' task_fe = kpu.load("/sd/preset/models/face/v2/FeatureExtraction.emodel")# 0x500000)\n' +
  10765. ' clock = time.clock()\n' +
  10766. '\n' +
  10767. '';
  10768. return code;
  10769. };
  10770. // 加载人脸检测模型,设置识别图像源
  10771. Blockly.Blocks['ai_model_face_start_recognition'] = {
  10772. init: function () {
  10773. this.appendDummyInput()
  10774. .appendField(Blockly.Msg.ai_model_face_start_recognition + Blockly.Msg.ai_models_customized_init_setup_source)
  10775. .appendField(new Blockly.FieldVariable("img"), "varitem");
  10776. // this.appendDummyInput()
  10777. // .appendField(Blockly.Msg.ai_models_customized_init_type_set)
  10778. // .appendField(new Blockly.FieldDropdown([
  10779. // [Blockly.Msg.ai_models_customized_init_type_set_fast, "fast"],
  10780. // [Blockly.Msg.ai_models_customized_init_type_set_slow, "slow"]
  10781. // ]), "detect_type");
  10782. // this.appendStatementInput("NAME")
  10783. // .setCheck(null)
  10784. // .appendField(Blockly.Msg.ai_speech_recognition_any_result_text_do);
  10785. this.setInputsInline(false);
  10786. this.setPreviousStatement(true, null);
  10787. this.setNextStatement(true, null);
  10788. this.setColour("#ee783a");
  10789. this.setTooltip(Blockly.Msg.ai_model_face_start_recognition_TOOLTIP);
  10790. this.setHelpUrl("");
  10791. }
  10792. }
  10793. Blockly.Python['ai_model_face_start_recognition'] = function (block) {
  10794. // var statements_name = Blockly.Python.statementToCode(block, 'NAME');
  10795. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  10796. // var dropdown_detect_type = block.getFieldValue('detect_type');
  10797. localStorage.setItem("ai_model_face_variable_name", variable_name);
  10798. var code = '' +
  10799. 'if int(str(firmware_info.ai()).split("-")[0]) < 2021:\n' +
  10800. ' index = -1\n' +
  10801. 'else:\n' +
  10802. ' clock.tick()\n' +
  10803. 'a = img_face.pix_to_ai()\n' +
  10804. 'code = kpu.run_yolo2(task_fd, ' + variable_name + ')\n';
  10805. return code;
  10806. }
  10807. Blockly.Blocks['ai_models_customized_init_type_set'] = {
  10808. init: function () {
  10809. // this.appendDummyInput()
  10810. // .appendField(Blockly.Msg.ai_model_face_start_recognition + Blockly.Msg.ai_models_customized_init_setup_source)
  10811. // .appendField(new Blockly.FieldVariable("img"), "varitem");
  10812. this.appendDummyInput()
  10813. .appendField(Blockly.Msg.ai_models_customized_init_type_set)
  10814. .appendField(new Blockly.FieldDropdown([
  10815. [Blockly.Msg.ai_models_customized_init_type_set_fast, "fast"],
  10816. [Blockly.Msg.ai_models_customized_init_type_set_slow, "slow"]
  10817. ]), "detect_type");
  10818. // this.appendStatementInput("NAME")
  10819. // .setCheck(null)
  10820. // .appendField(Blockly.Msg.ai_speech_recognition_any_result_text_do);
  10821. this.setInputsInline(false);
  10822. this.setPreviousStatement(true, null);
  10823. this.setNextStatement(true, null);
  10824. this.setColour("#ee783a");
  10825. this.setTooltip(Blockly.Msg.ai_models_customized_init_type_set.replace(":", ""));
  10826. this.setHelpUrl("");
  10827. }
  10828. }
  10829. Blockly.Python['ai_models_customized_init_type_set'] = function (block) {
  10830. // var statements_name = Blockly.Python.statementToCode(block, 'NAME');
  10831. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  10832. var dropdown_detect_type = block.getFieldValue('detect_type');
  10833. // localStorage.setItem("ai_model_face_variable_name", variable_name);
  10834. if (dropdown_detect_type == "fast") {
  10835. // var break_string = " break\n";
  10836. var break_string = "break_string = True\n";
  10837. } else if (dropdown_detect_type == "slow") {
  10838. // var break_string = " # break\n";
  10839. var break_string = "break_string = False\n";
  10840. }
  10841. var code = '' +
  10842. break_string +
  10843. '\n';
  10844. return code;
  10845. }
  10846. Blockly.Blocks['ai_model_face_model_load_finish'] = {
  10847. init: function () {
  10848. this.appendDummyInput()
  10849. .appendField(Blockly.Msg.ai_model_face_model_load_finish);
  10850. this.setOutput(true, null);
  10851. this.setColour("#ee783a");
  10852. this.setTooltip(Blockly.Msg.ai_model_face_model_load_finish_TOOLTIP);
  10853. this.setHelpUrl("");
  10854. }
  10855. };
  10856. Blockly.Python['ai_model_face_model_load_finish'] = function (block) {
  10857. var code = 'code is not None';
  10858. return [code, Blockly.Python.ORDER_NONE];
  10859. };
  10860. Blockly.Blocks['ai_model_face_model_list'] = {
  10861. init: function () {
  10862. this.appendDummyInput()
  10863. .appendField(Blockly.Msg.ai_model_face_model_list);
  10864. this.setOutput(true, null);
  10865. this.setColour("#ee783a");
  10866. this.setTooltip(Blockly.Msg.ai_model_face_model_list_TOOLTIP);
  10867. this.setHelpUrl("");
  10868. }
  10869. };
  10870. Blockly.Python['ai_model_face_model_list'] = function (block) {
  10871. var code = 'code';
  10872. return [code, Blockly.Python.ORDER_NONE];
  10873. };
  10874. Blockly.Blocks['ai_model_face_detection_rectangle'] = {
  10875. init: function () {
  10876. this.appendDummyInput()
  10877. .appendField(new Blockly.FieldImage("blockly/media/face_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  10878. this.appendDummyInput()
  10879. .appendField(Blockly.Msg.ai_models_from)
  10880. .appendField(new Blockly.FieldVariable("i"), "varitem")
  10881. .appendField(Blockly.Msg.ai_models_face_model_get)
  10882. .appendField(new Blockly.FieldDropdown([
  10883. [Blockly.Msg.ai_models_face_model_rect, "rect"],
  10884. [Blockly.Msg.ai_models_face_model_x, "X"],
  10885. [Blockly.Msg.ai_models_face_model_y, "Y"],
  10886. [Blockly.Msg.ai_models_face_model_w, "W"],
  10887. [Blockly.Msg.ai_models_face_model_h, "H"],
  10888. [Blockly.Msg.ai_models_face_model_x_center, "CX"],
  10889. [Blockly.Msg.ai_models_face_model_y_center, "CY"],
  10890. [Blockly.Msg.ai_models_face_model_count, "Number"],
  10891. [Blockly.Msg.ai_models_face_model_index, "Serial"],
  10892. [Blockly.Msg.ai_models_face_model_confidence, "Confidence"]
  10893. ]), "TYPE");
  10894. this.setInputsInline(false);
  10895. this.setOutput(true, null);
  10896. this.setColour("#ee783a");
  10897. var thisBlock = this;
  10898. this.setTooltip(function () {
  10899. var mode = thisBlock.getFieldValue('TYPE');
  10900. var TOOLTIPS = {
  10901. 'rect': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_rect),
  10902. 'X': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x),
  10903. 'Y': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y),
  10904. 'W': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_w),
  10905. 'H': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_h),
  10906. 'CX': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x_center),
  10907. 'CY': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y_center),
  10908. 'Number': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_count),
  10909. 'Serial': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_index),
  10910. 'Confidence': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_confidence)
  10911. };
  10912. return TOOLTIPS[mode];
  10913. });
  10914. this.setHelpUrl("");
  10915. }
  10916. };
  10917. Blockly.Python.ai_model_face_detection_rectangle = function (block) {
  10918. var type = block.getFieldValue('TYPE');
  10919. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  10920. var _code = "";
  10921. if (type == "rect") {
  10922. _code = variable_name + ".rect()";
  10923. } else if (type == "X") {
  10924. _code = variable_name + ".x()";
  10925. } else if (type == "Y") {
  10926. _code = variable_name + ".y()";
  10927. } else if (type == "W") {
  10928. _code = variable_name + ".w()";
  10929. } else if (type == "H") {
  10930. _code = variable_name + ".h()";
  10931. } else if (type == "Number") {
  10932. _code = variable_name + ".objnum()";
  10933. } else if (type == "Serial") {
  10934. _code = variable_name + ".index()";
  10935. } else if (type == "Confidence") {
  10936. _code = variable_name + ".value()";
  10937. } else if (type == "CX") {
  10938. _code = variable_name + ".x()+(" + variable_name + ".w()/2)";
  10939. } else if (type == "CY") {
  10940. _code = variable_name + ".y()+(" + variable_name + ".h()/2)";
  10941. }
  10942. return [_code, Blockly.Python.ORDER_ATOMIC];
  10943. };
  10944. Blockly.Blocks['ai_model_face_load_point_detection_model'] = {
  10945. init: function () {
  10946. this.appendDummyInput()
  10947. .appendField(Blockly.Msg.ai_model_face_load_point_detection_model + Blockly.Msg.ai_models_customized_init_setup_source)
  10948. .appendField(new Blockly.FieldVariable("img"), "varitem");
  10949. this.appendDummyInput()
  10950. .appendField(Blockly.Msg.ai_model_face_load_point_detection_model_1)
  10951. .appendField(new Blockly.FieldVariable("img_face"), "varitem1");
  10952. this.appendValueInput("class_input")
  10953. .setCheck(null)
  10954. .appendField(Blockly.Msg.ai_model_face_load_point_detection_model_2);
  10955. this.setInputsInline(false);
  10956. this.setPreviousStatement(true, null);
  10957. this.setNextStatement(true, null);
  10958. this.setColour("#ee783a");
  10959. this.setTooltip(Blockly.Msg.ai_model_face_load_point_detection_model_TOOLTIP);
  10960. this.setHelpUrl("");
  10961. }
  10962. }
  10963. Blockly.Python.ai_model_face_load_point_detection_model = function (block) {
  10964. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  10965. var variable_name_1 = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem1'), Blockly.Variables.NAME_TYPE);
  10966. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  10967. var _code = "run_five_points_inference(" + variable_name + ", " + variable_name_1 + ", " + value_class_input + ")\n";
  10968. return _code;
  10969. };
  10970. Blockly.Blocks['ai_model_face_gather'] = {
  10971. init: function () {
  10972. this.appendDummyInput()
  10973. .appendField(Blockly.Msg.ai_model_face_gather);
  10974. this.appendValueInput("class_input")
  10975. .setCheck(null)
  10976. .appendField(Blockly.Msg.ai_model_face_gather_1);
  10977. this.setPreviousStatement(true, null);
  10978. this.setNextStatement(true, null);
  10979. this.setColour("#ee783a");
  10980. this.setTooltip(Blockly.Msg.ai_model_face_gather_TOOLTIP);
  10981. this.setHelpUrl("");
  10982. }
  10983. };
  10984. Blockly.Python['ai_model_face_gather'] = function (block) {
  10985. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC).replace('\"', '').replace('\"', '');
  10986. Blockly.Python.definitions_['v831_import_os'] = `import os`
  10987. Blockly.Python.addVariable('ai_model_face_gather', `
  10988. def _CREATE_TEXT_FILE_WITH_CONTENT(_path, _data, _sep):
  10989. f = open(_path, 'a')
  10990. f.write(_data + _sep)
  10991. f.close()
  10992. `, true);
  10993. var code = '' +
  10994. '# start to gather faces\n' +
  10995. 'result = run_face_compare(record_ftrs, feature)\n' +
  10996. 'if int(str(firmware_info.ai()).split("-")[0]) < 2021 and collectNum < len(names):\n' +
  10997. ' if(len(names) > result[2]):\n' +
  10998. ' if result[1] <= 80:\n' +
  10999. ' a = lcd_draw_string(' + localStorage.getItem("ai_model_face_variable_name") + ', i.x(), i.y(), ("X :%2.1f" % (result[1])), color=(102, 204, 153), scale=1, mono_space=False)\n' +
  11000. ' if _gp_side_b.value() == True and side_button_state == False:\n' +
  11001. ' record_ftrs.append(feature)\n' +
  11002. ' collectNum = collectNum + 1\n' +
  11003. ' side_button_state = True\n' +
  11004. ' elif _gp_side_b.value() == False and side_button_state == True:\n' +
  11005. ' side_button_state = False\n' +
  11006. 'elif int(str(firmware_info.ai()).split("-")[0]) >= 2021 and collectNum < len(names):\n' +
  11007. ' if(len(names) > result[2]):\n' +
  11008. ' if result[1] <= ACCURACY:\n' +
  11009. ' a = lcd_draw_string(' + localStorage.getItem("ai_model_face_variable_name") + ', i.x(), i.y(), " " , color=(102, 204, 153), scale=1, mono_space=False)\n' +
  11010. ' if _gp_side_b.value() == True and side_button_state == False:\n' +
  11011. ' record_ftrs.append(feature)\n' +
  11012. ' collectNum = collectNum + 1\n' +
  11013. ' side_button_state = True\n' +
  11014. ' elif _gp_side_b.value() == False and side_button_state == True:\n' +
  11015. ' side_button_state = False\n' +
  11016. 'if collectNum >= len(names):\n' +
  11017. ' try:\n' +
  11018. ' os.remove("/sd/user/' + value_class_input + '.py")\n' +
  11019. ' except:\n' +
  11020. ' pass\n' +
  11021. ' all = {}\n' +
  11022. ' try:\n' +
  11023. ' for j in range(len(names)):\n' +
  11024. ' all[names[j]] = record_ftrs[j]\n' +
  11025. ' _CREATE_TEXT_FILE_WITH_CONTENT("/sd/user/' + value_class_input + '.py", "all =" + str(all), "\\r\\n")\n' +
  11026. '\n' +
  11027. ' except:\n' +
  11028. ' pass\n' +
  11029. // 'if break_string == True:\n' +
  11030. // ' break\n' +
  11031. '\n';
  11032. return code;
  11033. };
  11034. Blockly.Blocks['ai_model_face_gather_num_judge'] = {
  11035. init: function () {
  11036. this.appendDummyInput()
  11037. .appendField(Blockly.Msg.ai_model_face_gather_num_judge);
  11038. this.setOutput(true, null);
  11039. this.setColour("#ee783a");
  11040. this.setTooltip(Blockly.Msg.ai_model_face_gather_num_judge_TOOLTIP);
  11041. this.setHelpUrl("");
  11042. }
  11043. };
  11044. Blockly.Python['ai_model_face_gather_num_judge'] = function (block) {
  11045. var code = 'collectNum >= len(names)';
  11046. return [code, Blockly.Python.ORDER_NONE];
  11047. };
  11048. Blockly.Blocks['ai_model_face_data_load'] = {
  11049. init: function () {
  11050. this.appendDummyInput()
  11051. .appendField(Blockly.Msg.ai_model_face_data_load);
  11052. this.appendValueInput("class_input")
  11053. .setCheck(null)
  11054. .appendField(Blockly.Msg.ai_model_face_data_load_1);
  11055. this.setPreviousStatement(true, null);
  11056. this.setNextStatement(true, null);
  11057. this.setColour("#ee783a");
  11058. this.setTooltip(Blockly.Msg.ai_model_face_data_load_TOOLTIP);
  11059. this.setHelpUrl("");
  11060. }
  11061. };
  11062. Blockly.Python['ai_model_face_data_load'] = function (block) {
  11063. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC).replace('\"', '').replace('\"', '');
  11064. Blockly.Python.addVariable('ai_model_face_data_load', `
  11065. try:
  11066. import user.${value_class_input} as recorded_face_features
  11067. except:
  11068. pass
  11069. `, true);
  11070. var code = '' +
  11071. '# load faces data\n' +
  11072. 'all = {}\n' +
  11073. 'record_ftrs = []\n' +
  11074. 'collectNum = len(names)\n' +
  11075. 'try:\n' +
  11076. ' all = recorded_face_features.all\n' +
  11077. ' for j in range(len(names)):\n' +
  11078. ' record_ftrs.append(all[names[j]])\n' +
  11079. '\n' +
  11080. 'except:\n' +
  11081. ' pass\n' +
  11082. '\n';
  11083. // 'all = recorded_face_features.all\n' +
  11084. return code;
  11085. };
  11086. Blockly.Blocks['ai_model_face_recognition'] = {
  11087. init: function () {
  11088. this.appendDummyInput()
  11089. .appendField(Blockly.Msg.ai_model_face_recognition);
  11090. this.setPreviousStatement(true, null);
  11091. this.setNextStatement(true, null);
  11092. this.setColour("#ee783a");
  11093. this.setTooltip(Blockly.Msg.ai_model_face_recognition_TOOLTIP);
  11094. this.setHelpUrl("");
  11095. }
  11096. };
  11097. Blockly.Python['ai_model_face_recognition'] = function (block) {
  11098. var code = '' +
  11099. 'if collectNum >= len(names):\n' +
  11100. ' result = run_face_compare(record_ftrs, feature)\n' +
  11101. ' if int(str(firmware_info.ai()).split("-")[0]) < 2021:\n' +
  11102. ' if(len(names) > result[2]):\n' +
  11103. ' if result[1] > 80:\n' +
  11104. ' a = lcd_draw_string(' + localStorage.getItem("ai_model_face_variable_name") + ', i.x(), i.y(), ("%s :%2.1f" % (names[result[2]], result[1])), color=(0, 255, 0), scale=1, mono_space=False)\n' +
  11105. ' else:\n' +
  11106. ' if(len(names) > result[2]):\n' +
  11107. ' if result[1] > ACCURACY:\n' +
  11108. ' a = lcd_draw_string(' + localStorage.getItem("ai_model_face_variable_name") + ', i.x(), i.y(), ("%s :%2.1f" % (names[result[2]], result[1])), color=(0, 255, 0), scale=1, mono_space=False)\n' +
  11109. ' res_index = result[2]\n' +
  11110. '\n' +
  11111. 'if break_string == True:\n' +
  11112. ' break\n' +
  11113. '\n' +
  11114. '';
  11115. return code;
  11116. };
  11117. Blockly.Blocks['ai_model_face_recognition_get_result'] = {
  11118. init: function () {
  11119. this.appendDummyInput()
  11120. .appendField(Blockly.Msg.ai_model_face_recognition_get_result);
  11121. // this.appendStatementInput("NAME")
  11122. // .setCheck(null)
  11123. // .appendField(Blockly.Msg.ai_speech_recognition_any_result_text_do);
  11124. this.setOutput(true);
  11125. this.setColour("#ee783a");
  11126. this.setTooltip(Blockly.Msg.ai_model_face_recognition_get_result_TOOLTIP);
  11127. this.setHelpUrl("");
  11128. }
  11129. };
  11130. Blockly.Python['ai_model_face_recognition_get_result'] = function (block) {
  11131. // var statements_name = Blockly.Python.statementToCode(block, 'NAME');
  11132. var code = '(len(names) > result[2] and collectNum >= len(names))';
  11133. // statements_name +
  11134. // 'if break_string == True:\n' +
  11135. // ' break\n' +
  11136. // 'if int(str(firmware_info.ai()).split("-")[0]) < 2021:\n' +
  11137. // ' ' + localStorage.getItem("ai_model_face_variable_name") + ' = ' + localStorage.getItem("ai_model_face_variable_name") + '.cut(40,0,240,240)\n' +
  11138. // ' a = lcd.display(' + localStorage.getItem("ai_model_face_variable_name") + ', oft=(0,0))\n' +
  11139. // ' gc.collect()\n' +
  11140. // ' # kpu.memtest()\n' +
  11141. // 'else:\n' +
  11142. // ' fps = clock.fps()\n' +
  11143. // ' a = lcd.display(' + localStorage.getItem("ai_model_face_variable_name") + ')\n' +
  11144. // ' gc.collect()\n' +
  11145. // ' # kpu.memtest()\n' +
  11146. // '\n' +
  11147. // '';
  11148. return [code, Blockly.Python.ORDER_NONE];
  11149. };
  11150. Blockly.Blocks['ai_model_getfaceresult'] = {
  11151. init: function () {
  11152. this.appendDummyInput()
  11153. .appendField(Blockly.Msg.ai_model_getface);
  11154. this.setOutput(true, null);
  11155. this.setColour("#ee783a");
  11156. this.setTooltip(Blockly.Msg.ai_model_getface_TOOLTIP);
  11157. this.setHelpUrl("");
  11158. }
  11159. };
  11160. Blockly.Python['ai_model_getfaceresult'] = function (block) {
  11161. // TODO: Assemble Python into code variable.
  11162. var code = 'names[res_index] if (res_index != -1 and len(names) > res_index and collectNum >= len(names)) else ""';
  11163. // TODO: Change ORDER_NONE to the correct strength.
  11164. return [code, Blockly.Python.ORDER_NONE];
  11165. };
  11166. Blockly.Blocks['ai_model_show_face'] = {
  11167. init: function () {
  11168. this.appendDummyInput()
  11169. .appendField(Blockly.Msg.ai_model_show_face);
  11170. this.setPreviousStatement(true, null);
  11171. this.setNextStatement(true, null);
  11172. this.setColour("#ee783a");
  11173. this.setTooltip(Blockly.Msg.ai_model_show_face_TOOLTIP);
  11174. this.setHelpUrl("");
  11175. }
  11176. };
  11177. Blockly.Python['ai_model_show_face'] = function (block) {
  11178. var code = '' +
  11179. // ' if break_string == True:\n' +
  11180. // ' break\n' +
  11181. 'lcd_draw_string(img, 240, 0, (str(num)), color = (255, 102, 0), scale = 2, mono_space = False)\n' +
  11182. 'if isCollect == 0:\n' +
  11183. ' lcd_draw_string(img, 40, 0, "按A识别上次样本", color = (255, 0, 0), scale = 1, mono_space = False)\n' +
  11184. ' lcd_draw_string(img, 40, 16, "按C重新拍摄", color = (255, 0, 0), scale = 1, mono_space = False)\n' +
  11185. 'elif isCollect == 1:\n' +
  11186. ' lcd_draw_string(img, 40, 0, "开始识别", color = (255, 0, 0), scale = 1, mono_space = False)\n' +
  11187. 'elif isCollect == 2:\n' +
  11188. ' lcd_draw_string(img, 40, 0, "开始采集人脸模板数据", color = (255, 0, 0), scale = 1, mono_space = False)\n' +
  11189. 'if int(str(firmware_info.ai()).split("-")[0]) < 2021:\n' +
  11190. ' ' + localStorage.getItem("ai_model_face_variable_name") + ' = ' + localStorage.getItem("ai_model_face_variable_name") + '.cut(40,0,240,240)\n' +
  11191. ' lcd.display(' + localStorage.getItem("ai_model_face_variable_name") + ', oft=(0,0))\n' +
  11192. ' gc.collect()\n' +
  11193. 'else:\n' +
  11194. ' fps = clock.fps()\n' +
  11195. ' a = lcd.display(' + localStorage.getItem("ai_model_face_variable_name") + ')\n' +
  11196. ' gc.collect()\n' +
  11197. '';
  11198. return code;
  11199. };
  11200. Blockly.Blocks['ai_model_class_setup'] = {
  11201. init: function () {
  11202. // this.appendDummyInput()
  11203. // .appendField(Blockly.Msg.ai_model_class_setup);
  11204. this.appendValueInput("class_input")
  11205. .setCheck(null)
  11206. .appendField(Blockly.Msg.ai_model_class_name);
  11207. this.setPreviousStatement(true, null);
  11208. this.setNextStatement(true, null);
  11209. this.setColour("#ee783a");
  11210. this.setTooltip(Blockly.Msg.ai_model_class_name.replace(":", ""));
  11211. this.setHelpUrl("");
  11212. }
  11213. };
  11214. Blockly.Python['ai_model_class_setup'] = function (block) {
  11215. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  11216. var code = `
  11217. ############### config #################
  11218. class_names = ` + value_class_input + `
  11219. class_num = len(class_names)
  11220. sample_num = len(class_names) * 5
  11221. THRESHOLD = 11
  11222. board_cube = 0
  11223. button_state = False
  11224. FPIOA().set_function(10, FPIOA.GPIO1)
  11225. key1 = GPIO(GPIO.GPIO1, GPIO.IN, GPIO.PULL_UP)
  11226. `;
  11227. return code;
  11228. };
  11229. Blockly.Blocks['ai_class_model'] = {
  11230. init: function () {
  11231. this.appendDummyInput()
  11232. .appendField(Blockly.Msg.ai_class_model);
  11233. this.setPreviousStatement(true, null);
  11234. this.setNextStatement(true, null);
  11235. this.setColour("#ee783a");
  11236. this.setTooltip(Blockly.Msg.ai_class_model_TOOLTIP);
  11237. this.setHelpUrl("");
  11238. }
  11239. };
  11240. Blockly.Python['ai_class_model'] = function (block) {
  11241. Blockly.Python.addVariable('ai_class_model', `
  11242. try:
  11243. del model
  11244. except Exception:
  11245. pass
  11246. try:
  11247. del classifier
  11248. except Exception:
  11249. pass
  11250. gc.collect()
  11251. cap_num = 0
  11252. train_status = 0
  11253. last_cap_time = 0
  11254. last_btn_status = 0
  11255. res_index = -1
  11256. is_save = 0
  11257. `)
  11258. var code = 'model = kpu.load("/sd/preset/models/object_classifier.emodel")\n';
  11259. return code;
  11260. };
  11261. Blockly.Blocks['ai_class_model_cluster'] = {
  11262. init: function () {
  11263. this.appendDummyInput()
  11264. .appendField(Blockly.Msg.ai_class_model_cluster);
  11265. this.setPreviousStatement(true, null);
  11266. this.setNextStatement(true, null);
  11267. this.setColour("#ee783a");
  11268. this.setTooltip(Blockly.Msg.ai_class_model_cluster_TOOLTIP);
  11269. this.setHelpUrl("");
  11270. }
  11271. };
  11272. Blockly.Python['ai_class_model_cluster'] = function (block) {
  11273. // Blockly.Python.addVariable('ai_class_model_cluster', `
  11274. // `)
  11275. var code = 'classifier = kpu.classifier(model, class_num, sample_num)\n';
  11276. return code;
  11277. };
  11278. Blockly.Blocks['ai_model_class_start_load'] = {
  11279. init: function () {
  11280. this.appendDummyInput()
  11281. .appendField(Blockly.Msg.ai_models_customized_init_setup_source)
  11282. .appendField(new Blockly.FieldVariable("img"), "varitem");
  11283. this.appendDummyInput()
  11284. .appendField(Blockly.Msg.ai_model_class_start_load);
  11285. // this.appendStatementInput("NAME")
  11286. // .setCheck(null)
  11287. // .appendField(Blockly.Msg.ai_speech_recognition_any_result_text_do);
  11288. this.setInputsInline(false);
  11289. this.setPreviousStatement(true, null);
  11290. this.setNextStatement(true, null);
  11291. this.setColour("#ee783a");
  11292. this.setTooltip(Blockly.Msg.ai_model_class_start_load_TOOLTIP);
  11293. this.setHelpUrl("");
  11294. }
  11295. }
  11296. Blockly.Python['ai_model_class_start_load'] = function (block) {
  11297. // var statements_name = Blockly.Python.statementToCode(block, 'NAME');
  11298. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  11299. localStorage.setItem("ai_model_class_start_load", variable_name);
  11300. var code = '' +
  11301. 'if board_cube:\n' +
  11302. ' ' + variable_name + ' = ' + variable_name + '.rotation_corr(z_rotation=90)\n' +
  11303. ' ' + variable_name + '.pix_to_ai()\n' +
  11304. '\n';
  11305. return code;
  11306. }
  11307. Blockly.Blocks['ai_model_class_data_load'] = {
  11308. init: function () {
  11309. this.appendDummyInput()
  11310. .appendField(Blockly.Msg.ai_model_class_data_load);
  11311. this.appendValueInput("class_input")
  11312. .setCheck(null)
  11313. .appendField(Blockly.Msg.ai_model_class_data_load_1);
  11314. this.setPreviousStatement(true, null);
  11315. this.setNextStatement(true, null);
  11316. this.setColour("#ee783a");
  11317. this.setTooltip(Blockly.Msg.ai_model_class_data_load_TOOLTIP);
  11318. this.setHelpUrl("");
  11319. }
  11320. };
  11321. Blockly.Python['ai_model_class_data_load'] = function (block) {
  11322. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC).replace('\"', '').replace('\"', '');
  11323. var code = '' +
  11324. 'try:\n' +
  11325. ' saved_path = "/sd/user/' + value_class_input + '.classifier"\n' +
  11326. ' f=open(saved_path,"r")\n' +
  11327. ' classifier, class_num, sample_num = kpu.classifier.load(model, saved_path)\n' +
  11328. ' train_status = 1\n' +
  11329. 'except:\n' +
  11330. ' ' + localStorage.getItem("ai_model_class_start_load") + '.draw_rectangle(0, 0, 240,20, fill=True, color=(0,0,0))\n' +
  11331. ' lcd_draw_string(' + localStorage.getItem("ai_model_class_start_load") + ', 0, 0, "Not found last sample", color=lcd.WHITE,scale=1,mono_space=False)\n' +
  11332. '\n';
  11333. return code;
  11334. };
  11335. Blockly.Blocks['ai_model_class_gather_load_finish'] = {
  11336. init: function () {
  11337. this.appendDummyInput()
  11338. .appendField(Blockly.Msg.ai_model_class_gather_load_finish);
  11339. this.setOutput(true, null);
  11340. this.setColour("#ee783a");
  11341. this.setTooltip(Blockly.Msg.ai_model_class_gather_load_finish_TOOLTIP);
  11342. this.setHelpUrl("");
  11343. }
  11344. };
  11345. Blockly.Python['ai_model_class_gather_load_finish'] = function (block) {
  11346. var code = 'train_status == 0';
  11347. return [code, Blockly.Python.ORDER_NONE];
  11348. };
  11349. Blockly.Blocks['ai_model_class_cluster_train'] = {
  11350. init: function () {
  11351. this.appendDummyInput()
  11352. .appendField(Blockly.Msg.ai_model_class_cluster_train);
  11353. this.appendValueInput("class_input")
  11354. .setCheck(null)
  11355. .appendField(Blockly.Msg.ai_model_class_cluster_train_1);
  11356. this.setPreviousStatement(true, null);
  11357. this.setNextStatement(true, null);
  11358. this.setColour("#ee783a");
  11359. this.setTooltip(Blockly.Msg.ai_model_class_cluster_train_TOOLTIP);
  11360. this.setHelpUrl("");
  11361. }
  11362. };
  11363. Blockly.Python['ai_model_class_cluster_train'] = function (block) {
  11364. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC).replace('\"', '').replace('\"', '');
  11365. var code = '' +
  11366. 'if cap_num >= class_num + sample_num and train_status == 0:\n' +
  11367. ' ' + localStorage.getItem("ai_model_class_start_load") + '.draw_rectangle(58,98, len("training...")*10+8 , 24, fill=True, color=lcd.RED)\n' +
  11368. ' ' + localStorage.getItem("ai_model_class_start_load") + ' = lcd_draw_string(' + localStorage.getItem("ai_model_class_start_load") + ', 60, 100, "training...", color=lcd.WHITE,scale=1,mono_space=False)\n' +
  11369. ' lcd.display(' + localStorage.getItem("ai_model_class_start_load") + ')\n' +
  11370. ' try:\n' +
  11371. ' classifier.train()\n' +
  11372. ' saved_path = "/sd/user/' + value_class_input + '.classifier"\n' +
  11373. ' f=open(saved_path,"r")\n' +
  11374. ' os.remove("/sd/user/' + value_class_input + '.classifier")\n' +
  11375. ' classifier.save("/sd/user/' + value_class_input + '.classifier")\n' +
  11376. ' except:\n' +
  11377. ' classifier.save("/sd/user/' + value_class_input + '.classifier")\n' +
  11378. ' time.sleep(2)\n' +
  11379. ' train_status = 1\n' +
  11380. '\n';
  11381. return code;
  11382. };
  11383. Blockly.Blocks['ai_model_class_recognition'] = {
  11384. init: function () {
  11385. this.appendDummyInput()
  11386. .appendField(Blockly.Msg.ai_model_class_recognition);
  11387. this.setPreviousStatement(true, null);
  11388. this.setNextStatement(true, null);
  11389. this.setColour("#ee783a");
  11390. this.setTooltip(Blockly.Msg.ai_model_class_recognition_TOOLTIP);
  11391. this.setHelpUrl("");
  11392. }
  11393. };
  11394. Blockly.Python['ai_model_class_recognition'] = function (block) {
  11395. var code = '' +
  11396. '# predict\n' +
  11397. 'if train_status == 1:\n' +
  11398. ' res_index = -1\n' +
  11399. ' try:\n' +
  11400. ' res_index, min_dist = classifier.predict(' + localStorage.getItem("ai_model_class_start_load") + ')\n' +
  11401. ' except Exception as e:\n' +
  11402. ' print("predict err:", e)\n' +
  11403. ' if res_index >= 0 and min_dist < THRESHOLD :\n' +
  11404. ' ' + localStorage.getItem("ai_model_class_start_load") + '.draw_rectangle(0,0, len(class_names[res_index])*10+8 , 24, fill=True, color=lcd.RED)\n' +
  11405. ' ' + localStorage.getItem("ai_model_class_start_load") + ' = lcd_draw_string(' + localStorage.getItem("ai_model_class_start_load") + ', 2, 2, class_names[res_index], color=lcd.WHITE,scale=1,mono_space=False)\n' +
  11406. ' else:\n' +
  11407. ' ' + localStorage.getItem("ai_model_class_start_load") + '.draw_rectangle(0,0, len("maybe {}".format(class_names[res_index]))*10+8 , 24, fill=True, color=lcd.RED)\n' +
  11408. ' ' + localStorage.getItem("ai_model_class_start_load") + ' = lcd_draw_string(' + localStorage.getItem("ai_model_class_start_load") + ', 2, 2, "maybe {}".format(class_names[res_index]), color=lcd.WHITE,scale=1,mono_space=False)\n' +
  11409. '\n';
  11410. return code;
  11411. };
  11412. Blockly.Blocks['ai_model_show_class'] = {
  11413. init: function () {
  11414. this.appendDummyInput()
  11415. .appendField(Blockly.Msg.ai_model_show_face);
  11416. this.setPreviousStatement(true, null);
  11417. this.setNextStatement(true, null);
  11418. this.setColour("#ee783a");
  11419. this.setTooltip(Blockly.Msg.ai_model_show_face_TOOLTIP);
  11420. this.setHelpUrl("");
  11421. }
  11422. };
  11423. Blockly.Python['ai_model_show_class'] = function (block) {
  11424. var code = '' +
  11425. '' + localStorage.getItem("ai_model_class_start_load") + ' = ' + localStorage.getItem("ai_model_class_start_load") + '.cut(0,0,240,240)\n' +
  11426. 'lcd.display(' + localStorage.getItem("ai_model_class_start_load") + ', oft=(0,0))\n' +
  11427. '';
  11428. return code;
  11429. };
  11430. Blockly.Blocks['ai_model_customized_init_setup'] = {
  11431. init: function () {
  11432. this.appendDummyInput()
  11433. .appendField(Blockly.Msg.ai_models_customized_init_setup_title);
  11434. this.appendDummyInput()
  11435. .appendField(Blockly.Msg.ai_models_customized_init_setup_path)
  11436. .appendField(new Blockly.FieldTextInput("yolov2_int8"), "model_path");
  11437. this.appendValueInput("class_input")
  11438. .setCheck(null)
  11439. .appendField(Blockly.Msg.ai_models_customized_init_setup_name);
  11440. this.setPreviousStatement(true, null);
  11441. this.setNextStatement(true, null);
  11442. this.setColour("#ee783a");
  11443. this.setTooltip(Blockly.Msg.ai_model_customized_init_setup_TOOLTIP);
  11444. this.setHelpUrl("");
  11445. }
  11446. };
  11447. Blockly.Python['ai_model_customized_init_setup'] = function (block) {
  11448. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  11449. var text_model_path = block.getFieldValue('model_path');
  11450. Blockly.Python.definitions_['v831_import_from_maix_nn'] = "from maix import nn";
  11451. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  11452. let path = `/root/user/model/${text_model_path}`
  11453. if (text_model_path == "autologistics") {
  11454. path = `/root/preset/model/${text_model_path}`
  11455. }
  11456. let code = `class Yolo:
  11457. labels = ${value_class_input}
  11458. anchors = [1.19, 1.98, 2.79, 4.59, 4.53, 8.92, 8.06, 5.29, 10.32, 10.65]
  11459. m = {
  11460. "param": "${path}.param",
  11461. "bin": "${path}.bin"
  11462. }
  11463. options = {
  11464. "model_type": "awnn",
  11465. "inputs": {
  11466. "input0": (224, 224, 3)
  11467. },
  11468. "outputs": {
  11469. "output0": (7, 7, (1+4+len(labels))*5)
  11470. },
  11471. "mean": [127.5, 127.5, 127.5],
  11472. "norm": [0.0078125, 0.0078125, 0.0078125],
  11473. }
  11474. def __init__(self):
  11475. from maix import nn
  11476. from maix.nn import decoder
  11477. self.model = nn.load(self.m, opt=self.options)
  11478. self.decoder = decoder.Yolo2(len(self.labels), self.anchors, net_in_size=(224, 224), net_out_size=(7, 7))
  11479. def __del__(self):
  11480. del self.model
  11481. del self.decoder
  11482. Yolo = Yolo()
  11483. `;
  11484. return code;
  11485. };
  11486. Blockly.Blocks['ai_model_customized_load_setup'] = {
  11487. init: function () {
  11488. this.appendDummyInput()
  11489. .appendField(Blockly.Msg.ai_model_customized_load_setup_title);
  11490. this.appendDummyInput()
  11491. .appendField(Blockly.Msg.ai_models_customized_init_setup_path)
  11492. .appendField(new Blockly.FieldTextInput("yolov2_int8"), "model_path");
  11493. // this.appendDummyInput()
  11494. // .appendField(Blockly.Msg.ai_models_customized_init_setup_source)
  11495. // .appendField(new Blockly.FieldVariable("img_modelrecognition"), "varitem");
  11496. this.appendValueInput("class_input")
  11497. .setCheck(null)
  11498. .appendField(Blockly.Msg.ai_models_customized_init_setup_name);
  11499. this.setPreviousStatement(true, null);
  11500. this.setNextStatement(true, null);
  11501. this.setColour("#ee783a");
  11502. this.setTooltip(Blockly.Msg.ai_model_customized_load_setup_TOOLTIP);
  11503. this.setHelpUrl("");
  11504. }
  11505. };
  11506. Blockly.Python['ai_model_customized_load_setup'] = function (block) {
  11507. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  11508. var text_model_path = block.getFieldValue('model_path');
  11509. Blockly.Python.definitions_['v831_import_from_maix_nn'] = "from maix import nn";
  11510. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  11511. let path = `/root/user/model/${text_model_path}`
  11512. if (text_model_path == "autologistics") {
  11513. path = `/root/preset/model/${text_model_path}`
  11514. }
  11515. Blockly.Python.definitions_['CLASSYOLO'] = `class Yolo:
  11516. labels = ${value_class_input}
  11517. anchors = [1.19, 1.98, 2.79, 4.59, 4.53, 8.92, 8.06, 5.29, 10.32, 10.65]
  11518. m = {
  11519. "param": "${path}.param",
  11520. "bin": "${path}.bin"
  11521. }
  11522. options = {
  11523. "model_type": "awnn",
  11524. "inputs": {
  11525. "input0": (224, 224, 3)
  11526. },
  11527. "outputs": {
  11528. "output0": (7, 7, (1+4+len(labels))*5)
  11529. },
  11530. "mean": [127.5, 127.5, 127.5],
  11531. "norm": [0.0078125, 0.0078125, 0.0078125],
  11532. }
  11533. def __init__(self):
  11534. self.model = nn.load(self.m, opt=self.options)
  11535. self.decoder = decoder.Yolo2(len(self.labels), self.anchors, net_in_size=(224, 224), net_out_size=(7, 7))
  11536. self.FACESYOLOS = {
  11537. "FACE_START_X": [],
  11538. "FACE_START_Y": [],
  11539. "FACE_CENTET_X": [],
  11540. "FACE_CENTET_Y": [],
  11541. "FACE_WIDTH": [],
  11542. "FACE_HEIGHT": [],
  11543. "FACE_RESULT": [],
  11544. "FACE_CONFIDENCE": [],
  11545. }
  11546. def __del__(self):
  11547. del self.model
  11548. del self.decoder
  11549. def faceRecognizeShowResult(self,canvas):
  11550. out = self.model.forward(canvas, quantize=True, layout="hwc")
  11551. BOXES, PROBS = self.decoder.run(out, nms=0.3, threshold=0.3, img_size=(224, 224))
  11552. if len(BOXES):
  11553. for BOXESI, BOXOBJ in enumerate(BOXES):
  11554. canvas.draw_string((BOXOBJ[0]),(BOXOBJ[1]), ("".join([str(x) for x in [self.labels[PROBS[BOXESI][0]], ";", str(round((PROBS[BOXESI][1][PROBS[BOXESI][0]]*100), 2)) + str("%")]])), scale = 1, color = (255,0,0) , thickness = 1)
  11555. canvas.draw_rectangle((BOXOBJ[0]),(BOXOBJ[1]), (BOXOBJ[0])+(BOXOBJ[2]),(BOXOBJ[1])+ (BOXOBJ[3]), color=(255,0,0), thickness=1)
  11556. BOXES[BOXESI].append(PROBS[BOXESI])
  11557. return BOXES
  11558. FaceYolo = Yolo()
  11559. `;
  11560. var code = `_canvas_x, _canvas_y = 48,8
  11561. canvas = canvas.crop(48,8,224,224)
  11562. BOXES = FaceYolo.faceRecognizeShowResult(canvas)
  11563. `;
  11564. return code;
  11565. };
  11566. Blockly.Blocks['ai_model_customized_get_result'] = {
  11567. init: function () {
  11568. this.appendDummyInput()
  11569. .appendField(Blockly.Msg.ai_models_from)
  11570. .appendField(Blockly.Msg.ai_models_customized_get_result_any)
  11571. .appendField(new Blockly.FieldDropdown([
  11572. [Blockly.Msg.ai_models_customized_get_result_name, "classid"],
  11573. [Blockly.Msg.ai_models_customized_get_result_x, "x"],
  11574. [Blockly.Msg.ai_models_customized_get_result_y, "y"],
  11575. // [Blockly.Msg.ai_models_customized_get_result_w, "w"],
  11576. // [Blockly.Msg.ai_models_customized_get_result_h, "h"],
  11577. // [Blockly.Msg.ai_models_customized_get_result_x_center, "cx"],
  11578. // [Blockly.Msg.ai_models_customized_get_result_y_center, "cy"],
  11579. // [Blockly.Msg.ai_models_customized_get_result_count, "objnum"],
  11580. // [Blockly.Msg.ai_models_customized_get_result_index, "index"],
  11581. [Blockly.Msg.ai_models_customized_get_result_confidence, "value"],
  11582. [Blockly.Msg.ai_models_face_model_ws, "2"],
  11583. [Blockly.Msg.ai_models_face_model_hs, "3"],
  11584. [Blockly.Msg.ai_models_object_model_x_center, "CX"],
  11585. [Blockly.Msg.ai_models_object_model_y_center, "CY"]
  11586. ]), "get_results");
  11587. this.setOutput(true, null);
  11588. this.setColour("#ee783a");
  11589. var thisBlock = this;
  11590. this.setTooltip(function () {
  11591. var mode = thisBlock.getFieldValue('get_results');
  11592. var TOOLTIPS = {
  11593. 'classid': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_name),
  11594. 'x': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_x),
  11595. 'y': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_y),
  11596. 'w': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_w),
  11597. 'h': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_h),
  11598. 'cx': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_x_center),
  11599. 'cy': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_y_center),
  11600. 'objnum': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_count),
  11601. 'index': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_index),
  11602. 'value': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_confidence)
  11603. };
  11604. return TOOLTIPS[mode];
  11605. });
  11606. this.setHelpUrl("");
  11607. }
  11608. };
  11609. Blockly.Python['ai_model_customized_get_result'] = function (block) {
  11610. var variable_name = "BOXESI";
  11611. var dropdown_get_results = block.getFieldValue('get_results');
  11612. // TODO: Assemble Python into code variable.
  11613. var code = '';
  11614. if (dropdown_get_results == "classid") {
  11615. code = 'Yolo.labels[' + variable_name + '[4][0]]';
  11616. } else if (dropdown_get_results == 'x') {
  11617. code = variable_name + '[0]';
  11618. } else if (dropdown_get_results == "y") {
  11619. code = variable_name + "[1]";
  11620. } else if (dropdown_get_results == "w") {
  11621. code = `${variable_name}[0]+${variable_name}[2]`;
  11622. } else if (dropdown_get_results == "h") {
  11623. code = `${variable_name}[1]+${variable_name}[3]`;
  11624. } else if (dropdown_get_results == "value") {
  11625. code = `${variable_name}[4][1][${variable_name}[4][0]]`;
  11626. } else if (dropdown_get_results == "CX") {
  11627. code = `int((${variable_name}[0] + ${variable_name}[2])/2)`;
  11628. } else if (dropdown_get_results == "CY") {
  11629. code = `int((${variable_name}[1] + ${variable_name}[3])/2)`;
  11630. }
  11631. else {
  11632. code = `${variable_name}[${dropdown_get_results}]`;
  11633. }
  11634. // TODO: Change ORDER_NONE to the correct strength.
  11635. return [code, Blockly.Python.ORDER_NONE];
  11636. };
  11637. Blockly.Blocks['ai_model_customized_if_got_new'] = {
  11638. init: function () {
  11639. this.appendDummyInput()
  11640. .appendField(Blockly.Msg.ai_model_customized_attribute)
  11641. this.appendStatementInput("input")
  11642. .setCheck(null)
  11643. .appendField(Blockly.Msg.basic_motion_statement_exec);
  11644. this.updateShape_();
  11645. this.setMutator(new Blockly.Mutator(['ai_model_false_create_with_item']));
  11646. this.setColour("#ee783a");
  11647. this.setPreviousStatement(true, null);
  11648. this.setNextStatement(true, null);
  11649. this.setTooltip("");
  11650. this.setHelpUrl("");
  11651. this.itemCount_ = 0;
  11652. },
  11653. mutationToDom: function () {
  11654. if (!this.itemCount_)
  11655. return null;
  11656. var container = document.createElement('mutation');
  11657. this.itemCount_ && container.setAttribute('aielse', 1);
  11658. return container;
  11659. },
  11660. domToMutation: function (a) {
  11661. this.itemCount_ = parseInt(a.getAttribute('aielse'), 10);
  11662. this.updateShape_();
  11663. },
  11664. decompose: function (a) {
  11665. var b = a.newBlock('ai_model_false_create_with_container');
  11666. b.initSvg();
  11667. var c = b.nextConnection
  11668. this.itemCount_ && (a = a.newBlock("ai_model_false_create_with_item"),
  11669. a.initSvg(),
  11670. c.connect(a.previousConnection));
  11671. console.log("this.elseCount_",a)
  11672. return b
  11673. },
  11674. compose: function (a) {
  11675. var b = a.nextConnection.targetBlock();
  11676. this.itemCount_ = 0;
  11677. var e = null;
  11678. if (b && b.type === "ai_model_false_create_with_item") {
  11679. this.itemCount_++;
  11680. e = b.statementConnection_;
  11681. b = b.nextConnection && b.nextConnection.targetBlock()
  11682. }
  11683. this.updateShape_()
  11684. Blockly.Mutator.reconnect(e, this, "AIELSE")
  11685. },
  11686. saveConnections: function (a) {
  11687. a = a.nextConnection.targetBlock();
  11688. if (a && a.type === "ai_model_false_create_with_item") {
  11689. e = this.getInput("AIELSE");
  11690. a.statementConnection_ = e && e.connection.targetConnection;
  11691. a = a.nextConnection && a.nextConnection.targetBlock()
  11692. }
  11693. },
  11694. updateShape_: function () {
  11695. this.getInput("AIELSE") && this.removeInput("AIELSE");
  11696. console.log("this.itemCount_",this.itemCount_)
  11697. this.itemCount_ && this.appendStatementInput("AIELSE").appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSE)
  11698. },
  11699. };
  11700. Blockly.Python['ai_model_customized_if_got_new'] = function (block) {
  11701. var statements_input = Blockly.Python.statementToCode(block, 'input');
  11702. var statements_input1 = Blockly.Python.statementToCode(block, 'AIELSE');
  11703. let allBlocks = block.workspace.getAllBlocks();
  11704. let global = ""
  11705. try {
  11706. global = allBlocks[0].workspace.variableList.toString()
  11707. }
  11708. catch (e) {
  11709. console.log(e)
  11710. }
  11711. Blockly.Python.addFunction("cardStatements", `def cardStatements(BOXESI):
  11712. ${global != "" ? `global ${global}` : "pass"}
  11713. ${statements_input}
  11714. `)
  11715. var code = `if len(BOXES) > 0:
  11716. for BOXESI in BOXES:
  11717. cardStatements(BOXESI)
  11718. else:
  11719. ${statements_input1 != "" ? statements_input1 : " pass"}
  11720. `
  11721. return code;
  11722. };
  11723. Blockly.Blocks['ai_model_customized_if_got'] = {
  11724. init: function () {
  11725. this.appendDummyInput()
  11726. .appendField(Blockly.Msg.ai_models_customized_if_got);
  11727. this.setNextStatement(true, null);
  11728. this.setPreviousStatement(true, null);
  11729. this.setColour("#ee783a");
  11730. this.setTooltip(Blockly.Msg.ai_models_customized_if_got);
  11731. this.setHelpUrl("");
  11732. }
  11733. };
  11734. Blockly.Python['ai_model_customized_if_got'] = function (block) {
  11735. Blockly.Python.addFunction("faceRecognizeShowResult", `def faceRecognizeShowResult():
  11736. global BOXES
  11737. if len(BOXES):
  11738. for BOXESI, BOXOBJ in enumerate(BOXES):
  11739. canvas.draw_string((BOXOBJ[0]),(BOXOBJ[1]), ("".join([str(x) for x in [Yolo.labels[PROBS[BOXESI][0]], ";", str(round((PROBS[BOXESI][1][PROBS[BOXESI][0]]*100), 2)) + str("%")]])), scale = 1, color = (255,0,0) , thickness = 1)
  11740. canvas.draw_rectangle((BOXOBJ[0]),(BOXOBJ[1]), (BOXOBJ[0])+(BOXOBJ[2]),(BOXOBJ[1])+ (BOXOBJ[3]), color=(255,0,0), thickness=1)
  11741. `)
  11742. var code = 'faceRecognizeShowResult()\n';
  11743. return code;
  11744. };
  11745. Blockly.Blocks['ai_model_customized_attribute'] = {
  11746. init: function () {
  11747. this.appendDummyInput()
  11748. .appendField(Blockly.Msg.ai_model_customized_attribute);
  11749. this.setOutput(true, null);
  11750. this.setColour("#ee783a");
  11751. this.setTooltip(Blockly.Msg.ai_model_customized_attribute_TOOLTIP);
  11752. this.setHelpUrl("");
  11753. }
  11754. };
  11755. Blockly.Python['ai_model_customized_attribute'] = function (block) {
  11756. var code = 'len(BOXES)';
  11757. return [code, Blockly.Python.ORDER_NONE];
  11758. };
  11759. /*
  11760. _ ___ ____ _
  11761. / \ |_ _| / ___| _ _ ___| |_ ___ _ __ ___
  11762. / _ \ | | \___ \| | | / __| __/ _ \ '_ ` _ \
  11763. / ___ \ | | ___) | |_| \__ \ || __/ | | | | |
  11764. /_/ \_\___| |____/ \__, |___/\__\___|_| |_| |_|
  11765. |___/
  11766. */
  11767. Blockly.Blocks['ai_system_off'] = {
  11768. init: function () {
  11769. this.appendDummyInput()
  11770. .appendField(Blockly.Msg.ai_system_off);
  11771. this.setPreviousStatement(true);
  11772. this.setNextStatement(true);
  11773. this.setColour("#22398e");
  11774. this.setTooltip(Blockly.Msg.ai_system_restart_TOOLTIP);
  11775. this.setHelpUrl("");
  11776. }
  11777. };
  11778. Blockly.Python.ai_system_off = function (block) {
  11779. Blockly.Python.definitions_['v831_import_os'] = `import os`
  11780. var _code = `os.system("poweroff")
  11781. `
  11782. return _code;
  11783. }
  11784. Blockly.Blocks['ai_system_restart'] = {
  11785. init: function () {
  11786. this.appendDummyInput()
  11787. .appendField(Blockly.Msg.ai_system_restart);
  11788. this.setPreviousStatement(true);
  11789. this.setNextStatement(true);
  11790. this.setColour("#22398e");
  11791. this.setTooltip(Blockly.Msg.ai_system_off_TOOLTIP);
  11792. this.setHelpUrl("");
  11793. }
  11794. };
  11795. Blockly.Python.ai_system_restart = function (block) {
  11796. Blockly.Python.definitions_['v831_import_os'] = `import os`
  11797. var _code = `os.system("reboot")
  11798. `
  11799. return _code;
  11800. }
  11801. Blockly.Blocks['ai_system_refresh'] = {
  11802. init: function () {
  11803. this.appendDummyInput()
  11804. .appendField(Blockly.Msg.ai_system_refresh);
  11805. this.setPreviousStatement(true);
  11806. this.setNextStatement(true);
  11807. this.setColour("#22398e");
  11808. this.setTooltip(Blockly.Msg.ai_system_refresh_TOOLTIP);
  11809. this.setHelpUrl("");
  11810. }
  11811. };
  11812. Blockly.Python.ai_system_refresh = function (block) {
  11813. Blockly.Python.definitions_['v831_import_os'] = `import os`
  11814. var _code = `os.system("sync")
  11815. `
  11816. return _code;
  11817. }
  11818. Blockly.Blocks['ai_system_volume'] = {
  11819. init: function () {
  11820. this.appendDummyInput()
  11821. .appendField(Blockly.Msg.system_voice)
  11822. .appendField(new Blockly.FieldNumber(25, 0, 100), "play_vol");
  11823. this.setPreviousStatement(true);
  11824. this.setNextStatement(true);
  11825. this.setColour("#22398e");
  11826. }
  11827. }
  11828. Blockly.Python.ai_system_volume = function (block) {
  11829. var number_play_vol = block.getFieldValue('play_vol');
  11830. Blockly.Python.definitions_['v831_import_os'] = `import os`
  11831. Blockly.Python.definitions_['systemVoice_numberMap'] = `def voice_numberMap(value):
  11832. valueScaled = float(value - 0) / float(100)
  11833. return int(valueScaled * 31)
  11834. `
  11835. var _code = `VOICENUMP = str(voice_numberMap(${number_play_vol}))
  11836. time.sleep(0.01)
  11837. SYSTEMVOICE = "amixer cset numid=8,iface=MIXER,name=\\"LINEOUT volume\\" "+ VOICENUMP+""
  11838. os.system(SYSTEMVOICE)
  11839. `
  11840. return _code;
  11841. }
  11842. Blockly.Blocks['ai_system_thread'] = {
  11843. init: function () {
  11844. this.appendDummyInput()
  11845. .appendField(Blockly.Msg.ai_system_thread_setting)
  11846. .appendField(new Blockly.FieldNumber(1, 1, 5), "thread")
  11847. this.appendStatementInput("input")
  11848. .setCheck(null)
  11849. .appendField(Blockly.Msg.basic_motion_statement_exec);
  11850. this.setColour("#22398e");
  11851. this.setTooltip(Blockly.Msg.ai_system_thread_setting);
  11852. this.setHelpUrl("");
  11853. }
  11854. };
  11855. Blockly.Python['ai_system_thread'] = function (block) {
  11856. var threadNum = block.getFieldValue('thread');
  11857. var statements_input = Blockly.Python.statementToCode(block, 'input');
  11858. Blockly.Python.definitions_['v831_import_threading'] = `import threading`
  11859. let allBlocks = block.workspace.getAllBlocks();
  11860. let global = ""
  11861. try {
  11862. global = allBlocks[0].workspace.variableList.toString()
  11863. }
  11864. catch (e) {
  11865. console.log(e)
  11866. }
  11867. Blockly.Python.definitions_['v831_import_thread_calsss_fun' + threadNum] = `def thread_calsss_fun${threadNum}():
  11868. ${global != "" ? global : ""}
  11869. ${statements_input}
  11870. CocoPiThread${threadNum} = threading.Thread(target=thread_calsss_fun${threadNum})
  11871. `
  11872. var code = ``;
  11873. return code;
  11874. };
  11875. Blockly.Blocks['ai_system_thread_create_with_container'] = {
  11876. init: function () {
  11877. this.setColour("#22398e");
  11878. this.appendDummyInput()
  11879. .appendField(Blockly.Msg.serialcomm_write_item);
  11880. this.appendStatementInput('STACK');
  11881. this.setTooltip('');
  11882. this.contextMenu = false;
  11883. }
  11884. };
  11885. Blockly.Blocks['ai_system_thread_data_create_with_item'] = {
  11886. init: function () {
  11887. this.setColour("#22398e");
  11888. this.appendDummyInput()
  11889. .appendField(Blockly.Msg.ai_systen_thread_val);
  11890. this.setPreviousStatement(true);
  11891. this.setNextStatement(true);
  11892. this.setTooltip('');
  11893. this.contextMenu = false;
  11894. }
  11895. };
  11896. Blockly.Blocks['ai_system_thread_set_setDaemon'] = {
  11897. init: function () {
  11898. this.appendDummyInput()
  11899. .appendField(Blockly.Msg.ai_system_thread_setting).appendField(new Blockly.FieldNumber(1, 1, 5), "thread")
  11900. .appendField(Blockly.Msg.digital_set_as).appendField(new Blockly.FieldDropdown([
  11901. [Blockly.Msg.ai_system_thread_setDaemon, "True"],
  11902. [Blockly.Msg.ai_system_thread_not_setDaemon, "False"]
  11903. ]), "setDaemon");
  11904. this.setPreviousStatement(true, null);
  11905. this.setNextStatement(true, null)
  11906. this.setColour("#22398e");
  11907. this.setTooltip(Blockly.Msg.ai_system_thread_TOOLTIP);
  11908. this.setHelpUrl("");
  11909. }
  11910. };
  11911. Blockly.Python['ai_system_thread_set_setDaemon'] = function (block) {
  11912. var threadNum = block.getFieldValue('thread');
  11913. var queue_value = block.getFieldValue('setDaemon');
  11914. var code = `CocoPiThread${threadNum}.setDaemon(${queue_value})
  11915. `;
  11916. return code;
  11917. };
  11918. Blockly.Blocks['ai_system_thread_set_join'] = {
  11919. init: function () {
  11920. this.appendDummyInput()
  11921. .appendField(Blockly.Msg.ai_system_thread_setting)
  11922. .appendField(new Blockly.FieldNumber(1, 1, 5), "thread")
  11923. .appendField(Blockly.Msg.ai_system_thread_set_join)
  11924. this.setPreviousStatement(true, null);
  11925. this.setNextStatement(true, null)
  11926. this.setColour("#22398e");
  11927. this.setTooltip(Blockly.Msg.ai_system_thread_TOOLTIP);
  11928. this.setHelpUrl("");
  11929. }
  11930. };
  11931. Blockly.Python['ai_system_thread_set_join'] = function (block) {
  11932. var threadNum = block.getFieldValue('thread');
  11933. var code = `CocoPiThread${threadNum}.join()
  11934. `;
  11935. return code;
  11936. };
  11937. Blockly.Blocks['ai_system_thread_start'] = {
  11938. init: function () {
  11939. this.appendDummyInput()
  11940. .appendField(Blockly.Msg.ai_system_thread).appendField(new Blockly.FieldNumber(1, 1, 5), "thread");
  11941. // this.appendStatementInput("input")
  11942. // .setCheck(null)
  11943. // .appendField(Blockly.Msg.basic_motion_statement_exec);
  11944. this.setPreviousStatement(true, null);
  11945. this.setNextStatement(true, null)
  11946. this.setColour("#22398e");
  11947. this.setTooltip(Blockly.Msg.ai_system_thread_TOOLTIP);
  11948. this.setHelpUrl("");
  11949. }
  11950. };
  11951. Blockly.Python['ai_system_thread_start'] = function (block) {
  11952. var threadNum = block.getFieldValue('thread');
  11953. // var statements_input = Blockly.Python.statementToCode(block, 'input');
  11954. var code = `CocoPiThread${threadNum}.start()
  11955. `;
  11956. return code;
  11957. };
  11958. Blockly.Blocks["ai_system_set_queue"] = {
  11959. init: function () {
  11960. this.appendDummyInput()
  11961. .appendField(Blockly.Msg.ai_systen_queue_aritem)
  11962. this.appendValueInput("queueVaritem")
  11963. .setCheck(null);
  11964. this.appendDummyInput().appendField(Blockly.Msg.ai_systen_queue_put)
  11965. .appendField(new Blockly.FieldDropdown([
  11966. ["1", "1"],
  11967. ["2", "2"],
  11968. ["3", "3"],
  11969. ["4", "4"],
  11970. ["5", "5"]
  11971. ]), "queue_value");
  11972. this.setPreviousStatement(true, null);
  11973. this.setNextStatement(true, null);
  11974. this.setColour("#22398e");
  11975. this.setTooltip("");
  11976. }
  11977. }
  11978. Blockly.Python['ai_system_set_queue'] = function (block) {
  11979. var variable_name = Blockly.Python.valueToCode(block, 'queueVaritem', Blockly.Python.ORDER_ATOMIC);
  11980. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  11981. var queue_value = block.getFieldValue('queue_value');
  11982. Blockly.Python.definitions_["v831_import_queue"] = `import queue`
  11983. Blockly.Python.definitions_["v831_import_queue_init" + queue_value] = `queue${queue_value} = queue.Queue()`
  11984. var code = `queue${queue_value}.put(${variable_name})
  11985. `;
  11986. return code;
  11987. }
  11988. Blockly.Blocks["ai_system_get_queue"] = {
  11989. init: function () {
  11990. this.appendDummyInput()
  11991. .appendField(Blockly.Msg.ai_systen_queue_get)
  11992. .appendField(new Blockly.FieldDropdown([
  11993. ["1", "1"],
  11994. ["2", "2"],
  11995. ["3", "3"],
  11996. ["4", "4"],
  11997. ["5", "5"]
  11998. ]), "queue_value")
  11999. .appendField(Blockly.Msg.ai_systen_queue_value);
  12000. this.setOutput(true, null);
  12001. this.setColour("#22398e");
  12002. this.setTooltip("");
  12003. }
  12004. }
  12005. Blockly.Python['ai_system_get_queue'] = function (block) {
  12006. var queue_value = block.getFieldValue('queue_value');
  12007. var code = `queue${queue_value}.get()`;
  12008. return [code, Blockly.Python.ORDER_NONE];
  12009. }
  12010. // 拼音识别
  12011. Blockly.Blocks['speech_recognition_phonetic_to_pinyin_setup'] = {
  12012. init: function () {
  12013. this.appendDummyInput()
  12014. .appendField(new Blockly.FieldImage("blockly/media/speech_recognition_header_recognition_new.png", 130, 60, { alt: "*", flipRtl: "FALSE" }));
  12015. this.appendDummyInput()
  12016. .appendField(Blockly.Msg.ai_speech_recognition_phonetic_to_pinyin_setup);
  12017. this.setPreviousStatement(true, null);
  12018. this.setNextStatement(true, null);
  12019. this.setColour("#ee783a");
  12020. this.setTooltip(Blockly.Msg.speech_recognition_phonetic_to_pinyin_setup_TOOLTIP);
  12021. this.setHelpUrl("");
  12022. }
  12023. };
  12024. Blockly.Python['speech_recognition_phonetic_to_pinyin_setup'] = function (block) {
  12025. Blockly.Python.definitions_['v831_import_display'] = `from maix import display`;
  12026. Blockly.Python.definitions_['v831_import_image'] = `from maix import image`;
  12027. Blockly.Python.definitions_['v831_import_camera'] = `from maix import camera`;
  12028. Blockly.Python.definitions_["v831_import_maix_speech"] = `import _maix_speech`
  12029. Blockly.Python.definitions_.speech_recognition_phonetic_to_pinyin_setup_def = `asr = _maix_speech.Asr()
  12030. asr.open("hw:0,0", "/root/preset/drivers/ASR/CocoPi_Asr_Model/CocoPi_Asr_Model_V2.1")
  12031. asr.clear()
  12032. asr_kws_result=[0,0] #获取识别结果及识别概率
  12033. similars = [
  12034. ["pai3", "ai4"],
  12035. ["le4", "luo4"],
  12036. ]
  12037. def asr_kws(keys):
  12038. global asr_kws_result
  12039. tmp = max(keys)
  12040. asr_kws_result[0] = keys.index(tmp)
  12041. asr_kws_result[1] = round(tmp,2)
  12042. `
  12043. var code = "";
  12044. return code;
  12045. };
  12046. Blockly.Blocks.set_pinyin_dict_create_with_items_insert = {
  12047. init: function () {
  12048. this.setColour("#ee783a");
  12049. this.appendDummyInput("").appendField(new Blockly.FieldLabel(Blockly.Msg.ai_speech_recognition_setup_pinyin), "TIP");
  12050. this.itemCount_ = 3;
  12051. this.updateShape_();
  12052. this.setOutput(!0);
  12053. this.setMutator(new Blockly.Mutator(["set_pinyin_dict_create_with_item"]));
  12054. this.setTooltip(Blockly.Msg.ai_speech_recognition_setup_pinyin_and_tone)
  12055. },
  12056. mutationToDom: function () {
  12057. var a = document.createElement("mutation");
  12058. a.setAttribute("items", this.itemCount_);
  12059. return a
  12060. },
  12061. domToMutation: function (a) {
  12062. this.itemCount_ = parseInt(a.getAttribute("items"), 10);
  12063. this.updateShape_()
  12064. },
  12065. decompose: function (a) {
  12066. var b = a.newBlock("set_pinyin_dict_create_with_container");
  12067. b.initSvg();
  12068. for (var c = b.getInput("STACK").connection, e = 0; e < this.itemCount_; e++) {
  12069. var d = a.newBlock("set_pinyin_dict_create_with_item");
  12070. d.initSvg();
  12071. c.connect(d.previousConnection);
  12072. c = d.nextConnection
  12073. }
  12074. return b
  12075. },
  12076. compose: function (a) {
  12077. a = a.getInputTargetBlock("STACK");
  12078. for (var b = [], c = 0; a;)
  12079. b[c] = a.valueConnection_,
  12080. a = a.nextConnection && a.nextConnection.targetBlock(),
  12081. c++;
  12082. this.itemCount_ = c;
  12083. this.updateShape_();
  12084. for (c = 0; c < this.itemCount_; c++)
  12085. b[c] && this.getInput("ADD" + c).connection.connect(b[c])
  12086. },
  12087. saveConnections: function (a) {
  12088. a = a.getInputTargetBlock("STACK");
  12089. for (var b = 0; a;) {
  12090. var c = this.getInput("ADD" + b);
  12091. a.valueConnection_ = c && c.connection;
  12092. b++;
  12093. a = a.nextConnection && a.nextConnection.targetBlock()
  12094. }
  12095. },
  12096. updateShape_: function () {
  12097. this.getInput("EMPTY") && this.removeInput("EMPTY");
  12098. for (var a = [], b = 0; this.getInput("ADD" + b); b++)
  12099. a.push(this.getFieldValue("KEY" + b)),
  12100. this.removeInput("ADD" + b);
  12101. if (0 == this.itemCount_)
  12102. this.getField("TIP").setText(Blockly.Msg.ai_speech_recognition_setup_pinyin);
  12103. else
  12104. for (this.getField("TIP").setText(Blockly.Msg.ai_speech_recognition_setup_pinyin),
  12105. b = 0; b < this.itemCount_; b++)
  12106. this.appendInput_(Blockly.DUMMY_INPUT, "ADD" + b).appendField(new Blockly.FieldTextInput(a.length > b ? a[b] : 0 == b ? "key_0" : "key_" + b), "KEY" + b).appendField(":")
  12107. .appendField(Blockly.Msg.ai_speech_recognition_setup_tone).appendField(new Blockly.FieldDropdown([
  12108. ["1", "1"],
  12109. ["2", "2"],
  12110. ["3", "3"],
  12111. ["4", "4"],
  12112. ]), "toneADD" + b + b)
  12113. },
  12114. getVars: function () {
  12115. return [this.getFieldValue("VAR")]
  12116. },
  12117. renameVar: function (a, b) {
  12118. Blockly.Names.equals(a, this.getFieldValue("VAR")) && this.setTitleValue(b, "VAR")
  12119. }
  12120. };
  12121. Blockly.Blocks.set_pinyin_dict_create_with_item = {
  12122. init: function () {
  12123. this.setColour("#ee783a");
  12124. this.appendDummyInput().appendField(Blockly.Msg.ai_speech_recognition_setup_pinyin);
  12125. this.setPreviousStatement(!0);
  12126. this.setNextStatement(!0);
  12127. this.setTooltip(Blockly.Msg.ai_speech_recognition_setup_pinyin);
  12128. this.contextMenu = !1
  12129. }
  12130. };
  12131. Blockly.Blocks.set_pinyin_dict_create_with_container = {
  12132. init: function () {
  12133. this.setColour("#ee783a");
  12134. this.appendDummyInput().appendField(Blockly.Msg.robot_dog_servo_execution);
  12135. this.appendStatementInput("STACK");
  12136. this.setTooltip(Blockly.Msg.robot_dog_servo_execution);
  12137. this.contextMenu = !1
  12138. }
  12139. };
  12140. Blockly.Python.set_pinyin_dict_create_with_items_insert = function (block) {
  12141. for (var a = Array(this.itemCount_), b = 0; b < this.itemCount_; b++) {
  12142. var c = this.getFieldValue("KEY" + b);
  12143. a[b] = c + block.getFieldValue("toneADD" + b + b);
  12144. }
  12145. console.log(a.join(" "))
  12146. a = `"${a.join(" ")}"`;
  12147. return [a, Blockly.Python.ORDER_ATOMIC]
  12148. };
  12149. Blockly.Blocks['speech_recognition_phonetic_to_pinyin_set_param'] = {
  12150. init: function () {
  12151. this.appendDummyInput()
  12152. .appendField(Blockly.Msg.ai_speech_recognition_phonetic_to_pinyin_set_param);
  12153. // this.appendValueInput("data")
  12154. // .setCheck(null)
  12155. // .appendField(Blockly.Msg.ai_speech_recognition_setup_pinyin);
  12156. this.setPreviousStatement(true, null);
  12157. this.setNextStatement(true, null);
  12158. this.setInputsInline(false);
  12159. this.setColour("#ee783a");
  12160. this.pinyinitemCount_ = 1
  12161. this.updateShape_();
  12162. this.setMutator(new Blockly.Mutator(["v831_to_pinyin_set_param"]));
  12163. this.setTooltip(Blockly.Msg.ai_speech_recognition_phonetic_to_pinyin_set_param_TOOLTIP);
  12164. this.setHelpUrl("");
  12165. },
  12166. mutationToDom: function () {
  12167. var a = document.createElement("mutation");
  12168. a.setAttribute("items", this.pinyinitemCount_);
  12169. return a
  12170. },
  12171. domToMutation: function (a) {
  12172. this.pinyinitemCount_ = parseInt(a.getAttribute("items"), 10);
  12173. this.updateShape_()
  12174. },
  12175. decompose: function (a) {
  12176. var b = a.newBlock("v831_to_pinyin_set_param_container");
  12177. b.initSvg();
  12178. for (var c = b.getInput("STACK").connection, e = 0; e < this.pinyinitemCount_; e++) {
  12179. var d = a.newBlock("v831_to_pinyin_set_param");
  12180. d.initSvg();
  12181. c.connect(d.previousConnection);
  12182. c = d.nextConnection
  12183. }
  12184. return b
  12185. },
  12186. compose: function (a) {
  12187. var b = a.getInputTargetBlock("STACK");
  12188. for (a = []; b;)
  12189. a.push(b.valueConnection_),
  12190. b = b.nextConnection && b.nextConnection.targetBlock();
  12191. for (b = 0; b < this.pinyinitemCount_; b++) {
  12192. var c = this.getInput("PINYIN" + b).connection.targetConnection;
  12193. c && -1 == a.indexOf(c) && c.disconnect()
  12194. }
  12195. this.pinyinitemCount_ = a.length;
  12196. this.updateShape_();
  12197. for (b = 0; b < this.pinyinitemCount_; b++)
  12198. Blockly.Mutator.reconnect(a[b], this, "PINYIN" + b)
  12199. },
  12200. saveConnections: function (a) {
  12201. a = a.getInputTargetBlock("STACK");
  12202. for (var b = 0; a;) {
  12203. var c = this.getInput("PINYIN" + b);
  12204. a.valueConnection_ = c && c.connection.targetConnection;
  12205. b++;
  12206. a = a.nextConnection && a.nextConnection.targetBlock()
  12207. }
  12208. },
  12209. updateShape_: function () {
  12210. // this.itemCount_ && this.getInput("EMPTY") ? this.removeInput("EMPTY") : this.itemCount_ || this.getInput("EMPTY") || this.appendDummyInput("EMPTY").appendField(Blockly.Msg.TUPLE_CREATE_EMPTY_TITLE);
  12211. for (var a = 0; a < this.pinyinitemCount_; a++)
  12212. if (!this.getInput("PINYIN" + a)) {
  12213. var b = this.appendValueInput("PINYIN" + a);
  12214. 0 == a && b.appendField(Blockly.Msg.ai_speech_recognition_setup_pinyin_and_tone)
  12215. }
  12216. for (; this.getInput("PINYIN" + a);)
  12217. this.removeInput("PINYIN" + a),
  12218. a++
  12219. }
  12220. };
  12221. Blockly.Blocks['v831_to_pinyin_set_param'] = {
  12222. init: function () {
  12223. this.appendDummyInput()
  12224. .appendField(Blockly.Msg.ai_speech_recognition_setup_pinyin_and_tone);
  12225. this.setPreviousStatement(true);
  12226. this.setNextStatement(true);
  12227. this.setColour("#ee783a")
  12228. this.setTooltip('');
  12229. this.contextMenu = false;
  12230. }
  12231. };
  12232. Blockly.Blocks['v831_to_pinyin_set_param_container'] = {
  12233. init: function () {
  12234. this.setColour("#ee783a");
  12235. this.appendDummyInput()
  12236. .appendField(Blockly.Msg.ARD_SERIAL_SETUP);
  12237. this.appendStatementInput('STACK');
  12238. this.setTooltip('');
  12239. this.contextMenu = false;
  12240. }
  12241. };
  12242. Blockly.Python['speech_recognition_phonetic_to_pinyin_set_param'] = function (block) {
  12243. // var data = Blockly.Python.valueToCode(block, 'data', Blockly.Python.ORDER_ATOMIC);
  12244. Blockly.Python.definitions_['v831_import_display'] = `from maix import display`;
  12245. Blockly.Python.definitions_['v831_import_image'] = `from maix import image`;
  12246. Blockly.Python.definitions_['v831_import_camera'] = `from maix import camera`;
  12247. Blockly.Python.definitions_["v831_import_maix_speech"] = `import _maix_speech`
  12248. var a = Array(this.pinyinitemCount_)
  12249. for (let b = 0; b < this.pinyinitemCount_; b++) {
  12250. var c = Blockly.Python.valueToCode(this, "PINYIN" + b, Blockly.Python.ORDER_NONE)
  12251. a[b] = c.slice(1, -1).replaceAll(",", "")
  12252. }
  12253. a = a.map(r => [r, 0.1])
  12254. Blockly.Python.definitions_.speech_recognition_phonetic_to_pinyin_setup_def = `asr = _maix_speech.Asr()
  12255. asr.open("hw:0,0", "/root/preset/drivers/ASR/CocoPi_Asr_Model/CocoPi_Asr_Model_V2.1")
  12256. asr.clear()
  12257. asr_kws_result=[0,0] #获取识别结果及识别概率
  12258. similars = [
  12259. ["pai3", "ai4"],
  12260. ["le4", "luo4"],
  12261. ]
  12262. def asr_kws(keys):
  12263. global asr_kws_result
  12264. tmp = max(keys)
  12265. asr_kws_result[0] = keys.index(tmp)
  12266. asr_kws_result[1] = round(tmp,2)
  12267. `
  12268. // Blockly.Python.definitions_["set_keywords_function"] = `def SetKeywords(data):
  12269. // dataList = []
  12270. // for obj in data:
  12271. // dataList.append([obj,0.1])
  12272. // print(dataList)
  12273. // return dataList
  12274. // `
  12275. var code = `keywords = ${JSON.stringify(a)}
  12276. asr.set_kws(keywords, similars, asr_kws)
  12277. `;
  12278. return code;
  12279. };
  12280. Blockly.Blocks['speech_recognition_set_pinyin_tone'] = {
  12281. init: function () {
  12282. this.appendDummyInput()
  12283. .appendField("声调").appendField(new Blockly.FieldDropdown([
  12284. ["1", "1"],
  12285. ["2", "2"],
  12286. ["3", "3"],
  12287. ["4", "4"],
  12288. ]), "tone");
  12289. this.setOutput(true, null);
  12290. this.setColour("#ee783a");
  12291. this.setTooltip(Blockly.Msg.ai_speech_recognition_phonetic_to_pinyin_get_result_TOOLTIP);
  12292. this.setHelpUrl("");
  12293. }
  12294. };
  12295. Blockly.Python['speech_recognition_set_pinyin_tone'] = function (block) {
  12296. let tone = block.getFieldValue('tone');
  12297. console.log(tone)
  12298. Blockly.Python.definitions_.speech_recognition_phonetic_to_pinyin_start_recognition_def = `def getAsrResult():
  12299. global asr_kws_result,keywords
  12300. if(asr_kws_result[1]!=0):
  12301. asr_kws_result_list = keywords[asr_kws_result[0]][0]
  12302. return asr_kws_result_list
  12303. `
  12304. var code = tone;
  12305. return [code, Blockly.Python.ORDER_NONE];
  12306. };
  12307. Blockly.Blocks['speech_recognition_phonetic_to_pinyin_start_recognition'] = {
  12308. init: function () {
  12309. this.appendDummyInput()
  12310. .appendField(Blockly.Msg.speech_recognition_phonetic_to_pinyin_start_recognition);
  12311. this.setPreviousStatement(true, null);
  12312. this.setNextStatement(true, null);
  12313. this.setColour("#ee783a");
  12314. this.setTooltip(Blockly.Msg.speech_recognition_phonetic_to_pinyin_start_recognition_TOOLTIP);
  12315. this.setHelpUrl("");
  12316. }
  12317. };
  12318. Blockly.Python['speech_recognition_phonetic_to_pinyin_start_recognition'] = function (block) {
  12319. var code = "asr.run(1)\n";
  12320. return code;
  12321. };
  12322. Blockly.Blocks['speech_recognition_phonetic_to_pinyin_stop_recognition'] = {
  12323. init: function () {
  12324. this.appendDummyInput()
  12325. .appendField(Blockly.Msg.speech_recognition_phonetic_to_pinyin_stop_recognition);
  12326. this.setPreviousStatement(true, null);
  12327. this.setNextStatement(true, null);
  12328. this.setColour("#ee783a");
  12329. this.setTooltip(Blockly.Msg.speech_recognition_phonetic_to_pinyin_stop_recognition_TOOLTIP);
  12330. this.setHelpUrl("");
  12331. }
  12332. };
  12333. Blockly.Python['speech_recognition_phonetic_to_pinyin_stop_recognition'] = function (block) {
  12334. var code = "asr.exit()\n";
  12335. return code;
  12336. };
  12337. Blockly.Blocks['speech_recognition_phonetic_to_pinyin_pause_recognition'] = {
  12338. init: function () {
  12339. this.appendDummyInput()
  12340. .appendField(Blockly.Msg.speech_recognition_phonetic_to_pinyin_pause_recognition);
  12341. this.setPreviousStatement(true, null);
  12342. this.setNextStatement(true, null);
  12343. this.setColour("#ee783a");
  12344. this.setTooltip(Blockly.Msg.speech_recognition_phonetic_to_pinyin_pause_recognition_TOOLTIP);
  12345. this.setHelpUrl("");
  12346. }
  12347. };
  12348. Blockly.Python['speech_recognition_phonetic_to_pinyin_pause_recognition'] = function (block) {
  12349. var code = "asr.clear()\n";
  12350. return code;
  12351. };
  12352. Blockly.Blocks['speech_recognition_phonetic_to_pinyin_get_result'] = {
  12353. init: function () {
  12354. this.appendDummyInput()
  12355. .appendField(Blockly.Msg.ai_speech_recognition_phonetic_to_pinyin_get_result);
  12356. this.setOutput(true, null);
  12357. this.setColour("#ee783a");
  12358. this.setTooltip(Blockly.Msg.ai_speech_recognition_phonetic_to_pinyin_get_result_TOOLTIP);
  12359. this.setHelpUrl("");
  12360. }
  12361. };
  12362. Blockly.Python['speech_recognition_phonetic_to_pinyin_get_result'] = function (block) {
  12363. Blockly.Python.definitions_.speech_recognition_phonetic_to_pinyin_start_recognition_def = `def getAsrResult():
  12364. global asr_kws_result,keywords
  12365. if(asr_kws_result[1]!=0):
  12366. asr_kws_result_list = keywords[asr_kws_result[0]][0]
  12367. return asr_kws_result_list
  12368. `
  12369. var code = 'getAsrResult()';
  12370. return [code, Blockly.Python.ORDER_NONE];
  12371. };
  12372. // numpy + mnist
  12373. Blockly.Blocks['Numpy_Neural_Network_init'] = {
  12374. init: function () {
  12375. this.appendDummyInput()
  12376. .appendField(Blockly.Msg.numpy_mnist_model_init);
  12377. this.setPreviousStatement(true, null);
  12378. this.setNextStatement(true, null);
  12379. this.setColour("#ee783a");
  12380. this.setTooltip(Blockly.Msg.numpy_mnist_model_init);
  12381. this.setHelpUrl("");
  12382. }
  12383. };
  12384. Blockly.Python['Numpy_Neural_Network_init'] = function (block) {
  12385. Blockly.Python.definitions_['v831_import_numpy_as_np'] = `import numpy as np`;
  12386. Blockly.Python.definitions_["v831_import_pickle"] = `import pickle`
  12387. Blockly.Python.definitions_["v831_NeuralNetwork_init"] = `def sigmoid(x):
  12388. return 1/(1+np.exp(-x))
  12389. def grad(x):
  12390. return x*(1-x)
  12391. class NeuralNetwork:
  12392. """
  12393. 三层全连接前馈神经网络
  12394. """
  12395. def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate, active_function=sigmoid, gradient=grad, lambda_=0.1):
  12396. """
  12397. :param inputnodes: 输入层结点数
  12398. :param hiddennodes: 隐藏层节点数
  12399. :param outputnodes: 输出层节点数
  12400. :param learningrate: 学习率
  12401. :param active_function: 激活函数
  12402. :param gradient: 激活函数的导数
  12403. :param lambda_: L2正则化系数
  12404. """
  12405. self.inputnodes = inputnodes
  12406. self.hiddennodes = hiddennodes
  12407. self.outputnodes = outputnodes
  12408. self.learningrate = learningrate
  12409. self.active_function = active_function
  12410. self.gradient = gradient
  12411. self.lambda_ = lambda_
  12412. # 权值矩阵
  12413. self.weights_i_h = np.random.rand(
  12414. self.hiddennodes, self.inputnodes) - 0.5
  12415. self.weights_h_o = np.random.rand(
  12416. self.outputnodes, self.hiddennodes) - 0.5
  12417. def train_sgd(self, x, y):
  12418. """梯度下降训练"""
  12419. TRAINX = np.array(x).reshape(-1, 1)
  12420. target = np.zeros((self.outputnodes, 1)) + 0.01
  12421. target[y, 0] = 0.99
  12422. hiddeninputs = np.dot(self.weights_i_h, TRAINX)
  12423. hiddenoutputs = self.active_function(hiddeninputs)
  12424. outputinputs = np.dot(self.weights_h_o, hiddenoutputs)
  12425. final_outputs = self.active_function(outputinputs)
  12426. error = target - final_outputs
  12427. hidden_error = np.dot(self.weights_h_o.transpose(), error)
  12428. self.weights_h_o += self.learningrate * error * \
  12429. np.dot(self.gradient(final_outputs), hiddenoutputs.transpose())
  12430. self.weights_i_h += self.learningrate * hidden_error * \
  12431. np.dot(self.gradient(hiddenoutputs), TRAINX.transpose())
  12432. def fit(self, TRAINX, targets):
  12433. TRAINX = np.array(TRAINX)
  12434. for i in range(TRAINX.shape[0]):
  12435. self.train_sgd(TRAINX[i], targets[i])
  12436. def query(self, inputs, debug=False):
  12437. """单个值预测"""
  12438. inputs = np.array(inputs).reshape(-1, 1)
  12439. hidden_input = np.dot(self.weights_i_h, inputs)
  12440. hidden_output = self.active_function(hidden_input)
  12441. output_input = np.dot(self.weights_h_o, hidden_output)
  12442. final_output = self.active_function(output_input)
  12443. if debug:
  12444. print("predict: ", final_output)
  12445. return np.argmax(final_output)
  12446. def predict(self, inputs):
  12447. """批量预测"""
  12448. res = []
  12449. for x in inputs:
  12450. res.append(self.query(x))
  12451. return res
  12452. def __str__(self):
  12453. return "NeuralNetwork: \\ninput_nodes = {0}, hidden_nodes = {1}, \\noutputnodes = {2}, learningrate = {3}".format(self.inputnodes, self.hiddennodes, self.outputnodes, self.learningrate)
  12454. # 用测试数据测试
  12455. def accuracy(y_true, y_pred):
  12456. """准确度"""
  12457. y_true = np.array(y_true)
  12458. y_pred = np.array(y_pred)
  12459. return sum(y_true == y_pred)/y_true.shape[0]
  12460. # 用全部数据进行训练
  12461. def get_data():
  12462. # train_df = np.loadtxt("mnist_train.csv", delimiter=",", dtype=str)
  12463. # test_df = np.loadtxt("mnist_test.csv", delimiter=",", dtype=str)
  12464. global train_df, test_df
  12465. print(train_df.shape)
  12466. print(test_df.shape)
  12467. train_data = train_df.astype("int")
  12468. TRAINX = train_data[:, 1:]
  12469. TRAINY = train_data[:, 0]
  12470. TRAINX = TRAINX / 255 * 0.99 + 0.01
  12471. test_data = test_df.astype("int")
  12472. TESTX = test_data[:, 1:]
  12473. TESTY = test_data[:, 0]
  12474. TESTX = TESTX / 255 * 0.99 + 0.01
  12475. return TRAINX, TRAINY, TESTX, TESTY
  12476. `
  12477. var code = '';
  12478. return code;
  12479. };
  12480. Blockly.Blocks['Numpy_Neural_Network_load'] = {
  12481. init: function () {
  12482. this.appendDummyInput()
  12483. .appendField("numpy + mnist 训练模型加载");
  12484. this.setPreviousStatement(true, null);
  12485. this.setNextStatement(true, null);
  12486. this.setColour("#ee783a");
  12487. this.setTooltip("numpy + mnist 训练模型加载");
  12488. this.setHelpUrl("");
  12489. }
  12490. };
  12491. Blockly.Python['Numpy_Neural_Network_load'] = function (block) {
  12492. Blockly.Python.definitions_['v831_import_numpy_as_np'] = `import numpy as np`;
  12493. Blockly.Python.definitions_["v831_import_pickle"] = `import pickle`
  12494. Blockly.Python.definitions_["v831_NeuralNetwork_init"] = `def sigmoid(x):
  12495. return 1/(1+np.exp(-x))
  12496. def grad(x):
  12497. return x*(1-x)
  12498. class NeuralNetwork:
  12499. """
  12500. 三层全连接前馈神经网络
  12501. """
  12502. def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate, active_function=sigmoid, gradient=grad, lambda_=0.1):
  12503. """
  12504. :param inputnodes: 输入层结点数
  12505. :param hiddennodes: 隐藏层节点数
  12506. :param outputnodes: 输出层节点数
  12507. :param learningrate: 学习率
  12508. :param active_function: 激活函数
  12509. :param gradient: 激活函数的导数
  12510. :param lambda_: L2正则化系数
  12511. """
  12512. self.inputnodes = inputnodes
  12513. self.hiddennodes = hiddennodes
  12514. self.outputnodes = outputnodes
  12515. self.learningrate = learningrate
  12516. self.active_function = active_function
  12517. self.gradient = gradient
  12518. self.lambda_ = lambda_
  12519. # 权值矩阵
  12520. self.weights_i_h = np.random.rand(
  12521. self.hiddennodes, self.inputnodes) - 0.5
  12522. self.weights_h_o = np.random.rand(
  12523. self.outputnodes, self.hiddennodes) - 0.5
  12524. def train_sgd(self, x, y):
  12525. """梯度下降训练"""
  12526. TRAINX = np.array(x).reshape(-1, 1)
  12527. target = np.zeros((self.outputnodes, 1)) + 0.01
  12528. target[y, 0] = 0.99
  12529. hiddeninputs = np.dot(self.weights_i_h, TRAINX)
  12530. hiddenoutputs = self.active_function(hiddeninputs)
  12531. outputinputs = np.dot(self.weights_h_o, hiddenoutputs)
  12532. final_outputs = self.active_function(outputinputs)
  12533. error = target - final_outputs
  12534. hidden_error = np.dot(self.weights_h_o.transpose(), error)
  12535. self.weights_h_o += self.learningrate * error * \
  12536. np.dot(self.gradient(final_outputs), hiddenoutputs.transpose())
  12537. self.weights_i_h += self.learningrate * hidden_error * \
  12538. np.dot(self.gradient(hiddenoutputs), TRAINX.transpose())
  12539. def fit(self, TRAINX, targets):
  12540. TRAINX = np.array(TRAINX)
  12541. for i in range(TRAINX.shape[0]):
  12542. self.train_sgd(TRAINX[i], targets[i])
  12543. def query(self, inputs, debug=False):
  12544. """单个值预测"""
  12545. inputs = np.array(inputs).reshape(-1, 1)
  12546. hidden_input = np.dot(self.weights_i_h, inputs)
  12547. hidden_output = self.active_function(hidden_input)
  12548. output_input = np.dot(self.weights_h_o, hidden_output)
  12549. final_output = self.active_function(output_input)
  12550. if debug:
  12551. print("predict: ", final_output)
  12552. return np.argmax(final_output)
  12553. def predict(self, inputs):
  12554. """批量预测"""
  12555. res = []
  12556. for x in inputs:
  12557. res.append(self.query(x))
  12558. return res
  12559. def __str__(self):
  12560. return "NeuralNetwork: \\ninput_nodes = {0}, hidden_nodes = {1}, \\noutputnodes = {2}, learningrate = {3}".format(self.inputnodes, self.hiddennodes, self.outputnodes, self.learningrate)
  12561. # 用测试数据测试
  12562. def accuracy(y_true, y_pred):
  12563. """准确度"""
  12564. y_true = np.array(y_true)
  12565. y_pred = np.array(y_pred)
  12566. return sum(y_true == y_pred)/y_true.shape[0]
  12567. # 用全部数据进行训练
  12568. def get_data():
  12569. # train_df = np.loadtxt("mnist_train.csv", delimiter=",", dtype=str)
  12570. # test_df = np.loadtxt("mnist_test.csv", delimiter=",", dtype=str)
  12571. global train_df, test_df
  12572. print(train_df.shape)
  12573. print(test_df.shape)
  12574. train_data = train_df.astype("int")
  12575. TRAINX = train_data[:, 1:]
  12576. TRAINY = train_data[:, 0]
  12577. TRAINX = TRAINX / 255 * 0.99 + 0.01
  12578. test_data = test_df.astype("int")
  12579. TESTX = test_data[:, 1:]
  12580. TESTY = test_data[:, 0]
  12581. TESTX = TESTX / 255 * 0.99 + 0.01
  12582. return TRAINX, TRAINY, TESTX, TESTY
  12583. `
  12584. var code = `#构建神经网络训练服务
  12585. TRAINX, TRAINY, TESTX, TESTY = get_data()
  12586. NN = NeuralNetwork(784, 100, 10, 0.3)
  12587. NN.fit(TRAINX, TRAINY)
  12588. y_pred = NN.predict(TESTX)
  12589. `;
  12590. return code;
  12591. };
  12592. Blockly.Blocks['Numpy_Neural_Network_LOading_test_train'] = {
  12593. init: function () {
  12594. this.appendDummyInput()
  12595. .appendField(Blockly.Msg.numpy_mnist_loading_train_and_test_set);
  12596. this.appendValueInput("class_input")
  12597. .setCheck(null)
  12598. .appendField(Blockly.Msg.numpy_mnist_train_set_url);
  12599. this.appendValueInput("class_input1")
  12600. .setCheck(null)
  12601. .appendField(Blockly.Msg.numpy_mnist_test_set_url);
  12602. this.setPreviousStatement(true, null);
  12603. this.setNextStatement(true, null);
  12604. this.setColour("#ee783a");
  12605. this.setTooltip(Blockly.Msg.numpy_mnist_loading_train_and_test_set);
  12606. this.setHelpUrl("");
  12607. }
  12608. };
  12609. Blockly.Python['Numpy_Neural_Network_LOading_test_train'] = function (block) {
  12610. var trainUrl = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  12611. var testUrl = Blockly.Python.valueToCode(block, 'class_input1', Blockly.Python.ORDER_ATOMIC);
  12612. // TODO: Assemble Python into code variable.
  12613. var code = `test_df = np.loadtxt(${testUrl}, delimiter=",", dtype=str)
  12614. train_df = np.loadtxt(${trainUrl}, delimiter=",", dtype=str)
  12615. `;
  12616. // TODO: Change ORDER_NONE to the correct strength.
  12617. return code;
  12618. };
  12619. Blockly.Blocks['Numpy_Neural_Network_numpy_isStart'] = {
  12620. init: function () {
  12621. this.appendDummyInput()
  12622. .appendField(Blockly.Msg.numpy_mnist_is_training_start);
  12623. this.setOutput(true, null);
  12624. this.setColour("#ee783a");
  12625. this.setTooltip(Blockly.Msg.numpy_mnist_is_training_start);
  12626. this.setHelpUrl("");
  12627. }
  12628. };
  12629. Blockly.Python['Numpy_Neural_Network_numpy_isStart'] = function (block) {
  12630. // TODO: Assemble Python into code variable.
  12631. var code = `not os.path.exists("/usr/lib/python3.8/site-packages/numpy/random/__init__.py")`;
  12632. // TODO: Change ORDER_NONE to the correct strength.
  12633. return [code, Blockly.Python.ORDER_NONE];
  12634. };
  12635. Blockly.Blocks['Numpy_Neural_Network_setting_training_argument'] = {
  12636. init: function () {
  12637. this.appendDummyInput()
  12638. .appendField(Blockly.Msg.numpy_mnist_setting_training_argument);
  12639. this.appendValueInput("class_input")
  12640. .setCheck(null)
  12641. .appendField(Blockly.Msg.numpy_mnist_input_trait_dimensionality);
  12642. this.appendValueInput("class_input1")
  12643. .setCheck(null)
  12644. .appendField(Blockly.Msg.numpy_mnist_setting_training_argument_number_node);
  12645. this.appendValueInput("class_input2")
  12646. .setCheck(null)
  12647. .appendField(Blockly.Msg.numpy_mnist_output_dimensionality);
  12648. this.appendValueInput("class_input3")
  12649. .setCheck(null)
  12650. .appendField(Blockly.Msg.numpy_mnist_setting_training_argument_learning_rate);
  12651. this.appendValueInput("class_input4")
  12652. .setCheck(null)
  12653. .appendField(Blockly.Msg.numpy_mnist_setting_training_argument_frequency);
  12654. this.appendValueInput("class_input5")
  12655. .setCheck(null)
  12656. .appendField(Blockly.Msg.VIDEO_S_S);
  12657. this.setPreviousStatement(true, null);
  12658. this.setNextStatement(true, null);
  12659. this.setColour("#ee783a");
  12660. this.setTooltip(Blockly.Msg.numpy_mnist_setting_training_argument);
  12661. this.setHelpUrl("");
  12662. }
  12663. };
  12664. Blockly.Python['Numpy_Neural_Network_setting_training_argument'] = function (block) {
  12665. var class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  12666. var class_input1 = Blockly.Python.valueToCode(block, 'class_input1', Blockly.Python.ORDER_ATOMIC);
  12667. var class_input2 = Blockly.Python.valueToCode(block, 'class_input2', Blockly.Python.ORDER_ATOMIC);
  12668. var class_input3 = Blockly.Python.valueToCode(block, 'class_input3', Blockly.Python.ORDER_ATOMIC);
  12669. var class_input4 = Blockly.Python.valueToCode(block, 'class_input4', Blockly.Python.ORDER_ATOMIC);
  12670. var class_input5 = Blockly.Python.valueToCode(block, 'class_input5', Blockly.Python.ORDER_ATOMIC);
  12671. // TODO: Assemble Python into code variable.
  12672. console.log(Number(class_input.slice(1, -1)), class_input1, class_input2, class_input3)
  12673. var code = `TRAINX, TRAINY, TESTX, TESTY = get_data()
  12674. NN = NeuralNetwork(${Number(class_input.slice(1, -1))}, ${Number(class_input1.slice(1, -1))}, ${Number(class_input2.slice(1, -1))}, ${Number(class_input3.slice(1, -1))})
  12675. #开启神经网络训练,记录时长
  12676. AccuracyRATE = 0
  12677. # 训练10次,每3次训练下降一次学习率
  12678. for e in range(1, ${Number(class_input4.slice(1, -1)) + 1}):
  12679. if e % 3 == 0:
  12680. NN.learningrate /= 2
  12681. NN.fit(TRAINX, TRAINY)
  12682. y_pred = NN.predict(TESTX)
  12683. CURRENTACCURACY = accuracy(TESTY, y_pred)
  12684. if CURRENTACCURACY > AccuracyRATE:
  12685. AccuracyRATE = CURRENTACCURACY
  12686. #文件已保存至
  12687. with open(${class_input5}, "wb") as f: # 保存模型
  12688. pickle.dump(pickle.dumps(NN), f)
  12689. os.system("touch /usr/lib/python3.8/site-packages/numpy/random/__init__.py")
  12690. `;
  12691. // TODO: Change ORDER_NONE to the correct strength.
  12692. return code;
  12693. };
  12694. //
  12695. Blockly.Blocks['Numpy_Neural_Network_numpy_loading_model'] = {
  12696. init: function () {
  12697. this.appendDummyInput()
  12698. .appendField(Blockly.Msg.numpy_mnist_loading_model);
  12699. this.appendValueInput("class_input")
  12700. .setCheck(null)
  12701. .appendField(Blockly.Msg.AUDIO_S_PF);
  12702. this.setPreviousStatement(true, null);
  12703. this.setNextStatement(true, null);
  12704. this.setColour("#ee783a");
  12705. this.setTooltip(Blockly.Msg.numpy_mnist_loading_model);
  12706. this.setHelpUrl("");
  12707. }
  12708. };
  12709. Blockly.Python['Numpy_Neural_Network_numpy_loading_model'] = function (block) {
  12710. var trainUrl = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  12711. // TODO: Assemble Python into code variable.
  12712. var code = `try:
  12713. with open(${trainUrl},"rb") as f:
  12714. b_data = pickle.load(f)
  12715. net_model = pickle.loads(b_data)
  12716. except:
  12717. pass
  12718. `;
  12719. // TODO: Change ORDER_NONE to the correct strength.
  12720. return code;
  12721. };
  12722. Blockly.Python['Numpy_Neural_Network_LOading_test_train'] = function (block) {
  12723. var trainUrl = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  12724. var testUrl = Blockly.Python.valueToCode(block, 'class_input1', Blockly.Python.ORDER_ATOMIC);
  12725. // TODO: Assemble Python into code variable.
  12726. var code = `test_df = np.loadtxt(${testUrl}, delimiter=",", dtype=str)
  12727. train_df = np.loadtxt(${trainUrl}, delimiter=",", dtype=str)
  12728. `;
  12729. // TODO: Change ORDER_NONE to the correct strength.
  12730. return code;
  12731. };
  12732. Blockly.Blocks['Numpy_Neural_Network_numpy_loading_model_fun'] = {
  12733. init: function () {
  12734. // this.appendDummyInput()
  12735. // .appendField("获取检测结果");
  12736. this.appendDummyInput()
  12737. .appendField("在屏幕中显示numpy的检测结果")
  12738. this.setPreviousStatement(true, null);
  12739. this.setNextStatement(true, null);
  12740. this.setColour("#ee783a");
  12741. this.setTooltip(Blockly.Msg.ai_model_getclass_TOOLTIP);
  12742. this.setHelpUrl("");
  12743. }
  12744. };
  12745. Blockly.Python['Numpy_Neural_Network_numpy_loading_model_fun'] = function (block) {
  12746. Blockly.Python.definitions_["v831_import_PIL_image"] = `from PIL import Image`
  12747. Blockly.Python.definitions_["v831_function_numpy_result"] = `def numpyResult(img):
  12748. img_path="/root/user/image_1.png"
  12749. img_gray_path="/root/user/image_gray.png"
  12750. mk = img.crop(0, 0, 240, 240) #裁剪图像为320*240
  12751. mk1=img.crop(0, 0, 240, 240).resize(224,224) #继续裁剪图像为224*224
  12752. numpyResultcanvas= image.new(size = (320, 240), color = (0,255,0), mode = 'RGB') #创建背景图
  12753. numpyResultcanvas.draw_image(mk1.rotate(90,adjust=1),58,8) #贴224*224的图片
  12754. mk2=mk1.resize(28,28) #裁剪图片为28*28
  12755. numpyResultcanvas.draw_image(mk2,8,8) #贴28*28的图
  12756. mk2.save(img_path)
  12757. origin=Image.open(img_path).convert('L') #转为8位分辨率灰度图(0~255)
  12758. origin.save(img_gray_path)
  12759. origin_gray=image.open(img_gray_path)
  12760. numpyResultcanvas.draw_image(origin_gray,8,48)
  12761. numpyResultImg = np.array(origin).reshape(1, 784).astype('int')
  12762. numpyResultImg = numpyResultImg / 255 * 0.99 + 0.01
  12763. print(numpyResultImg.shape)
  12764. result = net_model.query(numpyResultImg)
  12765. return result
  12766. `
  12767. // TODO: Assemble Python into code variable.
  12768. var code = `_canvas_x, _canvas_y = 48,8
  12769. canvas = canvas.crop(48,8,224,224)
  12770. GETNUMPYRESULT = numpyResult(canvas)
  12771. if GETNUMPYRESULT != "":
  12772. canvas.draw_string(0,0, (str(GETNUMPYRESULT)), scale = 3, color = (204,204,204) , thickness = 1)
  12773. else:
  12774. canvas.draw_string(0,0, ("None"), scale = 3, color = (204,204,204) , thickness = 1)
  12775. `;
  12776. // TODO: Change ORDER_NONE to the correct strength.
  12777. return code;
  12778. };
  12779. Blockly.Blocks['Numpy_Neural_Network_numpy_loading_model_result'] = {
  12780. init: function () {
  12781. this.appendDummyInput()
  12782. .appendField(Blockly.Msg.numpy_mnist_get_result);
  12783. this.setOutput(true, null);
  12784. this.setColour("#ee783a");
  12785. this.setTooltip(Blockly.Msg.numpy_mnist_get_result);
  12786. this.setHelpUrl("");
  12787. }
  12788. };
  12789. Blockly.Python['Numpy_Neural_Network_numpy_loading_model_result'] = function (block) {
  12790. var code = `GETNUMPYRESULT`;
  12791. // TODO: Change ORDER_NONE to the correct strength.
  12792. return [code, Blockly.Python.ORDER_NONE];
  12793. };
  12794. //
  12795. Blockly.Blocks['Numpy_Neural_Network_numpy_clear'] = {
  12796. init: function () {
  12797. this.appendDummyInput()
  12798. .appendField(Blockly.Msg.numpy_mnist_clear);
  12799. this.setPreviousStatement(true, null);
  12800. this.setNextStatement(true, null);
  12801. this.setColour("#ee783a");
  12802. this.setTooltip(Blockly.Msg.numpy_mnist_clear);
  12803. this.setHelpUrl("");
  12804. }
  12805. };
  12806. Blockly.Python['Numpy_Neural_Network_numpy_clear'] = function (block) {
  12807. var code = `os.system('rm -rf /usr/lib/python3.8/site-packages/numpy/random/__init__.py')
  12808. `;
  12809. // TODO: Change ORDER_NONE to the correct strength.
  12810. return code;
  12811. };
  12812. Blockly.Blocks["cocopi_chatGpt_init"] = {
  12813. init: function () {
  12814. this.appendDummyInput()
  12815. .appendField(Blockly.Msg.ChatGpt_init); //
  12816. this.setPreviousStatement(true, null);
  12817. this.setNextStatement(true, null);
  12818. this.setColour("#7e3cca");
  12819. this.setTooltip(Blockly.Msg.ChatGpt_init);
  12820. this.setHelpUrl("");
  12821. }
  12822. };
  12823. Blockly.Python['cocopi_chatGpt_init'] = function (block) {
  12824. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  12825. sys.path.append("/root/")`
  12826. Blockly.Python.definitions_['v831_import_CocoPi_Gpt'] = `try:
  12827. from CocoPi import GPTrequest
  12828. except:
  12829. import sys
  12830. sys.path.append("/root/preset/drivers/pylib/")
  12831. from GPTrequest import GPTrequest`
  12832. Blockly.Python.definitions_['v831_import_CocoPi_Gpt_init'] = `gptInit = GPTrequest()`
  12833. var code = ``;
  12834. // TODO: Change ORDER_NONE to the correct strength.
  12835. return code;
  12836. };
  12837. Blockly.Blocks["cocopi_chatGpt_text_to_img_loading"] = {
  12838. init: function () {
  12839. this.appendValueInput("NAME")
  12840. .setCheck(null)
  12841. .appendField(Blockly.Msg.ChatGpt_set_img_des);
  12842. this.appendDummyInput()
  12843. .appendField(Blockly.Msg.ChatGpt_get_img_path_result);
  12844. this.setOutput(true, null);
  12845. this.setColour("#7e3cca");
  12846. this.setTooltip(Blockly.Msg.ChatGpt_set_img_des);
  12847. this.setInputsInline(false)
  12848. this.setHelpUrl("");
  12849. }
  12850. };
  12851. Blockly.Python['cocopi_chatGpt_text_to_img_loading'] = function (block) {
  12852. var text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC);
  12853. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  12854. sys.path.append("/root/")`
  12855. Blockly.Python.definitions_['v831_import_CocoPi_Gpt'] = `try:
  12856. from CocoPi import GPTrequest
  12857. except:
  12858. import sys
  12859. sys.path.append("/root/preset/drivers/pylib/")
  12860. from GPTrequest import GPTrequest`
  12861. Blockly.Python.definitions_['v831_import_CocoPi_Gpt_init'] = `gptInit = GPTrequest()`
  12862. // Blockly.Python.addVariable("TextContent", `TextContent = ""`, true)
  12863. var code = `gptInit.get_text_to_img(${text})`;
  12864. // TODO: Change ORDER_NONE to the correct strength.
  12865. return [code, Blockly.Python.ORDER_ATOMIC];
  12866. };
  12867. Blockly.Blocks["cocopi_chatGpt_image_to_text_loading"] = {
  12868. init: function () {
  12869. this.appendValueInput("NAME")
  12870. .setCheck(null)
  12871. .appendField(Blockly.Msg.ChatGpt_set_text_path);
  12872. this.appendValueInput("NAME1")
  12873. .setCheck(null)
  12874. .appendField(Blockly.Msg.ChatGpt_set_text_text_content);
  12875. this.appendDummyInput()
  12876. .appendField(Blockly.Msg.ChatGpt_get_text_content_result);
  12877. this.setOutput(true, null);
  12878. this.setInputsInline(false);
  12879. this.setColour("#7e3cca");
  12880. this.setTooltip(Blockly.Msg.ChatGpt_set_text_path);
  12881. this.setHelpUrl("");
  12882. }
  12883. };
  12884. Blockly.Python['cocopi_chatGpt_image_to_text_loading'] = function (block) {
  12885. var text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC);
  12886. var content = Blockly.Python.valueToCode(block, 'NAME1', Blockly.Python.ORDER_ATOMIC);
  12887. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  12888. sys.path.append("/root/")`
  12889. Blockly.Python.definitions_['v831_import_CocoPi_Gpt'] = `try:
  12890. from CocoPi import GPTrequest
  12891. except:
  12892. import sys
  12893. sys.path.append("/root/preset/drivers/pylib/")
  12894. from GPTrequest import GPTrequest`
  12895. Blockly.Python.definitions_['v831_import_CocoPi_Gpt_init'] = `gptInit = GPTrequest()`
  12896. var code = `gptInit.get_image_to_text(${text},${content})`;
  12897. // TODO: Change ORDER_NONE to the correct strength.
  12898. return [code, Blockly.Python.ORDER_ATOMIC];
  12899. };
  12900. Blockly.Blocks["cocopi_chatGpt_text_to_voice_loading"] = {
  12901. init: function () {
  12902. this.appendValueInput("NAME")
  12903. .setCheck(null)
  12904. .appendField(Blockly.Msg.ChatGpt_set_text_content);
  12905. this.appendDummyInput()
  12906. .appendField(Blockly.Msg.ChatGpt_get_voice_path_result);
  12907. this.setOutput(true, null);
  12908. this.setInputsInline(false);
  12909. this.setColour("#7e3cca");
  12910. this.setTooltip(Blockly.Msg.ChatGpt_get_voice_path_result);
  12911. this.setHelpUrl("");
  12912. }
  12913. };
  12914. Blockly.Python['cocopi_chatGpt_text_to_voice_loading'] = function (block) {
  12915. var text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC);
  12916. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  12917. sys.path.append("/root/")`
  12918. Blockly.Python.definitions_['v831_import_CocoPi_Gpt'] = `try:
  12919. from CocoPi import GPTrequest
  12920. except:
  12921. import sys
  12922. sys.path.append("/root/preset/drivers/pylib/")
  12923. from GPTrequest import GPTrequest`
  12924. Blockly.Python.definitions_['v831_import_CocoPi_Gpt_init'] = `gptInit = GPTrequest()`
  12925. var code = `gptInit.get_text_to_voice(${text})`;
  12926. // TODO: Change ORDER_NONE to the correct strength.
  12927. return [code, Blockly.Python.ORDER_ATOMIC];
  12928. };
  12929. Blockly.Blocks["cocopi_chatGpt_text_to_voice_result"] = {
  12930. init: function () {
  12931. this.appendDummyInput()
  12932. .appendField(Blockly.Msg.ChatGpt_get_voice_path_result);
  12933. this.setOutput(true, null);
  12934. this.setColour("#7e3cca");
  12935. this.setTooltip(Blockly.Msg.ChatGpt_get_voice_path_result);
  12936. this.setHelpUrl("");
  12937. }
  12938. };
  12939. Blockly.Python['cocopi_chatGpt_text_to_voice_result'] = function (block) {
  12940. var code = `gptInit.get_text_to_voice(VOICECONTENT)`;
  12941. return [code, Blockly.Python.ORDER_ATOMIC];
  12942. };
  12943. Blockly.Blocks["cocopi_chatGpt_voice_to_text_loading"] = {
  12944. init: function () {
  12945. this.appendValueInput("NAME")
  12946. .setCheck(null)
  12947. .appendField(Blockly.Msg.ChatGpt_set_voice_path);
  12948. this.appendDummyInput()
  12949. .appendField(Blockly.Msg.ChatGpt_get_voice_text_content_result);
  12950. this.setOutput(true, null);
  12951. this.setInputsInline(false)
  12952. this.setColour("#7e3cca");
  12953. this.setTooltip(Blockly.Msg.ChatGpt_set_voice_path);
  12954. this.setHelpUrl("");
  12955. }
  12956. };
  12957. Blockly.Python['cocopi_chatGpt_voice_to_text_loading'] = function (block) {
  12958. var text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC);
  12959. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  12960. sys.path.append("/root/")`
  12961. Blockly.Python.definitions_['v831_import_CocoPi_Gpt'] = `try:
  12962. from CocoPi import GPTrequest
  12963. except:
  12964. import sys
  12965. sys.path.append("/root/preset/drivers/pylib/")
  12966. from GPTrequest import GPTrequest`
  12967. Blockly.Python.definitions_['v831_import_CocoPi_Gpt_init'] = `gptInit = GPTrequest()`
  12968. var code = `gptInit.get_voice_to_text(${text})`;
  12969. // TODO: Change ORDER_NONE to the correct strength.
  12970. return [code, Blockly.Python.ORDER_ATOMIC];
  12971. };
  12972. Blockly.Blocks["cocopi_chatGpt_post_chatgpt_loading"] = {
  12973. init: function () {
  12974. this.appendValueInput("NAME")
  12975. .setCheck(null)
  12976. .appendField(Blockly.Msg.ChatGpt_set_problem_content);
  12977. this.appendDummyInput()
  12978. .appendField(Blockly.Msg.ChatGpt_get_problem_result);
  12979. this.setOutput(true, null);
  12980. this.setInputsInline(false)
  12981. this.setColour("#7e3cca");
  12982. this.setTooltip(Blockly.Msg.ChatGpt_set_problem_content);
  12983. this.setHelpUrl("");
  12984. }
  12985. };
  12986. Blockly.Python['cocopi_chatGpt_post_chatgpt_loading'] = function (block) {
  12987. var text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC);
  12988. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  12989. sys.path.append("/root/")`
  12990. Blockly.Python.definitions_['v831_import_CocoPi_Gpt'] = `try:
  12991. from CocoPi import GPTrequest
  12992. except:
  12993. import sys
  12994. sys.path.append("/root/preset/drivers/pylib/")
  12995. from GPTrequest import GPTrequest`
  12996. Blockly.Python.definitions_['v831_import_CocoPi_Gpt_init'] = `gptInit = GPTrequest()`
  12997. var code = `gptInit.get_post_chatgpt(${text})`;
  12998. // TODO: Change ORDER_NONE to the correct strength.
  12999. return [code, Blockly.Python.ORDER_ATOMIC];
  13000. };
  13001. Blockly.Blocks["cocopi_chatGpt_post_chatgpt_result"] = {
  13002. init: function () {
  13003. this.appendDummyInput()
  13004. .appendField(Blockly.Msg.ChatGpt_get_problem_result);
  13005. this.setOutput(true, null);
  13006. this.setColour("#7e3cca");
  13007. this.setTooltip(Blockly.Msg.ChatGpt_get_problem_result);
  13008. this.setHelpUrl("");
  13009. }
  13010. };
  13011. Blockly.Python['cocopi_chatGpt_post_chatgpt_result'] = function (block) {
  13012. var code = `gptInit.get_post_chatgpt(CHATGPTPROBLEM)`;
  13013. return [code, Blockly.Python.ORDER_ATOMIC];;
  13014. };
  13015. //
  13016. Blockly.Blocks["cocopi_chatGpt_result"] = {
  13017. init: function () {
  13018. this.appendDummyInput()
  13019. // .appendField(Blockly.Msg.ChatGpt_get_problem_result)
  13020. .appendField(new Blockly.FieldDropdown([
  13021. [Blockly.Msg.ChatGpt_get_img_path_result, "1"],
  13022. [Blockly.Msg.ChatGpt_get_text_content_result, "2"],
  13023. [Blockly.Msg.ChatGpt_get_voice_path_result, "3"],
  13024. [Blockly.Msg.ChatGpt_get_voice_text_content_result, "4"],
  13025. [Blockly.Msg.ChatGpt_get_problem_result, "5"],
  13026. [Blockly.Msg.ChatGpt_ai_agent_chat_result, "6"]
  13027. ]), "gpt_return");
  13028. this.setOutput(true, null);
  13029. this.setColour("#7e3cca");
  13030. this.setTooltip(Blockly.Msg.ChatGpt_get_problem_result);
  13031. this.setHelpUrl("");
  13032. }
  13033. };
  13034. Blockly.Python['cocopi_chatGpt_result'] = function (block) {
  13035. var queue_value = block.getFieldValue('gpt_return');
  13036. Blockly.Python.addVariable("CocoPiGPTRESULT", `CocoPiGPTRESULT = None`, true)
  13037. var res = ``;
  13038. if (queue_value == 5) {
  13039. res = `gptInit.get_post_chatgpt(CHATGPTPROBLEM)`
  13040. } else if (queue_value == 4) {
  13041. res = `gptInit.get_voice_to_text(VOICEPATH)`
  13042. } else if (queue_value == 3) {
  13043. res = `gptInit.get_text_to_voice(VOICECONTENT)`
  13044. } else if (queue_value == 2) {
  13045. res = `gptInit.get_image_to_text(IMAGEPATH,IMAGECONTENT)`
  13046. } else if (queue_value == 1) {
  13047. res = `gptInit.get_text_to_img(TextContent)`
  13048. }
  13049. else if (queue_value == 6) {
  13050. res = `gptInit.get_post_AI_Intelligence(AIAGENTCHATID,AIAGENTCHATMESSAGE)`
  13051. }
  13052. Blockly.Python.definitions_["CocoPiGPTRESULTFUNC"] = `def CocoPiGPTRESULTFUNC(res):
  13053. global CocoPiGPTRESULT
  13054. try:
  13055. CocoPiGPTRESULT = res
  13056. except:
  13057. CocoPiGPTRESULT = ""
  13058. return CocoPiGPTRESULT
  13059. `
  13060. var code = `CocoPiGPTRESULTFUNC(${res})`;
  13061. return [code, Blockly.Python.ORDER_ATOMIC];;
  13062. };
  13063. Blockly.Blocks["cocopi_chatGpt_AI_loading"] = {
  13064. init: function () {
  13065. this.appendValueInput("NAME")
  13066. .setCheck(null)
  13067. .appendField(Blockly.Msg.ChatGpt_ai_agent_chat_set_appID);
  13068. this.appendValueInput("NAME1")
  13069. .setCheck(null)
  13070. .appendField(Blockly.Msg.ChatGpt_set_problem_content);
  13071. this.appendDummyInput().appendField(Blockly.Msg.ChatGpt_ai_agent_chat_result)
  13072. this.setOutput(true, null);
  13073. this.setInputsInline(false);
  13074. this.setColour("#7e3cca");
  13075. this.setTooltip(Blockly.Msg.ChatGpt_set_text_path);
  13076. this.setHelpUrl("");
  13077. }
  13078. };
  13079. Blockly.Python['cocopi_chatGpt_AI_loading'] = function (block) {
  13080. var text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC);
  13081. var content = Blockly.Python.valueToCode(block, 'NAME1', Blockly.Python.ORDER_ATOMIC);
  13082. var text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC);
  13083. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  13084. sys.path.append("/root/")`
  13085. Blockly.Python.definitions_['v831_import_CocoPi_Gpt'] = `try:
  13086. from CocoPi import GPTrequest
  13087. except:
  13088. import sys
  13089. sys.path.append("/root/preset/drivers/pylib/")
  13090. from GPTrequest import GPTrequest`
  13091. Blockly.Python.definitions_['v831_import_CocoPi_Gpt_init'] = `gptInit = GPTrequest()`
  13092. var code = `gptInit.get_post_AI_Intelligence(${text},${content})`
  13093. return [code, Blockly.Python.ORDER_ATOMIC];
  13094. };
  13095. //
  13096. Blockly.Blocks["cocopi_chatGpt_AI_loading_more"] = {
  13097. init: function () {
  13098. this.appendValueInput("NAME")
  13099. .setCheck(null)
  13100. .appendField(Blockly.Msg.ChatGpt_ai_agent_chat_set_appID_more);
  13101. this.appendValueInput("NAME1")
  13102. .setCheck(null)
  13103. .appendField(Blockly.Msg.ChatGpt_set_problem_content);
  13104. this.appendDummyInput().appendField(Blockly.Msg.ChatGpt_ai_agent_chat_result_more)
  13105. this.setOutput(true, null);
  13106. this.setInputsInline(false);
  13107. this.setColour("#7e3cca");
  13108. this.setTooltip(Blockly.Msg.ChatGpt_set_text_path);
  13109. this.setHelpUrl("");
  13110. }
  13111. };
  13112. Blockly.Python['cocopi_chatGpt_AI_loading_more'] = function (block) {
  13113. var text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC);
  13114. var content = Blockly.Python.valueToCode(block, 'NAME1', Blockly.Python.ORDER_ATOMIC);
  13115. var text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC);
  13116. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  13117. sys.path.append("/root/")`
  13118. Blockly.Python.definitions_['v831_import_CocoPi_Gpt'] = `try:
  13119. from CocoPi import GPTrequest
  13120. except:
  13121. import sys
  13122. sys.path.append("/root/preset/drivers/pylib/")
  13123. from GPTrequest import GPTrequest`
  13124. Blockly.Python.definitions_['v831_import_CocoPi_Gpt_init'] = `gptInit = GPTrequest()`
  13125. var code = `gptInit.cocopi_chatGpt_AI_loading_more(${text},${content})`
  13126. // Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  13127. // Blockly.Python.definitions_['v831_import_json'] = `import json`
  13128. // Blockly.Python.definitions_['v831_import_os'] = `import os`
  13129. // Blockly.Python.definitions_['v831_import_time'] = `import time`
  13130. // Blockly.Python.addFunction("cocopi_chatGpt_AI_loading_more", `def cocopi_chatGpt_AI_loading_more(appID,content):
  13131. // url = "https://gpt4.cocorobo.cn/handle_muti_agent"
  13132. // headers={
  13133. // "Content-Type":"application/json",
  13134. // }
  13135. // cmd="cat /sys/class/sunxi_info/sys_info"
  13136. // rec=os.popen(cmd).read()
  13137. // hwMac=rec[rec.find("sunxi_serial : ")+20:rec.find("sunxi_serial : ")+52]
  13138. // params = {
  13139. // "muti_agent_id":appID,
  13140. // "message":content,
  13141. // "userId":"602def61-005d-11ee-91d8-005056b86db5",
  13142. // "session_name":"0606"
  13143. // }
  13144. // textContent = ""
  13145. // res = requests.post(url, headers=headers, data=json.dumps(params))
  13146. // try:
  13147. // textContent = json.loads(res.text)["FunctionResponse"]["response"]
  13148. // except:
  13149. // textContent = ""
  13150. // return textContent
  13151. // `, true)
  13152. return [code, Blockly.Python.ORDER_ATOMIC];
  13153. };
  13154. Blockly.Blocks.tuple_create_with_items_inserts = {
  13155. init: function () {
  13156. this.setHelpUrl(Blockly.Msg.TUPLE_CREATE_WITH_ITEMS_INSERT_HELPURL);
  13157. this.setColour("#ee783a");
  13158. this.itemCount_ = 3;
  13159. this.updateShape_();
  13160. this.setOutput(!0);
  13161. this.setMutator(new Blockly.Mutator(["tuple_create_with_items"]));
  13162. this.setTooltip(Blockly.Msg.TUPLE_CREATE_WITH_ITEMS_INSERT_TOOLTIP)
  13163. },
  13164. mutationToDom: function () {
  13165. var a = document.createElement("mutation");
  13166. a.setAttribute("items", this.itemCount_);
  13167. return a
  13168. },
  13169. domToMutation: function (a) {
  13170. this.itemCount_ = parseInt(a.getAttribute("items"), 10);
  13171. this.updateShape_()
  13172. },
  13173. decompose: function (a) {
  13174. var b = a.newBlock("tuple_create_with_containers");
  13175. b.initSvg();
  13176. for (var c = b.getInput("STACK").connection, e = 0; e < this.itemCount_; e++) {
  13177. var d = a.newBlock("tuple_create_with_items");
  13178. d.initSvg();
  13179. c.connect(d.previousConnection);
  13180. c = d.nextConnection
  13181. }
  13182. return b
  13183. },
  13184. compose: function (a) {
  13185. var b = a.getInputTargetBlock("STACK");
  13186. for (a = []; b;)
  13187. a.push(b.valueConnection_),
  13188. b = b.nextConnection && b.nextConnection.targetBlock();
  13189. for (b = 0; b < this.itemCount_; b++) {
  13190. var c = this.getInput("ADD" + b).connection.targetConnection;
  13191. c && -1 == a.indexOf(c) && c.disconnect()
  13192. }
  13193. this.itemCount_ = a.length;
  13194. this.updateShape_();
  13195. for (b = 0; b < this.itemCount_; b++)
  13196. Blockly.Mutator.reconnect(a[b], this, "ADD" + b)
  13197. },
  13198. saveConnections: function (a) {
  13199. a = a.getInputTargetBlock("STACK");
  13200. for (var b = 0; a;) {
  13201. var c = this.getInput("ADD" + b);
  13202. a.valueConnection_ = c && c.connection.targetConnection;
  13203. b++;
  13204. a = a.nextConnection && a.nextConnection.targetBlock()
  13205. }
  13206. },
  13207. updateShape_: function () {
  13208. this.itemCount_ && this.getInput("EMPTY") ? this.removeInput("EMPTY") : this.itemCount_ || this.getInput("EMPTY") || this.appendDummyInput("EMPTY").appendField(Blockly.Msg.TUPLE_CREATE_EMPTY_TITLE);
  13209. for (var a = 0; a < this.itemCount_; a++)
  13210. if (!this.getInput("ADD" + a)) {
  13211. var b = this.appendValueInput("ADD" + a);
  13212. 0 == a && b.appendField(Blockly.Msg.TUPLE_CREATE_WITH_INPUT_WITH)
  13213. }
  13214. for (; this.getInput("ADD" + a);)
  13215. this.removeInput("ADD" + a),
  13216. a++
  13217. }
  13218. };
  13219. Blockly.Blocks.tuple_create_with_items = {
  13220. init: function () {
  13221. this.setColour("#ee783a");
  13222. this.appendDummyInput().appendField(Blockly.Msg.TUPLE_CREATE_WITH_ITEM_TITLE);
  13223. this.setPreviousStatement(!0);
  13224. this.setNextStatement(!0);
  13225. this.setTooltip(Blockly.Msg.TUPLE_CREATE_WITH_ITEM_TOOLTIP);
  13226. this.contextMenu = !1
  13227. }
  13228. };
  13229. Blockly.Blocks.tuple_create_with_containers = {
  13230. init: function () {
  13231. this.setColour("#ee783a");
  13232. this.appendDummyInput().appendField(Blockly.Msg.TUPLE_CREATE_WITH_CONTAINER_TITLE_ADD);
  13233. this.appendStatementInput("STACK");
  13234. this.setTooltip(Blockly.Msg.TUPLE_CREATE_WITH_CONTAINER_TOOLTIP);
  13235. this.contextMenu = !1
  13236. }
  13237. };
  13238. Blockly.Python.tuple_create_with_items_inserts = function (a) {
  13239. for (var b = Array(a.itemCount_), c = 0; c < a.itemCount_; c++)
  13240. b[c] = Blockly.Python.valueToCode(a, "ADD" + c, Blockly.Python.ORDER_NONE) || "None";
  13241. return [1 == a.itemCount_ ? "(" + b[0] + ",)" : "(" + b.join(", ") + ")", Blockly.Python.ORDER_ATOMIC]
  13242. };