ai.js 587 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516
  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("开始识别二维码")
  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. var code = 'findQrcode = canvas.find_qrcodes() \n';
  3287. // TODO: Change ORDER_NONE to the correct strength.
  3288. return code;
  3289. };
  3290. Blockly.Blocks['ai_vision_find_qrcode_result'] = {
  3291. init: function () {
  3292. this.appendDummyInput()
  3293. .appendField("屏幕显示二维码").
  3294. appendField(new Blockly.FieldDropdown([
  3295. ["源文本", "SourceText"],
  3296. ["检测框", "CheckBox"],
  3297. ]), "qrcode_result")
  3298. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  3299. // .appendField();
  3300. this.setOutput(false, null);
  3301. this.setNextStatement(true, null);
  3302. this.setPreviousStatement(true, null);
  3303. this.setColour("#f0983e");
  3304. this.setTooltip(Blockly.Msg.ai_vision_find_qrcode_TOOLTIP);
  3305. this.setHelpUrl("");
  3306. }
  3307. };
  3308. Blockly.Python['ai_vision_find_qrcode_result'] = function (block) {
  3309. let qrcode_result = block.getFieldValue('qrcode_result');
  3310. let qrcodeResultAllBlock = getBlocksByTypeName("ai_vision_find_qrcode_result")
  3311. // console.log("qrcode_result",qrcodeResultAllBlock[0].innerText)
  3312. let findQrcodeStr = qrcodeResultAllBlock[0].innerText
  3313. let Source = findQrcodeStr.indexOf("Source") != -1 ? "True" : "False"
  3314. let CheckBox = findQrcodeStr.indexOf("CheckBox") != -1 ? "True" : "False"
  3315. Blockly.Python.addFunction("showQRcodeResult", `def showQRcodeResult(Source,CheckBox):
  3316. global findQrcode
  3317. for i in findQrcode:
  3318. if Source:
  3319. canvas.draw_string((i["x"]),(i["y"]), (i["payload"]), scale = 1, color = (255,0,0) , thickness = 1)
  3320. if CheckBox:
  3321. canvas.draw_rectangle((i["x"]),(i["y"]), (i["x"])+(i["w"]),(i["y"])+ (i["h"]), color=(255,0,0), thickness=1)
  3322. `)
  3323. // TODO: Assemble Python into code variable.
  3324. let code = ""
  3325. if (qrcodeResultAllBlock[0].id == block.id) {
  3326. code = `showQRcodeResult(${Source},${CheckBox})
  3327. `
  3328. }
  3329. // TODO: Change ORDER_NONE to the correct strength.
  3330. return code;
  3331. };
  3332. Blockly.Blocks['ai_vision_qrcode_get_info'] = {
  3333. init: function () {
  3334. this.appendDummyInput()
  3335. .appendField(Blockly.Msg.ai_vision_qrcode_get_result + Blockly.Msg.ai_vision_pattern_detection_content)
  3336. this.setInputsInline(false);
  3337. this.setOutput(true, null);
  3338. this.setColour("#f0983e");
  3339. this.setTooltip(Blockly.Msg.ai_vision_qrcode_get_result + Blockly.Msg.ai_vision_pattern_detection_content);
  3340. this.setHelpUrl("");
  3341. }
  3342. };
  3343. Blockly.Python['ai_vision_qrcode_get_info'] = function (block) {
  3344. Blockly.Python.addFunction("getQRcodeResult", `def getQRcodeResult():
  3345. global findQrcode
  3346. qrCodeList = []
  3347. for i in findQrcode:
  3348. qrCodeList.append({i["payload"]})
  3349. return qrCodeList
  3350. `)
  3351. let code = "getQRcodeResult()"
  3352. return [code, Blockly.Python.ORDER_NONE];
  3353. };
  3354. Blockly.Blocks['ai_vision_find_apriltag'] = {
  3355. init: function () {
  3356. this.appendDummyInput()
  3357. .appendField(new Blockly.FieldImage("blockly/media/ai_vision_apriltag_.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  3358. this.appendDummyInput()
  3359. .appendField(Blockly.Msg.ai_vision_get_canvas + Blockly.Msg.ai_vision_apriltag_detected_result)
  3360. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  3361. // .appendField();
  3362. // this.setOutput(true, null);
  3363. this.setNextStatement(true, null);
  3364. this.setPreviousStatement(true, null);
  3365. this.setColour("#f0983e");
  3366. this.setTooltip(Blockly.Msg.ai_vision_find_apriltag_TOOLTIP);
  3367. this.setHelpUrl("");
  3368. }
  3369. };
  3370. Blockly.Python['ai_vision_find_apriltag'] = function (block) {
  3371. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  3372. // TODO: Assemble Python into code variable.
  3373. var code = 'findApriltags = canvas.find_apriltags(families = 16,fx = (6/5.76)*320,fy = (6/3.24)*240,cx = 160,cy = 120)';
  3374. // TODO: Change ORDER_NONE to the correct strength.
  3375. return code;
  3376. };
  3377. Blockly.Blocks['ai_vision_apriltag_get_info'] = {
  3378. init: function () {
  3379. this.appendDummyInput()
  3380. .appendField(Blockly.Msg.ai_vision_apriltag_get_result)
  3381. .appendField(new Blockly.FieldDropdown([
  3382. [Blockly.Msg.ai_vision_pattern_detection_content, "ApriltagId"],
  3383. // [Blockly.Msg.ai_vision_pattern_detection_type_id, "id"],
  3384. [Blockly.Msg.ai_vision_pattern_detection_type_family, "ApriltagCheckout"]
  3385. ]), "get_info");
  3386. this.setInputsInline(false);
  3387. // this.setOutput(true, null);
  3388. this.setNextStatement(true, null);
  3389. this.setPreviousStatement(true, null);
  3390. this.setColour("#f0983e");
  3391. var thisBlock = this;
  3392. this.setTooltip(function () {
  3393. var mode = thisBlock.getFieldValue('get_info');
  3394. var TOOLTIPS = {
  3395. 'payload': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
  3396. 'id': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
  3397. 'family': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_type_family),
  3398. 'x': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_x),
  3399. 'y': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_y),
  3400. 'w': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_w),
  3401. 'h': Blockly.Msg.ai_vision_apriltag_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_h)
  3402. };
  3403. return TOOLTIPS[mode];
  3404. });
  3405. this.setHelpUrl("");
  3406. }
  3407. };
  3408. Blockly.Python['ai_vision_apriltag_get_info'] = function (block) {
  3409. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  3410. var dropdown_name = block.getFieldValue('get_info');
  3411. let apriltagAllBlock = getBlocksByTypeName("ai_vision_apriltag_get_info")
  3412. let findQrcodeStr = apriltagAllBlock[0].innerText
  3413. let ApriltagId = findQrcodeStr.indexOf("ApriltagId") != -1 ? "True" : "False"
  3414. let ApriltagCheckout = findQrcodeStr.indexOf("ApriltagCheckout") != -1 ? "True" : "False"
  3415. Blockly.Python.addFunction("getApriltagCanvasResult", `def getApriltagCanvasResult(ApriltagId,ApriltagCheckout):
  3416. global findApriltags
  3417. for i in findApriltags:
  3418. if apriltagId:
  3419. canvas.draw_string((i["x"]),(i["y"]), (i["id"]), scale = 1, color = (255,0,0) , thickness = 1)
  3420. if ApriltagCheckout:
  3421. canvas.draw_rectangle((i["x"]),(i["y"]), (i["x"])+(i["w"]),(i["y"])+ (i["h"]), color=(255,0,0), thickness=1)
  3422. )`)
  3423. // TODO: Assemble Python into code variable.
  3424. var code = `getApriltagCanvasResult(${ApriltagId},${ApriltagCheckout})`;
  3425. // TODO: Change ORDER_NONE to the correct strength.
  3426. return code;
  3427. };
  3428. Blockly.Blocks['ai_vision_find_datamatrices'] = {
  3429. init: function () {
  3430. this.appendDummyInput()
  3431. .appendField(Blockly.Msg.ai_vision_get_canvas + Blockly.Msg.ai_vision_apriltag_detected_result);
  3432. this.setOutput(true, null);
  3433. this.setColour("#f0983e");
  3434. this.setTooltip(Blockly.Msg.ai_vision_find_datamatrices_TOOLTIP);
  3435. this.setHelpUrl("");
  3436. }
  3437. };
  3438. Blockly.Python['ai_vision_find_datamatrices'] = function (block) {
  3439. Blockly.Python.addFunction("getApriltagResult", `def getApriltagResult():
  3440. global findApriltags
  3441. if len(findApriltags):
  3442. return findApriltags[0]["id"]
  3443. else:
  3444. return ""
  3445. )`)
  3446. var code = `getApriltagResult()`;
  3447. // TODO: Change ORDER_NONE to the correct strength.
  3448. return [code, Blockly.Python.ORDER_NONE];
  3449. };
  3450. Blockly.Blocks['ai_vision_datamatrices_get_info'] = {
  3451. init: function () {
  3452. this.appendDummyInput()
  3453. .appendField(Blockly.Msg.ai_vision_from)
  3454. .appendField(new Blockly.FieldVariable("i"), "varitem")
  3455. .appendField(Blockly.Msg.ai_vision_data_matrices_get_result)
  3456. .appendField(new Blockly.FieldDropdown([
  3457. [Blockly.Msg.ai_vision_pattern_detection_content, "payload"],
  3458. [Blockly.Msg.ai_vision_pattern_detection_rows, "rows"],
  3459. [Blockly.Msg.ai_vision_pattern_detection_cols, "columns"],
  3460. [Blockly.Msg.ai_vision_pattern_detection_can_tolerate_count, "capacity"],
  3461. [Blockly.Msg.ai_vision_pattern_detection_can_not_tolerate_count, "padding"],
  3462. [Blockly.Msg.ai_vision_pattern_detection_x, "x"],
  3463. [Blockly.Msg.ai_vision_pattern_detection_y, "y"],
  3464. [Blockly.Msg.ai_vision_pattern_detection_w, "w"],
  3465. [Blockly.Msg.ai_vision_pattern_detection_h, "h"]
  3466. ]), "get_info");
  3467. this.setInputsInline(false);
  3468. this.setOutput(true, null);
  3469. this.setColour("#f0983e");
  3470. var thisBlock = this;
  3471. this.setTooltip(function () {
  3472. var mode = thisBlock.getFieldValue('get_info');
  3473. var TOOLTIPS = {
  3474. 'payload': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
  3475. 'rows': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_rows),
  3476. 'columns': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_cols),
  3477. 'capacity': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_can_tolerate_count),
  3478. 'padding': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_can_not_tolerate_count),
  3479. 'x': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_x),
  3480. 'y': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_y),
  3481. 'w': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_w),
  3482. 'h': Blockly.Msg.ai_vision_datamatrices_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_h)
  3483. };
  3484. return TOOLTIPS[mode];
  3485. });
  3486. this.setHelpUrl("");
  3487. }
  3488. };
  3489. Blockly.Python['ai_vision_datamatrices_get_info'] = function (block) {
  3490. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  3491. var dropdown_name = block.getFieldValue('get_info');
  3492. // TODO: Assemble Python into code variable.
  3493. var code = variable_name + '.' + dropdown_name + '()';
  3494. // TODO: Change ORDER_NONE to the correct strength.
  3495. return [code, Blockly.Python.ORDER_NONE];
  3496. };
  3497. Blockly.Blocks['ai_vision_find_barcodes'] = {
  3498. init: function () {
  3499. this.appendDummyInput()
  3500. .appendField(new Blockly.FieldImage("blockly/media/ai_vision_barcode_.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  3501. this.appendDummyInput()
  3502. .appendField(Blockly.Msg.ai_vision_get_canvas + Blockly.Msg.ai_vision_barcode_detected_result)
  3503. // .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  3504. // .appendField();
  3505. this.setNextStatement(true);
  3506. this.setPreviousStatement(true);
  3507. this.setColour("#f0983e");
  3508. this.setTooltip(Blockly.Msg.ai_vision_find_barcodes_TOOLTIP);
  3509. this.setHelpUrl("");
  3510. }
  3511. };
  3512. Blockly.Python['ai_vision_find_barcodes'] = function (block) {
  3513. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  3514. // TODO: Assemble Python into code variable.
  3515. var code = 'findBarcodes = canvas.find_barcodes() \n';
  3516. // TODO: Change ORDER_NONE to the correct strength.
  3517. return code;
  3518. };
  3519. Blockly.Blocks['ai_vision_barcodes_get_info'] = {
  3520. init: function () {
  3521. this.appendDummyInput()
  3522. .appendField("屏幕显示条形码")
  3523. .appendField(new Blockly.FieldDropdown([
  3524. [Blockly.Msg.ai_vision_pattern_detection_content, "barcodesPayload"],
  3525. [Blockly.Msg.ai_models_face_model_rect, "barcodesCorners"]
  3526. ]), "get_info");
  3527. this.setInputsInline(false);
  3528. this.setOutput(false, null);
  3529. this.setNextStatement(true);
  3530. this.setPreviousStatement(true);
  3531. this.setColour("#f0983e");
  3532. var thisBlock = this;
  3533. this.setTooltip(function () {
  3534. var mode = thisBlock.getFieldValue('get_info');
  3535. var TOOLTIPS = {
  3536. 'payload': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_content),
  3537. 'corners': Blockly.Msg.ai_models_face_model_rect.replace('%1', Blockly.Msg.ai_models_face_model_rect),
  3538. 'x': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_x),
  3539. 'y': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_y),
  3540. 'w': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_w),
  3541. 'h': Blockly.Msg.ai_vision_barcodes_get_info_TOOLTIP.replace('%1', Blockly.Msg.ai_vision_pattern_detection_h)
  3542. };
  3543. return TOOLTIPS[mode];
  3544. });
  3545. this.setHelpUrl("");
  3546. }
  3547. };
  3548. Blockly.Python['ai_vision_barcodes_get_info'] = function (block) {
  3549. var dropdown_name = block.getFieldValue('get_info');
  3550. let qrcodeResultAllBlock = getBlocksByTypeName("ai_vision_barcodes_get_info")
  3551. // console.log("qrcode_result",qrcodeResultAllBlock[0].innerText)
  3552. let findQrcodeStr = qrcodeResultAllBlock[0].innerText
  3553. let barcodesPayload = findQrcodeStr.indexOf("barcodesPayload") != -1 ? "True" : "False"
  3554. let barcodesCorners = findQrcodeStr.indexOf("barcodesCorners") != -1 ? "True" : "False"
  3555. Blockly.Python.addFunction("showBarcodesResult", `def showBarcodesResult(type1,type2):
  3556. global findBarcodes
  3557. for i in findBarcodes:
  3558. if type1:
  3559. canvas.draw_string((i["corners"][0][0]),(i["corners"][0][1]), (i["payload"]), scale = 1, color = (255,0,0) , thickness = 1)
  3560. if type2:
  3561. x1,y1 = i['corners'][0] #访问字典的列表
  3562. x2,y2 = i['corners'][1]
  3563. x3,y3 = i['corners'][2]
  3564. x4,y4 = i['corners'][3]
  3565. canvas.draw_line(x1, y1, x2, y2, color = (0, 255, 0), thickness = 3)
  3566. canvas.draw_line(x2, y2, x3, y3, color = (0, 255, 0), thickness = 3)
  3567. canvas.draw_line(x3, y3, x4, y4, color = (0, 255, 0), thickness = 3)
  3568. canvas.draw_line(x4, y4, x1, y1, color = (0, 255, 0), thickness = 3)
  3569. `)
  3570. // TODO: Assemble Python into code variable.
  3571. let code = ""
  3572. if (qrcodeResultAllBlock[0].id == block.id) {
  3573. code = `showBarcodesResult(${barcodesPayload},${barcodesCorners})
  3574. `
  3575. }
  3576. return code
  3577. };
  3578. Blockly.Blocks['ai_vision_barcodes_get_info_result'] = {
  3579. init: function () {
  3580. this.appendDummyInput()
  3581. .appendField(Blockly.Msg.uav_bar_code)
  3582. this.setInputsInline(false);
  3583. this.setOutput(true, null);
  3584. this.setColour("#f0983e");
  3585. this.setTooltip(Blockly.Msg.uav_bar_code);
  3586. }
  3587. };
  3588. Blockly.Python['ai_vision_barcodes_get_info_result'] = function (block) {
  3589. Blockly.Python.addFunction("getBarcodesResult", `def getBarcodesResult():
  3590. global findBarcodes
  3591. if len(findBarcodes)>0:
  3592. return findBarcodes[0]["payload"]
  3593. else:
  3594. return ""
  3595. `)
  3596. var code = `getBarcodesResult()`;
  3597. return [code, Blockly.Python.ORDER_NONE];
  3598. };
  3599. Blockly.Blocks['ai_vision_get_circles'] = {
  3600. init: function () {
  3601. this.appendDummyInput()
  3602. .appendField(Blockly.Msg.ai_vision_from)
  3603. .appendField(new Blockly.FieldVariable("i"), "varitem")
  3604. .appendField(Blockly.Msg.ai_vision_find_circle_detected_result)
  3605. .appendField(new Blockly.FieldDropdown([
  3606. [Blockly.Msg.ai_vision_find_circle_detected_x, "x()"],
  3607. [Blockly.Msg.ai_vision_find_circle_detected_y, "y()"],
  3608. [Blockly.Msg.ai_vision_find_circle_detected_radius, "r()"]
  3609. ]), "blob_type");
  3610. this.setInputsInline(false);
  3611. this.setOutput(true, null);
  3612. this.setColour("#f0983e");
  3613. this.setTooltip(Blockly.Msg.ai_vision_get_circles_TOOLTIP);
  3614. this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
  3615. }
  3616. };
  3617. Blockly.Python['ai_vision_get_circles'] = function (block) {
  3618. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  3619. var type = block.getFieldValue('blob_type');
  3620. var code = variable_name + '.' + type;
  3621. return [code, Blockly.Python.ORDER_NONE];
  3622. };
  3623. Blockly.Blocks['ai_vision_get_rects'] = {
  3624. init: function () {
  3625. this.appendDummyInput()
  3626. .appendField(Blockly.Msg.ai_vision_from)
  3627. .appendField(new Blockly.FieldVariable("i"), "varitem")
  3628. .appendField(Blockly.Msg.ai_vision_find_rect_detected_result)
  3629. .appendField(new Blockly.FieldDropdown([
  3630. [Blockly.Msg.ai_vision_find_rect_detected_x, "x()"],
  3631. [Blockly.Msg.ai_vision_find_rect_detected_y, "y()"],
  3632. [Blockly.Msg.ai_vision_find_rect_detected_w, "w()"],
  3633. [Blockly.Msg.ai_vision_find_rect_detected_h, "h()"],
  3634. [Blockly.Msg.ai_vision_find_rect_detected_tuple, "rect()"],
  3635. [Blockly.Msg.ai_vision_find_rect_detected_corners, "corners()"]
  3636. ]), "blob_type");
  3637. this.setInputsInline(false);
  3638. this.setOutput(true, null);
  3639. this.setColour("#f0983e");
  3640. this.setTooltip(Blockly.Msg.ai_vision_get_rects_TOOLTIP);
  3641. this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
  3642. }
  3643. };
  3644. Blockly.Python['ai_vision_get_rects'] = function (block) {
  3645. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  3646. var type = block.getFieldValue('blob_type');
  3647. // TODO: Assemble Python into code variable.
  3648. var code = variable_name + '.' + type;
  3649. // TODO: Change ORDER_NONE to the correct strength.
  3650. return [code, Blockly.Python.ORDER_NONE];
  3651. };
  3652. Blockly.Blocks['ai_vision_get_lines'] = {
  3653. init: function () {
  3654. this.appendDummyInput()
  3655. .appendField(Blockly.Msg.ai_vision_from)
  3656. .appendField(new Blockly.FieldVariable("i"), "varitem")
  3657. .appendField(Blockly.Msg.ai_vision_find_line_detected_result)
  3658. .appendField(new Blockly.FieldDropdown([
  3659. [Blockly.Msg.ai_vision_find_line_detected_start_x, "x1()"],
  3660. [Blockly.Msg.ai_vision_find_line_detected_start_y, "y1()"],
  3661. [Blockly.Msg.ai_vision_find_line_detected_end_x, "x2()"],
  3662. [Blockly.Msg.ai_vision_find_line_detected_end_y, "y2()"],
  3663. [Blockly.Msg.ai_vision_find_line_detected_length, "length()"],
  3664. [Blockly.Msg.ai_vision_find_line_detected_angle, "theta()"]
  3665. ]), "blob_type");
  3666. this.setInputsInline(false);
  3667. this.setOutput(true, null);
  3668. this.setColour("#f0983e");
  3669. this.setTooltip(Blockly.Msg.ai_vision_get_lines_TOOLTIP);
  3670. this.setHelpUrl("https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find_circles");
  3671. }
  3672. };
  3673. Blockly.Python['ai_vision_get_lines'] = function (block) {
  3674. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  3675. var type = block.getFieldValue('blob_type');
  3676. // TODO: Assemble Python into code variable.
  3677. var code = variable_name + '.' + type;
  3678. // TODO: Change ORDER_NONE to the correct strength.
  3679. return [code, Blockly.Python.ORDER_NONE];
  3680. };
  3681. /*
  3682. _ ___ __ __ _ _
  3683. / \ |_ _| | \/ (_) ___ _ __ ___ _ __ | |__ ___ _ __ ___
  3684. / _ \ | | | |\/| | |/ __| '__/ _ \| '_ \| '_ \ / _ \| '_ \ / _ \
  3685. / ___ \ | | | | | | | (__| | | (_) | |_) | | | | (_) | | | | __/
  3686. /_/ \_\___| |_| |_|_|\___|_| \___/| .__/|_| |_|\___/|_| |_|\___|
  3687. |_|
  3688. */
  3689. Blockly.Blocks['ai_mphone_init'] = {
  3690. init: function () {
  3691. this.appendDummyInput()
  3692. .appendField(new Blockly.FieldImage("blockly/media/microphone_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  3693. this.appendDummyInput()
  3694. .appendField(Blockly.Msg.ai_microphone_init);
  3695. this.setPreviousStatement(true);
  3696. this.setNextStatement(true);
  3697. this.setColour("#5fcd8e");
  3698. this.setTooltip(Blockly.Msg.ai_mphone_init_TOOLTIP);
  3699. this.setHelpUrl("");
  3700. }
  3701. };
  3702. Blockly.Python.ai_mphone_init = function (block) {
  3703. Blockly.Python.definitions_.import_all_mphone = "" +
  3704. "from Maix import GPIO, I2S, FFT\n" +
  3705. "from board import board_info\n" +
  3706. "from fpioa_manager import fm\n" +
  3707. "\n" +
  3708. "sample_rate = 38640\n" +
  3709. "sample_points = 1024\n" +
  3710. "fft_points = 512\n" +
  3711. "hist_x_num = 50\n" +
  3712. "\n" +
  3713. "fm.register(20,fm.fpioa.I2S0_IN_D0, force=True)\n" +
  3714. "fm.register(19,fm.fpioa.I2S0_WS, force=True)\n" +
  3715. "fm.register(18,fm.fpioa.I2S0_SCLK, force=True)\n" +
  3716. "\n" +
  3717. "_recorder_rx = I2S(I2S.DEVICE_0)\n" +
  3718. "_recorder_rx.channel_config(_recorder_rx.CHANNEL_0, _recorder_rx.RECEIVER, align_mode = I2S.STANDARD_MODE)\n" +
  3719. "_recorder_rx.set_sample_rate(sample_rate)\n" +
  3720. "def _microphone_read_average(lst): \n" +
  3721. " return int((sum(lst)/len(lst))*100)\n" +
  3722. "";
  3723. // Blockly.Python.addVariable('sample_rate', "sample_rate = 38640", true);
  3724. // Blockly.Python.addVariable('sample_points', "sample_points = 1024", true);
  3725. // Blockly.Python.addVariable('fft_points', "fft_points = 512", true);
  3726. // Blockly.Python.addVariable('hist_x_num', "hist_x_num = 50", true);
  3727. // Blockly.Python.addVariable('_recorder_rx', "_recorder_rx = None", true);
  3728. // Blockly.Python.addVariable('fft_res', "fft_res = None", true);
  3729. // Blockly.Python.addVariable('fft_amp', "fft_amp = None", true);
  3730. Blockly.Python.addVariable('read_all_channel', "read_all_channel = [0, 0, 0, 0, 0, 0, 0, 0]", true);
  3731. // Blockly.Python.addVariable('_recorder_rx', "", true);
  3732. var _code = "" +
  3733. "";
  3734. return _code;
  3735. }
  3736. Blockly.Blocks['mphone_audio_spectrum_init'] = {
  3737. init: function () {
  3738. this.appendDummyInput()
  3739. .appendField(new Blockly.FieldImage("blockly/media/ai_audio_spectrum.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  3740. this.appendDummyInput()
  3741. .appendField(Blockly.Msg.ai_microphone_start_spectrum_analyze);
  3742. this.setPreviousStatement(true, null);
  3743. this.setNextStatement(true, null);
  3744. this.setColour("#5fcd8e");
  3745. this.setTooltip(Blockly.Msg.mphone_audio_spectrum_init_TOOLTIP);
  3746. this.setHelpUrl("");
  3747. }
  3748. };
  3749. Blockly.Python['mphone_audio_spectrum_init'] = function (block) {
  3750. // TODO: Assemble Python into code variable.
  3751. Blockly.Python.definitions_['v831_import_time'] = `import time`
  3752. var code = "" +
  3753. "time.sleep_ms(10)\n" +
  3754. "_audio = _recorder_rx.record(sample_points)\n" +
  3755. "fft_res = FFT.run(_audio.to_bytes(),fft_points)\n" +
  3756. "fft_amp = FFT.amplitude(fft_res)\n\n" +
  3757. "read_all_channel[0] = int(_microphone_read_average(fft_amp[0:63]))\n" +
  3758. "read_all_channel[1] = int(_microphone_read_average(fft_amp[64:127]))\n" +
  3759. "read_all_channel[2] = int(_microphone_read_average(fft_amp[127:191]))\n" +
  3760. "read_all_channel[3] = int(_microphone_read_average(fft_amp[192:255]))\n" +
  3761. "read_all_channel[4] = int(_microphone_read_average(fft_amp[256:319]))\n" +
  3762. "read_all_channel[5] = int(_microphone_read_average(fft_amp[320:383]))\n" +
  3763. "read_all_channel[6] = int(_microphone_read_average(fft_amp[384:447]))\n" +
  3764. "read_all_channel[7] = int(_microphone_read_average(fft_amp[448:514]))\n" +
  3765. "";
  3766. return code;
  3767. };
  3768. Blockly.Blocks['ai_mphone_read_sound_sensitivity'] = {
  3769. init: function () {
  3770. this.appendDummyInput()
  3771. .appendField(Blockly.Msg.ai_microphone_get_spectrum_analyze_result)
  3772. .appendField(new Blockly.FieldDropdown([
  3773. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_1, "0"],
  3774. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_2, "1"],
  3775. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_3, "2"],
  3776. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_4, "3"],
  3777. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_5, "4"],
  3778. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_6, "5"],
  3779. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_7, "6"],
  3780. [Blockly.Msg.ai_microphone_get_spectrum_analyze_result_8, "7"]
  3781. ]), "level");
  3782. this.setOutput(true, null);
  3783. this.setColour("#5fcd8e");
  3784. this.setTooltip(Blockly.Msg.ai_mphone_read_sound_sensitivity_TOOLTIP);
  3785. this.setHelpUrl("");
  3786. }
  3787. };
  3788. Blockly.Python['ai_mphone_read_sound_sensitivity'] = function (block) {
  3789. var dropdown_level = block.getFieldValue('level');
  3790. // TODO: Assemble Python into code variable.
  3791. var code = 'read_all_channel[' + dropdown_level + ']';
  3792. // TODO: Change ORDER_NONE to the correct strength.
  3793. return [code, Blockly.Python.ORDER_NONE];
  3794. };
  3795. Blockly.Blocks['ai_mphone_Record_audio_init'] = {
  3796. init: function () {
  3797. this.appendDummyInput()
  3798. .appendField(new Blockly.FieldImage("blockly/media/microphone_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  3799. this.appendDummyInput()
  3800. .appendField(Blockly.Msg.ai_mphone_Record_audio_init);
  3801. this.setPreviousStatement(true);
  3802. this.setNextStatement(true);
  3803. this.setColour("#5fcd8e");
  3804. this.setTooltip(Blockly.Msg.ai_mphone_Record_audio_init_TOOLTIP);
  3805. this.setHelpUrl("");
  3806. }
  3807. };
  3808. Blockly.Python.ai_mphone_Record_audio_init = function (block) {
  3809. Blockly.Python.definitions_.import_ai_mphone_Record_audio_init = "" +
  3810. "from Maix import GPIO, I2S\n" +
  3811. "from fpioa_manager import fm\n" +
  3812. 'import audio\n' +
  3813. "\n" +
  3814. '# user setting\n' +
  3815. "sample_rate = 16000\n" +
  3816. "# default seting\n" +
  3817. "sample_points = 2048\n" +
  3818. "wav_ch = 2\n" +
  3819. "\n" +
  3820. 'fm.register(8, fm.fpioa.GPIO0, force=True)\n' +
  3821. 'wifi_en = GPIO(GPIO.GPIO0, GPIO.OUT)\n' +
  3822. 'wifi_en.value(0)\n' +
  3823. "\n" +
  3824. "fm.register(20,fm.fpioa.I2S0_IN_D0, force=True)\n" +
  3825. "fm.register(19,fm.fpioa.I2S0_WS, force=True)\n" +
  3826. "fm.register(18,fm.fpioa.I2S0_SCLK, force=True)\n" +
  3827. "\n" +
  3828. "rx = I2S(I2S.DEVICE_0)\n" +
  3829. "rx.channel_config(rx.CHANNEL_0, rx.RECEIVER, align_mode=I2S.STANDARD_MODE)\n" +
  3830. "rx.set_sample_rate(sample_rate)\n" +
  3831. "print(rx)\n" +
  3832. "";
  3833. var _code = "" +
  3834. "";
  3835. return _code;
  3836. }
  3837. Blockly.Blocks['ai_mphone_record_init'] = {
  3838. init: function () {
  3839. this.appendDummyInput()
  3840. .appendField(Blockly.Msg.ai_microphone_init);
  3841. this.setPreviousStatement(true);
  3842. this.setNextStatement(true, null);
  3843. this.setColour("#5fcd8e");
  3844. this.setTooltip(Blockly.Msg.ai_microphone_init);
  3845. this.setHelpUrl("");
  3846. }
  3847. }
  3848. Blockly.Python['ai_mphone_record_init'] = function (block) {
  3849. Blockly.Python.definitions_['v831_import_pyaudio'] = `import pyaudio`
  3850. Blockly.Python.definitions_['v831_import_wave'] = `import wave`
  3851. Blockly.Python.definitions_['v831_import_os'] = `import os`
  3852. let code = ''
  3853. return code;
  3854. }
  3855. Blockly.Blocks['ai_mphone_record_setting'] = {
  3856. init: function () {
  3857. this.appendValueInput("record_time").setCheck(null)
  3858. .appendField(Blockly.Msg.ai_mphone_Record_audio_start_time)
  3859. this.appendValueInput("path")
  3860. .setCheck(null)
  3861. .appendField(Blockly.Msg.ai_mphone_Record_audio_start_path);
  3862. this.appendDummyInput()
  3863. .appendField(Blockly.Msg.ai_mphone_Record_audio_start);
  3864. this.setPreviousStatement(true);
  3865. this.setNextStatement(true, null);
  3866. this.setInputsInline(false);
  3867. this.setColour("#5fcd8e");
  3868. this.setTooltip(Blockly.Msg.ai_mphone_Record_audio_start);
  3869. this.setHelpUrl("");
  3870. }
  3871. }
  3872. Blockly.Python['ai_mphone_record_setting'] = function (block) {
  3873. var value_path = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  3874. var record_time = Blockly.Python.valueToCode(block, 'record_time', Blockly.Python.ORDER_ATOMIC);
  3875. Blockly.Python.definitions_['v831_import_pyaudio'] = `import pyaudio`
  3876. Blockly.Python.definitions_['v831_import_wave'] = `import wave`
  3877. Blockly.Python.definitions_['v831_import_os'] = `import os`
  3878. let code = `if(os.path.exists(${value_path})):
  3879. os.system("rm ${value_path.slice(1, value_path.length - 1)}")
  3880. CHUNK = 1024
  3881. FORMAT = pyaudio.paInt16
  3882. CHANNELS = 1
  3883. RATE = 16000
  3884. RECORD_SECONDS = ${Number(record_time) + 1}
  3885. WAVE_OUTPUT_FILENAME = ${value_path}
  3886. p = pyaudio.PyAudio()
  3887. stream = p.open(format=FORMAT,channels=CHANNELS,rate=RATE,input=True,frames_per_buffer=CHUNK)
  3888. print("* recording")
  3889. frames = []
  3890. for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
  3891. data = stream.read(CHUNK,exception_on_overflow = False)
  3892. frames.append(data)
  3893. print("* done recording")
  3894. stream.stop_stream()
  3895. stream.close()
  3896. p.terminate()
  3897. wf = wave.open(WAVE_OUTPUT_FILENAME, "wb")
  3898. wf.setnchannels(CHANNELS)
  3899. wf.setsampwidth(p.get_sample_size(FORMAT))
  3900. wf.setframerate(RATE)
  3901. wf.writeframes(b"".join(frames))
  3902. wf.close()
  3903. `
  3904. return code;
  3905. }
  3906. Blockly.Blocks['ai_mphone_Record_audio_start'] = {
  3907. init: function () {
  3908. this.appendDummyInput()
  3909. .appendField(Blockly.Msg.ai_mphone_Record_audio_start);
  3910. this.setPreviousStatement(true);
  3911. this.setNextStatement(true, null);
  3912. this.setColour("#5fcd8e");
  3913. this.setTooltip(Blockly.Msg.ai_mphone_Record_audio_start_TOOLTIP);
  3914. this.setHelpUrl("");
  3915. }
  3916. };
  3917. Blockly.Python['ai_mphone_Record_audio_start'] = function (block) {
  3918. var code = `
  3919. print("* recording")
  3920. frames = []
  3921. for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
  3922. data = stream.read(CHUNK,exception_on_overflow = False)
  3923. frames.append(data)
  3924. print("* done recording")
  3925. stream.stop_stream()
  3926. stream.close()
  3927. p.terminate()
  3928. wf = wave.open(WAVE_OUTPUT_FILENAME, "wb")
  3929. wf.setnchannels(CHANNELS)
  3930. wf.setsampwidth(p.get_sample_size(FORMAT))
  3931. wf.setframerate(RATE)
  3932. wf.writeframes(b"".join(frames))
  3933. wf.close()
  3934. `;
  3935. return code;
  3936. };
  3937. Blockly.Blocks['ai_audio_start_recording'] = {
  3938. init: function () {
  3939. this.appendDummyInput()
  3940. .appendField(Blockly.Msg.ai_audio_play_music_file);
  3941. this.appendValueInput("path")
  3942. .setCheck(null)
  3943. .appendField(Blockly.Msg.ai_audio_play_music_file_set_file_path);
  3944. this.setPreviousStatement(true, null);
  3945. this.setNextStatement(true, null);
  3946. this.setColour("#5fcd8e");
  3947. this.setTooltip(Blockly.Msg.ai_audio_stop_recording_TOOLTIP);
  3948. this.setHelpUrl("");
  3949. }
  3950. };
  3951. Blockly.Python['ai_audio_start_recording'] = function (block) {
  3952. // TODO: Assemble Python into code variable.
  3953. Blockly.Python.definitions_['v831_import_pyaudio'] = `import pyaudio`
  3954. Blockly.Python.definitions_['v831_import_wave'] = `import wave`
  3955. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  3956. sys.path.append("/root/")`
  3957. var code = `
  3958. CHUNK = 1024
  3959. wf = wave.open(r"test.wav", "rb")#(sys.argv[1], "rb"
  3960. p = pyaudio.PyAudio()
  3961. stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
  3962. channels=wf.getnchannels(),
  3963. rate=wf.getframerate(),
  3964. output=True)
  3965. data = wf.readframes(CHUNK)
  3966. while len(data) > 0:
  3967. stream.write(data)
  3968. data = wf.readframes(CHUNK)
  3969. stream.stop_stream()
  3970. stream.close()
  3971. p.terminate()
  3972. `;
  3973. return code;
  3974. };
  3975. /*
  3976. _ ___ _ _ _
  3977. / \ |_ _| / \ _ _ __| (_) ___
  3978. / _ \ | | / _ \| | | |/ _` | |/ _ \
  3979. / ___ \ | | / ___ \ |_| | (_| | | (_) |
  3980. /_/ \_\___| /_/ \_\__,_|\__,_|_|\___/
  3981. */
  3982. Blockly.Blocks['ai_audio_play_init'] = {
  3983. init: function () {
  3984. this.appendDummyInput()
  3985. .appendField(Blockly.Msg.AUDIO_S);
  3986. this.setPreviousStatement(true, null);
  3987. this.setNextStatement(true, null);
  3988. this.setColour("#603ea0");
  3989. this.setTooltip("");
  3990. this.setHelpUrl("");
  3991. }
  3992. };
  3993. Blockly.Python['ai_audio_play_init'] = function (block) {
  3994. Blockly.Python.definitions_['v831_import_os'] = `import os`
  3995. Blockly.Python.definitions_['v831_import_pyaudio'] = `import pyaudio`
  3996. Blockly.Python.definitions_['v831_import_wave'] = `import wave`
  3997. Blockly.Python.definitions_['v831_import_time'] = `import time`
  3998. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  3999. sys.path.append("/root/")`
  4000. Blockly.Python.addVariable("VOICESTATE", "VOICESTATE = 0", true)
  4001. Blockly.Python.addVariable("VOICEDATA", "", true)
  4002. Blockly.Python.addVariable("VOICESTREAM", 'VOICESTREAM = ""', true)
  4003. Blockly.Python.addVariable("VOICEPYAUDIO", "VOICEPYAUDIO = pyaudio.PyAudio()", true)
  4004. Blockly.Python.addVariable("CHUNK", "CHUNK = 1024", true)
  4005. Blockly.Python.addVariable("VOICEPPATH", "", true)
  4006. Blockly.Python.addVariable("VOICEPLAYSTATE", 'VOICEPLAYSTATE = True', true)
  4007. Blockly.Python.addVariable("VOICEWF", 'VOICEWF = ""', true)
  4008. var _code = ``;
  4009. return _code;
  4010. };
  4011. Blockly.Blocks['ai_audio_play_volume'] = {
  4012. init: function () {
  4013. this.appendDummyInput()
  4014. .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol)
  4015. .appendField(new Blockly.FieldNumber(25, 0, 100), "play_vol")
  4016. .appendField("(0~100)");
  4017. this.setPreviousStatement(true, null);
  4018. this.setNextStatement(true, null);
  4019. this.setColour("#603ea0");
  4020. this.setTooltip("");
  4021. this.setHelpUrl("");
  4022. }
  4023. };
  4024. Blockly.Python['ai_audio_play_volume'] = function (block) {
  4025. var number_play_vol = block.getFieldValue('play_vol');
  4026. Blockly.Python.definitions_['systemVoice_numberMap'] = `def voice_numberMap(value):
  4027. valueScaled = float(value - 0) / float(100)
  4028. return int(valueScaled * 31)
  4029. `
  4030. var _code = `VOICENUMP = str(voice_numberMap(${number_play_vol}))
  4031. time.sleep(0.01)
  4032. SYSTEMVOICE = "amixer cset numid=8,iface=MIXER,name=\\"LINEOUT volume\\" "+ VOICENUMP+""
  4033. `;
  4034. return _code;
  4035. };
  4036. Blockly.Blocks['ai_audio_play_song'] = {
  4037. init: function () {
  4038. this.appendValueInput("path")
  4039. .setCheck(null)
  4040. .appendField(Blockly.Msg.ai_audio_play_music_file_set_file_path)
  4041. this.appendDummyInput()
  4042. .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol)
  4043. .appendField(new Blockly.FieldNumber(25, 0, 100), "play_vol")
  4044. .appendField("(0~100)");
  4045. this.appendDummyInput()
  4046. .appendField(Blockly.Msg.ai_audio_start_play);
  4047. this.setPreviousStatement(true, null);
  4048. this.setNextStatement(true, null);
  4049. this.setColour("#603ea0");
  4050. this.setTooltip(Blockly.Msg.ai_audio_start_play);
  4051. this.setHelpUrl("");
  4052. }
  4053. };
  4054. Blockly.Python['ai_audio_play_song'] = function (block) {
  4055. var path_to_video = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  4056. var number_play_vol = block.getFieldValue('play_vol');
  4057. Blockly.Python.definitions_['v831_import_os'] = `import os`
  4058. Blockly.Python.definitions_['v831_import_pyaudio'] = `import pyaudio`
  4059. Blockly.Python.definitions_['v831_import_wave'] = `import wave`
  4060. Blockly.Python.definitions_['v831_import_time'] = `import time`
  4061. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  4062. sys.path.append("/root/")`
  4063. Blockly.Python.addVariable("CHUNK", "CHUNK = 1024", true)
  4064. Blockly.Python.definitions_['systemVoice_numberMap'] = `def voice_numberMap(value):
  4065. valueScaled = float(value - 0) / float(100)
  4066. return int(valueScaled * 31)
  4067. VOICENUMP = str(voice_numberMap(${number_play_vol}))
  4068. time.sleep(0.01)
  4069. SYSTEMVOICE = "amixer cset numid=8,iface=MIXER,name=\\"LINEOUT volume\\" "+ VOICENUMP+""
  4070. os.system(SYSTEMVOICE)
  4071. `
  4072. // Blockly.Python.addVariable("VOICEPPATH","",false)
  4073. var _code = `
  4074. wf = wave.open(${path_to_video}, "rb")#(sys.argv[1], "rb"
  4075. p = pyaudio.PyAudio()
  4076. stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),channels=wf.getnchannels(),rate=wf.getframerate(),output=True)
  4077. data = wf.readframes(CHUNK)
  4078. while len(data) > 0:
  4079. stream.write(data)
  4080. data = wf.readframes(CHUNK)
  4081. stream.stop_stream()
  4082. stream.close()
  4083. p.terminate()
  4084. `;
  4085. return _code;
  4086. };
  4087. Blockly.Blocks['ai_audio_play_start'] = {
  4088. init: function () {
  4089. this.appendDummyInput()
  4090. .appendField(Blockly.Msg.ai_audio_start_play);
  4091. this.setPreviousStatement(true, null);
  4092. this.setNextStatement(true, null);
  4093. this.setColour("#603ea0");
  4094. this.setTooltip(Blockly.Msg.ai_audio_start_play);
  4095. this.setHelpUrl("");
  4096. }
  4097. }
  4098. Blockly.Python['ai_audio_play_start'] = function (block) {
  4099. Blockly.Python.definitions_["VoicePlayState"] = `def VoicePlayState():
  4100. global VOICESTATE,VOICEDATA,CHUNK,VOICESTREAM,VOICEPYAUDIO,VOICEWF,VOICEPPATH
  4101. if VOICESTATE == 0:
  4102. VOICEWF = wave.open(VOICEPPATH, "rb")#(sys.argv[1], "rb"
  4103. VOICEPYAUDIO = pyaudio.PyAudio()
  4104. VOICESTREAM = VOICEPYAUDIO.open(format=VOICEPYAUDIO.get_format_from_width(VOICEWF.getsampwidth()),channels=VOICEWF.getnchannels(),rate=VOICEWF.getframerate(),output=True)
  4105. VOICEDATA = VOICEWF.readframes(CHUNK)
  4106. VOICESTATE = 1
  4107. return True
  4108. else:
  4109. if len(VOICEDATA) > 0:
  4110. try:
  4111. VOICESTREAM.write(VOICEDATA)
  4112. VOICEDATA = VOICEWF.readframes(CHUNK)
  4113. return True
  4114. except:
  4115. VOICESTATE = 0
  4116. else:
  4117. VOICESTREAM.stop_stream()
  4118. VOICESTREAM.close()
  4119. VOICEPYAUDIO.terminate()
  4120. VOICESTATE = 0
  4121. return False
  4122. `
  4123. let code = `VOICEPLAYSTATE = VoicePlayState()
  4124. `
  4125. return code;
  4126. }
  4127. Blockly.Blocks['ai_audio_play_finish'] = {
  4128. init: function () {
  4129. this.appendDummyInput()
  4130. .appendField(Blockly.Msg.ai_audio_end_play);
  4131. this.setPreviousStatement(true, null);
  4132. this.setNextStatement(true, null);
  4133. this.setColour("#603ea0");
  4134. this.setTooltip("");
  4135. this.setHelpUrl("");
  4136. }
  4137. };
  4138. Blockly.Python['ai_audio_play_finish'] = function (block) {
  4139. // TODO: Assemble Python into code variable.
  4140. var code = `try:
  4141. VOICESTREAM.stop_stream()
  4142. VOICESTREAM.close()
  4143. VOICEPYAUDIO.terminate()
  4144. VOICESTATE = 0
  4145. except:
  4146. pass
  4147. `;
  4148. return code;
  4149. };
  4150. Blockly.Blocks['ai_audio_get_play_status'] = {
  4151. init: function () {
  4152. this.appendDummyInput()
  4153. .appendField(Blockly.Msg.ai_audio_get_play_status);
  4154. this.setOutput(true, null);
  4155. this.setColour("#603ea0");
  4156. this.setTooltip(Blockly.Msg.ai_audio_play_state_speaker_TOOLTIP);
  4157. this.setHelpUrl("");
  4158. }
  4159. };
  4160. Blockly.Python['ai_audio_get_play_status'] = function (block) {
  4161. // TODO: Assemble Python into code variable.
  4162. var code = 'VOICEPLAYSTATE';
  4163. // TODO: Change ORDER_NONE to the correct strength.
  4164. return [code, Blockly.Python.ORDER_NONE];
  4165. };
  4166. // 在线语音识别
  4167. Blockly.Blocks['ai_online_voice_init'] = {
  4168. init: function () {
  4169. this.appendDummyInput()
  4170. .appendField(Blockly.Msg.online_voice_init); //
  4171. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  4172. this.appendValueInput("B").setCheck(null).appendField('API Secret');
  4173. this.appendValueInput("C").setCheck(null).appendField('API Key'); //Blockly.Msg.online_voice_init_path
  4174. this.setPreviousStatement(true, null);
  4175. this.setNextStatement(true, null);
  4176. this.setColour("#0000ff");
  4177. this.setTooltip(Blockly.Msg.online_voice_init);
  4178. this.setHelpUrl("");
  4179. }
  4180. };
  4181. Blockly.Python['ai_online_voice_init'] = function (block) {
  4182. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  4183. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  4184. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  4185. // TODO: Assemble Python into code variable.
  4186. Blockly.Python.definitions_['v831_import_time'] = `import time`
  4187. Blockly.Python.definitions_['v831_import_websocket'] = `import websocket`
  4188. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  4189. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  4190. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  4191. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  4192. Blockly.Python.definitions_['v831_import_json'] = `import json`
  4193. Blockly.Python.definitions_['v831_import_urlencode'] = `from urllib.parse import urlencode`
  4194. Blockly.Python.definitions_['v831_import_ssl'] = `import ssl`
  4195. Blockly.Python.definitions_['v831_import_format_date_time'] = `from wsgiref.handlers import format_date_time`
  4196. Blockly.Python.definitions_['v831_import_from_time_mktime'] = `from time import mktime`
  4197. Blockly.Python.definitions_['v831_import_thread_as_thread'] = `import _thread as thread`
  4198. Blockly.Python.definitions_.ai_online_voice_init = `
  4199. STATUS_FIRST_FRAME = 0 # 第一帧的标识
  4200. STATUS_CONTINUE_FRAME = 1 # 中间帧标识
  4201. STATUS_LAST_FRAME = 2 # 最后一帧的标识
  4202. class Ws_Param_voice(object):
  4203. # 初始化
  4204. def __init__(self, APPID, APIKey, APISecret, AudioFile):
  4205. self.APPID_voice = APPID
  4206. self.APIKey_voice = APIKey
  4207. self.APISecret_voice = APISecret
  4208. self.AudioFile_voice = AudioFile
  4209. # 公共参数(common)
  4210. self.CommonArgs = {"app_id": self.APPID_voice}
  4211. # 业务参数(business),更多个性化参数可在官网查看
  4212. self.BusinessArgs = {"domain": "iat", "language": "zh_cn", "accent": "mandarin", "vinfo":1,"vad_eos":10000}
  4213. # 生成url
  4214. def create_url_voice(self):
  4215. url = "wss://ws-api.xfyun.cn/v2/iat"
  4216. # 生成RFC1123格式的时间戳
  4217. now = datetime.now()
  4218. date = format_date_time(mktime(now.timetuple()))
  4219. # 拼接字符串
  4220. signature_origin = "host: " + "ws-api.xfyun.cn" + "\\n"
  4221. signature_origin += "date: " + date + "\\n"
  4222. signature_origin += "GET " + "/v2/iat " + "HTTP/1.1"
  4223. # 进行hmac-sha256进行加密
  4224. signature_sha = hmac.new(self.APISecret_voice .encode("utf-8"), signature_origin.encode("utf-8"),
  4225. digestmod=hashlib.sha256).digest()
  4226. signature_sha = base64.b64encode(signature_sha).decode(encoding="utf-8")
  4227. authorization_origin = "api_key=\\"%s\\", algorithm=\\"%s\\", headers=\\"%s\\", signature=\\"%s\\"" % (self.APIKey_voice , "hmac-sha256", "host date request-line", signature_sha)
  4228. authorization = base64.b64encode(authorization_origin.encode("utf-8")).decode(encoding="utf-8")
  4229. # 将请求的鉴权参数组合为字典
  4230. v = {
  4231. "authorization": authorization,
  4232. "date": date,
  4233. "host": "ws-api.xfyun.cn"
  4234. }
  4235. # 拼接鉴权参数,生成url
  4236. url = url + "?" + urlencode(v)
  4237. # print("date: ",date)
  4238. # print("v: ",v)
  4239. # 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释,比对相同参数时生成的url与自己代码生成的url是否一致
  4240. # print('websocket url :', url)
  4241. return url
  4242. # 收到websocket消息的处理
  4243. def on_message_voice(ws, message):
  4244. global asr_result,txt_result
  4245. try:
  4246. code = json.loads(message)["code"]
  4247. sid = json.loads(message)["sid"]
  4248. if code != 0:
  4249. errMsg = json.loads(message)["message"]
  4250. print("sid:%s call error:%s code is:%s" % (sid, errMsg, code))
  4251. else:
  4252. data = json.loads(message)["data"]["result"]["ws"]
  4253. # print(json.loads(message))
  4254. result = ""
  4255. for i in data:
  4256. for w in i["cw"]:
  4257. result += w["w"]
  4258. print("sid:%s call success!,data is:%s" % (sid, json.dumps(data, ensure_ascii=False)))
  4259. print("json.dumps(data, ensure_ascii=False)",eval(json.dumps(data, ensure_ascii=False)))
  4260. asr_result.append(eval(json.dumps(data, ensure_ascii=False)))
  4261. txt_result.append(eval(json.dumps(data, ensure_ascii=False)[1]))
  4262. except Exception as e:
  4263. print("receive msg,but parse exception:", e)
  4264. # 收到websocket错误的处理
  4265. def on_error_voice(ws, error):
  4266. print("### error:", error)
  4267. # 收到websocket关闭的处理
  4268. def on_close_voice(ws):
  4269. print("### closed ###")
  4270. # 收到websocket连接建立的处理
  4271. def on_open_voice(ws):
  4272. def run_voice(*args):
  4273. global wsParam_voice
  4274. frameSize = 8000 # 每一帧的音频大小
  4275. intervel = 0.04 # 发送音频间隔(单位:s)
  4276. status = STATUS_FIRST_FRAME # 音频的状态信息,标识音频是第一帧,还是中间帧、最后一帧
  4277. with open(wsParam_voice.AudioFile_voice, "rb") as fp:
  4278. while True:
  4279. buf = fp.read(frameSize)
  4280. # 文件结束
  4281. if not buf:
  4282. status = STATUS_LAST_FRAME
  4283. # 第一帧处理
  4284. # 发送第一帧音频,带business 参数
  4285. # appid 必须带上,只需第一帧发送
  4286. if status == STATUS_FIRST_FRAME:
  4287. d = {"common": wsParam_voice.CommonArgs,
  4288. "business": wsParam_voice.BusinessArgs,
  4289. "data": {"status": 0, "format": "audio/L16;rate=16000",
  4290. "audio": str(base64.b64encode(buf), "utf-8"),
  4291. "encoding": "raw"}}
  4292. d = json.dumps(d)
  4293. ws.send(d)
  4294. status = STATUS_CONTINUE_FRAME
  4295. # 中间帧处理
  4296. elif status == STATUS_CONTINUE_FRAME:
  4297. d = {"data": {"status": 1, "format": "audio/L16;rate=16000",
  4298. "audio": str(base64.b64encode(buf), "utf-8"),
  4299. "encoding": "raw"}}
  4300. ws.send(json.dumps(d))
  4301. # 最后一帧处理
  4302. elif status == STATUS_LAST_FRAME:
  4303. d = {"data": {"status": 2, "format": "audio/L16;rate=16000",
  4304. "audio": str(base64.b64encode(buf), "utf-8"),
  4305. "encoding": "raw"}}
  4306. ws.send(json.dumps(d))
  4307. time.sleep(1)
  4308. break
  4309. # 模拟音频采样间隔
  4310. time.sleep(intervel)
  4311. ws.close()
  4312. thread.start_new_thread(run_voice, ())
  4313. asr_result=[]
  4314. txt_result=[]
  4315. APPId_voice = ${APPId}
  4316. APISecret_voice = ${APISecret}
  4317. APIKey_voice = ${APIKey}
  4318. `
  4319. var code = '';
  4320. return code;
  4321. };
  4322. // 在线语音识别加载
  4323. Blockly.Blocks['ai_online_voice_loading'] = {
  4324. init: function () {
  4325. this.appendDummyInput()
  4326. .appendField(Blockly.Msg.online_voice_loading); //
  4327. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  4328. this.appendValueInput("B").setCheck(null).appendField('API Secret');
  4329. this.appendValueInput("C").setCheck(null).appendField('API Key');
  4330. this.appendValueInput("path").setCheck(null).appendField(Blockly.Msg.online_voice_init_path);
  4331. this.setPreviousStatement(true, null);
  4332. this.setNextStatement(true, null);
  4333. this.setColour("#0000ff");
  4334. this.setTooltip(Blockly.Msg.online_voice_loading);
  4335. this.setHelpUrl("");
  4336. }
  4337. };
  4338. Blockly.Python['ai_online_voice_loading'] = function (block) {
  4339. var AudioFile = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  4340. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  4341. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  4342. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  4343. // TODO: Assemble Python into code variable.
  4344. Blockly.Python.definitions_['v831_import_time'] = `import time`
  4345. Blockly.Python.definitions_['v831_import_websocket'] = `import websocket`
  4346. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  4347. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  4348. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  4349. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  4350. Blockly.Python.definitions_['v831_import_json'] = `import json`
  4351. Blockly.Python.definitions_['v831_import_urlencode'] = `from urllib.parse import urlencode`
  4352. Blockly.Python.definitions_['v831_import_ssl'] = `import ssl`
  4353. Blockly.Python.definitions_['v831_import_format_date_time'] = `from wsgiref.handlers import format_date_time`
  4354. Blockly.Python.definitions_['v831_import_from_time_mktime'] = `from time import mktime`
  4355. Blockly.Python.definitions_['v831_import_thread_as_thread'] = `import _thread as thread`
  4356. Blockly.Python.definitions_.ai_online_voice_init = `
  4357. STATUS_FIRST_FRAME = 0 # 第一帧的标识
  4358. STATUS_CONTINUE_FRAME = 1 # 中间帧标识
  4359. STATUS_LAST_FRAME = 2 # 最后一帧的标识
  4360. class Ws_Param_voice(object):
  4361. # 初始化
  4362. def __init__(self, APPID, APIKey, APISecret, AudioFile):
  4363. self.APPID_voice = APPID
  4364. self.APIKey_voice = APIKey
  4365. self.APISecret_voice = APISecret
  4366. self.AudioFile_voice = AudioFile
  4367. # 公共参数(common)
  4368. self.CommonArgs = {"app_id": self.APPID_voice}
  4369. # 业务参数(business),更多个性化参数可在官网查看
  4370. self.BusinessArgs = {"domain": "iat", "language": "zh_cn", "accent": "mandarin", "vinfo":1,"vad_eos":10000}
  4371. # 生成url
  4372. def create_url_voice(self):
  4373. url = "wss://ws-api.xfyun.cn/v2/iat"
  4374. # 生成RFC1123格式的时间戳
  4375. now = datetime.now()
  4376. date = format_date_time(mktime(now.timetuple()))
  4377. # 拼接字符串
  4378. signature_origin = "host: " + "ws-api.xfyun.cn" + "\\n"
  4379. signature_origin += "date: " + date + "\\n"
  4380. signature_origin += "GET " + "/v2/iat " + "HTTP/1.1"
  4381. # 进行hmac-sha256进行加密
  4382. signature_sha = hmac.new(self.APISecret_voice .encode("utf-8"), signature_origin.encode("utf-8"),
  4383. digestmod=hashlib.sha256).digest()
  4384. signature_sha = base64.b64encode(signature_sha).decode(encoding="utf-8")
  4385. authorization_origin = "api_key=\\"%s\\", algorithm=\\"%s\\", headers=\\"%s\\", signature=\\"%s\\"" % (self.APIKey_voice , "hmac-sha256", "host date request-line", signature_sha)
  4386. authorization = base64.b64encode(authorization_origin.encode("utf-8")).decode(encoding="utf-8")
  4387. # 将请求的鉴权参数组合为字典
  4388. v = {
  4389. "authorization": authorization,
  4390. "date": date,
  4391. "host": "ws-api.xfyun.cn"
  4392. }
  4393. # 拼接鉴权参数,生成url
  4394. url = url + "?" + urlencode(v)
  4395. # print("date: ",date)
  4396. # print("v: ",v)
  4397. # 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释,比对相同参数时生成的url与自己代码生成的url是否一致
  4398. # print('websocket url :', url)
  4399. return url
  4400. # 收到websocket消息的处理
  4401. def on_message_voice(ws, message):
  4402. global asr_result,txt_result
  4403. try:
  4404. code = json.loads(message)["code"]
  4405. sid = json.loads(message)["sid"]
  4406. if code != 0:
  4407. errMsg = json.loads(message)["message"]
  4408. print("sid:%s call error:%s code is:%s" % (sid, errMsg, code))
  4409. else:
  4410. data = json.loads(message)["data"]["result"]["ws"]
  4411. # print(json.loads(message))
  4412. result = ""
  4413. for i in data:
  4414. for w in i["cw"]:
  4415. result += w["w"]
  4416. print("sid:%s call success!,data is:%s" % (sid, json.dumps(data, ensure_ascii=False)))
  4417. print("json.dumps(data, ensure_ascii=False)",eval(json.dumps(data, ensure_ascii=False)))
  4418. asr_result.append(eval(json.dumps(data, ensure_ascii=False)))
  4419. txt_result.append(eval(json.dumps(data, ensure_ascii=False)[1]))
  4420. except Exception as e:
  4421. print("receive msg,but parse exception:", e)
  4422. # 收到websocket错误的处理
  4423. def on_error_voice(ws, error):
  4424. print("### error:", error)
  4425. # 收到websocket关闭的处理
  4426. def on_close_voice(ws):
  4427. print("### closed ###")
  4428. # 收到websocket连接建立的处理
  4429. def on_open_voice(ws):
  4430. def run_voice(*args):
  4431. global wsParam_voice
  4432. frameSize = 8000 # 每一帧的音频大小
  4433. intervel = 0.04 # 发送音频间隔(单位:s)
  4434. status = STATUS_FIRST_FRAME # 音频的状态信息,标识音频是第一帧,还是中间帧、最后一帧
  4435. with open(wsParam_voice.AudioFile_voice, "rb") as fp:
  4436. while True:
  4437. buf = fp.read(frameSize)
  4438. # 文件结束
  4439. if not buf:
  4440. status = STATUS_LAST_FRAME
  4441. # 第一帧处理
  4442. # 发送第一帧音频,带business 参数
  4443. # appid 必须带上,只需第一帧发送
  4444. if status == STATUS_FIRST_FRAME:
  4445. d = {"common": wsParam_voice.CommonArgs,
  4446. "business": wsParam_voice.BusinessArgs,
  4447. "data": {"status": 0, "format": "audio/L16;rate=16000",
  4448. "audio": str(base64.b64encode(buf), "utf-8"),
  4449. "encoding": "raw"}}
  4450. d = json.dumps(d)
  4451. ws.send(d)
  4452. status = STATUS_CONTINUE_FRAME
  4453. # 中间帧处理
  4454. elif status == STATUS_CONTINUE_FRAME:
  4455. d = {"data": {"status": 1, "format": "audio/L16;rate=16000",
  4456. "audio": str(base64.b64encode(buf), "utf-8"),
  4457. "encoding": "raw"}}
  4458. ws.send(json.dumps(d))
  4459. # 最后一帧处理
  4460. elif status == STATUS_LAST_FRAME:
  4461. d = {"data": {"status": 2, "format": "audio/L16;rate=16000",
  4462. "audio": str(base64.b64encode(buf), "utf-8"),
  4463. "encoding": "raw"}}
  4464. ws.send(json.dumps(d))
  4465. time.sleep(1)
  4466. break
  4467. # 模拟音频采样间隔
  4468. time.sleep(intervel)
  4469. ws.close()
  4470. thread.start_new_thread(run_voice, ())
  4471. asr_result=[]
  4472. txt_result=[]
  4473. APPId_voice = ${APPId}
  4474. APISecret_voice = ${APISecret}
  4475. APIKey_voice = ${APIKey}
  4476. `
  4477. Blockly.Python.definitions_['get_online_voice_data'] = `asr_string=""
  4478. def getVoiceData():
  4479. global wsParam_voice,asr_string,txt_result,asr_result
  4480. # 测试时候在此处正确填写相关信息即可运行
  4481. asr_string = ""
  4482. txt_result = []
  4483. asr_result = []
  4484. time1 = datetime.now()
  4485. wsParam_voice = Ws_Param_voice(APPID=APPId_voice, APISecret=APISecret_voice,
  4486. APIKey=APIKey_voice,
  4487. AudioFile=${AudioFile})
  4488. websocket.enableTrace(False)
  4489. wsUrl = wsParam_voice.create_url_voice()
  4490. ws = websocket.WebSocketApp(wsUrl, on_message=on_message_voice, on_error=on_error_voice)
  4491. ws.on_open = on_open_voice
  4492. ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
  4493. time2 = datetime.now()
  4494. for i in range(len(asr_result)):
  4495. for j in range(len(asr_result[i])):
  4496. txt_result.append(asr_result[i][j]["cw"][0]["w"])
  4497. print("txt_result:", txt_result)
  4498. for i in range(len(txt_result)):
  4499. asr_string=asr_string+txt_result[i]
  4500. print("asr_string:", asr_string)
  4501. return asr_string
  4502. `
  4503. let code = `VoiceResult = getVoiceData()
  4504. `;
  4505. return code;
  4506. };
  4507. Blockly.Blocks['ai_online_voice_result'] = {
  4508. init: function () {
  4509. this.appendDummyInput()
  4510. .appendField(Blockly.Msg.online_voice_result); //
  4511. this.setOutput(true, null);
  4512. this.setColour("#0000ff");
  4513. this.setTooltip(Blockly.Msg.online_voice_result);
  4514. this.setHelpUrl("");
  4515. }
  4516. };
  4517. Blockly.Python['ai_online_voice_result'] = function (block) {
  4518. let code = `VoiceResult`;
  4519. return [code, Blockly.Python.ORDER_ATOMIC];
  4520. };
  4521. // 在线人脸识别
  4522. Blockly.Blocks['ai_online_ident_init'] = {
  4523. init: function () {
  4524. this.appendDummyInput()
  4525. .appendField(Blockly.Msg.online_face_init); //
  4526. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  4527. this.appendValueInput("B").setCheck(null).appendField('API Secret');
  4528. this.appendValueInput("C").setCheck(null).appendField('API Key');
  4529. this.setPreviousStatement(true, null);
  4530. this.setNextStatement(true, null);
  4531. this.setColour("#0000ff");
  4532. this.setTooltip(Blockly.Msg.online_face_init);
  4533. this.setHelpUrl("");
  4534. }
  4535. };
  4536. Blockly.Python['ai_online_ident_init'] = function (block) {
  4537. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  4538. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  4539. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  4540. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  4541. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  4542. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  4543. Blockly.Python.definitions_['v831_import_from_time_mktime'] = `from time import mktime`
  4544. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  4545. Blockly.Python.definitions_['v831_import_format_date_time'] = `from wsgiref.handlers import format_date_time`
  4546. Blockly.Python.definitions_['v831_import_urlencode'] = `from urllib.parse import urlencode`
  4547. Blockly.Python.definitions_['v831_import_os'] = `import os`
  4548. Blockly.Python.definitions_['v831_import_traceback'] = `import traceback`
  4549. Blockly.Python.definitions_['v831_import_json'] = `import json`
  4550. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  4551. Blockly.Python.definitions_.ai_online_ident_init = `
  4552. class AssembleHeaderException(Exception):
  4553. def __init__(self, msg):
  4554. self.message = msg
  4555. class Url:
  4556. def __init__(this, host, path, schema):
  4557. this.host = host
  4558. this.path = path
  4559. this.schema = schema
  4560. pass
  4561. # 进行sha256加密和base64编码
  4562. def sha256base64(data):
  4563. sha256 = hashlib.sha256()
  4564. sha256.update(data)
  4565. digest = base64.b64encode(sha256.digest()).decode(encoding="utf-8")
  4566. return digest
  4567. def parse_url_face(requset_url):
  4568. stidx = requset_url.index("://")
  4569. host = requset_url[stidx + 3:]
  4570. schema = requset_url[:stidx + 3]
  4571. edidx = host.index("/")
  4572. if edidx <= 0:
  4573. raise AssembleHeaderException("invalid request url:" + requset_url)
  4574. path = host[edidx:]
  4575. host = host[:edidx]
  4576. u = Url(host, path, schema)
  4577. return u
  4578. def assemble_ws_auth_url_face(requset_url, method="GET", api_key="", api_secret=""):
  4579. u = parse_url_face(requset_url)
  4580. host = u.host
  4581. path = u.path
  4582. now = datetime.now()
  4583. date = format_date_time(mktime(now.timetuple()))
  4584. print(date)
  4585. # date = "Thu, 12 Dec 2019 01:57:27 GMT"
  4586. signature_origin = "host: {}\\ndate: {}\\n{} {} HTTP/1.1".format(host, date, method, path)
  4587. print(signature_origin)
  4588. signature_sha = hmac.new(api_secret.encode("utf-8"), signature_origin.encode("utf-8"),
  4589. digestmod=hashlib.sha256).digest()
  4590. signature_sha = base64.b64encode(signature_sha).decode(encoding="utf-8")
  4591. authorization_origin = "api_key=\\"%s\\", algorithm=\\"%s\\", headers=\\"%s\\", signature=\\"%s\\"" % (
  4592. api_key, "hmac-sha256", "host date request-line", signature_sha)
  4593. authorization = base64.b64encode(authorization_origin.encode("utf-8")).decode(encoding="utf-8")
  4594. print(authorization_origin)
  4595. values = {
  4596. "host": host,
  4597. "date": date,
  4598. "authorization": authorization
  4599. }
  4600. return requset_url + "?" + urlencode(values)
  4601. def gen_body_face(appid, img_path, server_id):
  4602. with open(img_path, "rb") as f:
  4603. img_data = f.read()
  4604. body = {
  4605. "header": {
  4606. "app_id": appid,
  4607. "status": 3
  4608. },
  4609. "parameter": {
  4610. server_id: {
  4611. "service_kind": "face_detect",
  4612. #"detect_points": "1", #检测特征点
  4613. #"detect_property": "1", #检测人脸属性
  4614. "face_detect_result": {
  4615. "encoding": "utf8",
  4616. "compress": "raw",
  4617. "format": "json"
  4618. }
  4619. }
  4620. },
  4621. "payload": {
  4622. "input1": {
  4623. "encoding": "jpg",
  4624. "status": 3,
  4625. "image": str(base64.b64encode(img_data), "utf-8")
  4626. }
  4627. }
  4628. }
  4629. return json.dumps(body)
  4630. APPId_face = ${APPId}
  4631. APISecret_face = ${APISecret}
  4632. APIKey_face = ${APIKey}
  4633. `;
  4634. let code = ''
  4635. return code;
  4636. };
  4637. // 在线人脸识别加载
  4638. Blockly.Blocks['ai_online_ident_loading'] = {
  4639. init: function () {
  4640. this.appendDummyInput()
  4641. .appendField(Blockly.Msg.online_face_loading); //
  4642. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  4643. this.appendValueInput("B").setCheck(null).appendField('API Secret');
  4644. this.appendValueInput("C").setCheck(null).appendField('API Key');
  4645. this.appendValueInput("path")
  4646. .setCheck(null)
  4647. .appendField(Blockly.Msg.online_face_init_path); //
  4648. this.setPreviousStatement(true, null);
  4649. this.setNextStatement(true, null);
  4650. this.setColour("#0000ff");
  4651. this.setTooltip(Blockly.Msg.online_face_loading);
  4652. this.setHelpUrl("");
  4653. }
  4654. };
  4655. Blockly.Python['ai_online_ident_loading'] = function (block) {
  4656. var path = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  4657. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  4658. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  4659. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  4660. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  4661. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  4662. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  4663. Blockly.Python.definitions_['v831_import_from_time_mktime'] = `from time import mktime`
  4664. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  4665. Blockly.Python.definitions_['v831_import_format_date_time'] = `from wsgiref.handlers import format_date_time`
  4666. Blockly.Python.definitions_['v831_import_urlencode'] = `from urllib.parse import urlencode`
  4667. Blockly.Python.definitions_['v831_import_os'] = `import os`
  4668. Blockly.Python.definitions_['v831_import_traceback'] = `import traceback`
  4669. Blockly.Python.definitions_['v831_import_json'] = `import json`
  4670. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  4671. Blockly.Python.definitions_.ai_online_ident_init = `
  4672. class AssembleHeaderException(Exception):
  4673. def __init__(self, msg):
  4674. self.message = msg
  4675. class Url:
  4676. def __init__(this, host, path, schema):
  4677. this.host = host
  4678. this.path = path
  4679. this.schema = schema
  4680. pass
  4681. # 进行sha256加密和base64编码
  4682. def sha256base64(data):
  4683. sha256 = hashlib.sha256()
  4684. sha256.update(data)
  4685. digest = base64.b64encode(sha256.digest()).decode(encoding="utf-8")
  4686. return digest
  4687. def parse_url_face(requset_url):
  4688. stidx = requset_url.index("://")
  4689. host = requset_url[stidx + 3:]
  4690. schema = requset_url[:stidx + 3]
  4691. edidx = host.index("/")
  4692. if edidx <= 0:
  4693. raise AssembleHeaderException("invalid request url:" + requset_url)
  4694. path = host[edidx:]
  4695. host = host[:edidx]
  4696. u = Url(host, path, schema)
  4697. return u
  4698. def assemble_ws_auth_url_face(requset_url, method="GET", api_key="", api_secret=""):
  4699. u = parse_url_face(requset_url)
  4700. host = u.host
  4701. path = u.path
  4702. now = datetime.now()
  4703. date = format_date_time(mktime(now.timetuple()))
  4704. print(date)
  4705. # date = "Thu, 12 Dec 2019 01:57:27 GMT"
  4706. signature_origin = "host: {}\\ndate: {}\\n{} {} HTTP/1.1".format(host, date, method, path)
  4707. print(signature_origin)
  4708. signature_sha = hmac.new(api_secret.encode("utf-8"), signature_origin.encode("utf-8"),
  4709. digestmod=hashlib.sha256).digest()
  4710. signature_sha = base64.b64encode(signature_sha).decode(encoding="utf-8")
  4711. authorization_origin = "api_key=\\"%s\\", algorithm=\\"%s\\", headers=\\"%s\\", signature=\\"%s\\"" % (
  4712. api_key, "hmac-sha256", "host date request-line", signature_sha)
  4713. authorization = base64.b64encode(authorization_origin.encode("utf-8")).decode(encoding="utf-8")
  4714. print(authorization_origin)
  4715. values = {
  4716. "host": host,
  4717. "date": date,
  4718. "authorization": authorization
  4719. }
  4720. return requset_url + "?" + urlencode(values)
  4721. def gen_body_face(appid, img_path, server_id):
  4722. with open(img_path, "rb") as f:
  4723. img_data = f.read()
  4724. body = {
  4725. "header": {
  4726. "app_id": appid,
  4727. "status": 3
  4728. },
  4729. "parameter": {
  4730. server_id: {
  4731. "service_kind": "face_detect",
  4732. #"detect_points": "1", #检测特征点
  4733. #"detect_property": "1", #检测人脸属性
  4734. "face_detect_result": {
  4735. "encoding": "utf8",
  4736. "compress": "raw",
  4737. "format": "json"
  4738. }
  4739. }
  4740. },
  4741. "payload": {
  4742. "input1": {
  4743. "encoding": "jpg",
  4744. "status": 3,
  4745. "image": str(base64.b64encode(img_data), "utf-8")
  4746. }
  4747. }
  4748. }
  4749. return json.dumps(body)
  4750. APPId_face = ${APPId}
  4751. APISecret_face = ${APISecret}
  4752. APIKey_face = ${APIKey}
  4753. `;
  4754. Blockly.Python.definitions_.online_ident = `
  4755. ONLINE_IDENT_DATA = ""
  4756. def run_face(appid, apikey, apisecret, img_path, server_id="s67c9c78c"):
  4757. url = "http://api.xf-yun.com/v1/private/{}".format(server_id)
  4758. request_url = assemble_ws_auth_url_face(url, "POST", apikey, apisecret)
  4759. headers = {"content-type": "application/json", "host": "api.xf-yun.com", "app_id": appid}
  4760. print(request_url)
  4761. response = requests.post(request_url, data=gen_body_face(appid, img_path, server_id), headers=headers)
  4762. resp_data = json.loads(response.content.decode("utf-8"))
  4763. print(resp_data)
  4764. picRes=base64.b64decode(resp_data["payload"]["face_detect_result"]["text"]).decode()
  4765. print(picRes)
  4766. with open("/root/pic0.txt","w") as f:
  4767. f.write(picRes)
  4768. return eval(picRes)
  4769. `
  4770. let code = `
  4771. if __name__ == "__main__":
  4772. img_path_face = ${path}
  4773. ONLINE_IDENT_DATA = run_face(appid=APPId_face,apisecret=APISecret_face,apikey=APIKey_face,img_path=img_path_face,)
  4774. `;
  4775. return code;
  4776. };
  4777. // 在线人脸识别结果
  4778. Blockly.Blocks['ai_online_ident_result'] = {
  4779. init: function () {
  4780. this.appendDummyInput()
  4781. .appendField(Blockly.Msg.online_face_result); //
  4782. this.setOutput(true, null);
  4783. this.setColour("#0000ff");
  4784. this.setTooltip(Blockly.Msg.online_face_result);
  4785. this.setHelpUrl("");
  4786. }
  4787. };
  4788. Blockly.Python['ai_online_ident_result'] = function (block) {
  4789. let code = `ONLINE_IDENT_DATA["face_num"]`;
  4790. return [code, Blockly.Python.ORDER_ATOMIC];
  4791. };
  4792. Blockly.Blocks['ai_online_ident_result_people'] = {
  4793. init: function () {
  4794. this.appendDummyInput()
  4795. .appendField(Blockly.Msg.serialcomm_write_item_first)
  4796. .appendField(new Blockly.FieldVariable("i"), "varitem")
  4797. .appendField(Blockly.Msg.face_information)
  4798. .appendField(new Blockly.FieldDropdown([
  4799. [Blockly.Msg.ai_models_object_model_x, "x"],
  4800. [Blockly.Msg.ai_models_object_model_y, "y"],
  4801. [Blockly.Msg.ai_models_object_model_w, "w"],
  4802. [Blockly.Msg.ai_models_object_model_h, "h"],
  4803. // ['人脸属性信息', "property"],
  4804. [Blockly.Msg.ai_models_object_model_confidence, "score"]
  4805. ]), "TYPE");
  4806. this.setOutput(true, null);
  4807. this.setColour("#0000ff");
  4808. this.setTooltip('');
  4809. this.setHelpUrl("");
  4810. }
  4811. }
  4812. Blockly.Python['ai_online_ident_result_people'] = function (block) {
  4813. var type = block.getFieldValue('TYPE');
  4814. var i = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  4815. let code = '';
  4816. if (type == "w") {
  4817. code = `ONLINE_IDENT_DATA.get('face_'+str(${i})).get('${type}') + ONLINE_IDENT_DATA.get('face_'+str(${i})).get('x')`
  4818. } else if (type == "h") {
  4819. code = `ONLINE_IDENT_DATA.get('face_'+str(${i})).get('${type}') + ONLINE_IDENT_DATA.get('face_'+str(${i})).get('y')`
  4820. } else {
  4821. code = `ONLINE_IDENT_DATA.get('face_'+str(${i})).get('${type}')`
  4822. }
  4823. return [code, Blockly.Python.ORDER_ATOMIC];
  4824. };
  4825. Blockly.Blocks['ai_audio_init'] = {
  4826. init: function () {
  4827. this.appendDummyInput()
  4828. .appendField(new Blockly.FieldImage("blockly/media/speaker_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  4829. this.appendDummyInput()
  4830. .appendField(Blockly.Msg.Text_to_speech_initialization); //
  4831. this.appendValueInput("A")
  4832. .setCheck(null)
  4833. .appendField('APP ID');
  4834. this.appendValueInput("B")
  4835. .setCheck(null)
  4836. .appendField('API Secret');
  4837. this.appendValueInput("C")
  4838. .setCheck(null)
  4839. .appendField('API Key');
  4840. this.setPreviousStatement(true, null);
  4841. this.setNextStatement(true, null);
  4842. this.setColour("#0000ff");
  4843. this.setTooltip(Blockly.Msg.Text_to_speech_initialization);
  4844. this.setHelpUrl("");
  4845. }
  4846. };
  4847. // 在线文本转语音初始化
  4848. Blockly.Python.ai_audio_init = function (block) {
  4849. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  4850. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  4851. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  4852. Blockly.Python.definitions_['v831_import_time'] = `import time`
  4853. Blockly.Python.definitions_['v831_import_os'] = `import os`
  4854. Blockly.Python.definitions_['v831_import_json'] = `import json`
  4855. Blockly.Python.definitions_['v831_import_wave'] = `import wave`
  4856. Blockly.Python.definitions_['v831_import_urlencode'] = `from urllib.parse import urlencode`
  4857. Blockly.Python.definitions_['v831_import_format_date_time'] = `from wsgiref.handlers import format_date_time`
  4858. Blockly.Python.definitions_['v831_import_from_time_mktime'] = `from time import mktime`
  4859. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  4860. // Blockly.Python.definitions_['v831_import_datetime'] = `import datetime`
  4861. Blockly.Python.definitions_['v831_import_websocket'] = `import websocket`
  4862. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  4863. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  4864. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  4865. Blockly.Python.definitions_['v831_import_ssl'] = `import ssl`
  4866. Blockly.Python.definitions_['v831_import_thread_as_thread'] = `import _thread as thread`
  4867. Blockly.Python.definitions_.ai_audio_init_function = `
  4868. STATUS_FIRST_FRAME = 0 # 第一帧的标识
  4869. STATUS_CONTINUE_FRAME = 1 # 中间帧标识
  4870. STATUS_LAST_FRAME = 2 # 最后一帧的标识
  4871. class Ws_Param_Audio(object):
  4872. # 初始化
  4873. def __init__(self, APPID, APIKey, APISecret, Text):
  4874. self.APPID = APPID
  4875. self.APIKey = APIKey
  4876. self.APISecret = APISecret
  4877. self.Text = Text
  4878. # 公共参数(common)
  4879. self.CommonArgs = {"app_id": self.APPID}
  4880. # 业务参数(business),更多个性化参数可在官网查看
  4881. self.BusinessArgs = {"aue": "raw", "auf": "audio/L16;rate=16000", "vcn": "xiaoyan", "tte": "utf8"}
  4882. self.Data = {"status": 2, "text": str(base64.b64encode(self.Text.encode("utf-8")), "UTF8")}
  4883. #使用小语种须使用以下方式,此处的unicode指的是 utf16小端的编码方式,即"UTF-16LE"”
  4884. #self.Data = {"status": 2, "text": str(base64.b64encode(self.Text.encode('utf-16')), "UTF8")}
  4885. # 生成url
  4886. def create_url(self):
  4887. url = "wss://tts-api.xfyun.cn/v2/tts"
  4888. # 生成RFC1123格式的时间戳
  4889. now = datetime.now()
  4890. date = format_date_time(mktime(now.timetuple()))
  4891. # 拼接字符串
  4892. signature_origin = "host: " + "ws-api.xfyun.cn" + "\\n"
  4893. signature_origin += "date: " + date + "\\n"
  4894. signature_origin += "GET " + "/v2/tts " + "HTTP/1.1"
  4895. # 进行hmac-sha256进行加密
  4896. signature_sha = hmac.new(self.APISecret.encode("utf-8"), signature_origin.encode("utf-8"),digestmod=hashlib.sha256).digest()
  4897. signature_sha = base64.b64encode(signature_sha).decode(encoding="utf-8")
  4898. authorization_origin = "api_key=\\"%s\\", algorithm=\\"%s\\", headers=\\"%s\\", signature=\\"%s\\"" % (self.APIKey, "hmac-sha256", "host date request-line", signature_sha)
  4899. authorization = base64.b64encode(authorization_origin.encode("utf-8")).decode(encoding="utf-8")
  4900. # 将请求的鉴权参数组合为字典
  4901. v = {
  4902. "authorization": authorization,
  4903. "date": date,
  4904. "host": "ws-api.xfyun.cn"
  4905. }
  4906. # 拼接鉴权参数,生成url
  4907. url = url + "?" + urlencode(v)
  4908. # print("date: ",date)
  4909. # print("v: ",v)
  4910. # 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释,比对相同参数时生成的url与自己代码生成的url是否一致
  4911. # print('websocket url :', url)
  4912. return url
  4913. APPId_Audio = ${APPId}
  4914. APISecret_Audio = ${APISecret}
  4915. APIKey_Audio = ${APIKey}
  4916. `;
  4917. var _code = "";
  4918. return _code;
  4919. }
  4920. Blockly.Blocks['ai_audio_play_state'] = {
  4921. init: function () {
  4922. this.appendDummyInput()
  4923. .appendField(Blockly.Msg.text_conversion_voice); //
  4924. this.appendValueInput("A")
  4925. .setCheck(null)
  4926. .appendField('APP ID');
  4927. this.appendValueInput("B")
  4928. .setCheck(null)
  4929. .appendField('API Secret');
  4930. this.appendValueInput("C")
  4931. .setCheck(null)
  4932. .appendField('API Key');
  4933. this.appendValueInput("NAME")
  4934. .setCheck(null)
  4935. .appendField(Blockly.Msg.Content_of_text); //
  4936. this.appendValueInput("path")
  4937. .setCheck(null)
  4938. .appendField(Blockly.Msg.Voice_path); //
  4939. this.setPreviousStatement(true, null);
  4940. this.setNextStatement(true, null);
  4941. this.setColour("#0000ff");
  4942. this.setTooltip(Blockly.Msg.text_conversion_voice);
  4943. this.setHelpUrl("");
  4944. }
  4945. };
  4946. Blockly.Python['ai_audio_play_state'] = function (block) {
  4947. // TODO: Assemble Python into code variable.
  4948. let text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC)
  4949. let url = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC)
  4950. let path = `${url.slice(1, url.length - 1)}`
  4951. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  4952. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  4953. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  4954. Blockly.Python.definitions_['v831_import_time'] = `import time`
  4955. Blockly.Python.definitions_['v831_import_os'] = `import os`
  4956. Blockly.Python.definitions_['v831_import_json'] = `import json`
  4957. Blockly.Python.definitions_['v831_import_wave'] = `import wave`
  4958. Blockly.Python.definitions_['v831_import_urlencode'] = `from urllib.parse import urlencode`
  4959. Blockly.Python.definitions_['v831_import_format_date_time'] = `from wsgiref.handlers import format_date_time`
  4960. Blockly.Python.definitions_['v831_import_from_time_mktime'] = `from time import mktime`
  4961. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  4962. // Blockly.Python.definitions_['v831_import_datetime'] = `import datetime`
  4963. Blockly.Python.definitions_['v831_import_websocket'] = `import websocket`
  4964. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  4965. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  4966. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  4967. Blockly.Python.definitions_['v831_import_ssl'] = `import ssl`
  4968. Blockly.Python.definitions_['v831_import_thread_as_thread'] = `import _thread as thread`
  4969. Blockly.Python.definitions_.ai_audio_init_function = `
  4970. STATUS_FIRST_FRAME = 0 # 第一帧的标识
  4971. STATUS_CONTINUE_FRAME = 1 # 中间帧标识
  4972. STATUS_LAST_FRAME = 2 # 最后一帧的标识
  4973. class Ws_Param_Audio(object):
  4974. # 初始化
  4975. def __init__(self, APPID, APIKey, APISecret, Text):
  4976. self.APPID = APPID
  4977. self.APIKey = APIKey
  4978. self.APISecret = APISecret
  4979. self.Text = Text
  4980. # 公共参数(common)
  4981. self.CommonArgs = {"app_id": self.APPID}
  4982. # 业务参数(business),更多个性化参数可在官网查看
  4983. self.BusinessArgs = {"aue": "raw", "auf": "audio/L16;rate=16000", "vcn": "xiaoyan", "tte": "utf8"}
  4984. self.Data = {"status": 2, "text": str(base64.b64encode(self.Text.encode("utf-8")), "UTF8")}
  4985. #使用小语种须使用以下方式,此处的unicode指的是 utf16小端的编码方式,即"UTF-16LE"”
  4986. #self.Data = {"status": 2, "text": str(base64.b64encode(self.Text.encode('utf-16')), "UTF8")}
  4987. # 生成url
  4988. def create_url(self):
  4989. url = "wss://tts-api.xfyun.cn/v2/tts"
  4990. # 生成RFC1123格式的时间戳
  4991. now = datetime.now()
  4992. date = format_date_time(mktime(now.timetuple()))
  4993. # 拼接字符串
  4994. signature_origin = "host: " + "ws-api.xfyun.cn" + "\\n"
  4995. signature_origin += "date: " + date + "\\n"
  4996. signature_origin += "GET " + "/v2/tts " + "HTTP/1.1"
  4997. # 进行hmac-sha256进行加密
  4998. signature_sha = hmac.new(self.APISecret.encode("utf-8"), signature_origin.encode("utf-8"),digestmod=hashlib.sha256).digest()
  4999. signature_sha = base64.b64encode(signature_sha).decode(encoding="utf-8")
  5000. authorization_origin = "api_key=\\"%s\\", algorithm=\\"%s\\", headers=\\"%s\\", signature=\\"%s\\"" % (self.APIKey, "hmac-sha256", "host date request-line", signature_sha)
  5001. authorization = base64.b64encode(authorization_origin.encode("utf-8")).decode(encoding="utf-8")
  5002. # 将请求的鉴权参数组合为字典
  5003. v = {
  5004. "authorization": authorization,
  5005. "date": date,
  5006. "host": "ws-api.xfyun.cn"
  5007. }
  5008. # 拼接鉴权参数,生成url
  5009. url = url + "?" + urlencode(v)
  5010. # print("date: ",date)
  5011. # print("v: ",v)
  5012. # 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释,比对相同参数时生成的url与自己代码生成的url是否一致
  5013. # print('websocket url :', url)
  5014. return url
  5015. APPId_Audio = ${APPId}
  5016. APISecret_Audio = ${APISecret}
  5017. APIKey_Audio = ${APIKey}
  5018. `;
  5019. Blockly.Python.definitions_.on_message = `
  5020. def on_message(ws, message):
  5021. try:
  5022. message =json.loads(message)
  5023. code = message["code"]
  5024. sid = message["sid"]
  5025. audio = message["data"]["audio"]
  5026. audio = base64.b64decode(audio)
  5027. status = message["data"]["status"]
  5028. #print(message)
  5029. if status == 2:
  5030. print("ws is closed")
  5031. ws.close()
  5032. if code != 0:
  5033. errMsg = message["message"]
  5034. print("sid:%s call error:%s code is:%s" % (sid, errMsg, code))
  5035. else:
  5036. pcm_path = "${path}"
  5037. with open(pcm_path, "ab") as f:
  5038. f.write(audio)
  5039. print("tts")
  5040. with open(pcm_path, "rb") as pcmfile:
  5041. pcmdata = pcmfile.read()
  5042. with wave.open(pcm_path + ".wav", "wb") as wavfile:
  5043. wavfile.setparams((1, 2, 16000, 0, "NONE", "NONE"))
  5044. wavfile.writeframes(pcmdata)
  5045. except Exception as e:
  5046. print("receive msg,but parse exception:", e)
  5047. # 收到websocket错误的处理
  5048. def on_error(ws, error):
  5049. print("### error:", error)
  5050. # 收到websocket关闭的处理
  5051. def on_close(ws):
  5052. print("### closed ###")
  5053. # 收到websocket连接建立的处理
  5054. def on_open(ws):
  5055. def run(*args):
  5056. global wsParam_Audio
  5057. d = {"common": wsParam_Audio.CommonArgs,"business": wsParam_Audio.BusinessArgs,"data": wsParam_Audio.Data,}
  5058. d = json.dumps(d)
  5059. print("------>开始发送文本数据")
  5060. ws.send(d)
  5061. if os.path.exists(${url}):
  5062. os.remove(${url})
  5063. thread.start_new_thread(run, ())
  5064. `
  5065. Blockly.Python.addVariable('wsParam_Audio', 'wsParam_Audio = ""', true);
  5066. let code = `if __name__ == "__main__":
  5067. #with open('word.txt', 'r',encoding="UTF-8")as f:#txt= f.read()
  5068. txt_Audio=${text}
  5069. if os.path.exists(${url}):
  5070. os.remove(${url})
  5071. # 测试时候在此处正确填写相关信息即可运行
  5072. wsParam_Audio = Ws_Param_Audio(APPID=APPId_Audio, APISecret=APISecret_Audio,APIKey=APIKey_Audio,Text=txt_Audio)
  5073. websocket.enableTrace(False)
  5074. wsUrl = wsParam_Audio.create_url()
  5075. ws = websocket.WebSocketApp(wsUrl, on_message=on_message, on_error=on_error, on_close=on_close)
  5076. ws.on_open = on_open
  5077. ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
  5078. `
  5079. // TODO: Change ORDER_NONE to the correct strength.
  5080. return code
  5081. };
  5082. // 在线物体识别
  5083. Blockly.Blocks['ai_online_object_init'] = {
  5084. init: function () {
  5085. this.appendDummyInput()
  5086. .appendField(Blockly.Msg.online_object_init); //
  5087. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  5088. // this.appendValueInput("B").setCheck(null).appendField('APISecret');
  5089. this.appendValueInput("C").setCheck(null).appendField('API Key'); //Blockly.Msg.online_voice_init_path
  5090. this.setPreviousStatement(true, null);
  5091. this.setNextStatement(true, null);
  5092. this.setColour("#0000ff");
  5093. this.setTooltip(Blockly.Msg.online_object_init);
  5094. this.setHelpUrl("");
  5095. }
  5096. };
  5097. Blockly.Python['ai_online_object_init'] = function (block) {
  5098. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  5099. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  5100. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  5101. // TODO: Assemble Python into code variable.
  5102. Blockly.Python.definitions_['v831_import_time'] = `import time`
  5103. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  5104. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  5105. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  5106. Blockly.Python.definitions_.ai_online_object_init = `
  5107. API_KEY = ${APIKey}
  5108. APPID = ${APPId}
  5109. def objectGetHeader(APPID, image_url, API_KEY):
  5110. with open(image_url, "rb") as f:
  5111. img_data = f.read()
  5112. curTime = str(int(time.time()))
  5113. paramBase64 = base64.b64encode(img_data)
  5114. tmp = str(paramBase64, "utf-8")
  5115. m2 = hashlib.md5()
  5116. m2.update((API_KEY + curTime + tmp).encode("utf-8"))
  5117. checkSum = m2.hexdigest()
  5118. header = {
  5119. 'X-CurTime': curTime,
  5120. 'X-Param': paramBase64,
  5121. 'X-Appid': APPID,
  5122. 'X-CheckSum': checkSum,
  5123. }
  5124. return header
  5125. `
  5126. var code = '';
  5127. return code;
  5128. };
  5129. Blockly.Blocks['ai_online_object_loading'] = {
  5130. init: function () {
  5131. this.appendDummyInput()
  5132. .appendField(Blockly.Msg.online_object_load); //
  5133. this.appendValueInput("path").setCheck(null).appendField(Blockly.Msg.online_face_init_path);
  5134. this.setPreviousStatement(true, null);
  5135. this.setNextStatement(true, null);
  5136. this.setColour("#0000ff");
  5137. this.setTooltip(Blockly.Msg.online_object_load);
  5138. this.setHelpUrl("");
  5139. }
  5140. }
  5141. Blockly.Python.ai_online_object_loading = function (block) {
  5142. var AudioFile = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  5143. let code = `ImageUrl = ${AudioFile}
  5144. OBJECTRESULT = requests.post("http://tupapi.xfyun.cn/v1/currency", headers=objectGetHeader(APPID, ImageUrl,API_KEY))
  5145. `
  5146. return code
  5147. }
  5148. Blockly.Blocks['ai_online_object_result'] = {
  5149. init: function () {
  5150. this.appendDummyInput()
  5151. .appendField(Blockly.Msg.online_object_result); //
  5152. this.setOutput(true, null);
  5153. this.setColour("#0000ff");
  5154. this.setTooltip(Blockly.Msg.online_object_result);
  5155. this.setHelpUrl("");
  5156. }
  5157. };
  5158. Blockly.Python['ai_online_object_result'] = function (block) {
  5159. let code = `OBJECTRESULT.content`;
  5160. return [code, Blockly.Python.ORDER_ATOMIC];
  5161. };
  5162. // 在线车牌识别
  5163. Blockly.Blocks['ai_online_card_init'] = {
  5164. init: function () {
  5165. this.appendDummyInput()
  5166. .appendField(Blockly.Msg.online_card_init); //
  5167. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  5168. this.appendValueInput("B").setCheck(null).appendField('API Secret');
  5169. this.appendValueInput("C").setCheck(null).appendField('API Key'); //Blockly.Msg.online_voice_init_path
  5170. this.setPreviousStatement(true, null);
  5171. this.setNextStatement(true, null);
  5172. this.setColour("#0000ff");
  5173. this.setTooltip(Blockly.Msg.online_card_init);
  5174. this.setHelpUrl("");
  5175. }
  5176. };
  5177. Blockly.Python['ai_online_card_init'] = function (block) {
  5178. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  5179. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  5180. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  5181. // TODO: Assemble Python into code variable.
  5182. Blockly.Python.definitions_['v831_import_urlencode'] = `from urllib.parse import urlencode`
  5183. Blockly.Python.definitions_['v831_import_format_date_time'] = `from wsgiref.handlers import format_date_time`
  5184. Blockly.Python.definitions_['v831_import_from_time_mktime'] = `from time import mktime`
  5185. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  5186. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  5187. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  5188. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  5189. Blockly.Python.definitions_['v831_import_traceback'] = `import traceback`
  5190. Blockly.Python.definitions_['v831_import_json'] = `import json`
  5191. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  5192. Blockly.Python.definitions_.ai_online_card_init = `
  5193. APPId_Card = ${APPId}
  5194. APISecret_Card = ${APISecret}
  5195. APIKey_Card = ${APIKey}
  5196. class AssembleHeaderException_Card(Exception):
  5197. def __init__(self, msg):
  5198. self.message = msg
  5199. class Url_Card:
  5200. def __init__(this, host, path, schema):
  5201. this.host = host
  5202. this.path = path
  5203. this.schema = schema
  5204. pass
  5205. # 进行sha256加密和base64编码
  5206. def sha256base64_Card(data):
  5207. sha256 = hashlib.sha256()
  5208. sha256.update(data)
  5209. digest = base64.b64encode(sha256.digest()).decode(encoding="utf-8")
  5210. return digest
  5211. def parse_url_Card(requset_url):
  5212. stidx = requset_url.index("://")
  5213. host = requset_url[stidx + 3:]
  5214. schema = requset_url[:stidx + 3]
  5215. edidx = host.index("/")
  5216. if edidx <= 0:
  5217. raise AssembleHeaderException_Card("invalid request url:" + requset_url)
  5218. path = host[edidx:]
  5219. host = host[:edidx]
  5220. u = Url_Card(host, path, schema)
  5221. return u
  5222. def assemble_ws_auth_url_Card(requset_url, method="GET", api_key="", api_secret=""):
  5223. u = parse_url_Card(requset_url)
  5224. host = u.host
  5225. path = u.path
  5226. now = datetime.now()
  5227. date = format_date_time(mktime(now.timetuple()))
  5228. print(date)
  5229. # date = "Thu, 12 Dec 2019 01:57:27 GMT"
  5230. signature_origin = "host: {}\\ndate: {}\\n{} {} HTTP/1.1".format(host, date, method, path)
  5231. print(signature_origin)
  5232. signature_sha = hmac.new(api_secret.encode("utf-8"), signature_origin.encode("utf-8"),
  5233. digestmod=hashlib.sha256).digest()
  5234. signature_sha = base64.b64encode(signature_sha).decode(encoding="utf-8")
  5235. authorization_origin = "api_key=\\"%s\\", algorithm=\\"%s\\", headers=\\"%s\\", signature=\\"%s\\"" % (
  5236. api_key, "hmac-sha256", "host date request-line", signature_sha)
  5237. authorization = base64.b64encode(authorization_origin.encode("utf-8")).decode(encoding="utf-8")
  5238. print(authorization_origin)
  5239. values = {
  5240. "host": host,
  5241. "date": date,
  5242. "authorization": authorization
  5243. }
  5244. return requset_url + "?" + urlencode(values)
  5245. def gen_body_Card(appid, img_path, server_id):
  5246. with open(img_path, "rb") as f:
  5247. img_data = f.read()
  5248. body = {
  5249. "header": {
  5250. "app_id": appid,
  5251. "status": 3
  5252. },
  5253. "parameter": {
  5254. server_id: {
  5255. "carLicenseRes": {
  5256. "encoding": "utf8",
  5257. "compress": "raw",
  5258. "format": "json"
  5259. }
  5260. }
  5261. },
  5262. "payload": {
  5263. "carImgBase64Str": {
  5264. "encoding": "jpg",
  5265. "status": 3,
  5266. "image": str(base64.b64encode(img_data), "utf-8")
  5267. }
  5268. }
  5269. }
  5270. return json.dumps(body)
  5271. `
  5272. var code = '';
  5273. return code;
  5274. };
  5275. Blockly.Blocks['ai_online_card_loading'] = {
  5276. init: function () {
  5277. this.appendDummyInput()
  5278. .appendField(Blockly.Msg.online_card_load); //
  5279. this.appendValueInput("path").setCheck(null).appendField(Blockly.Msg.online_face_init_path);
  5280. this.setPreviousStatement(true, null);
  5281. this.setNextStatement(true, null);
  5282. this.setColour("#0000ff");
  5283. this.setTooltip(Blockly.Msg.online_card_load);
  5284. this.setHelpUrl("");
  5285. }
  5286. }
  5287. Blockly.Python.ai_online_card_loading = function (block) {
  5288. var AudioFile = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  5289. Blockly.Python.definitions_.ai_online_card_load = `def run_Card(appid, apikey, apisecret, img_path, server_id="jd_ocr_car"):
  5290. url = "http://api.xf-yun.com/v1/private/{}".format(server_id)
  5291. request_url = assemble_ws_auth_url_Card(url, "POST", apikey, apisecret)
  5292. headers = {"content-type": "application/json", "host": "api.xf-yun.com", "app_id": appid}
  5293. #print(request_url)
  5294. response = requests.post(request_url, data=gen_body_Card(appid, img_path, server_id), headers=headers)
  5295. resp_data = json.loads(response.content.decode("utf-8"))
  5296. print(resp_data)
  5297. resultCard = base64.b64decode(resp_data["payload"]["carLicenseRes"]["text"]).decode("utf-8")
  5298. return json.loads(resultCard)
  5299. CARDRESULT = ""
  5300. `
  5301. let code = `if __name__ == "__main__":
  5302. img_path_Card = ${AudioFile}
  5303. CARDRESULT = run_Card(appid=APPId_Card,apisecret=APISecret_Card,apikey=APIKey_Card,img_path=img_path_Card,)
  5304. `
  5305. return code
  5306. }
  5307. Blockly.Blocks['ai_online_card_result'] = {
  5308. init: function () {
  5309. this.appendDummyInput()
  5310. .appendField(Blockly.Msg.online_card_result); //
  5311. this.setOutput(true, null);
  5312. this.setColour("#0000ff");
  5313. this.setTooltip(Blockly.Msg.online_card_result);
  5314. this.setHelpUrl("");
  5315. }
  5316. };
  5317. Blockly.Python['ai_online_card_result'] = function (block) {
  5318. let code = `CARDRESULT`;
  5319. return [code, Blockly.Python.ORDER_ATOMIC];
  5320. };
  5321. // 机器翻译
  5322. Blockly.Blocks['ai_online_machine_translation_init'] = {
  5323. init: function () {
  5324. this.appendDummyInput()
  5325. .appendField(Blockly.Msg.machine_translation_init); //
  5326. this.appendDummyInput()
  5327. .appendField(Blockly.Msg.BLYNK_GET_DATANAME)
  5328. .appendField(new Blockly.FieldDropdown([
  5329. [Blockly.Msg.ai_speech_recognition_setup_set_simplified_chinese, "cn"],
  5330. [Blockly.Msg.ai_speech_recognition_setup_set_english, "en"],
  5331. ]), "from")
  5332. .appendField(Blockly.Msg.machine_translation_to)
  5333. .appendField(new Blockly.FieldDropdown([
  5334. [Blockly.Msg.ai_speech_recognition_setup_set_english, "en"],
  5335. [Blockly.Msg.ai_speech_recognition_setup_set_simplified_chinese, "cn"],
  5336. ]), "to");
  5337. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  5338. this.appendValueInput("B").setCheck(null).appendField('API Secret');
  5339. this.appendValueInput("C").setCheck(null).appendField('API Key');
  5340. this.setPreviousStatement(true, null);
  5341. this.setNextStatement(true, null);
  5342. this.setColour("#0000ff");
  5343. this.setTooltip(Blockly.Msg.machine_translation_init);
  5344. this.setHelpUrl("");
  5345. }
  5346. };
  5347. Blockly.Python['ai_online_machine_translation_init'] = function (block) {
  5348. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  5349. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  5350. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  5351. let FROM = block.getFieldValue('from')
  5352. let TO = block.getFieldValue('to')
  5353. // TODO: Assemble Python into code variable.
  5354. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  5355. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  5356. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  5357. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  5358. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  5359. Blockly.Python.definitions_['v831_import_json'] = `import json`
  5360. Blockly.Python.definitions_.ai_online_machine_translation_init = `
  5361. APPId_machine = ${APPId}
  5362. APISecret_machine = ${APISecret}
  5363. APIKey_machine = ${APIKey}
  5364. TRANSLATIONTEXT = ""
  5365. class get_result_machine(object):
  5366. def __init__(self,host,TRANSLATIONTEXT):
  5367. # 应用ID(到控制台获取)
  5368. self.APPID_machine = ${APPId}
  5369. # 接口APISercet(到控制台机器翻译服务页面获取)
  5370. self.Secret_machine = ${APISecret}
  5371. # 接口APIKey(到控制台机器翻译服务页面获取)
  5372. self.APIKey_machine= ${APIKey}
  5373. # 以下为POST请求
  5374. self.Host = "itrans.xfyun.cn"
  5375. self.RequestUri = "/v2/its"
  5376. # 设置url
  5377. # print(host)
  5378. self.url="https://"+host+self.RequestUri
  5379. self.HttpMethod = "POST"
  5380. self.Algorithm = "hmac-sha256"
  5381. self.HttpProto = "HTTP/1.1"
  5382. # 设置当前时间
  5383. curTime_utc = datetime.utcnow()
  5384. self.Date = self.httpdate_machine(curTime_utc)
  5385. # 设置业务参数
  5386. # 语种列表参数值请参照接口文档:https://www.xfyun.cn/doc/nlp/xftrans/API.html
  5387. self.Text=TRANSLATIONTEXT
  5388. self.BusinessArgs={
  5389. "from": "${FROM}",
  5390. "to": "${TO}",
  5391. }
  5392. def hashlib_256_machine(self, res):
  5393. m = hashlib.sha256(bytes(res.encode(encoding="utf-8"))).digest()
  5394. result = "SHA-256=" + base64.b64encode(m).decode(encoding="utf-8")
  5395. return result
  5396. def httpdate_machine(self, dt):
  5397. """
  5398. Return a string representation of a date according to RFC 1123
  5399. (HTTP/1.1).
  5400. The supplied date must be in UTC.
  5401. """
  5402. weekday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dt.weekday()]
  5403. month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  5404. "Oct", "Nov", "Dec"][dt.month - 1]
  5405. return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (weekday, dt.day, month,
  5406. dt.year, dt.hour, dt.minute, dt.second)
  5407. def generateSignature_machine(self, digest):
  5408. signatureStr = "host: " + self.Host + "\\n"
  5409. signatureStr += "date: " + self.Date + "\\n"
  5410. signatureStr += self.HttpMethod + " " + self.RequestUri \\
  5411. + " " + self.HttpProto + "\\n"
  5412. signatureStr += "digest: " + digest
  5413. signature = hmac.new(bytes(self.Secret_machine.encode(encoding="utf-8")),
  5414. bytes(signatureStr.encode(encoding="utf-8")),
  5415. digestmod=hashlib.sha256).digest()
  5416. result = base64.b64encode(signature)
  5417. return result.decode(encoding="utf-8")
  5418. def init_header_machine(self, data):
  5419. digest = self.hashlib_256_machine(data)
  5420. #print(digest)
  5421. sign = self.generateSignature_machine(digest)
  5422. authHeader = "api_key=\\""+self.APIKey_machine+"\\", algorithm=\\""+self.Algorithm+"\\", headers=\\"host date request-line digest\\", signature=\\""+sign+"\\""
  5423. #print(authHeader)
  5424. headers = {
  5425. "Content-Type": "application/json",
  5426. "Accept": "application/json",
  5427. "Method": "POST",
  5428. "Host": self.Host,
  5429. "Date": self.Date,
  5430. "Digest": digest,
  5431. "Authorization": authHeader
  5432. }
  5433. return headers
  5434. def get_body_machine(self):
  5435. content = str(base64.b64encode(self.Text.encode("utf-8")), "utf-8")
  5436. postdata = {
  5437. "common": {"app_id": self.APPID_machine},
  5438. "business": self.BusinessArgs,
  5439. "data": {
  5440. "text": content,
  5441. }
  5442. }
  5443. body = json.dumps(postdata)
  5444. #print(body)
  5445. return body
  5446. def call_url_machine(self):
  5447. if self.APPID_machine == "" or self.APIKey_machine == "" or self.Secret_machine == "":
  5448. print("Appid 或APIKey 或APISecret 为空!请打开demo代码,填写相关信息。")
  5449. else:
  5450. code = 0
  5451. body=self.get_body_machine()
  5452. headers=self.init_header_machine(body)
  5453. #print(self.url)
  5454. response = requests.post(self.url, data=body, headers=headers,timeout=8)
  5455. status_code = response.status_code
  5456. #print(response.content)
  5457. if status_code!=200:
  5458. # 鉴权失败
  5459. print("Http请求失败,状态码:" + str(status_code) + ",错误信息:" + response.text)
  5460. print("请根据错误信息检查代码,接口文档:https://www.xfyun.cn/doc/nlp/xftrans/API.html")
  5461. else:
  5462. # 鉴权成功
  5463. respData = json.loads(response.text)
  5464. #print('result',respData["data"]['result']['trans_result']['dst'])
  5465. # return
  5466. # 以下仅用于调试
  5467. code = str(respData["code"])
  5468. if code=="0":
  5469. return respData["data"]["result"]["trans_result"]["dst"]
  5470. else:
  5471. print("请前往https://www.xfyun.cn/document/error-code?code=" + code + "查询解决办法")
  5472. `
  5473. var code = '';
  5474. return code;
  5475. };
  5476. Blockly.Blocks['ai_online_machine_translation_loading'] = {
  5477. init: function () {
  5478. this.appendDummyInput()
  5479. .appendField(Blockly.Msg.machine_translation_load); //
  5480. this.appendDummyInput()
  5481. .appendField(Blockly.Msg.BLYNK_GET_DATANAME)
  5482. .appendField(new Blockly.FieldDropdown([
  5483. [Blockly.Msg.ai_speech_recognition_setup_set_simplified_chinese, "cn"],
  5484. [Blockly.Msg.ai_speech_recognition_setup_set_english, "en"],
  5485. ]), "from")
  5486. .appendField(Blockly.Msg.machine_translation_to)
  5487. .appendField(new Blockly.FieldDropdown([
  5488. [Blockly.Msg.ai_speech_recognition_setup_set_english, "en"],
  5489. [Blockly.Msg.ai_speech_recognition_setup_set_simplified_chinese, "cn"],
  5490. ]), "to");
  5491. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  5492. this.appendValueInput("B").setCheck(null).appendField('API Secret');
  5493. this.appendValueInput("C").setCheck(null).appendField('API Key');
  5494. this.appendValueInput("NAME")
  5495. .setCheck(null)
  5496. .appendField(Blockly.Msg.Content_of_text); //
  5497. this.setPreviousStatement(true, null);
  5498. this.setNextStatement(true, null);
  5499. this.setColour("#0000ff");
  5500. this.setTooltip(Blockly.Msg.machine_translation_load);
  5501. this.setHelpUrl("");
  5502. }
  5503. }
  5504. Blockly.Python.ai_online_machine_translation_loading = function (block) {
  5505. let text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC)
  5506. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  5507. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  5508. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  5509. let FROM = block.getFieldValue('from')
  5510. let TO = block.getFieldValue('to')
  5511. // TODO: Assemble Python into code variable.
  5512. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  5513. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  5514. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  5515. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  5516. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  5517. Blockly.Python.definitions_['v831_import_json'] = `import json`
  5518. Blockly.Python.definitions_.ai_online_machine_translation_init = `
  5519. APPId_machine = ${APPId}
  5520. APISecret_machine = ${APISecret}
  5521. APIKey_machine = ${APIKey}
  5522. TRANSLATIONTEXT = ""
  5523. class get_result_machine(object):
  5524. def __init__(self,host,TRANSLATIONTEXT):
  5525. # 应用ID(到控制台获取)
  5526. self.APPID_machine = ${APPId}
  5527. # 接口APISercet(到控制台机器翻译服务页面获取)
  5528. self.Secret_machine = ${APISecret}
  5529. # 接口APIKey(到控制台机器翻译服务页面获取)
  5530. self.APIKey_machine= ${APIKey}
  5531. # 以下为POST请求
  5532. self.Host = "itrans.xfyun.cn"
  5533. self.RequestUri = "/v2/its"
  5534. # 设置url
  5535. # print(host)
  5536. self.url="https://"+host+self.RequestUri
  5537. self.HttpMethod = "POST"
  5538. self.Algorithm = "hmac-sha256"
  5539. self.HttpProto = "HTTP/1.1"
  5540. # 设置当前时间
  5541. curTime_utc = datetime.utcnow()
  5542. self.Date = self.httpdate_machine(curTime_utc)
  5543. # 设置业务参数
  5544. # 语种列表参数值请参照接口文档:https://www.xfyun.cn/doc/nlp/xftrans/API.html
  5545. self.Text=TRANSLATIONTEXT
  5546. self.BusinessArgs={
  5547. "from": "${FROM}",
  5548. "to": "${TO}",
  5549. }
  5550. def hashlib_256_machine(self, res):
  5551. m = hashlib.sha256(bytes(res.encode(encoding="utf-8"))).digest()
  5552. result = "SHA-256=" + base64.b64encode(m).decode(encoding="utf-8")
  5553. return result
  5554. def httpdate_machine(self, dt):
  5555. """
  5556. Return a string representation of a date according to RFC 1123
  5557. (HTTP/1.1).
  5558. The supplied date must be in UTC.
  5559. """
  5560. weekday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dt.weekday()]
  5561. month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  5562. "Oct", "Nov", "Dec"][dt.month - 1]
  5563. return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (weekday, dt.day, month,
  5564. dt.year, dt.hour, dt.minute, dt.second)
  5565. def generateSignature_machine(self, digest):
  5566. signatureStr = "host: " + self.Host + "\\n"
  5567. signatureStr += "date: " + self.Date + "\\n"
  5568. signatureStr += self.HttpMethod + " " + self.RequestUri \\
  5569. + " " + self.HttpProto + "\\n"
  5570. signatureStr += "digest: " + digest
  5571. signature = hmac.new(bytes(self.Secret_machine.encode(encoding="utf-8")),
  5572. bytes(signatureStr.encode(encoding="utf-8")),
  5573. digestmod=hashlib.sha256).digest()
  5574. result = base64.b64encode(signature)
  5575. return result.decode(encoding="utf-8")
  5576. def init_header_machine(self, data):
  5577. digest = self.hashlib_256_machine(data)
  5578. #print(digest)
  5579. sign = self.generateSignature_machine(digest)
  5580. authHeader = "api_key=\\""+self.APIKey_machine+"\\", algorithm=\\""+self.Algorithm+"\\", headers=\\"host date request-line digest\\", signature=\\""+sign+"\\""
  5581. #print(authHeader)
  5582. headers = {
  5583. "Content-Type": "application/json",
  5584. "Accept": "application/json",
  5585. "Method": "POST",
  5586. "Host": self.Host,
  5587. "Date": self.Date,
  5588. "Digest": digest,
  5589. "Authorization": authHeader
  5590. }
  5591. return headers
  5592. def get_body_machine(self):
  5593. content = str(base64.b64encode(self.Text.encode("utf-8")), "utf-8")
  5594. postdata = {
  5595. "common": {"app_id": self.APPID_machine},
  5596. "business": self.BusinessArgs,
  5597. "data": {
  5598. "text": content,
  5599. }
  5600. }
  5601. body = json.dumps(postdata)
  5602. #print(body)
  5603. return body
  5604. def call_url_machine(self):
  5605. if self.APPID_machine == "" or self.APIKey_machine == "" or self.Secret_machine == "":
  5606. print("Appid 或APIKey 或APISecret 为空!请打开demo代码,填写相关信息。")
  5607. else:
  5608. code = 0
  5609. body=self.get_body_machine()
  5610. headers=self.init_header_machine(body)
  5611. #print(self.url)
  5612. response = requests.post(self.url, data=body, headers=headers,timeout=8)
  5613. status_code = response.status_code
  5614. #print(response.content)
  5615. if status_code!=200:
  5616. # 鉴权失败
  5617. print("Http请求失败,状态码:" + str(status_code) + ",错误信息:" + response.text)
  5618. print("请根据错误信息检查代码,接口文档:https://www.xfyun.cn/doc/nlp/xftrans/API.html")
  5619. else:
  5620. # 鉴权成功
  5621. respData = json.loads(response.text)
  5622. #print('result',respData["data"]['result']['trans_result']['dst'])
  5623. # return
  5624. # 以下仅用于调试
  5625. code = str(respData["code"])
  5626. if code=="0":
  5627. return respData["data"]["result"]["trans_result"]["dst"]
  5628. else:
  5629. print("请前往https://www.xfyun.cn/document/error-code?code=" + code + "查询解决办法")
  5630. `
  5631. let code = `TRANSLATIONTEXT = ${text}
  5632. machinehost = "itrans.xfyun.cn"
  5633. #初始化类
  5634. gClass=get_result_machine(host=machinehost,TRANSLATIONTEXT=TRANSLATIONTEXT)
  5635. TRANSLATIONRESULT = gClass.call_url_machine()
  5636. `
  5637. return code
  5638. }
  5639. Blockly.Blocks['ai_online_machine_translation_result'] = {
  5640. init: function () {
  5641. this.appendDummyInput()
  5642. .appendField(Blockly.Msg.machine_translation_result); //
  5643. this.setOutput(true, null);
  5644. this.setColour("#0000ff");
  5645. this.setTooltip(Blockly.Msg.machine_translation_result);
  5646. this.setHelpUrl("");
  5647. }
  5648. };
  5649. Blockly.Python['ai_online_machine_translation_result'] = function (block) {
  5650. let code = `TRANSLATIONRESULT`;
  5651. return [code, Blockly.Python.ORDER_ATOMIC];
  5652. };
  5653. // 手写文字识别
  5654. Blockly.Blocks['ai_online_handwritten_text_init'] = {
  5655. init: function () {
  5656. this.appendDummyInput()
  5657. .appendField(Blockly.Msg.handwritten_text_init); //
  5658. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  5659. // this.appendValueInput("B").setCheck(null).appendField('API Secret');
  5660. this.appendValueInput("C").setCheck(null).appendField('API Key');
  5661. this.setPreviousStatement(true, null);
  5662. this.setNextStatement(true, null);
  5663. this.setColour("#0000ff");
  5664. this.setTooltip(Blockly.Msg.handwritten_text_init);
  5665. this.setHelpUrl("");
  5666. }
  5667. };
  5668. Blockly.Python['ai_online_handwritten_text_init'] = function (block) {
  5669. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  5670. // var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  5671. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  5672. // TODO: Assemble Python into code variable.
  5673. Blockly.Python.definitions_['v831_import_time'] = `import time`
  5674. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  5675. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  5676. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  5677. Blockly.Python.definitions_['v831_import_json'] = `import json`
  5678. Blockly.Python.definitions_['v831_import_from_urllib_parse'] = `from urllib import parse`
  5679. Blockly.Python.definitions_.ai_online_handwritten_text_init = `
  5680. URL_handwritten_text = "http://webapi.xfyun.cn/v1/service/v1/ocr/handwriting"
  5681. APPId_handwritten_text = ${APPId}
  5682. APIKey_handwritten_text = ${APIKey}
  5683. def textGetHeader_handwritten_text(language, location):
  5684. curTime = str(int(time.time()))
  5685. param = "{\\"language\\":\\""+language+"\\",\\"location\\":\\""+location+"\\"}"
  5686. paramBase64 = base64.b64encode(param.encode("utf-8"))
  5687. m2 = hashlib.md5()
  5688. str1 = APIKey_handwritten_text + curTime + str(paramBase64, "utf-8")
  5689. m2.update(str1.encode("utf-8"))
  5690. checkSum = m2.hexdigest()
  5691. # 组装http请求头
  5692. header = {
  5693. "X-CurTime": curTime,
  5694. "X-Param": paramBase64,
  5695. "X-Appid": APPId_handwritten_text,
  5696. "X-CheckSum": checkSum,
  5697. "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
  5698. }
  5699. return header
  5700. def getBody_handwritten_text(filepath):
  5701. with open(filepath, "rb") as f:
  5702. imgfile = f.read()
  5703. data = {"image": str(base64.b64encode(imgfile), "utf-8")}
  5704. return data
  5705. `
  5706. var code = '';
  5707. return code;
  5708. };
  5709. Blockly.Blocks['ai_online_handwritten_text_loading'] = {
  5710. init: function () {
  5711. this.appendDummyInput()
  5712. .appendField(Blockly.Msg.handwritten_text_load); //
  5713. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  5714. // this.appendValueInput("B").setCheck(null).appendField('API Secret');
  5715. this.appendValueInput("C").setCheck(null).appendField('API Key');
  5716. this.appendValueInput("path")
  5717. .setCheck(null)
  5718. .appendField(Blockly.Msg.online_face_init_path); //
  5719. this.setPreviousStatement(true, null);
  5720. this.setNextStatement(true, null);
  5721. this.setColour("#0000ff");
  5722. this.setTooltip(Blockly.Msg.handwritten_text_load);
  5723. this.setHelpUrl("");
  5724. }
  5725. }
  5726. Blockly.Python.ai_online_handwritten_text_loading = function (block) {
  5727. let text = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC)
  5728. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  5729. // var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  5730. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  5731. // TODO: Assemble Python into code variable.
  5732. Blockly.Python.definitions_['v831_import_time'] = `import time`
  5733. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  5734. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  5735. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  5736. Blockly.Python.definitions_['v831_import_json'] = `import json`
  5737. Blockly.Python.definitions_['v831_import_from_urllib_parse'] = `from urllib import parse`
  5738. Blockly.Python.definitions_.ai_online_handwritten_text_init = `
  5739. URL_handwritten_text = "http://webapi.xfyun.cn/v1/service/v1/ocr/handwriting"
  5740. APPId_handwritten_text = ${APPId}
  5741. APIKey_handwritten_text = ${APIKey}
  5742. def textGetHeader_handwritten_text(language, location):
  5743. curTime = str(int(time.time()))
  5744. param = "{\\"language\\":\\""+language+"\\",\\"location\\":\\""+location+"\\"}"
  5745. paramBase64 = base64.b64encode(param.encode("utf-8"))
  5746. m2 = hashlib.md5()
  5747. str1 = APIKey_handwritten_text + curTime + str(paramBase64, "utf-8")
  5748. m2.update(str1.encode("utf-8"))
  5749. checkSum = m2.hexdigest()
  5750. # 组装http请求头
  5751. header = {
  5752. "X-CurTime": curTime,
  5753. "X-Param": paramBase64,
  5754. "X-Appid": APPId_handwritten_text,
  5755. "X-CheckSum": checkSum,
  5756. "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
  5757. }
  5758. return header
  5759. def getBody_handwritten_text(filepath):
  5760. with open(filepath, "rb") as f:
  5761. imgfile = f.read()
  5762. data = {"image": str(base64.b64encode(imgfile), "utf-8")}
  5763. return data
  5764. `
  5765. let code = `# 语种设置
  5766. textLanguage = "cn|en"
  5767. # 是否返回文本位置信息
  5768. textLocation = "false"
  5769. # 图片上传接口地址
  5770. picFilePath_handwritten_text = ${text}
  5771. HandwrittenTextData = requests.post(URL_handwritten_text, headers=textGetHeader_handwritten_text(textLanguage, textLocation), data=getBody_handwritten_text(picFilePath_handwritten_text))
  5772. `
  5773. return code
  5774. }
  5775. Blockly.Blocks['ai_online_handwritten_text_result'] = {
  5776. init: function () {
  5777. this.appendDummyInput()
  5778. .appendField(Blockly.Msg.handwritten_text_result); //
  5779. this.setOutput(true, null);
  5780. this.setColour("#0000ff");
  5781. this.setTooltip(Blockly.Msg.handwritten_text_result);
  5782. this.setHelpUrl("");
  5783. }
  5784. };
  5785. Blockly.Python['ai_online_handwritten_text_result'] = function (block) {
  5786. Blockly.Python.definitions_.ai_online_handwritten_text_result = `def handwrittenTextDatas_handwritten_text(r):
  5787. my_json_data = r.content.decode("utf8")
  5788. # print(type(my_json))
  5789. loadsData = json.loads(my_json_data)
  5790. # s = json.dumps(data, indent=4, sort_keys=True)
  5791. print("data",my_json_data)
  5792. deal_data = ""
  5793. if loadsData["code"] == "0" :
  5794. t = loadsData["data"]["block"][0]["line"]
  5795. for i in t:
  5796. print(i)
  5797. for j in i["word"]:
  5798. print(j["content"])
  5799. deal_data = deal_data + j["content"] + " "
  5800. #print('deal_data',deal_data)
  5801. return deal_data
  5802. `
  5803. let code = `handwrittenTextDatas_handwritten_text(HandwrittenTextData)`;
  5804. return [code, Blockly.Python.ORDER_ATOMIC];
  5805. };
  5806. // 公式识别
  5807. Blockly.Blocks['ai_online_formula_init'] = {
  5808. init: function () {
  5809. this.appendDummyInput()
  5810. .appendField(Blockly.Msg.formula_init); //
  5811. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  5812. this.appendValueInput("B").setCheck(null).appendField('API Secret');
  5813. this.appendValueInput("C").setCheck(null).appendField('API Key');
  5814. this.setPreviousStatement(true, null);
  5815. this.setNextStatement(true, null);
  5816. this.setColour("#0000ff");
  5817. this.setTooltip(Blockly.Msg.formula_init);
  5818. this.setHelpUrl("");
  5819. }
  5820. };
  5821. Blockly.Python['ai_online_formula_init'] = function (block) {
  5822. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  5823. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  5824. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  5825. // TODO: Assemble Python into code variable.
  5826. Blockly.Python.definitions_['v831_import_time'] = `import time`
  5827. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  5828. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  5829. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  5830. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  5831. Blockly.Python.definitions_['v831_import_json'] = `import json`
  5832. Blockly.Python.definitions_['v831_import_from_urllib_parse'] = `from urllib import parse`
  5833. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  5834. Blockly.Python.definitions_.ai_online_formula_init = `class get_result_formula(object):
  5835. def __init__(self,host,path):
  5836. # 应用ID(到控制台获取)
  5837. self.APPID_formula = ${APPId}
  5838. # 接口APISercet(到控制台公式识别服务页面获取)
  5839. self.Secret_formula = ${APISecret}
  5840. # 接口APIKey(到控制台公式识别服务页面获取)
  5841. self.APIKey_formula = ${APIKey}
  5842. # 以下为POST请求
  5843. self.Host_formula = host
  5844. self.RequestUri_formula = "/v2/itr"
  5845. # 设置url
  5846. # print(host)
  5847. self.url_formula="https://"+host+self.RequestUri_formula
  5848. self.HttpMethod_formula = "POST"
  5849. self.Algorithm_formula = "hmac-sha256"
  5850. self.HttpProto_formula = "HTTP/1.1"
  5851. # 设置当前时间
  5852. curTime_utc = datetime.utcnow()
  5853. self.Date_formula = self.httpdate_formula(curTime_utc)
  5854. #设置测试图片文件
  5855. self.AudioPath_formula = path
  5856. self.BusinessArgs_formula={
  5857. "ent": "teach-photo-print",
  5858. "aue": "raw",
  5859. }
  5860. def imgRead_formula(self, path):
  5861. with open(path, "rb") as fo:
  5862. return fo.read()
  5863. def hashlib_256_formula(self, res):
  5864. m = hashlib.sha256(bytes(res.encode(encoding="utf-8"))).digest()
  5865. result = "SHA-256=" + base64.b64encode(m).decode(encoding="utf-8")
  5866. return result
  5867. def httpdate_formula(self, dt):
  5868. """
  5869. Return a string representation of a date according to RFC 1123
  5870. (HTTP/1.1).
  5871. The supplied date must be in UTC.
  5872. """
  5873. weekday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dt.weekday()]
  5874. month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  5875. "Oct", "Nov", "Dec"][dt.month - 1]
  5876. return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (weekday, dt.day, month,
  5877. dt.year, dt.hour, dt.minute, dt.second)
  5878. def generateSignature_formula(self, digest):
  5879. signatureStr = "host: " + self.Host_formula + "\\n"
  5880. signatureStr += "date: " + self.Date_formula + "\\n"
  5881. signatureStr += self.HttpMethod_formula + " " + self.RequestUri_formula \\
  5882. + " " + self.HttpProto_formula + "\\n"
  5883. signatureStr += "digest: " + digest
  5884. signature = hmac.new(bytes(self.Secret_formula.encode(encoding="utf-8")),
  5885. bytes(signatureStr.encode(encoding="utf-8")),
  5886. digestmod=hashlib.sha256).digest()
  5887. result = base64.b64encode(signature)
  5888. return result.decode(encoding="utf-8")
  5889. def init_header_formula(self, data):
  5890. digest = self.hashlib_256_formula(data)
  5891. #print(digest)
  5892. sign = self.generateSignature_formula(digest)
  5893. authHeader = "api_key=\\""+self.APIKey_formula+"\\", algorithm=\\""+self.Algorithm_formula+"\\", headers=\\"host date request-line digest\\", signature=\\""+sign+"\\""
  5894. #print(authHeader)
  5895. headers = {
  5896. "Content-Type": "application/json",
  5897. "Accept": "application/json",
  5898. "Method": "POST",
  5899. "Host": self.Host_formula,
  5900. "Date": self.Date_formula,
  5901. "Digest": digest,
  5902. "Authorization": authHeader
  5903. }
  5904. return headers
  5905. def get_body_formula(self):
  5906. audioData = self.imgRead_formula(self.AudioPath_formula)
  5907. content = base64.b64encode(audioData).decode(encoding="utf-8")
  5908. postdata = {
  5909. "common": {"app_id": self.APPID_formula},
  5910. "business": self.BusinessArgs_formula,
  5911. "data": {
  5912. "image": content,
  5913. }
  5914. }
  5915. body = json.dumps(postdata)
  5916. #print(body)
  5917. return body
  5918. def call_url_formula(self):
  5919. if self.APPID_formula == "" or self.APIKey_formula == "" or self.Secret_formula == "":
  5920. print("Appid 或APIKey 或APISecret 为空!请打开demo代码,填写相关信息。")
  5921. else:
  5922. code = 0
  5923. body=self.get_body_formula()
  5924. headers=self.init_header_formula(body)
  5925. #print(self.url_formula)
  5926. response = requests.post(self.url_formula, data=body, headers=headers,timeout=8)
  5927. status_code = response.status_code
  5928. #print(response.content)
  5929. if status_code!=200:
  5930. # 鉴权失败
  5931. print("Http请求失败,状态码:" + str(status_code) + ",错误信息:" + response.text)
  5932. print("请根据错误信息检查代码,接口文档:https://www.xfyun.cn/doc/words/formula-discern/API.html")
  5933. else:
  5934. # 鉴权成功
  5935. respData = json.loads(response.text)
  5936. dataCotent = ""
  5937. try:
  5938. for i in respData["data"]["region"]:
  5939. dataCotent += i["recog"]["content"]
  5940. except:
  5941. dataCotent = "Failure"
  5942. return dataCotent
  5943. FORMULARESULT = ""
  5944. `
  5945. var code = '';
  5946. return code;
  5947. };
  5948. Blockly.Blocks['ai_online_formula_loading'] = {
  5949. init: function () {
  5950. this.appendDummyInput()
  5951. .appendField(Blockly.Msg.formula_load); //
  5952. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  5953. this.appendValueInput("B").setCheck(null).appendField('API Secret');
  5954. this.appendValueInput("C").setCheck(null).appendField('API Key');
  5955. this.appendValueInput("path")
  5956. .setCheck(null)
  5957. .appendField(Blockly.Msg.online_face_init_path); //
  5958. this.setPreviousStatement(true, null);
  5959. this.setNextStatement(true, null);
  5960. this.setColour("#0000ff");
  5961. this.setTooltip(Blockly.Msg.formula_load);
  5962. this.setHelpUrl("");
  5963. }
  5964. }
  5965. Blockly.Python.ai_online_formula_loading = function (block) {
  5966. let text = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC)
  5967. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  5968. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  5969. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  5970. // TODO: Assemble Python into code variable.
  5971. Blockly.Python.definitions_['v831_import_time'] = `import time`
  5972. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  5973. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  5974. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  5975. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  5976. Blockly.Python.definitions_['v831_import_json'] = `import json`
  5977. Blockly.Python.definitions_['v831_import_from_urllib_parse'] = `from urllib import parse`
  5978. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  5979. Blockly.Python.definitions_.ai_online_formula_init = `class get_result_formula(object):
  5980. def __init__(self,host,path):
  5981. # 应用ID(到控制台获取)
  5982. self.APPID_formula = ${APPId}
  5983. # 接口APISercet(到控制台公式识别服务页面获取)
  5984. self.Secret_formula = ${APISecret}
  5985. # 接口APIKey(到控制台公式识别服务页面获取)
  5986. self.APIKey_formula = ${APIKey}
  5987. # 以下为POST请求
  5988. self.Host_formula = host
  5989. self.RequestUri_formula = "/v2/itr"
  5990. # 设置url
  5991. # print(host)
  5992. self.url_formula="https://"+host+self.RequestUri_formula
  5993. self.HttpMethod_formula = "POST"
  5994. self.Algorithm_formula = "hmac-sha256"
  5995. self.HttpProto_formula = "HTTP/1.1"
  5996. # 设置当前时间
  5997. curTime_utc = datetime.utcnow()
  5998. self.Date_formula = self.httpdate_formula(curTime_utc)
  5999. #设置测试图片文件
  6000. self.AudioPath_formula = path
  6001. self.BusinessArgs_formula={
  6002. "ent": "teach-photo-print",
  6003. "aue": "raw",
  6004. }
  6005. def imgRead_formula(self, path):
  6006. with open(path, "rb") as fo:
  6007. return fo.read()
  6008. def hashlib_256_formula(self, res):
  6009. m = hashlib.sha256(bytes(res.encode(encoding="utf-8"))).digest()
  6010. result = "SHA-256=" + base64.b64encode(m).decode(encoding="utf-8")
  6011. return result
  6012. def httpdate_formula(self, dt):
  6013. """
  6014. Return a string representation of a date according to RFC 1123
  6015. (HTTP/1.1).
  6016. The supplied date must be in UTC.
  6017. """
  6018. weekday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dt.weekday()]
  6019. month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  6020. "Oct", "Nov", "Dec"][dt.month - 1]
  6021. return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (weekday, dt.day, month,
  6022. dt.year, dt.hour, dt.minute, dt.second)
  6023. def generateSignature_formula(self, digest):
  6024. signatureStr = "host: " + self.Host_formula + "\\n"
  6025. signatureStr += "date: " + self.Date_formula + "\\n"
  6026. signatureStr += self.HttpMethod_formula + " " + self.RequestUri_formula \\
  6027. + " " + self.HttpProto_formula + "\\n"
  6028. signatureStr += "digest: " + digest
  6029. signature = hmac.new(bytes(self.Secret_formula.encode(encoding="utf-8")),
  6030. bytes(signatureStr.encode(encoding="utf-8")),
  6031. digestmod=hashlib.sha256).digest()
  6032. result = base64.b64encode(signature)
  6033. return result.decode(encoding="utf-8")
  6034. def init_header_formula(self, data):
  6035. digest = self.hashlib_256_formula(data)
  6036. #print(digest)
  6037. sign = self.generateSignature_formula(digest)
  6038. authHeader = "api_key=\\""+self.APIKey_formula+"\\", algorithm=\\""+self.Algorithm_formula+"\\", headers=\\"host date request-line digest\\", signature=\\""+sign+"\\""
  6039. #print(authHeader)
  6040. headers = {
  6041. "Content-Type": "application/json",
  6042. "Accept": "application/json",
  6043. "Method": "POST",
  6044. "Host": self.Host_formula,
  6045. "Date": self.Date_formula,
  6046. "Digest": digest,
  6047. "Authorization": authHeader
  6048. }
  6049. return headers
  6050. def get_body_formula(self):
  6051. audioData = self.imgRead_formula(self.AudioPath_formula)
  6052. content = base64.b64encode(audioData).decode(encoding="utf-8")
  6053. postdata = {
  6054. "common": {"app_id": self.APPID_formula},
  6055. "business": self.BusinessArgs_formula,
  6056. "data": {
  6057. "image": content,
  6058. }
  6059. }
  6060. body = json.dumps(postdata)
  6061. #print(body)
  6062. return body
  6063. def call_url_formula(self):
  6064. if self.APPID_formula == "" or self.APIKey_formula == "" or self.Secret_formula == "":
  6065. print("Appid 或APIKey 或APISecret 为空!请打开demo代码,填写相关信息。")
  6066. else:
  6067. code = 0
  6068. body=self.get_body_formula()
  6069. headers=self.init_header_formula(body)
  6070. #print(self.url_formula)
  6071. response = requests.post(self.url_formula, data=body, headers=headers,timeout=8)
  6072. status_code = response.status_code
  6073. #print(response.content)
  6074. if status_code!=200:
  6075. # 鉴权失败
  6076. print("Http请求失败,状态码:" + str(status_code) + ",错误信息:" + response.text)
  6077. print("请根据错误信息检查代码,接口文档:https://www.xfyun.cn/doc/words/formula-discern/API.html")
  6078. else:
  6079. # 鉴权成功
  6080. respData = json.loads(response.text)
  6081. dataCotent = ""
  6082. try:
  6083. for i in respData["data"]["region"]:
  6084. dataCotent += i["recog"]["content"]
  6085. except:
  6086. dataCotent = "Failure"
  6087. return dataCotent
  6088. FORMULARESULT = ""
  6089. `
  6090. let code = `
  6091. if __name__ == "__main__":
  6092. formulaUrlPath_formula = ${text}
  6093. ##示例: host="rest-api.xfyun.cn"域名形式
  6094. host = "rest-api.xfyun.cn"
  6095. #初始化类
  6096. gClass=get_result_formula(host,path=formulaUrlPath_formula)
  6097. FORMULARESULT = gClass.call_url_formula()
  6098. `
  6099. return code
  6100. }
  6101. Blockly.Blocks['ai_online_formula_result'] = {
  6102. init: function () {
  6103. this.appendDummyInput()
  6104. .appendField(Blockly.Msg.formula_result); //
  6105. this.setOutput(true, null);
  6106. this.setColour("#0000ff");
  6107. this.setTooltip(Blockly.Msg.formula_result);
  6108. this.setHelpUrl("");
  6109. }
  6110. };
  6111. Blockly.Python['ai_online_formula_result'] = function (block) {
  6112. let code = `FORMULARESULT`;
  6113. return [code, Blockly.Python.ORDER_ATOMIC];
  6114. };
  6115. // 身份证识别
  6116. Blockly.Blocks['ai_online_ID_card_init'] = {
  6117. init: function () {
  6118. this.appendDummyInput()
  6119. .appendField('身份证识别初始化'); //
  6120. this.appendValueInput("A").setCheck(null).appendField('APP ID');
  6121. this.appendValueInput("B").setCheck(null).appendField('API Secret');
  6122. this.appendValueInput("C").setCheck(null).appendField('API Key');
  6123. this.setPreviousStatement(true, null);
  6124. this.setNextStatement(true, null);
  6125. this.setColour("#0000ff");
  6126. this.setTooltip(Blockly.Msg.handwritten_text_init);
  6127. this.setHelpUrl("");
  6128. }
  6129. };
  6130. Blockly.Python['ai_online_ID_card_init'] = function (block) {
  6131. var APPId = Blockly.Python.valueToCode(block, 'A', Blockly.Python.ORDER_ATOMIC);
  6132. var APISecret = Blockly.Python.valueToCode(block, 'B', Blockly.Python.ORDER_ATOMIC);
  6133. var APIKey = Blockly.Python.valueToCode(block, 'C', Blockly.Python.ORDER_ATOMIC);
  6134. // TODO: Assemble Python into code variable.
  6135. Blockly.Python.definitions_['v831_import_time'] = `import time`
  6136. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  6137. Blockly.Python.definitions_['v831_import_hashlib'] = `import hashlib`
  6138. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  6139. Blockly.Python.definitions_['v831_import_hmac'] = `import hmac`
  6140. Blockly.Python.definitions_['v831_import_json'] = `import json`
  6141. Blockly.Python.definitions_['v831_import_from_time_mktime'] = `from time import mktime`
  6142. Blockly.Python.definitions_['v831_import_datetime_datetime'] = `from datetime import datetime`
  6143. Blockly.Python.definitions_['v831_import_from_urllib_parse'] = `from urllib import parse`
  6144. Blockly.Python.definitions_['v831_import_format_date_time'] = `from wsgiref.handlers import format_date_time`
  6145. Blockly.Python.definitions_['v831_import_urlencode'] = `from urllib.parse import urlencode`
  6146. Blockly.Python.definitions_.ai_online_formula_init = `class AssembleHeaderException(Exception):
  6147. def __init__(self, msg):
  6148. self.message = msg
  6149. class Url:
  6150. def __init__(this, host, path, schema):
  6151. this.host = host
  6152. this.path = path
  6153. this.schema = schema
  6154. pass
  6155. # calculate sha256 and encode to base64
  6156. def sha256base64(data):
  6157. sha256 = hashlib.sha256()
  6158. sha256.update(data)
  6159. digest = base64.b64encode(sha256.digest()).decode(encoding="utf-8")
  6160. return digest
  6161. def parse_url(requset_url):
  6162. stidx = requset_url.index("://")
  6163. host = requset_url[stidx + 3:]
  6164. schema = requset_url[:stidx + 3]
  6165. edidx = host.index("/")
  6166. if edidx <= 0:
  6167. raise AssembleHeaderException("invalid request url:" + requset_url)
  6168. path = host[edidx:]
  6169. host = host[:edidx]
  6170. u = Url(host, path, schema)
  6171. return u
  6172. # build websocket auth request url
  6173. def assemble_ws_auth_url(requset_url, method="POST", api_key="", api_secret=""):
  6174. u = parse_url(requset_url)
  6175. host = u.host
  6176. path = u.path
  6177. now = datetime.now()
  6178. date = format_date_time(mktime(now.timetuple()))
  6179. #print(date)
  6180. # date = "Thu, 12 Dec 2019 01:57:27 GMT"
  6181. signature_origin = "host: {}\\ndate: {}\\n{} {} HTTP/1.1".format(host, date, method, path)
  6182. #print(signature_origin)
  6183. signature_sha = hmac.new(api_secret.encode("utf-8"), signature_origin.encode("utf-8"),
  6184. digestmod=hashlib.sha256).digest()
  6185. signature_sha = base64.b64encode(signature_sha).decode(encoding="utf-8")
  6186. authorization_origin = "api_key=\\"%s\\", algorithm=\\"%s\\", headers=\\"%s\\", signature=\\"%s\\"" % (
  6187. api_key, "hmac-sha256", "host date request-line", signature_sha)
  6188. authorization = base64.b64encode(authorization_origin.encode("utf-8")).decode(encoding="utf-8")
  6189. #print(authorization_origin)
  6190. values = {
  6191. "host": host,
  6192. "date": date,
  6193. "authorization": authorization
  6194. }
  6195. return requset_url + "?" + urlencode(values)
  6196. # 控制台获取以下信息
  6197. APPId = ${APPId}
  6198. APISecret = ${APISecret}
  6199. APIKey = ${APIKey}
  6200. `
  6201. var code = '';
  6202. return code;
  6203. };
  6204. Blockly.Blocks['ai_online_ID_card_loading'] = {
  6205. init: function () {
  6206. this.appendDummyInput()
  6207. .appendField('身份证识别加载'); //
  6208. // this.appendDummyInput()
  6209. // .appendField('设置识别语种')
  6210. // .appendField(new Blockly.FieldDropdown([
  6211. // ["en", "en"],
  6212. // ["cn|en", "cn|en"],
  6213. // ]), "language");
  6214. this.appendValueInput("path")
  6215. .setCheck(null)
  6216. .appendField(Blockly.Msg.online_face_init_path); //
  6217. this.setPreviousStatement(true, null);
  6218. this.setNextStatement(true, null);
  6219. this.setColour("#0000ff");
  6220. this.setTooltip(Blockly.Msg.handwritten_text_load);
  6221. this.setHelpUrl("");
  6222. }
  6223. }
  6224. Blockly.Python.ai_online_ID_card_loading = function (block) {
  6225. let text = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC)
  6226. // let language = Blockly.Python.valueToCode(block, 'language', Blockly.Python.ORDER_ATOMIC);
  6227. let code = `with open(${text}, "rb") as f:
  6228. imageBytes = f.read()
  6229. url = 'http://api.xf-yun.com/v1/private/s5ccecfce'
  6230. body = {
  6231. "header": {
  6232. "app_id": APPId,
  6233. "status": 3,
  6234. },
  6235. "parameter": {
  6236. "s5ccecfce": {
  6237. "template_list": "id_card",
  6238. "result": {
  6239. "encoding": "utf8",
  6240. "compress": "raw",
  6241. "format": "json"
  6242. }
  6243. }
  6244. },
  6245. "payload": {
  6246. "s5ccecfce_data_1": {
  6247. "encoding": "jpg",
  6248. "image": str(base64.b64encode(imageBytes), 'UTF-8'),
  6249. "status": 3
  6250. }
  6251. }
  6252. }
  6253. request_Id_card_url = assemble_ws_auth_url(url, "POST", APIKey, APISecret)
  6254. headers = {'content-type': "application/json", 'host': 'api.xf-yun.com', 'app_id': APPId}
  6255. #print(request_url)
  6256. Id_card_response = requests.post(request_Id_card_url, data=json.dumps(body), headers=headers)
  6257. IDCardtempResult = json.loads(Id_card_response.content.decode())
  6258. IDCardfinalResult = base64.b64decode(IDCard['payload']['result']['text']).decode()
  6259. IDCardfinalResult = IDCardfinalResult.replace(" ", "").replace("\n", "").replace("\t", "").strip()
  6260. `
  6261. return code
  6262. }
  6263. Blockly.Blocks['ai_online_ID_card_result'] = {
  6264. init: function () {
  6265. this.appendDummyInput()
  6266. .appendField('身份证识别结果'); //
  6267. this.setOutput(true, null);
  6268. this.setColour("#0000ff");
  6269. this.setTooltip(Blockly.Msg.handwritten_text_result);
  6270. this.setHelpUrl("");
  6271. }
  6272. };
  6273. Blockly.Python['ai_online_ID_card_result'] = function (block) {
  6274. let code = `IDCardfinalResult`;
  6275. return [code, Blockly.Python.ORDER_ATOMIC];
  6276. };
  6277. // 情绪识别
  6278. Blockly.Blocks['ai_online_emotion_init'] = {
  6279. init: function () {
  6280. this.appendDummyInput()
  6281. .appendField(Blockly.Msg.emotion_init);
  6282. // this.appendValueInput("path")
  6283. // .setCheck(null).appendField(Blockly.Msg.set_dictionary)
  6284. // .appendField(new Blockly.FieldVariable("emotion_recognize_result"), "varitem");
  6285. // this.appendDummyInput().appendField(Blockly.Msg.dictionary_contain);
  6286. this.setInputsInline(false)
  6287. this.setPreviousStatement(true, null);
  6288. this.setNextStatement(true, null);; //
  6289. this.setColour("#0000ff");
  6290. this.setTooltip(Blockly.Msg.emotion_init);
  6291. this.setHelpUrl("");
  6292. }
  6293. };
  6294. Blockly.Python['ai_online_emotion_init'] = function (block) {
  6295. // let = []
  6296. // let text = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC)
  6297. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  6298. // TODO: Assemble Python into code variable.
  6299. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  6300. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  6301. Blockly.Python.definitions_['v831_import_json'] = `import json`
  6302. Blockly.Python.addVariable("EMOTION_SEND_REQUEST", "EMOTION_SEND_REQUEST = {}", true);
  6303. Blockly.Python.definitions_['emotion_init'] = `def get_img_base64str(single_image_path):
  6304. with open(single_image_path, "rb") as fp:
  6305. imgbase64 = base64.b64encode(fp.read())
  6306. return imgbase64.decode()
  6307. url = "https://ai-api.cocorobo.hk/face"
  6308. `
  6309. // ${variable_name} =${text}
  6310. var code = ``;
  6311. return code;
  6312. };
  6313. Blockly.Blocks['ai_online_emotion_loading'] = {
  6314. init: function () {
  6315. this.appendDummyInput()
  6316. .appendField(Blockly.Msg.emotion_load); //
  6317. this.appendValueInput("path")
  6318. .setCheck(null)
  6319. .appendField(Blockly.Msg.online_face_init_path); //
  6320. this.setPreviousStatement(true, null);
  6321. this.setNextStatement(true, null);
  6322. this.setColour("#0000ff");
  6323. this.setTooltip(Blockly.Msg.emotion_load);
  6324. this.setHelpUrl("");
  6325. }
  6326. }
  6327. Blockly.Python.ai_online_emotion_loading = function (block) {
  6328. let text = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC)
  6329. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  6330. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  6331. Blockly.Python.definitions_['v831_import_json'] = `import json`
  6332. Blockly.Python.addVariable("EMOTION_SEND_REQUEST", "EMOTION_SEND_REQUEST = {}", true);
  6333. Blockly.Python.definitions_['emotion_init'] = `def get_img_base64str(single_image_path):
  6334. with open(single_image_path, "rb") as fp:
  6335. imgbase64 = base64.b64encode(fp.read())
  6336. return imgbase64.decode()
  6337. url = "https://ai-api.cocorobo.cn/face"
  6338. `
  6339. // let language = Blockly.Python.valueToCode(block, 'language', Blockly.Python.ORDER_ATOMIC);
  6340. let code = `imageEmotionURLbase64 = str(get_img_base64str(${text}))
  6341. # print(imageURLbase64)
  6342. data = "{\\"image\\":\\"" + str(imageEmotionURLbase64) + "\\"}"
  6343. try:
  6344. EMOTION_SEND_REQUEST = requests.post(url, data = data , headers = { "Ocp-Apim-Subscription-Key": "6baf787532ef4eec8bc93c88518b8916", "Content-type": "application/json" }, timeout = 10000)
  6345. EMOTION_SEND_REQUEST = json.loads(EMOTION_SEND_REQUEST.content)["data"]["result"]["face_list"][0]
  6346. # print(str(_COCOCLOUD_SEND_REQUEST.status_code)+", "+str(_COCOCLOUD_SEND_REQUEST.content))
  6347. except BaseException as e:
  6348. EMOTION_SEND_REQUEST = "no result"
  6349. pass
  6350. `
  6351. return code
  6352. }
  6353. Blockly.Blocks['ai_online_emotion_result'] = {
  6354. init: function () {
  6355. this.appendDummyInput()
  6356. .appendField(Blockly.Msg.emotion_result)
  6357. .appendField(new Blockly.FieldDropdown([
  6358. [Blockly.Msg.emotion_result_age, "age"],
  6359. [Blockly.Msg.emotion_result_emotion, "emotion"],
  6360. [Blockly.Msg.emotion_result_gender, "gender"],
  6361. [Blockly.Msg.emotion_result_expression, "expression"]
  6362. ]), "emotion_type");
  6363. this.setOutput(true, null);
  6364. this.setColour("#0000ff");
  6365. this.setTooltip(Blockly.Msg.emotion_result);
  6366. this.setHelpUrl("");
  6367. }
  6368. };
  6369. Blockly.Python['ai_online_emotion_result'] = function (block) {
  6370. var type = block.getFieldValue('emotion_type');
  6371. let code = ""
  6372. // console.log(type)
  6373. if (type == "age") {
  6374. code = `EMOTION_SEND_REQUEST["age"]`
  6375. } else {
  6376. code = `EMOTION_SEND_REQUEST["${type}"]["type"]`
  6377. }
  6378. return [code, Blockly.Python.ORDER_ATOMIC];
  6379. };
  6380. // 手势识别
  6381. Blockly.Blocks['ai_online_gesture_init'] = {
  6382. init: function () {
  6383. this.appendDummyInput()
  6384. .appendField(Blockly.Msg.gesture_init); //
  6385. this.setPreviousStatement(true, null);
  6386. this.setNextStatement(true, null);
  6387. this.setColour("#0000ff");
  6388. this.setTooltip(Blockly.Msg.gesture_init);
  6389. this.setHelpUrl("");
  6390. }
  6391. };
  6392. Blockly.Python['ai_online_gesture_init'] = function (block) {
  6393. // let = []
  6394. // TODO: Assemble Python into code variable.
  6395. Blockly.Python.definitions_['v831_import_fromPIL_Image'] = `from PIL import Image`
  6396. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  6397. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  6398. Blockly.Python.definitions_['v831_import_json'] = `import json`
  6399. Blockly.Python.definitions_['emotion_init'] = `
  6400. def getImgData(single_image_path):
  6401. imgGesture = Image.open(single_image_path).convert("RGB")
  6402. imgGesture.save(single_image_path.split(".")[0] + ".png", "png")
  6403. with open(single_image_path.split(".")[0] + ".png", "rb") as fp:
  6404. return fp.read()
  6405. url = "https://ai-api.cocorobo.hk/gesture"
  6406. `
  6407. var code = '';
  6408. return code;
  6409. };
  6410. Blockly.Blocks['ai_online_gesture_loading'] = {
  6411. init: function () {
  6412. this.appendDummyInput()
  6413. .appendField(Blockly.Msg.gesture_load);
  6414. this.appendValueInput("path")
  6415. .setCheck(null)
  6416. .appendField(Blockly.Msg.online_face_init_path); //
  6417. this.setPreviousStatement(true, null);
  6418. this.setNextStatement(true, null);
  6419. this.setColour("#0000ff");
  6420. this.setTooltip(Blockly.Msg.gesture_load);
  6421. this.setHelpUrl("");
  6422. }
  6423. }
  6424. Blockly.Python.ai_online_gesture_loading = function (block) {
  6425. let text = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC)
  6426. Blockly.Python.definitions_['v831_import_fromPIL_Image'] = `from PIL import Image`
  6427. Blockly.Python.definitions_['v831_import_base64'] = `import base64`
  6428. Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  6429. Blockly.Python.definitions_['v831_import_json'] = `import json`
  6430. Blockly.Python.definitions_['emotion_init'] = `
  6431. def getImgData(single_image_path):
  6432. imgGesture = Image.open(single_image_path).convert("RGB")
  6433. imgGesture.save(single_image_path.split(".")[0] + ".png", "png")
  6434. with open(single_image_path.split(".")[0] + ".png", "rb") as fp:
  6435. return fp.read()
  6436. url = "https://ai-api.cocorobo.cn/gesture"
  6437. `
  6438. let code = `try:
  6439. payload={}
  6440. files=[("image",("filename.png", getImgData(${text}),"image/png"))]
  6441. headers = {}
  6442. GESTURE_SEND_REQUEST = requests.post(url, headers=headers, data=payload, files=files, timeout = 10000)
  6443. GESTURE_SEND_REQUEST = json.loads(GESTURE_SEND_REQUEST.content)["data"]["result"][0]["classname"]
  6444. # print(str(_COCOCLOUD_SEND_REQUEST.status_code)+", "+str(_COCOCLOUD_SEND_REQUEST.content))
  6445. except BaseException as e:
  6446. GESTURE_SEND_REQUEST = "no result"
  6447. pass
  6448. `
  6449. return code
  6450. }
  6451. Blockly.Blocks['ai_online_gesture_result'] = {
  6452. init: function () {
  6453. this.appendDummyInput()
  6454. .appendField(Blockly.Msg.gesture_result);
  6455. this.setOutput(true, null);
  6456. this.setColour("#0000ff");
  6457. this.setTooltip(Blockly.Msg.gesture_result);
  6458. this.setHelpUrl("");
  6459. }
  6460. };
  6461. Blockly.Python['ai_online_gesture_result'] = function (block) {
  6462. let code = `GESTURE_SEND_REQUEST`
  6463. return [code, Blockly.Python.ORDER_ATOMIC];
  6464. };
  6465. Blockly.Blocks['ai_audio_set'] = {
  6466. init: function () {
  6467. this.appendValueInput("Num")
  6468. .setCheck(null)
  6469. .appendField(Blockly.Msg.ai_audio_set_music_vol);
  6470. this.setInputsInline(true);
  6471. this.setPreviousStatement(true, null);
  6472. this.setNextStatement(true, null);
  6473. this.setColour("#603ea0");
  6474. this.setTooltip("");
  6475. this.setHelpUrl("");
  6476. }
  6477. };
  6478. Blockly.Python.ai_audio_set = function (block) {
  6479. var _num = Blockly.Python.valueToCode(block, 'Num', Blockly.Python.ORDER_ATOMIC);
  6480. var _code = "_player.volume(" + _num + ")\n"
  6481. return _code;
  6482. }
  6483. var SPEECH_RECOGNITION_BLOCK_COLOR = "230";
  6484. Blockly.Blocks['speech_recognition_new_setup'] = {
  6485. init: function () {
  6486. this.appendDummyInput()
  6487. .appendField(new Blockly.FieldImage("blockly/media/speech_recognition_header_new.png", 70, 70, { alt: "*", flipRtl: "FALSE" }));
  6488. this.appendDummyInput()
  6489. .appendField(Blockly.Msg.ai_speech_recognition_setup);
  6490. this.setPreviousStatement(true, null);
  6491. this.setNextStatement(true, null);
  6492. this.setColour("#ee783a");
  6493. this.setTooltip(Blockly.Msg.speech_recognition_new_setup_TOOLTIP);
  6494. this.setHelpUrl("");
  6495. }
  6496. };
  6497. Blockly.Python['speech_recognition_new_setup'] = function (block) {
  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. let code = ""
  6505. return code;
  6506. };
  6507. Blockly.Blocks['speech_recognition_clear'] = {
  6508. init: function () {
  6509. // this.appendDummyInput()
  6510. // .appendField(new Blockly.FieldImage("blockly/media/speech_recognition_header_new.png", 70, 70, { alt: "*", flipRtl: "FALSE" }));
  6511. this.appendDummyInput()
  6512. .appendField(Blockly.Msg.speech_recognition_Clean_up_the_last_study_result);
  6513. this.setPreviousStatement(true, null);
  6514. this.setNextStatement(true, null);
  6515. this.setColour("#ee783a");
  6516. this.setTooltip(Blockly.Msg.speech_recognition_Clean_up_the_last_study_result);
  6517. this.setHelpUrl("");
  6518. }
  6519. };
  6520. Blockly.Python['speech_recognition_clear'] = function (block) {
  6521. Blockly.Python.definitions_["v831_import_coco_mfcc"] = `import _coco_mfcc`
  6522. Blockly.Python.definitions_["v831_coco_mfcc_init"] = `mfcc = _coco_mfcc.MFCC(is_reply=False)`;
  6523. var code = `mfcc.clear()
  6524. `;
  6525. return code;
  6526. };
  6527. Blockly.Blocks['speech_recognition_record_start_result'] = {
  6528. init: function () {
  6529. this.appendDummyInput()
  6530. .appendField(new Blockly.FieldImage("blockly/media/speech_recognition_header_recording_new.png", 130, 60, { alt: "*", flipRtl: "FALSE" }));
  6531. this.appendDummyInput()
  6532. .appendField(Blockly.Msg.ai_speech_recognition_start_recording);
  6533. this.appendDummyInput()
  6534. .appendField(Blockly.Msg.ai_speech_recognition_start_recording_clip_text)
  6535. .appendField(new Blockly.FieldNumber(2, 1, 6, 1), "clip")
  6536. // .appendField(Blockly.Msg.ai_speech_recognition_start_recording_take_note);
  6537. this.appendDummyInput()
  6538. .appendField(Blockly.Msg.image_process_interface)
  6539. .appendField(new Blockly.FieldVariable("recordCanvas"), "varitem");
  6540. this.setPreviousStatement(true, null);
  6541. this.setNextStatement(true, null);
  6542. this.setColour("#ee783a");
  6543. this.setTooltip(Blockly.Msg.ai_speech_recognition_start_recording);
  6544. this.setHelpUrl("");
  6545. }
  6546. };
  6547. Blockly.Python['speech_recognition_record_start_result'] = function (block) {
  6548. var number_take = block.getFieldValue('clip');
  6549. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  6550. Blockly.Python.definitions_["v831_import_coco_mfcc"] = `import _coco_mfcc`
  6551. Blockly.Python.definitions_["v831_import_time"] = `import time`
  6552. Blockly.Python.definitions_["v831_coco_mfcc_init"] = `mfcc = _coco_mfcc.MFCC(is_reply=False)`;
  6553. Blockly.Python.addVariable("RecordState", "RecordState = 0", true);
  6554. Blockly.Python.addVariable("recordFrequency", "recordFrequency = 0", true);
  6555. Blockly.Python.addVariable("recordResultData", `recordResultData = None`, true);
  6556. Blockly.Python.addVariable('number_take', `number_take = ${number_take}`, true)
  6557. Blockly.Python.definitions_["v831_mfcc_clear"] = `mfcc.clear()
  6558. `
  6559. Blockly.Python.definitions_['v831_import_display'] = `from maix import display`;
  6560. Blockly.Python.definitions_['v831_import_image'] = `from maix import image`;
  6561. Blockly.Python.definitions_['v831_import_camera'] = `from maix import camera`;
  6562. Blockly.Python.definitions_['v831_import_os'] = `import os`
  6563. Blockly.Python.definitions_['v831_camera_set'] = `cameraSize = True
  6564. def CAMERATYPE():
  6565. global cameraSize
  6566. try:
  6567. if os.path.exists("/etc/cameraSize.cfg"):
  6568. cameraSize = True
  6569. else:
  6570. cameraSize = False
  6571. except:
  6572. cameraSize = False
  6573. CAMERATYPE()
  6574. `
  6575. Blockly.Python.addVariable("ScreenOrientation", `ScreenOrientation = False`, true)
  6576. Blockly.Python.addVariable(variable_name, `${variable_name} = image.new(size = (320, 240))`, true)
  6577. Blockly.Python.definitions_['import_image'] = `image.load_freetype("/root/preset/fonts/simhei.ttf")
  6578. if cameraSize==True:
  6579. camera.camera.config(size=(320,240))
  6580. else:
  6581. camera.camera.config(size=(240,320))
  6582. `
  6583. let idenSuccess = "录制成功";
  6584. let speek = "说第";
  6585. let SpeechFragment = "语音片段";
  6586. if (localStorage.getItem('handPyLanguage') == "zh-hant") {
  6587. idenSuccess = "錄製成功";
  6588. speek = "說第 ";
  6589. SpeechFragment = "語音片段";
  6590. } else if (localStorage.getItem('handPyLanguage') == "en") {
  6591. idenSuccess = "Recorded successfully";
  6592. speek = "Speek ";
  6593. SpeechFragment = "Speech fragment";
  6594. }
  6595. var code = `if RecordState == 0:
  6596. mfcc.recording(recordFrequency)
  6597. recordFrequency = recordFrequency + 1
  6598. time.sleep(1)
  6599. RecordState = 1
  6600. if RecordState == 1:
  6601. recordData = mfcc.state()
  6602. if recordData == mfcc._mfcc_result:
  6603. ${variable_name}.draw_string(0,80,"${idenSuccess}", scale = 2, color = (0,204,204) , thickness = 1)
  6604. time.sleep(1)
  6605. if recordFrequency < number_take:
  6606. RecordState = 0
  6607. else:
  6608. ${variable_name}.draw_string(0,80, "${speek} "+str(recordFrequency)+" ${SpeechFragment}", scale = 2, color = (0,204,204) , thickness = 1)
  6609. `
  6610. return code
  6611. };
  6612. Blockly.Blocks['speech_recognition_new_start_recognition'] = {
  6613. init: function () {
  6614. this.appendDummyInput()
  6615. .appendField(new Blockly.FieldImage("blockly/media/speech_recognition_header_recognition_new.png", 130, 60, { alt: "*", flipRtl: "FALSE" }));
  6616. this.appendDummyInput()
  6617. .appendField(Blockly.Msg.ai_speech_recognition_start_recognizing);
  6618. this.setPreviousStatement(true, null);
  6619. this.setNextStatement(true, null);
  6620. this.setColour("#ee783a");
  6621. this.setTooltip(Blockly.Msg.speech_recognition_new_start_recognition_TOOLTIP);
  6622. this.setHelpUrl("");
  6623. }
  6624. };
  6625. Blockly.Python['speech_recognition_new_start_recognition'] = function (block) {
  6626. Blockly.Python.definitions_["v831_import_coco_mfcc"] = `import _coco_mfcc`
  6627. Blockly.Python.definitions_["v831_import_time"] = `import time`
  6628. Blockly.Python.definitions_["v831_coco_mfcc_init"] = `mfcc = _coco_mfcc.MFCC(is_reply=False)`;
  6629. Blockly.Python.addVariable("RecordState", "RecordState = 0", true);
  6630. Blockly.Python.addVariable("recordFrequency", "recordFrequency = 0", true);
  6631. Blockly.Python.addVariable("recordResultData", `recordResultData = None`, true);
  6632. var code = `if RecordState == 0:
  6633. mfcc.recognize()
  6634. recordResultData = None
  6635. time.sleep(1)
  6636. RecordState = 1
  6637. `
  6638. return code
  6639. };
  6640. Blockly.Blocks['speech_recognition_new_recognition_get_any_result'] = {
  6641. init: function () {
  6642. this.appendDummyInput()
  6643. .appendField(Blockly.Msg.ai_speech_recognition_any_result_text);
  6644. this.appendStatementInput("NAME")
  6645. .setCheck(null)
  6646. .appendField(Blockly.Msg.ai_speech_recognition_any_result_text_do);
  6647. this.setPreviousStatement(true, null);
  6648. this.setNextStatement(true, null);
  6649. this.setColour("#ee783a");
  6650. this.setTooltip(Blockly.Msg.speech_recognition_new_recognition_get_any_result_TOOLTIP);
  6651. this.setHelpUrl("");
  6652. }
  6653. };
  6654. Blockly.Python['speech_recognition_new_recognition_get_any_result'] = function (block) {
  6655. var statements_name = Blockly.Python.statementToCode(block, 'NAME');
  6656. // TODO: Assemble Python into code variable.
  6657. var code = `if RecordState == 1:
  6658. recordData = mfcc.state()
  6659. if recordData == mfcc._mfcc_result:
  6660. recordResultData = mfcc.result()
  6661. time.sleep(1)
  6662. RecordState = 0
  6663. ${statements_name}
  6664. `;
  6665. return code;
  6666. };
  6667. Blockly.Blocks['speech_recognition_new_recognition_get_result'] = {
  6668. init: function () {
  6669. this.appendDummyInput()
  6670. .appendField(Blockly.Msg.ai_speech_recognition_get_result)
  6671. .appendField(new Blockly.FieldNumber(1, 1, 6, 1), "index")
  6672. .appendField(Blockly.Msg.ai_speech_recognition_get_result_after);
  6673. this.setOutput(true, null);
  6674. this.setColour("#ee783a");
  6675. this.setTooltip(Blockly.Msg.speech_recognition_new_recognition_get_result_TOOLTIP);
  6676. this.setHelpUrl("");
  6677. }
  6678. };
  6679. Blockly.Python['speech_recognition_new_recognition_get_result'] = function (block) {
  6680. var number_index = block.getFieldValue('index');
  6681. // TODO: Assemble Python into code variable.
  6682. var code = `recordResultData == ${number_index - 1}`;
  6683. // TODO: Change ORDER_NONE to the correct strength.
  6684. return [code, Blockly.Python.ORDER_NONE];
  6685. };
  6686. /*
  6687. _ ___ ____ _
  6688. / \ |_ _| / ___| _ __ ___ __ _| | _____ _ __
  6689. / _ \ | | \___ \| '_ \ / _ \/ _` | |/ / _ \ '__|
  6690. / ___ \ _ | | _ ___) | |_) | __/ (_| | < __/ |
  6691. /_/ \_(_)___(_) |____/| .__/ \___|\__,_|_|\_\___|_|
  6692. |_|
  6693. */
  6694. Blockly.Blocks['ai_audio_init_speaker'] = {
  6695. init: function () {
  6696. this.appendDummyInput()
  6697. .appendField(new Blockly.FieldImage("blockly/media/speaker_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  6698. this.appendDummyInput()
  6699. .appendField(Blockly.Msg.ai_audio_init);
  6700. this.setColour("#5f7fc1");
  6701. this.setTooltip(Blockly.Msg.ai_audio_init_speaker_TOOLTIP);
  6702. this.setHelpUrl("");
  6703. this.setPreviousStatement(true);
  6704. this.setNextStatement(true);
  6705. }
  6706. };
  6707. Blockly.Python.ai_audio_init_speaker = function (block) {
  6708. Blockly.Python.definitions_.import_Maix = "from Maix import I2S, GPIO";
  6709. Blockly.Python.definitions_.import_fpioa_manager = "from fpioa_manager import *";
  6710. Blockly.Python.definitions_.import_audio = "import audio";
  6711. var _code = "";
  6712. return _code;
  6713. }
  6714. Blockly.Blocks['ai_audio_play_song_speaker'] = {
  6715. init: function () {
  6716. this.appendDummyInput()
  6717. .appendField(Blockly.Msg.ai_audio_play_music_file);
  6718. // this.appendDummyInput()
  6719. // .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol)
  6720. // .appendField(new Blockly.FieldNumber(95, 0, 100, 1), "play_vol");
  6721. this.appendValueInput("volume")
  6722. .setCheck(null)
  6723. .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol);
  6724. this.appendValueInput("path")
  6725. .setCheck(null)
  6726. .appendField(Blockly.Msg.ai_audio_play_music_file_set_file_path);
  6727. this.setPreviousStatement(true, null);
  6728. this.setNextStatement(true, null);
  6729. this.setColour("#5f7fc1");
  6730. this.setTooltip(Blockly.Msg.ai_audio_play_song_speaker_TOOLTIP);
  6731. this.setHelpUrl("");
  6732. }
  6733. };
  6734. Blockly.Python['ai_audio_play_song_speaker'] = function (block) {
  6735. // var number_play_vol = block.getFieldValue('play_vol');
  6736. var number_play_vol = Blockly.Python.valueToCode(block, 'volume', Blockly.Python.ORDER_ATOMIC);
  6737. var value_name = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  6738. var _in = "" +
  6739. "_audio_rx = I2S(I2S.DEVICE_1)\n" +
  6740. "\n" +
  6741. "def _speaker_play_song(path, volume):\n" +
  6742. " global _audio_play_state,_player\n" +
  6743. " _player = audio.Audio(path = path)\n" +
  6744. " _player.volume(volume)\n" +
  6745. " try:\n" +
  6746. " _wav_info = _player.play_process(_audio_rx)\n" +
  6747. " _audio_rx.channel_config(_audio_rx.CHANNEL_1, I2S.TRANSMITTER, resolution = I2S.RESOLUTION_16_BIT, align_mode = I2S.STANDARD_MODE)\n" +
  6748. " _audio_rx.set_sample_rate(_wav_info[1])\n" +
  6749. " _audio_play_state = 0\n" +
  6750. "\n" +
  6751. " fm.register(34,fm.fpioa.I2S1_OUT_D1)\n" +
  6752. " fm.register(35,fm.fpioa.I2S1_SCLK)\n" +
  6753. " fm.register(33,fm.fpioa.I2S1_WS)\n" +
  6754. "\n" +
  6755. " while True:\n" +
  6756. " _audio_play_state = _player.play()\n" +
  6757. " if (_audio_play_state) == True:\n" +
  6758. " print(\"Playing.\")\n" +
  6759. " elif (_audio_play_state) == False:\n" +
  6760. " print(\"Done.\")\n" +
  6761. " break\n" +
  6762. "\n" +
  6763. " except:\n" +
  6764. " pass\n" +
  6765. "\n";
  6766. // Blockly.Python.definitions_.import_audio = _in;
  6767. Blockly.Python.addVariable('_audio_play_state', _in, true);
  6768. // Blockly.Python.addSetup("ai_audio_read", _in);
  6769. var _code = "_speaker_play_song(path=" + value_name + ", volume=" + number_play_vol + ")\n";
  6770. return _code;
  6771. };
  6772. Blockly.Blocks['ai_audio_play_finish_speaker'] = {
  6773. init: function () {
  6774. this.appendDummyInput()
  6775. .appendField(Blockly.Msg.ai_audio_end_play);
  6776. this.setPreviousStatement(true, null);
  6777. this.setNextStatement(true, null);
  6778. this.setColour("#5f7fc1");
  6779. this.setTooltip(Blockly.Msg.ai_audio_play_finish_speaker_TOOLTIP);
  6780. this.setHelpUrl("");
  6781. }
  6782. };
  6783. Blockly.Python['ai_audio_play_finish_speaker'] = function (block) {
  6784. // TODO: Assemble Python into code variable.
  6785. var code = '_player.finish()\n';
  6786. return code;
  6787. };
  6788. Blockly.Blocks['ai_audio_set_speaker'] = {
  6789. init: function () {
  6790. this.appendValueInput("Num")
  6791. .setCheck(null)
  6792. .appendField(Blockly.Msg.ai_audio_set_music_vol);
  6793. this.setInputsInline(true);
  6794. this.setPreviousStatement(true, null);
  6795. this.setNextStatement(true, null);
  6796. this.setColour("#5f7fc1");
  6797. this.setTooltip(Blockly.Msg.ai_audio_set_speaker_TOOLTIP);
  6798. this.setHelpUrl("");
  6799. }
  6800. };
  6801. Blockly.Python.ai_audio_set_speaker = function (block) {
  6802. var _num = Blockly.Python.valueToCode(block, 'Num', Blockly.Python.ORDER_ATOMIC);
  6803. var _code = "_player.volume(" + _num + ")\n"
  6804. return _code;
  6805. }
  6806. Blockly.Blocks['ai_audio_files_list'] = {
  6807. init: function () {
  6808. this.appendDummyInput()
  6809. .appendField(Blockly.Msg.ai_audio_files_list);
  6810. this.appendValueInput("path")
  6811. .setCheck(null)
  6812. .appendField(Blockly.Msg.ai_audio_files_list_set_file_path);
  6813. this.setOutput(true, null);
  6814. this.setColour("#5f7fc1");
  6815. this.setTooltip(Blockly.Msg.ai_audio_files_list_TOOLTIP);
  6816. this.setHelpUrl("");
  6817. }
  6818. };
  6819. Blockly.Python.ai_audio_files_list = function (block) {
  6820. var value_name = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  6821. Blockly.Python.definitions_['v831_import_os'] = `import os`
  6822. Blockly.Python.definitions_.import_ai_audio_files_list = '' +
  6823. 'def getRawFileList(path):\n' +
  6824. ' files = []\n' +
  6825. ' for f in os.listdir(path):\n' +
  6826. ' if (not f.endswith("~") or not f == "") and f.split(".")[1] == "wav":\n' +
  6827. ' files.append(path + "/" + f)\n' +
  6828. ' return files\n' +
  6829. '\n';
  6830. var code = 'getRawFileList(' + value_name + ')';
  6831. return [code, Blockly.Python.ORDER_NONE];
  6832. };
  6833. Blockly.Blocks['ai_audio_play_song_list_loop_speaker'] = {
  6834. init: function () {
  6835. this.appendDummyInput()
  6836. .appendField(Blockly.Msg.ai_audio_play_music_file_list_loop);
  6837. // this.appendDummyInput()
  6838. // .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol)
  6839. // .appendField(new Blockly.FieldNumber(95, 0, 100, 1), "play_vol");
  6840. this.appendValueInput("volume")
  6841. .setCheck(null)
  6842. .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol);
  6843. // this.appendValueInput("path")
  6844. // .setCheck(null)
  6845. // .appendField(Blockly.Msg.ai_audio_play_music_file_set_file_path);
  6846. this.appendValueInput("my_list")
  6847. .setCheck(null)
  6848. .appendField(Blockly.Msg.ai_audio_play_music_file_set_files_list);
  6849. this.appendValueInput("firstIndex")
  6850. .setCheck(null)
  6851. .appendField(Blockly.Msg.ai_audio_play_music_file_set_first_index);
  6852. this.setPreviousStatement(true, null);
  6853. this.setNextStatement(true, null);
  6854. this.setColour("#5f7fc1");
  6855. this.setTooltip(Blockly.Msg.ai_audio_play_song_speaker_list_loop_TOOLTIP);
  6856. this.setHelpUrl("");
  6857. }
  6858. };
  6859. Blockly.Python['ai_audio_play_song_list_loop_speaker'] = function (block) {
  6860. // var number_play_vol = block.getFieldValue('play_vol');
  6861. var number_play_vol = Blockly.Python.valueToCode(block, 'volume', Blockly.Python.ORDER_ATOMIC);
  6862. // var value_name = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  6863. var my_list = Blockly.Python.valueToCode(block, 'my_list', Blockly.Python.ORDER_ATOMIC);
  6864. var firstIndex = Blockly.Python.valueToCode(block, 'firstIndex', Blockly.Python.ORDER_ATOMIC);
  6865. var _in = "" +
  6866. "_audio_rx = I2S(I2S.DEVICE_1)\n" +
  6867. "direction = 0\n" +
  6868. "firstIndex = " + firstIndex + "\n" +
  6869. "\n" +
  6870. "# 上一首\n" +
  6871. "def previous_song(index,my_list):\n" +
  6872. " global direction\n" +
  6873. " index = index - 1\n" +
  6874. " direction = 1\n" +
  6875. " if index == -1:\n" +
  6876. " index = len(my_list) - 1\n" +
  6877. " return index\n" +
  6878. "\n" +
  6879. "# 切换上一首\n" +
  6880. "def change_previous_song(index):\n" +
  6881. " global _audio_play_state,direction,firstIndex,_player\n" +
  6882. " _player.finish()\n" +
  6883. " _audio_play_state = 0\n" +
  6884. " firstIndex = index\n" +
  6885. " direction = 1\n" +
  6886. "\n" +
  6887. "# 下一首\n" +
  6888. "def next_song(index,my_list):\n" +
  6889. " global direction\n" +
  6890. " index = index + 1\n" +
  6891. " direction = 2\n" +
  6892. " if index == len(my_list):\n" +
  6893. " index = 0\n" +
  6894. " return index\n" +
  6895. "\n" +
  6896. "# 切换下一首\n" +
  6897. "def change_next_song(index):\n" +
  6898. " global _audio_play_state,direction,firstIndex,_player\n" +
  6899. " _player.finish()\n" +
  6900. " _audio_play_state = 0\n" +
  6901. " firstIndex = index\n" +
  6902. " direction = 2\n" +
  6903. "\n" +
  6904. "def _speaker_play_song_list_loop(volume, my_list):\n" +
  6905. " global _audio_play_state,_player,firstIndex,play_again,direction\n" +
  6906. " _player = audio.Audio(path = my_list[firstIndex])\n" +
  6907. " _player.volume(volume)\n" +
  6908. " try:\n" +
  6909. // " firstIndex = index\n" +
  6910. " _wav_info = _player.play_process(_audio_rx)\n" +
  6911. " _audio_rx.channel_config(_audio_rx.CHANNEL_1, I2S.TRANSMITTER, resolution = I2S.RESOLUTION_16_BIT, align_mode = I2S.STANDARD_MODE)\n" +
  6912. " _audio_rx.set_sample_rate(_wav_info[1])\n" +
  6913. " _audio_play_state = 0\n" +
  6914. "\n" +
  6915. " fm.register(34,fm.fpioa.I2S1_OUT_D1)\n" +
  6916. " fm.register(35,fm.fpioa.I2S1_SCLK)\n" +
  6917. " fm.register(33,fm.fpioa.I2S1_WS)\n" +
  6918. "\n" +
  6919. " button_a_state = False\n" +
  6920. " button_b_state = False\n" +
  6921. " while True:\n" +
  6922. " _audio_play_state = _player.play()\n" +
  6923. " if (_audio_play_state) == True:\n" +
  6924. " #print(\"Playing.\")\n" +
  6925. " if (_gp_side_a.value() == 1) and button_a_state == False:\n" +
  6926. " index = previous_song(firstIndex,my_list)\n" +
  6927. " button_a_state = True\n" +
  6928. " elif (_gp_side_a.value() == 0) and button_a_state == True:\n" +
  6929. " button_a_state = False\n" +
  6930. " change_previous_song(index)\n" +
  6931. " break\n" +
  6932. " if (_gp_side_b.value() == 1) and button_b_state == False:\n" +
  6933. " index = next_song(firstIndex,my_list)\n" +
  6934. " button_b_state = True\n" +
  6935. " elif (_gp_side_b.value() == 0) and button_b_state == True:\n" +
  6936. " button_b_state = False\n" +
  6937. " change_next_song(index)\n" +
  6938. " break\n" +
  6939. "\n" +
  6940. " elif (_audio_play_state) == False:\n" +
  6941. " #print(\"Done.\")\n" +
  6942. " index = firstIndex + 1\n" +
  6943. " if index == len(my_list):\n" +
  6944. " index = 0\n" +
  6945. " firstIndex = index\n" +
  6946. " break\n" +
  6947. "\n" +
  6948. " except:\n" +
  6949. " play_again = True\n" +
  6950. " if direction == 0:\n" +
  6951. " direction = 3\n" +
  6952. " return\n" +
  6953. "\n";
  6954. // Blockly.Python.definitions_.import_audio = _in;
  6955. Blockly.Python.addVariable('_audio_play_list_loop_state', _in, true);
  6956. Blockly.Python.definitions_['import_define_x_ai_button'] = '' +
  6957. 'from fpioa_manager import *\n' +
  6958. 'from Maix import FPIOA, GPIO\n' +
  6959. '\n' +
  6960. '_gp_side_buttons = [9, 10, 11]\n' +
  6961. '\n' +
  6962. 'FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)\n' +
  6963. 'FPIOA().set_function(_gp_side_buttons[1],FPIOA.GPIO1)\n' +
  6964. 'FPIOA().set_function(_gp_side_buttons[2],FPIOA.GPIO2)\n' +
  6965. '\n' +
  6966. '_gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)\n' +
  6967. '_gp_side_b = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)\n' +
  6968. '_gp_side_c = GPIO(GPIO.GPIO2,GPIO.IN,GPIO.PULL_UP)\n' +
  6969. '\n';
  6970. // Blockly.Python.addSetup("ai_audio_read", _in);
  6971. var _code = "_speaker_play_song_list_loop(volume=" + number_play_vol + ", my_list=" + my_list + ")\n";
  6972. return _code;
  6973. };
  6974. Blockly.Blocks['ai_audio_play_song_list_random_speaker'] = {
  6975. init: function () {
  6976. this.appendDummyInput()
  6977. .appendField(Blockly.Msg.ai_audio_play_music_file_list_random);
  6978. // this.appendDummyInput()
  6979. // .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol)
  6980. // .appendField(new Blockly.FieldNumber(95, 0, 100, 1), "play_vol");
  6981. this.appendValueInput("volume")
  6982. .setCheck(null)
  6983. .appendField(Blockly.Msg.ai_audio_play_music_file_set_vol);
  6984. // this.appendValueInput("path")
  6985. // .setCheck(null)
  6986. // .appendField(Blockly.Msg.ai_audio_play_music_file_set_file_path);
  6987. this.appendValueInput("my_list")
  6988. .setCheck(null)
  6989. .appendField(Blockly.Msg.ai_audio_play_music_file_set_files_list);
  6990. this.appendValueInput("firstIndex")
  6991. .setCheck(null)
  6992. .appendField(Blockly.Msg.ai_audio_play_music_file_set_first_index);
  6993. this.setPreviousStatement(true, null);
  6994. this.setNextStatement(true, null);
  6995. this.setColour("#5f7fc1");
  6996. this.setTooltip(Blockly.Msg.ai_audio_play_music_file_list_random_TOOLTIP);
  6997. this.setHelpUrl("");
  6998. }
  6999. };
  7000. Blockly.Python['ai_audio_play_song_list_random_speaker'] = function (block) {
  7001. // var number_play_vol = block.getFieldValue('play_vol');
  7002. var number_play_vol = Blockly.Python.valueToCode(block, 'volume', Blockly.Python.ORDER_ATOMIC);
  7003. // var value_name = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  7004. var my_list = Blockly.Python.valueToCode(block, 'my_list', Blockly.Python.ORDER_ATOMIC);
  7005. var firstIndex = Blockly.Python.valueToCode(block, 'firstIndex', Blockly.Python.ORDER_ATOMIC);
  7006. Blockly.Python.definitions_['v831_import_time'] = `import time`
  7007. Blockly.Python.definitions_['v831_import_random'] = `import random`
  7008. var _in = "" +
  7009. "random.seed(time.ticks_cpu())\n" +
  7010. "\n" +
  7011. "_audio_rx = I2S(I2S.DEVICE_1)\n" +
  7012. "firstIndex = " + firstIndex + "\n" +
  7013. "\n" +
  7014. "# 随机一首\n" +
  7015. "def random_song(index,my_list):\n" +
  7016. " index = random.randrange(0, len(my_list), 1)\n" +
  7017. " return index\n" +
  7018. "\n" +
  7019. "# 切换歌曲\n" +
  7020. "def change_random_song(index):\n" +
  7021. " global _audio_play_state,firstIndex,_player\n" +
  7022. " _player.finish()\n" +
  7023. " _audio_play_state = 0\n" +
  7024. " firstIndex = index\n" +
  7025. "\n" +
  7026. "def _speaker_play_song_list_random(volume, my_list):\n" +
  7027. " global _audio_play_state,_player,firstIndex,play_again\n" +
  7028. " _player = audio.Audio(path = my_list[firstIndex])\n" +
  7029. " _player.volume(volume)\n" +
  7030. " try:\n" +
  7031. // " firstIndex = index\n" +
  7032. " _wav_info = _player.play_process(_audio_rx)\n" +
  7033. " _audio_rx.channel_config(_audio_rx.CHANNEL_1, I2S.TRANSMITTER, resolution = I2S.RESOLUTION_16_BIT, align_mode = I2S.STANDARD_MODE)\n" +
  7034. " _audio_rx.set_sample_rate(_wav_info[1])\n" +
  7035. " _audio_play_state = 0\n" +
  7036. "\n" +
  7037. " fm.register(34,fm.fpioa.I2S1_OUT_D1)\n" +
  7038. " fm.register(35,fm.fpioa.I2S1_SCLK)\n" +
  7039. " fm.register(33,fm.fpioa.I2S1_WS)\n" +
  7040. "\n" +
  7041. " button_a_state = False\n" +
  7042. " while True:\n" +
  7043. " _audio_play_state = _player.play()\n" +
  7044. " if (_audio_play_state) == True:\n" +
  7045. " #print(\"Playing.\")\n" +
  7046. " if (_gp_side_a.value() == 1) and button_a_state == False:\n" +
  7047. " index = random_song(firstIndex,my_list)\n" +
  7048. " button_a_state = True\n" +
  7049. " elif (_gp_side_a.value() == 0) and button_a_state == True:\n" +
  7050. " button_a_state = False\n" +
  7051. " change_random_song(index)\n" +
  7052. " break\n" +
  7053. "\n" +
  7054. " elif (_audio_play_state) == False:\n" +
  7055. " #print(\"Done.\")\n" +
  7056. " index = random_song(firstIndex,my_list)\n" +
  7057. " firstIndex = index\n" +
  7058. " break\n" +
  7059. "\n" +
  7060. " except:\n" +
  7061. " play_again = True\n" +
  7062. " return\n" +
  7063. "\n";
  7064. // Blockly.Python.definitions_.import_audio = _in;
  7065. Blockly.Python.addVariable('_audio_play_list_random_state', _in, true);
  7066. Blockly.Python.definitions_['import_define_x_ai_button'] = '' +
  7067. 'from fpioa_manager import *\n' +
  7068. 'from Maix import FPIOA, GPIO\n' +
  7069. '\n' +
  7070. '_gp_side_buttons = [9, 10, 11]\n' +
  7071. '\n' +
  7072. 'FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)\n' +
  7073. 'FPIOA().set_function(_gp_side_buttons[1],FPIOA.GPIO1)\n' +
  7074. 'FPIOA().set_function(_gp_side_buttons[2],FPIOA.GPIO2)\n' +
  7075. '\n' +
  7076. '_gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)\n' +
  7077. '_gp_side_b = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)\n' +
  7078. '_gp_side_c = GPIO(GPIO.GPIO2,GPIO.IN,GPIO.PULL_UP)\n' +
  7079. '\n';
  7080. // Blockly.Python.addSetup("ai_audio_read", _in);
  7081. var _code = "_speaker_play_song_list_random(volume=" + number_play_vol + ", my_list=" + my_list + ")\n";
  7082. return _code;
  7083. };
  7084. /*
  7085. _ ___ __ ___ _
  7086. / \ |_ _| \ \ / (_) __| | ___ ___
  7087. / _ \ | | \ \ / /| |/ _` |/ _ \/ _ \
  7088. / ___ \ | | \ V / | | (_| | __/ (_) |
  7089. /_/ \_\___| \_/ |_|\__,_|\___|\___/
  7090. */
  7091. Blockly.Blocks['ai_video_record_setup'] = {
  7092. init: function () {
  7093. this.appendDummyInput()
  7094. .appendField(new Blockly.FieldImage("blockly/media/video_record_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  7095. this.appendDummyInput()
  7096. .appendField(Blockly.Msg.ai_video_start_recording);
  7097. this.appendValueInput("save_path")
  7098. .setCheck(null)
  7099. .appendField(Blockly.Msg.ai_video_start_recording_file_path);
  7100. this.appendValueInput("w")
  7101. .setCheck(null)
  7102. .appendField(Blockly.Msg.ai_video_start_recording_width);
  7103. this.appendValueInput("h")
  7104. .setCheck(null)
  7105. .appendField(Blockly.Msg.ai_video_start_recording_height);
  7106. this.setPreviousStatement(true, null);
  7107. this.setNextStatement(true, null);
  7108. this.setColour("#60c1bb");
  7109. this.setTooltip(Blockly.Msg.ai_video_record_setup_TOOLTIP);
  7110. this.setHelpUrl("");
  7111. }
  7112. };
  7113. Blockly.Python['ai_video_record_setup'] = function (block) {
  7114. var value_save_path = Blockly.Python.valueToCode(block, 'save_path', Blockly.Python.ORDER_ATOMIC);
  7115. var value_w = Blockly.Python.valueToCode(block, 'w', Blockly.Python.ORDER_ATOMIC);
  7116. var value_h = Blockly.Python.valueToCode(block, 'h', Blockly.Python.ORDER_ATOMIC);
  7117. Blockly.Python.definitions_.import_video = "import video";
  7118. Blockly.Python.definitions_.import_Maix = "from Maix import GPIO";
  7119. Blockly.Python.addVariable('record_vid', 'record_vid = None', true);
  7120. // TODO: Assemble Python into code variable.
  7121. var code = "_record_vid = video.open(" + value_save_path + ", record=1, width=" + value_w + ", height=" + value_h + ", interval=20000, quality=80)\n";
  7122. return code;
  7123. };
  7124. Blockly.Blocks['ai_video_get'] = {
  7125. init: function () {
  7126. this.appendDummyInput()
  7127. .appendField(Blockly.Msg.ai_video_to_canvas)
  7128. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  7129. .appendField(Blockly.Msg.ai_video_record);
  7130. this.setInputsInline(false);
  7131. this.setPreviousStatement(true, null);
  7132. this.setNextStatement(true, null);
  7133. this.setColour("#60c1bb");
  7134. this.setTooltip(Blockly.Msg.ai_video_get_TOOLTIP);
  7135. this.setHelpUrl("");
  7136. }
  7137. };
  7138. Blockly.Python.ai_video_get = function (block) {
  7139. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  7140. var _code = "_record_vid.record(" + variable_name + ")\n"
  7141. return _code;
  7142. }
  7143. Blockly.Blocks['ai_video_stop'] = {
  7144. init: function () {
  7145. this.appendDummyInput()
  7146. .appendField(Blockly.Msg.ai_video_stop_recording);
  7147. this.setPreviousStatement(true);
  7148. this.setNextStatement(true);
  7149. this.setColour("#60c1bb");
  7150. this.setTooltip(Blockly.Msg.ai_video_stop_TOOLTIP);
  7151. this.setHelpUrl("");
  7152. }
  7153. };
  7154. Blockly.Python.ai_video_stop = function (block) {
  7155. var _code = "_record_vid.record_finish()\n"
  7156. return _code;
  7157. }
  7158. Blockly.Blocks['ai_video_set_init'] = {
  7159. init: function () {
  7160. this.appendDummyInput()
  7161. .appendField(new Blockly.FieldImage("blockly/media/video_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  7162. this.appendDummyInput()
  7163. .appendField(Blockly.Msg.ai_video_set_play_init);
  7164. this.setPreviousStatement(true, null);
  7165. this.setNextStatement(true, null);
  7166. this.setColour("#4d9994");
  7167. this.setTooltip(Blockly.Msg.ai_video_set_play_init);
  7168. this.setHelpUrl("");
  7169. }
  7170. }
  7171. Blockly.Python['ai_video_set_init'] = function (block) {
  7172. Blockly.Python.definitions_['v831_import_os'] = `import os`
  7173. Blockly.Python.definitions_['v831_import_pyaudio'] = `import pyaudio`
  7174. Blockly.Python.definitions_['v831_import_av'] = `import av`
  7175. Blockly.Python.definitions_['v831_import_from_maix_display'] = `from maix import display`
  7176. Blockly.Python.definitions_['v831_import_from_maix_camera'] = `from maix import camera`
  7177. Blockly.Python.definitions_['v831_import_from_maix_image'] = `from maix import image`
  7178. // Blockly.Python.addVariable("VIDEOPLAY",`VIDEOPLAY = False`,true)
  7179. let code = ``
  7180. return code;
  7181. }
  7182. Blockly.Blocks['ai_video_set'] = {
  7183. init: function () {
  7184. // this.appendDummyInput()
  7185. // .appendField(Blockly.Msg.ai_video_play_video);
  7186. this.appendValueInput("path")
  7187. .setCheck(null)
  7188. .appendField(Blockly.Msg.ai_video_play_video_file_path);
  7189. // this.appendDummyInput()
  7190. // .appendField(Blockly.Msg.ai_video_play_video_2);
  7191. this.setPreviousStatement(true, null);
  7192. this.setNextStatement(true, null);
  7193. this.setColour("#4d9994");
  7194. this.setTooltip(Blockly.Msg.ai_video_play_video);
  7195. this.setHelpUrl("https://cn.maixpy.sipeed.com/maixpy/zh/api_reference/media/video.html#1-avi");
  7196. }
  7197. };
  7198. Blockly.Python['ai_video_set'] = function (block) {
  7199. // Blockly.Python.definitions_.import_Maix = "from maix import display, camera, image";
  7200. var value_path = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  7201. var code = `pathToVideo = ${value_path}
  7202. containerVideo = av.open(pathToVideo)
  7203. streamVideo = containerVideo.streams.video[0]
  7204. `
  7205. return code;
  7206. };
  7207. Blockly.Blocks['ai_video_set_stats'] = {
  7208. init: function () {
  7209. this.appendDummyInput()
  7210. .appendField(Blockly.Msg.ai_video_set_play_stats);
  7211. // this.appendDummyInput()
  7212. // .appendField(Blockly.Msg.ai_video_play_video_2);
  7213. this.setOutput(true, null)
  7214. this.setColour("#4d9994");
  7215. this.setTooltip(Blockly.Msg.ai_video_set_play_stats);
  7216. this.setHelpUrl("https://cn.maixpy.sipeed.com/maixpy/zh/api_reference/media/video.html#1-avi");
  7217. }
  7218. };
  7219. Blockly.Python['ai_video_set_stats'] = function (block) {
  7220. var code = `containerVideo.decode(video=0)`
  7221. return [code, Blockly.Python.ORDER_NONE];
  7222. };
  7223. Blockly.Blocks['ai_video_set_play'] = {
  7224. init: function () {
  7225. this.appendDummyInput().appendField(Blockly.Msg.ai_models_from)
  7226. .appendField(new Blockly.FieldVariable("i"), "varitem")
  7227. .appendField(Blockly.Msg.ai_video_set_play);
  7228. this.setPreviousStatement(true, null);
  7229. this.setNextStatement(true, null);
  7230. this.setColour("#4d9994");
  7231. this.setTooltip(Blockly.Msg.ai_video_set_play);
  7232. this.setHelpUrl("");
  7233. }
  7234. }
  7235. Blockly.Python['ai_video_set_play'] = function (block) {
  7236. var value = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  7237. let code = `if "Video" in repr(${value}):
  7238. VideoImg = image.load(bytes(${value}.to_rgb().planes[0]), (streamVideo.width, streamVideo.height))
  7239. display.show(VideoImg)
  7240. `
  7241. return code;
  7242. }
  7243. Blockly.Blocks['ai_video_stop_play'] = {
  7244. init: function () {
  7245. this.appendDummyInput()
  7246. .appendField(Blockly.Msg.ai_video_stop_video_playing);
  7247. this.setPreviousStatement(true, null);
  7248. this.setNextStatement(true, null);
  7249. this.setColour("#4d9994");
  7250. this.setTooltip(Blockly.Msg.ai_video_stop_play_TOOLTIP);
  7251. this.setHelpUrl("");
  7252. }
  7253. };
  7254. Blockly.Python['ai_video_stop_play'] = function (block) {
  7255. // TODO: Assemble Python into code variable.
  7256. var code = `break
  7257. `;
  7258. return code;
  7259. };
  7260. Blockly.Blocks['ai_video_record_init'] = {
  7261. init: function () {
  7262. this.appendDummyInput()
  7263. .appendField(new Blockly.FieldImage("blockly/media/video_record_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  7264. this.appendDummyInput()
  7265. .appendField(Blockly.Msg.ai_video_start_recording);
  7266. this.appendValueInput("save_path")
  7267. .setCheck(null)
  7268. .appendField(Blockly.Msg.ai_video_start_recording_file_path);
  7269. this.appendValueInput("w")
  7270. .setCheck(null)
  7271. .appendField(Blockly.Msg.ai_video_start_recording_width);
  7272. this.appendValueInput("h")
  7273. .setCheck(null)
  7274. .appendField(Blockly.Msg.ai_video_start_recording_height);
  7275. this.setPreviousStatement(true, null);
  7276. this.setNextStatement(true, null);
  7277. this.setColour("#4d9994");
  7278. this.setTooltip(Blockly.Msg.ai_video_record_setup_TOOLTIP);
  7279. // this.setHelpUrl("https://cn.maixpy.sipeed.com/maixpy/zh/api_reference/media/video.html#1-avi");
  7280. }
  7281. };
  7282. Blockly.Python['ai_video_record_init'] = function (block) {
  7283. Blockly.Python.definitions_['v831_import_os'] = `import os`
  7284. Blockly.Python.definitions_['v831_import_pyaudio'] = `import pyaudio`
  7285. Blockly.Python.definitions_['v831_import_av'] = `import av`
  7286. Blockly.Python.definitions_['v831_import_from_maix_display'] = `from maix import display`
  7287. Blockly.Python.definitions_['v831_import_from_maix_camera'] = `from maix import camera`
  7288. Blockly.Python.definitions_['v831_import_from_maix_image'] = `from maix import image`
  7289. var value_path = Blockly.Python.valueToCode(block, 'save_path', Blockly.Python.ORDER_ATOMIC);
  7290. var w = Blockly.Python.valueToCode(block, 'w', Blockly.Python.ORDER_ATOMIC);
  7291. var h = Blockly.Python.valueToCode(block, 'h', Blockly.Python.ORDER_ATOMIC);
  7292. var code = `path_to_video = ${value_path}
  7293. fps = 10
  7294. container = av.open(path_to_video, mode='w')
  7295. stream = container.add_stream('h264', rate=fps) # h264 or mpeg4
  7296. stream.width = ${w}
  7297. stream.height = ${h}
  7298. stream.pix_fmt = 'yuv420p'
  7299. `
  7300. return code;
  7301. };
  7302. Blockly.Blocks['ai_video_record_get'] = {
  7303. init: function () {
  7304. this.appendDummyInput()
  7305. .appendField(Blockly.Msg.ai_video_to_canvas)
  7306. .appendField(new Blockly.FieldVariable("canvas"), "varitem")
  7307. .appendField(Blockly.Msg.ai_video_record);
  7308. this.setInputsInline(false);
  7309. this.setPreviousStatement(true, null);
  7310. this.setNextStatement(true, null);
  7311. this.setColour("#4d9994");
  7312. this.setTooltip(Blockly.Msg.ai_video_get_TOOLTIP);
  7313. this.setHelpUrl("");
  7314. }
  7315. };
  7316. Blockly.Python.ai_video_record_get = function (block) {
  7317. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  7318. var _code = `frame = av.VideoFrame(${variable_name}.width, ${variable_name}.height, 'rgb24')
  7319. frame.planes[0].update(${variable_name}.tobytes())
  7320. for packet in stream.encode(frame):
  7321. container.mux(packet)
  7322. `
  7323. return _code;
  7324. }
  7325. Blockly.Blocks['ai_video_record_stop'] = {
  7326. init: function () {
  7327. this.appendDummyInput()
  7328. .appendField(Blockly.Msg.ai_video_stop_recording);
  7329. this.setPreviousStatement(true);
  7330. this.setNextStatement(true);
  7331. this.setColour("#4d9994");
  7332. this.setTooltip(Blockly.Msg.ai_video_stop_TOOLTIP);
  7333. this.setHelpUrl("");
  7334. }
  7335. };
  7336. Blockly.Python.ai_video_record_stop = function (block) {
  7337. var _code = `
  7338. #Flush stream
  7339. for packet in stream.encode():
  7340. container.mux(packet)
  7341. #Close the file
  7342. container.close()
  7343. `
  7344. return _code;
  7345. }
  7346. // 视频录制
  7347. Blockly.Blocks['ai_video_record'] = {
  7348. init: function () {
  7349. this.appendDummyInput()
  7350. .appendField(new Blockly.FieldImage("blockly/media/video_init_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  7351. this.appendDummyInput()
  7352. .appendField(Blockly.Msg.ai_video_recording_and_save);
  7353. this.appendValueInput("path")
  7354. .setCheck(null)
  7355. .appendField(Blockly.Msg.VIDEO_SET_P);
  7356. this.appendDummyInput()
  7357. .appendField(Blockly.Msg.ai_video_play_video_2);
  7358. this.setPreviousStatement(true, null);
  7359. this.setNextStatement(true, null);
  7360. this.setColour("#4d9994");
  7361. this.setTooltip(Blockly.Msg.ai_video_set_TOOLTIP);
  7362. this.setHelpUrl("https://cn.maixpy.sipeed.com/maixpy/zh/api_reference/media/video.html#1-avi");
  7363. }
  7364. };
  7365. Blockly.Python['ai_video_record'] = function (block) {
  7366. var value_path = Blockly.Python.valueToCode(block, 'path', Blockly.Python.ORDER_ATOMIC);
  7367. Blockly.Python.definitions_['v831_import_time'] = `import time`
  7368. Blockly.Python.definitions_['v831_import_pyaudio'] = `import pyaudio`
  7369. Blockly.Python.definitions_['v831_import_av'] = `import av`
  7370. Blockly.Python.definitions_['v831_import_os'] = `import os`
  7371. Blockly.Python.definitions_['v831_import_from_maix_display'] = `import display`
  7372. Blockly.Python.definitions_['v831_import_from_maix_camera'] = `import camera`
  7373. Blockly.Python.definitions_['v831_import_from_maix_image'] = `import image`
  7374. var code = `path_to_video = ${value_path};
  7375. duration, fps = 4, 10
  7376. total_frames = duration * fps
  7377. container = av.open(path_to_video, mode='w')
  7378. stream = container.add_stream('h264', rate=fps) # h264 or mpeg4
  7379. stream.width = 320
  7380. stream.height = 240
  7381. stream.pix_fmt = 'yuv420p'
  7382. for frame_i in range(total_frames):
  7383. img = image.new(size=(stream.width, stream.height), mode='RGB')
  7384. img.draw_string(0, 0, str(time.time()), 2)
  7385. frame = av.VideoFrame(img.width, img.height, 'rgb24')
  7386. frame.planes[0].update(img.tobytes())
  7387. for packet in stream.encode(frame):
  7388. container.mux(packet)
  7389. #Flush stream
  7390. for packet in stream.encode():
  7391. container.mux(packet)
  7392. #Close the file
  7393. container.close()
  7394. #Play the video
  7395. container = av.open(path_to_video)
  7396. stream = container.streams.video[0]
  7397. for frame in container.decode(video=0):
  7398. if 'Video' in repr(frame):
  7399. img = image.load(bytes(frame.to_rgb().planes[0]), (stream.width, stream.height))
  7400. display.show(img)`
  7401. return code;
  7402. };
  7403. Blockly.Blocks['ai_video_status'] = {
  7404. init: function () {
  7405. this.appendDummyInput()
  7406. .appendField(Blockly.Msg.ai_video_get_video_playing_status);
  7407. this.setOutput(true, null);
  7408. this.setColour("#4d9994");
  7409. this.setTooltip(Blockly.Msg.ai_video_status_TOOLTIP);
  7410. this.setHelpUrl("");
  7411. }
  7412. };
  7413. Blockly.Python.ai_video_status = function (block) {
  7414. var code = "_video_play.play()";
  7415. return [code, Blockly.Python.ORDER_NONE];
  7416. }
  7417. /*
  7418. _ ___ __ __ _ _
  7419. / \ |_ _| | \/ | ___ __| | ___| |___
  7420. / _ \ | | | |\/| |/ _ \ / _` |/ _ \ / __|
  7421. / ___ \ | | | | | | (_) | (_| | __/ \__ \
  7422. /_/ \_\___| |_| |_|\___/ \__,_|\___|_|___/
  7423. */
  7424. Blockly.Blocks['ai_model_allinit'] = {
  7425. init: function () {
  7426. // let types = getLocalStorage("isQiehuan");
  7427. // if (!types) {
  7428. // saveLocalStorage("isQiehuan", 0);
  7429. // }
  7430. this.appendDummyInput()
  7431. .appendField(Blockly.Msg.ai_models_init_title)
  7432. .appendField(new Blockly.FieldDropdown([
  7433. [Blockly.Msg.ai_models_init_hand_written_model, "Single Digit Recognition"],
  7434. [Blockly.Msg.ai_models_init_object_model, "Common Object Recognition"],
  7435. [Blockly.Msg.ai_models_init_face_model, "Human Face Recognition"]
  7436. ]), "DEGREE");
  7437. this.appendDummyInput().appendField(Blockly.Msg.ai_model_allinit_language).appendField(new Blockly.FieldDropdown([
  7438. [Blockly.Msg.ai_speech_recognition_setup_set_english, "English"],
  7439. [Blockly.Msg.ai_speech_recognition_setup_set_simplified_chinese, "Simplified Chinese"],
  7440. [Blockly.Msg.ai_speech_recognition_setup_set_traditional_chinese, "Traditional Chinese"]
  7441. ]), "interface")
  7442. // this.appendDummyInput()
  7443. // .appendField(Blockly.Msg.ai_camera_camera_fit_set1)
  7444. // .appendField(new Blockly.FieldVariable("img_mnist"), "camera_varitem");
  7445. // this.appendDummyInput()
  7446. // .appendField(Blockly.Msg.ai_models_init_source)
  7447. // .appendField(new Blockly.FieldVariable("img_mnist"), "varitem");
  7448. this.setPreviousStatement(true, null);
  7449. this.setNextStatement(true, null);
  7450. this.setColour("#ee783a");
  7451. var thisBlock = this;
  7452. this.setTooltip(function () {
  7453. var mode = thisBlock.getFieldValue('DEGREE');
  7454. var TOOLTIPS = {
  7455. 'Single Digit Recognition': Blockly.Msg.ai_model_allinit_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_hand_written_model),
  7456. 'Common Object Recognition': Blockly.Msg.ai_model_allinit_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_object_model),
  7457. 'Human Face Recognition': Blockly.Msg.ai_model_allinit_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_face_model)
  7458. };
  7459. return TOOLTIPS[mode];
  7460. });
  7461. this.setHelpUrl('');
  7462. }
  7463. };
  7464. Blockly.Python.ai_model_allinit = function (block) {
  7465. Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
  7466. var degee = block.getFieldValue('DEGREE');
  7467. var _code = "";
  7468. if (degee == "Single Digit Recognition") {
  7469. Blockly.Python.addVariable('task_mnist', "task_mnist = kpu.load(\"/sd/preset/models/mnist.kmodel\")\n", true);
  7470. } else if (degee == "Common Object Recognition") {
  7471. var language = localStorage.getItem("handPyLanguage");
  7472. Blockly.Python.addVariable('task_objectrecognition', 'task_objectrecognition = None', true);
  7473. if (language == "en") {
  7474. _code = '' +
  7475. 'classes_objectrecognition = [\'aeroplane\', \'bicycle\', \'bird\', \'boat\', \'bottle\', \'bus\', \'car\', \'cat\', \'chair\', \'cow\', \'diningtable\', \'dog\', \'horse\', \'motorbike\', \'person\', \'pottedplant\', \'sheep\', \'sofa\', \'train\', \'tvmonitor\']\n' +
  7476. 'task_objectrecognition = kpu.load("/sd/preset/models/tinyyolo_v2_20class.kmodel")\n' +
  7477. 'object_anchor = (1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52)\n' +
  7478. 'a = kpu.init_yolo2(task_objectrecognition, 0.5, 0.3, 5, object_anchor)\n' +
  7479. '';
  7480. }
  7481. else if (language == "zh-hans") {
  7482. _code = '' +
  7483. 'classes_objectrecognition = [\'飞机\', \'自行车\', \'鸟\', \'船\', \'瓶子\', \'公共汽车\', \'汽车\', \'猫\', \'椅子\', \'牛\', \'餐桌\', \'狗\', \'马\', \'摩托车\', \'人\', \'盆栽\', \'羊\', \'沙发\', \'火车\', \'电视监视器\']\n' +
  7484. 'task_objectrecognition = kpu.load("/sd/preset/models/tinyyolo_v2_20class.kmodel")\n' +
  7485. 'object_anchor = (1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52)\n' +
  7486. 'a = kpu.init_yolo2(task_objectrecognition, 0.5, 0.3, 5, object_anchor)\n' +
  7487. '';
  7488. }
  7489. else {
  7490. _code = '' +
  7491. 'classes_objectrecognition = [\'飛機\', \'自行車\', \'鳥\', \'船\', \'瓶子\', \'公共汽車\', \'汽車\', \'貓\', \'椅子\', \'牛\', \'餐桌\', \'狗\', \'馬\', \'摩托車\', \'人\', \'盆栽\', \'羊\', \'沙發\', \'火車\', \'電視監視器\']\n' +
  7492. 'task_objectrecognition = kpu.load("/sd/preset/models/tinyyolo_v2_20class.kmodel")\n' +
  7493. 'object_anchor = (1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52)\n' +
  7494. 'a = kpu.init_yolo2(task_objectrecognition, 0.5, 0.3, 5, object_anchor)\n' +
  7495. '';
  7496. }
  7497. } else {
  7498. Blockly.Python.addVariable('task_facerecognition', 'task_facerecognition = None', true);
  7499. _code = "" +
  7500. "task_facerecognition = kpu.load(\"/sd/preset/models/preset/face-recognition.kmodel\")\n" +
  7501. "anchor_face = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025)\n" +
  7502. "a = kpu.init_yolo2(task_facerecognition, 0.5, 0.3, 5, anchor_face)\n" +
  7503. "\n";
  7504. }
  7505. return _code;
  7506. }
  7507. // 边缘检测
  7508. Blockly.Blocks['ai_model_edge_init'] = {
  7509. init: function () {
  7510. // let types = getLocalStorage("isQiehuan");
  7511. // if (!types) {
  7512. // saveLocalStorage("isQiehuan", 0);
  7513. // }
  7514. this.appendDummyInput()
  7515. .appendField(Blockly.Msg.ai_models_init_title)
  7516. .appendField(Blockly.Msg.ai_models_edge_detection_model);
  7517. this.setPreviousStatement(true, null);
  7518. this.setNextStatement(true, null);
  7519. this.setColour("#ee783a");
  7520. this.setTooltip(Blockly.Msg.ai_models_init_title + Blockly.Msg.ai_models_edge_detection_model);
  7521. this.setHelpUrl('');
  7522. }
  7523. };
  7524. Blockly.Python.ai_model_edge_init = function (block) {
  7525. Blockly.Python.definitions_['v831_import_numpy_as_np'] = `import numpy as np`
  7526. Blockly.Python.definitions_.import_KPU = `
  7527. class Edge:
  7528. model = {
  7529. "param": "/root/preset/model/sobel_int8.param",
  7530. "bin": "/root/preset/model/sobel_int8.bin"
  7531. }
  7532. input_size = (224, 224, 3)
  7533. output_size = (222, 222, 3)
  7534. options = {
  7535. "model_type": "awnn",
  7536. "inputs": {
  7537. "input0": input_size
  7538. },
  7539. "outputs": {
  7540. "output0": output_size
  7541. },
  7542. "mean": [127.5, 127.5, 127.5],
  7543. "norm": [0.0078125, 0.0078125, 0.0078125],
  7544. }
  7545. def __init__(self):
  7546. from maix import nn
  7547. print("-- load model:", self.model)
  7548. self.model = nn.load(self.model, opt=self.options)
  7549. print("-- load ok")
  7550. def __del__(self):
  7551. del self.model
  7552. m = Edge()`;
  7553. // var degee = block.getFieldValue('DEGREE');
  7554. var _code = "";
  7555. return _code;
  7556. }
  7557. // 加载边缘检测模型
  7558. Blockly.Blocks['ai_model_edge_loading'] = {
  7559. init: function () {
  7560. // let types = getLocalStorage("isQiehuan");
  7561. // if (!types) {
  7562. // saveLocalStorage("isQiehuan", 0);
  7563. // }
  7564. this.appendDummyInput()
  7565. .appendField(Blockly.Msg.ai_models_load_edge_detection_model);
  7566. // this.appendDummyInput()
  7567. // .appendField(Blockly.Msg.ai_models_init_source)
  7568. // .appendField(new Blockly.FieldVariable("img_edgedetection"), "varitem");
  7569. this.setPreviousStatement(true, null);
  7570. this.setNextStatement(true, null);
  7571. this.setColour("#ee783a");
  7572. // var thisBlock = this;
  7573. this.setTooltip(Blockly.Msg.ai_models_load_edge_detection_model);
  7574. this.setHelpUrl('');
  7575. }
  7576. };
  7577. Blockly.Python.ai_model_edge_loading = function (block) {
  7578. // var degee = block.getFieldValue('varitem');
  7579. Blockly.Python.definitions_['v831_import_numpy_as_np'] = `import numpy as np`
  7580. Blockly.Python.definitions_.import_KPU = `
  7581. class Edge:
  7582. model = {
  7583. "param": "/root/preset/model/sobel_int8.param",
  7584. "bin": "/root/preset/model/sobel_int8.bin"
  7585. }
  7586. input_size = (224, 224, 3)
  7587. output_size = (222, 222, 3)
  7588. options = {
  7589. "model_type": "awnn",
  7590. "inputs": {
  7591. "input0": input_size
  7592. },
  7593. "outputs": {
  7594. "output0": output_size
  7595. },
  7596. "mean": [127.5, 127.5, 127.5],
  7597. "norm": [0.0078125, 0.0078125, 0.0078125],
  7598. }
  7599. def __init__(self):
  7600. from maix import nn
  7601. print("-- load model:", self.model)
  7602. self.model = nn.load(self.model, opt=self.options)
  7603. print("-- load ok")
  7604. def __del__(self):
  7605. del self.model
  7606. m = Edge()`;
  7607. // ${degee} = camera.capture().resize(224,224)
  7608. var _code = `canvas = canvas.resize(224, 224, padding = 0)
  7609. out = m.model.forward(canvas, quantize=True, layout="hwc")
  7610. out = out.astype(np.float32).reshape(m.output_size)
  7611. out = (np.ndarray.__abs__(out) * 255 / out.max()).astype(np.uint8)
  7612. data = out.tobytes()
  7613. canvas = canvas.load(data,(222, 222), mode="RGB")
  7614. `;
  7615. return _code;
  7616. }
  7617. // 获取边缘测试模型结果
  7618. Blockly.Blocks['ai_model_edge_result'] = {
  7619. init: function () {
  7620. this.appendDummyInput()
  7621. .appendField(Blockly.Msg.ai_models_get_edge_detection_result);
  7622. this.setOutput(true, null);
  7623. // this.setNextStatement(true, null);
  7624. this.setColour("#ee783a");
  7625. // var thisBlock = this;
  7626. this.setTooltip(Blockly.Msg.ai_models_get_edge_detection_result);
  7627. this.setHelpUrl('');
  7628. }
  7629. };
  7630. Blockly.Python.ai_model_edge_result = function (block) {
  7631. var degee = block.getFieldValue('varitem');
  7632. var _code = "edgeModel";
  7633. return [_code, Blockly.Python.ORDER_ATOMIC];
  7634. }
  7635. Blockly.Blocks['ai_model_allload'] = {
  7636. init: function () {
  7637. this.appendDummyInput()
  7638. .appendField(Blockly.Msg.ai_models_load_title)
  7639. .appendField(new Blockly.FieldDropdown([
  7640. [Blockly.Msg.ai_models_init_hand_written_model, "Single Digit Recognition"],
  7641. [Blockly.Msg.ai_models_init_object_model, "Common Object Recognition"],
  7642. [Blockly.Msg.ai_models_init_face_model, "Human Face Recognition"]
  7643. ]), "DEGREE");
  7644. // this.appendDummyInput()
  7645. // .appendField(Blockly.Msg.ai_camera_camera_fit_set1)
  7646. // .appendField(new Blockly.FieldVariable("img_mnist"), "camera_varitem");
  7647. this.appendDummyInput()
  7648. .appendField(Blockly.Msg.ai_models_init_source)
  7649. .appendField(new Blockly.FieldVariable("img_mnist"), "varitem");
  7650. this.setPreviousStatement(true, null);
  7651. this.setNextStatement(true, null);
  7652. this.setColour("#ee783a");
  7653. var thisBlock = this;
  7654. this.setTooltip(function () {
  7655. var mode = thisBlock.getFieldValue('DEGREE');
  7656. var TOOLTIPS = {
  7657. 'Single Digit Recognition': Blockly.Msg.ai_model_allload_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_hand_written_model),
  7658. 'Common Object Recognition': Blockly.Msg.ai_model_allload_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_object_model),
  7659. 'Human Face Recognition': Blockly.Msg.ai_model_allload_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_face_model)
  7660. };
  7661. return TOOLTIPS[mode];
  7662. });
  7663. this.setHelpUrl('');
  7664. }
  7665. };
  7666. Blockly.Python.ai_model_allload = function (block) {
  7667. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  7668. // var camera_variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('camera_varitem'), Blockly.Variables.NAME_TYPE);
  7669. Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
  7670. var degee = block.getFieldValue('DEGREE');
  7671. var _code = ""
  7672. if (degee == "Single Digit Recognition") {
  7673. // Blockly.Python.addVariable('img_mnist_resized', '', true);
  7674. _code += "img_mnist1=" + variable_name + ".to_grayscale(1)\n";
  7675. _code += "img_mnist_resized=img_mnist1.resize(28,28)\n";
  7676. _code += "a=img_mnist_resized.invert()\n";
  7677. _code += "a=img_mnist_resized.strech_char(1)\n";
  7678. _code += "a=img_mnist_resized.pix_to_ai()\n";
  7679. _code += "fmap_mnist=kpu.forward(task_mnist, img_mnist_resized)\n";
  7680. _code += "plist_mnist=fmap_mnist[:]\n";
  7681. _code += "pmax_mnist=max(plist_mnist)\n";
  7682. _code += "max_index_mnist=plist_mnist.index(pmax_mnist)\n";
  7683. } else if (degee == "Common Object Recognition") {
  7684. // Blockly.Python.addVariable('code_objectrecognition', '', true);
  7685. _code += "code_objectrecognition = kpu.run_yolo2(task_objectrecognition, " + variable_name + ")\n";
  7686. _code += variable_name + ".ai_to_pix()\n";
  7687. } else {
  7688. // Blockly.Python.addVariable('code_facerecognition', '', true);
  7689. _code += "code_facerecognition = kpu.run_yolo2(task_facerecognition, " + variable_name + ")\n";
  7690. _code += variable_name + ".ai_to_pix()\n";
  7691. }
  7692. return _code;
  7693. }
  7694. Blockly.Blocks['ai_model_index_init'] = {
  7695. init: function () {
  7696. this.appendValueInput("class_input")
  7697. .setCheck(null)
  7698. .appendField(Blockly.Msg.ai_models_init_title)
  7699. // .appendField("DEGREE");
  7700. this.setPreviousStatement(true, null);
  7701. this.setNextStatement(true, null);
  7702. this.setColour("#ee783a");
  7703. this.setTooltip(Blockly.Msg.ai_models_init_title);
  7704. this.setHelpUrl('');
  7705. }
  7706. };
  7707. Blockly.Python.ai_model_index_init = function (block) {
  7708. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  7709. Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
  7710. console.log('value_class_input', value_class_input)
  7711. var _code = value_class_input;
  7712. // Blockly.Python.addVariable('task_mnist', "task_mnist = kpu.load(\"/sd/preset/models/mnist.kmodel\")\n", true);
  7713. return _code;
  7714. }
  7715. // 手写数字识别
  7716. Blockly.Blocks['ai_model_look_written_model'] = {
  7717. init: function () {
  7718. // this.appendDummyInput()
  7719. // .appendField(Blockly.Msg.ai_models_init_title)
  7720. // .appendField(new Blockly.FieldDropdown([
  7721. // [Blockly.Msg.ai_models_init_hand_written_model, "Human Face Recognition"]
  7722. // ]), "DEGREE");
  7723. this.appendDummyInput()
  7724. .appendField(Blockly.Msg.ai_models_init_hand_written_model);
  7725. this.setOutput(true, null);
  7726. this.setColour("#ee783a");
  7727. this.setTooltip(Blockly.Msg.ai_models_init_title + Blockly.Msg.ai_models_init_hand_written_model);
  7728. this.setHelpUrl("");
  7729. }
  7730. };
  7731. Blockly.Python.ai_model_look_written_model = function (block) {
  7732. // Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
  7733. var _code = "task_mnist = kpu.load(\"/sd/preset/models/mnist.kmodel\")\n";
  7734. return [_code, Blockly.Python.ORDER_ATOMIC];
  7735. }
  7736. Blockly.Blocks['ai_model_look_written_model_old'] = {
  7737. init: function () {
  7738. this.appendDummyInput()
  7739. .appendField(Blockly.Msg.ai_models_init_title)
  7740. .appendField(new Blockly.FieldDropdown([
  7741. [Blockly.Msg.ai_models_init_hand_written_model, "Human Face Recognition"]
  7742. ]), "DEGREE");
  7743. // this.appendDummyInput()
  7744. // .appendField(Blockly.Msg.ai_models_init_hand_written_model);
  7745. this.setPreviousStatement(true, null);
  7746. this.setNextStatement(true, null);
  7747. this.setColour("#ee783a");
  7748. this.setTooltip(Blockly.Msg.ai_models_init_title + Blockly.Msg.ai_models_init_hand_written_model);
  7749. this.setHelpUrl("");
  7750. }
  7751. };
  7752. Blockly.Python.ai_model_look_written_model_old = function (block) {
  7753. Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
  7754. var _code = "task_mnist = kpu.load(\"/sd/preset/models/mnist.kmodel\")\n";
  7755. return _code;
  7756. }
  7757. Blockly.Blocks['ai_model_index_load'] = {
  7758. init: function () {
  7759. this.appendDummyInput()
  7760. .appendField(Blockly.Msg.ai_models_load_title)
  7761. .appendField(new Blockly.FieldDropdown([
  7762. [Blockly.Msg.ai_models_init_hand_written_model, "Single Digit Recognition"]
  7763. ]), "DEGREE");
  7764. this.appendDummyInput()
  7765. .appendField(Blockly.Msg.ai_models_init_source)
  7766. .appendField(new Blockly.FieldVariable("img_mnist"), "varitem");
  7767. this.setPreviousStatement(true, null);
  7768. this.setNextStatement(true, null);
  7769. this.setColour("#ee783a");
  7770. var thisBlock = this;
  7771. this.setTooltip(function () {
  7772. var mode = thisBlock.getFieldValue('DEGREE');
  7773. var TOOLTIPS = {
  7774. 'Single Digit Recognition': Blockly.Msg.ai_model_allload_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_hand_written_model)
  7775. };
  7776. return TOOLTIPS[mode];
  7777. });
  7778. this.setHelpUrl('');
  7779. }
  7780. };
  7781. Blockly.Python.ai_model_index_load = function (block) {
  7782. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  7783. // var camera_variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('camera_varitem'), Blockly.Variables.NAME_TYPE);
  7784. Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
  7785. var degee = block.getFieldValue('DEGREE');
  7786. var _code = ""
  7787. if (degee == "Single Digit Recognition") {
  7788. // Blockly.Python.addVariable('img_mnist_resized', '', true);
  7789. _code += "img_mnist1=" + variable_name + ".to_grayscale(1)\n";
  7790. _code += "img_mnist_resized=img_mnist1.resize(28,28)\n";
  7791. _code += "a=img_mnist_resized.invert()\n";
  7792. _code += "a=img_mnist_resized.strech_char(1)\n";
  7793. _code += "a=img_mnist_resized.pix_to_ai()\n";
  7794. _code += "fmap_mnist=kpu.forward(task_mnist, img_mnist_resized)\n";
  7795. _code += "plist_mnist=fmap_mnist[:]\n";
  7796. _code += "pmax_mnist=max(plist_mnist)\n";
  7797. _code += "max_index_mnist=plist_mnist.index(pmax_mnist)\n";
  7798. }
  7799. return _code;
  7800. }
  7801. Blockly.Blocks['ai_model_index_mnist'] = {
  7802. init: function () {
  7803. this.appendDummyInput()
  7804. .appendField(new Blockly.FieldImage("blockly/media/digit_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  7805. this.appendDummyInput()
  7806. .appendField(Blockly.Msg.ai_models_hand_written_model_result);
  7807. this.appendDummyInput()
  7808. .appendField(new Blockly.FieldDropdown([
  7809. [Blockly.Msg.ai_models_hand_written_model_number, "c"],
  7810. [Blockly.Msg.ai_models_hand_written_model_confidence, "confidence"]
  7811. ]), "value");
  7812. this.setOutput(true, null);
  7813. this.setColour("#ee783a");
  7814. this.setTooltip(Blockly.Msg.ai_model_index_mnist_TOOLTIP);
  7815. this.setHelpUrl('');
  7816. }
  7817. };
  7818. Blockly.Python.ai_model_index_mnist = function (block) {
  7819. var value = block.getFieldValue('value');
  7820. var _code = "max_index_mnist";
  7821. if (value == "confidence") {
  7822. _code = "int(pmax_mnist*100)";
  7823. }
  7824. return [_code, Blockly.Python.ORDER_ATOMIC];
  7825. }
  7826. Blockly.Blocks['ai_model_digital_init'] = {
  7827. init: function () {
  7828. this.appendDummyInput()
  7829. .appendField(Blockly.Msg.ai_models_init_title)
  7830. .appendField(Blockly.Msg.ai_models_init_hand_written_model);
  7831. // this.appendDummyInput()
  7832. // .appendField(Blockly.Msg.ai_models_init_hand_written_model);
  7833. this.setPreviousStatement(true, null);
  7834. this.setNextStatement(true, null);
  7835. this.setColour("#ee783a");
  7836. this.setTooltip(Blockly.Msg.ai_models_init_title + Blockly.Msg.ai_models_init_hand_written_model);
  7837. this.setHelpUrl("");
  7838. }
  7839. };
  7840. Blockly.Python.ai_model_digital_init = function (block) {
  7841. // Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
  7842. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  7843. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  7844. var _code = `
  7845. class Number_recognition:
  7846. mdsc_path = "/root/preset/model/Number.mud"
  7847. labels = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
  7848. anchors = [1.0, 5.0, 1.35, 5.42, 0.49, 2.55, 0.86, 3.75, 0.65, 4.38]
  7849. def __init__(self):
  7850. self.model = nn.load(self.mdsc_path)
  7851. self.decoder = decoder.Yolo2(len(self.labels) , self.anchors , net_in_size = (224, 224) ,net_out_size = (7,7))
  7852. def __del__(self):
  7853. del self.model
  7854. del self.decoder
  7855. def cal_fps(self ,start , end):
  7856. one_second = 1
  7857. one_flash = end - start
  7858. fps = one_second / one_flash
  7859. return fps
  7860. number_recognition = Number_recognition()
  7861. `;
  7862. return _code;
  7863. }
  7864. Blockly.Blocks['ai_model_digital_init_loding'] = {
  7865. init: function () {
  7866. this.appendDummyInput()
  7867. .appendField(Blockly.Msg.ai_models_init_hand_written_model);
  7868. // this.appendDummyInput()
  7869. // .appendField(Blockly.Msg.ai_models_init_source)
  7870. // .appendField(new Blockly.FieldVariable("img_mnist"), "varitem");
  7871. this.setPreviousStatement(true, null);
  7872. this.setNextStatement(true, null);
  7873. this.setColour("#ee783a");
  7874. var thisBlock = this;
  7875. this.setTooltip(function () {
  7876. var mode = thisBlock.getFieldValue('DEGREE');
  7877. var TOOLTIPS = {
  7878. 'Single Digit Recognition': Blockly.Msg.ai_model_allload_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_hand_written_model)
  7879. };
  7880. return TOOLTIPS[mode];
  7881. });
  7882. this.setHelpUrl('');
  7883. }
  7884. };
  7885. Blockly.Python.ai_model_digital_init_loding = function (block) {
  7886. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  7887. // var camera_variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('camera_varitem'), Blockly.Variables.NAME_TYPE);
  7888. Blockly.Python.definitions_.import_KPU = ``;
  7889. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  7890. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  7891. Blockly.Python.definitions_['v831_import_from_Number_recognition'] = `
  7892. class Number_recognition:
  7893. mdsc_path = "/root/preset/model/Number.mud"
  7894. labels = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
  7895. anchors = [1.0, 5.0, 1.35, 5.42, 0.49, 2.55, 0.86, 3.75, 0.65, 4.38]
  7896. def __init__(self):
  7897. self.model = nn.load(self.mdsc_path)
  7898. self.decoder = decoder.Yolo2(len(self.labels) , self.anchors , net_in_size = (224, 224) ,net_out_size = (7,7))
  7899. self.NumberRecognition = []
  7900. def __del__(self):
  7901. del self.model
  7902. del self.decoder
  7903. def cal_fps(self ,start , end):
  7904. one_second = 1
  7905. one_flash = end - start
  7906. fps = one_second / one_flash
  7907. return fps
  7908. def digitalShowCanvas(self,canvas):
  7909. out = number_recognition.model.forward(canvas, quantize=1, layout = "hwc")
  7910. BOXES, PROBS = number_recognition.decoder.run(out, nms=0.5, threshold=0.3, img_size=(224,224))
  7911. if len(BOXES):
  7912. for BOXESI, BOXOBJ in enumerate(BOXES):
  7913. 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)
  7914. canvas.draw_rectangle(BOXOBJ[0],BOXOBJ[1], BOXOBJ[0]+BOXOBJ[2],BOXOBJ[1]+ BOXOBJ[3], color=(255,0,0), thickness=1)
  7915. BOXES[BOXESI].append(PROBS[BOXESI])
  7916. #self.NumberRecognition = BOXES
  7917. else:
  7918. BOXES = []
  7919. return BOXES
  7920. number_recognition = Number_recognition()
  7921. `;
  7922. var _code = `canvas = canvas.crop(48,8,224,224)
  7923. BOXES = number_recognition.digitalShowCanvas(canvas)
  7924. `
  7925. return _code;
  7926. }
  7927. Blockly.Blocks['ai_model_digital_new'] = {
  7928. init: function () {
  7929. this.appendDummyInput()
  7930. .appendField(Blockly.Msg.ai_model_digital_new_text)
  7931. this.appendStatementInput("input")
  7932. .setCheck(null)
  7933. .appendField(Blockly.Msg.basic_motion_statement_exec);
  7934. this.updateShape_();
  7935. this.setMutator(new Blockly.Mutator(['ai_model_false_create_with_item']));
  7936. this.setColour("#ee783a");
  7937. this.setPreviousStatement(true, null);
  7938. this.setNextStatement(true, null);
  7939. this.setTooltip("");
  7940. this.setHelpUrl("");
  7941. this.itemCount_ = 0;
  7942. },
  7943. mutationToDom: function () {
  7944. if (!this.itemCount_)
  7945. return null;
  7946. var container = document.createElement('mutation');
  7947. this.itemCount_ && container.setAttribute('aielse', 1);
  7948. return container;
  7949. },
  7950. domToMutation: function (a) {
  7951. this.itemCount_ = parseInt(a.getAttribute('aielse'), 10);
  7952. this.updateShape_();
  7953. },
  7954. decompose: function (a) {
  7955. var b = a.newBlock('ai_model_false_create_with_container');
  7956. b.initSvg();
  7957. var c = b.nextConnection
  7958. this.itemCount_ && (a = a.newBlock("ai_model_false_create_with_item"),
  7959. a.initSvg(),
  7960. c.connect(a.previousConnection));
  7961. console.log("this.elseCount_", a)
  7962. return b
  7963. },
  7964. compose: function (a) {
  7965. var b = a.nextConnection.targetBlock();
  7966. this.itemCount_ = 0;
  7967. var e = null;
  7968. if (b && b.type === "ai_model_false_create_with_item") {
  7969. this.itemCount_++;
  7970. e = b.statementConnection_;
  7971. b = b.nextConnection && b.nextConnection.targetBlock()
  7972. }
  7973. this.updateShape_()
  7974. Blockly.Mutator.reconnect(e, this, "AIELSE")
  7975. },
  7976. saveConnections: function (a) {
  7977. a = a.nextConnection.targetBlock();
  7978. if (a && a.type === "ai_model_false_create_with_item") {
  7979. e = this.getInput("AIELSE");
  7980. a.statementConnection_ = e && e.connection.targetConnection;
  7981. a = a.nextConnection && a.nextConnection.targetBlock()
  7982. }
  7983. },
  7984. updateShape_: function () {
  7985. this.getInput("AIELSE") && this.removeInput("AIELSE");
  7986. console.log("this.itemCount_", this.itemCount_)
  7987. this.itemCount_ && this.appendStatementInput("AIELSE").appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSE)
  7988. },
  7989. };
  7990. Blockly.Blocks['ai_model_false_create_with_container'] = {
  7991. init: function () {
  7992. this.setColour("#ee783a");
  7993. this.appendDummyInput()
  7994. .appendField(Blockly.Msg.CONTROLS_REPEAT_INPUT_DO);
  7995. this.setNextStatement(true)
  7996. this.setTooltip('');
  7997. this.contextMenu = false;
  7998. }
  7999. };
  8000. Blockly.Blocks['ai_model_false_create_with_item'] = {
  8001. init: function () {
  8002. this.setColour("#ee783a");
  8003. this.appendDummyInput().appendField(Blockly.Msg.CONTROLS_IF_ELSE_TITLE_ELSE);
  8004. this.setPreviousStatement(!0);
  8005. this.setTooltip(Blockly.Msg.CONTROLS_IF_ELSE_TOOLTIP);
  8006. this.contextMenu = !1
  8007. }
  8008. };
  8009. Blockly.Python['ai_model_digital_new'] = function (block) {
  8010. var statements_input = Blockly.Python.statementToCode(block, 'input');
  8011. var statements_input1 = Blockly.Python.statementToCode(block, 'AIELSE');
  8012. let allBlocks = block.workspace.getAllBlocks();
  8013. let global = ""
  8014. try {
  8015. global = allBlocks[0].workspace.variableList.toString()
  8016. }
  8017. catch (e) {
  8018. console.log(e)
  8019. }
  8020. console.log(statements_input1)
  8021. Blockly.Python.addFunction("digitalStatements", `def digitalStatements(BOXESI):
  8022. ${global != "" ? `global ${global}` : "pass"}
  8023. ${statements_input}
  8024. `)
  8025. var code = `if (len(BOXES)):
  8026. for BOXESI in BOXES:
  8027. digitalStatements(BOXESI)
  8028. else:
  8029. ${statements_input1 != "" ? statements_input1 : " pass"}
  8030. `
  8031. return code;
  8032. };
  8033. Blockly.Blocks['ai_model_digital_result'] = {
  8034. init: function () {
  8035. this.appendDummyInput()
  8036. .appendField(new Blockly.FieldImage("blockly/media/digit_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  8037. this.appendDummyInput()
  8038. // .appendField(Blockly.Msg.ai_models_from)
  8039. // .appendField(new Blockly.FieldVariable("i"), "varitem")
  8040. .appendField(Blockly.Msg.ai_models_hand_written_model_result);
  8041. this.appendDummyInput()
  8042. .appendField(new Blockly.FieldDropdown([
  8043. [Blockly.Msg.ai_models_hand_written_model_number, "c"],
  8044. [Blockly.Msg.ai_models_hand_written_model_confidence, "confidence"],
  8045. [Blockly.Msg.ai_models_object_model_x, "X"],
  8046. [Blockly.Msg.ai_models_object_model_y, "Y"],
  8047. // [Blockly.Msg.ai_models_object_model_w, "W"],
  8048. // [Blockly.Msg.ai_models_object_model_h, "H"],
  8049. [Blockly.Msg.ai_models_face_model_ws, "2"],
  8050. [Blockly.Msg.ai_models_face_model_hs, "3"],
  8051. [Blockly.Msg.ai_models_object_model_x_center, "CX"],
  8052. [Blockly.Msg.ai_models_object_model_y_center, "CY"]
  8053. ]), "value");
  8054. this.setOutput(true, null);
  8055. this.setColour("#ee783a");
  8056. this.setTooltip(Blockly.Msg.ai_models_hand_written_model_result);
  8057. this.setHelpUrl('');
  8058. }
  8059. };
  8060. Blockly.Python.ai_model_digital_result = function (block) {
  8061. var value = "BOXESI"
  8062. var type = block.getFieldValue('value');
  8063. // var _code = `"{}:{:.2f}%".format(number_recognition.labels[probs[${value}][0]], probs[${value}][1][probs[${value}][0]]*100)`;
  8064. if (type == "confidence") {
  8065. _code = `${value}[4][1][${value}[4][0]]*100`;
  8066. }
  8067. else if (type == "X") {
  8068. _code = `${value}[0]`;
  8069. }
  8070. else if (type == "Y") {
  8071. _code = `${value}[1]`;
  8072. }
  8073. else if (type == "W") {
  8074. _code = `int(${value}[0] + ${value}[2])`;
  8075. }
  8076. else if (type == "H") {
  8077. _code = `int(${value}[1] + ${value}[3])`;
  8078. }
  8079. else if (type == "c") {
  8080. _code = `number_recognition.labels[${value}[4][0]]`;
  8081. } else if (type == "CX") {
  8082. _code = `int((${value}[0] +${value}[0] + ${value}[2])/2)`;
  8083. } else if (type == "CY") {
  8084. _code = `int((${value}[1] + ${value}[1] + ${value}[3])/2)`;
  8085. }
  8086. else {
  8087. _code = `${value}[${type}]`;
  8088. }
  8089. return [_code, Blockly.Python.ORDER_ATOMIC];
  8090. }
  8091. Blockly.Blocks['ai_model_digital'] = {
  8092. init: function () {
  8093. this.appendDummyInput()
  8094. .appendField(Blockly.Msg.ai_model_digital);
  8095. // this.appendDummyInput()
  8096. // .appendField(new Blockly.FieldDropdown([
  8097. // [Blockly.Msg.ai_models_hand_written_model_number, "digitalResult"],
  8098. // [Blockly.Msg.ai_models_hand_written_model_confidence, "digitalConfidence"],
  8099. // ["检测框", "digitalCheckout"]
  8100. // ]), "digital");
  8101. this.setInputsInline(false);
  8102. this.setPreviousStatement(true, null);
  8103. this.setNextStatement(true, null);
  8104. this.setColour("#ee783a");
  8105. this.setTooltip();
  8106. this.setHelpUrl("");
  8107. }
  8108. };
  8109. Blockly.Python.ai_model_digital = function (block) {
  8110. Blockly.Python.addVariable("getDigitalResult", `getDigitalResult = []`)
  8111. Blockly.Python.addFunction("digitalShowCanvas", `def digitalShowCanvas():
  8112. global BOXES,getDigitalResult
  8113. getDigitalResult = []
  8114. if len(BOXES):
  8115. for BOXESI, BOXOBJ in enumerate(BOXES):
  8116. 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)
  8117. canvas.draw_rectangle(BOXOBJ[0],BOXOBJ[1], BOXOBJ[0]+BOXOBJ[2],BOXOBJ[1]+ BOXOBJ[3], color=(255,0,0), thickness=1)
  8118. getDigitalResult.append({"digitalResult":number_recognition.labels[PROBS[BOXESI][0]],"digitalConfidence":round(PROBS[BOXESI][1][PROBS[BOXESI][0]]*100, 2)})
  8119. `)
  8120. var _code = "digitalShowCanvas()\n";
  8121. return _code;
  8122. }
  8123. Blockly.Blocks['ai_model_digital_attribute'] = {
  8124. init: function () {
  8125. this.appendDummyInput()
  8126. .appendField(Blockly.Msg.ai_model_digital_attribute);
  8127. this.setOutput(false, null);
  8128. this.setNextStatement(true, null);
  8129. this.setPreviousStatement(true, null);
  8130. this.setColour("#ee783a");
  8131. this.setTooltip(Blockly.Msg.ai_model_digital_attribute);
  8132. this.setHelpUrl("");
  8133. }
  8134. };
  8135. Blockly.Python['ai_model_digital_attribute'] = function (block) {
  8136. Blockly.Python.addFunction("", `def ai_model_digital_attribute():`)
  8137. var code = 'len(BOXES)';
  8138. return code;
  8139. };
  8140. Blockly.Blocks['ai_model_object_init'] = {
  8141. init: function () {
  8142. this.appendDummyInput()
  8143. .appendField(Blockly.Msg.ai_models_init_title)
  8144. .appendField(Blockly.Msg.ai_models_init_object_model);
  8145. // this.appendDummyInput().appendField(Blockly.Msg.ai_model_allinit_language).appendField(new Blockly.FieldDropdown([
  8146. // [Blockly.Msg.ai_speech_recognition_setup_set_english, "English"],
  8147. // [Blockly.Msg.ai_speech_recognition_setup_set_simplified_chinese, "Simplified Chinese"],
  8148. // [Blockly.Msg.ai_speech_recognition_setup_set_traditional_chinese, "Traditional Chinese"]
  8149. // ]), "interface")
  8150. // this.appendValueInput("class_input")
  8151. // .setCheck(null)
  8152. // .appendField(Blockly.Msg.ai_models_customized_init_setup_name);
  8153. this.setPreviousStatement(true, null);
  8154. this.setNextStatement(true, null);
  8155. this.setColour("#ee783a");
  8156. var thisBlock = this;
  8157. this.setTooltip(function () {
  8158. var mode = thisBlock.getFieldValue('DEGREE');
  8159. var TOOLTIPS = {
  8160. 'Common Object Recognition': Blockly.Msg.ai_model_allinit_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_object_model_title)
  8161. };
  8162. return TOOLTIPS[mode];
  8163. });
  8164. this.setHelpUrl('');
  8165. }
  8166. };
  8167. Blockly.Python.ai_model_object_init = function (block) {
  8168. var language = localStorage.getItem("handPyLanguage");
  8169. let a = ''
  8170. if (language == "en") {
  8171. a = '["aeroplane","bicycle","bird","boat","bottle","bus","car","cat","chair","cow","diningtable","dog","horse","motorbike","person","pottedplant","sheep","sofa","train","tvmonitor"]'
  8172. } else if (language == "zh-hans") {
  8173. a = '["飞机", "自行车", "鸟", "船", "瓶子", "公共汽车", "汽车", "猫", "椅子", "牛", "餐桌", "狗", "马", "摩托车", "人", "盆栽", "羊", "沙发", "火车", "电视监视器"]'
  8174. } else {
  8175. a = '["飛機", "自行車", "鳥", "船", "瓶子", "公共汽車", "汽車", "貓", "椅子", "牛", "餐桌", "狗", "馬", "摩托車", "人", "盆栽", "羊", "沙發", "火車", "電視監視器"]'
  8176. }
  8177. console.log(language)
  8178. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  8179. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  8180. Blockly.Python.definitions_.import_object = `
  8181. model = {
  8182. "param": "/root/preset/model/yolo2_20class_awnn.param",
  8183. "bin": "/root/preset/model/yolo2_20class_awnn.bin"
  8184. }
  8185. options = {
  8186. "model_type": "awnn",
  8187. "inputs": {
  8188. "input0": (224, 224, 3)
  8189. },
  8190. "outputs": {
  8191. "output0": (7, 7, (1+4+20)*5)
  8192. },
  8193. "mean": [127.5, 127.5, 127.5],
  8194. "norm": [0.0078125, 0.0078125, 0.0078125],
  8195. }
  8196. labels = ${a}
  8197. anchors = [5.4, 5.38, 1.65, 2.09, 0.8, 1.83, 2.45, 4.14, 0.46, 0.8]
  8198. m = nn.load(model, opt=options)
  8199. yolo2_decoder = decoder.Yolo2(len(labels), anchors, net_in_size=(options["inputs"]["input0"][0], options["inputs"]["input0"][1]), net_out_size=(7, 7))
  8200. `;
  8201. var _code = "";
  8202. return _code;
  8203. }
  8204. Blockly.Blocks['ai_model_object_load'] = {
  8205. init: function () {
  8206. this.appendDummyInput()
  8207. .appendField(Blockly.Msg.ai_models_load_title)
  8208. .appendField(Blockly.Msg.ai_models_init_object_model);
  8209. // this.appendDummyInput()
  8210. // .appendField(Blockly.Msg.ai_models_init_source)
  8211. // .appendField(new Blockly.FieldVariable("img_objectrecognition"), "varitem");
  8212. this.setPreviousStatement(true, null);
  8213. this.setNextStatement(true, null);
  8214. this.setColour("#ee783a");
  8215. var thisBlock = this;
  8216. this.setTooltip(function () {
  8217. var mode = thisBlock.getFieldValue('DEGREE');
  8218. var TOOLTIPS = {
  8219. 'Common Object Recognition': Blockly.Msg.ai_model_allload_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_object_model)
  8220. };
  8221. return TOOLTIPS[mode];
  8222. });
  8223. this.setHelpUrl('');
  8224. }
  8225. };
  8226. Blockly.Python.ai_model_object_load = function (block) {
  8227. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  8228. // var degee = block.getFieldValue('DEGREE');
  8229. var language = localStorage.getItem("handPyLanguage");
  8230. let a = ''
  8231. if (language == "en") {
  8232. a = '["aeroplane","bicycle","bird","boat","bottle","bus","car","cat","chair","cow","diningtable","dog","horse","motorbike","person","pottedplant","sheep","sofa","train","tvmonitor"]'
  8233. } else if (language == "zh-hans") {
  8234. a = '["飞机", "自行车", "鸟", "船", "瓶子", "公共汽车", "汽车", "猫", "椅子", "牛", "餐桌", "狗", "马", "摩托车", "人", "盆栽", "羊", "沙发", "火车", "电视监视器"]'
  8235. } else {
  8236. a = '["飛機", "自行車", "鳥", "船", "瓶子", "公共汽車", "汽車", "貓", "椅子", "牛", "餐桌", "狗", "馬", "摩托車", "人", "盆栽", "羊", "沙發", "火車", "電視監視器"]'
  8237. }
  8238. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  8239. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  8240. Blockly.Python.definitions_.import_object = `class ObjectRecognition:
  8241. def __init__(self):
  8242. model = {
  8243. "param": "/root/preset/model/yolo2_20class_awnn.param",
  8244. "bin": "/root/preset/model/yolo2_20class_awnn.bin"
  8245. }
  8246. options = {
  8247. "model_type": "awnn",
  8248. "inputs": {
  8249. "input0": (224, 224, 3)
  8250. },
  8251. "outputs": {
  8252. "output0": (7, 7, (1+4+20)*5)
  8253. },
  8254. "mean": [127.5, 127.5, 127.5],
  8255. "norm": [0.0078125, 0.0078125, 0.0078125],
  8256. }
  8257. self.labels = ${a}
  8258. anchors = [5.4, 5.38, 1.65, 2.09, 0.8, 1.83, 2.45, 4.14, 0.46, 0.8]
  8259. self.m = nn.load(model, opt=options)
  8260. self.yolo2_decoder = decoder.Yolo2(len(self.labels), anchors, net_in_size=(224,224), net_out_size=(7, 7))
  8261. self.OBJECTS = {
  8262. "OBJECT_START_X": [],
  8263. "OBJECT_START_Y": [],
  8264. "OBJECT_CONFIDENCE": [],
  8265. "OBJECT_WIDTH": [],
  8266. "OBJECT_HEIGHT": [],
  8267. "OBJECT_CENTER_X": [],
  8268. "OBJECT_CENTER_Y": [],
  8269. "OBJECT_RESULT": [],
  8270. }
  8271. def objectRecognize(self, canvas):
  8272. self.OBJECTS = {
  8273. "OBJECT_START_X": [],
  8274. "OBJECT_START_Y": [],
  8275. "OBJECT_CONFIDENCE": [],
  8276. "OBJECT_WIDTH": [],
  8277. "OBJECT_HEIGHT": [],
  8278. "OBJECT_CENTER_X": [],
  8279. "OBJECT_CENTER_Y": [],
  8280. "OBJECT_RESULT": [],
  8281. }
  8282. out = self.m.forward(canvas.tobytes(), quantize=True, layout="hwc")
  8283. BOXES, PROBS = self.yolo2_decoder.run(out, nms=0.3, threshold=0.3, img_size=(224,224))
  8284. if len(BOXES):
  8285. for BOXESI, BOXOBJ in enumerate(BOXES):
  8286. 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)
  8287. canvas.draw_rectangle(BOXOBJ[0],BOXOBJ[1], BOXOBJ[0]+BOXOBJ[2],BOXOBJ[1]+ BOXOBJ[3], color=(255,0,0), thickness=1)
  8288. BOXES[BOXESI].append(PROBS[BOXESI])
  8289. else:
  8290. BOXES = []
  8291. return BOXES
  8292. ObjectRecognition = ObjectRecognition()
  8293. `;
  8294. var _code = `canvas = canvas.crop(48,8,224,224)
  8295. BOXES = ObjectRecognition.objectRecognize(canvas)
  8296. `
  8297. return _code;
  8298. }
  8299. // ai_model_look_new
  8300. Blockly.Blocks['ai_model_look_new'] = {
  8301. init: function () {
  8302. this.appendDummyInput()
  8303. .appendField(Blockly.Msg.ai_models_object_model_if)
  8304. this.appendStatementInput("input")
  8305. .setCheck(null)
  8306. .appendField(Blockly.Msg.basic_motion_statement_exec);
  8307. this.updateShape_();
  8308. this.setMutator(new Blockly.Mutator(['ai_model_false_create_with_item']));
  8309. this.setColour("#ee783a");
  8310. this.setPreviousStatement(true, null);
  8311. this.setNextStatement(true, null);
  8312. this.setTooltip("");
  8313. this.setHelpUrl("");
  8314. this.itemCount_ = 0;
  8315. },
  8316. mutationToDom: function () {
  8317. if (!this.itemCount_)
  8318. return null;
  8319. var container = document.createElement('mutation');
  8320. this.itemCount_ && container.setAttribute('aielse', 1);
  8321. return container;
  8322. },
  8323. domToMutation: function (a) {
  8324. this.itemCount_ = parseInt(a.getAttribute('aielse'), 10);
  8325. this.updateShape_();
  8326. },
  8327. decompose: function (a) {
  8328. var b = a.newBlock('ai_model_false_create_with_container');
  8329. b.initSvg();
  8330. var c = b.nextConnection
  8331. this.itemCount_ && (a = a.newBlock("ai_model_false_create_with_item"),
  8332. a.initSvg(),
  8333. c.connect(a.previousConnection));
  8334. console.log("this.elseCount_", a)
  8335. return b
  8336. },
  8337. compose: function (a) {
  8338. var b = a.nextConnection.targetBlock();
  8339. this.itemCount_ = 0;
  8340. var e = null;
  8341. if (b && b.type === "ai_model_false_create_with_item") {
  8342. this.itemCount_++;
  8343. e = b.statementConnection_;
  8344. b = b.nextConnection && b.nextConnection.targetBlock()
  8345. }
  8346. this.updateShape_()
  8347. Blockly.Mutator.reconnect(e, this, "AIELSE")
  8348. },
  8349. saveConnections: function (a) {
  8350. a = a.nextConnection.targetBlock();
  8351. if (a && a.type === "ai_model_false_create_with_item") {
  8352. e = this.getInput("AIELSE");
  8353. a.statementConnection_ = e && e.connection.targetConnection;
  8354. a = a.nextConnection && a.nextConnection.targetBlock()
  8355. }
  8356. },
  8357. updateShape_: function () {
  8358. this.getInput("AIELSE") && this.removeInput("AIELSE");
  8359. console.log("this.itemCount_", this.itemCount_)
  8360. this.itemCount_ && this.appendStatementInput("AIELSE").appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSE)
  8361. },
  8362. };
  8363. Blockly.Python.ai_model_look_new = function (block) {
  8364. var statements_input = Blockly.Python.statementToCode(block, 'input');
  8365. var statements_input1 = Blockly.Python.statementToCode(block, 'AIELSE');
  8366. let allBlocks = block.workspace.getAllBlocks();
  8367. let global = ""
  8368. try {
  8369. global = allBlocks[0].workspace.variableList.toString()
  8370. }
  8371. catch (e) {
  8372. console.log(e)
  8373. }
  8374. Blockly.Python.addFunction("modelStatements", `def modelStatements(BOXESI):
  8375. ${global != "" ? `global ${global}` : "pass"}
  8376. ${statements_input}
  8377. `)
  8378. var code = `if len(BOXES):
  8379. for BOXESI in BOXES:
  8380. modelStatements(BOXESI)
  8381. else:
  8382. ${statements_input1 != "" ? statements_input1 : " pass"}
  8383. `
  8384. return code;
  8385. }
  8386. Blockly.Blocks['ai_model_get'] = {
  8387. init: function () {
  8388. this.appendDummyInput()
  8389. .appendField(new Blockly.FieldImage("blockly/media/object_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  8390. this.appendDummyInput()
  8391. .appendField(Blockly.Msg.ai_models_from)
  8392. .appendField(Blockly.Msg.ai_models_object_model_get)
  8393. .appendField(new Blockly.FieldDropdown([
  8394. [Blockly.Msg.ai_models_object_model_name, "Object"],
  8395. [Blockly.Msg.ai_models_object_model_x, "X"],
  8396. [Blockly.Msg.ai_models_object_model_y, "Y"],
  8397. // [Blockly.Msg.ai_models_object_model_w, "W"],
  8398. // [Blockly.Msg.ai_models_object_model_h, "H"],
  8399. // [Blockly.Msg.ai_models_object_model_x_center, "CX"],
  8400. // [Blockly.Msg.ai_models_object_model_y_center, "CY"],
  8401. // [Blockly.Msg.ai_models_object_model_count, "Number"],
  8402. // [Blockly.Msg.ai_models_object_model_index, "Serial"],
  8403. [Blockly.Msg.ai_models_object_model_confidence, "Confidence"],
  8404. [Blockly.Msg.ai_models_face_model_ws, "2"],
  8405. [Blockly.Msg.ai_models_face_model_hs, "3"],
  8406. [Blockly.Msg.ai_models_object_model_x_center, "CX"],
  8407. [Blockly.Msg.ai_models_object_model_y_center, "CY"]
  8408. ]), "TYPE");
  8409. this.setInputsInline(false);
  8410. this.setOutput(true, null);
  8411. this.setColour("#ee783a");
  8412. var thisBlock = this;
  8413. this.setTooltip(function () {
  8414. var mode = thisBlock.getFieldValue('TYPE');
  8415. var TOOLTIPS = {
  8416. 'Object': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_name),
  8417. 'X': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_x),
  8418. 'Y': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_y),
  8419. 'W': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_w),
  8420. 'H': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_h),
  8421. // 'CX': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_x_center),
  8422. // 'CY': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_y_center),
  8423. // 'Number': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_count),
  8424. // 'Serial': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_index),
  8425. // 'Confidence': Blockly.Msg.ai_model_get_TOOLTIP.replace('%1', Blockly.Msg.ai_models_object_model_confidence)
  8426. };
  8427. return TOOLTIPS[mode];
  8428. });
  8429. this.setHelpUrl("");
  8430. }
  8431. };
  8432. Blockly.Python.ai_model_get = function (block) {
  8433. var type = block.getFieldValue('TYPE');
  8434. var variable_name = "BOXESI";
  8435. var _code = "";
  8436. if (type == "Object") {
  8437. _code = `labels[${variable_name}[4][0]]`
  8438. // _code = `"{}:{:.2f}%".format(labels[probs[${variable_name}][0]], probs[${variable_name}][1][probs[${variable_name}][0]]*100)`
  8439. } else if (type == "X") {
  8440. _code = '' + variable_name + '[0]';
  8441. } else if (type == "Y") {
  8442. _code = '' + variable_name + '[1]';
  8443. } else if (type == "W") {
  8444. _code = 'int(' + variable_name + '[0]+' + '' + variable_name + '[2])';
  8445. } else if (type == "H") {
  8446. _code = 'int(' + variable_name + '[1]+' + '' + variable_name + '[3])';
  8447. } else if (type == "Confidence") {
  8448. _code = `${variable_name}[4][1][${variable_name}[4][0]]*100`;
  8449. } else if (type == "CX") {
  8450. _code = `int((${variable_name}[0] +${variable_name}[0] + ${variable_name}[2])/2)`;
  8451. } else if (type == "CY") {
  8452. _code = `int((${variable_name}[1] + ${variable_name}[1] + ${variable_name}[3])/2)`;
  8453. }
  8454. else {
  8455. _code = `${variable_name}[${type}]`;
  8456. }
  8457. return [_code, Blockly.Python.ORDER_ATOMIC];
  8458. }
  8459. Blockly.Blocks['ai_model_look'] = {
  8460. init: function () {
  8461. this.appendDummyInput()
  8462. .appendField(Blockly.Msg.ai_models_object_model_if);
  8463. this.setInputsInline(false);
  8464. this.setOutput(true, null);
  8465. this.setColour("#ee783a");
  8466. this.setTooltip(Blockly.Msg.ai_model_look_TOOLTIP);
  8467. this.setHelpUrl("");
  8468. }
  8469. };
  8470. Blockly.Python.ai_model_look = function (block) {
  8471. var _code = "len(boxes)";
  8472. return [_code, Blockly.Python.ORDER_ATOMIC];
  8473. }
  8474. Blockly.Blocks['ai_model_look_attribute'] = {
  8475. init: function () {
  8476. this.appendDummyInput()
  8477. .appendField(Blockly.Msg.ai_model_look_attribute);
  8478. this.setNextStatement(true, null);
  8479. this.setPreviousStatement(true, null);
  8480. this.setColour("#ee783a");
  8481. this.setTooltip(Blockly.Msg.ai_model_look_attribute);
  8482. this.setHelpUrl("");
  8483. }
  8484. };
  8485. Blockly.Python['ai_model_look_attribute'] = function (block) {
  8486. Blockly.Python.addFunction("objectRecognize", `def objectRecognize():
  8487. global BOXES
  8488. if len(BOXES):
  8489. for BOXESI, BOXOBJ in enumerate(BOXES):
  8490. 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)
  8491. canvas.draw_rectangle(BOXOBJ[0],BOXOBJ[1], BOXOBJ[0]+BOXOBJ[2],BOXOBJ[1]+ BOXOBJ[3], color=(255,0,0), thickness=1)
  8492. `)
  8493. var code = 'objectRecognize()\n';
  8494. return code;
  8495. };
  8496. // 猜拳识别
  8497. Blockly.Blocks['ai_model_Guesswork_init'] = {
  8498. init: function () {
  8499. this.appendDummyInput()
  8500. .appendField(Blockly.Msg.ai_models_init_title)
  8501. .appendField(Blockly.Msg.ai_models_init_guesswork_model);
  8502. // this.appendDummyInput().appendField(Blockly.Msg.ai_model_allinit_language).appendField(new Blockly.FieldDropdown([
  8503. // [Blockly.Msg.ai_speech_recognition_setup_set_english, "English"],
  8504. // [Blockly.Msg.ai_speech_recognition_setup_set_simplified_chinese, "Simplified Chinese"],
  8505. // [Blockly.Msg.ai_speech_recognition_setup_set_traditional_chinese, "Traditional Chinese"]
  8506. // ]), "interface")
  8507. // this.appendValueInput("class_input")
  8508. // .setCheck(null)
  8509. // .appendField(Blockly.Msg.ai_models_customized_init_setup_name);
  8510. this.setPreviousStatement(true, null);
  8511. this.setNextStatement(true, null);
  8512. this.setColour("#ee783a");
  8513. var thisBlock = this;
  8514. this.setTooltip(function () {
  8515. var mode = thisBlock.getFieldValue('DEGREE');
  8516. var TOOLTIPS = {
  8517. 'Common Object Recognition': Blockly.Msg.ai_model_allinit_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_object_model_title)
  8518. };
  8519. return TOOLTIPS[mode];
  8520. });
  8521. this.setHelpUrl('');
  8522. }
  8523. };
  8524. Blockly.Python.ai_model_Guesswork_init = function (block) {
  8525. var language = localStorage.getItem("handPyLanguage");
  8526. console.log(language)
  8527. let a = ''
  8528. if (language == "en") {
  8529. a = '["Scissors", "Stone" ,"Paper"]'
  8530. } else if (language == "zh-hans") {
  8531. a = '["剪刀", "石头" ,"布"]'
  8532. } else {
  8533. a = '["剪刀", "石頭" ,"布"]'
  8534. }
  8535. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  8536. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  8537. Blockly.Python.definitions_.import_Guesswork_init = `class Mora:
  8538. mud_path = "/root/preset/model/mora_int8.mud"
  8539. labels = ${a}
  8540. anchors = [3.23, 3.25, 1.47, 1.55, 5.09, 5.33, 4.03, 4.28, 2.12, 2.56]
  8541. def __init__(self) -> None:
  8542. from maix import nn
  8543. self.model = nn.load(self.mud_path)
  8544. from maix.nn import decoder
  8545. self.decoder = decoder.Yolo2(len(self.labels) , self.anchors , net_in_size = (224, 224) ,net_out_size = (7,7))
  8546. def __del__(self):
  8547. del self.model
  8548. del self.decoder
  8549. def cal_fps(self ,start , end):
  8550. one_second = 1
  8551. one_flash = end - start
  8552. fps = one_second / one_flash
  8553. return fps
  8554. def draw_rectangle_with_title(self ,img, box, disp_str , fps ):
  8555. img.draw_rectangle(box[0], box[1], box[0] + box[2], box[1] + box[3],color=(255, 0, 0), thickness=2)
  8556. img.draw_string(box[0], box[1] ,disp_str, scale=1,color=(222, 0, 3), thickness=2)
  8557. #img.draw_string(0, 0 ,"FPS :"+str(fps), scale=2 ,color=(0, 0, 255), thickness=2)
  8558. def process(self,input):
  8559. t = time()
  8560. out = self.model.forward(input, quantize=1, layout = "hwc")
  8561. boxes, probs = self.decoder.run(out, nms=0.5, threshold=0.3, img_size=(224,224))
  8562. for i, box in enumerate(boxes):
  8563. class_id = probs[i][0]
  8564. prob = probs[i][1][class_id]
  8565. disp_str = "{}:{:.2f}%".format(self.labels[class_id], prob*100)
  8566. fps = self.cal_fps(t, time())
  8567. self.draw_rectangle_with_title(input, box, disp_str, fps)
  8568. Mora = Mora()
  8569. `;
  8570. var _code = "";
  8571. return _code;
  8572. }
  8573. Blockly.Blocks['ai_model_Guesswork_load'] = {
  8574. init: function () {
  8575. this.appendDummyInput()
  8576. .appendField(Blockly.Msg.ai_models_load_title)
  8577. .appendField(Blockly.Msg.ai_models_init_guesswork_model);
  8578. // this.appendDummyInput()
  8579. // .appendField(Blockly.Msg.ai_models_init_source)
  8580. // .appendField(new Blockly.FieldVariable("img_guessworkrecognition"), "varitem");
  8581. this.setPreviousStatement(true, null);
  8582. this.setNextStatement(true, null);
  8583. this.setColour("#ee783a");
  8584. var thisBlock = this;
  8585. this.setTooltip(function () {
  8586. var mode = thisBlock.getFieldValue('DEGREE');
  8587. var TOOLTIPS = {
  8588. 'Common Object Recognition': Blockly.Msg.ai_model_allload_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_object_model)
  8589. };
  8590. return TOOLTIPS[mode];
  8591. });
  8592. this.setHelpUrl('');
  8593. }
  8594. };
  8595. Blockly.Python.ai_model_Guesswork_load = function (block) {
  8596. var language = localStorage.getItem("handPyLanguage");
  8597. let a = ''
  8598. if (language == "en") {
  8599. a = '["Scissors", "Stone" ,"Paper"]'
  8600. } else if (language == "zh-hans") {
  8601. a = '["剪刀", "石头" ,"布"]'
  8602. } else {
  8603. a = '["剪刀", "石頭" ,"布"]'
  8604. }
  8605. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  8606. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  8607. Blockly.Python.definitions_.import_Guesswork_init = `class Mora:
  8608. mud_path = "/root/preset/model/mora_int8.mud"
  8609. labels = ${a}
  8610. anchors = [3.23, 3.25, 1.47, 1.55, 5.09, 5.33, 4.03, 4.28, 2.12, 2.56]
  8611. def __init__(self) -> None:
  8612. self.model = nn.load(self.mud_path)
  8613. self.decoder = decoder.Yolo2(len(self.labels) , self.anchors , net_in_size = (224, 224) ,net_out_size = (7,7))
  8614. self.MORAS = {
  8615. "MORA_START_X": [],
  8616. "MORA_START_Y": [],
  8617. "MORA_CONFIDENCE": [],
  8618. "MORA_WIDTH": [],
  8619. "MORA_HEIGHT": [],
  8620. "MORA_CENTER_X": [],
  8621. "MORA_CENTER_Y": [],
  8622. "MORA_RESULT": [],
  8623. }
  8624. def __del__(self):
  8625. del self.model
  8626. del self.decoder
  8627. def cal_fps(self ,start , end):
  8628. one_second = 1
  8629. one_flash = end - start
  8630. fps = one_second / one_flash
  8631. return fps
  8632. def draw_rectangle_with_title(self ,img, box, disp_str ):
  8633. img.draw_rectangle(box[0], box[1], box[0] + box[2], box[1] + box[3],color=(255, 0, 0), thickness=2)
  8634. img.draw_string(box[0], box[1] ,disp_str, scale=1,color=(222, 0, 3), thickness=2)
  8635. def process(self,input):
  8636. out = self.model.forward(input, quantize=1, layout = "hwc")
  8637. boxes, probs = self.decoder.run(out, nms=0.5, threshold=0.3, img_size=(224,224))
  8638. for i, box in enumerate(boxes):
  8639. class_id = probs[i][0]
  8640. prob = probs[i][1][class_id]
  8641. disp_str = "{}:{:.2f}%".format(self.labels[class_id], prob*100)
  8642. self.draw_rectangle_with_title(input, box, disp_str)
  8643. boxes[i].append(probs[i])
  8644. return boxes
  8645. Mora = Mora()
  8646. `;
  8647. var _code = `canvas = canvas.crop(48,8,224,224)
  8648. BOXES = Mora.process(canvas)
  8649. `
  8650. return _code;
  8651. }
  8652. // ai_model_Guesswork_look_new
  8653. Blockly.Blocks['ai_model_Guesswork_look_new'] = {
  8654. init: function () {
  8655. this.appendDummyInput()
  8656. .appendField(Blockly.Msg.ai_model_guesswork_look_attribute)
  8657. this.appendStatementInput("input")
  8658. .setCheck(null)
  8659. .appendField(Blockly.Msg.basic_motion_statement_exec);
  8660. this.updateShape_();
  8661. this.setMutator(new Blockly.Mutator(['ai_model_false_create_with_item']));
  8662. this.setColour("#ee783a");
  8663. this.setPreviousStatement(true, null);
  8664. this.setNextStatement(true, null);
  8665. this.setTooltip("");
  8666. this.setHelpUrl("");
  8667. this.itemCount_ = 0;
  8668. },
  8669. mutationToDom: function () {
  8670. if (!this.itemCount_)
  8671. return null;
  8672. var container = document.createElement('mutation');
  8673. this.itemCount_ && container.setAttribute('aielse', 1);
  8674. return container;
  8675. },
  8676. domToMutation: function (a) {
  8677. this.itemCount_ = parseInt(a.getAttribute('aielse'), 10);
  8678. this.updateShape_();
  8679. },
  8680. decompose: function (a) {
  8681. var b = a.newBlock('ai_model_false_create_with_container');
  8682. b.initSvg();
  8683. var c = b.nextConnection
  8684. this.itemCount_ && (a = a.newBlock("ai_model_false_create_with_item"),
  8685. a.initSvg(),
  8686. c.connect(a.previousConnection));
  8687. console.log("this.elseCount_", a)
  8688. return b
  8689. },
  8690. compose: function (a) {
  8691. var b = a.nextConnection.targetBlock();
  8692. this.itemCount_ = 0;
  8693. var e = null;
  8694. if (b && b.type === "ai_model_false_create_with_item") {
  8695. this.itemCount_++;
  8696. e = b.statementConnection_;
  8697. b = b.nextConnection && b.nextConnection.targetBlock()
  8698. }
  8699. this.updateShape_()
  8700. Blockly.Mutator.reconnect(e, this, "AIELSE")
  8701. },
  8702. saveConnections: function (a) {
  8703. a = a.nextConnection.targetBlock();
  8704. if (a && a.type === "ai_model_false_create_with_item") {
  8705. e = this.getInput("AIELSE");
  8706. a.statementConnection_ = e && e.connection.targetConnection;
  8707. a = a.nextConnection && a.nextConnection.targetBlock()
  8708. }
  8709. },
  8710. updateShape_: function () {
  8711. this.getInput("AIELSE") && this.removeInput("AIELSE");
  8712. console.log("this.itemCount_", this.itemCount_)
  8713. this.itemCount_ && this.appendStatementInput("AIELSE").appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSE)
  8714. },
  8715. };
  8716. Blockly.Python.ai_model_Guesswork_look_new = function (block) {
  8717. var statements_input = Blockly.Python.statementToCode(block, 'input');
  8718. var statements_input1 = Blockly.Python.statementToCode(block, 'AIELSE');
  8719. let allBlocks = block.workspace.getAllBlocks();
  8720. let global = ""
  8721. try {
  8722. global = allBlocks[0].workspace.variableList.toString()
  8723. }
  8724. catch (e) {
  8725. console.log(e)
  8726. }
  8727. Blockly.Python.addFunction("GuessworkStatements", `def GuessworkStatements(BOXESI):
  8728. ${global != "" ? `global ${global}` : "pass"}
  8729. ${statements_input}
  8730. `)
  8731. var code = `if len(BOXES) > 0:
  8732. for BOXESI in BOXES:
  8733. GuessworkStatements(BOXESI)
  8734. else:
  8735. ${statements_input1 != "" ? statements_input1 : " pass"}
  8736. `
  8737. return code;
  8738. }
  8739. Blockly.Blocks['ai_model_Guesswork_get'] = {
  8740. init: function () {
  8741. this.appendDummyInput()
  8742. .appendField(new Blockly.FieldImage("blockly/media/object_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  8743. this.appendDummyInput()
  8744. .appendField(Blockly.Msg.ai_models_from)
  8745. .appendField(Blockly.Msg.ai_models_guesswork_model_get)
  8746. .appendField(new Blockly.FieldDropdown([
  8747. [Blockly.Msg.ai_models_object_model_name, "Object"],
  8748. [Blockly.Msg.ai_models_object_model_x, "X"],
  8749. [Blockly.Msg.ai_models_object_model_y, "Y"],
  8750. // [Blockly.Msg.ai_models_object_model_w, "W"],
  8751. // [Blockly.Msg.ai_models_object_model_h, "H"],
  8752. // [Blockly.Msg.ai_models_object_model_x_center, "CX"],
  8753. // [Blockly.Msg.ai_models_object_model_y_center, "CY"],
  8754. // [Blockly.Msg.ai_models_object_model_count, "Number"],
  8755. // [Blockly.Msg.ai_models_object_model_index, "Serial"],
  8756. [Blockly.Msg.ai_models_object_model_confidence, "Confidence"],
  8757. [Blockly.Msg.ai_models_face_model_ws, "2"],
  8758. [Blockly.Msg.ai_models_face_model_hs, "3"],
  8759. [Blockly.Msg.ai_models_object_model_x_center, "CX"],
  8760. [Blockly.Msg.ai_models_object_model_y_center, "CY"]
  8761. ]), "TYPE");
  8762. this.setInputsInline(false);
  8763. this.setOutput(true, null);
  8764. this.setColour("#ee783a");
  8765. var thisBlock = this;
  8766. this.setTooltip(Blockly.Msg.ai_models_guesswork_model_get);
  8767. this.setHelpUrl("");
  8768. }
  8769. };
  8770. Blockly.Python.ai_model_Guesswork_get = function (block) {
  8771. var type = block.getFieldValue('TYPE');
  8772. var variable_name = "BOXESI";
  8773. var _code = "";
  8774. if (type == "Object") {
  8775. _code = `Mora.labels[${variable_name}[4][0]]`
  8776. // _code = `"{}:{:.2f}%".format(labels[probs[${variable_name}][0]], probs[${variable_name}][1][probs[${variable_name}][0]]*100)`
  8777. } else if (type == "X") {
  8778. _code = '' + variable_name + '[0]';
  8779. } else if (type == "Y") {
  8780. _code = '' + variable_name + '[1]';
  8781. } else if (type == "W") {
  8782. _code = 'int(' + variable_name + '[0]+' + '' + variable_name + '[2])';
  8783. } else if (type == "H") {
  8784. _code = 'int(' + variable_name + '[1]+' + '' + variable_name + '[3])';
  8785. } else if (type == "Confidence") {
  8786. _code = `${variable_name}[4][1][${variable_name}[4][0]]*100`;
  8787. } else if (type == "CX") {
  8788. _code = `int((${variable_name}[0] +${variable_name}[0] + ${variable_name}[2])/2)`;
  8789. } else if (type == "CY") {
  8790. _code = `int((${variable_name}[1] + ${variable_name}[1] + ${variable_name}[3])/2)`;
  8791. }
  8792. else {
  8793. _code = `${variable_name}[${type}]`;
  8794. }
  8795. return [_code, Blockly.Python.ORDER_ATOMIC];
  8796. }
  8797. Blockly.Blocks['ai_model_Guesswork_look'] = {
  8798. init: function () {
  8799. this.appendDummyInput()
  8800. .appendField(Blockly.Msg.ai_models_guesswork_model_if);
  8801. this.setInputsInline(false);
  8802. this.setNextStatement(true, null);
  8803. this.setPreviousStatement(true, null);
  8804. this.setColour("#ee783a");
  8805. this.setTooltip(Blockly.Msg.ai_models_guesswork_model_if);
  8806. this.setHelpUrl("");
  8807. }
  8808. };
  8809. Blockly.Python.ai_model_Guesswork_look = function (block) {
  8810. Blockly.Python.addFunction("guessworkShowRecognize", `def guessworkShowRecognize():
  8811. global BOXES
  8812. if len(BOXES):
  8813. for BOXESI, BOXOBJ in enumerate(BOXES):
  8814. 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)
  8815. canvas.draw_rectangle(BOXOBJ[0],BOXOBJ[1], BOXOBJ[0]+BOXOBJ[2],BOXOBJ[1]+ BOXOBJ[3], color=(255,0,0), thickness=1)
  8816. `)
  8817. var _code = "guessworkShowRecognize()\n";
  8818. return _code;
  8819. }
  8820. Blockly.Blocks['ai_model_Guesswork_look_attribute'] = {
  8821. init: function () {
  8822. this.appendDummyInput()
  8823. .appendField(Blockly.Msg.ai_model_guesswork_look_attribute);
  8824. this.setOutput(true, null);
  8825. this.setColour("#ee783a");
  8826. this.setTooltip(Blockly.Msg.ai_model_guesswork_look_attribute);
  8827. this.setHelpUrl("");
  8828. }
  8829. };
  8830. Blockly.Python['ai_model_Guesswork_look_attribute'] = function (block) {
  8831. var code = 'len(BOXES)';
  8832. return [code, Blockly.Python.ORDER_NONE];
  8833. };
  8834. Blockly.Blocks['ai_model_face_recognition_load'] = {
  8835. init: function () {
  8836. this.appendDummyInput()
  8837. .appendField(Blockly.Msg.ai_models_load_title)
  8838. .appendField(Blockly.Msg.ai_models_init_face_model);
  8839. // this.appendDummyInput()
  8840. // .appendField(Blockly.Msg.ai_models_init_source)
  8841. // .appendField(new Blockly.FieldVariable("img_facedetection"), "varitem");
  8842. this.setPreviousStatement(true, null);
  8843. this.setNextStatement(true, null);
  8844. this.setColour("#ee783a");
  8845. var thisBlock = this;
  8846. this.setTooltip(function () {
  8847. var mode = thisBlock.getFieldValue('DEGREE');
  8848. var TOOLTIPS = {
  8849. 'Human Face Recognition': Blockly.Msg.ai_model_allload_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_face_model)
  8850. };
  8851. return TOOLTIPS[mode];
  8852. });
  8853. this.setHelpUrl('');
  8854. }
  8855. };
  8856. Blockly.Python.ai_model_face_recognition_load = function (block) {
  8857. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  8858. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  8859. Blockly.Python.definitions_.import_face_recognition = `class YoloFaceRecognize:
  8860. labels = ["person"]
  8861. anchors = [1.19, 1.98, 2.79, 4.59, 4.53, 8.92, 8.06, 5.29, 10.32, 10.65]
  8862. m = {
  8863. "param": "/root/preset/model/yolo2_face_awnn.param",
  8864. "bin": "/root/preset/model/yolo2_face_awnn.bin"
  8865. }
  8866. options = {
  8867. "model_type": "awnn",
  8868. "inputs": {
  8869. "input0": (224, 224, 3)
  8870. },
  8871. "outputs": {
  8872. "output0": (7, 7, (1+4+len(labels))*5)
  8873. },
  8874. "mean": [127.5, 127.5, 127.5],
  8875. "norm": [0.0078125, 0.0078125, 0.0078125],
  8876. }
  8877. def __init__(self):
  8878. self.model = nn.load(self.m, opt=self.options)
  8879. self.decoder = decoder.Yolo2(len(self.labels), self.anchors, net_in_size=(224, 224), net_out_size=(7, 7))
  8880. self.FACES = {
  8881. "FACES_START_X": [],
  8882. "FACES_START_Y": [],
  8883. "FACENUM": 0,
  8884. "FACES_CONFIDENCE": [],
  8885. "FACES_WIDTH": [],
  8886. "FACES_HEIGHT": [],
  8887. "FACES_CENTER_X": [],
  8888. "FACES_CENTER_Y": []
  8889. }
  8890. def __del__(self):
  8891. del self.model
  8892. del self.decoder
  8893. def faceShowDetection(self,canvas):
  8894. out = YoloFaceRecognize.model.forward(canvas.tobytes(), quantize=True, layout="hwc")
  8895. BOXES, PROBS = YoloFaceRecognize.decoder.run(out, nms=0.3, threshold=0.5, img_size=(224, 224))
  8896. if len(BOXES):
  8897. for BOXESI, BOXOBJ in enumerate(BOXES):
  8898. canvas.draw_rectangle(BOXOBJ[0],BOXOBJ[1], BOXOBJ[0]+BOXOBJ[2],BOXOBJ[1]+ BOXOBJ[3], color=(255,0,0), thickness=1)
  8899. BOXES[BOXESI].append(PROBS[BOXESI])
  8900. def returnFaces(self):
  8901. return self.FACES
  8902. YoloFaceRecognize = YoloFaceRecognize()
  8903. `;
  8904. var _code = `canvas = canvas.crop(48,8,224,224)
  8905. BOXES = YoloFaceRecognize.faceShowDetection(canvas)\n`
  8906. return _code;
  8907. }
  8908. Blockly.Blocks['ai_model_xy'] = {
  8909. init: function () {
  8910. this.appendDummyInput()
  8911. .appendField(new Blockly.FieldImage("blockly/media/face_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  8912. this.appendDummyInput()
  8913. .appendField(Blockly.Msg.ai_models_from)
  8914. .appendField(Blockly.Msg.ai_models_face_model_get)
  8915. .appendField(new Blockly.FieldDropdown([
  8916. [Blockly.Msg.ai_models_object_model_name, "Object"],
  8917. [Blockly.Msg.ai_models_face_model_x, "X"],
  8918. [Blockly.Msg.ai_models_face_model_y, "Y"],
  8919. // [Blockly.Msg.ai_models_face_model_w, "W"],
  8920. // [Blockly.Msg.ai_models_face_model_h, "H"],
  8921. // [Blockly.Msg.ai_models_face_model_x_center, "CX"],
  8922. // [Blockly.Msg.ai_models_face_model_y_center, "CY"],
  8923. [Blockly.Msg.ai_models_face_model_count, "Number"],
  8924. // [Blockly.Msg.ai_models_face_model_index, "Serial"],
  8925. [Blockly.Msg.ai_models_face_model_confidence, "Confidence"],
  8926. [Blockly.Msg.ai_models_face_model_ws, "2"],
  8927. [Blockly.Msg.ai_models_face_model_hs, "3"],
  8928. [Blockly.Msg.ai_models_object_model_x_center, "CX"],
  8929. [Blockly.Msg.ai_models_object_model_y_center, "CY"]
  8930. ]), "TYPE");
  8931. this.setInputsInline(false);
  8932. this.setOutput(true, null);
  8933. this.setColour("#ee783a");
  8934. var thisBlock = this;
  8935. this.setTooltip(function () {
  8936. var mode = thisBlock.getFieldValue('TYPE');
  8937. var TOOLTIPS = {
  8938. 'X': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x),
  8939. 'Y': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y),
  8940. 'W': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_w),
  8941. 'H': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_h),
  8942. // 'CX': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x_center),
  8943. // 'CY': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y_center),
  8944. 'Number': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_count),
  8945. // 'Serial': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_index),
  8946. // 'Confidence': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_confidence)
  8947. };
  8948. return TOOLTIPS[mode];
  8949. });
  8950. this.setHelpUrl("");
  8951. }
  8952. };
  8953. Blockly.Python.ai_model_xy = function (block) {
  8954. var type = block.getFieldValue('TYPE');
  8955. var variable_name = "BOXESI";
  8956. var _code = "";
  8957. if (type == "X") {
  8958. _code = '' + variable_name + '[0]';
  8959. } else if (type == "Y") {
  8960. _code = '' + variable_name + '[1]';
  8961. } else if (type == "W") {
  8962. _code = 'int(' + variable_name + '[0]+' + '' + variable_name + '[2])';
  8963. } else if (type == "H") {
  8964. _code = 'int(' + variable_name + '[1]+' + '' + variable_name + '[3])';
  8965. } else if (type == "Confidence") {
  8966. _code = `${variable_name}[4][1][${variable_name}[4][0]]*100`;
  8967. } else if (type == "Object") {
  8968. _code = `"Face"`;
  8969. } else if (type == "CX") {
  8970. _code = `int((${variable_name}[0] +${variable_name}[0] + ${variable_name}[2])/2)`;
  8971. } else if (type == "CY") {
  8972. _code = `int((${variable_name}[1] + ${variable_name}[1] + ${variable_name}[3])/2)`;
  8973. } else if (type == "Number") {
  8974. _code = `len(boxes)`;
  8975. }
  8976. else {
  8977. _code = `${variable_name}[${type}]`;
  8978. }
  8979. return [_code, Blockly.Python.ORDER_ATOMIC];
  8980. };
  8981. Blockly.Blocks['ai_model_face_new'] = {
  8982. init: function () {
  8983. this.appendDummyInput()
  8984. .appendField(Blockly.Msg.ai_model_face_model_list)
  8985. this.appendStatementInput("input")
  8986. .setCheck(null)
  8987. .appendField(Blockly.Msg.basic_motion_statement_exec);
  8988. this.updateShape_();
  8989. this.setMutator(new Blockly.Mutator(['ai_model_false_create_with_item']));
  8990. this.setColour("#ee783a");
  8991. this.setPreviousStatement(true, null);
  8992. this.setNextStatement(true, null);
  8993. this.setTooltip("");
  8994. this.setHelpUrl("");
  8995. this.itemCount_ = 0;
  8996. },
  8997. mutationToDom: function () {
  8998. if (!this.itemCount_)
  8999. return null;
  9000. var container = document.createElement('mutation');
  9001. this.itemCount_ && container.setAttribute('aielse', 1);
  9002. return container;
  9003. },
  9004. domToMutation: function (a) {
  9005. this.itemCount_ = parseInt(a.getAttribute('aielse'), 10);
  9006. this.updateShape_();
  9007. },
  9008. decompose: function (a) {
  9009. var b = a.newBlock('ai_model_false_create_with_container');
  9010. b.initSvg();
  9011. var c = b.nextConnection
  9012. this.itemCount_ && (a = a.newBlock("ai_model_false_create_with_item"),
  9013. a.initSvg(),
  9014. c.connect(a.previousConnection));
  9015. console.log("this.elseCount_", a)
  9016. return b
  9017. },
  9018. compose: function (a) {
  9019. var b = a.nextConnection.targetBlock();
  9020. this.itemCount_ = 0;
  9021. var e = null;
  9022. if (b && b.type === "ai_model_false_create_with_item") {
  9023. this.itemCount_++;
  9024. e = b.statementConnection_;
  9025. b = b.nextConnection && b.nextConnection.targetBlock()
  9026. }
  9027. this.updateShape_()
  9028. Blockly.Mutator.reconnect(e, this, "AIELSE")
  9029. },
  9030. saveConnections: function (a) {
  9031. a = a.nextConnection.targetBlock();
  9032. if (a && a.type === "ai_model_false_create_with_item") {
  9033. e = this.getInput("AIELSE");
  9034. a.statementConnection_ = e && e.connection.targetConnection;
  9035. a = a.nextConnection && a.nextConnection.targetBlock()
  9036. }
  9037. },
  9038. updateShape_: function () {
  9039. this.getInput("AIELSE") && this.removeInput("AIELSE");
  9040. console.log("this.itemCount_", this.itemCount_)
  9041. this.itemCount_ && this.appendStatementInput("AIELSE").appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSE)
  9042. },
  9043. };
  9044. Blockly.Python.ai_model_face_new = function (block) {
  9045. var statements_input = Blockly.Python.statementToCode(block, 'input');
  9046. var statements_input1 = Blockly.Python.statementToCode(block, 'AIELSE');
  9047. let allBlocks = block.workspace.getAllBlocks();
  9048. let global = ""
  9049. try {
  9050. global = allBlocks[0].workspace.variableList.toString()
  9051. }
  9052. catch (e) {
  9053. console.log(e)
  9054. }
  9055. Blockly.Python.addFunction("GuessworkStatements", `def GuessworkStatements(BOXESI):
  9056. ${global != "" ? `global ${global}` : "pass"}
  9057. ${statements_input}
  9058. `)
  9059. var code = `if len(BOXES) > 0:
  9060. for BOXESI in BOXES:
  9061. GuessworkStatements(BOXESI)
  9062. else:
  9063. ${statements_input1 != "" ? statements_input1 : " pass"}
  9064. `
  9065. return code;
  9066. }
  9067. Blockly.Blocks['ai_model_face'] = {
  9068. init: function () {
  9069. this.appendDummyInput()
  9070. .appendField(Blockly.Msg.ai_models_face_model_if);
  9071. this.setInputsInline(false);
  9072. this.setNextStatement(true, null);
  9073. this.setPreviousStatement(true, null);
  9074. this.setColour("#ee783a");
  9075. this.setTooltip(Blockly.Msg.ai_models_face_model_if);
  9076. this.setHelpUrl("");
  9077. }
  9078. };
  9079. Blockly.Python.ai_model_face = function (block) {
  9080. Blockly.Python.addFunction("faceShowDetection", `def faceShowDetection():
  9081. global BOXES
  9082. if len(BOXES):
  9083. for BOXESI, BOXOBJ in enumerate(BOXES):
  9084. canvas.draw_rectangle(BOXOBJ[0],BOXOBJ[1], BOXOBJ[0]+BOXOBJ[2],BOXOBJ[1]+ BOXOBJ[3], color=(255,0,0), thickness=1)
  9085. `)
  9086. var _code = "faceShowDetection()\n";
  9087. return _code;
  9088. }
  9089. Blockly.Blocks['ai_model_face_attribute'] = {
  9090. init: function () {
  9091. this.appendDummyInput()
  9092. .appendField(Blockly.Msg.ai_model_face_model_list);
  9093. this.setOutput(true, null);
  9094. this.setColour("#ee783a");
  9095. this.setTooltip(Blockly.Msg.ai_model_face_model_list);
  9096. this.setHelpUrl("");
  9097. }
  9098. };
  9099. Blockly.Python['ai_model_face_attribute'] = function (block) {
  9100. var code = 'boxes';
  9101. return [code, Blockly.Python.ORDER_NONE];
  9102. };
  9103. // 车牌识别
  9104. Blockly.Blocks['ai_model_card_recognition_init'] = {
  9105. init: function () {
  9106. // this.appendDummyInput()
  9107. // .appendField(new Blockly.FieldImage("blockly/media/header_customized_model.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
  9108. this.appendDummyInput()
  9109. .appendField(Blockly.Msg.ai_models_init_title)
  9110. .appendField(Blockly.Msg.ai_models_init_card_model);
  9111. this.setInputsInline(false);
  9112. this.setPreviousStatement(true, null);
  9113. this.setNextStatement(true, null);
  9114. this.setColour("#ee783a");
  9115. this.setTooltip(Blockly.Msg.ai_model_customized_init_setup_definition_TOOLTIP);
  9116. this.setHelpUrl("");
  9117. }
  9118. };
  9119. Blockly.Python['ai_model_card_recognition_init'] = function (block) {
  9120. // TODO: Assemble Python into code variable.
  9121. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  9122. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  9123. Blockly.Python.addVariable('CARDDATABOEX', `CARDDATABOEX = ""`, true)
  9124. Blockly.Python.definitions_['v831_card_recognition_init'] = `class LPR:
  9125. loc_model_path = "/root/preset/model/loc.mud"
  9126. reg_model_path = "/root/preset/model/reg.mud"
  9127. chars = ["皖", "沪", "津", "渝", "冀", "晋", "蒙", "辽", "吉", "黑",
  9128. "苏", "浙", "京", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤",
  9129. "桂", "琼", "川", "贵", "云", "藏", "陕", "甘", "青", "宁",
  9130. "新", "警", "学", "A", "B", "C", "D", "E", "F", "G",
  9131. "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S",
  9132. "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3",
  9133. "4", "5", "6", "7", "8", "9", "-"]
  9134. variances = [0.1, 0.2]
  9135. steps = [8, 16, 32]
  9136. min_sizes = [12, 24, 48, 96, 192, 320]
  9137. card_input_img = None
  9138. def __init__(self) -> None:
  9139. from maix import nn
  9140. self.loc_model = nn.load(self.loc_model_path, opt=None)
  9141. self.reg_model = nn.load(self.reg_model_path, opt=None)
  9142. from maix.nn import decoder
  9143. self.loc_decoder = decoder.license_plate_location(
  9144. [224, 224], self.steps, self.min_sizes, self.variances)
  9145. self.reg_decoder = decoder.CTC((1, 68, 18))
  9146. def __del__(self):
  9147. del self.loc_model
  9148. del self.loc_decoder
  9149. def cal_fps(self, start, end):
  9150. one_second = 1
  9151. one_flash = end - start
  9152. fps = one_second / one_flash
  9153. return fps
  9154. def draw_fps(self, img, fps):
  9155. img.draw_string(0, 0, "FPS :"+str(fps), scale=1,
  9156. color=(255, 0, 255), thickness=1)
  9157. def draw_string(self, img, x, y, string, color):
  9158. img.draw_string(x, y, string, color=color)
  9159. def draw_paste(self, src, dst):
  9160. src.paste(dst, 0, 0)
  9161. def draw_rectangle(self, img, box):
  9162. img.draw_rectangle(box[0], box[1], box[2], box[3],
  9163. color=(230, 230, 250), thickness=2)
  9164. def draw_point(self, img, landmark):
  9165. for i in range(4):
  9166. x = landmark[2 * i]
  9167. y = landmark[2 * i + 1]
  9168. img.draw_rectangle(x-2, y-2, x+2, y+2,
  9169. color=(193, 255, 193), thickness=-1)
  9170. def process(self, input):
  9171. global CARDDATABOEX
  9172. self.card_input_img = input
  9173. # retinaface decoder only support chw layout
  9174. loc_out = self.loc_model.forward(input, quantize=1, layout="chw")
  9175. boxes, landmarks = self.loc_decoder.run(loc_out, nms=0.2, score_thresh=0.7, outputs_shape=[
  9176. [1, 4, 2058], [1, 2, 2058], [1, 8, 2058]])
  9177. if len(boxes):
  9178. # print(boxes,landmarks)
  9179. for boxesi, box in enumerate(boxes):
  9180. boxes[boxesi].append(landmarks[boxesi])
  9181. CARDDATABOEX = boxes
  9182. def get_card_data(self, landmark):
  9183. # landmark = i[4][:6]
  9184. reg_in = self.card_input_img.crop_affine(landmark, 94, 24)
  9185. reg_out = self.reg_model.forward(reg_in, quantize=1, layout="chw")
  9186. LP_number = self.reg_decoder.run(reg_out)
  9187. string_LP = ""
  9188. for id in LP_number:
  9189. string_LP += self.chars[id]
  9190. return string_LP
  9191. LPRCARD = LPR()
  9192. `;
  9193. var code = '';
  9194. return code;
  9195. };
  9196. // 车牌识别
  9197. Blockly.Blocks['ai_model_card_recognition_load'] = {
  9198. init: function () {
  9199. this.appendDummyInput()
  9200. .appendField(Blockly.Msg.ai_models_load_title)
  9201. .appendField(Blockly.Msg.ai_models_init_card_model);
  9202. // this.appendDummyInput()
  9203. // .appendField(Blockly.Msg.ai_models_customized_init_setup_source)
  9204. // .appendField(new Blockly.FieldVariable("img_card_recognition"), "varitem");
  9205. this.setInputsInline(false);
  9206. this.setPreviousStatement(true, null);
  9207. this.setNextStatement(true, null);
  9208. this.setColour("#ee783a");
  9209. this.setTooltip(Blockly.Msg.ai_model_customized_init_setup_definition_TOOLTIP);
  9210. this.setHelpUrl("");
  9211. }
  9212. };
  9213. Blockly.Python['ai_model_card_recognition_load'] = function (block) {
  9214. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  9215. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  9216. Blockly.Python.addVariable('CARDDATABOEX', `CARDDATABOEX = ""`, true)
  9217. Blockly.Python.definitions_['v831_card_recognition_init'] = `class LPR:
  9218. loc_model_path = "/root/preset/model/loc.mud"
  9219. reg_model_path = "/root/preset/model/reg.mud"
  9220. chars = ["皖", "沪", "津", "渝", "冀", "晋", "蒙", "辽", "吉", "黑",
  9221. "苏", "浙", "京", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤",
  9222. "桂", "琼", "川", "贵", "云", "藏", "陕", "甘", "青", "宁",
  9223. "新", "警", "学", "A", "B", "C", "D", "E", "F", "G",
  9224. "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S",
  9225. "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3",
  9226. "4", "5", "6", "7", "8", "9", "-"]
  9227. variances = [0.1, 0.2]
  9228. steps = [8, 16, 32]
  9229. min_sizes = [12, 24, 48, 96, 192, 320]
  9230. card_input_img = None
  9231. def __init__(self) -> None:
  9232. self.loc_model = nn.load(self.loc_model_path, opt=None)
  9233. self.reg_model = nn.load(self.reg_model_path, opt=None)
  9234. self.loc_decoder = decoder.license_plate_location(
  9235. [224, 224], self.steps, self.min_sizes, self.variances)
  9236. self.reg_decoder = decoder.CTC((1, 68, 18))
  9237. self.LPRS = {
  9238. "LPR_START_X":[],
  9239. "LPR_START_Y":[],
  9240. "LPR_CENTER_X":[],
  9241. "LPR_CENTER_Y":[],
  9242. "LPR_WIDTH":[],
  9243. "LPR_HEIGHT":[],
  9244. "LPR_CONFIDENCE":[],
  9245. "LPR_RESULT":[],
  9246. }
  9247. def __del__(self):
  9248. del self.loc_model
  9249. del self.loc_decoder
  9250. def cal_fps(self, start, end):
  9251. one_second = 1
  9252. one_flash = end - start
  9253. fps = one_second / one_flash
  9254. return fps
  9255. def draw_fps(self, img, fps):
  9256. img.draw_string(0, 0, "FPS :"+str(fps), scale=1,
  9257. color=(255, 0, 255), thickness=1)
  9258. def draw_string(self, img, x, y, string, color):
  9259. img.draw_string(x, y, string, color=color)
  9260. def draw_paste(self, src, dst):
  9261. src.paste(dst, 0, 0)
  9262. def draw_rectangle(self, img, box):
  9263. img.draw_rectangle(box[0], box[1], box[2], box[3],
  9264. color=(230, 230, 250), thickness=2)
  9265. def draw_point(self, img, landmark):
  9266. for i in range(4):
  9267. x = landmark[2 * i]
  9268. y = landmark[2 * i + 1]
  9269. img.draw_rectangle(x-2, y-2, x+2, y+2,
  9270. color=(193, 255, 193), thickness=-1)
  9271. def process(self, input):
  9272. self.card_input_img = input
  9273. # retinaface decoder only support chw layout
  9274. loc_out = self.loc_model.forward(input, quantize=1, layout="chw")
  9275. 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]])
  9276. self.LPRS = {
  9277. "LPR_START_X":[],
  9278. "LPR_START_Y":[],
  9279. "LPR_CENTER_X":[],
  9280. "LPR_CENTER_Y":[],
  9281. "LPR_WIDTH":[],
  9282. "LPR_HEIGHT":[],
  9283. "LPR_CONFIDENCE":[],
  9284. "LPR_RESULT":[],
  9285. }
  9286. for i,box in enumerate(boxes):
  9287. landmark = landmarks[i][:6]
  9288. reg_in = input.crop_affine(landmark , 94 , 24)
  9289. reg_out = self.reg_model.forward(reg_in , quantize=1, layout = "chw")
  9290. LP_number = self.reg_decoder.run(reg_out)
  9291. string_LP = ''
  9292. for id in LP_number:
  9293. string_LP += self.chars[id]
  9294. # string_LP += str(id)
  9295. input.draw_string(box[0]+5, box[1]+5, string_LP, color=(225,0,0))
  9296. input.draw_rectangle(box[0], box[1], box[2], box[3],color=(250,0,0), thickness=1)
  9297. boxes[i].append(landmarks[i])
  9298. return boxes
  9299. def get_card_data(self, landmark):
  9300. # landmark = i[4][:6]
  9301. reg_in = self.card_input_img.crop_affine(landmark, 94, 24)
  9302. reg_out = self.reg_model.forward(reg_in, quantize=1, layout="chw")
  9303. LP_number = self.reg_decoder.run(reg_out)
  9304. string_LP = ""
  9305. for id in LP_number:
  9306. string_LP += self.chars[id]
  9307. return string_LP
  9308. LPRCARD = LPR()
  9309. `;
  9310. // TODO: Assemble Python into code variable.
  9311. var code = `canvas = canvas.crop(48,8,224,224)
  9312. BOXES = LPRCARD.process(canvas)
  9313. `;
  9314. return code;
  9315. };
  9316. // 车牌识别
  9317. Blockly.Blocks['ai_model_card_xy_new'] = {
  9318. init: function () {
  9319. this.appendDummyInput()
  9320. .appendField(Blockly.Msg.ai_models_card_attribute)
  9321. this.appendStatementInput("input")
  9322. .setCheck(null)
  9323. .appendField(Blockly.Msg.basic_motion_statement_exec);
  9324. this.updateShape_();
  9325. this.setMutator(new Blockly.Mutator(['ai_model_false_create_with_item']));
  9326. this.setColour("#ee783a");
  9327. this.setPreviousStatement(true, null);
  9328. this.setNextStatement(true, null);
  9329. this.setTooltip("");
  9330. this.setHelpUrl("");
  9331. this.itemCount_ = 0;
  9332. },
  9333. mutationToDom: function () {
  9334. if (!this.itemCount_)
  9335. return null;
  9336. var container = document.createElement('mutation');
  9337. this.itemCount_ && container.setAttribute('aielse', 1);
  9338. return container;
  9339. },
  9340. domToMutation: function (a) {
  9341. this.itemCount_ = parseInt(a.getAttribute('aielse'), 10);
  9342. this.updateShape_();
  9343. },
  9344. decompose: function (a) {
  9345. var b = a.newBlock('ai_model_false_create_with_container');
  9346. b.initSvg();
  9347. var c = b.nextConnection
  9348. this.itemCount_ && (a = a.newBlock("ai_model_false_create_with_item"),
  9349. a.initSvg(),
  9350. c.connect(a.previousConnection));
  9351. console.log("this.elseCount_", a)
  9352. return b
  9353. },
  9354. compose: function (a) {
  9355. var b = a.nextConnection.targetBlock();
  9356. this.itemCount_ = 0;
  9357. var e = null;
  9358. if (b && b.type === "ai_model_false_create_with_item") {
  9359. this.itemCount_++;
  9360. e = b.statementConnection_;
  9361. b = b.nextConnection && b.nextConnection.targetBlock()
  9362. }
  9363. this.updateShape_()
  9364. Blockly.Mutator.reconnect(e, this, "AIELSE")
  9365. },
  9366. saveConnections: function (a) {
  9367. a = a.nextConnection.targetBlock();
  9368. if (a && a.type === "ai_model_false_create_with_item") {
  9369. e = this.getInput("AIELSE");
  9370. a.statementConnection_ = e && e.connection.targetConnection;
  9371. a = a.nextConnection && a.nextConnection.targetBlock()
  9372. }
  9373. },
  9374. updateShape_: function () {
  9375. this.getInput("AIELSE") && this.removeInput("AIELSE");
  9376. console.log("this.itemCount_", this.itemCount_)
  9377. this.itemCount_ && this.appendStatementInput("AIELSE").appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSE)
  9378. },
  9379. };
  9380. Blockly.Python['ai_model_card_xy_new'] = function (block) {
  9381. var statements_input = Blockly.Python.statementToCode(block, 'input');
  9382. var statements_input1 = Blockly.Python.statementToCode(block, 'AIELSE');
  9383. let allBlocks = block.workspace.getAllBlocks();
  9384. let global = ""
  9385. try {
  9386. global = allBlocks[0].workspace.variableList.toString()
  9387. }
  9388. catch (e) {
  9389. console.log(e)
  9390. }
  9391. Blockly.Python.addFunction("cardStatements", `def cardStatements(BOXESI):
  9392. ${global != "" ? `global ${global}` : "pass"}
  9393. ${statements_input}
  9394. `)
  9395. var code = `if len(BOXES) > 0:
  9396. for BOXESI in BOXES:
  9397. cardStatements(BOXESI)
  9398. else:
  9399. ${statements_input1 != "" ? statements_input1 : " pass"}
  9400. `
  9401. return code;
  9402. };
  9403. Blockly.Blocks['ai_model_card_get'] = {
  9404. init: function () {
  9405. this.appendDummyInput()
  9406. .appendField(new Blockly.FieldImage("blockly/media/object_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  9407. this.appendDummyInput()
  9408. .appendField(Blockly.Msg.ai_models_from)
  9409. .appendField(Blockly.Msg.ai_models_card_model_get)
  9410. .appendField(new Blockly.FieldDropdown([
  9411. [Blockly.Msg.ai_models_init_card_result, "Object"],
  9412. [Blockly.Msg.ai_models_object_model_x, "X"],
  9413. [Blockly.Msg.ai_models_object_model_y, "Y"],
  9414. // [Blockly.Msg.ai_models_object_model_w, "W"],
  9415. // [Blockly.Msg.ai_models_object_model_h, "H"],
  9416. // [Blockly.Msg.ai_models_object_model_x_center, "CX"],
  9417. // [Blockly.Msg.ai_models_object_model_y_center, "CY"],
  9418. // [Blockly.Msg.ai_models_object_model_count, "Number"],
  9419. // [Blockly.Msg.ai_models_object_model_index, "Serial"],
  9420. // [Blockly.Msg.ai_models_object_model_confidence, "Confidence"],
  9421. [Blockly.Msg.ai_models_face_model_ws, "2"],
  9422. [Blockly.Msg.ai_models_face_model_hs, "3"],
  9423. [Blockly.Msg.ai_models_object_model_x_center, "CX"],
  9424. [Blockly.Msg.ai_models_object_model_y_center, "CY"]
  9425. ]), "TYPE");
  9426. this.setInputsInline(false);
  9427. this.setOutput(true, null);
  9428. this.setColour("#ee783a");
  9429. var thisBlock = this;
  9430. this.setTooltip(Blockly.Msg.ai_models_guesswork_model_get);
  9431. this.setHelpUrl("");
  9432. }
  9433. };
  9434. Blockly.Python.ai_model_card_get = function (block) {
  9435. var type = block.getFieldValue('TYPE');
  9436. var variable_name = "BOXESI";
  9437. var _code = "";
  9438. if (type == "Object") {
  9439. _code = `LPRCARD.get_card_data(${variable_name}[4][:6])`
  9440. // _code = `"{}:{:.2f}%".format(labels[probs[${variable_name}][0]], probs[${variable_name}][1][probs[${variable_name}][0]]*100)`
  9441. } else if (type == "X") {
  9442. _code = '' + variable_name + '[0]';
  9443. } else if (type == "Y") {
  9444. _code = '' + variable_name + '[1]';
  9445. } else if (type == "W") {
  9446. _code = 'int(' + variable_name + '[2])';
  9447. } else if (type == "H") {
  9448. _code = 'int(' + variable_name + '[3])';
  9449. } else if (type == "Confidence") {
  9450. _code = `${variable_name}[4][1][${variable_name}[4][0]]*100`;
  9451. } else if (type == "CX") {
  9452. _code = `int((${variable_name}[0] + ${variable_name}[2])/2)`;
  9453. } else if (type == "CY") {
  9454. _code = `int((${variable_name}[1] + ${variable_name}[3])/2)`;
  9455. }
  9456. else {
  9457. _code = `${variable_name}[${type}]-${variable_name}[${type - 2}]`;
  9458. }
  9459. // }
  9460. return [_code, Blockly.Python.ORDER_ATOMIC];
  9461. }
  9462. //
  9463. // 车牌识别
  9464. Blockly.Blocks['ai_model_card_xy'] = {
  9465. init: function () {
  9466. this.appendDummyInput()
  9467. .appendField(Blockly.Msg.ai_models_card_if_true);
  9468. this.setInputsInline(false);
  9469. this.setNextStatement(true, null);
  9470. this.setPreviousStatement(true, null);
  9471. this.setColour("#ee783a");
  9472. this.setTooltip(Blockly.Msg.ai_models_card_if_true);
  9473. this.setHelpUrl("");
  9474. }
  9475. };
  9476. Blockly.Python['ai_model_card_xy'] = function (block) {
  9477. Blockly.Python.addFunction("cardShowRecognize", `def cardShowRecognize():
  9478. global CARDDATABOEX
  9479. if len(CARDDATABOEX):
  9480. for CARDDATABOEXI in CARDDATABOEX:
  9481. 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)
  9482. canvas.draw_string(CARDDATABOEXI[0],CARDDATABOEXI[1], (str(LPRCARD.get_card_data(CARDDATABOEXI[4][:6]))), scale = 1, color = (255,255,255) , thickness = 1)
  9483. `)
  9484. var code = 'cardShowRecognize()\n';
  9485. return code;
  9486. };
  9487. Blockly.Blocks['ai_model_card_result'] = {
  9488. init: function () {
  9489. this.appendDummyInput()
  9490. .appendField(Blockly.Msg.ai_models_card_attribute);
  9491. this.setInputsInline(false);
  9492. this.setOutput(true, null);
  9493. this.setColour("#ee783a");
  9494. this.setTooltip(Blockly.Msg.ai_models_card_attribute);
  9495. this.setHelpUrl("");
  9496. }
  9497. };
  9498. Blockly.Python['ai_model_card_result'] = function (block) {
  9499. var code = 'CARDDATABOEX';
  9500. return [code, Blockly.Python.ORDER_ATOMIC];
  9501. };
  9502. Blockly.Blocks['ai_model_face_recognition_init_new'] = {
  9503. init: function () {
  9504. this.appendDummyInput().appendField(Blockly.Msg.ai_models_init_title)
  9505. .appendField('人脸识别模型');
  9506. this.setPreviousStatement(true, null);
  9507. this.setNextStatement(true, null);
  9508. this.setColour("#ee783a");
  9509. this.setTooltip(Blockly.Msg.ai_models_init_title + Blockly.Msg.ai_models_init_face_model);
  9510. this.setHelpUrl("");
  9511. }
  9512. };
  9513. Blockly.Python.ai_model_face_recognition_init_new = function (block) {
  9514. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  9515. Blockly.Python.definitions_['v831_import_FaceRecognize'] = `from maix.nn.app.face import FaceRecognize`
  9516. Blockly.Python.definitions_.import_face_recognition_init_new = `
  9517. class Face_recognize:
  9518. score_threshold = 70 #识别分数阈值
  9519. input_size = (224, 224, 3) #输入图片尺寸
  9520. input_size_fe = (128, 128, 3) #输入人脸数据
  9521. feature_len = 256 #人脸数据宽度
  9522. steps = [8, 16, 32] #
  9523. channel_num = 0 #通道数量
  9524. users = [] #初始化用户列表
  9525. threshold = 0.5 #人脸阈值
  9526. nms = 0.3
  9527. max_face_num = 3 #输出的画面中的人脸的最大个数
  9528. 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"] #人脸标签定义
  9529. model = {
  9530. "param": "/root/preset/model/model_int8.param",
  9531. "bin": "/root/preset/model/model_int8.bin"
  9532. }
  9533. model_fe = {
  9534. "param": "/root/preset/model/fe_res18_117.param",
  9535. "bin": "/root/preset/model/fe_res18_117.bin"
  9536. }
  9537. def __init__(self):
  9538. for i in range(len(self.steps)):
  9539. self.channel_num += self.input_size[1] / self.steps[i] * (self.input_size[0] / self.steps[i]) * 2
  9540. self.channel_num = int(self.channel_num) #统计通道数量
  9541. self.options = { #准备人脸输出参数
  9542. "model_type": "awnn",
  9543. "inputs": {
  9544. "input0": self.input_size
  9545. },
  9546. "outputs": {
  9547. "output0": (1, 4, self.channel_num) ,
  9548. "431": (1, 2, self.channel_num) ,
  9549. "output2": (1, 10, self.channel_num)
  9550. },
  9551. "mean": [127.5, 127.5, 127.5],
  9552. "norm": [0.0078125, 0.0078125, 0.0078125],
  9553. }
  9554. self.options_fe = { #准备特征提取参数
  9555. "model_type": "awnn",
  9556. "inputs": {
  9557. "inputs_blob": self.input_size_fe
  9558. },
  9559. "outputs": {
  9560. "FC_blob": (1, 1, self.feature_len)
  9561. },
  9562. "mean": [127.5, 127.5, 127.5],
  9563. "norm": [0.0078125, 0.0078125, 0.0078125],
  9564. }
  9565. print("-- load model:", self.model)
  9566. self.m = nn.load(self.model, opt=self.options)
  9567. print("-- load ok")
  9568. print("-- load model:", self.model_fe)
  9569. self.m_fe = nn.load(self.model_fe, opt=self.options_fe)
  9570. print("-- load ok")
  9571. self.face_recognizer = FaceRecognize(self.m, self.m_fe, self.feature_len, self.input_size, self.threshold, self.nms, self.max_face_num)
  9572. def map_face(self,box,points): #将224*224空间的位置转换到240*240或320*240空间内
  9573. from maix import display # print(box,points)
  9574. if display.width() == display.height():
  9575. def tran(x):
  9576. return int(x/224*display.width())
  9577. box = list(map(tran, box))
  9578. def tran_p(p):
  9579. return list(map(tran, p))
  9580. points = list(map(tran_p, points))
  9581. else:
  9582. # 168x224(320x240) > 224x224(240x240) > 320x240
  9583. s = (224*display.height()/display.width()) # 168x224
  9584. w, h, c = display.width()/224, display.height()/224, 224/s
  9585. t, d = c*h, (224 - s) // 2 # d = 224 - s // 2 == 28
  9586. 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)
  9587. def tran_p(p):
  9588. return [int(p[0]*w), int((p[1]-d)*t)] # 224 - 168 / 2 = 28 so 168 / (old_h - 28) = 240 / new_h
  9589. points = list(map(tran_p, points))
  9590. # print(box,points)
  9591. return box,points
  9592. def recognize(self, feature): #进行人脸匹配
  9593. def _compare(user): #定义映射函数
  9594. return self.face_recognizer.compare(user, feature) #推测匹配分数 score相关分数
  9595. face_score_l = list(map(_compare,self.users)) #映射特征数据在记录中的比对分数
  9596. return max(enumerate(face_score_l), key=lambda x: x[-1]) #提取出人脸分数最大值和最大值所在的位置
  9597. def __del__(self):
  9598. del self.face_recognizer
  9599. del self.m_fe
  9600. del self.m
  9601. face_recognizer = Face_recognize()
  9602. `;
  9603. // var degee = block.getFieldValue('DEGREE');
  9604. var _code = "";
  9605. // Blockly.Python.addVariable('task_facerecognition', 'task_facerecognition = None', true);
  9606. return [_code, Blockly.Python.ORDER_ATOMIC];
  9607. }
  9608. Blockly.Blocks['ai_model_face_recognition_load_new'] = {
  9609. init: function () {
  9610. this.appendDummyInput()
  9611. .appendField(Blockly.Msg.ai_models_load_title)
  9612. .appendField(Blockly.Msg.ai_models_init_face_model);
  9613. this.appendDummyInput()
  9614. .appendField(Blockly.Msg.ai_models_init_source)
  9615. .appendField(new Blockly.FieldVariable("img_facerecognition"), "varitem");
  9616. this.setPreviousStatement(true, null);
  9617. this.setNextStatement(true, null);
  9618. this.setColour("#ee783a");
  9619. var thisBlock = this;
  9620. this.setTooltip(function () {
  9621. var mode = thisBlock.getFieldValue('DEGREE');
  9622. var TOOLTIPS = {
  9623. 'Human Face Recognition': Blockly.Msg.ai_model_allload_TOOLTIP.replace('%1', Blockly.Msg.ai_models_init_face_model)
  9624. };
  9625. return TOOLTIPS[mode];
  9626. });
  9627. this.setHelpUrl('');
  9628. }
  9629. };
  9630. Blockly.Python.ai_model_face_recognition_load_new = function (block) {
  9631. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  9632. // var camera_variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('camera_varitem'), Blockly.Variables.NAME_TYPE);
  9633. // Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
  9634. // var degee = block.getFieldValue('DEGREE');
  9635. var _code = `facesRecognitionLoadNew = face_recognizer.face_recognizer.get_faces(AI_img.tobytes(),False) #提取人脸特征信息
  9636. `;
  9637. // Blockly.Python.addVariable('code_facerecognition', '', true);
  9638. // _code += "code_facerecognition = kpu.run_yolo2(task_facerecognition, " + variable_name + ")\n";
  9639. // _code += variable_name + ".ai_to_pix()\n";
  9640. return _code;
  9641. }
  9642. Blockly.Blocks['ai_model_xy_new'] = {
  9643. init: function () {
  9644. this.appendDummyInput()
  9645. .appendField(new Blockly.FieldImage("blockly/media/face_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  9646. this.appendDummyInput()
  9647. .appendField(Blockly.Msg.ai_models_from)
  9648. .appendField(new Blockly.FieldVariable("i"), "varitem")
  9649. .appendField(Blockly.Msg.ai_models_face_model_get)
  9650. .appendField(new Blockly.FieldDropdown([
  9651. [Blockly.Msg.ai_models_face_model_x, "X"],
  9652. [Blockly.Msg.ai_models_face_model_y, "Y"],
  9653. [Blockly.Msg.ai_models_face_model_w, "W"],
  9654. [Blockly.Msg.ai_models_face_model_h, "H"],
  9655. [Blockly.Msg.ai_models_face_model_x_center, "CX"],
  9656. [Blockly.Msg.ai_models_face_model_y_center, "CY"],
  9657. [Blockly.Msg.ai_models_face_model_count, "Number"],
  9658. [Blockly.Msg.ai_models_face_model_index, "Serial"],
  9659. [Blockly.Msg.ai_models_face_model_confidence, "Confidence"]
  9660. ]), "TYPE");
  9661. this.setInputsInline(false);
  9662. this.setOutput(true, null);
  9663. this.setColour("#ee783a");
  9664. var thisBlock = this;
  9665. this.setTooltip(function () {
  9666. var mode = thisBlock.getFieldValue('TYPE');
  9667. var TOOLTIPS = {
  9668. 'X': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x),
  9669. 'Y': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y),
  9670. 'W': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_w),
  9671. 'H': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_h),
  9672. 'CX': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x_center),
  9673. 'CY': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y_center),
  9674. 'Number': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_count),
  9675. 'Serial': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_index),
  9676. 'Confidence': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_confidence)
  9677. };
  9678. return TOOLTIPS[mode];
  9679. });
  9680. this.setHelpUrl("");
  9681. }
  9682. };
  9683. Blockly.Python.ai_model_xy_new = function (block) {
  9684. var type = block.getFieldValue('TYPE');
  9685. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  9686. var _code = "";
  9687. if (type == "X") {
  9688. _code = variable_name + "[0]";
  9689. } else if (type == "Y") {
  9690. _code = variable_name + "[1]";
  9691. } else if (type == "W") {
  9692. _code = variable_name + "[0] + " + variable_name + "[2]";
  9693. } else if (type == "H") {
  9694. _code = variable_name + "[1] + " + variable_name + "[3]";
  9695. }
  9696. // else if (type == "Number") {
  9697. // _code = variable_name + ".objnum()";
  9698. // } else if (type == "Serial") {
  9699. // _code = variable_name + ".index()";
  9700. // } else if (type == "Confidence") {
  9701. // _code = variable_name + ".value()";
  9702. // } else if (type == "CX") {
  9703. // _code = variable_name + ".x()+(" + variable_name + ".w()/2)";
  9704. // } else if (type == "CY") {
  9705. // _code = variable_name + ".y()+(" + variable_name + ".h()/2)";
  9706. // }
  9707. return [_code, Blockly.Python.ORDER_ATOMIC];
  9708. };
  9709. Blockly.Blocks['ai_model_face_attribute_new'] = {
  9710. init: function () {
  9711. this.appendDummyInput()
  9712. .appendField(Blockly.Msg.ai_model_face_attribute);
  9713. this.setOutput(true, null);
  9714. this.setColour("#ee783a");
  9715. this.setTooltip(Blockly.Msg.ai_model_face_attribute_TOOLTIP);
  9716. this.setHelpUrl("");
  9717. }
  9718. };
  9719. Blockly.Python['ai_model_face_attribute_new'] = function (block) {
  9720. var code = 'facesRecognitionLoadNew';
  9721. return [code, Blockly.Python.ORDER_NONE];
  9722. };
  9723. Blockly.Blocks['face_recognition_load'] = {
  9724. init: function () {
  9725. this.appendDummyInput()
  9726. .appendField(Blockly.Msg.ai_model_face_name);
  9727. this.appendDummyInput()
  9728. .appendField(Blockly.Msg.v831_face_recognition_load);
  9729. // this.appendDummyInput()
  9730. // .appendField(Blockly.Msg.ai_models_init_source)
  9731. // .appendField(new Blockly.FieldVariable("img_recognized_face"), "varitem");
  9732. this.appendValueInput("class_input")
  9733. .setCheck(null)
  9734. .appendField(Blockly.Msg.LISTS_CREATE_WITH_CONTAINER_TITLE_ADD + ":")
  9735. // .appendField(new Blockly.FieldVariable("people_name_list"), "people_name");
  9736. this.setInputsInline(false);
  9737. this.setPreviousStatement(true, null);
  9738. this.setNextStatement(true, null);
  9739. this.setColour("#ee783a");
  9740. this.setTooltip(Blockly.Msg.v831_face_recognition_load_title);
  9741. this.setHelpUrl("");
  9742. }
  9743. };
  9744. Blockly.Python['face_recognition_load'] = function (block) {
  9745. // var face_list = Blockly.Python.variableDB_.getName(block.getFieldValue('people_name'), Blockly.Variables.NAME_TYPE);
  9746. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  9747. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  9748. Blockly.Python.definitions_['v831_import_from_maix_nn_face'] = `from maix.nn.app import face`
  9749. Blockly.Python.definitions_['v831_import_from_maix_nn_FaceRecognize'] = `from maix.nn.app.face import FaceRecognize`
  9750. Blockly.Python.addVariable('NUMBERPERSON', 'NUMBERPERSON = 0', true);
  9751. Blockly.Python.definitions_.import_Face_Recognizer = `class Face_Recognizer:
  9752. max_face_num = 4
  9753. detect_threshold = 0.5
  9754. detect_nms = 0.3
  9755. FEATURES = []
  9756. def __init__(self, threshold = 0.5, nms = 0.3, max_face_num = 1):
  9757. model = "/root/preset/model/retinaface.mud"
  9758. model_fe = "/root/preset/model/fe_resnet.mud"
  9759. self.input_size = (224, 224, 3)
  9760. input_size_fe = (128, 128, 3)
  9761. self.feature_len = 256
  9762. self.features = []
  9763. m = nn.load(model)
  9764. m_fe = nn.load(model_fe)
  9765. self.isLoadRecoredFace = False
  9766. self.CLASSNAMEFACELIST = ${value_class_input}
  9767. self.recognizer = FaceRecognize(m, m_fe, self.feature_len, self.input_size, threshold, nms, max_face_num)
  9768. self.FACERECOGNIZES = {
  9769. }
  9770. self.FACESRECOGNITONRESULT = []
  9771. def get_faces(self, img, std_img = False):
  9772. faces = self.recognizer.get_faces(img, std_img)
  9773. return faces
  9774. def __len__(self):
  9775. return len(self.features)
  9776. def add_user(self, name, feature):
  9777. self.features.append([name, feature])
  9778. return True
  9779. def remove_user(self, name_del):
  9780. rm = None
  9781. for name, feature in self.features:
  9782. if name_del == name:
  9783. rm = [name, feature]
  9784. if rm:
  9785. self.features.remove(rm)
  9786. return True
  9787. return False
  9788. def recognize(self, feature):
  9789. max_score = 0
  9790. uid = -1
  9791. for i, user in enumerate(self.features):
  9792. score = self.recognizer.compare(user[1], feature)
  9793. if score > max_score:
  9794. max_score = score
  9795. uid = i
  9796. if uid >= 0:
  9797. return self.features[uid][0], max_score
  9798. return None, 0
  9799. def get_input_size(self):
  9800. return self.input_size
  9801. def get_feature_len(self):
  9802. return self.feature_len
  9803. def darw_info(self, img, box, points, disp_str, bg_color=(255, 0, 0, 255), font_color=(255, 255, 255, 255), font_size=32):
  9804. font_wh = image.get_string_size(disp_str)
  9805. for p in points:
  9806. img.draw_rectangle(p[0] - 1, p[1] -1, p[0] + 1, p[1] + 1, color=bg_color)
  9807. img.draw_rectangle(box[0], box[1], box[0] + box[2], box[1] + box[3], color=bg_color, thickness=2)
  9808. if disp_str:
  9809. img.draw_rectangle(box[0], box[1] - font_wh[1], box[0] + font_wh[0], box[1], color=bg_color, thickness = -1)
  9810. img.draw_string(box[0], box[1] - font_wh[1], disp_str, color=font_color)
  9811. def map_face(self,box,points,IMAGEFACESHOW):
  9812. # print(box,points)
  9813. for p in points:
  9814. IMAGEFACESHOW.draw_rectangle(p[0] - 1, p[1] -1, p[0] + 1, p[1] + 1, color=(255,200,255))
  9815. #return box,points
  9816. def darw_title(self, img, dis_size ,key_l = None, key_r =None):
  9817. if key_C:
  9818. key_l = "| "+ key_l
  9819. img.draw_string( 1, 2 ,key_l , scale = 1, color = (255, 255, 255), thickness = 2)
  9820. if key_D:
  9821. key_r = key_r+" |"
  9822. w = int(dis_size[0] - 4 - image.get_string_size(key_r)[0] * 1)
  9823. img.draw_string( w, 2 ,key_r , scale = 1, color = (255, 255, 255), thickness = 2)
  9824. def recognizeShowMessage(self, canvas):
  9825. self.FACESRECOGNITONRESULT = FACERECGNIZER.get_faces(canvas)
  9826. if len(self.FACESRECOGNITONRESULT):
  9827. for FACESRECOGNITONRESULTI in self.FACESRECOGNITONRESULT:
  9828. if self.isLoadRecoredFace:
  9829. if int(FACERECGNIZER.recognize(FACESRECOGNITONRESULTI[3])[1])>85:
  9830. 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)
  9831. 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)
  9832. else:
  9833. 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)
  9834. 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)
  9835. else:
  9836. FACERECGNIZER.map_face(FACESRECOGNITONRESULTI[1],FACESRECOGNITONRESULTI[2],canvas)
  9837. 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)
  9838. FACERECGNIZER = Face_Recognizer(0.5, 0.3, max_face_num = 4)
  9839. `
  9840. var code = `canvas = canvas.crop(48, 8,224, 224)
  9841. FACERECGNIZER.recognizeShowMessage(canvas)
  9842. `;
  9843. return code;
  9844. };
  9845. Blockly.Blocks['face_recognition_if_face_new'] = {
  9846. init: function () {
  9847. this.appendDummyInput()
  9848. .appendField(Blockly.Msg.ai_model_face_attribute_new)
  9849. this.appendStatementInput("input")
  9850. .setCheck(null)
  9851. .appendField(Blockly.Msg.basic_motion_statement_exec);
  9852. this.updateShape_();
  9853. this.setMutator(new Blockly.Mutator(['ai_model_false_create_with_item']));
  9854. this.setColour("#ee783a");
  9855. this.setPreviousStatement(true, null);
  9856. this.setNextStatement(true, null);
  9857. this.setTooltip("");
  9858. this.setHelpUrl("");
  9859. this.itemCount_ = 0;
  9860. },
  9861. mutationToDom: function () {
  9862. if (!this.itemCount_)
  9863. return null;
  9864. var container = document.createElement('mutation');
  9865. this.itemCount_ && container.setAttribute('aielse', 1);
  9866. return container;
  9867. },
  9868. domToMutation: function (a) {
  9869. this.itemCount_ = parseInt(a.getAttribute('aielse'), 10);
  9870. this.updateShape_();
  9871. },
  9872. decompose: function (a) {
  9873. var b = a.newBlock('ai_model_false_create_with_container');
  9874. b.initSvg();
  9875. var c = b.nextConnection
  9876. this.itemCount_ && (a = a.newBlock("ai_model_false_create_with_item"),
  9877. a.initSvg(),
  9878. c.connect(a.previousConnection));
  9879. console.log("this.elseCount_",a)
  9880. return b
  9881. },
  9882. compose: function (a) {
  9883. var b = a.nextConnection.targetBlock();
  9884. this.itemCount_ = 0;
  9885. var e = null;
  9886. if (b && b.type === "ai_model_false_create_with_item") {
  9887. this.itemCount_++;
  9888. e = b.statementConnection_;
  9889. b = b.nextConnection && b.nextConnection.targetBlock()
  9890. }
  9891. this.updateShape_()
  9892. Blockly.Mutator.reconnect(e, this, "AIELSE")
  9893. },
  9894. saveConnections: function (a) {
  9895. a = a.nextConnection.targetBlock();
  9896. if (a && a.type === "ai_model_false_create_with_item") {
  9897. e = this.getInput("AIELSE");
  9898. a.statementConnection_ = e && e.connection.targetConnection;
  9899. a = a.nextConnection && a.nextConnection.targetBlock()
  9900. }
  9901. },
  9902. updateShape_: function () {
  9903. this.getInput("AIELSE") && this.removeInput("AIELSE");
  9904. console.log("this.itemCount_",this.itemCount_)
  9905. this.itemCount_ && this.appendStatementInput("AIELSE").appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSE)
  9906. },
  9907. };
  9908. Blockly.Python['face_recognition_if_face_new'] = function (block) {
  9909. var statements_input = Blockly.Python.statementToCode(block, 'input');
  9910. var statements_input1 = Blockly.Python.statementToCode(block, 'AIELSE');
  9911. let allBlocks = block.workspace.getAllBlocks();
  9912. let global = ""
  9913. try {
  9914. global = allBlocks[0].workspace.variableList.toString()
  9915. }
  9916. catch (e) {
  9917. console.log(e)
  9918. }
  9919. Blockly.Python.addFunction("recognitionStatements", `def recognitionStatements(BOXESI):
  9920. ${global != "" ? `global ${global}` : "pass"}
  9921. ${statements_input}
  9922. `)
  9923. var code = `if len(FACERECGNIZER.FACESRECOGNITONRESULT) > 0:
  9924. for BOXESI in FACERECGNIZER.FACESRECOGNITONRESULT:
  9925. recognitionStatements(BOXESI)
  9926. else:
  9927. ${statements_input1 != "" ? statements_input1 : " pass"}
  9928. `
  9929. return code;
  9930. };
  9931. Blockly.Blocks['face_recognition_if_face'] = {
  9932. init: function () {
  9933. this.appendDummyInput()
  9934. .appendField(Blockly.Msg.ai_models_face_model_if_new);
  9935. this.setInputsInline(false);
  9936. this.setNextStatement(true, null);
  9937. this.setPreviousStatement(true, null);
  9938. this.setColour("#ee783a");
  9939. this.setTooltip(Blockly.Msg.ai_models_face_model_if_new);
  9940. this.setHelpUrl("");
  9941. }
  9942. };
  9943. Blockly.Python['face_recognition_if_face'] = function (block) {
  9944. var code = ``;
  9945. return code;
  9946. };
  9947. Blockly.Blocks['face_recognition_face_result'] = {
  9948. init: function () {
  9949. this.appendDummyInput()
  9950. .appendField(Blockly.Msg.ai_model_face_attribute_new);
  9951. this.setInputsInline(false);
  9952. this.setOutput(true, null);
  9953. this.setColour("#ee783a");
  9954. this.setTooltip(Blockly.Msg.ai_model_face_attribute_new);
  9955. this.setHelpUrl("");
  9956. }
  9957. };
  9958. Blockly.Python['face_recognition_face_result'] = function (block) {
  9959. var code = `FACESRECOGNITONRESULT`;
  9960. return [code, Blockly.Python.ORDER_ATOMIC];
  9961. };
  9962. Blockly.Blocks['face_recognition_detection_rectangle'] = {
  9963. init: function () {
  9964. this.appendDummyInput()
  9965. .appendField(Blockly.Msg.ai_models_face_model_get)
  9966. .appendField(new Blockly.FieldDropdown([
  9967. // [Blockly.Msg.face_information, "rect"],
  9968. [Blockly.Msg.ai_models_face_model_x, "X"],
  9969. [Blockly.Msg.ai_models_face_model_y, "Y"],
  9970. [Blockly.Msg.ai_models_face_model_confidence_1, "Confidence"],
  9971. [Blockly.Msg.ai_models_face_model_ws, "2"],
  9972. [Blockly.Msg.ai_models_face_model_hs, "3"],
  9973. [Blockly.Msg.ai_models_object_model_x_center, "CX"],
  9974. [Blockly.Msg.ai_models_object_model_y_center, "CY"],
  9975. [Blockly.Msg.v831_model_face_name, "0"],
  9976. [Blockly.Msg.v831_models_face_model_confidence, "1"],
  9977. ]), "TYPE");
  9978. this.setInputsInline(false);
  9979. this.setOutput(true, null);
  9980. this.setColour("#ee783a");
  9981. var thisBlock = this;
  9982. this.setTooltip(function () {
  9983. var mode = thisBlock.getFieldValue('TYPE');
  9984. var TOOLTIPS = {
  9985. 'rect': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_rect),
  9986. 'X': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x),
  9987. 'Y': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y),
  9988. 'W': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_w),
  9989. 'H': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_h),
  9990. 'CX': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x_center),
  9991. 'CY': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y_center),
  9992. 'Number': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_count),
  9993. 'Serial': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_index),
  9994. 'Confidence': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_confidence)
  9995. };
  9996. return TOOLTIPS[mode];
  9997. });
  9998. this.setHelpUrl("");
  9999. }
  10000. };
  10001. Blockly.Python.face_recognition_detection_rectangle = function (block) {
  10002. var type = block.getFieldValue('TYPE');
  10003. var _code = "";
  10004. if (type == "rect") {
  10005. _code = "FACESRECOGNITONRESULTI[3]";
  10006. } else if (type == "X") {
  10007. _code = "FACESRECOGNITONRESULTI[1][0]";
  10008. } else if (type == "Y") {
  10009. _code = "FACESRECOGNITONRESULTI[1][1]";
  10010. } else if (type == "W") {
  10011. _code = `int(FACESRECOGNITONRESULTI[1][2]+FACESRECOGNITONRESULTI[1][0])`;
  10012. } else if (type == "H") {
  10013. _code = `int(FACESRECOGNITONRESULTI[1][3]+FACESRECOGNITONRESULTI[1][1])`;
  10014. } else if (type == "Number") {
  10015. _code = "FACESRECOGNITONRESULTI.objnum()";
  10016. } else if (type == "Serial") {
  10017. _code = "FACESRECOGNITONRESULTI.index()";
  10018. } else if (type == "Confidence") {
  10019. _code = "FACESRECOGNITONRESULTI[0]";
  10020. } else if (type == "CX") {
  10021. _code = `int((FACESRECOGNITONRESULTI[1][2]+FACESRECOGNITONRESULTI[1][0]+FACESRECOGNITONRESULTI[1][0])/2)`;
  10022. } else if (type == "CY") {
  10023. _code = `int((FACESRECOGNITONRESULTI[1][3]+FACESRECOGNITONRESULTI[1][1]+FACESRECOGNITONRESULTI[1][1])/2)`;
  10024. } else if (type == "0" || type == "1") {
  10025. _code = `FACERECGNIZER.recognize(FACESRECOGNITONRESULTI[3])[${type}]`
  10026. }
  10027. else {
  10028. _code = `FACESRECOGNITONRESULTI[1][${type}]`;
  10029. }
  10030. return [_code, Blockly.Python.ORDER_ATOMIC];
  10031. };
  10032. Blockly.Blocks['face_recognition_map_face'] = {
  10033. init: function () {
  10034. this.appendDummyInput()
  10035. .appendField(Blockly.Msg.face_recognition_map_face_canvas)
  10036. .appendField(new Blockly.FieldVariable("img_recognized_face"), "varitem")
  10037. .appendField(Blockly.Msg.face_recognition_map_face_show)
  10038. .appendField(Blockly.Msg.face_recognition_map_face_key_point);
  10039. // this.setInputsInline(false);
  10040. this.setPreviousStatement(true, null);
  10041. this.setNextStatement(true, null);
  10042. this.setColour("#ee783a");
  10043. this.setTooltip(Blockly.Msg.v831_setting_face_data_save_title);
  10044. this.setHelpUrl("");
  10045. }
  10046. };
  10047. Blockly.Python['face_recognition_map_face'] = function (block) {
  10048. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  10049. // var variable1 = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem1'), Blockly.Variables.NAME_TYPE);
  10050. var code = `FACERECGNIZER.map_face(FACESRECOGNITONRESULTI[1],FACESRECOGNITONRESULTI[2],${variable_name})
  10051. `;
  10052. return code;
  10053. };
  10054. Blockly.Blocks['face_recognition_add'] = {
  10055. init: function () {
  10056. this.appendDummyInput()
  10057. .appendField(Blockly.Msg.CocoRobo_EVENT_ACTION_OPT1)
  10058. .appendField(new Blockly.FieldDropdown([
  10059. ["A", "A"],
  10060. ["B", "B"],
  10061. ["C", "C"],
  10062. ["D", "D"]
  10063. ]), "button_type")
  10064. .appendField(Blockly.Msg.ai_model_face_gather_1);
  10065. this.setInputsInline(false);
  10066. this.setPreviousStatement(true, null);
  10067. this.setNextStatement(true, null);
  10068. this.setColour("#ee783a");
  10069. this.setTooltip(Blockly.Msg.v831_setting_face_data_save_title);
  10070. this.setHelpUrl("");
  10071. }
  10072. };
  10073. Blockly.Python['face_recognition_add'] = function (block) {
  10074. var _type = block.getFieldValue('button_type');
  10075. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  10076. sys.path.append("/root/")`
  10077. Blockly.Python.definitions_['v831_import_CocoPi_BUTTON'] = `from CocoPi import BUTTON`
  10078. // Blockly.Python.definitions_['v831_import_CocoPi'] = `sys.path.append("/root/")`
  10079. Blockly.Python.definitions_['import_define_x_v831_button'] = `key_A = BUTTON(14)
  10080. key_B = BUTTON(8)
  10081. key_C = BUTTON(13)
  10082. key_D = BUTTON(7)
  10083. `
  10084. // var code = `key_${_type}.is_pressed()`
  10085. Blockly.Python.definitions_['v831_import_os'] = `import os`
  10086. Blockly.Python.definitions_['v831_import_json'] = `import json`
  10087. Blockly.Python.addVariable('ai_model_face_gather', `
  10088. def _CREATE_TEXT_FILE_WITH_CONTENT(_path, _data, _sep):
  10089. f = open(_path, "a")
  10090. f.write(_data + _sep)
  10091. f.close()
  10092. `, true);
  10093. var language = localStorage.getItem("handPyLanguage");
  10094. let saveFace = ""
  10095. if (language == "en") {
  10096. saveFace = "Save Face"
  10097. } else if (language == "zh-hant") {
  10098. saveFace = "儲存人臉"
  10099. } else {
  10100. saveFace = "保存人脸"
  10101. }
  10102. var code = `if key_${_type}.is_pressed():
  10103. while not (key_${_type}.is_pressed() == False):
  10104. time.sleep(0.1)
  10105. if NUMBERPERSON < len(FACERECGNIZER.CLASSNAMEFACELIST):
  10106. NUMBERPERSON = NUMBERPERSON + 1
  10107. FACERECGNIZER.add_user(FACERECGNIZER.CLASSNAMEFACELIST[len(FACERECGNIZER)], FACESRECOGNITONRESULTI[3])
  10108. FEATURES = FACERECGNIZER.features
  10109. try:
  10110. os.remove("/root/user/model/recorded_face_features.py")
  10111. except:
  10112. pass
  10113. try:
  10114. _CREATE_TEXT_FILE_WITH_CONTENT("/root/user/model/recorded_face_features.py", json.dumps(FACERECGNIZER.features), "\\r\\n")
  10115. except:
  10116. pass
  10117. canvas.draw_string(0,0, str("${saveFace}") + str(NUMBERPERSON), scale = 1, color = (51,204,0) , thickness = 1)
  10118. v831_display_show_canvas(canvas,_canvas_x,_canvas_y)
  10119. time.sleep(1000 / 1000)
  10120. `;
  10121. return code;
  10122. };
  10123. Blockly.Blocks['face_recognition_remove'] = {
  10124. init: function () {
  10125. this.appendDummyInput()
  10126. .appendField(Blockly.Msg.CocoRobo_EVENT_ACTION_OPT1)
  10127. .appendField(new Blockly.FieldDropdown([
  10128. ["A", "A"],
  10129. ["B", "B"],
  10130. ["C", "C"],
  10131. ["D", "D"]
  10132. ]), "button_type")
  10133. .appendField(Blockly.Msg.v831_setting_face_data_remove)
  10134. // this.appendStatementInput("input")
  10135. // .setCheck(null)
  10136. // .appendField(Blockly.Msg.basic_motion_statement_exec);
  10137. // .appendField(new Blockly.FieldVariable("save_face_data"), "varitem");
  10138. this.setInputsInline(false);
  10139. this.setPreviousStatement(true, null);
  10140. this.setNextStatement(true, null);
  10141. this.setColour("#ee783a");
  10142. this.setTooltip(Blockly.Msg.v831_setting_face_data_remove_title);
  10143. this.setHelpUrl("");
  10144. }
  10145. };
  10146. Blockly.Python['face_recognition_remove'] = function (block) {
  10147. var _type = block.getFieldValue('button_type');
  10148. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  10149. sys.path.append("/root/")`
  10150. Blockly.Python.definitions_['v831_import_CocoPi_BUTTON'] = `from CocoPi import BUTTON`
  10151. // Blockly.Python.definitions_['v831_import_CocoPi'] = `sys.path.append("/root/")`
  10152. Blockly.Python.definitions_['import_define_x_v831_button'] = `key_A = BUTTON(14)
  10153. key_B = BUTTON(8)
  10154. key_C = BUTTON(13)
  10155. key_D = BUTTON(7)
  10156. `
  10157. let language = localStorage.getItem("handPyLanguage");
  10158. let removeFace = ""
  10159. let faceData = ""
  10160. if (language == "en") {
  10161. removeFace = "Remove Face"
  10162. faceData = "Data"
  10163. } else if (language == "zh-hant") {
  10164. removeFace = "刪除人臉"
  10165. faceData = "數據"
  10166. } else {
  10167. removeFace = "删除人脸"
  10168. faceData = "数据"
  10169. }
  10170. var code = `if key_${_type}.is_pressed() and NUMBERPERSON > 0:
  10171. while not (key_${_type}.is_pressed() == False):
  10172. time.sleep(0.1)
  10173. canvas.draw_rectangle(0,0, 0+320,0+ 20, color=(0,0,0), thickness=-1)
  10174. canvas.draw_string(40,0, ("".join([str(x) for x in ["${removeFace}", NUMBERPERSON, "${faceData}"]])), scale = 1, color = (255,255,255) , thickness = 1)
  10175. v831_display_show_canvas(canvas,_canvas_x,_canvas_y)
  10176. time.sleep(1000 / 1000)
  10177. NUMBERPERSON = NUMBERPERSON - 1
  10178. FACERECGNIZER.remove_user(FACERECGNIZER.CLASSNAMEFACELIST[len(FACERECGNIZER)-1])
  10179. FEATURES = FACERECGNIZER.features
  10180. `;
  10181. return code;
  10182. };
  10183. Blockly.Blocks['face_recognition_data_load'] = {
  10184. init: function () {
  10185. this.appendDummyInput()
  10186. .appendField(Blockly.Msg.ai_model_face_data_load);
  10187. this.setPreviousStatement(true, null);
  10188. this.setNextStatement(true, null);
  10189. this.setColour("#ee783a");
  10190. this.setTooltip(Blockly.Msg.ai_model_face_data_load);
  10191. this.setHelpUrl("");
  10192. }
  10193. };
  10194. Blockly.Python['face_recognition_data_load'] = function (block) {
  10195. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  10196. Blockly.Python.definitions_['v831_import_json'] = `import json`
  10197. Blockly.Python.definitions_['load_face_data'] = `isLoadRecoredFace = False
  10198. try:
  10199. with open("/root/user/model/recorded_face_features.py", "r") as file:
  10200. FACERECGNIZER.features = json.loads(file.read())
  10201. FACERECGNIZER.isLoadRecoredFace = True
  10202. except:
  10203. FACERECGNIZER.isLoadRecoredFace = False
  10204. `
  10205. var code = ``;
  10206. return code;
  10207. };
  10208. Blockly.Blocks['face_recognition_data_results'] = {
  10209. init: function () {
  10210. this.appendDummyInput()
  10211. .appendField(Blockly.Msg.ai_models_from)
  10212. .appendField(new Blockly.FieldVariable("img_face_Recognizer"), "varitem")
  10213. .appendField(Blockly.Msg.WIFIEASYMODE_COCOCLOUD_TYPE)
  10214. .appendField(new Blockly.FieldDropdown([
  10215. [Blockly.Msg.v831_model_face_name, "0"],
  10216. [Blockly.Msg.ai_models_face_model_confidence, "1"],
  10217. ]), "TYPE");
  10218. this.setInputsInline(false);
  10219. this.setOutput(true, null);
  10220. this.setColour("#ee783a");
  10221. this.setTooltip(Blockly.Msg.v831_face_recognition_result_title);
  10222. this.setHelpUrl("");
  10223. }
  10224. };
  10225. Blockly.Python['face_recognition_data_results'] = function (block) {
  10226. var type = block.getFieldValue('TYPE');
  10227. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  10228. var code = `FACERECGNIZER.recognize(${variable_name})[${type}]
  10229. `;
  10230. return [code, Blockly.Python.ORDER_ATOMIC];
  10231. };
  10232. // 自学习
  10233. Blockly.Blocks['ai_model_self_learning_init'] = {
  10234. init: function () {
  10235. this.appendDummyInput()
  10236. .appendField(Blockly.Msg.ai_model_self_learning_init);
  10237. this.appendValueInput("class_input")
  10238. .setCheck(null)
  10239. .appendField(Blockly.Msg.ai_models_customized_init_setup_name);
  10240. this.setInputsInline(false);
  10241. this.setPreviousStatement(true, null);
  10242. this.setNextStatement(true, null);
  10243. this.setColour("#ee783a");
  10244. this.setTooltip(Blockly.Msg.ai_model_self_learning_init);
  10245. this.setHelpUrl("");
  10246. }
  10247. };
  10248. Blockly.Python['ai_model_self_learning_init'] = function (block) {
  10249. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  10250. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  10251. Blockly.Python.definitions_.import_self_learning_init = `
  10252. CLASSNUM = ${JSON.parse(value_class_input).length}
  10253. SAMPLENUM = ${JSON.parse(value_class_input).length * 5}
  10254. CLASSNAMELIST = ${value_class_input}
  10255. `
  10256. var code = ``;
  10257. return code;
  10258. };
  10259. Blockly.Blocks['ai_model_self_learning_load'] = {
  10260. init: function () {
  10261. this.appendDummyInput()
  10262. .appendField(Blockly.Msg.ai_model_self_learning_load);
  10263. this.appendValueInput("class_input")
  10264. .setCheck(null)
  10265. .appendField(Blockly.Msg.ai_models_customized_init_setup_name);
  10266. this.setInputsInline(false);
  10267. this.setPreviousStatement(true, null);
  10268. this.setNextStatement(true, null);
  10269. this.setColour("#ee783a");
  10270. this.setTooltip(Blockly.Msg.ai_model_self_learning_load);
  10271. this.setHelpUrl("");
  10272. }
  10273. };
  10274. Blockly.Python['ai_model_self_learning_load'] = function (block) {
  10275. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  10276. Blockly.Python.definitions_['v831_import_Classifier'] = `from maix.nn.app.classifier import Classifier`
  10277. Blockly.Python.addVariable("SELFLEARNCOUNT", `SELFLEARNCOUNT = 0`, true)
  10278. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  10279. Blockly.Python.definitions_.import_self_learning_init = `
  10280. CLASSNUM = ${JSON.parse(value_class_input).length}
  10281. SAMPLENUM = ${JSON.parse(value_class_input).length * 5}
  10282. CLASSNAMELIST = ${value_class_input}
  10283. `
  10284. Blockly.Python.definitions_.self_learing_load = `class Self_learn:
  10285. model = {
  10286. "param": "/root/preset/model/resnet18_1000_awnn.param",
  10287. "bin": "/root/preset/model/resnet18_1000_awnn.bin"
  10288. }
  10289. options = {
  10290. "model_type": "awnn",
  10291. "inputs": {
  10292. "input0": (224, 224, 3)
  10293. },
  10294. "outputs": {
  10295. "190": (1, 1, 512)
  10296. },
  10297. "mean": [127.5, 127.5, 127.5],
  10298. "norm": [0.0176, 0.0176, 0.0176],
  10299. }
  10300. class_num = CLASSNUM #学习类别
  10301. sample_num = SAMPLENUM #学习类别总数量
  10302. curr_class = 0
  10303. curr_sample = 0
  10304. def __init__(self):
  10305. self.m = nn.load(self.model, opt=self.options)
  10306. self.classifier = Classifier(self.m, self.class_num, self.sample_num, 512, 224, 224)
  10307. SELFLEARN = Self_learn()
  10308. `
  10309. var code = '';
  10310. return code;
  10311. };
  10312. Blockly.Blocks['ai_model_self_learning_recognize_load'] = {
  10313. init: function () {
  10314. this.appendDummyInput()
  10315. .appendField(Blockly.Msg.ai_model_self_learning_recognize_load);
  10316. this.appendValueInput("class_input")
  10317. .setCheck(null)
  10318. .appendField(Blockly.Msg.ai_models_customized_init_setup_name);
  10319. this.setInputsInline(false);
  10320. this.setPreviousStatement(true, null);
  10321. this.setNextStatement(true, null);
  10322. this.setColour("#ee783a");
  10323. this.setTooltip(Blockly.Msg.ai_model_self_learning_recognize_load);
  10324. this.setHelpUrl("");
  10325. }
  10326. };
  10327. Blockly.Python['ai_model_self_learning_recognize_load'] = function (block) {
  10328. Blockly.Python.definitions_['v831_import_from_maix_nn'] = `from maix import nn`
  10329. Blockly.Python.definitions_['v831_import_Classifier'] = `from maix.nn.app.classifier import Classifier`
  10330. Blockly.Python.definitions_['v831_from_nn_import_load'] = `from maix.nn.app.classifier import load`
  10331. Blockly.Python.definitions_['v831_import_os'] = `import os`
  10332. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  10333. Blockly.Python.addVariable("SELFLEARNCOUNT", `SELFLEARNCOUNT = 0`, true)
  10334. Blockly.Python.definitions_.import_self_learning_init = `
  10335. CLASSNUM = ${JSON.parse(value_class_input).length}
  10336. SAMPLENUM = ${JSON.parse(value_class_input).length * 5}
  10337. CLASSNAMELIST = ${value_class_input}
  10338. `
  10339. Blockly.Python.definitions_.self_learing_load = `class Self_learn:
  10340. model = {
  10341. "param": "/root/preset/model/resnet18_1000_awnn.param",
  10342. "bin": "/root/preset/model/resnet18_1000_awnn.bin"
  10343. }
  10344. options = {
  10345. "model_type": "awnn",
  10346. "inputs": {
  10347. "input0": (224, 224, 3)
  10348. },
  10349. "outputs": {
  10350. "190": (1, 1, 512)
  10351. },
  10352. "mean": [127.5, 127.5, 127.5],
  10353. "norm": [0.0176, 0.0176, 0.0176],
  10354. }
  10355. class_num = CLASSNUM #学习类别
  10356. sample_num = SAMPLENUM #学习类别总数量
  10357. curr_class = 0
  10358. curr_sample = 0
  10359. def __init__(self):
  10360. if os.path.isfile("/root/module.bin"):
  10361. self.m = nn.load(self.model, opt=self.options)
  10362. self.classifier = load(self.m,"/root/module.bin")
  10363. else:
  10364. self.m = nn.load(self.model, opt=self.options)
  10365. self.classifier = Classifier(self.m, self.class_num, self.sample_num, 512, 224, 224)
  10366. print("not have model!")
  10367. SELFLEARN = Self_learn()
  10368. `
  10369. var code = '';
  10370. return code;
  10371. };
  10372. Blockly.Blocks['ai_model_self_learning_add_class'] = {
  10373. init: function () {
  10374. this.appendDummyInput()
  10375. .appendField(Blockly.Msg.CocoRobo_EVENT_ACTION_OPT1)
  10376. .appendField(new Blockly.FieldDropdown([
  10377. ["A", "A"],
  10378. ["B", "B"],
  10379. ["C", "C"],
  10380. ["D", "D"]
  10381. ]), "button_type")
  10382. .appendField(Blockly.Msg.ai_model_self_learning_add_class_category + Blockly.Msg.ai_model_self_learning_make_data_set);
  10383. this.setInputsInline(false);
  10384. this.setPreviousStatement(true, null);
  10385. this.setNextStatement(true, null);
  10386. this.setColour("#ee783a");
  10387. this.setTooltip(Blockly.Msg.ai_model_self_learning_add_class_category + Blockly.Msg.ai_model_self_learning_make_data_set);
  10388. this.setHelpUrl("");
  10389. }
  10390. };
  10391. Blockly.Python['ai_model_self_learning_add_class'] = function (block) {
  10392. var _type = block.getFieldValue('button_type');
  10393. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  10394. sys.path.append("/root/")`
  10395. Blockly.Python.definitions_['v831_import_CocoPi_BUTTON'] = `from CocoPi import BUTTON`
  10396. // Blockly.Python.definitions_['v831_import_CocoPi'] = `sys.path.append("/root/")`
  10397. Blockly.Python.definitions_['import_define_x_v831_button'] = `key_A = BUTTON(14)
  10398. key_B = BUTTON(8)
  10399. key_C = BUTTON(13)
  10400. key_D = BUTTON(7)
  10401. `
  10402. var code = `canvas = canvas.crop(48, 8, 224, 224)
  10403. if key_${_type}.is_pressed() and SELFLEARNCOUNT < SELFLEARN.class_num:
  10404. while not (key_${_type}.is_pressed() == False):
  10405. time.sleep(0.1)
  10406. SELFLEARN.classifier.add_class_img(canvas)
  10407. for i in range(5):
  10408. SELFLEARN.classifier.add_sample_img(canvas)
  10409. SELFLEARNCOUNT = SELFLEARNCOUNT + 1
  10410. `;
  10411. return code;
  10412. };
  10413. Blockly.Blocks['ai_model_self_learning_make_data_set'] = {
  10414. init: function () {
  10415. this.appendDummyInput()
  10416. .appendField(Blockly.Msg.ai_model_self_learning_make_data_set);
  10417. this.setInputsInline(false);
  10418. this.setPreviousStatement(true, null);
  10419. this.setNextStatement(true, null);
  10420. this.setColour("#ee783a");
  10421. this.setTooltip(Blockly.Msg.ai_model_self_learning_make_data_set);
  10422. this.setHelpUrl("");
  10423. }
  10424. };
  10425. Blockly.Python['ai_model_self_learning_make_data_set'] = function (block) {
  10426. var code = `for i in range(5):
  10427. SELFLEARN.classifier.add_sample_img(canvas)
  10428. `;
  10429. return code;
  10430. };
  10431. Blockly.Blocks['ai_model_self_learning_picture'] = {
  10432. init: function () {
  10433. this.appendDummyInput()
  10434. .appendField(Blockly.Msg.CocoRobo_EVENT_ACTION_OPT1)
  10435. .appendField(new Blockly.FieldDropdown([
  10436. ["A", "A"],
  10437. ["B", "B"],
  10438. ["C", "C"],
  10439. ["D", "D"]
  10440. ]), "button_type")
  10441. .appendField(Blockly.Msg.ai_model_self_learning_picture + Blockly.Msg.ai_model_self_learning_save_modal);
  10442. this.setInputsInline(false);
  10443. this.setPreviousStatement(true, null);
  10444. this.setNextStatement(true, null);
  10445. this.setColour("#ee783a");
  10446. this.setTooltip(Blockly.Msg.ai_model_self_learning_picture + Blockly.Msg.ai_model_self_learning_save_modal);
  10447. this.setHelpUrl("");
  10448. }
  10449. };
  10450. Blockly.Python['ai_model_self_learning_picture'] = function (block) {
  10451. var _type = block.getFieldValue('button_type');
  10452. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  10453. sys.path.append("/root/")`
  10454. Blockly.Python.definitions_['v831_import_CocoPi_BUTTON'] = `from CocoPi import BUTTON`
  10455. // Blockly.Python.definitions_['v831_import_CocoPi'] = `sys.path.append("/root/")`
  10456. Blockly.Python.definitions_['import_define_x_v831_button'] = `key_A = BUTTON(14)
  10457. key_B = BUTTON(8)
  10458. key_C = BUTTON(13)
  10459. key_D = BUTTON(7)
  10460. `
  10461. var code = `if key_${_type}.is_pressed() and SELFLEARNCOUNT >= SELFLEARN.class_num:
  10462. while not (key_${_type}.is_pressed() == False):
  10463. time.sleep(0.1)
  10464. SELFLEARN.classifier.train()
  10465. SELFLEARN.classifier.save("/root/module.bin")
  10466. `;
  10467. return code;
  10468. }; Blockly.Blocks['ai_model_self_learning_save_modal'] = {
  10469. init: function () {
  10470. this.appendDummyInput()
  10471. .appendField(Blockly.Msg.ai_model_self_learning_save_modal);
  10472. this.setInputsInline(false);
  10473. this.setPreviousStatement(true, null);
  10474. this.setNextStatement(true, null);
  10475. this.setColour("#ee783a");
  10476. this.setTooltip(Blockly.Msg.ai_model_self_learning_save_modal);
  10477. this.setHelpUrl("");
  10478. }
  10479. };
  10480. Blockly.Python['ai_model_self_learning_save_modal'] = function (block) {
  10481. var code = `SELFLEARN.classifier.save("/root/module.bin")
  10482. `;
  10483. return code;
  10484. };
  10485. Blockly.Blocks['ai_model_self_learning_class_verification'] = {
  10486. init: function () {
  10487. this.appendDummyInput()
  10488. .appendField(Blockly.Msg.ai_model_self_learning_class_verification);
  10489. this.setInputsInline(false);
  10490. this.setPreviousStatement(true, null);
  10491. this.setNextStatement(true, null);
  10492. this.setColour("#ee783a");
  10493. this.setTooltip(Blockly.Msg.ai_model_self_learning_class_verification);
  10494. this.setHelpUrl("");
  10495. }
  10496. };
  10497. Blockly.Python['ai_model_self_learning_class_verification'] = function (block) {
  10498. var code = `canvas = canvas.crop(48, 8, 224, 224)
  10499. SELFLEARNidx, SELFLEARNdistance = SELFLEARN.classifier.predict(canvas)
  10500. `;
  10501. return code;
  10502. };
  10503. Blockly.Blocks['ai_model_self_learning_result'] = {
  10504. init: function () {
  10505. this.appendDummyInput()
  10506. .appendField(Blockly.Msg.ai_model_self_learning_result)
  10507. .appendField(new Blockly.FieldDropdown([
  10508. [Blockly.Msg.ai_model_self_learning_result_name, "X"],
  10509. [Blockly.Msg.ai_models_object_model_confidence, "Y"]
  10510. ]), "TYPE");;
  10511. this.setOutput(true, null);
  10512. this.setColour("#ee783a");
  10513. this.setTooltip(Blockly.Msg.ai_model_self_learning_result);
  10514. this.setHelpUrl("");
  10515. }
  10516. };
  10517. Blockly.Python['ai_model_self_learning_result'] = function (block) {
  10518. var type = block.getFieldValue('TYPE');
  10519. var code = '';
  10520. if (type == 'X') {
  10521. code = `CLASSNAMELIST[SELFLEARNidx]`
  10522. } else {
  10523. code = `100-SELFLEARNdistance`
  10524. }
  10525. return [code, Blockly.Python.ORDER_ATOMIC];
  10526. };
  10527. Blockly.Blocks['ai_model_customized_init_setup_definition'] = {
  10528. init: function () {
  10529. this.appendDummyInput()
  10530. .appendField(new Blockly.FieldImage("blockly/media/header_customized_model.png", 50, 50, { alt: "*", flipRtl: "FALSE" }));
  10531. this.appendDummyInput()
  10532. .appendField(Blockly.Msg.ai_model_customized_init_setup_definition);
  10533. this.setInputsInline(false);
  10534. this.setPreviousStatement(true, null);
  10535. this.setNextStatement(true, null);
  10536. this.setColour("#ee783a");
  10537. this.setTooltip(Blockly.Msg.ai_model_customized_init_setup_definition_TOOLTIP);
  10538. this.setHelpUrl("");
  10539. }
  10540. };
  10541. Blockly.Python['ai_model_customized_init_setup_definition'] = function (block) {
  10542. // TODO: Assemble Python into code variable.
  10543. Blockly.Python.includes_.import_amxisd = '' +
  10544. 'print("Clearing Cached Variables...", end="")\n' +
  10545. 'for name in dir(): \n' +
  10546. ' if not name.startswith(\'_\'): \n' +
  10547. ' del globals()[name]\n' +
  10548. 'print(" Done")\n' +
  10549. 'import KPU as kpu\n' +
  10550. 'kpu.memtest()\n' +
  10551. 'from Maix import utils\n' +
  10552. 'import gc\n' +
  10553. 'gc.enable()\n' +
  10554. 'utils.gc_heap_size()\n' +
  10555. '\n' +
  10556. '################# Done Init #################\n' +
  10557. '';
  10558. var code = '';
  10559. return code;
  10560. };
  10561. // 初始化serialcomm_read_setup_title_text_1
  10562. Blockly.Blocks['ai_model_index_custom_initialization_init_1'] = {
  10563. init: function () {
  10564. this.appendValueInput("class_input")
  10565. .setCheck(null)
  10566. .appendField(Blockly.Msg.serialcomm_read_setup_title_text_1)
  10567. // .appendField("DEGREE");
  10568. this.setPreviousStatement(true, null);
  10569. this.setNextStatement(true, null);
  10570. this.setColour("#ee783a");
  10571. this.setTooltip(Blockly.Msg.serialcomm_read_setup_title_text_1);
  10572. this.setHelpUrl('');
  10573. }
  10574. };
  10575. Blockly.Python.ai_model_index_custom_initialization_init_1 = function (block) {
  10576. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  10577. Blockly.Python.definitions_.import_KPU = "import KPU as kpu";
  10578. var _code = value_class_input;
  10579. return _code;
  10580. }
  10581. // 自定义人脸识别
  10582. Blockly.Blocks['ai_model_face_model'] = {
  10583. init: function () {
  10584. this.appendDummyInput()
  10585. .appendField(Blockly.Msg.ai_model_face_setup);
  10586. this.setInputsInline(false);
  10587. this.setOutput(true, null);
  10588. this.setColour("#ee783a");
  10589. this.setTooltip(Blockly.Msg.ai_model_face_setup);
  10590. this.setHelpUrl("");
  10591. }
  10592. };
  10593. Blockly.Python.ai_model_face_model = function (block) {
  10594. Blockly.Python.definitions_['v831_import_time'] = `import time`
  10595. Blockly.Python.definitions_['v831_import_gc'] = `import gc`
  10596. Blockly.Python.includes_.import_amxisd = '' +
  10597. 'for name in dir(): \n' +
  10598. ' if not name.startswith(\'_\'): \n' +
  10599. ' del globals()[name]\n' +
  10600. 'gc.enable()\n' +
  10601. '\n' +
  10602. 'try:\n' +
  10603. ' from cocorobo import firmware_info\n' +
  10604. 'except BaseException as e:\n' +
  10605. ' print(str(e))\n' +
  10606. ' pass\n' +
  10607. '\n' +
  10608. 'if int(str(firmware_info.ai()).split("-")[0]) < 2021:\n' +
  10609. ' from Maix import utils\n' +
  10610. ' utils.gc_heap_size(0xAF000)\n' +
  10611. 'else:\n' +
  10612. ' import utime\n' +
  10613. ' clock = None\n' +
  10614. ' ACCURACY = 85\n' +
  10615. '\n' +
  10616. 'task_fd = None\n' +
  10617. 'task_ld = None\n' +
  10618. 'task_fe = None\n' +
  10619. '\n' +
  10620. '';
  10621. var code = `
  10622. from fpioa_manager import *
  10623. from Maix import FPIOA, GPIO
  10624. _gp_side_buttons = [9, 10, 11]
  10625. FPIOA().set_function(_gp_side_buttons[0],FPIOA.GPIO0)
  10626. FPIOA().set_function(_gp_side_buttons[1],FPIOA.GPIO1)
  10627. FPIOA().set_function(_gp_side_buttons[2],FPIOA.GPIO2)
  10628. _gp_side_a = GPIO(GPIO.GPIO0,GPIO.IN,GPIO.PULL_UP)
  10629. _gp_side_b = GPIO(GPIO.GPIO1,GPIO.IN,GPIO.PULL_UP)
  10630. _gp_side_c = GPIO(GPIO.GPIO2,GPIO.IN,GPIO.PULL_UP)
  10631. side_button_state = False
  10632. def run_face_compare(record_ftrs, feature):
  10633. scores = []
  10634. for j in range(len(record_ftrs)):
  10635. score = kpu.face_compare(record_ftrs[j], feature)
  10636. scores.append(score)
  10637. max_score = 0
  10638. # if int(str(firmware_info.ai()).split("-")[0]) < 2021:
  10639. # index = -1
  10640. # else:
  10641. # index = 0
  10642. index = -1
  10643. for k in range(len(scores)):
  10644. if max_score < scores[k]:
  10645. max_score = scores[k]
  10646. index = k
  10647. return [scores, max_score, index]
  10648. # print(scores)
  10649. `;
  10650. // var code = '';
  10651. return [code, Blockly.Python.ORDER_ATOMIC];
  10652. }
  10653. // 设置人脸识别名称
  10654. Blockly.Blocks['ai_model_face_setup'] = {
  10655. init: function () {
  10656. // this.appendDummyInput()
  10657. // .appendField(Blockly.Msg.ai_model_face_setup);
  10658. this.appendValueInput("class_input")
  10659. .setCheck(null)
  10660. .appendField(Blockly.Msg.ai_model_face_name);
  10661. this.setPreviousStatement(true, null);
  10662. this.setNextStatement(true, null);
  10663. this.setColour("#ee783a");
  10664. this.setTooltip(Blockly.Msg.ai_model_face_name.replace(":", ""));
  10665. this.setHelpUrl("");
  10666. }
  10667. };
  10668. Blockly.Python['ai_model_face_setup'] = function (block) {
  10669. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  10670. var code = `
  10671. img_lcd = image.Image()
  10672. # img_face = image.Image(size=(128, 128))
  10673. collectNum = 0
  10674. record_ftrs = []
  10675. result = []
  10676. res_index = -1
  10677. # a = img_face.pix_to_ai()
  10678. names = ` + value_class_input + `
  10679. `;
  10680. return code;
  10681. };
  10682. Blockly.Blocks['ai_face_detection_model'] = {
  10683. init: function () {
  10684. this.appendDummyInput()
  10685. .appendField(Blockly.Msg.ai_face_detection_model);
  10686. this.setPreviousStatement(true, null);
  10687. this.setNextStatement(true, null);
  10688. this.setColour("#ee783a");
  10689. this.setTooltip(Blockly.Msg.ai_face_detection_model_TOOLTIP);
  10690. this.setHelpUrl("");
  10691. }
  10692. };
  10693. Blockly.Python['ai_face_detection_model'] = function (block) {
  10694. var code = '\n' +
  10695. 'if int(str(firmware_info.ai()).split("-")[0]) < 2021:\n' +
  10696. ' task_fd = kpu.load("/sd/preset/models/face_reocgnition/FD.emodel") # 人脸检测模型\n' +
  10697. 'else:\n' +
  10698. ' task_fd = kpu.load("/sd/preset/models/face/v2/FaceDetection.emodel") # 0x300000)\n' +
  10699. 'anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025)\n' +
  10700. 'a = kpu.init_yolo2(task_fd, 0.5, 0.3, 5, anchor)\n' +
  10701. '\n' +
  10702. '';
  10703. return code;
  10704. };
  10705. Blockly.Blocks['ai_face_point_detection_model'] = {
  10706. init: function () {
  10707. this.appendDummyInput()
  10708. .appendField(Blockly.Msg.ai_face_point_detection_model);
  10709. this.setPreviousStatement(true, null);
  10710. this.setNextStatement(true, null);
  10711. this.setColour("#ee783a");
  10712. this.setTooltip(Blockly.Msg.ai_face_point_detection_model_TOOLTIP);
  10713. this.setHelpUrl("");
  10714. }
  10715. };
  10716. Blockly.Python['ai_face_point_detection_model'] = function (block) {
  10717. Blockly.Python.definitions_.import_ai_face_point_detection_model = `
  10718. def run_five_points_inference(canvas, canvas_face, detected_face_rect):
  10719. a = canvas.draw_rectangle(detected_face_rect)
  10720. # Create face image from detection
  10721. face_cut = canvas.cut(detected_face_rect[0], detected_face_rect[1], detected_face_rect[2], detected_face_rect[3])
  10722. # Convert created face image to a dimension of 128x128
  10723. face_cut_128 = face_cut.resize(128, 128)
  10724. # 将正脸图像转为kpu格式, 便于后面的关键点模型的使用
  10725. a = face_cut_128.pix_to_ai()
  10726. # Get landmark for 5 points of the detected face
  10727. # 运行人脸5点关键点检测模型, 获取当前识别人脸的关键点数据
  10728. fmap = kpu.forward(task_ld, face_cut_128)
  10729. # print("fmap", fmap)
  10730. # 获取关键点预测结果
  10731. plist = fmap[:]
  10732. # print("plist", plist)
  10733. # Get position of left eye, then draw it on the canvas.
  10734. le = (i.x()+int(plist[0]*i.w() - 10), i.y()+int(plist[1]*i.h()))
  10735. a = canvas.draw_circle(le[0], le[1], 4)
  10736. # Get position of Right eye, then draw it on the canvas.
  10737. re = (i.x()+int(plist[2]*i.w()), i.y()+int(plist[3]*i.h()))
  10738. a = canvas.draw_circle(re[0], re[1], 4)
  10739. # Get position of Nose, then draw it on the canvas.
  10740. nose = (i.x()+int(plist[4]*i.w()), i.y()+int(plist[5]*i.h()))
  10741. a = canvas.draw_circle(nose[0], nose[1], 4)
  10742. # Get position of Left cheek, then draw it on the canvas.
  10743. lm = (i.x()+int(plist[6]*i.w()), i.y()+int(plist[7]*i.h()))
  10744. a = canvas.draw_circle(lm[0], lm[1], 4)
  10745. # Get position of Right cheek, then draw it on the canvas.
  10746. rm = (i.x()+int(plist[8]*i.w()), i.y()+int(plist[9]*i.h()))
  10747. a = canvas.draw_circle(rm[0], rm[1], 4)
  10748. # Align face to standard position
  10749. src_point = [le, re, nose, lm, rm]
  10750. '''
  10751. get_affine_transform() 函数, 根据获得的5点坐标与标准正脸坐标获取仿射变换矩阵
  10752. - 计算3个二维点对之间的仿射变换矩阵H(2行x3列),自由度为6。
  10753. - 仿射变换的原理就是根据源图像中一个三角形的顶点坐标和应用仿射变换之后的
  10754. - 目标图像中一个三角形的顶点坐标计算出一个变换矩阵,然后将这个矩阵应用到整个源图像。
  10755. src_point 为输入源图像中的三角形顶点坐标
  10756. dst_point 为输出目标图像中的三角形顶点坐标
  10757. Ref: https://zhuanlan.zhihu.com/p/142260846
  10758. '''
  10759. T = image.get_affine_transform(src_point, dst_point)
  10760. '''
  10761. warp_affine_ai() 函数, 对原始图片人脸图片进行仿射变换,变换为正脸图像
  10762. '''
  10763. a = image.warp_affine_ai(canvas, canvas_face, T)
  10764. '''
  10765. 将正脸图像转为kpu格式
  10766. '''
  10767. a = canvas_face.ai_to_pix()
  10768. # Recycle face_cut_128 in order to reduce memory usage
  10769. del(face_cut_128)
  10770. `;
  10771. var code = '\n' +
  10772. 'if int(str(firmware_info.ai()).split("-")[0]) < 2021:\n' +
  10773. ' task_ld = kpu.load("/sd/preset/models/face_reocgnition/KP_chwise.emodel") # 人脸5点关键点检测模型\n' +
  10774. 'else:\n' +
  10775. ' task_ld = kpu.load("/sd/preset/models/face/v2/FaceLandmarkDetection.emodel")# 0x400000)\n' +
  10776. 'dst_point = [(44, 59), (84, 59), (64, 82), (47, 105),(81, 105)]\n' +
  10777. '\n' +
  10778. '';
  10779. return code;
  10780. };
  10781. Blockly.Blocks['ai_face_Vitter_eigenvalue_model'] = {
  10782. init: function () {
  10783. this.appendDummyInput()
  10784. .appendField(Blockly.Msg.ai_face_Vitter_eigenvalue_model);
  10785. this.setPreviousStatement(true, null);
  10786. this.setNextStatement(true, null);
  10787. this.setColour("#ee783a");
  10788. this.setTooltip(Blockly.Msg.ai_face_Vitter_eigenvalue_model_TOOLTIP);
  10789. this.setHelpUrl("");
  10790. }
  10791. };
  10792. Blockly.Python['ai_face_Vitter_eigenvalue_model'] = function (block) {
  10793. Blockly.Python.definitions_.import_ai_face_Vitter_eigenvalue_model = `
  10794. def run_fmap_calculation(canvas_face):
  10795. global fmap
  10796. # Calculate face feature vector from 'img_face', 计算正脸图片的196维特征值
  10797. fmap = kpu.forward(task_fe, canvas_face)
  10798. # 获取计算结果
  10799. feature = kpu.face_encode(fmap[:])
  10800. return feature
  10801. `;
  10802. var code = '\n' +
  10803. 'if int(str(firmware_info.ai()).split("-")[0]) < 2021:\n' +
  10804. ' task_fe = kpu.load("/sd/preset/models/face_reocgnition/face_recognition_v4_2020_09_07.emodel") # 人脸196维特征值模型\n' +
  10805. ' kpu.set_outputs(task_fe, 0,1,1, 512)\n' +
  10806. 'else:\n' +
  10807. ' task_fe = kpu.load("/sd/preset/models/face/v2/FeatureExtraction.emodel")# 0x500000)\n' +
  10808. ' clock = time.clock()\n' +
  10809. '\n' +
  10810. '';
  10811. return code;
  10812. };
  10813. // 加载人脸检测模型,设置识别图像源
  10814. Blockly.Blocks['ai_model_face_start_recognition'] = {
  10815. init: function () {
  10816. this.appendDummyInput()
  10817. .appendField(Blockly.Msg.ai_model_face_start_recognition + Blockly.Msg.ai_models_customized_init_setup_source)
  10818. .appendField(new Blockly.FieldVariable("img"), "varitem");
  10819. // this.appendDummyInput()
  10820. // .appendField(Blockly.Msg.ai_models_customized_init_type_set)
  10821. // .appendField(new Blockly.FieldDropdown([
  10822. // [Blockly.Msg.ai_models_customized_init_type_set_fast, "fast"],
  10823. // [Blockly.Msg.ai_models_customized_init_type_set_slow, "slow"]
  10824. // ]), "detect_type");
  10825. // this.appendStatementInput("NAME")
  10826. // .setCheck(null)
  10827. // .appendField(Blockly.Msg.ai_speech_recognition_any_result_text_do);
  10828. this.setInputsInline(false);
  10829. this.setPreviousStatement(true, null);
  10830. this.setNextStatement(true, null);
  10831. this.setColour("#ee783a");
  10832. this.setTooltip(Blockly.Msg.ai_model_face_start_recognition_TOOLTIP);
  10833. this.setHelpUrl("");
  10834. }
  10835. }
  10836. Blockly.Python['ai_model_face_start_recognition'] = function (block) {
  10837. // var statements_name = Blockly.Python.statementToCode(block, 'NAME');
  10838. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  10839. // var dropdown_detect_type = block.getFieldValue('detect_type');
  10840. localStorage.setItem("ai_model_face_variable_name", variable_name);
  10841. var code = '' +
  10842. 'if int(str(firmware_info.ai()).split("-")[0]) < 2021:\n' +
  10843. ' index = -1\n' +
  10844. 'else:\n' +
  10845. ' clock.tick()\n' +
  10846. 'a = img_face.pix_to_ai()\n' +
  10847. 'code = kpu.run_yolo2(task_fd, ' + variable_name + ')\n';
  10848. return code;
  10849. }
  10850. Blockly.Blocks['ai_models_customized_init_type_set'] = {
  10851. init: function () {
  10852. // this.appendDummyInput()
  10853. // .appendField(Blockly.Msg.ai_model_face_start_recognition + Blockly.Msg.ai_models_customized_init_setup_source)
  10854. // .appendField(new Blockly.FieldVariable("img"), "varitem");
  10855. this.appendDummyInput()
  10856. .appendField(Blockly.Msg.ai_models_customized_init_type_set)
  10857. .appendField(new Blockly.FieldDropdown([
  10858. [Blockly.Msg.ai_models_customized_init_type_set_fast, "fast"],
  10859. [Blockly.Msg.ai_models_customized_init_type_set_slow, "slow"]
  10860. ]), "detect_type");
  10861. // this.appendStatementInput("NAME")
  10862. // .setCheck(null)
  10863. // .appendField(Blockly.Msg.ai_speech_recognition_any_result_text_do);
  10864. this.setInputsInline(false);
  10865. this.setPreviousStatement(true, null);
  10866. this.setNextStatement(true, null);
  10867. this.setColour("#ee783a");
  10868. this.setTooltip(Blockly.Msg.ai_models_customized_init_type_set.replace(":", ""));
  10869. this.setHelpUrl("");
  10870. }
  10871. }
  10872. Blockly.Python['ai_models_customized_init_type_set'] = function (block) {
  10873. // var statements_name = Blockly.Python.statementToCode(block, 'NAME');
  10874. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  10875. var dropdown_detect_type = block.getFieldValue('detect_type');
  10876. // localStorage.setItem("ai_model_face_variable_name", variable_name);
  10877. if (dropdown_detect_type == "fast") {
  10878. // var break_string = " break\n";
  10879. var break_string = "break_string = True\n";
  10880. } else if (dropdown_detect_type == "slow") {
  10881. // var break_string = " # break\n";
  10882. var break_string = "break_string = False\n";
  10883. }
  10884. var code = '' +
  10885. break_string +
  10886. '\n';
  10887. return code;
  10888. }
  10889. Blockly.Blocks['ai_model_face_model_load_finish'] = {
  10890. init: function () {
  10891. this.appendDummyInput()
  10892. .appendField(Blockly.Msg.ai_model_face_model_load_finish);
  10893. this.setOutput(true, null);
  10894. this.setColour("#ee783a");
  10895. this.setTooltip(Blockly.Msg.ai_model_face_model_load_finish_TOOLTIP);
  10896. this.setHelpUrl("");
  10897. }
  10898. };
  10899. Blockly.Python['ai_model_face_model_load_finish'] = function (block) {
  10900. var code = 'code is not None';
  10901. return [code, Blockly.Python.ORDER_NONE];
  10902. };
  10903. Blockly.Blocks['ai_model_face_model_list'] = {
  10904. init: function () {
  10905. this.appendDummyInput()
  10906. .appendField(Blockly.Msg.ai_model_face_model_list);
  10907. this.setOutput(true, null);
  10908. this.setColour("#ee783a");
  10909. this.setTooltip(Blockly.Msg.ai_model_face_model_list_TOOLTIP);
  10910. this.setHelpUrl("");
  10911. }
  10912. };
  10913. Blockly.Python['ai_model_face_model_list'] = function (block) {
  10914. var code = 'code';
  10915. return [code, Blockly.Python.ORDER_NONE];
  10916. };
  10917. Blockly.Blocks['ai_model_face_detection_rectangle'] = {
  10918. init: function () {
  10919. this.appendDummyInput()
  10920. .appendField(new Blockly.FieldImage("blockly/media/face_recognition_header.png", 45, 45, { alt: "*", flipRtl: "FALSE" }));
  10921. this.appendDummyInput()
  10922. .appendField(Blockly.Msg.ai_models_from)
  10923. .appendField(new Blockly.FieldVariable("i"), "varitem")
  10924. .appendField(Blockly.Msg.ai_models_face_model_get)
  10925. .appendField(new Blockly.FieldDropdown([
  10926. [Blockly.Msg.ai_models_face_model_rect, "rect"],
  10927. [Blockly.Msg.ai_models_face_model_x, "X"],
  10928. [Blockly.Msg.ai_models_face_model_y, "Y"],
  10929. [Blockly.Msg.ai_models_face_model_w, "W"],
  10930. [Blockly.Msg.ai_models_face_model_h, "H"],
  10931. [Blockly.Msg.ai_models_face_model_x_center, "CX"],
  10932. [Blockly.Msg.ai_models_face_model_y_center, "CY"],
  10933. [Blockly.Msg.ai_models_face_model_count, "Number"],
  10934. [Blockly.Msg.ai_models_face_model_index, "Serial"],
  10935. [Blockly.Msg.ai_models_face_model_confidence, "Confidence"]
  10936. ]), "TYPE");
  10937. this.setInputsInline(false);
  10938. this.setOutput(true, null);
  10939. this.setColour("#ee783a");
  10940. var thisBlock = this;
  10941. this.setTooltip(function () {
  10942. var mode = thisBlock.getFieldValue('TYPE');
  10943. var TOOLTIPS = {
  10944. 'rect': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_rect),
  10945. 'X': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x),
  10946. 'Y': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y),
  10947. 'W': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_w),
  10948. 'H': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_h),
  10949. 'CX': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_x_center),
  10950. 'CY': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_y_center),
  10951. 'Number': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_count),
  10952. 'Serial': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_index),
  10953. 'Confidence': Blockly.Msg.ai_model_xy_TOOLTIP.replace('%1', Blockly.Msg.ai_models_face_model_confidence)
  10954. };
  10955. return TOOLTIPS[mode];
  10956. });
  10957. this.setHelpUrl("");
  10958. }
  10959. };
  10960. Blockly.Python.ai_model_face_detection_rectangle = function (block) {
  10961. var type = block.getFieldValue('TYPE');
  10962. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  10963. var _code = "";
  10964. if (type == "rect") {
  10965. _code = variable_name + ".rect()";
  10966. } else if (type == "X") {
  10967. _code = variable_name + ".x()";
  10968. } else if (type == "Y") {
  10969. _code = variable_name + ".y()";
  10970. } else if (type == "W") {
  10971. _code = variable_name + ".w()";
  10972. } else if (type == "H") {
  10973. _code = variable_name + ".h()";
  10974. } else if (type == "Number") {
  10975. _code = variable_name + ".objnum()";
  10976. } else if (type == "Serial") {
  10977. _code = variable_name + ".index()";
  10978. } else if (type == "Confidence") {
  10979. _code = variable_name + ".value()";
  10980. } else if (type == "CX") {
  10981. _code = variable_name + ".x()+(" + variable_name + ".w()/2)";
  10982. } else if (type == "CY") {
  10983. _code = variable_name + ".y()+(" + variable_name + ".h()/2)";
  10984. }
  10985. return [_code, Blockly.Python.ORDER_ATOMIC];
  10986. };
  10987. Blockly.Blocks['ai_model_face_load_point_detection_model'] = {
  10988. init: function () {
  10989. this.appendDummyInput()
  10990. .appendField(Blockly.Msg.ai_model_face_load_point_detection_model + Blockly.Msg.ai_models_customized_init_setup_source)
  10991. .appendField(new Blockly.FieldVariable("img"), "varitem");
  10992. this.appendDummyInput()
  10993. .appendField(Blockly.Msg.ai_model_face_load_point_detection_model_1)
  10994. .appendField(new Blockly.FieldVariable("img_face"), "varitem1");
  10995. this.appendValueInput("class_input")
  10996. .setCheck(null)
  10997. .appendField(Blockly.Msg.ai_model_face_load_point_detection_model_2);
  10998. this.setInputsInline(false);
  10999. this.setPreviousStatement(true, null);
  11000. this.setNextStatement(true, null);
  11001. this.setColour("#ee783a");
  11002. this.setTooltip(Blockly.Msg.ai_model_face_load_point_detection_model_TOOLTIP);
  11003. this.setHelpUrl("");
  11004. }
  11005. }
  11006. Blockly.Python.ai_model_face_load_point_detection_model = function (block) {
  11007. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  11008. var variable_name_1 = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem1'), Blockly.Variables.NAME_TYPE);
  11009. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  11010. var _code = "run_five_points_inference(" + variable_name + ", " + variable_name_1 + ", " + value_class_input + ")\n";
  11011. return _code;
  11012. };
  11013. Blockly.Blocks['ai_model_face_gather'] = {
  11014. init: function () {
  11015. this.appendDummyInput()
  11016. .appendField(Blockly.Msg.ai_model_face_gather);
  11017. this.appendValueInput("class_input")
  11018. .setCheck(null)
  11019. .appendField(Blockly.Msg.ai_model_face_gather_1);
  11020. this.setPreviousStatement(true, null);
  11021. this.setNextStatement(true, null);
  11022. this.setColour("#ee783a");
  11023. this.setTooltip(Blockly.Msg.ai_model_face_gather_TOOLTIP);
  11024. this.setHelpUrl("");
  11025. }
  11026. };
  11027. Blockly.Python['ai_model_face_gather'] = function (block) {
  11028. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC).replace('\"', '').replace('\"', '');
  11029. Blockly.Python.definitions_['v831_import_os'] = `import os`
  11030. Blockly.Python.addVariable('ai_model_face_gather', `
  11031. def _CREATE_TEXT_FILE_WITH_CONTENT(_path, _data, _sep):
  11032. f = open(_path, 'a')
  11033. f.write(_data + _sep)
  11034. f.close()
  11035. `, true);
  11036. var code = '' +
  11037. '# start to gather faces\n' +
  11038. 'result = run_face_compare(record_ftrs, feature)\n' +
  11039. 'if int(str(firmware_info.ai()).split("-")[0]) < 2021 and collectNum < len(names):\n' +
  11040. ' if(len(names) > result[2]):\n' +
  11041. ' if result[1] <= 80:\n' +
  11042. ' 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' +
  11043. ' if _gp_side_b.value() == True and side_button_state == False:\n' +
  11044. ' record_ftrs.append(feature)\n' +
  11045. ' collectNum = collectNum + 1\n' +
  11046. ' side_button_state = True\n' +
  11047. ' elif _gp_side_b.value() == False and side_button_state == True:\n' +
  11048. ' side_button_state = False\n' +
  11049. 'elif int(str(firmware_info.ai()).split("-")[0]) >= 2021 and collectNum < len(names):\n' +
  11050. ' if(len(names) > result[2]):\n' +
  11051. ' if result[1] <= ACCURACY:\n' +
  11052. ' 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' +
  11053. ' if _gp_side_b.value() == True and side_button_state == False:\n' +
  11054. ' record_ftrs.append(feature)\n' +
  11055. ' collectNum = collectNum + 1\n' +
  11056. ' side_button_state = True\n' +
  11057. ' elif _gp_side_b.value() == False and side_button_state == True:\n' +
  11058. ' side_button_state = False\n' +
  11059. 'if collectNum >= len(names):\n' +
  11060. ' try:\n' +
  11061. ' os.remove("/sd/user/' + value_class_input + '.py")\n' +
  11062. ' except:\n' +
  11063. ' pass\n' +
  11064. ' all = {}\n' +
  11065. ' try:\n' +
  11066. ' for j in range(len(names)):\n' +
  11067. ' all[names[j]] = record_ftrs[j]\n' +
  11068. ' _CREATE_TEXT_FILE_WITH_CONTENT("/sd/user/' + value_class_input + '.py", "all =" + str(all), "\\r\\n")\n' +
  11069. '\n' +
  11070. ' except:\n' +
  11071. ' pass\n' +
  11072. // 'if break_string == True:\n' +
  11073. // ' break\n' +
  11074. '\n';
  11075. return code;
  11076. };
  11077. Blockly.Blocks['ai_model_face_gather_num_judge'] = {
  11078. init: function () {
  11079. this.appendDummyInput()
  11080. .appendField(Blockly.Msg.ai_model_face_gather_num_judge);
  11081. this.setOutput(true, null);
  11082. this.setColour("#ee783a");
  11083. this.setTooltip(Blockly.Msg.ai_model_face_gather_num_judge_TOOLTIP);
  11084. this.setHelpUrl("");
  11085. }
  11086. };
  11087. Blockly.Python['ai_model_face_gather_num_judge'] = function (block) {
  11088. var code = 'collectNum >= len(names)';
  11089. return [code, Blockly.Python.ORDER_NONE];
  11090. };
  11091. Blockly.Blocks['ai_model_face_data_load'] = {
  11092. init: function () {
  11093. this.appendDummyInput()
  11094. .appendField(Blockly.Msg.ai_model_face_data_load);
  11095. this.appendValueInput("class_input")
  11096. .setCheck(null)
  11097. .appendField(Blockly.Msg.ai_model_face_data_load_1);
  11098. this.setPreviousStatement(true, null);
  11099. this.setNextStatement(true, null);
  11100. this.setColour("#ee783a");
  11101. this.setTooltip(Blockly.Msg.ai_model_face_data_load_TOOLTIP);
  11102. this.setHelpUrl("");
  11103. }
  11104. };
  11105. Blockly.Python['ai_model_face_data_load'] = function (block) {
  11106. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC).replace('\"', '').replace('\"', '');
  11107. Blockly.Python.addVariable('ai_model_face_data_load', `
  11108. try:
  11109. import user.${value_class_input} as recorded_face_features
  11110. except:
  11111. pass
  11112. `, true);
  11113. var code = '' +
  11114. '# load faces data\n' +
  11115. 'all = {}\n' +
  11116. 'record_ftrs = []\n' +
  11117. 'collectNum = len(names)\n' +
  11118. 'try:\n' +
  11119. ' all = recorded_face_features.all\n' +
  11120. ' for j in range(len(names)):\n' +
  11121. ' record_ftrs.append(all[names[j]])\n' +
  11122. '\n' +
  11123. 'except:\n' +
  11124. ' pass\n' +
  11125. '\n';
  11126. // 'all = recorded_face_features.all\n' +
  11127. return code;
  11128. };
  11129. Blockly.Blocks['ai_model_face_recognition'] = {
  11130. init: function () {
  11131. this.appendDummyInput()
  11132. .appendField(Blockly.Msg.ai_model_face_recognition);
  11133. this.setPreviousStatement(true, null);
  11134. this.setNextStatement(true, null);
  11135. this.setColour("#ee783a");
  11136. this.setTooltip(Blockly.Msg.ai_model_face_recognition_TOOLTIP);
  11137. this.setHelpUrl("");
  11138. }
  11139. };
  11140. Blockly.Python['ai_model_face_recognition'] = function (block) {
  11141. var code = '' +
  11142. 'if collectNum >= len(names):\n' +
  11143. ' result = run_face_compare(record_ftrs, feature)\n' +
  11144. ' if int(str(firmware_info.ai()).split("-")[0]) < 2021:\n' +
  11145. ' if(len(names) > result[2]):\n' +
  11146. ' if result[1] > 80:\n' +
  11147. ' 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' +
  11148. ' else:\n' +
  11149. ' if(len(names) > result[2]):\n' +
  11150. ' if result[1] > ACCURACY:\n' +
  11151. ' 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' +
  11152. ' res_index = result[2]\n' +
  11153. '\n' +
  11154. 'if break_string == True:\n' +
  11155. ' break\n' +
  11156. '\n' +
  11157. '';
  11158. return code;
  11159. };
  11160. Blockly.Blocks['ai_model_face_recognition_get_result'] = {
  11161. init: function () {
  11162. this.appendDummyInput()
  11163. .appendField(Blockly.Msg.ai_model_face_recognition_get_result);
  11164. // this.appendStatementInput("NAME")
  11165. // .setCheck(null)
  11166. // .appendField(Blockly.Msg.ai_speech_recognition_any_result_text_do);
  11167. this.setOutput(true);
  11168. this.setColour("#ee783a");
  11169. this.setTooltip(Blockly.Msg.ai_model_face_recognition_get_result_TOOLTIP);
  11170. this.setHelpUrl("");
  11171. }
  11172. };
  11173. Blockly.Python['ai_model_face_recognition_get_result'] = function (block) {
  11174. // var statements_name = Blockly.Python.statementToCode(block, 'NAME');
  11175. var code = '(len(names) > result[2] and collectNum >= len(names))';
  11176. // statements_name +
  11177. // 'if break_string == True:\n' +
  11178. // ' break\n' +
  11179. // 'if int(str(firmware_info.ai()).split("-")[0]) < 2021:\n' +
  11180. // ' ' + localStorage.getItem("ai_model_face_variable_name") + ' = ' + localStorage.getItem("ai_model_face_variable_name") + '.cut(40,0,240,240)\n' +
  11181. // ' a = lcd.display(' + localStorage.getItem("ai_model_face_variable_name") + ', oft=(0,0))\n' +
  11182. // ' gc.collect()\n' +
  11183. // ' # kpu.memtest()\n' +
  11184. // 'else:\n' +
  11185. // ' fps = clock.fps()\n' +
  11186. // ' a = lcd.display(' + localStorage.getItem("ai_model_face_variable_name") + ')\n' +
  11187. // ' gc.collect()\n' +
  11188. // ' # kpu.memtest()\n' +
  11189. // '\n' +
  11190. // '';
  11191. return [code, Blockly.Python.ORDER_NONE];
  11192. };
  11193. Blockly.Blocks['ai_model_getfaceresult'] = {
  11194. init: function () {
  11195. this.appendDummyInput()
  11196. .appendField(Blockly.Msg.ai_model_getface);
  11197. this.setOutput(true, null);
  11198. this.setColour("#ee783a");
  11199. this.setTooltip(Blockly.Msg.ai_model_getface_TOOLTIP);
  11200. this.setHelpUrl("");
  11201. }
  11202. };
  11203. Blockly.Python['ai_model_getfaceresult'] = function (block) {
  11204. // TODO: Assemble Python into code variable.
  11205. var code = 'names[res_index] if (res_index != -1 and len(names) > res_index and collectNum >= len(names)) else ""';
  11206. // TODO: Change ORDER_NONE to the correct strength.
  11207. return [code, Blockly.Python.ORDER_NONE];
  11208. };
  11209. Blockly.Blocks['ai_model_show_face'] = {
  11210. init: function () {
  11211. this.appendDummyInput()
  11212. .appendField(Blockly.Msg.ai_model_show_face);
  11213. this.setPreviousStatement(true, null);
  11214. this.setNextStatement(true, null);
  11215. this.setColour("#ee783a");
  11216. this.setTooltip(Blockly.Msg.ai_model_show_face_TOOLTIP);
  11217. this.setHelpUrl("");
  11218. }
  11219. };
  11220. Blockly.Python['ai_model_show_face'] = function (block) {
  11221. var code = '' +
  11222. // ' if break_string == True:\n' +
  11223. // ' break\n' +
  11224. 'lcd_draw_string(img, 240, 0, (str(num)), color = (255, 102, 0), scale = 2, mono_space = False)\n' +
  11225. 'if isCollect == 0:\n' +
  11226. ' lcd_draw_string(img, 40, 0, "按A识别上次样本", color = (255, 0, 0), scale = 1, mono_space = False)\n' +
  11227. ' lcd_draw_string(img, 40, 16, "按C重新拍摄", color = (255, 0, 0), scale = 1, mono_space = False)\n' +
  11228. 'elif isCollect == 1:\n' +
  11229. ' lcd_draw_string(img, 40, 0, "开始识别", color = (255, 0, 0), scale = 1, mono_space = False)\n' +
  11230. 'elif isCollect == 2:\n' +
  11231. ' lcd_draw_string(img, 40, 0, "开始采集人脸模板数据", color = (255, 0, 0), scale = 1, mono_space = False)\n' +
  11232. 'if int(str(firmware_info.ai()).split("-")[0]) < 2021:\n' +
  11233. ' ' + localStorage.getItem("ai_model_face_variable_name") + ' = ' + localStorage.getItem("ai_model_face_variable_name") + '.cut(40,0,240,240)\n' +
  11234. ' lcd.display(' + localStorage.getItem("ai_model_face_variable_name") + ', oft=(0,0))\n' +
  11235. ' gc.collect()\n' +
  11236. 'else:\n' +
  11237. ' fps = clock.fps()\n' +
  11238. ' a = lcd.display(' + localStorage.getItem("ai_model_face_variable_name") + ')\n' +
  11239. ' gc.collect()\n' +
  11240. '';
  11241. return code;
  11242. };
  11243. Blockly.Blocks['ai_model_class_setup'] = {
  11244. init: function () {
  11245. // this.appendDummyInput()
  11246. // .appendField(Blockly.Msg.ai_model_class_setup);
  11247. this.appendValueInput("class_input")
  11248. .setCheck(null)
  11249. .appendField(Blockly.Msg.ai_model_class_name);
  11250. this.setPreviousStatement(true, null);
  11251. this.setNextStatement(true, null);
  11252. this.setColour("#ee783a");
  11253. this.setTooltip(Blockly.Msg.ai_model_class_name.replace(":", ""));
  11254. this.setHelpUrl("");
  11255. }
  11256. };
  11257. Blockly.Python['ai_model_class_setup'] = function (block) {
  11258. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  11259. var code = `
  11260. ############### config #################
  11261. class_names = ` + value_class_input + `
  11262. class_num = len(class_names)
  11263. sample_num = len(class_names) * 5
  11264. THRESHOLD = 11
  11265. board_cube = 0
  11266. button_state = False
  11267. FPIOA().set_function(10, FPIOA.GPIO1)
  11268. key1 = GPIO(GPIO.GPIO1, GPIO.IN, GPIO.PULL_UP)
  11269. `;
  11270. return code;
  11271. };
  11272. Blockly.Blocks['ai_class_model'] = {
  11273. init: function () {
  11274. this.appendDummyInput()
  11275. .appendField(Blockly.Msg.ai_class_model);
  11276. this.setPreviousStatement(true, null);
  11277. this.setNextStatement(true, null);
  11278. this.setColour("#ee783a");
  11279. this.setTooltip(Blockly.Msg.ai_class_model_TOOLTIP);
  11280. this.setHelpUrl("");
  11281. }
  11282. };
  11283. Blockly.Python['ai_class_model'] = function (block) {
  11284. Blockly.Python.addVariable('ai_class_model', `
  11285. try:
  11286. del model
  11287. except Exception:
  11288. pass
  11289. try:
  11290. del classifier
  11291. except Exception:
  11292. pass
  11293. gc.collect()
  11294. cap_num = 0
  11295. train_status = 0
  11296. last_cap_time = 0
  11297. last_btn_status = 0
  11298. res_index = -1
  11299. is_save = 0
  11300. `)
  11301. var code = 'model = kpu.load("/sd/preset/models/object_classifier.emodel")\n';
  11302. return code;
  11303. };
  11304. Blockly.Blocks['ai_class_model_cluster'] = {
  11305. init: function () {
  11306. this.appendDummyInput()
  11307. .appendField(Blockly.Msg.ai_class_model_cluster);
  11308. this.setPreviousStatement(true, null);
  11309. this.setNextStatement(true, null);
  11310. this.setColour("#ee783a");
  11311. this.setTooltip(Blockly.Msg.ai_class_model_cluster_TOOLTIP);
  11312. this.setHelpUrl("");
  11313. }
  11314. };
  11315. Blockly.Python['ai_class_model_cluster'] = function (block) {
  11316. // Blockly.Python.addVariable('ai_class_model_cluster', `
  11317. // `)
  11318. var code = 'classifier = kpu.classifier(model, class_num, sample_num)\n';
  11319. return code;
  11320. };
  11321. Blockly.Blocks['ai_model_class_start_load'] = {
  11322. init: function () {
  11323. this.appendDummyInput()
  11324. .appendField(Blockly.Msg.ai_models_customized_init_setup_source)
  11325. .appendField(new Blockly.FieldVariable("img"), "varitem");
  11326. this.appendDummyInput()
  11327. .appendField(Blockly.Msg.ai_model_class_start_load);
  11328. // this.appendStatementInput("NAME")
  11329. // .setCheck(null)
  11330. // .appendField(Blockly.Msg.ai_speech_recognition_any_result_text_do);
  11331. this.setInputsInline(false);
  11332. this.setPreviousStatement(true, null);
  11333. this.setNextStatement(true, null);
  11334. this.setColour("#ee783a");
  11335. this.setTooltip(Blockly.Msg.ai_model_class_start_load_TOOLTIP);
  11336. this.setHelpUrl("");
  11337. }
  11338. }
  11339. Blockly.Python['ai_model_class_start_load'] = function (block) {
  11340. // var statements_name = Blockly.Python.statementToCode(block, 'NAME');
  11341. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  11342. localStorage.setItem("ai_model_class_start_load", variable_name);
  11343. var code = '' +
  11344. 'if board_cube:\n' +
  11345. ' ' + variable_name + ' = ' + variable_name + '.rotation_corr(z_rotation=90)\n' +
  11346. ' ' + variable_name + '.pix_to_ai()\n' +
  11347. '\n';
  11348. return code;
  11349. }
  11350. Blockly.Blocks['ai_model_class_data_load'] = {
  11351. init: function () {
  11352. this.appendDummyInput()
  11353. .appendField(Blockly.Msg.ai_model_class_data_load);
  11354. this.appendValueInput("class_input")
  11355. .setCheck(null)
  11356. .appendField(Blockly.Msg.ai_model_class_data_load_1);
  11357. this.setPreviousStatement(true, null);
  11358. this.setNextStatement(true, null);
  11359. this.setColour("#ee783a");
  11360. this.setTooltip(Blockly.Msg.ai_model_class_data_load_TOOLTIP);
  11361. this.setHelpUrl("");
  11362. }
  11363. };
  11364. Blockly.Python['ai_model_class_data_load'] = function (block) {
  11365. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC).replace('\"', '').replace('\"', '');
  11366. var code = '' +
  11367. 'try:\n' +
  11368. ' saved_path = "/sd/user/' + value_class_input + '.classifier"\n' +
  11369. ' f=open(saved_path,"r")\n' +
  11370. ' classifier, class_num, sample_num = kpu.classifier.load(model, saved_path)\n' +
  11371. ' train_status = 1\n' +
  11372. 'except:\n' +
  11373. ' ' + localStorage.getItem("ai_model_class_start_load") + '.draw_rectangle(0, 0, 240,20, fill=True, color=(0,0,0))\n' +
  11374. ' 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' +
  11375. '\n';
  11376. return code;
  11377. };
  11378. Blockly.Blocks['ai_model_class_gather_load_finish'] = {
  11379. init: function () {
  11380. this.appendDummyInput()
  11381. .appendField(Blockly.Msg.ai_model_class_gather_load_finish);
  11382. this.setOutput(true, null);
  11383. this.setColour("#ee783a");
  11384. this.setTooltip(Blockly.Msg.ai_model_class_gather_load_finish_TOOLTIP);
  11385. this.setHelpUrl("");
  11386. }
  11387. };
  11388. Blockly.Python['ai_model_class_gather_load_finish'] = function (block) {
  11389. var code = 'train_status == 0';
  11390. return [code, Blockly.Python.ORDER_NONE];
  11391. };
  11392. Blockly.Blocks['ai_model_class_cluster_train'] = {
  11393. init: function () {
  11394. this.appendDummyInput()
  11395. .appendField(Blockly.Msg.ai_model_class_cluster_train);
  11396. this.appendValueInput("class_input")
  11397. .setCheck(null)
  11398. .appendField(Blockly.Msg.ai_model_class_cluster_train_1);
  11399. this.setPreviousStatement(true, null);
  11400. this.setNextStatement(true, null);
  11401. this.setColour("#ee783a");
  11402. this.setTooltip(Blockly.Msg.ai_model_class_cluster_train_TOOLTIP);
  11403. this.setHelpUrl("");
  11404. }
  11405. };
  11406. Blockly.Python['ai_model_class_cluster_train'] = function (block) {
  11407. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC).replace('\"', '').replace('\"', '');
  11408. var code = '' +
  11409. 'if cap_num >= class_num + sample_num and train_status == 0:\n' +
  11410. ' ' + localStorage.getItem("ai_model_class_start_load") + '.draw_rectangle(58,98, len("training...")*10+8 , 24, fill=True, color=lcd.RED)\n' +
  11411. ' ' + 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' +
  11412. ' lcd.display(' + localStorage.getItem("ai_model_class_start_load") + ')\n' +
  11413. ' try:\n' +
  11414. ' classifier.train()\n' +
  11415. ' saved_path = "/sd/user/' + value_class_input + '.classifier"\n' +
  11416. ' f=open(saved_path,"r")\n' +
  11417. ' os.remove("/sd/user/' + value_class_input + '.classifier")\n' +
  11418. ' classifier.save("/sd/user/' + value_class_input + '.classifier")\n' +
  11419. ' except:\n' +
  11420. ' classifier.save("/sd/user/' + value_class_input + '.classifier")\n' +
  11421. ' time.sleep(2)\n' +
  11422. ' train_status = 1\n' +
  11423. '\n';
  11424. return code;
  11425. };
  11426. Blockly.Blocks['ai_model_class_recognition'] = {
  11427. init: function () {
  11428. this.appendDummyInput()
  11429. .appendField(Blockly.Msg.ai_model_class_recognition);
  11430. this.setPreviousStatement(true, null);
  11431. this.setNextStatement(true, null);
  11432. this.setColour("#ee783a");
  11433. this.setTooltip(Blockly.Msg.ai_model_class_recognition_TOOLTIP);
  11434. this.setHelpUrl("");
  11435. }
  11436. };
  11437. Blockly.Python['ai_model_class_recognition'] = function (block) {
  11438. var code = '' +
  11439. '# predict\n' +
  11440. 'if train_status == 1:\n' +
  11441. ' res_index = -1\n' +
  11442. ' try:\n' +
  11443. ' res_index, min_dist = classifier.predict(' + localStorage.getItem("ai_model_class_start_load") + ')\n' +
  11444. ' except Exception as e:\n' +
  11445. ' print("predict err:", e)\n' +
  11446. ' if res_index >= 0 and min_dist < THRESHOLD :\n' +
  11447. ' ' + 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' +
  11448. ' ' + 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' +
  11449. ' else:\n' +
  11450. ' ' + 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' +
  11451. ' ' + 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' +
  11452. '\n';
  11453. return code;
  11454. };
  11455. Blockly.Blocks['ai_model_show_class'] = {
  11456. init: function () {
  11457. this.appendDummyInput()
  11458. .appendField(Blockly.Msg.ai_model_show_face);
  11459. this.setPreviousStatement(true, null);
  11460. this.setNextStatement(true, null);
  11461. this.setColour("#ee783a");
  11462. this.setTooltip(Blockly.Msg.ai_model_show_face_TOOLTIP);
  11463. this.setHelpUrl("");
  11464. }
  11465. };
  11466. Blockly.Python['ai_model_show_class'] = function (block) {
  11467. var code = '' +
  11468. '' + localStorage.getItem("ai_model_class_start_load") + ' = ' + localStorage.getItem("ai_model_class_start_load") + '.cut(0,0,240,240)\n' +
  11469. 'lcd.display(' + localStorage.getItem("ai_model_class_start_load") + ', oft=(0,0))\n' +
  11470. '';
  11471. return code;
  11472. };
  11473. Blockly.Blocks['ai_model_customized_init_setup'] = {
  11474. init: function () {
  11475. this.appendDummyInput()
  11476. .appendField(Blockly.Msg.ai_models_customized_init_setup_title);
  11477. this.appendDummyInput()
  11478. .appendField(Blockly.Msg.ai_models_customized_init_setup_path)
  11479. .appendField(new Blockly.FieldTextInput("yolov2_int8"), "model_path");
  11480. this.appendValueInput("class_input")
  11481. .setCheck(null)
  11482. .appendField(Blockly.Msg.ai_models_customized_init_setup_name);
  11483. this.setPreviousStatement(true, null);
  11484. this.setNextStatement(true, null);
  11485. this.setColour("#ee783a");
  11486. this.setTooltip(Blockly.Msg.ai_model_customized_init_setup_TOOLTIP);
  11487. this.setHelpUrl("");
  11488. }
  11489. };
  11490. Blockly.Python['ai_model_customized_init_setup'] = function (block) {
  11491. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  11492. var text_model_path = block.getFieldValue('model_path');
  11493. Blockly.Python.definitions_['v831_import_from_maix_nn'] = "from maix import nn";
  11494. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  11495. let path = `/root/user/model/${text_model_path}`
  11496. if (text_model_path == "autologistics") {
  11497. path = `/root/preset/model/${text_model_path}`
  11498. }
  11499. let code = `class Yolo:
  11500. labels = ${value_class_input}
  11501. anchors = [1.19, 1.98, 2.79, 4.59, 4.53, 8.92, 8.06, 5.29, 10.32, 10.65]
  11502. m = {
  11503. "param": "${path}.param",
  11504. "bin": "${path}.bin"
  11505. }
  11506. options = {
  11507. "model_type": "awnn",
  11508. "inputs": {
  11509. "input0": (224, 224, 3)
  11510. },
  11511. "outputs": {
  11512. "output0": (7, 7, (1+4+len(labels))*5)
  11513. },
  11514. "mean": [127.5, 127.5, 127.5],
  11515. "norm": [0.0078125, 0.0078125, 0.0078125],
  11516. }
  11517. def __init__(self):
  11518. from maix import nn
  11519. from maix.nn import decoder
  11520. self.model = nn.load(self.m, opt=self.options)
  11521. self.decoder = decoder.Yolo2(len(self.labels), self.anchors, net_in_size=(224, 224), net_out_size=(7, 7))
  11522. def __del__(self):
  11523. del self.model
  11524. del self.decoder
  11525. Yolo = Yolo()
  11526. `;
  11527. return code;
  11528. };
  11529. Blockly.Blocks['ai_model_customized_load_setup'] = {
  11530. init: function () {
  11531. this.appendDummyInput()
  11532. .appendField(Blockly.Msg.ai_model_customized_load_setup_title);
  11533. this.appendDummyInput()
  11534. .appendField(Blockly.Msg.ai_models_customized_init_setup_path)
  11535. .appendField(new Blockly.FieldTextInput("yolov2_int8"), "model_path");
  11536. // this.appendDummyInput()
  11537. // .appendField(Blockly.Msg.ai_models_customized_init_setup_source)
  11538. // .appendField(new Blockly.FieldVariable("img_modelrecognition"), "varitem");
  11539. this.appendValueInput("class_input")
  11540. .setCheck(null)
  11541. .appendField(Blockly.Msg.ai_models_customized_init_setup_name);
  11542. this.setPreviousStatement(true, null);
  11543. this.setNextStatement(true, null);
  11544. this.setColour("#ee783a");
  11545. this.setTooltip(Blockly.Msg.ai_model_customized_load_setup_TOOLTIP);
  11546. this.setHelpUrl("");
  11547. }
  11548. };
  11549. Blockly.Python['ai_model_customized_load_setup'] = function (block) {
  11550. var value_class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  11551. var text_model_path = block.getFieldValue('model_path');
  11552. Blockly.Python.definitions_['v831_import_from_maix_nn'] = "from maix import nn";
  11553. Blockly.Python.definitions_['v831_import_from_maixnn_decoder'] = `from maix.nn import decoder`
  11554. let path = `/root/user/model/${text_model_path}`
  11555. if (text_model_path == "autologistics") {
  11556. path = `/root/preset/model/${text_model_path}`
  11557. }
  11558. Blockly.Python.definitions_['CLASSYOLO'] = `class Yolo:
  11559. labels = ${value_class_input}
  11560. anchors = [1.19, 1.98, 2.79, 4.59, 4.53, 8.92, 8.06, 5.29, 10.32, 10.65]
  11561. m = {
  11562. "param": "${path}.param",
  11563. "bin": "${path}.bin"
  11564. }
  11565. options = {
  11566. "model_type": "awnn",
  11567. "inputs": {
  11568. "input0": (224, 224, 3)
  11569. },
  11570. "outputs": {
  11571. "output0": (7, 7, (1+4+len(labels))*5)
  11572. },
  11573. "mean": [127.5, 127.5, 127.5],
  11574. "norm": [0.0078125, 0.0078125, 0.0078125],
  11575. }
  11576. def __init__(self):
  11577. self.model = nn.load(self.m, opt=self.options)
  11578. self.decoder = decoder.Yolo2(len(self.labels), self.anchors, net_in_size=(224, 224), net_out_size=(7, 7))
  11579. self.FACESYOLOS = {
  11580. "FACE_START_X": [],
  11581. "FACE_START_Y": [],
  11582. "FACE_CENTET_X": [],
  11583. "FACE_CENTET_Y": [],
  11584. "FACE_WIDTH": [],
  11585. "FACE_HEIGHT": [],
  11586. "FACE_RESULT": [],
  11587. "FACE_CONFIDENCE": [],
  11588. }
  11589. def __del__(self):
  11590. del self.model
  11591. del self.decoder
  11592. def faceRecognizeShowResult(self,canvas):
  11593. out = self.model.forward(canvas, quantize=True, layout="hwc")
  11594. BOXES, PROBS = self.decoder.run(out, nms=0.3, threshold=0.3, img_size=(224, 224))
  11595. if len(BOXES):
  11596. for BOXESI, BOXOBJ in enumerate(BOXES):
  11597. 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)
  11598. canvas.draw_rectangle((BOXOBJ[0]),(BOXOBJ[1]), (BOXOBJ[0])+(BOXOBJ[2]),(BOXOBJ[1])+ (BOXOBJ[3]), color=(255,0,0), thickness=1)
  11599. BOXES[BOXESI].append(PROBS[BOXESI])
  11600. return BOXES
  11601. FaceYolo = Yolo()
  11602. `;
  11603. var code = `canvas = canvas.crop(48,8,224,224)
  11604. BOXES = FaceYolo.faceRecognizeShowResult(canvas)
  11605. `;
  11606. return code;
  11607. };
  11608. Blockly.Blocks['ai_model_customized_get_result'] = {
  11609. init: function () {
  11610. this.appendDummyInput()
  11611. .appendField(Blockly.Msg.ai_models_from)
  11612. .appendField(Blockly.Msg.ai_models_customized_get_result_any)
  11613. .appendField(new Blockly.FieldDropdown([
  11614. [Blockly.Msg.ai_models_customized_get_result_name, "classid"],
  11615. [Blockly.Msg.ai_models_customized_get_result_x, "x"],
  11616. [Blockly.Msg.ai_models_customized_get_result_y, "y"],
  11617. // [Blockly.Msg.ai_models_customized_get_result_w, "w"],
  11618. // [Blockly.Msg.ai_models_customized_get_result_h, "h"],
  11619. // [Blockly.Msg.ai_models_customized_get_result_x_center, "cx"],
  11620. // [Blockly.Msg.ai_models_customized_get_result_y_center, "cy"],
  11621. // [Blockly.Msg.ai_models_customized_get_result_count, "objnum"],
  11622. // [Blockly.Msg.ai_models_customized_get_result_index, "index"],
  11623. [Blockly.Msg.ai_models_customized_get_result_confidence, "value"],
  11624. [Blockly.Msg.ai_models_face_model_ws, "2"],
  11625. [Blockly.Msg.ai_models_face_model_hs, "3"],
  11626. [Blockly.Msg.ai_models_object_model_x_center, "CX"],
  11627. [Blockly.Msg.ai_models_object_model_y_center, "CY"]
  11628. ]), "get_results");
  11629. this.setOutput(true, null);
  11630. this.setColour("#ee783a");
  11631. var thisBlock = this;
  11632. this.setTooltip(function () {
  11633. var mode = thisBlock.getFieldValue('get_results');
  11634. var TOOLTIPS = {
  11635. 'classid': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_name),
  11636. 'x': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_x),
  11637. 'y': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_y),
  11638. 'w': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_w),
  11639. 'h': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_h),
  11640. 'cx': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_x_center),
  11641. 'cy': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_y_center),
  11642. 'objnum': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_count),
  11643. 'index': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_index),
  11644. 'value': Blockly.Msg.ai_model_customized_get_result_TOOLTIP.replace('%1', Blockly.Msg.ai_models_customized_get_result_confidence)
  11645. };
  11646. return TOOLTIPS[mode];
  11647. });
  11648. this.setHelpUrl("");
  11649. }
  11650. };
  11651. Blockly.Python['ai_model_customized_get_result'] = function (block) {
  11652. var variable_name = "BOXESI";
  11653. var dropdown_get_results = block.getFieldValue('get_results');
  11654. // TODO: Assemble Python into code variable.
  11655. var code = '';
  11656. if (dropdown_get_results == "classid") {
  11657. code = 'Yolo.labels[' + variable_name + '[4][0]]';
  11658. } else if (dropdown_get_results == 'x') {
  11659. code = variable_name + '[0]';
  11660. } else if (dropdown_get_results == "y") {
  11661. code = variable_name + "[1]";
  11662. } else if (dropdown_get_results == "w") {
  11663. code = `${variable_name}[0]+${variable_name}[2]`;
  11664. } else if (dropdown_get_results == "h") {
  11665. code = `${variable_name}[1]+${variable_name}[3]`;
  11666. } else if (dropdown_get_results == "value") {
  11667. code = `${variable_name}[4][1][${variable_name}[4][0]]`;
  11668. } else if (dropdown_get_results == "CX") {
  11669. code = `int((${variable_name}[0] + ${variable_name}[2])/2)`;
  11670. } else if (dropdown_get_results == "CY") {
  11671. code = `int((${variable_name}[1] + ${variable_name}[3])/2)`;
  11672. }
  11673. else {
  11674. code = `${variable_name}[${dropdown_get_results}]`;
  11675. }
  11676. // TODO: Change ORDER_NONE to the correct strength.
  11677. return [code, Blockly.Python.ORDER_NONE];
  11678. };
  11679. Blockly.Blocks['ai_model_customized_if_got_new'] = {
  11680. init: function () {
  11681. this.appendDummyInput()
  11682. .appendField(Blockly.Msg.ai_model_customized_attribute)
  11683. this.appendStatementInput("input")
  11684. .setCheck(null)
  11685. .appendField(Blockly.Msg.basic_motion_statement_exec);
  11686. this.updateShape_();
  11687. this.setMutator(new Blockly.Mutator(['ai_model_false_create_with_item']));
  11688. this.setColour("#ee783a");
  11689. this.setPreviousStatement(true, null);
  11690. this.setNextStatement(true, null);
  11691. this.setTooltip("");
  11692. this.setHelpUrl("");
  11693. this.itemCount_ = 0;
  11694. },
  11695. mutationToDom: function () {
  11696. if (!this.itemCount_)
  11697. return null;
  11698. var container = document.createElement('mutation');
  11699. this.itemCount_ && container.setAttribute('aielse', 1);
  11700. return container;
  11701. },
  11702. domToMutation: function (a) {
  11703. this.itemCount_ = parseInt(a.getAttribute('aielse'), 10);
  11704. this.updateShape_();
  11705. },
  11706. decompose: function (a) {
  11707. var b = a.newBlock('ai_model_false_create_with_container');
  11708. b.initSvg();
  11709. var c = b.nextConnection
  11710. this.itemCount_ && (a = a.newBlock("ai_model_false_create_with_item"),
  11711. a.initSvg(),
  11712. c.connect(a.previousConnection));
  11713. console.log("this.elseCount_",a)
  11714. return b
  11715. },
  11716. compose: function (a) {
  11717. var b = a.nextConnection.targetBlock();
  11718. this.itemCount_ = 0;
  11719. var e = null;
  11720. if (b && b.type === "ai_model_false_create_with_item") {
  11721. this.itemCount_++;
  11722. e = b.statementConnection_;
  11723. b = b.nextConnection && b.nextConnection.targetBlock()
  11724. }
  11725. this.updateShape_()
  11726. Blockly.Mutator.reconnect(e, this, "AIELSE")
  11727. },
  11728. saveConnections: function (a) {
  11729. a = a.nextConnection.targetBlock();
  11730. if (a && a.type === "ai_model_false_create_with_item") {
  11731. e = this.getInput("AIELSE");
  11732. a.statementConnection_ = e && e.connection.targetConnection;
  11733. a = a.nextConnection && a.nextConnection.targetBlock()
  11734. }
  11735. },
  11736. updateShape_: function () {
  11737. this.getInput("AIELSE") && this.removeInput("AIELSE");
  11738. console.log("this.itemCount_",this.itemCount_)
  11739. this.itemCount_ && this.appendStatementInput("AIELSE").appendField(Blockly.Msg.CONTROLS_IF_MSG_ELSE)
  11740. },
  11741. };
  11742. Blockly.Python['ai_model_customized_if_got_new'] = function (block) {
  11743. var statements_input = Blockly.Python.statementToCode(block, 'input');
  11744. var statements_input1 = Blockly.Python.statementToCode(block, 'AIELSE');
  11745. let allBlocks = block.workspace.getAllBlocks();
  11746. let global = ""
  11747. try {
  11748. global = allBlocks[0].workspace.variableList.toString()
  11749. }
  11750. catch (e) {
  11751. console.log(e)
  11752. }
  11753. Blockly.Python.addFunction("cardStatements", `def cardStatements(BOXESI):
  11754. ${global != "" ? `global ${global}` : "pass"}
  11755. ${statements_input}
  11756. `)
  11757. var code = `if len(BOXES) > 0:
  11758. for BOXESI in BOXES:
  11759. cardStatements(BOXESI)
  11760. else:
  11761. ${statements_input1 != "" ? statements_input1 : " pass"}
  11762. `
  11763. return code;
  11764. };
  11765. Blockly.Blocks['ai_model_customized_if_got'] = {
  11766. init: function () {
  11767. this.appendDummyInput()
  11768. .appendField(Blockly.Msg.ai_models_customized_if_got);
  11769. this.setNextStatement(true, null);
  11770. this.setPreviousStatement(true, null);
  11771. this.setColour("#ee783a");
  11772. this.setTooltip(Blockly.Msg.ai_models_customized_if_got);
  11773. this.setHelpUrl("");
  11774. }
  11775. };
  11776. Blockly.Python['ai_model_customized_if_got'] = function (block) {
  11777. Blockly.Python.addFunction("faceRecognizeShowResult", `def faceRecognizeShowResult():
  11778. global BOXES
  11779. if len(BOXES):
  11780. for BOXESI, BOXOBJ in enumerate(BOXES):
  11781. 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)
  11782. canvas.draw_rectangle((BOXOBJ[0]),(BOXOBJ[1]), (BOXOBJ[0])+(BOXOBJ[2]),(BOXOBJ[1])+ (BOXOBJ[3]), color=(255,0,0), thickness=1)
  11783. `)
  11784. var code = 'faceRecognizeShowResult()\n';
  11785. return code;
  11786. };
  11787. Blockly.Blocks['ai_model_customized_attribute'] = {
  11788. init: function () {
  11789. this.appendDummyInput()
  11790. .appendField(Blockly.Msg.ai_model_customized_attribute);
  11791. this.setOutput(true, null);
  11792. this.setColour("#ee783a");
  11793. this.setTooltip(Blockly.Msg.ai_model_customized_attribute_TOOLTIP);
  11794. this.setHelpUrl("");
  11795. }
  11796. };
  11797. Blockly.Python['ai_model_customized_attribute'] = function (block) {
  11798. var code = 'len(BOXES)';
  11799. return [code, Blockly.Python.ORDER_NONE];
  11800. };
  11801. /*
  11802. _ ___ ____ _
  11803. / \ |_ _| / ___| _ _ ___| |_ ___ _ __ ___
  11804. / _ \ | | \___ \| | | / __| __/ _ \ '_ ` _ \
  11805. / ___ \ | | ___) | |_| \__ \ || __/ | | | | |
  11806. /_/ \_\___| |____/ \__, |___/\__\___|_| |_| |_|
  11807. |___/
  11808. */
  11809. Blockly.Blocks['ai_system_off'] = {
  11810. init: function () {
  11811. this.appendDummyInput()
  11812. .appendField(Blockly.Msg.ai_system_off);
  11813. this.setPreviousStatement(true);
  11814. this.setNextStatement(true);
  11815. this.setColour("#22398e");
  11816. this.setTooltip(Blockly.Msg.ai_system_restart_TOOLTIP);
  11817. this.setHelpUrl("");
  11818. }
  11819. };
  11820. Blockly.Python.ai_system_off = function (block) {
  11821. Blockly.Python.definitions_['v831_import_os'] = `import os`
  11822. var _code = `os.system("poweroff")
  11823. `
  11824. return _code;
  11825. }
  11826. Blockly.Blocks['ai_system_restart'] = {
  11827. init: function () {
  11828. this.appendDummyInput()
  11829. .appendField(Blockly.Msg.ai_system_restart);
  11830. this.setPreviousStatement(true);
  11831. this.setNextStatement(true);
  11832. this.setColour("#22398e");
  11833. this.setTooltip(Blockly.Msg.ai_system_off_TOOLTIP);
  11834. this.setHelpUrl("");
  11835. }
  11836. };
  11837. Blockly.Python.ai_system_restart = function (block) {
  11838. Blockly.Python.definitions_['v831_import_os'] = `import os`
  11839. var _code = `os.system("reboot")
  11840. `
  11841. return _code;
  11842. }
  11843. Blockly.Blocks['ai_system_refresh'] = {
  11844. init: function () {
  11845. this.appendDummyInput()
  11846. .appendField(Blockly.Msg.ai_system_refresh);
  11847. this.setPreviousStatement(true);
  11848. this.setNextStatement(true);
  11849. this.setColour("#22398e");
  11850. this.setTooltip(Blockly.Msg.ai_system_refresh_TOOLTIP);
  11851. this.setHelpUrl("");
  11852. }
  11853. };
  11854. Blockly.Python.ai_system_refresh = function (block) {
  11855. Blockly.Python.definitions_['v831_import_os'] = `import os`
  11856. var _code = `os.system("sync")
  11857. `
  11858. return _code;
  11859. }
  11860. Blockly.Blocks['ai_system_volume'] = {
  11861. init: function () {
  11862. this.appendDummyInput()
  11863. .appendField(Blockly.Msg.system_voice)
  11864. .appendField(new Blockly.FieldNumber(25, 0, 100), "play_vol");
  11865. this.setPreviousStatement(true);
  11866. this.setNextStatement(true);
  11867. this.setColour("#22398e");
  11868. }
  11869. }
  11870. Blockly.Python.ai_system_volume = function (block) {
  11871. var number_play_vol = block.getFieldValue('play_vol');
  11872. Blockly.Python.definitions_['v831_import_os'] = `import os`
  11873. Blockly.Python.definitions_['systemVoice_numberMap'] = `def voice_numberMap(value):
  11874. valueScaled = float(value - 0) / float(100)
  11875. return int(valueScaled * 31)
  11876. `
  11877. var _code = `VOICENUMP = str(voice_numberMap(${number_play_vol}))
  11878. time.sleep(0.01)
  11879. SYSTEMVOICE = "amixer cset numid=8,iface=MIXER,name=\\"LINEOUT volume\\" "+ VOICENUMP+""
  11880. os.system(SYSTEMVOICE)
  11881. `
  11882. return _code;
  11883. }
  11884. Blockly.Blocks['ai_system_thread'] = {
  11885. init: function () {
  11886. this.appendDummyInput()
  11887. .appendField(Blockly.Msg.ai_system_thread_setting)
  11888. .appendField(new Blockly.FieldNumber(1, 1, 5), "thread")
  11889. this.appendStatementInput("input")
  11890. .setCheck(null)
  11891. .appendField(Blockly.Msg.basic_motion_statement_exec);
  11892. this.setColour("#22398e");
  11893. this.setTooltip(Blockly.Msg.ai_system_thread_setting);
  11894. this.setHelpUrl("");
  11895. }
  11896. };
  11897. Blockly.Python['ai_system_thread'] = function (block) {
  11898. var threadNum = block.getFieldValue('thread');
  11899. var statements_input = Blockly.Python.statementToCode(block, 'input');
  11900. Blockly.Python.definitions_['v831_import_threading'] = `import threading`
  11901. let allBlocks = block.workspace.getAllBlocks();
  11902. let global = ""
  11903. try {
  11904. global = allBlocks[0].workspace.variableList.toString()
  11905. }
  11906. catch (e) {
  11907. console.log(e)
  11908. }
  11909. Blockly.Python.definitions_['v831_import_thread_calsss_fun' + threadNum] = `def thread_calsss_fun${threadNum}():
  11910. ${global != "" ? global : ""}
  11911. ${statements_input}
  11912. CocoPiThread${threadNum} = threading.Thread(target=thread_calsss_fun${threadNum})
  11913. `
  11914. var code = ``;
  11915. return code;
  11916. };
  11917. Blockly.Blocks['ai_system_thread_create_with_container'] = {
  11918. init: function () {
  11919. this.setColour("#22398e");
  11920. this.appendDummyInput()
  11921. .appendField(Blockly.Msg.serialcomm_write_item);
  11922. this.appendStatementInput('STACK');
  11923. this.setTooltip('');
  11924. this.contextMenu = false;
  11925. }
  11926. };
  11927. Blockly.Blocks['ai_system_thread_data_create_with_item'] = {
  11928. init: function () {
  11929. this.setColour("#22398e");
  11930. this.appendDummyInput()
  11931. .appendField(Blockly.Msg.ai_systen_thread_val);
  11932. this.setPreviousStatement(true);
  11933. this.setNextStatement(true);
  11934. this.setTooltip('');
  11935. this.contextMenu = false;
  11936. }
  11937. };
  11938. Blockly.Blocks['ai_system_thread_set_setDaemon'] = {
  11939. init: function () {
  11940. this.appendDummyInput()
  11941. .appendField(Blockly.Msg.ai_system_thread_setting).appendField(new Blockly.FieldNumber(1, 1, 5), "thread")
  11942. .appendField(Blockly.Msg.digital_set_as).appendField(new Blockly.FieldDropdown([
  11943. [Blockly.Msg.ai_system_thread_setDaemon, "True"],
  11944. [Blockly.Msg.ai_system_thread_not_setDaemon, "False"]
  11945. ]), "setDaemon");
  11946. this.setPreviousStatement(true, null);
  11947. this.setNextStatement(true, null)
  11948. this.setColour("#22398e");
  11949. this.setTooltip(Blockly.Msg.ai_system_thread_TOOLTIP);
  11950. this.setHelpUrl("");
  11951. }
  11952. };
  11953. Blockly.Python['ai_system_thread_set_setDaemon'] = function (block) {
  11954. var threadNum = block.getFieldValue('thread');
  11955. var queue_value = block.getFieldValue('setDaemon');
  11956. var code = `CocoPiThread${threadNum}.setDaemon(${queue_value})
  11957. `;
  11958. return code;
  11959. };
  11960. Blockly.Blocks['ai_system_thread_set_join'] = {
  11961. init: function () {
  11962. this.appendDummyInput()
  11963. .appendField(Blockly.Msg.ai_system_thread_setting)
  11964. .appendField(new Blockly.FieldNumber(1, 1, 5), "thread")
  11965. .appendField(Blockly.Msg.ai_system_thread_set_join)
  11966. this.setPreviousStatement(true, null);
  11967. this.setNextStatement(true, null)
  11968. this.setColour("#22398e");
  11969. this.setTooltip(Blockly.Msg.ai_system_thread_TOOLTIP);
  11970. this.setHelpUrl("");
  11971. }
  11972. };
  11973. Blockly.Python['ai_system_thread_set_join'] = function (block) {
  11974. var threadNum = block.getFieldValue('thread');
  11975. var code = `CocoPiThread${threadNum}.join()
  11976. `;
  11977. return code;
  11978. };
  11979. Blockly.Blocks['ai_system_thread_start'] = {
  11980. init: function () {
  11981. this.appendDummyInput()
  11982. .appendField(Blockly.Msg.ai_system_thread).appendField(new Blockly.FieldNumber(1, 1, 5), "thread");
  11983. // this.appendStatementInput("input")
  11984. // .setCheck(null)
  11985. // .appendField(Blockly.Msg.basic_motion_statement_exec);
  11986. this.setPreviousStatement(true, null);
  11987. this.setNextStatement(true, null)
  11988. this.setColour("#22398e");
  11989. this.setTooltip(Blockly.Msg.ai_system_thread_TOOLTIP);
  11990. this.setHelpUrl("");
  11991. }
  11992. };
  11993. Blockly.Python['ai_system_thread_start'] = function (block) {
  11994. var threadNum = block.getFieldValue('thread');
  11995. // var statements_input = Blockly.Python.statementToCode(block, 'input');
  11996. var code = `CocoPiThread${threadNum}.start()
  11997. `;
  11998. return code;
  11999. };
  12000. Blockly.Blocks["ai_system_set_queue"] = {
  12001. init: function () {
  12002. this.appendDummyInput()
  12003. .appendField(Blockly.Msg.ai_systen_queue_aritem)
  12004. this.appendValueInput("queueVaritem")
  12005. .setCheck(null);
  12006. this.appendDummyInput().appendField(Blockly.Msg.ai_systen_queue_put)
  12007. .appendField(new Blockly.FieldDropdown([
  12008. ["1", "1"],
  12009. ["2", "2"],
  12010. ["3", "3"],
  12011. ["4", "4"],
  12012. ["5", "5"]
  12013. ]), "queue_value");
  12014. this.setPreviousStatement(true, null);
  12015. this.setNextStatement(true, null);
  12016. this.setColour("#22398e");
  12017. this.setTooltip("");
  12018. }
  12019. }
  12020. Blockly.Python['ai_system_set_queue'] = function (block) {
  12021. var variable_name = Blockly.Python.valueToCode(block, 'queueVaritem', Blockly.Python.ORDER_ATOMIC);
  12022. // var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('varitem'), Blockly.Variables.NAME_TYPE);
  12023. var queue_value = block.getFieldValue('queue_value');
  12024. Blockly.Python.definitions_["v831_import_queue"] = `import queue`
  12025. Blockly.Python.definitions_["v831_import_queue_init" + queue_value] = `queue${queue_value} = queue.Queue()`
  12026. var code = `queue${queue_value}.put(${variable_name})
  12027. `;
  12028. return code;
  12029. }
  12030. Blockly.Blocks["ai_system_get_queue"] = {
  12031. init: function () {
  12032. this.appendDummyInput()
  12033. .appendField(Blockly.Msg.ai_systen_queue_get)
  12034. .appendField(new Blockly.FieldDropdown([
  12035. ["1", "1"],
  12036. ["2", "2"],
  12037. ["3", "3"],
  12038. ["4", "4"],
  12039. ["5", "5"]
  12040. ]), "queue_value")
  12041. .appendField(Blockly.Msg.ai_systen_queue_value);
  12042. this.setOutput(true, null);
  12043. this.setColour("#22398e");
  12044. this.setTooltip("");
  12045. }
  12046. }
  12047. Blockly.Python['ai_system_get_queue'] = function (block) {
  12048. var queue_value = block.getFieldValue('queue_value');
  12049. var code = `queue${queue_value}.get()`;
  12050. return [code, Blockly.Python.ORDER_NONE];
  12051. }
  12052. // 拼音识别
  12053. Blockly.Blocks['speech_recognition_phonetic_to_pinyin_setup'] = {
  12054. init: function () {
  12055. this.appendDummyInput()
  12056. .appendField(new Blockly.FieldImage("blockly/media/speech_recognition_header_recognition_new.png", 130, 60, { alt: "*", flipRtl: "FALSE" }));
  12057. this.appendDummyInput()
  12058. .appendField(Blockly.Msg.ai_speech_recognition_phonetic_to_pinyin_setup);
  12059. this.setPreviousStatement(true, null);
  12060. this.setNextStatement(true, null);
  12061. this.setColour("#ee783a");
  12062. this.setTooltip(Blockly.Msg.speech_recognition_phonetic_to_pinyin_setup_TOOLTIP);
  12063. this.setHelpUrl("");
  12064. }
  12065. };
  12066. Blockly.Python['speech_recognition_phonetic_to_pinyin_setup'] = function (block) {
  12067. Blockly.Python.definitions_['v831_import_display'] = `from maix import display`;
  12068. Blockly.Python.definitions_['v831_import_image'] = `from maix import image`;
  12069. Blockly.Python.definitions_['v831_import_camera'] = `from maix import camera`;
  12070. Blockly.Python.definitions_["v831_import_maix_speech"] = `import _maix_speech`
  12071. Blockly.Python.definitions_.speech_recognition_phonetic_to_pinyin_setup_def = `asr = _maix_speech.Asr()
  12072. asr.open("hw:0,0", "/root/preset/drivers/ASR/CocoPi_Asr_Model/CocoPi_Asr_Model_V2.1")
  12073. asr.clear()
  12074. asr_kws_result=[0,0] #获取识别结果及识别概率
  12075. similars = [
  12076. ["pai3", "ai4"],
  12077. ["le4", "luo4"],
  12078. ]
  12079. def asr_kws(keys):
  12080. global asr_kws_result
  12081. tmp = max(keys)
  12082. asr_kws_result[0] = keys.index(tmp)
  12083. asr_kws_result[1] = round(tmp,2)
  12084. `
  12085. var code = "";
  12086. return code;
  12087. };
  12088. Blockly.Blocks.set_pinyin_dict_create_with_items_insert = {
  12089. init: function () {
  12090. this.setColour("#ee783a");
  12091. this.appendDummyInput("").appendField(new Blockly.FieldLabel(Blockly.Msg.ai_speech_recognition_setup_pinyin), "TIP");
  12092. this.itemCount_ = 3;
  12093. this.updateShape_();
  12094. this.setOutput(!0);
  12095. this.setMutator(new Blockly.Mutator(["set_pinyin_dict_create_with_item"]));
  12096. this.setTooltip(Blockly.Msg.ai_speech_recognition_setup_pinyin_and_tone)
  12097. },
  12098. mutationToDom: function () {
  12099. var a = document.createElement("mutation");
  12100. a.setAttribute("items", this.itemCount_);
  12101. return a
  12102. },
  12103. domToMutation: function (a) {
  12104. this.itemCount_ = parseInt(a.getAttribute("items"), 10);
  12105. this.updateShape_()
  12106. },
  12107. decompose: function (a) {
  12108. var b = a.newBlock("set_pinyin_dict_create_with_container");
  12109. b.initSvg();
  12110. for (var c = b.getInput("STACK").connection, e = 0; e < this.itemCount_; e++) {
  12111. var d = a.newBlock("set_pinyin_dict_create_with_item");
  12112. d.initSvg();
  12113. c.connect(d.previousConnection);
  12114. c = d.nextConnection
  12115. }
  12116. return b
  12117. },
  12118. compose: function (a) {
  12119. a = a.getInputTargetBlock("STACK");
  12120. for (var b = [], c = 0; a;)
  12121. b[c] = a.valueConnection_,
  12122. a = a.nextConnection && a.nextConnection.targetBlock(),
  12123. c++;
  12124. this.itemCount_ = c;
  12125. this.updateShape_();
  12126. for (c = 0; c < this.itemCount_; c++)
  12127. b[c] && this.getInput("ADD" + c).connection.connect(b[c])
  12128. },
  12129. saveConnections: function (a) {
  12130. a = a.getInputTargetBlock("STACK");
  12131. for (var b = 0; a;) {
  12132. var c = this.getInput("ADD" + b);
  12133. a.valueConnection_ = c && c.connection;
  12134. b++;
  12135. a = a.nextConnection && a.nextConnection.targetBlock()
  12136. }
  12137. },
  12138. updateShape_: function () {
  12139. this.getInput("EMPTY") && this.removeInput("EMPTY");
  12140. for (var a = [], b = 0; this.getInput("ADD" + b); b++)
  12141. a.push(this.getFieldValue("KEY" + b)),
  12142. this.removeInput("ADD" + b);
  12143. if (0 == this.itemCount_)
  12144. this.getField("TIP").setText(Blockly.Msg.ai_speech_recognition_setup_pinyin);
  12145. else
  12146. for (this.getField("TIP").setText(Blockly.Msg.ai_speech_recognition_setup_pinyin),
  12147. b = 0; b < this.itemCount_; b++)
  12148. 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(":")
  12149. .appendField(Blockly.Msg.ai_speech_recognition_setup_tone).appendField(new Blockly.FieldDropdown([
  12150. ["1", "1"],
  12151. ["2", "2"],
  12152. ["3", "3"],
  12153. ["4", "4"],
  12154. ]), "toneADD" + b + b)
  12155. },
  12156. getVars: function () {
  12157. return [this.getFieldValue("VAR")]
  12158. },
  12159. renameVar: function (a, b) {
  12160. Blockly.Names.equals(a, this.getFieldValue("VAR")) && this.setTitleValue(b, "VAR")
  12161. }
  12162. };
  12163. Blockly.Blocks.set_pinyin_dict_create_with_item = {
  12164. init: function () {
  12165. this.setColour("#ee783a");
  12166. this.appendDummyInput().appendField(Blockly.Msg.ai_speech_recognition_setup_pinyin);
  12167. this.setPreviousStatement(!0);
  12168. this.setNextStatement(!0);
  12169. this.setTooltip(Blockly.Msg.ai_speech_recognition_setup_pinyin);
  12170. this.contextMenu = !1
  12171. }
  12172. };
  12173. Blockly.Blocks.set_pinyin_dict_create_with_container = {
  12174. init: function () {
  12175. this.setColour("#ee783a");
  12176. this.appendDummyInput().appendField(Blockly.Msg.robot_dog_servo_execution);
  12177. this.appendStatementInput("STACK");
  12178. this.setTooltip(Blockly.Msg.robot_dog_servo_execution);
  12179. this.contextMenu = !1
  12180. }
  12181. };
  12182. Blockly.Python.set_pinyin_dict_create_with_items_insert = function (block) {
  12183. for (var a = Array(this.itemCount_), b = 0; b < this.itemCount_; b++) {
  12184. var c = this.getFieldValue("KEY" + b);
  12185. a[b] = c + block.getFieldValue("toneADD" + b + b);
  12186. }
  12187. console.log(a.join(" "))
  12188. a = `"${a.join(" ")}"`;
  12189. return [a, Blockly.Python.ORDER_ATOMIC]
  12190. };
  12191. Blockly.Blocks['speech_recognition_phonetic_to_pinyin_set_param'] = {
  12192. init: function () {
  12193. this.appendDummyInput()
  12194. .appendField(Blockly.Msg.ai_speech_recognition_phonetic_to_pinyin_set_param);
  12195. // this.appendValueInput("data")
  12196. // .setCheck(null)
  12197. // .appendField(Blockly.Msg.ai_speech_recognition_setup_pinyin);
  12198. this.setPreviousStatement(true, null);
  12199. this.setNextStatement(true, null);
  12200. this.setInputsInline(false);
  12201. this.setColour("#ee783a");
  12202. this.pinyinitemCount_ = 1
  12203. this.updateShape_();
  12204. this.setMutator(new Blockly.Mutator(["v831_to_pinyin_set_param"]));
  12205. this.setTooltip(Blockly.Msg.ai_speech_recognition_phonetic_to_pinyin_set_param_TOOLTIP);
  12206. this.setHelpUrl("");
  12207. },
  12208. mutationToDom: function () {
  12209. var a = document.createElement("mutation");
  12210. a.setAttribute("items", this.pinyinitemCount_);
  12211. return a
  12212. },
  12213. domToMutation: function (a) {
  12214. this.pinyinitemCount_ = parseInt(a.getAttribute("items"), 10);
  12215. this.updateShape_()
  12216. },
  12217. decompose: function (a) {
  12218. var b = a.newBlock("v831_to_pinyin_set_param_container");
  12219. b.initSvg();
  12220. for (var c = b.getInput("STACK").connection, e = 0; e < this.pinyinitemCount_; e++) {
  12221. var d = a.newBlock("v831_to_pinyin_set_param");
  12222. d.initSvg();
  12223. c.connect(d.previousConnection);
  12224. c = d.nextConnection
  12225. }
  12226. return b
  12227. },
  12228. compose: function (a) {
  12229. var b = a.getInputTargetBlock("STACK");
  12230. for (a = []; b;)
  12231. a.push(b.valueConnection_),
  12232. b = b.nextConnection && b.nextConnection.targetBlock();
  12233. for (b = 0; b < this.pinyinitemCount_; b++) {
  12234. var c = this.getInput("PINYIN" + b).connection.targetConnection;
  12235. c && -1 == a.indexOf(c) && c.disconnect()
  12236. }
  12237. this.pinyinitemCount_ = a.length;
  12238. this.updateShape_();
  12239. for (b = 0; b < this.pinyinitemCount_; b++)
  12240. Blockly.Mutator.reconnect(a[b], this, "PINYIN" + b)
  12241. },
  12242. saveConnections: function (a) {
  12243. a = a.getInputTargetBlock("STACK");
  12244. for (var b = 0; a;) {
  12245. var c = this.getInput("PINYIN" + b);
  12246. a.valueConnection_ = c && c.connection.targetConnection;
  12247. b++;
  12248. a = a.nextConnection && a.nextConnection.targetBlock()
  12249. }
  12250. },
  12251. updateShape_: function () {
  12252. // this.itemCount_ && this.getInput("EMPTY") ? this.removeInput("EMPTY") : this.itemCount_ || this.getInput("EMPTY") || this.appendDummyInput("EMPTY").appendField(Blockly.Msg.TUPLE_CREATE_EMPTY_TITLE);
  12253. for (var a = 0; a < this.pinyinitemCount_; a++)
  12254. if (!this.getInput("PINYIN" + a)) {
  12255. var b = this.appendValueInput("PINYIN" + a);
  12256. 0 == a && b.appendField(Blockly.Msg.ai_speech_recognition_setup_pinyin_and_tone)
  12257. }
  12258. for (; this.getInput("PINYIN" + a);)
  12259. this.removeInput("PINYIN" + a),
  12260. a++
  12261. }
  12262. };
  12263. Blockly.Blocks['v831_to_pinyin_set_param'] = {
  12264. init: function () {
  12265. this.appendDummyInput()
  12266. .appendField(Blockly.Msg.ai_speech_recognition_setup_pinyin_and_tone);
  12267. this.setPreviousStatement(true);
  12268. this.setNextStatement(true);
  12269. this.setColour("#ee783a")
  12270. this.setTooltip('');
  12271. this.contextMenu = false;
  12272. }
  12273. };
  12274. Blockly.Blocks['v831_to_pinyin_set_param_container'] = {
  12275. init: function () {
  12276. this.setColour("#ee783a");
  12277. this.appendDummyInput()
  12278. .appendField(Blockly.Msg.ARD_SERIAL_SETUP);
  12279. this.appendStatementInput('STACK');
  12280. this.setTooltip('');
  12281. this.contextMenu = false;
  12282. }
  12283. };
  12284. Blockly.Python['speech_recognition_phonetic_to_pinyin_set_param'] = function (block) {
  12285. // var data = Blockly.Python.valueToCode(block, 'data', Blockly.Python.ORDER_ATOMIC);
  12286. Blockly.Python.definitions_['v831_import_display'] = `from maix import display`;
  12287. Blockly.Python.definitions_['v831_import_image'] = `from maix import image`;
  12288. Blockly.Python.definitions_['v831_import_camera'] = `from maix import camera`;
  12289. Blockly.Python.definitions_["v831_import_maix_speech"] = `import _maix_speech`
  12290. var a = Array(this.pinyinitemCount_)
  12291. for (let b = 0; b < this.pinyinitemCount_; b++) {
  12292. var c = Blockly.Python.valueToCode(this, "PINYIN" + b, Blockly.Python.ORDER_NONE)
  12293. a[b] = c.slice(1, -1).replaceAll(",", "")
  12294. }
  12295. a = a.map(r => [r, 0.1])
  12296. Blockly.Python.definitions_.speech_recognition_phonetic_to_pinyin_setup_def = `asr = _maix_speech.Asr()
  12297. asr.open("hw:0,0", "/root/preset/drivers/ASR/CocoPi_Asr_Model/CocoPi_Asr_Model_V2.1")
  12298. asr.clear()
  12299. asr_kws_result=[0,0] #获取识别结果及识别概率
  12300. similars = [
  12301. ["pai3", "ai4"],
  12302. ["le4", "luo4"],
  12303. ]
  12304. def asr_kws(keys):
  12305. global asr_kws_result
  12306. tmp = max(keys)
  12307. asr_kws_result[0] = keys.index(tmp)
  12308. asr_kws_result[1] = round(tmp,2)
  12309. `
  12310. // Blockly.Python.definitions_["set_keywords_function"] = `def SetKeywords(data):
  12311. // dataList = []
  12312. // for obj in data:
  12313. // dataList.append([obj,0.1])
  12314. // print(dataList)
  12315. // return dataList
  12316. // `
  12317. var code = `keywords = ${JSON.stringify(a)}
  12318. asr.set_kws(keywords, similars, asr_kws)
  12319. `;
  12320. return code;
  12321. };
  12322. Blockly.Blocks['speech_recognition_set_pinyin_tone'] = {
  12323. init: function () {
  12324. this.appendDummyInput()
  12325. .appendField("声调").appendField(new Blockly.FieldDropdown([
  12326. ["1", "1"],
  12327. ["2", "2"],
  12328. ["3", "3"],
  12329. ["4", "4"],
  12330. ]), "tone");
  12331. this.setOutput(true, null);
  12332. this.setColour("#ee783a");
  12333. this.setTooltip(Blockly.Msg.ai_speech_recognition_phonetic_to_pinyin_get_result_TOOLTIP);
  12334. this.setHelpUrl("");
  12335. }
  12336. };
  12337. Blockly.Python['speech_recognition_set_pinyin_tone'] = function (block) {
  12338. let tone = block.getFieldValue('tone');
  12339. console.log(tone)
  12340. Blockly.Python.definitions_.speech_recognition_phonetic_to_pinyin_start_recognition_def = `def getAsrResult():
  12341. global asr_kws_result,keywords
  12342. if(asr_kws_result[1]!=0):
  12343. asr_kws_result_list = keywords[asr_kws_result[0]][0]
  12344. return asr_kws_result_list
  12345. `
  12346. var code = tone;
  12347. return [code, Blockly.Python.ORDER_NONE];
  12348. };
  12349. Blockly.Blocks['speech_recognition_phonetic_to_pinyin_start_recognition'] = {
  12350. init: function () {
  12351. this.appendDummyInput()
  12352. .appendField(Blockly.Msg.speech_recognition_phonetic_to_pinyin_start_recognition);
  12353. this.setPreviousStatement(true, null);
  12354. this.setNextStatement(true, null);
  12355. this.setColour("#ee783a");
  12356. this.setTooltip(Blockly.Msg.speech_recognition_phonetic_to_pinyin_start_recognition_TOOLTIP);
  12357. this.setHelpUrl("");
  12358. }
  12359. };
  12360. Blockly.Python['speech_recognition_phonetic_to_pinyin_start_recognition'] = function (block) {
  12361. var code = "asr.run(1)\n";
  12362. return code;
  12363. };
  12364. Blockly.Blocks['speech_recognition_phonetic_to_pinyin_stop_recognition'] = {
  12365. init: function () {
  12366. this.appendDummyInput()
  12367. .appendField(Blockly.Msg.speech_recognition_phonetic_to_pinyin_stop_recognition);
  12368. this.setPreviousStatement(true, null);
  12369. this.setNextStatement(true, null);
  12370. this.setColour("#ee783a");
  12371. this.setTooltip(Blockly.Msg.speech_recognition_phonetic_to_pinyin_stop_recognition_TOOLTIP);
  12372. this.setHelpUrl("");
  12373. }
  12374. };
  12375. Blockly.Python['speech_recognition_phonetic_to_pinyin_stop_recognition'] = function (block) {
  12376. var code = "asr.exit()\n";
  12377. return code;
  12378. };
  12379. Blockly.Blocks['speech_recognition_phonetic_to_pinyin_pause_recognition'] = {
  12380. init: function () {
  12381. this.appendDummyInput()
  12382. .appendField(Blockly.Msg.speech_recognition_phonetic_to_pinyin_pause_recognition);
  12383. this.setPreviousStatement(true, null);
  12384. this.setNextStatement(true, null);
  12385. this.setColour("#ee783a");
  12386. this.setTooltip(Blockly.Msg.speech_recognition_phonetic_to_pinyin_pause_recognition_TOOLTIP);
  12387. this.setHelpUrl("");
  12388. }
  12389. };
  12390. Blockly.Python['speech_recognition_phonetic_to_pinyin_pause_recognition'] = function (block) {
  12391. var code = "asr.clear()\n";
  12392. return code;
  12393. };
  12394. Blockly.Blocks['speech_recognition_phonetic_to_pinyin_get_result'] = {
  12395. init: function () {
  12396. this.appendDummyInput()
  12397. .appendField(Blockly.Msg.ai_speech_recognition_phonetic_to_pinyin_get_result);
  12398. this.setOutput(true, null);
  12399. this.setColour("#ee783a");
  12400. this.setTooltip(Blockly.Msg.ai_speech_recognition_phonetic_to_pinyin_get_result_TOOLTIP);
  12401. this.setHelpUrl("");
  12402. }
  12403. };
  12404. Blockly.Python['speech_recognition_phonetic_to_pinyin_get_result'] = function (block) {
  12405. Blockly.Python.definitions_.speech_recognition_phonetic_to_pinyin_start_recognition_def = `def getAsrResult():
  12406. global asr_kws_result,keywords
  12407. if(asr_kws_result[1]!=0):
  12408. asr_kws_result_list = keywords[asr_kws_result[0]][0]
  12409. return asr_kws_result_list
  12410. `
  12411. var code = 'getAsrResult()';
  12412. return [code, Blockly.Python.ORDER_NONE];
  12413. };
  12414. // numpy + mnist
  12415. Blockly.Blocks['Numpy_Neural_Network_init'] = {
  12416. init: function () {
  12417. this.appendDummyInput()
  12418. .appendField(Blockly.Msg.numpy_mnist_model_init);
  12419. this.setPreviousStatement(true, null);
  12420. this.setNextStatement(true, null);
  12421. this.setColour("#ee783a");
  12422. this.setTooltip(Blockly.Msg.numpy_mnist_model_init);
  12423. this.setHelpUrl("");
  12424. }
  12425. };
  12426. Blockly.Python['Numpy_Neural_Network_init'] = function (block) {
  12427. Blockly.Python.definitions_['v831_import_numpy_as_np'] = `import numpy as np`;
  12428. Blockly.Python.definitions_["v831_import_pickle"] = `import pickle`
  12429. Blockly.Python.definitions_["v831_NeuralNetwork_init"] = `def sigmoid(x):
  12430. return 1/(1+np.exp(-x))
  12431. def grad(x):
  12432. return x*(1-x)
  12433. class NeuralNetwork:
  12434. """
  12435. 三层全连接前馈神经网络
  12436. """
  12437. def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate, active_function=sigmoid, gradient=grad, lambda_=0.1):
  12438. """
  12439. :param inputnodes: 输入层结点数
  12440. :param hiddennodes: 隐藏层节点数
  12441. :param outputnodes: 输出层节点数
  12442. :param learningrate: 学习率
  12443. :param active_function: 激活函数
  12444. :param gradient: 激活函数的导数
  12445. :param lambda_: L2正则化系数
  12446. """
  12447. self.inputnodes = inputnodes
  12448. self.hiddennodes = hiddennodes
  12449. self.outputnodes = outputnodes
  12450. self.learningrate = learningrate
  12451. self.active_function = active_function
  12452. self.gradient = gradient
  12453. self.lambda_ = lambda_
  12454. # 权值矩阵
  12455. self.weights_i_h = np.random.rand(
  12456. self.hiddennodes, self.inputnodes) - 0.5
  12457. self.weights_h_o = np.random.rand(
  12458. self.outputnodes, self.hiddennodes) - 0.5
  12459. def train_sgd(self, x, y):
  12460. """梯度下降训练"""
  12461. TRAINX = np.array(x).reshape(-1, 1)
  12462. target = np.zeros((self.outputnodes, 1)) + 0.01
  12463. target[y, 0] = 0.99
  12464. hiddeninputs = np.dot(self.weights_i_h, TRAINX)
  12465. hiddenoutputs = self.active_function(hiddeninputs)
  12466. outputinputs = np.dot(self.weights_h_o, hiddenoutputs)
  12467. final_outputs = self.active_function(outputinputs)
  12468. error = target - final_outputs
  12469. hidden_error = np.dot(self.weights_h_o.transpose(), error)
  12470. self.weights_h_o += self.learningrate * error * \
  12471. np.dot(self.gradient(final_outputs), hiddenoutputs.transpose())
  12472. self.weights_i_h += self.learningrate * hidden_error * \
  12473. np.dot(self.gradient(hiddenoutputs), TRAINX.transpose())
  12474. def fit(self, TRAINX, targets):
  12475. TRAINX = np.array(TRAINX)
  12476. for i in range(TRAINX.shape[0]):
  12477. self.train_sgd(TRAINX[i], targets[i])
  12478. def query(self, inputs, debug=False):
  12479. """单个值预测"""
  12480. inputs = np.array(inputs).reshape(-1, 1)
  12481. hidden_input = np.dot(self.weights_i_h, inputs)
  12482. hidden_output = self.active_function(hidden_input)
  12483. output_input = np.dot(self.weights_h_o, hidden_output)
  12484. final_output = self.active_function(output_input)
  12485. if debug:
  12486. print("predict: ", final_output)
  12487. return np.argmax(final_output)
  12488. def predict(self, inputs):
  12489. """批量预测"""
  12490. res = []
  12491. for x in inputs:
  12492. res.append(self.query(x))
  12493. return res
  12494. def __str__(self):
  12495. return "NeuralNetwork: \\ninput_nodes = {0}, hidden_nodes = {1}, \\noutputnodes = {2}, learningrate = {3}".format(self.inputnodes, self.hiddennodes, self.outputnodes, self.learningrate)
  12496. # 用测试数据测试
  12497. def accuracy(y_true, y_pred):
  12498. """准确度"""
  12499. y_true = np.array(y_true)
  12500. y_pred = np.array(y_pred)
  12501. return sum(y_true == y_pred)/y_true.shape[0]
  12502. # 用全部数据进行训练
  12503. def get_data():
  12504. # train_df = np.loadtxt("mnist_train.csv", delimiter=",", dtype=str)
  12505. # test_df = np.loadtxt("mnist_test.csv", delimiter=",", dtype=str)
  12506. global train_df, test_df
  12507. print(train_df.shape)
  12508. print(test_df.shape)
  12509. train_data = train_df.astype("int")
  12510. TRAINX = train_data[:, 1:]
  12511. TRAINY = train_data[:, 0]
  12512. TRAINX = TRAINX / 255 * 0.99 + 0.01
  12513. test_data = test_df.astype("int")
  12514. TESTX = test_data[:, 1:]
  12515. TESTY = test_data[:, 0]
  12516. TESTX = TESTX / 255 * 0.99 + 0.01
  12517. return TRAINX, TRAINY, TESTX, TESTY
  12518. `
  12519. var code = '';
  12520. return code;
  12521. };
  12522. Blockly.Blocks['Numpy_Neural_Network_load'] = {
  12523. init: function () {
  12524. this.appendDummyInput()
  12525. .appendField("numpy + mnist 训练模型加载");
  12526. this.setPreviousStatement(true, null);
  12527. this.setNextStatement(true, null);
  12528. this.setColour("#ee783a");
  12529. this.setTooltip("numpy + mnist 训练模型加载");
  12530. this.setHelpUrl("");
  12531. }
  12532. };
  12533. Blockly.Python['Numpy_Neural_Network_load'] = function (block) {
  12534. Blockly.Python.definitions_['v831_import_numpy_as_np'] = `import numpy as np`;
  12535. Blockly.Python.definitions_["v831_import_pickle"] = `import pickle`
  12536. Blockly.Python.definitions_["v831_NeuralNetwork_init"] = `def sigmoid(x):
  12537. return 1/(1+np.exp(-x))
  12538. def grad(x):
  12539. return x*(1-x)
  12540. class NeuralNetwork:
  12541. """
  12542. 三层全连接前馈神经网络
  12543. """
  12544. def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate, active_function=sigmoid, gradient=grad, lambda_=0.1):
  12545. """
  12546. :param inputnodes: 输入层结点数
  12547. :param hiddennodes: 隐藏层节点数
  12548. :param outputnodes: 输出层节点数
  12549. :param learningrate: 学习率
  12550. :param active_function: 激活函数
  12551. :param gradient: 激活函数的导数
  12552. :param lambda_: L2正则化系数
  12553. """
  12554. self.inputnodes = inputnodes
  12555. self.hiddennodes = hiddennodes
  12556. self.outputnodes = outputnodes
  12557. self.learningrate = learningrate
  12558. self.active_function = active_function
  12559. self.gradient = gradient
  12560. self.lambda_ = lambda_
  12561. # 权值矩阵
  12562. self.weights_i_h = np.random.rand(
  12563. self.hiddennodes, self.inputnodes) - 0.5
  12564. self.weights_h_o = np.random.rand(
  12565. self.outputnodes, self.hiddennodes) - 0.5
  12566. def train_sgd(self, x, y):
  12567. """梯度下降训练"""
  12568. TRAINX = np.array(x).reshape(-1, 1)
  12569. target = np.zeros((self.outputnodes, 1)) + 0.01
  12570. target[y, 0] = 0.99
  12571. hiddeninputs = np.dot(self.weights_i_h, TRAINX)
  12572. hiddenoutputs = self.active_function(hiddeninputs)
  12573. outputinputs = np.dot(self.weights_h_o, hiddenoutputs)
  12574. final_outputs = self.active_function(outputinputs)
  12575. error = target - final_outputs
  12576. hidden_error = np.dot(self.weights_h_o.transpose(), error)
  12577. self.weights_h_o += self.learningrate * error * \
  12578. np.dot(self.gradient(final_outputs), hiddenoutputs.transpose())
  12579. self.weights_i_h += self.learningrate * hidden_error * \
  12580. np.dot(self.gradient(hiddenoutputs), TRAINX.transpose())
  12581. def fit(self, TRAINX, targets):
  12582. TRAINX = np.array(TRAINX)
  12583. for i in range(TRAINX.shape[0]):
  12584. self.train_sgd(TRAINX[i], targets[i])
  12585. def query(self, inputs, debug=False):
  12586. """单个值预测"""
  12587. inputs = np.array(inputs).reshape(-1, 1)
  12588. hidden_input = np.dot(self.weights_i_h, inputs)
  12589. hidden_output = self.active_function(hidden_input)
  12590. output_input = np.dot(self.weights_h_o, hidden_output)
  12591. final_output = self.active_function(output_input)
  12592. if debug:
  12593. print("predict: ", final_output)
  12594. return np.argmax(final_output)
  12595. def predict(self, inputs):
  12596. """批量预测"""
  12597. res = []
  12598. for x in inputs:
  12599. res.append(self.query(x))
  12600. return res
  12601. def __str__(self):
  12602. return "NeuralNetwork: \\ninput_nodes = {0}, hidden_nodes = {1}, \\noutputnodes = {2}, learningrate = {3}".format(self.inputnodes, self.hiddennodes, self.outputnodes, self.learningrate)
  12603. # 用测试数据测试
  12604. def accuracy(y_true, y_pred):
  12605. """准确度"""
  12606. y_true = np.array(y_true)
  12607. y_pred = np.array(y_pred)
  12608. return sum(y_true == y_pred)/y_true.shape[0]
  12609. # 用全部数据进行训练
  12610. def get_data():
  12611. # train_df = np.loadtxt("mnist_train.csv", delimiter=",", dtype=str)
  12612. # test_df = np.loadtxt("mnist_test.csv", delimiter=",", dtype=str)
  12613. global train_df, test_df
  12614. print(train_df.shape)
  12615. print(test_df.shape)
  12616. train_data = train_df.astype("int")
  12617. TRAINX = train_data[:, 1:]
  12618. TRAINY = train_data[:, 0]
  12619. TRAINX = TRAINX / 255 * 0.99 + 0.01
  12620. test_data = test_df.astype("int")
  12621. TESTX = test_data[:, 1:]
  12622. TESTY = test_data[:, 0]
  12623. TESTX = TESTX / 255 * 0.99 + 0.01
  12624. return TRAINX, TRAINY, TESTX, TESTY
  12625. `
  12626. var code = `#构建神经网络训练服务
  12627. TRAINX, TRAINY, TESTX, TESTY = get_data()
  12628. NN = NeuralNetwork(784, 100, 10, 0.3)
  12629. NN.fit(TRAINX, TRAINY)
  12630. y_pred = NN.predict(TESTX)
  12631. `;
  12632. return code;
  12633. };
  12634. Blockly.Blocks['Numpy_Neural_Network_LOading_test_train'] = {
  12635. init: function () {
  12636. this.appendDummyInput()
  12637. .appendField(Blockly.Msg.numpy_mnist_loading_train_and_test_set);
  12638. this.appendValueInput("class_input")
  12639. .setCheck(null)
  12640. .appendField(Blockly.Msg.numpy_mnist_train_set_url);
  12641. this.appendValueInput("class_input1")
  12642. .setCheck(null)
  12643. .appendField(Blockly.Msg.numpy_mnist_test_set_url);
  12644. this.setPreviousStatement(true, null);
  12645. this.setNextStatement(true, null);
  12646. this.setColour("#ee783a");
  12647. this.setTooltip(Blockly.Msg.numpy_mnist_loading_train_and_test_set);
  12648. this.setHelpUrl("");
  12649. }
  12650. };
  12651. Blockly.Python['Numpy_Neural_Network_LOading_test_train'] = function (block) {
  12652. var trainUrl = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  12653. var testUrl = Blockly.Python.valueToCode(block, 'class_input1', Blockly.Python.ORDER_ATOMIC);
  12654. // TODO: Assemble Python into code variable.
  12655. var code = `test_df = np.loadtxt(${testUrl}, delimiter=",", dtype=str)
  12656. train_df = np.loadtxt(${trainUrl}, delimiter=",", dtype=str)
  12657. `;
  12658. // TODO: Change ORDER_NONE to the correct strength.
  12659. return code;
  12660. };
  12661. Blockly.Blocks['Numpy_Neural_Network_numpy_isStart'] = {
  12662. init: function () {
  12663. this.appendDummyInput()
  12664. .appendField(Blockly.Msg.numpy_mnist_is_training_start);
  12665. this.setOutput(true, null);
  12666. this.setColour("#ee783a");
  12667. this.setTooltip(Blockly.Msg.numpy_mnist_is_training_start);
  12668. this.setHelpUrl("");
  12669. }
  12670. };
  12671. Blockly.Python['Numpy_Neural_Network_numpy_isStart'] = function (block) {
  12672. // TODO: Assemble Python into code variable.
  12673. var code = `not os.path.exists("/usr/lib/python3.8/site-packages/numpy/random/__init__.py")`;
  12674. // TODO: Change ORDER_NONE to the correct strength.
  12675. return [code, Blockly.Python.ORDER_NONE];
  12676. };
  12677. Blockly.Blocks['Numpy_Neural_Network_setting_training_argument'] = {
  12678. init: function () {
  12679. this.appendDummyInput()
  12680. .appendField(Blockly.Msg.numpy_mnist_setting_training_argument);
  12681. this.appendValueInput("class_input")
  12682. .setCheck(null)
  12683. .appendField(Blockly.Msg.numpy_mnist_input_trait_dimensionality);
  12684. this.appendValueInput("class_input1")
  12685. .setCheck(null)
  12686. .appendField(Blockly.Msg.numpy_mnist_setting_training_argument_number_node);
  12687. this.appendValueInput("class_input2")
  12688. .setCheck(null)
  12689. .appendField(Blockly.Msg.numpy_mnist_output_dimensionality);
  12690. this.appendValueInput("class_input3")
  12691. .setCheck(null)
  12692. .appendField(Blockly.Msg.numpy_mnist_setting_training_argument_learning_rate);
  12693. this.appendValueInput("class_input4")
  12694. .setCheck(null)
  12695. .appendField(Blockly.Msg.numpy_mnist_setting_training_argument_frequency);
  12696. this.appendValueInput("class_input5")
  12697. .setCheck(null)
  12698. .appendField(Blockly.Msg.VIDEO_S_S);
  12699. this.setPreviousStatement(true, null);
  12700. this.setNextStatement(true, null);
  12701. this.setColour("#ee783a");
  12702. this.setTooltip(Blockly.Msg.numpy_mnist_setting_training_argument);
  12703. this.setHelpUrl("");
  12704. }
  12705. };
  12706. Blockly.Python['Numpy_Neural_Network_setting_training_argument'] = function (block) {
  12707. var class_input = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  12708. var class_input1 = Blockly.Python.valueToCode(block, 'class_input1', Blockly.Python.ORDER_ATOMIC);
  12709. var class_input2 = Blockly.Python.valueToCode(block, 'class_input2', Blockly.Python.ORDER_ATOMIC);
  12710. var class_input3 = Blockly.Python.valueToCode(block, 'class_input3', Blockly.Python.ORDER_ATOMIC);
  12711. var class_input4 = Blockly.Python.valueToCode(block, 'class_input4', Blockly.Python.ORDER_ATOMIC);
  12712. var class_input5 = Blockly.Python.valueToCode(block, 'class_input5', Blockly.Python.ORDER_ATOMIC);
  12713. // TODO: Assemble Python into code variable.
  12714. console.log(Number(class_input.slice(1, -1)), class_input1, class_input2, class_input3)
  12715. var code = `TRAINX, TRAINY, TESTX, TESTY = get_data()
  12716. 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))})
  12717. #开启神经网络训练,记录时长
  12718. AccuracyRATE = 0
  12719. # 训练10次,每3次训练下降一次学习率
  12720. for e in range(1, ${Number(class_input4.slice(1, -1)) + 1}):
  12721. if e % 3 == 0:
  12722. NN.learningrate /= 2
  12723. NN.fit(TRAINX, TRAINY)
  12724. y_pred = NN.predict(TESTX)
  12725. CURRENTACCURACY = accuracy(TESTY, y_pred)
  12726. if CURRENTACCURACY > AccuracyRATE:
  12727. AccuracyRATE = CURRENTACCURACY
  12728. #文件已保存至
  12729. with open(${class_input5}, "wb") as f: # 保存模型
  12730. pickle.dump(pickle.dumps(NN), f)
  12731. os.system("touch /usr/lib/python3.8/site-packages/numpy/random/__init__.py")
  12732. `;
  12733. // TODO: Change ORDER_NONE to the correct strength.
  12734. return code;
  12735. };
  12736. //
  12737. Blockly.Blocks['Numpy_Neural_Network_numpy_loading_model'] = {
  12738. init: function () {
  12739. this.appendDummyInput()
  12740. .appendField(Blockly.Msg.numpy_mnist_loading_model);
  12741. this.appendValueInput("class_input")
  12742. .setCheck(null)
  12743. .appendField(Blockly.Msg.AUDIO_S_PF);
  12744. this.setPreviousStatement(true, null);
  12745. this.setNextStatement(true, null);
  12746. this.setColour("#ee783a");
  12747. this.setTooltip(Blockly.Msg.numpy_mnist_loading_model);
  12748. this.setHelpUrl("");
  12749. }
  12750. };
  12751. Blockly.Python['Numpy_Neural_Network_numpy_loading_model'] = function (block) {
  12752. var trainUrl = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  12753. // TODO: Assemble Python into code variable.
  12754. var code = `try:
  12755. with open(${trainUrl},"rb") as f:
  12756. b_data = pickle.load(f)
  12757. net_model = pickle.loads(b_data)
  12758. except:
  12759. pass
  12760. `;
  12761. // TODO: Change ORDER_NONE to the correct strength.
  12762. return code;
  12763. };
  12764. Blockly.Python['Numpy_Neural_Network_LOading_test_train'] = function (block) {
  12765. var trainUrl = Blockly.Python.valueToCode(block, 'class_input', Blockly.Python.ORDER_ATOMIC);
  12766. var testUrl = Blockly.Python.valueToCode(block, 'class_input1', Blockly.Python.ORDER_ATOMIC);
  12767. // TODO: Assemble Python into code variable.
  12768. var code = `test_df = np.loadtxt(${testUrl}, delimiter=",", dtype=str)
  12769. train_df = np.loadtxt(${trainUrl}, delimiter=",", dtype=str)
  12770. `;
  12771. // TODO: Change ORDER_NONE to the correct strength.
  12772. return code;
  12773. };
  12774. Blockly.Blocks['Numpy_Neural_Network_numpy_loading_model_fun'] = {
  12775. init: function () {
  12776. // this.appendDummyInput()
  12777. // .appendField("获取检测结果");
  12778. this.appendDummyInput()
  12779. .appendField("在屏幕中显示numpy的检测结果")
  12780. this.setPreviousStatement(true, null);
  12781. this.setNextStatement(true, null);
  12782. this.setColour("#ee783a");
  12783. this.setTooltip(Blockly.Msg.ai_model_getclass_TOOLTIP);
  12784. this.setHelpUrl("");
  12785. }
  12786. };
  12787. Blockly.Python['Numpy_Neural_Network_numpy_loading_model_fun'] = function (block) {
  12788. Blockly.Python.definitions_["v831_import_PIL_image"] = `from PIL import Image`
  12789. Blockly.Python.definitions_["v831_function_numpy_result"] = `def numpyResult(img):
  12790. img_path="/root/user/image_1.png"
  12791. img_gray_path="/root/user/image_gray.png"
  12792. mk = img.crop(0, 0, 240, 240) #裁剪图像为320*240
  12793. mk1=img.crop(0, 0, 240, 240).resize(224,224) #继续裁剪图像为224*224
  12794. numpyResultcanvas= image.new(size = (320, 240), color = (0,255,0), mode = 'RGB') #创建背景图
  12795. numpyResultcanvas.draw_image(mk1.rotate(90,adjust=1),58,8) #贴224*224的图片
  12796. mk2=mk1.resize(28,28) #裁剪图片为28*28
  12797. numpyResultcanvas.draw_image(mk2,8,8) #贴28*28的图
  12798. mk2.save(img_path)
  12799. origin=Image.open(img_path).convert('L') #转为8位分辨率灰度图(0~255)
  12800. origin.save(img_gray_path)
  12801. origin_gray=image.open(img_gray_path)
  12802. numpyResultcanvas.draw_image(origin_gray,8,48)
  12803. numpyResultImg = np.array(origin).reshape(1, 784).astype('int')
  12804. numpyResultImg = numpyResultImg / 255 * 0.99 + 0.01
  12805. print(numpyResultImg.shape)
  12806. result = net_model.query(numpyResultImg)
  12807. return result
  12808. `
  12809. // TODO: Assemble Python into code variable.
  12810. var code = `canvas = canvas.crop(48,8,224,224)
  12811. GETNUMPYRESULT = numpyResult(canvas)
  12812. if GETNUMPYRESULT != "":
  12813. canvas.draw_string(0,0, (str(GETNUMPYRESULT)), scale = 3, color = (204,204,204) , thickness = 1)
  12814. else:
  12815. canvas.draw_string(0,0, ("None"), scale = 3, color = (204,204,204) , thickness = 1)
  12816. `;
  12817. // TODO: Change ORDER_NONE to the correct strength.
  12818. return code;
  12819. };
  12820. Blockly.Blocks['Numpy_Neural_Network_numpy_loading_model_result'] = {
  12821. init: function () {
  12822. this.appendDummyInput()
  12823. .appendField(Blockly.Msg.numpy_mnist_get_result);
  12824. this.setOutput(true, null);
  12825. this.setColour("#ee783a");
  12826. this.setTooltip(Blockly.Msg.numpy_mnist_get_result);
  12827. this.setHelpUrl("");
  12828. }
  12829. };
  12830. Blockly.Python['Numpy_Neural_Network_numpy_loading_model_result'] = function (block) {
  12831. var code = `GETNUMPYRESULT`;
  12832. // TODO: Change ORDER_NONE to the correct strength.
  12833. return [code, Blockly.Python.ORDER_NONE];
  12834. };
  12835. //
  12836. Blockly.Blocks['Numpy_Neural_Network_numpy_clear'] = {
  12837. init: function () {
  12838. this.appendDummyInput()
  12839. .appendField(Blockly.Msg.numpy_mnist_clear);
  12840. this.setPreviousStatement(true, null);
  12841. this.setNextStatement(true, null);
  12842. this.setColour("#ee783a");
  12843. this.setTooltip(Blockly.Msg.numpy_mnist_clear);
  12844. this.setHelpUrl("");
  12845. }
  12846. };
  12847. Blockly.Python['Numpy_Neural_Network_numpy_clear'] = function (block) {
  12848. var code = `os.system('rm -rf /usr/lib/python3.8/site-packages/numpy/random/__init__.py')
  12849. `;
  12850. // TODO: Change ORDER_NONE to the correct strength.
  12851. return code;
  12852. };
  12853. Blockly.Blocks["cocopi_chatGpt_init"] = {
  12854. init: function () {
  12855. this.appendDummyInput()
  12856. .appendField(Blockly.Msg.ChatGpt_init); //
  12857. this.setPreviousStatement(true, null);
  12858. this.setNextStatement(true, null);
  12859. this.setColour("#7e3cca");
  12860. this.setTooltip(Blockly.Msg.ChatGpt_init);
  12861. this.setHelpUrl("");
  12862. }
  12863. };
  12864. Blockly.Python['cocopi_chatGpt_init'] = function (block) {
  12865. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  12866. sys.path.append("/root/")`
  12867. Blockly.Python.definitions_['v831_import_CocoPi_Gpt'] = `try:
  12868. from CocoPi import GPTrequest
  12869. except:
  12870. import sys
  12871. sys.path.append("/root/preset/drivers/pylib/")
  12872. from GPTrequest import GPTrequest`
  12873. Blockly.Python.definitions_['v831_import_CocoPi_Gpt_init'] = `gptInit = GPTrequest()`
  12874. var code = ``;
  12875. // TODO: Change ORDER_NONE to the correct strength.
  12876. return code;
  12877. };
  12878. Blockly.Blocks["cocopi_chatGpt_text_to_img_loading"] = {
  12879. init: function () {
  12880. this.appendValueInput("NAME")
  12881. .setCheck(null)
  12882. .appendField(Blockly.Msg.ChatGpt_set_img_des);
  12883. this.appendDummyInput()
  12884. .appendField(Blockly.Msg.ChatGpt_get_img_path_result);
  12885. this.setOutput(true, null);
  12886. this.setColour("#7e3cca");
  12887. this.setTooltip(Blockly.Msg.ChatGpt_set_img_des);
  12888. this.setInputsInline(false)
  12889. this.setHelpUrl("");
  12890. }
  12891. };
  12892. Blockly.Python['cocopi_chatGpt_text_to_img_loading'] = function (block) {
  12893. var text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC);
  12894. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  12895. sys.path.append("/root/")`
  12896. Blockly.Python.definitions_['v831_import_CocoPi_Gpt'] = `try:
  12897. from CocoPi import GPTrequest
  12898. except:
  12899. import sys
  12900. sys.path.append("/root/preset/drivers/pylib/")
  12901. from GPTrequest import GPTrequest`
  12902. Blockly.Python.definitions_['v831_import_CocoPi_Gpt_init'] = `gptInit = GPTrequest()`
  12903. // Blockly.Python.addVariable("TextContent", `TextContent = ""`, true)
  12904. var code = `gptInit.get_text_to_img(${text})`;
  12905. // TODO: Change ORDER_NONE to the correct strength.
  12906. return [code, Blockly.Python.ORDER_ATOMIC];
  12907. };
  12908. Blockly.Blocks["cocopi_chatGpt_image_to_text_loading"] = {
  12909. init: function () {
  12910. this.appendValueInput("NAME")
  12911. .setCheck(null)
  12912. .appendField(Blockly.Msg.ChatGpt_set_text_path);
  12913. this.appendValueInput("NAME1")
  12914. .setCheck(null)
  12915. .appendField(Blockly.Msg.ChatGpt_set_text_text_content);
  12916. this.appendDummyInput()
  12917. .appendField(Blockly.Msg.ChatGpt_get_text_content_result);
  12918. this.setOutput(true, null);
  12919. this.setInputsInline(false);
  12920. this.setColour("#7e3cca");
  12921. this.setTooltip(Blockly.Msg.ChatGpt_set_text_path);
  12922. this.setHelpUrl("");
  12923. }
  12924. };
  12925. Blockly.Python['cocopi_chatGpt_image_to_text_loading'] = function (block) {
  12926. var text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC);
  12927. var content = Blockly.Python.valueToCode(block, 'NAME1', Blockly.Python.ORDER_ATOMIC);
  12928. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  12929. sys.path.append("/root/")`
  12930. Blockly.Python.definitions_['v831_import_CocoPi_Gpt'] = `try:
  12931. from CocoPi import GPTrequest
  12932. except:
  12933. import sys
  12934. sys.path.append("/root/preset/drivers/pylib/")
  12935. from GPTrequest import GPTrequest`
  12936. Blockly.Python.definitions_['v831_import_CocoPi_Gpt_init'] = `gptInit = GPTrequest()`
  12937. var code = `gptInit.get_image_to_text(${text},${content})`;
  12938. // TODO: Change ORDER_NONE to the correct strength.
  12939. return [code, Blockly.Python.ORDER_ATOMIC];
  12940. };
  12941. Blockly.Blocks["cocopi_chatGpt_text_to_voice_loading"] = {
  12942. init: function () {
  12943. this.appendValueInput("NAME")
  12944. .setCheck(null)
  12945. .appendField(Blockly.Msg.ChatGpt_set_text_content);
  12946. this.appendDummyInput()
  12947. .appendField(Blockly.Msg.ChatGpt_get_voice_path_result);
  12948. this.setOutput(true, null);
  12949. this.setInputsInline(false);
  12950. this.setColour("#7e3cca");
  12951. this.setTooltip(Blockly.Msg.ChatGpt_get_voice_path_result);
  12952. this.setHelpUrl("");
  12953. }
  12954. };
  12955. Blockly.Python['cocopi_chatGpt_text_to_voice_loading'] = function (block) {
  12956. var text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC);
  12957. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  12958. sys.path.append("/root/")`
  12959. Blockly.Python.definitions_['v831_import_CocoPi_Gpt'] = `try:
  12960. from CocoPi import GPTrequest
  12961. except:
  12962. import sys
  12963. sys.path.append("/root/preset/drivers/pylib/")
  12964. from GPTrequest import GPTrequest`
  12965. Blockly.Python.definitions_['v831_import_CocoPi_Gpt_init'] = `gptInit = GPTrequest()`
  12966. var code = `gptInit.get_text_to_voice(${text})`;
  12967. // TODO: Change ORDER_NONE to the correct strength.
  12968. return [code, Blockly.Python.ORDER_ATOMIC];
  12969. };
  12970. Blockly.Blocks["cocopi_chatGpt_text_to_voice_result"] = {
  12971. init: function () {
  12972. this.appendDummyInput()
  12973. .appendField(Blockly.Msg.ChatGpt_get_voice_path_result);
  12974. this.setOutput(true, null);
  12975. this.setColour("#7e3cca");
  12976. this.setTooltip(Blockly.Msg.ChatGpt_get_voice_path_result);
  12977. this.setHelpUrl("");
  12978. }
  12979. };
  12980. Blockly.Python['cocopi_chatGpt_text_to_voice_result'] = function (block) {
  12981. var code = `gptInit.get_text_to_voice(VOICECONTENT)`;
  12982. return [code, Blockly.Python.ORDER_ATOMIC];
  12983. };
  12984. Blockly.Blocks["cocopi_chatGpt_voice_to_text_loading"] = {
  12985. init: function () {
  12986. this.appendValueInput("NAME")
  12987. .setCheck(null)
  12988. .appendField(Blockly.Msg.ChatGpt_set_voice_path);
  12989. this.appendDummyInput()
  12990. .appendField(Blockly.Msg.ChatGpt_get_voice_text_content_result);
  12991. this.setOutput(true, null);
  12992. this.setInputsInline(false)
  12993. this.setColour("#7e3cca");
  12994. this.setTooltip(Blockly.Msg.ChatGpt_set_voice_path);
  12995. this.setHelpUrl("");
  12996. }
  12997. };
  12998. Blockly.Python['cocopi_chatGpt_voice_to_text_loading'] = function (block) {
  12999. var text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC);
  13000. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  13001. sys.path.append("/root/")`
  13002. Blockly.Python.definitions_['v831_import_CocoPi_Gpt'] = `try:
  13003. from CocoPi import GPTrequest
  13004. except:
  13005. import sys
  13006. sys.path.append("/root/preset/drivers/pylib/")
  13007. from GPTrequest import GPTrequest`
  13008. Blockly.Python.definitions_['v831_import_CocoPi_Gpt_init'] = `gptInit = GPTrequest()`
  13009. var code = `gptInit.get_voice_to_text(${text})`;
  13010. // TODO: Change ORDER_NONE to the correct strength.
  13011. return [code, Blockly.Python.ORDER_ATOMIC];
  13012. };
  13013. Blockly.Blocks["cocopi_chatGpt_post_chatgpt_loading"] = {
  13014. init: function () {
  13015. this.appendValueInput("NAME")
  13016. .setCheck(null)
  13017. .appendField(Blockly.Msg.ChatGpt_set_problem_content);
  13018. this.appendDummyInput()
  13019. .appendField(Blockly.Msg.ChatGpt_get_problem_result);
  13020. this.setOutput(true, null);
  13021. this.setInputsInline(false)
  13022. this.setColour("#7e3cca");
  13023. this.setTooltip(Blockly.Msg.ChatGpt_set_problem_content);
  13024. this.setHelpUrl("");
  13025. }
  13026. };
  13027. Blockly.Python['cocopi_chatGpt_post_chatgpt_loading'] = function (block) {
  13028. var text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC);
  13029. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  13030. sys.path.append("/root/")`
  13031. Blockly.Python.definitions_['v831_import_CocoPi_Gpt'] = `try:
  13032. from CocoPi import GPTrequest
  13033. except:
  13034. import sys
  13035. sys.path.append("/root/preset/drivers/pylib/")
  13036. from GPTrequest import GPTrequest`
  13037. Blockly.Python.definitions_['v831_import_CocoPi_Gpt_init'] = `gptInit = GPTrequest()`
  13038. var code = `gptInit.get_post_chatgpt(${text})`;
  13039. // TODO: Change ORDER_NONE to the correct strength.
  13040. return [code, Blockly.Python.ORDER_ATOMIC];
  13041. };
  13042. Blockly.Blocks["cocopi_chatGpt_post_chatgpt_result"] = {
  13043. init: function () {
  13044. this.appendDummyInput()
  13045. .appendField(Blockly.Msg.ChatGpt_get_problem_result);
  13046. this.setOutput(true, null);
  13047. this.setColour("#7e3cca");
  13048. this.setTooltip(Blockly.Msg.ChatGpt_get_problem_result);
  13049. this.setHelpUrl("");
  13050. }
  13051. };
  13052. Blockly.Python['cocopi_chatGpt_post_chatgpt_result'] = function (block) {
  13053. var code = `gptInit.get_post_chatgpt(CHATGPTPROBLEM)`;
  13054. return [code, Blockly.Python.ORDER_ATOMIC];;
  13055. };
  13056. //
  13057. Blockly.Blocks["cocopi_chatGpt_result"] = {
  13058. init: function () {
  13059. this.appendDummyInput()
  13060. // .appendField(Blockly.Msg.ChatGpt_get_problem_result)
  13061. .appendField(new Blockly.FieldDropdown([
  13062. [Blockly.Msg.ChatGpt_get_img_path_result, "1"],
  13063. [Blockly.Msg.ChatGpt_get_text_content_result, "2"],
  13064. [Blockly.Msg.ChatGpt_get_voice_path_result, "3"],
  13065. [Blockly.Msg.ChatGpt_get_voice_text_content_result, "4"],
  13066. [Blockly.Msg.ChatGpt_get_problem_result, "5"],
  13067. [Blockly.Msg.ChatGpt_ai_agent_chat_result, "6"]
  13068. ]), "gpt_return");
  13069. this.setOutput(true, null);
  13070. this.setColour("#7e3cca");
  13071. this.setTooltip(Blockly.Msg.ChatGpt_get_problem_result);
  13072. this.setHelpUrl("");
  13073. }
  13074. };
  13075. Blockly.Python['cocopi_chatGpt_result'] = function (block) {
  13076. var queue_value = block.getFieldValue('gpt_return');
  13077. Blockly.Python.addVariable("CocoPiGPTRESULT", `CocoPiGPTRESULT = None`, true)
  13078. var res = ``;
  13079. if (queue_value == 5) {
  13080. res = `gptInit.get_post_chatgpt(CHATGPTPROBLEM)`
  13081. } else if (queue_value == 4) {
  13082. res = `gptInit.get_voice_to_text(VOICEPATH)`
  13083. } else if (queue_value == 3) {
  13084. res = `gptInit.get_text_to_voice(VOICECONTENT)`
  13085. } else if (queue_value == 2) {
  13086. res = `gptInit.get_image_to_text(IMAGEPATH,IMAGECONTENT)`
  13087. } else if (queue_value == 1) {
  13088. res = `gptInit.get_text_to_img(TextContent)`
  13089. }
  13090. else if (queue_value == 6) {
  13091. res = `gptInit.get_post_AI_Intelligence(AIAGENTCHATID,AIAGENTCHATMESSAGE)`
  13092. }
  13093. Blockly.Python.definitions_["CocoPiGPTRESULTFUNC"] = `def CocoPiGPTRESULTFUNC(res):
  13094. global CocoPiGPTRESULT
  13095. try:
  13096. CocoPiGPTRESULT = res
  13097. except:
  13098. CocoPiGPTRESULT = ""
  13099. return CocoPiGPTRESULT
  13100. `
  13101. var code = `CocoPiGPTRESULTFUNC(${res})`;
  13102. return [code, Blockly.Python.ORDER_ATOMIC];;
  13103. };
  13104. Blockly.Blocks["cocopi_chatGpt_AI_loading"] = {
  13105. init: function () {
  13106. this.appendValueInput("NAME")
  13107. .setCheck(null)
  13108. .appendField(Blockly.Msg.ChatGpt_ai_agent_chat_set_appID);
  13109. this.appendValueInput("NAME1")
  13110. .setCheck(null)
  13111. .appendField(Blockly.Msg.ChatGpt_set_problem_content);
  13112. this.appendDummyInput().appendField(Blockly.Msg.ChatGpt_ai_agent_chat_result)
  13113. this.setOutput(true, null);
  13114. this.setInputsInline(false);
  13115. this.setColour("#7e3cca");
  13116. this.setTooltip(Blockly.Msg.ChatGpt_set_text_path);
  13117. this.setHelpUrl("");
  13118. }
  13119. };
  13120. Blockly.Python['cocopi_chatGpt_AI_loading'] = function (block) {
  13121. var text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC);
  13122. var content = Blockly.Python.valueToCode(block, 'NAME1', Blockly.Python.ORDER_ATOMIC);
  13123. var text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC);
  13124. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  13125. sys.path.append("/root/")`
  13126. Blockly.Python.definitions_['v831_import_CocoPi_Gpt'] = `try:
  13127. from CocoPi import GPTrequest
  13128. except:
  13129. import sys
  13130. sys.path.append("/root/preset/drivers/pylib/")
  13131. from GPTrequest import GPTrequest`
  13132. Blockly.Python.definitions_['v831_import_CocoPi_Gpt_init'] = `gptInit = GPTrequest()`
  13133. var code = `gptInit.get_post_AI_Intelligence(${text},${content})`
  13134. return [code, Blockly.Python.ORDER_ATOMIC];
  13135. };
  13136. //
  13137. Blockly.Blocks["cocopi_chatGpt_AI_loading_more"] = {
  13138. init: function () {
  13139. this.appendValueInput("NAME")
  13140. .setCheck(null)
  13141. .appendField(Blockly.Msg.ChatGpt_ai_agent_chat_set_appID_more);
  13142. this.appendValueInput("NAME1")
  13143. .setCheck(null)
  13144. .appendField(Blockly.Msg.ChatGpt_set_problem_content);
  13145. this.appendDummyInput().appendField(Blockly.Msg.ChatGpt_ai_agent_chat_result_more)
  13146. this.setOutput(true, null);
  13147. this.setInputsInline(false);
  13148. this.setColour("#7e3cca");
  13149. this.setTooltip(Blockly.Msg.ChatGpt_set_text_path);
  13150. this.setHelpUrl("");
  13151. }
  13152. };
  13153. Blockly.Python['cocopi_chatGpt_AI_loading_more'] = function (block) {
  13154. var text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC);
  13155. var content = Blockly.Python.valueToCode(block, 'NAME1', Blockly.Python.ORDER_ATOMIC);
  13156. var text = Blockly.Python.valueToCode(block, 'NAME', Blockly.Python.ORDER_ATOMIC);
  13157. Blockly.Python.definitions_['v831_import_sys'] = `import sys
  13158. sys.path.append("/root/")`
  13159. Blockly.Python.definitions_['v831_import_CocoPi_Gpt'] = `try:
  13160. from CocoPi import GPTrequest
  13161. except:
  13162. import sys
  13163. sys.path.append("/root/preset/drivers/pylib/")
  13164. from GPTrequest import GPTrequest`
  13165. Blockly.Python.definitions_['v831_import_CocoPi_Gpt_init'] = `gptInit = GPTrequest()`
  13166. var code = `gptInit.cocopi_chatGpt_AI_loading_more(${text},${content})`
  13167. // Blockly.Python.definitions_['v831_import_requests'] = `import requests`
  13168. // Blockly.Python.definitions_['v831_import_json'] = `import json`
  13169. // Blockly.Python.definitions_['v831_import_os'] = `import os`
  13170. // Blockly.Python.definitions_['v831_import_time'] = `import time`
  13171. // Blockly.Python.addFunction("cocopi_chatGpt_AI_loading_more", `def cocopi_chatGpt_AI_loading_more(appID,content):
  13172. // url = "https://gpt4.cocorobo.cn/handle_muti_agent"
  13173. // headers={
  13174. // "Content-Type":"application/json",
  13175. // }
  13176. // cmd="cat /sys/class/sunxi_info/sys_info"
  13177. // rec=os.popen(cmd).read()
  13178. // hwMac=rec[rec.find("sunxi_serial : ")+20:rec.find("sunxi_serial : ")+52]
  13179. // params = {
  13180. // "muti_agent_id":appID,
  13181. // "message":content,
  13182. // "userId":"602def61-005d-11ee-91d8-005056b86db5",
  13183. // "session_name":"0606"
  13184. // }
  13185. // textContent = ""
  13186. // res = requests.post(url, headers=headers, data=json.dumps(params))
  13187. // try:
  13188. // textContent = json.loads(res.text)["FunctionResponse"]["response"]
  13189. // except:
  13190. // textContent = ""
  13191. // return textContent
  13192. // `, true)
  13193. return [code, Blockly.Python.ORDER_ATOMIC];
  13194. };
  13195. Blockly.Blocks.tuple_create_with_items_inserts = {
  13196. init: function () {
  13197. this.setHelpUrl(Blockly.Msg.TUPLE_CREATE_WITH_ITEMS_INSERT_HELPURL);
  13198. this.setColour("#ee783a");
  13199. this.itemCount_ = 3;
  13200. this.updateShape_();
  13201. this.setOutput(!0);
  13202. this.setMutator(new Blockly.Mutator(["tuple_create_with_items"]));
  13203. this.setTooltip(Blockly.Msg.TUPLE_CREATE_WITH_ITEMS_INSERT_TOOLTIP)
  13204. },
  13205. mutationToDom: function () {
  13206. var a = document.createElement("mutation");
  13207. a.setAttribute("items", this.itemCount_);
  13208. return a
  13209. },
  13210. domToMutation: function (a) {
  13211. this.itemCount_ = parseInt(a.getAttribute("items"), 10);
  13212. this.updateShape_()
  13213. },
  13214. decompose: function (a) {
  13215. var b = a.newBlock("tuple_create_with_containers");
  13216. b.initSvg();
  13217. for (var c = b.getInput("STACK").connection, e = 0; e < this.itemCount_; e++) {
  13218. var d = a.newBlock("tuple_create_with_items");
  13219. d.initSvg();
  13220. c.connect(d.previousConnection);
  13221. c = d.nextConnection
  13222. }
  13223. return b
  13224. },
  13225. compose: function (a) {
  13226. var b = a.getInputTargetBlock("STACK");
  13227. for (a = []; b;)
  13228. a.push(b.valueConnection_),
  13229. b = b.nextConnection && b.nextConnection.targetBlock();
  13230. for (b = 0; b < this.itemCount_; b++) {
  13231. var c = this.getInput("ADD" + b).connection.targetConnection;
  13232. c && -1 == a.indexOf(c) && c.disconnect()
  13233. }
  13234. this.itemCount_ = a.length;
  13235. this.updateShape_();
  13236. for (b = 0; b < this.itemCount_; b++)
  13237. Blockly.Mutator.reconnect(a[b], this, "ADD" + b)
  13238. },
  13239. saveConnections: function (a) {
  13240. a = a.getInputTargetBlock("STACK");
  13241. for (var b = 0; a;) {
  13242. var c = this.getInput("ADD" + b);
  13243. a.valueConnection_ = c && c.connection.targetConnection;
  13244. b++;
  13245. a = a.nextConnection && a.nextConnection.targetBlock()
  13246. }
  13247. },
  13248. updateShape_: function () {
  13249. this.itemCount_ && this.getInput("EMPTY") ? this.removeInput("EMPTY") : this.itemCount_ || this.getInput("EMPTY") || this.appendDummyInput("EMPTY").appendField(Blockly.Msg.TUPLE_CREATE_EMPTY_TITLE);
  13250. for (var a = 0; a < this.itemCount_; a++)
  13251. if (!this.getInput("ADD" + a)) {
  13252. var b = this.appendValueInput("ADD" + a);
  13253. 0 == a && b.appendField(Blockly.Msg.TUPLE_CREATE_WITH_INPUT_WITH)
  13254. }
  13255. for (; this.getInput("ADD" + a);)
  13256. this.removeInput("ADD" + a),
  13257. a++
  13258. }
  13259. };
  13260. Blockly.Blocks.tuple_create_with_items = {
  13261. init: function () {
  13262. this.setColour("#ee783a");
  13263. this.appendDummyInput().appendField(Blockly.Msg.TUPLE_CREATE_WITH_ITEM_TITLE);
  13264. this.setPreviousStatement(!0);
  13265. this.setNextStatement(!0);
  13266. this.setTooltip(Blockly.Msg.TUPLE_CREATE_WITH_ITEM_TOOLTIP);
  13267. this.contextMenu = !1
  13268. }
  13269. };
  13270. Blockly.Blocks.tuple_create_with_containers = {
  13271. init: function () {
  13272. this.setColour("#ee783a");
  13273. this.appendDummyInput().appendField(Blockly.Msg.TUPLE_CREATE_WITH_CONTAINER_TITLE_ADD);
  13274. this.appendStatementInput("STACK");
  13275. this.setTooltip(Blockly.Msg.TUPLE_CREATE_WITH_CONTAINER_TOOLTIP);
  13276. this.contextMenu = !1
  13277. }
  13278. };
  13279. Blockly.Python.tuple_create_with_items_inserts = function (a) {
  13280. for (var b = Array(a.itemCount_), c = 0; c < a.itemCount_; c++)
  13281. b[c] = Blockly.Python.valueToCode(a, "ADD" + c, Blockly.Python.ORDER_NONE) || "None";
  13282. return [1 == a.itemCount_ ? "(" + b[0] + ",)" : "(" + b.join(", ") + ")", Blockly.Python.ORDER_ATOMIC]
  13283. };