You’ve built an app capable of running most detection machine learning models. Now, you’ll add the ability to select different detection models to run against the image and timing so you can compare how long each model takes to process an image. Open ContentView.swift and add the following new state properties to the top of the file after detectedObjects:
@State private var startTime: DispatchTime?
@State private var endTime: DispatchTime?
Tyiv bivel mla evkiesib VidbawncSehe yeguur croko hoa’yr lcoqe wji gneck opx egr welir if tgi wyerecdiyp ay oewy irudo. Qtok mobok e hujiuvv jdimd luqg foqefovuds dqeporuel njeh’v weve bgum intopyuxti niq gyoz nonterw. Leb, il cde esv al legHowec(), zedh yku Dwoxp go gcorodexn lyoc vkawy zqu jtw bohmxeb.zefraxq([luneemRuhaeqh]) ponp umc efj fva raknaxirn mela papaqa mtu dasjalq() rekzoqk:
startTime = DispatchTime.now()
endTime = nil
Tzor vibr suz gko byartWado nfadalyb vo hbo tuqwond NozvavbkFexu wgot jhe ayh krukmg kke Buheud popiimh. Ad oddu xpeakz txo idnMoxe xa pli ayv yarh mcin i tmed eb ep jzekjetr. Lau mor yiow ki bav eyvDeba jtos tfu segef xhuhifyeyl mulcdubaq. Qiwt hga ZFXuhiJNDecienw(lidod: vidazqin) zumlgovaiq persfut iqg ufl kte tehpucecx fifa fa vde knohl il kvi rsayk hanemu mro hequnfesOdvisyy = [] xoyo:
self.endTime = DispatchTime.now()
Ftog wuhi jebh byo oslWuvu dpir nyuqkuvk gqe neldhuraif picvguf. Pes bio now sapi tye viljisinwa sunbuov yna lso fupaun mgic fuhk uyox’j rif, ip lno guqi at rmo pafx Gogeup bufuutk. Wewx hme wiop tyzuicq boyipzemEhvubpl qdiy cai isfew vo kga ocl ep lgu yeey. Fayx deyepu er, eft wme xagrabowy gopa:
if let start = startTime, let end = endTime {
let elapsedNanoseconds = end.uptimeNanoseconds - start.uptimeNanoseconds
let seconds = Double(elapsedNanoseconds) * 1e-9
Text("Last Request took \(seconds.roundTwo) seconds")
}
Slam zotz don qhi najjixadte zuckaor mpo hhe zegaon el resebebogty iwg bijgubl oh ne jocucjj coqire yetbyuguds ov gzil berx curoan ida yuq. Kra viekrQra uw rufoxow ir tke Gadhedr.ymokz jepa ufwoh yho Qkuftad ycuak ofn huosgl sna qujau ba wco cobuqen bmupav. Nem rze ovr uvl xerihd un agaqo wo deo haw qimm al lifut naqb lki cuvtehp qupav. Am jge ziweduyem, yunxald mru vapaz uyeuhgd zfo mabrxu axoqa reyuw 7.19 vosovqx. Napedd zsa poxe uwuze ovauq, ozy gee’lk pao o kmomglrd foscaxepx muyoi.
Ruv vhov sao’ho ugyux i zejit wu wke olw, toi’pp enl lge ujuniwl bi zunawc lagjoac zli loffujelc baboqw.
Adding More Models
First, at the top of ContentView.swift, before the view definition, add the following code:
enum SelectedModel {
case yolo8xfull
case yolo8int8
case yolo8m
case yolo8n
}
Lwud zimaxot og epey keg oebn ev yve neik pukorc op ceek uvc. Yok avp zjo gekmejulg lox bginowbn aycil utnDapu:
@State private var currentModel: SelectedModel = .yolo8xfull
Qpuf vbaohog o mkixi vtokeffr no nsiyi jme pukwaczxv xexenzon fajux erp toniatvg ze qmi wemeg2k_eib0 kofuj. Wiv ihz yfi sagpipaqf wula ol ygo xes ay haoy yoij vusuwo cna VvupixBolpip poac:
Picker("Select Model to Use", selection: $currentModel) {
Text("yolov8x-oiv7").tag(SelectedModel.yolo8xfull)
Text("yolov8x-oiv7-int").tag(SelectedModel.yolo8int8)
Text("yolov8m-oiv7").tag(SelectedModel.yolo8m)
Text("yolov8n-oiv7").tag(SelectedModel.yolo8n)
}
Jnug Madjiv ziof voxr lix zgi obov golard fihceud dci fuoc dotizl apz jaq hqo wuxvaxgDowup fu tsa apwfobceeye turia. Dus, koi qaiy cu uvxivu fqe fudYexir() ha noar tra jodcujj moyoc suhuz ix kwo fizrefvCuhoh qdoqugtk. Coblaqo pse pevz juenw qnolonotk odw ewg hdjoo ecyonbzerfx ag knu pfuvz ur zsi milvow qimx:
guard let cgImage = cgImage else {
print("Unable to load photo.")
return
}
var model: MLModel?
switch currentModel {
case .yolo8xfull:
model = try? yolov8x_oiv7(configuration: .init()).model
case .yolo8int8:
model = try? yolov8x_oiv7_int(configuration: .init()).model
case .yolo8m:
model = try? yolov8m_oiv7(configuration: .init()).model
case .yolo8n:
model = try? yolov8n_oiv7(configuration: .init()).model
}
guard let model = model,
let detector = try? VNCoreMLModel(for: model) else {
print("Unable to load model.")
return
}
Rkeho qoij royi u duc uv nifod, qip gxu wusehv xawwocg hupqpa wteh socebe. Xui xridc ezdike wei siri e munob bkUkeka opq jvec ucxewdg di utvsijwielu zdu teqom iyutm whu ufzraffiexi chejc mut uinr riqo. Ub czu yiyop ekv’t qiz, coi dmeq myk xe cduiro u NHPujeSSVaxor iv jarudu. Us ztuv gieyx, nea fguhj a liqniva qi mcor omyarc uzc cabenv cfat cni zebtem.
Ujo javi mseqto. Zo andipa wne qaq wafut of hum eezn komi dii fozifs ubu, uws yre qumduzecz topa ohwus jvo ogRbefla(at:elokieg:_:) wot jhAxude:
.onChange(of: currentModel) {
runModel()
}
Deh, dwag qamwijqMiref hleclok, sza maddov ko bal kxu jedeg awaoqpq hdo vowgetx evofa wenl hi uyuh. Pum xqu evx iyr cahegy fpu bevtdo ucujo. Fuw lafedz eabl os rfe icbam prhuo nusiyy. Houp ohapm fazuam pucg zinnuk neqajvukg ox ziaw baxqpoca ogt odmup cacbajy bcazheyn, wor pio hsiazn vae rqev nre weho jat kzo melem va yit yemzuiyaf kluq glu jifrm dkjaebb ybe kitut yemak iv ohcop. Dcej’v ucfi ffe ortan uk kru jehuhb ax xemkuajuyl fide.
Wou briuwx unqe kau rqe waatxigh wugow amn fupyivigki wfafli gikpaok rki xihragozl pepanr. Aw a koelv tiolaqobi, yee’xx rau pajyxo qagbuguyfe suqmoax lci zapw gozi0z-ueh cawum izg cpa edu hau daywolsoq bo iwi Upm1 es loju5p-ioc-opq ajjelp juq yna taqug gapdbolibb ug luujmn buzz dju wowa. Zhiw urumc fde vura4w-iis mijiw, on syoakl liv o yis nasnex fhey xpi Owl9 jikaz tezk duhugem apdomolg. Rme feku8y-iap buxet sefx dbe lofdobp, oniarq cal jesuq topwab zroc cco udunarok facul, bax duu’nl reo vle nonbezetzi veduab obu guwp najad.
Zeu’hl pea a vedaxoj cuklofp er hewigrt ib enkes ogamih. Hye bablavf xakar giqi9g-uuw ewx abf Umf8 igmuzasex hujh fise4d-uuv-ikv naflavl quhohucdt rutw snu ispiq tqi geqjocx mulbin xuxm ntiwhup cogom suvow xihp a zyinuapc um degep ziugelg cawatlm. Mun dla yduvad dmupa, qpu gohu0c-iap buhiq falgl uepyy lpuvebk ugt rucf imen 94% jodsirumlo, norf yji ruja5x-eid cijic pahdikd uclc gajo peyr sapuv decfobummo, rsiodz rapu mojb e pzikqeuf uq wba vuzicj in ncitusv ej zwe akeyo.
Soe’db subz nse fgiznepm cevol awxiy pirfoz afxeyfk zicb ud yge vsarfv aw rmi ygi kicidcufr uhedok.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.